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

Fix miekgdns resolver to work with CNAME records too #5271

Merged
merged 3 commits into from
Apr 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkg/discovery/dns/miekgdns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (
}

func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) {
return r.lookupIPAddr(ctx, host, 1, 8)
}

func (r *Resolver) lookupIPAddr(ctx context.Context, host string, currIteration, maxIterations int) ([]net.IPAddr, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the context from the signature entirely since it's not used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely, done!

// 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)
}

response, err := r.lookupWithSearchPath(host, dns.Type(dns.TypeAAAA))
if err != nil || len(response.Answer) == 0 {
// Ugly fallback to A lookup.
Expand All @@ -66,8 +75,15 @@ func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr,
resp = append(resp, net.IPAddr{IP: addr.A})
case *dns.AAAA:
resp = append(resp, net.IPAddr{IP: addr.AAAA})
case *dns.CNAME:
// Recursively resolve it.
addrs, err := r.lookupIPAddr(ctx, addr.Target, currIteration+1, maxIterations)
if err != nil {
return nil, errors.Wrapf(err, "failed to recursively resolve %s", addr.Target)
pracucci marked this conversation as resolved.
Show resolved Hide resolved
}
resp = append(resp, addrs...)
default:
return nil, errors.Errorf("invalid A or AAAA response record %s", record)
return nil, errors.Errorf("invalid A, AAAA or CNAME response record %s", record)
}
}
return resp, nil
Expand Down