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/ioutil"
"net/http"
"path"
)
// DNSRecord a DNS record
type DNSRecord struct {
Type string `json:"type"`
Name string `json:"name"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Data string `json:"data"`
Priority int `json:"priority,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)
if err != nil {
return err
}
resource := path.Clean(fmt.Sprintf("/v1/domains/%s/records/TXT/%s", domainZone, recordName))
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 {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("could not create record %v; Status: %v; Body: %s", string(body), resp.StatusCode, string(bodyBytes))
}
return nil
}

View file

@ -29,7 +29,6 @@ const (
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
)
// Config is used to configure the creation of the DNSProvider
@ -38,7 +37,6 @@ type Config struct {
APISecret string
PropagationTimeout time.Duration
PollingInterval time.Duration
SequenceInterval time.Duration
TTL int
HTTPClient *http.Client
}
@ -49,7 +47,6 @@ func NewDefaultConfig() *Config {
TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
HTTPClient: &http.Client{
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
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
domainZone, err := d.getZone(fqdn)
if err != nil {
return err
return fmt.Errorf("godaddy: failed to get zone: %w", err)
}
recordName := d.extractRecordName(fqdn, domainZone)
rec := []DNSRecord{
{
records, err := d.getRecords(domainZone, "TXT", recordName)
if err != nil {
return fmt.Errorf("godaddy: failed to get TXT records: %w", err)
}
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 d.updateRecords(rec, domainZone, recordName)
return nil
}
// 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 {
fqdn, _ := dns01.GetRecord(domain, keyAuth)
fqdn, value := dns01.GetRecord(domain, keyAuth)
domainZone, err := d.getZone(fqdn)
if err != nil {
return err
return fmt.Errorf("godaddy: failed to get zone: %w", err)
}
recordName := d.extractRecordName(fqdn, domainZone)
rec := []DNSRecord{
{
Type: "TXT",
Name: recordName,
Data: "null",
},
records, err := d.getRecords(domainZone, "TXT", recordName)
if err != nil {
return fmt.Errorf("godaddy: failed to get TXT records: %w", err)
}
return d.updateRecords(rec, domainZone, recordName)
if len(records) == 0 {
return nil
}
// Sequential All DNS challenges for this provider will be resolved sequentially.
// Returns the interval between each iteration.
func (d *DNSProvider) Sequential() time.Duration {
return d.config.SequenceInterval
allTxtRecords, err := d.getRecords(domainZone, "TXT", "")
if err != nil {
return fmt.Errorf("godaddy: failed to get all TXT records: %w", err)
}
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 {