feat: allow parallel solve. (#1114)

This commit is contained in:
Ludovic Fernandez 2020-04-17 22:38:13 +02:00 committed by GitHub
parent 1ac1986687
commit c0bc316a5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 31 deletions

View file

@ -7,35 +7,64 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path"
) )
// DNSRecord a DNS record // DNSRecord a DNS record
type DNSRecord struct { type DNSRecord struct {
Type string `json:"type"` Name string `json:"name,omitempty"`
Name string `json:"name"` Type string `json:"type,omitempty"`
Data string `json:"data"` Data string `json:"data"`
Priority int `json:"priority,omitempty"` Priority int `json:"priority,omitempty"`
TTL int `json:"ttl,omitempty"` TTL int `json:"ttl,omitempty"`
} }
func (d *DNSProvider) updateRecords(records []DNSRecord, domainZone string, recordName string) error { func (d *DNSProvider) getRecords(domainZone string, rType string, recordName string) ([]DNSRecord, error) {
resource := path.Clean(fmt.Sprintf("/v1/domains/%s/records/%s/%s", domainZone, rType, recordName))
resp, err := d.makeRequest(http.MethodGet, resource, nil)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("could not get records: Domain: %s; Record: %s, Status: %v; Body: %s",
domainZone, recordName, resp.StatusCode, string(bodyBytes))
}
var records []DNSRecord
err = json.NewDecoder(resp.Body).Decode(&records)
if err != nil {
return nil, err
}
return records, nil
}
func (d *DNSProvider) updateTxtRecords(records []DNSRecord, domainZone string, recordName string) error {
body, err := json.Marshal(records) body, err := json.Marshal(records)
if err != nil { if err != nil {
return err return err
} }
resource := path.Clean(fmt.Sprintf("/v1/domains/%s/records/TXT/%s", domainZone, recordName))
var resp *http.Response var resp *http.Response
resp, err = d.makeRequest(http.MethodPut, fmt.Sprintf("/v1/domains/%s/records/TXT/%s", domainZone, recordName), bytes.NewReader(body)) resp, err = d.makeRequest(http.MethodPut, resource, bytes.NewReader(body))
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close() defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body) bodyBytes, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("could not create record %v; Status: %v; Body: %s", string(body), resp.StatusCode, string(bodyBytes)) return fmt.Errorf("could not create record %v; Status: %v; Body: %s", string(body), resp.StatusCode, string(bodyBytes))
} }
return nil return nil
} }

View file

@ -29,7 +29,6 @@ const (
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL" EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
) )
// Config is used to configure the creation of the DNSProvider // Config is used to configure the creation of the DNSProvider
@ -38,7 +37,6 @@ type Config struct {
APISecret string APISecret string
PropagationTimeout time.Duration PropagationTimeout time.Duration
PollingInterval time.Duration PollingInterval time.Duration
SequenceInterval time.Duration
TTL int TTL int
HTTPClient *http.Client HTTPClient *http.Client
} }
@ -49,7 +47,6 @@ func NewDefaultConfig() *Config {
TTL: env.GetOrDefaultInt(EnvTTL, minTTL), TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
HTTPClient: &http.Client{ HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
}, },
@ -103,48 +100,86 @@ func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
// Present creates a TXT record to fulfill the dns-01 challenge // Present creates a TXT record to fulfill the dns-01 challenge
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth) fqdn, value := dns01.GetRecord(domain, keyAuth)
domainZone, err := d.getZone(fqdn) domainZone, err := d.getZone(fqdn)
if err != nil { if err != nil {
return err return fmt.Errorf("godaddy: failed to get zone: %w", err)
} }
recordName := d.extractRecordName(fqdn, domainZone) recordName := d.extractRecordName(fqdn, domainZone)
rec := []DNSRecord{
{ records, err := d.getRecords(domainZone, "TXT", recordName)
Type: "TXT", if err != nil {
Name: recordName, return fmt.Errorf("godaddy: failed to get TXT records: %w", err)
Data: value,
TTL: d.config.TTL,
},
} }
return d.updateRecords(rec, domainZone, recordName) var newRecords []DNSRecord
for _, record := range records {
if record.Data != "" {
newRecords = append(newRecords, record)
}
}
record := DNSRecord{
Type: "TXT",
Name: recordName,
Data: value,
TTL: d.config.TTL,
}
newRecords = append(newRecords, record)
err = d.updateTxtRecords(newRecords, domainZone, recordName)
if err != nil {
return fmt.Errorf("godaddy: failed to add TXT record: %w", err)
}
return nil
} }
// CleanUp sets null value in the TXT DNS record as GoDaddy has no proper DELETE record method // CleanUp sets null value in the TXT DNS record as GoDaddy has no proper DELETE record method
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, _ := dns01.GetRecord(domain, keyAuth) fqdn, value := dns01.GetRecord(domain, keyAuth)
domainZone, err := d.getZone(fqdn) domainZone, err := d.getZone(fqdn)
if err != nil { if err != nil {
return err return fmt.Errorf("godaddy: failed to get zone: %w", err)
} }
recordName := d.extractRecordName(fqdn, domainZone) recordName := d.extractRecordName(fqdn, domainZone)
rec := []DNSRecord{
{ records, err := d.getRecords(domainZone, "TXT", recordName)
Type: "TXT", if err != nil {
Name: recordName, return fmt.Errorf("godaddy: failed to get TXT records: %w", err)
Data: "null",
},
} }
return d.updateRecords(rec, domainZone, recordName) if len(records) == 0 {
} return nil
}
// Sequential All DNS challenges for this provider will be resolved sequentially. allTxtRecords, err := d.getRecords(domainZone, "TXT", "")
// Returns the interval between each iteration. if err != nil {
func (d *DNSProvider) Sequential() time.Duration { return fmt.Errorf("godaddy: failed to get all TXT records: %w", err)
return d.config.SequenceInterval }
var recordsKeep []DNSRecord
for _, record := range allTxtRecords {
if record.Data != value && record.Data != "" {
recordsKeep = append(recordsKeep, record)
}
}
// GoDaddy API don't provide a way to delete a record, an "empty" record must be added.
if len(recordsKeep) == 0 {
emptyRecord := DNSRecord{Name: "empty", Data: ""}
recordsKeep = append(recordsKeep, emptyRecord)
}
err = d.updateTxtRecords(recordsKeep, domainZone, "")
if err != nil {
return fmt.Errorf("godaddy: failed to remove TXT record: %w", err)
}
return nil
} }
func (d *DNSProvider) extractRecordName(fqdn, domain string) string { func (d *DNSProvider) extractRecordName(fqdn, domain string) string {