From ad16c5d00fd90904b1b0434197898e4aecff5cfa Mon Sep 17 00:00:00 2001 From: Atharva Shinde Date: Wed, 21 Sep 2022 18:03:30 +0530 Subject: [PATCH 1/2] feat: cname handling in lookupsrv --- pkg/discovery/dns/miekgdns/resolver.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/discovery/dns/miekgdns/resolver.go b/pkg/discovery/dns/miekgdns/resolver.go index e8e847b50a..16f6f349c5 100644 --- a/pkg/discovery/dns/miekgdns/resolver.go +++ b/pkg/discovery/dns/miekgdns/resolver.go @@ -19,7 +19,16 @@ type Resolver struct { ResolvConf string } -func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, addrs []*net.SRV, err error) { +func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) (cname string, resp []*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, resp []*net.SRV, err 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) + } + var target string if service == "" && proto == "" { target = name @@ -35,18 +44,24 @@ func (r *Resolver) LookupSRV(ctx context.Context, service, proto, name string) ( for _, record := range response.Answer { switch addr := record.(type) { case *dns.SRV: - addrs = append(addrs, &net.SRV{ + resp = append(resp, &net.SRV{ Weight: addr.Weight, Target: addr.Target, Priority: addr.Priority, Port: addr.Port, }) + case *dns.CNAME: + _, addrs, err := r.lookupSRV(service, proto, addr.Target, currIteration+1, maxIterations) + if err != nil { + return "", nil, errors.Wrapf(err, "recursively resolve %s", addr.Target) + } + resp = append(resp, addrs...) default: return "", nil, errors.Errorf("invalid SRV response record %s", record) } } - return "", addrs, nil + return "", resp, nil } func (r *Resolver) LookupIPAddr(_ context.Context, host string) ([]net.IPAddr, error) { From 644683fcff2a27916f564dcaff34c2e8e5135dc7 Mon Sep 17 00:00:00 2001 From: Atharva Shinde Date: Wed, 21 Sep 2022 18:25:27 +0530 Subject: [PATCH 2/2] feat: cname handling in lookupsrv --- pkg/discovery/dns/miekgdns/resolver.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/discovery/dns/miekgdns/resolver.go b/pkg/discovery/dns/miekgdns/resolver.go index 16f6f349c5..a4d8876bd1 100644 --- a/pkg/discovery/dns/miekgdns/resolver.go +++ b/pkg/discovery/dns/miekgdns/resolver.go @@ -51,7 +51,8 @@ func (r *Resolver) lookupSRV(service, proto, name string, currIteration, maxIter Port: addr.Port, }) case *dns.CNAME: - _, addrs, err := r.lookupSRV(service, proto, addr.Target, currIteration+1, maxIterations) + // Recursively resolve it. + _, addrs, err := r.lookupSRV(service, proto, target, currIteration+1, maxIterations) if err != nil { return "", nil, errors.Wrapf(err, "recursively resolve %s", addr.Target) }