From 3a46680b7347e6e2c904ee8f928c2246d8907180 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Fri, 21 Sep 2018 17:28:50 +0200 Subject: [PATCH] Fix: gcloud wildcard (#643) * fix: gcloud wildcard. * refactor: minor changes. --- acme/client.go | 2 +- acme/dns_challenge.go | 6 +++++- providers/dns/gcloud/googlecloud.go | 23 +++++++++++++++-------- providers/dns/gcloud/googlecloud_test.go | 3 +-- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/acme/client.go b/acme/client.go index 72e5f63f..12fd5b37 100644 --- a/acme/client.go +++ b/acme/client.go @@ -593,7 +593,7 @@ func (c *Client) solveChallengeForAuthz(authorizations []authorization) error { } } - // for all valid presolvers, first submit the challenges so they have max time to propigate + // for all valid presolvers, first submit the challenges so they have max time to propagate for _, item := range authSolvers { authz := item.authz i := item.challengeIndex diff --git a/acme/dns_challenge.go b/acme/dns_challenge.go index 73956625..63094064 100644 --- a/acme/dns_challenge.go +++ b/acme/dns_challenge.go @@ -30,6 +30,9 @@ const ( // DefaultPollingInterval default polling interval DefaultPollingInterval = 2 * time.Second + + // DefaultTTL default TTL + DefaultTTL = 120 ) var defaultNameservers = []string{ @@ -67,7 +70,7 @@ func DNS01Record(domain, keyAuth string) (fqdn string, value string, ttl int) { keyAuthShaBytes := sha256.Sum256([]byte(keyAuth)) // base64URL encoding without padding value = base64.RawURLEncoding.EncodeToString(keyAuthShaBytes[:sha256.Size]) - ttl = 120 + ttl = DefaultTTL fqdn = fmt.Sprintf("_acme-challenge.%s.", domain) return } @@ -149,6 +152,7 @@ func checkDNSPropagation(fqdn, value string) (bool, error) { if err != nil { return false, err } + if r.Rcode == dns.RcodeSuccess { // If we see a CNAME here then use the alias for _, rr := range r.Answer { diff --git a/providers/dns/gcloud/googlecloud.go b/providers/dns/gcloud/googlecloud.go index dbc35904..d8df7071 100644 --- a/providers/dns/gcloud/googlecloud.go +++ b/providers/dns/gcloud/googlecloud.go @@ -131,26 +131,33 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { return fmt.Errorf("googlecloud: %v", err) } + // Look for existing records. + existing, err := d.findTxtRecords(zone, fqdn) + if err != nil { + return fmt.Errorf("googlecloud: %v", err) + } + rec := &dns.ResourceRecordSet{ Name: fqdn, Rrdatas: []string{value}, Ttl: int64(d.config.TTL), Type: "TXT", } - change := &dns.Change{ - Additions: []*dns.ResourceRecordSet{rec}, - } - // Look for existing records. - existing, err := d.findTxtRecords(zone, fqdn) - if err != nil { - return fmt.Errorf("googlecloud: %v", err) - } + change := &dns.Change{} + if len(existing) > 0 { // Attempt to delete the existing records when adding our new one. change.Deletions = existing + + // Append existing TXT record data to the new TXT record data + for _, value := range existing { + rec.Rrdatas = append(rec.Rrdatas, value.Rrdatas...) + } } + change.Additions = []*dns.ResourceRecordSet{rec} + chg, err := d.client.Changes.Create(d.config.Project, zone, change).Do() if err != nil { return fmt.Errorf("googlecloud: %v", err) diff --git a/providers/dns/gcloud/googlecloud_test.go b/providers/dns/gcloud/googlecloud_test.go index 4a4aae57..9bc9330c 100644 --- a/providers/dns/gcloud/googlecloud_test.go +++ b/providers/dns/gcloud/googlecloud_test.go @@ -5,11 +5,10 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "golang.org/x/net/context" "golang.org/x/oauth2/google" "google.golang.org/api/dns/v1" - - "github.com/stretchr/testify/assert" ) var (