1
0
mirror of https://github.com/go-acme/lego.git synced 2024-11-29 09:52:07 +02:00

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",
"stylecheck",
"godox",
"gomnd",
]
[issues]
@ -121,3 +122,6 @@
[[issues.exclude-rules]]
path = "providers/dns/checkdomain/client.go"
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) {
// Retry if the nonce was invalidated
case *acme.NonceError:
log.Infof("nonce error retry: %s", err)
return err
default:
cancel()
@ -104,7 +103,11 @@ func (a *Core) retrievablePost(uri string, content []byte, response interface{})
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 {
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
func (p *DNSProvider) Present(domain, token, keyAuth string) error {
domainID, err := p.getDomainIDByName(domain)
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
domainID, err := d.getDomainIDByName(domain)
if err != nil {
return fmt.Errorf("checkdomain: %v", err)
}
err = p.checkNameservers(domainID)
err = d.checkNameservers(domainID)
if err != nil {
return fmt.Errorf("checkdomain: %v", err)
}
name, value := dns01.GetRecord(domain, keyAuth)
err = p.createRecord(domainID, &Record{
err = d.createRecord(domainID, &Record{
Name: name,
TTL: p.config.TTL,
TTL: d.config.TTL,
Type: "TXT",
Value: value,
})
@ -122,31 +122,31 @@ func (p *DNSProvider) Present(domain, token, keyAuth string) error {
}
// CleanUp removes the TXT record previously created
func (p *DNSProvider) CleanUp(domain, token, keyAuth string) error {
domainID, err := p.getDomainIDByName(domain)
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
domainID, err := d.getDomainIDByName(domain)
if err != nil {
return fmt.Errorf("checkdomain: %v", err)
}
err = p.checkNameservers(domainID)
err = d.checkNameservers(domainID)
if err != nil {
return fmt.Errorf("checkdomain: %v", err)
}
name, value := dns01.GetRecord(domain, keyAuth)
err = p.deleteTXTRecord(domainID, name, value)
err = d.deleteTXTRecord(domainID, name, value)
if err != nil {
return fmt.Errorf("checkdomain: %v", err)
}
p.domainIDMu.Lock()
delete(p.domainIDMapping, name)
p.domainIDMu.Unlock()
d.domainIDMu.Lock()
delete(d.domainIDMapping, name)
d.domainIDMu.Unlock()
return nil
}
func (p *DNSProvider) Timeout() (timeout, interval time.Duration) {
return p.config.PropagationTimeout, p.config.PollingInterval
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
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
p.domainIDMu.Lock()
id, ok := p.domainIDMapping[name]
p.domainIDMu.Unlock()
d.domainIDMu.Lock()
id, ok := d.domainIDMapping[name]
d.domainIDMu.Unlock()
if ok {
return id, nil
}
// Find out by querying API
domains, err := p.listDomains()
domains, err := d.listDomains()
if err != nil {
return domainNotFound, err
}
@ -116,9 +116,9 @@ func (p *DNSProvider) getDomainIDByName(name string) (int, error) {
// Linear search over all registered domains
for _, domain := range domains {
if domain.Name == name || strings.HasSuffix(name, "."+domain.Name) {
p.domainIDMu.Lock()
p.domainIDMapping[name] = domain.ID
p.domainIDMu.Unlock()
d.domainIDMu.Lock()
d.domainIDMapping[name] = domain.ID
d.domainIDMu.Unlock()
return domain.ID, nil
}
@ -127,8 +127,8 @@ func (p *DNSProvider) getDomainIDByName(name string) (int, error) {
return domainNotFound, fmt.Errorf("domain not found")
}
func (p *DNSProvider) listDomains() ([]*Domain, error) {
req, err := p.makeRequest(http.MethodGet, "/v1/domains", http.NoBody)
func (d *DNSProvider) listDomains() ([]*Domain, error) {
req, err := d.makeRequest(http.MethodGet, "/v1/domains", http.NoBody)
if err != nil {
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()
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)
}
@ -166,22 +166,22 @@ func (p *DNSProvider) listDomains() ([]*Domain, error) {
return domainList, nil
}
func (p *DNSProvider) getNameserverInfo(domainID int) (*NameserverResponse, error) {
req, err := p.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers", domainID), http.NoBody)
func (d *DNSProvider) getNameserverInfo(domainID int) (*NameserverResponse, error) {
req, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers", domainID), http.NoBody)
if err != nil {
return nil, err
}
res := &NameserverResponse{}
if err := p.sendRequest(req, res); err != nil {
if err := d.sendRequest(req, res); err != nil {
return nil, err
}
return res, nil
}
func (p *DNSProvider) checkNameservers(domainID int) error {
info, err := p.getNameserverInfo(domainID)
func (d *DNSProvider) checkNameservers(domainID int) error {
info, err := d.getNameserverInfo(domainID)
if err != nil {
return err
}
@ -203,35 +203,35 @@ func (p *DNSProvider) checkNameservers(domainID int) error {
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)
if err != nil {
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 {
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.
// 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.
func (p *DNSProvider) deleteTXTRecord(domainID int, recordName, recordValue string) error {
domainInfo, err := p.getDomainInfo(domainID)
func (d *DNSProvider) deleteTXTRecord(domainID int, recordName, recordValue string) error {
domainInfo, err := d.getDomainInfo(domainID)
if err != nil {
return err
}
nsInfo, err := p.getNameserverInfo(domainID)
nsInfo, err := d.getNameserverInfo(domainID)
if err != nil {
return err
}
allRecords, err := p.listRecords(domainID, "")
allRecords, err := d.listRecords(domainID, "")
if err != nil {
return err
}
@ -256,17 +256,17 @@ func (p *DNSProvider) deleteTXTRecord(domainID int, recordName, recordValue stri
recordsToKeep = append(recordsToKeep, record)
}
return p.replaceRecords(domainID, recordsToKeep)
return d.replaceRecords(domainID, recordsToKeep)
}
func (p *DNSProvider) getDomainInfo(domainID int) (*DomainResponse, error) {
req, err := p.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d", domainID), http.NoBody)
func (d *DNSProvider) getDomainInfo(domainID int) (*DomainResponse, error) {
req, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d", domainID), http.NoBody)
if err != nil {
return nil, err
}
var res DomainResponse
err = p.sendRequest(req, &res)
err = d.sendRequest(req, &res)
if err != nil {
return nil, err
}
@ -274,8 +274,8 @@ func (p *DNSProvider) getDomainInfo(domainID int) (*DomainResponse, error) {
return &res, nil
}
func (p *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, error) {
req, err := p.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), http.NoBody)
func (d *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, error) {
req, err := d.makeRequest(http.MethodGet, fmt.Sprintf("/v1/domains/%d/nameservers/records", domainID), http.NoBody)
if err != nil {
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()
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)
}
@ -312,18 +312,18 @@ func (p *DNSProvider) listRecords(domainID int, recordType string) ([]*Record, e
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)
if err != nil {
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 {
return err
}
return p.sendRequest(req, nil)
return d.sendRequest(req, nil)
}
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
}
func (p *DNSProvider) makeRequest(method, resource string, body io.Reader) (*http.Request, error) {
uri, err := p.config.Endpoint.Parse(resource)
func (d *DNSProvider) makeRequest(method, resource string, body io.Reader) (*http.Request, error) {
uri, err := d.config.Endpoint.Parse(resource)
if err != nil {
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("Authorization", "Bearer "+p.config.Token)
req.Header.Set("Authorization", "Bearer "+d.config.Token)
if method != http.MethodGet {
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
}
func (p *DNSProvider) sendRequest(req *http.Request, result interface{}) error {
resp, err := p.config.HTTPClient.Do(req)
func (d *DNSProvider) sendRequest(req *http.Request, result interface{}) error {
resp, err := d.config.HTTPClient.Do(req)
if err != nil {
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 {
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode <= http.StatusNetworkAuthenticationRequired {
if resp.StatusCode >= http.StatusBadRequest {
if resp.Body == nil {
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 {
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode <= http.StatusNetworkAuthenticationRequired {
if resp.StatusCode >= http.StatusBadRequest {
if resp.Body == nil {
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",
apiKey: "foo",
handlers: map[string]http.HandlerFunc{
"/": http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
fmt.Println(req.URL)
}),
"/" + hostedZone + "/txt": mockHandlerCreateRecord,
},
},