Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Fix miekgdns resolver LookupSRV function to work with CNAME records too #5716

Merged
16 changes: 16 additions & 0 deletions pkg/discovery/dns/miekgdns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ type Resolver struct {
}

func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error) {
return r.lookupSRV(service, proto, name, 1, 8)
}

func (r *Resolver) lookupSRV(service, proto, name string, currIteration, maxIterations int) (cname string, addrs []*net.SRV, err error) {
Atharva-Shinde marked this conversation as resolved.
Show resolved Hide resolved
// We want to protect from infinite loops when resolving DNS records recursively.
if currIteration > maxIterations {
return "", nil, errors.Errorf("maximum number of recursive iterations reached (%d)", maxIterations)
}
var target string
if service == "" && proto == "" {
target = name
Expand All @@ -41,6 +49,14 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (
Priority: addr.Priority,
Port: addr.Port,
})
case *dns.CNAME:
var resp []*net.SRV
Atharva-Shinde marked this conversation as resolved.
Show resolved Hide resolved
// Recursively resolve it.
_, resp, err := r.lookupSRV("", "", addr.Target, currIteration+1, maxIterations)
if err != nil {
return "", nil, errors.Wrapf(err, "recursively resolve %s", addr.Target)
}
addrs = append(addrs, resp...)
default:
return "", nil, errors.Errorf("invalid SRV response record %s", record)
}
Expand Down