From 68544d809d57582188939ce3b8ec1e82a7d6cb54 Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 7 Apr 2022 18:09:14 +0200 Subject: [PATCH 1/3] Fix miekgdns resolver to work with CNAME records too Signed-off-by: Marco Pracucci --- pkg/discovery/dns/miekgdns/resolver.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/discovery/dns/miekgdns/resolver.go b/pkg/discovery/dns/miekgdns/resolver.go index 0348967c2e..4caa0d2b1f 100644 --- a/pkg/discovery/dns/miekgdns/resolver.go +++ b/pkg/discovery/dns/miekgdns/resolver.go @@ -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) { + // 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. @@ -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) + } + 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 From 9873cab28053c0b2317bb8c8fa180824240186ca Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 7 Apr 2022 20:35:40 +0200 Subject: [PATCH 2/3] Remove unused context Signed-off-by: Marco Pracucci --- pkg/discovery/dns/miekgdns/resolver.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/discovery/dns/miekgdns/resolver.go b/pkg/discovery/dns/miekgdns/resolver.go index 4caa0d2b1f..a6a2b6cb7e 100644 --- a/pkg/discovery/dns/miekgdns/resolver.go +++ b/pkg/discovery/dns/miekgdns/resolver.go @@ -49,11 +49,11 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) ( return "", addrs, nil } -func (r *Resolver) LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error) { - return r.lookupIPAddr(ctx, host, 1, 8) +func (r *Resolver) LookupIPAddr(_ context.Context, host string) ([]net.IPAddr, error) { + return r.lookupIPAddr(host, 1, 8) } -func (r *Resolver) lookupIPAddr(ctx context.Context, host string, currIteration, maxIterations int) ([]net.IPAddr, error) { +func (r *Resolver) lookupIPAddr(host string, currIteration, maxIterations int) ([]net.IPAddr, error) { // 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) @@ -77,7 +77,7 @@ func (r *Resolver) lookupIPAddr(ctx context.Context, host string, currIteration, resp = append(resp, net.IPAddr{IP: addr.AAAA}) case *dns.CNAME: // Recursively resolve it. - addrs, err := r.lookupIPAddr(ctx, addr.Target, currIteration+1, maxIterations) + addrs, err := r.lookupIPAddr(addr.Target, currIteration+1, maxIterations) if err != nil { return nil, errors.Wrapf(err, "failed to recursively resolve %s", addr.Target) } From 58d5c123c6247567299b8efba2b39148fd3b5160 Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Thu, 7 Apr 2022 20:36:11 +0200 Subject: [PATCH 3/3] Update pkg/discovery/dns/miekgdns/resolver.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Pracucci Co-authored-by: Lucas Servén Marín --- pkg/discovery/dns/miekgdns/resolver.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/discovery/dns/miekgdns/resolver.go b/pkg/discovery/dns/miekgdns/resolver.go index a6a2b6cb7e..e8e847b50a 100644 --- a/pkg/discovery/dns/miekgdns/resolver.go +++ b/pkg/discovery/dns/miekgdns/resolver.go @@ -79,7 +79,7 @@ func (r *Resolver) lookupIPAddr(host string, currIteration, maxIterations int) ( // Recursively resolve it. addrs, err := r.lookupIPAddr(addr.Target, currIteration+1, maxIterations) if err != nil { - return nil, errors.Wrapf(err, "failed to recursively resolve %s", addr.Target) + return nil, errors.Wrapf(err, "recursively resolve %s", addr.Target) } resp = append(resp, addrs...) default: