Minors changes (#1059)

This commit is contained in:
Ludovic Fernandez 2020-02-10 18:20:52 +01:00 committed by GitHub
parent 8f349e5a5f
commit 2e30fd0ba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 59 deletions

View file

@ -33,6 +33,7 @@
"wsl", "wsl",
"stylecheck", "stylecheck",
"godox", "godox",
"gomnd",
] ]
[issues] [issues]
@ -121,3 +122,6 @@
[[issues.exclude-rules]] [[issues.exclude-rules]]
path = "providers/dns/checkdomain/client.go" path = "providers/dns/checkdomain/client.go"
text = "`payed` is a misspelling of `paid`" text = "`payed` is a misspelling of `paid`"
[[issues.exclude-rules]]
path = "providers/dns/namecheap/namecheap_test.go"
text = "cognitive complexity (\\d+) of func `TestDNSProvider_getHosts` is high"

View file

@ -93,7 +93,6 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
switch err.(type) { switch err.(type) {
// Retry if the nonce was invalidated // Retry if the nonce was invalidated
case *acme.NonceError: case *acme.NonceError:
log.Infof("nonce error retry: %s", err)
return err return err
default: default:
cancel() cancel()
@ -104,7 +103,11 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
return nil return nil
} }
err := backoff.Retry(operation, backoff.WithContext(bo, ctx)) notify := func(err error, duration time.Duration) {
log.Infof("retry due to: %v", err)
}
err := backoff.RetryNotify(operation, backoff.WithContext(bo, ctx), notify)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -94,22 +94,22 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
} }
// Present creates a TXT record to fulfill the dns-01 challenge // Present creates a TXT record to fulfill the dns-01 challenge
func (p *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
domainID, err := p.getDomainIDByName(domain) domainID, err := d.getDomainIDByName(domain)
if err != nil { if err != nil {
return fmt.Errorf("checkdomain: %v", err) return fmt.Errorf("checkdomain: %v", err)
} }
err = p.checkNameservers(domainID) err = d.checkNameservers(domainID)
if err != nil { if err != nil {
return fmt.Errorf("checkdomain: %v", err) return fmt.Errorf("checkdomain: %v", err)
} }
name, value := dns01.GetRecord(domain, keyAuth) name, value := dns01.GetRecord(domain, keyAuth)
err = p.createRecord(domainID, &Record{ err = d.createRecord(domainID, &Record{
Name: name, Name: name,
TTL: p.config.TTL, TTL: d.config.TTL,
Type: "TXT", Type: "TXT",
Value: value, Value: value,
}) })
@ -122,31 +122,31 @@ func (p *DNSProvider) Present(domain, token, keyAuth string) error {
} }
// CleanUp removes the TXT record previously created // CleanUp removes the TXT record previously created
func (p *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
domainID, err := p.getDomainIDByName(domain) domainID, err := d.getDomainIDByName(domain)
if err != nil { if err != nil {
return fmt.Errorf("checkdomain: %v", err) return fmt.Errorf("checkdomain: %v", err)
} }
err = p.checkNameservers(domainID) err = d.checkNameservers(domainID)
if err != nil { if err != nil {
return fmt.Errorf("checkdomain: %v", err) return fmt.Errorf("checkdomain: %v", err)
} }
name, value := dns01.GetRecord(domain, keyAuth) name, value := dns01.GetRecord(domain, keyAuth)
err = p.deleteTXTRecord(domainID, name, value) err = d.deleteTXTRecord(domainID, name, value)
if err != nil { if err != nil {
return fmt.Errorf("checkdomain: %v", err) return fmt.Errorf("checkdomain: %v", err)
} }
p.domainIDMu.Lock() d.domainIDMu.Lock()
delete(p.domainIDMapping, name) delete(d.domainIDMapping, name)
p.domainIDMu.Unlock() d.domainIDMu.Unlock()
return nil return nil
} }
func (p *DNSProvider) Timeout() (timeout, interval time.Duration) { func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
return p.config.PropagationTimeout, p.config.PollingInterval return d.config.PropagationTimeout, d.config.PollingInterval
} }

View file

@ -98,17 +98,17 @@ type (
} }
) )
func (p *DNSProvider) getDomainIDByName(name string) (int, error) { func (d *DNSProvider) getDomainIDByName(name string) (int, error) {
// Load from cache if exists // Load from cache if exists
p.domainIDMu.Lock() d.domainIDMu.Lock()
id, ok := p.domainIDMapping[name] id, ok := d.domainIDMapping[name]
p.domainIDMu.Unlock() d.domainIDMu.Unlock()
if ok { if ok {
return id, nil return id, nil
} }
// Find out by querying API // Find out by querying API
domains, err := p.listDomains() domains, err := d.listDomains()
if err != nil { if err != nil {
return domainNotFound, err return domainNotFound, err
} }
@ -116,9 +116,9 @@ func (p *DNSProvider) getDomainIDByName(name string) (int, error) {
// Linear search over all registered domains // Linear search over all registered domains
for _, domain := range domains { for _, domain := range domains {
if domain.Name == name || strings.HasSuffix(name, "."+domain.Name) { if domain.Name == name || strings.HasSuffix(name, "."+domain.Name) {
p.domainIDMu.Lock() d.domainIDMu.Lock()
p.domainIDMapping[name] = domain.ID d.domainIDMapping[name] = domain.ID
p.domainIDMu.Unlock() d.domainIDMu.Unlock()
return domain.ID, nil return domain.ID, nil
} }
@ -127,8 +127,8 @@ func (p *DNSProvider) getDomainIDByName(name string) (int, error) {
return domainNotFound, fmt.Errorf("domain not found") return domainNotFound, fmt.Errorf("domain not found")
} }
func (p *DNSProvider) listDomains() ([]*Domain, error) { func (d *DNSProvider) listDomains() ([]*Domain, error) {
req, err := p.makeRequest(http.MethodGet, "/v1/domains", http.NoBody) req, err := d.makeRequest(http.MethodGet, "/v1/domains", http.NoBody)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to make request: %v", err) return nil, fmt.Errorf("failed to make request: %v", err)
} }
@ -148,7 +148,7 @@ func (p *DNSProvider) listDomains() ([]*Domain, error) {
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
var res DomainListingResponse var res DomainListingResponse
if err := p.sendRequest(req, &res); err != nil { if err := d.sendRequest(req, &res); err != nil {
return nil, fmt.Errorf("failed to send domain listing request: %v", err) return nil, fmt.Errorf("failed to send domain listing request: %v", err)
} }
@ -166,22 +166,22 @@ func (p *DNSProvider) listDomains() ([]*Domain, error) {
return domainList, nil return domainList, nil
} }
func (p *DNSProvider) getNameserverInfo(domainID int) (*NameserverResponse, error) { func (d *DNSProvider) getNameserverInfo(domainID int) (*NameserverResponse, error) {
req, err := p.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers", domainID), http.NoBody) req, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers", domainID), http.NoBody)
if err != nil { if err != nil {
return nil, err return nil, err
} }
res := &NameserverResponse{} res := &NameserverResponse{}
if err := p.sendRequest(req, res); err != nil { if err := d.sendRequest(req, res); err != nil {
return nil, err return nil, err
} }
return res, nil return res, nil
} }
func (p *DNSProvider) checkNameservers(domainID int) error { func (d *DNSProvider) checkNameservers(domainID int) error {
info, err := p.getNameserverInfo(domainID) info, err := d.getNameserverInfo(domainID)
if err != nil { if err != nil {
return err return err
} }
@ -203,35 +203,35 @@ func (p *DNSProvider) checkNameservers(domainID int) error {
return nil return nil
} }
func (p *DNSProvider) createRecord(domainID int, record *Record) error { func (d *DNSProvider) createRecord(domainID int, record *Record) error {
bs, err := json.Marshal(record) bs, err := json.Marshal(record)
if err != nil { if err != nil {
return fmt.Errorf("encoding record failed: %v", err) return fmt.Errorf("encoding record failed: %v", err)
} }
req, err := p.makeRequest(http.MethodPost, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), bytes.NewReader(bs)) req, err := d.makeRequest(http.MethodPost, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), bytes.NewReader(bs))
if err != nil { if err != nil {
return err return err
} }
return p.sendRequest(req, nil) return d.sendRequest(req, nil)
} }
// Checkdomain doesn't seem provide a way to delete records but one can replace all records at once. // Checkdomain doesn't seem provide a way to delete records but one can replace all records at once.
// The current solution is to fetch all records and then use that list minus the record deleted as the new record list. // The current solution is to fetch all records and then use that list minus the record deleted as the new record list.
// TODO: Simplify this function once Checkdomain do provide the functionality. // TODO: Simplify this function once Checkdomain do provide the functionality.
func (p *DNSProvider) deleteTXTRecord(domainID int, recordName, recordValue string) error { func (d *DNSProvider) deleteTXTRecord(domainID int, recordName, recordValue string) error {
domainInfo, err := p.getDomainInfo(domainID) domainInfo, err := d.getDomainInfo(domainID)
if err != nil { if err != nil {
return err return err
} }
nsInfo, err := p.getNameserverInfo(domainID) nsInfo, err := d.getNameserverInfo(domainID)
if err != nil { if err != nil {
return err return err
} }
allRecords, err := p.listRecords(domainID, "") allRecords, err := d.listRecords(domainID, "")
if err != nil { if err != nil {
return err return err
} }
@ -256,17 +256,17 @@ func (p *DNSProvider) deleteTXTRecord(domainID int, recordName, recordValue stri
recordsToKeep = append(recordsToKeep, record) recordsToKeep = append(recordsToKeep, record)
} }
return p.replaceRecords(domainID, recordsToKeep) return d.replaceRecords(domainID, recordsToKeep)
} }
func (p *DNSProvider) getDomainInfo(domainID int) (*DomainResponse, error) { func (d *DNSProvider) getDomainInfo(domainID int) (*DomainResponse, error) {
req, err := p.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d", domainID), http.NoBody) req, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d", domainID), http.NoBody)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var res DomainResponse var res DomainResponse
err = p.sendRequest(req, &res) err = d.sendRequest(req, &res)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -274,8 +274,8 @@ func (p *DNSProvider) getDomainInfo(domainID int) (*DomainResponse, error) {
return &res, nil return &res, nil
} }
func (p *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, error) { func (d *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, error) {
req, err := p.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), http.NoBody) req, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), http.NoBody)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to make request: %v", err) return nil, fmt.Errorf("failed to make request: %v", err)
} }
@ -295,7 +295,7 @@ func (p *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, e
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
var res RecordListingResponse var res RecordListingResponse
if err := p.sendRequest(req, &res); err != nil { if err := d.sendRequest(req, &res); err != nil {
return nil, fmt.Errorf("failed to send record listing request: %v", err) return nil, fmt.Errorf("failed to send record listing request: %v", err)
} }
@ -312,18 +312,18 @@ func (p *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, e
return recordList, nil return recordList, nil
} }
func (p *DNSProvider) replaceRecords(domainID int, records []*Record) error { func (d *DNSProvider) replaceRecords(domainID int, records []*Record) error {
bs, err := json.Marshal(records) bs, err := json.Marshal(records)
if err != nil { if err != nil {
return fmt.Errorf("encoding record failed: %v", err) return fmt.Errorf("encoding record failed: %v", err)
} }
req, err := p.makeRequest(http.MethodPut, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), bytes.NewReader(bs)) req, err := d.makeRequest(http.MethodPut, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), bytes.NewReader(bs))
if err != nil { if err != nil {
return err return err
} }
return p.sendRequest(req, nil) return d.sendRequest(req, nil)
} }
func skipRecord(recordName, recordValue string, record *Record, nsInfo *NameserverResponse) bool { func skipRecord(recordName, recordValue string, record *Record, nsInfo *NameserverResponse) bool {
@ -348,8 +348,8 @@ func skipRecord(recordName, recordValue string, record *Record, nsInfo *Nameserv
return false return false
} }
func (p *DNSProvider) makeRequest(method, resource string, body io.Reader) (*http.Request, error) { func (d *DNSProvider) makeRequest(method, resource string, body io.Reader) (*http.Request, error) {
uri, err := p.config.Endpoint.Parse(resource) uri, err := d.config.Endpoint.Parse(resource)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -360,7 +360,7 @@ func (p *DNSProvider) makeRequest(method, resource string, body io.Reader) (*htt
} }
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+p.config.Token) req.Header.Set("Authorization", "Bearer "+d.config.Token)
if method != http.MethodGet { if method != http.MethodGet {
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
} }
@ -368,8 +368,8 @@ func (p *DNSProvider) makeRequest(method, resource string, body io.Reader) (*htt
return req, nil return req, nil
} }
func (p *DNSProvider) sendRequest(req *http.Request, result interface{}) error { func (d *DNSProvider) sendRequest(req *http.Request, result interface{}) error {
resp, err := p.config.HTTPClient.Do(req) resp, err := d.config.HTTPClient.Do(req)
if err != nil { if err != nil {
return err return err
} }

View file

@ -180,7 +180,7 @@ func (c *Client) do(req *http.Request, to interface{}) (*http.Response, error) {
} }
func checkResponse(resp *http.Response) error { func checkResponse(resp *http.Response) error {
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode <= http.StatusNetworkAuthenticationRequired { if resp.StatusCode >= http.StatusBadRequest {
if resp.Body == nil { if resp.Body == nil {
return fmt.Errorf("request failed with status code %d and empty body", resp.StatusCode) return fmt.Errorf("request failed with status code %d and empty body", resp.StatusCode)
} }

View file

@ -180,7 +180,7 @@ func (c *Client) do(req *http.Request, to interface{}) (*http.Response, error) {
} }
func checkResponse(resp *http.Response) error { func checkResponse(resp *http.Response) error {
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode <= http.StatusNetworkAuthenticationRequired { if resp.StatusCode >= http.StatusBadRequest {
if resp.Body == nil { if resp.Body == nil {
return fmt.Errorf("request failed with status code %d and empty body", resp.StatusCode) return fmt.Errorf("request failed with status code %d and empty body", resp.StatusCode)
} }

View file

@ -153,9 +153,6 @@ func TestDNSProvider_Present(t *testing.T) {
username: "bar", username: "bar",
apiKey: "foo", apiKey: "foo",
handlers: map[string]http.HandlerFunc{ handlers: map[string]http.HandlerFunc{
"/": http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
fmt.Println(req.URL)
}),
"/" + hostedZone + "/txt": mockHandlerCreateRecord, "/" + hostedZone + "/txt": mockHandlerCreateRecord,
}, },
}, },