From 9008ec69493c278e1e1da187a95552f101d9cf5b Mon Sep 17 00:00:00 2001 From: xenolf Date: Fri, 11 Mar 2016 03:20:25 +0100 Subject: [PATCH] Move functions from dns package back into ACME. --- acme/dns_challenge.go | 38 +++++++++++++++------ acme/dns_challenge_test.go | 2 +- providers/dns/cloudflare/cloudflare.go | 7 ++-- providers/dns/dnsimple/dnsimple.go | 3 +- providers/dns/rfc2136/rfc2136.go | 2 +- providers/dns/route53/route53.go | 3 +- providers/dns/utils.go | 47 -------------------------- 7 files changed, 35 insertions(+), 67 deletions(-) delete mode 100644 providers/dns/utils.go diff --git a/acme/dns_challenge.go b/acme/dns_challenge.go index 49c1a264..361c76c5 100644 --- a/acme/dns_challenge.go +++ b/acme/dns_challenge.go @@ -69,7 +69,7 @@ func (s *dnsChallenge) Solve(chlng challenge, domain string) error { logf("[INFO][%s] Checking DNS record propagation...", domain) - err = waitFor(30, 2, func() (bool, error) { + err = WaitFor(30, 2, func() (bool, error) { return preCheckDNS(fqdn, value) }) if err != nil { @@ -160,7 +160,7 @@ func dnsQuery(fqdn string, rtype uint16, nameserver string, recursive bool) (in func lookupNameservers(fqdn string) ([]string, error) { var authoritativeNss []string - zone, err := findZoneByFqdn(fqdn, recursiveNameserver) + zone, err := FindZoneByFqdn(fqdn, recursiveNameserver) if err != nil { return nil, err } @@ -182,8 +182,8 @@ func lookupNameservers(fqdn string) ([]string, error) { return nil, fmt.Errorf("Could not determine authoritative nameservers") } -// findZoneByFqdn determines the zone of the given fqdn -func findZoneByFqdn(fqdn, nameserver string) (string, error) { +// FindZoneByFqdn determines the zone of the given fqdn +func FindZoneByFqdn(fqdn, nameserver string) (string, error) { // Do we have it cached? if zone, ok := fqdnToZone[fqdn]; ok { return zone, nil @@ -208,8 +208,8 @@ func findZoneByFqdn(fqdn, nameserver string) (string, error) { if soa, ok := ans.(*dns.SOA); ok { zone := soa.Hdr.Name // If we ended up on one of the TLDs, it means the domain did not exist. - publicsuffix, _ := publicsuffix.PublicSuffix(unFqdn(zone)) - if publicsuffix == unFqdn(zone) { + publicsuffix, _ := publicsuffix.PublicSuffix(UnFqdn(zone)) + if publicsuffix == UnFqdn(zone) { return "", fmt.Errorf("Could not determine zone authoritatively") } fqdnToZone[fqdn] = zone @@ -223,8 +223,8 @@ func findZoneByFqdn(fqdn, nameserver string) (string, error) { if soa, ok := ns.(*dns.SOA); ok { zone := soa.Hdr.Name // If we ended up on one of the TLDs, it means the domain did not exist. - publicsuffix, _ := publicsuffix.PublicSuffix(unFqdn(zone)) - if publicsuffix == unFqdn(zone) { + publicsuffix, _ := publicsuffix.PublicSuffix(UnFqdn(zone)) + if publicsuffix == UnFqdn(zone) { return "", fmt.Errorf("Could not determine zone authoritatively") } fqdnToZone[fqdn] = zone @@ -239,8 +239,26 @@ func clearFqdnCache() { fqdnToZone = map[string]string{} } -// waitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds. -func waitFor(timeout, interval int, f func() (bool, error)) error { +// ToFqdn converts the name into a fqdn appending a trailing dot. +func ToFqdn(name string) string { + n := len(name) + if n == 0 || name[n-1] == '.' { + return name + } + return name + "." +} + +// UnFqdn converts the fqdn into a name removing the trailing dot. +func UnFqdn(name string) string { + n := len(name) + if n != 0 && name[n-1] == '.' { + return name[:n-1] + } + return name +} + +// WaitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds. +func WaitFor(timeout, interval int, f func() (bool, error)) error { var lastErr string timeup := time.After(time.Duration(timeout) * time.Second) for { diff --git a/acme/dns_challenge_test.go b/acme/dns_challenge_test.go index 54d5e095..760c7991 100644 --- a/acme/dns_challenge_test.go +++ b/acme/dns_challenge_test.go @@ -167,7 +167,7 @@ func TestCheckAuthoritativeNssErr(t *testing.T) { func TestWaitForTimeout(t *testing.T) { c := make(chan error) go func() { - err := waitFor(3, 1, func() (bool, error) { + err := WaitFor(3, 1, func() (bool, error) { return false, nil }) c <- err diff --git a/providers/dns/cloudflare/cloudflare.go b/providers/dns/cloudflare/cloudflare.go index 2b9337c4..d531dfc6 100644 --- a/providers/dns/cloudflare/cloudflare.go +++ b/providers/dns/cloudflare/cloudflare.go @@ -11,7 +11,6 @@ import ( "time" "github.com/xenolf/lego/acme" - "github.com/xenolf/lego/providers/dns" ) // CloudFlareAPIURL represents the API endpoint to call. @@ -50,7 +49,7 @@ func (c *DNSProviderCloudFlare) Present(domain, token, keyAuth string) error { rec := cloudFlareRecord{ Type: "TXT", - Name: dns.UnFqdn(fqdn), + Name: acme.UnFqdn(fqdn), Content: value, TTL: 120, } @@ -105,7 +104,7 @@ func (c *DNSProviderCloudFlare) getHostedZoneID(fqdn string) (string, error) { var hostedZone HostedZone for _, zone := range zones { - name := dns.ToFqdn(zone.Name) + name := acme.ToFqdn(zone.Name) if strings.HasSuffix(fqdn, name) { if len(zone.Name) > len(hostedZone.Name) { hostedZone = zone @@ -137,7 +136,7 @@ func (c *DNSProviderCloudFlare) findTxtRecord(fqdn string) (*cloudFlareRecord, e } for _, rec := range records { - if rec.Name == dns.UnFqdn(fqdn) && rec.Type == "TXT" { + if rec.Name == acme.UnFqdn(fqdn) && rec.Type == "TXT" { return &rec, nil } } diff --git a/providers/dns/dnsimple/dnsimple.go b/providers/dns/dnsimple/dnsimple.go index 8b4f95e1..0c9f03e2 100644 --- a/providers/dns/dnsimple/dnsimple.go +++ b/providers/dns/dnsimple/dnsimple.go @@ -7,7 +7,6 @@ import ( "github.com/weppos/dnsimple-go/dnsimple" "github.com/xenolf/lego/acme" - "github.com/xenolf/lego/providers/dns" ) // DNSProviderDNSimple is an implementation of the DNSProvider interface. @@ -124,7 +123,7 @@ func (c *DNSProviderDNSimple) newTxtRecord(zone, fqdn, value string, ttl int) *d } func (c *DNSProviderDNSimple) extractRecordName(fqdn, domain string) string { - name := dns.UnFqdn(fqdn) + name := acme.UnFqdn(fqdn) if idx := strings.Index(name, "."+domain); idx != -1 { return name[:idx] } diff --git a/providers/dns/rfc2136/rfc2136.go b/providers/dns/rfc2136/rfc2136.go index b2832d50..df303001 100644 --- a/providers/dns/rfc2136/rfc2136.go +++ b/providers/dns/rfc2136/rfc2136.go @@ -60,7 +60,7 @@ func (r *DNSProviderRFC2136) CleanUp(domain, token, keyAuth string) error { func (r *DNSProviderRFC2136) changeRecord(action, fqdn, value string, ttl int) error { // Find the zone for the given fqdn - zone, err := findZoneByFqdn(fqdn, r.nameserver) + zone, err := acme.FindZoneByFqdn(fqdn, r.nameserver) if err != nil { return err } diff --git a/providers/dns/route53/route53.go b/providers/dns/route53/route53.go index fc8cd8cc..ce3bb975 100644 --- a/providers/dns/route53/route53.go +++ b/providers/dns/route53/route53.go @@ -8,7 +8,6 @@ import ( "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/route53" "github.com/xenolf/lego/acme" - "github.com/xenolf/lego/providers/dns" ) // DNSProviderRoute53 is an implementation of the DNSProvider interface @@ -71,7 +70,7 @@ func (r *DNSProviderRoute53) changeRecord(action, fqdn, value string, ttl int) e return err } - return dns.WaitFor(90, 5, func() (bool, error) { + return acme.WaitFor(90, 5, func() (bool, error) { status, err := r.client.GetChange(resp.ChangeInfo.ID) if err != nil { return false, err diff --git a/providers/dns/utils.go b/providers/dns/utils.go deleted file mode 100644 index 9df2a8bb..00000000 --- a/providers/dns/utils.go +++ /dev/null @@ -1,47 +0,0 @@ -package dns - -import ( - "fmt" - "time" -) - -// ToFqdn converts the name into a fqdn appending a trailing dot. -func ToFqdn(name string) string { - n := len(name) - if n == 0 || name[n-1] == '.' { - return name - } - return name + "." -} - -// UnFqdn converts the fqdn into a name removing the trailing dot. -func UnFqdn(name string) string { - n := len(name) - if n != 0 && name[n-1] == '.' { - return name[:n-1] - } - return name -} - -// WaitFor polls the given function 'f', once every 'interval' seconds, up to 'timeout' seconds. -func WaitFor(timeout, interval int, f func() (bool, error)) error { - var lastErr string - timeup := time.After(time.Duration(timeout) * time.Second) - for { - select { - case <-timeup: - return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr) - default: - } - - stop, err := f() - if stop { - return nil - } - if err != nil { - lastErr = err.Error() - } - - time.Sleep(time.Duration(interval) * time.Second) - } -}