traefik/vendor/github.com/go-acme/lego/providers/dns/pdns/pdns.go

183 lines
4.3 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// Package pdns implements a DNS provider for solving the DNS-01 challenge using PowerDNS nameserver.
2017-02-07 21:33:23 +00:00
package pdns
import (
"bytes"
"encoding/json"
2018-09-17 13:16:03 +00:00
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
"net/http"
"net/url"
"time"
2019-03-14 10:04:04 +00:00
"github.com/go-acme/lego/challenge/dns01"
"github.com/go-acme/lego/log"
"github.com/go-acme/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
2018-09-17 13:16:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
APIKey string
Host *url.URL
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
HTTPClient *http.Client
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
2019-01-07 17:30:06 +00:00
TTL: env.GetOrDefaultInt("PDNS_TTL", dns01.DefaultTTL),
2018-09-17 13:16:03 +00:00
PropagationTimeout: env.GetOrDefaultSecond("PDNS_PROPAGATION_TIMEOUT", 120*time.Second),
PollingInterval: env.GetOrDefaultSecond("PDNS_POLLING_INTERVAL", 2*time.Second),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("PDNS_HTTP_TIMEOUT", 30*time.Second),
},
}
}
2018-05-31 07:30:04 +00:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface
2017-02-07 21:33:23 +00:00
type DNSProvider struct {
apiVersion int
2018-09-17 13:16:03 +00:00
config *Config
2017-02-07 21:33:23 +00:00
}
// NewDNSProvider returns a DNSProvider instance configured for pdns.
// Credentials must be passed in the environment variable:
// PDNS_API_URL and PDNS_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("PDNS_API_KEY", "PDNS_API_URL")
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("pdns: %v", err)
2018-07-03 10:44:04 +00:00
}
hostURL, err := url.Parse(values["PDNS_API_URL"])
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("pdns: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.Host = hostURL
config.APIKey = values["PDNS_API_KEY"]
return NewDNSProviderConfig(config)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// NewDNSProviderConfig return a DNSProvider instance configured for pdns.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("pdns: the configuration of the DNS provider is nil")
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
if config.APIKey == "" {
return nil, fmt.Errorf("pdns: API key missing")
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
if config.Host == nil || config.Host.Host == "" {
return nil, fmt.Errorf("pdns: API URL missing")
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
d := &DNSProvider{config: config}
2018-07-03 10:44:04 +00:00
apiVersion, err := d.getAPIVersion()
if err != nil {
2018-09-17 13:16:03 +00:00
log.Warnf("pdns: failed to get API version %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
d.apiVersion = apiVersion
2017-02-07 21:33:23 +00:00
2018-07-03 10:44:04 +00:00
return d, nil
2017-02-07 21:33:23 +00:00
}
// Timeout returns the timeout and interval to use when checking for DNS
// propagation. Adjusting here to cope with spikes in propagation times.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
2018-09-17 13:16:03 +00:00
return d.config.PropagationTimeout, d.config.PollingInterval
2017-02-07 21:33:23 +00:00
}
2018-10-10 14:28:04 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(domain, keyAuth)
2018-07-03 10:44:04 +00:00
zone, err := d.getHostedZone(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("pdns: %v", err)
2017-02-07 21:33:23 +00:00
}
name := fqdn
// pre-v1 API wants non-fqdn
2018-07-03 10:44:04 +00:00
if d.apiVersion == 0 {
2019-01-07 17:30:06 +00:00
name = dns01.UnFqdn(fqdn)
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
rec := Record{
2017-02-07 21:33:23 +00:00
Content: "\"" + value + "\"",
Disabled: false,
// pre-v1 API
Type: "TXT",
Name: name,
2018-09-17 13:16:03 +00:00
TTL: d.config.TTL,
2017-02-07 21:33:23 +00:00
}
rrsets := rrSets{
RRSets: []rrSet{
2018-05-31 07:30:04 +00:00
{
2017-02-07 21:33:23 +00:00
Name: name,
ChangeType: "REPLACE",
Type: "TXT",
Kind: "Master",
2018-09-17 13:16:03 +00:00
TTL: d.config.TTL,
2019-01-07 17:30:06 +00:00
Records: []Record{rec},
2017-02-07 21:33:23 +00:00
},
},
}
body, err := json.Marshal(rrsets)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("pdns: %v", err)
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
_, err = d.sendRequest(http.MethodPatch, zone.URL, bytes.NewReader(body))
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("pdns: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}
// CleanUp removes the TXT record matching the specified parameters
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, _ := dns01.GetRecord(domain, keyAuth)
2017-02-07 21:33:23 +00:00
2018-07-03 10:44:04 +00:00
zone, err := d.getHostedZone(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("pdns: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
set, err := d.findTxtRecord(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("pdns: %v", err)
2017-02-07 21:33:23 +00:00
}
rrsets := rrSets{
RRSets: []rrSet{
2018-05-31 07:30:04 +00:00
{
2017-02-07 21:33:23 +00:00
Name: set.Name,
Type: set.Type,
ChangeType: "DELETE",
},
},
}
body, err := json.Marshal(rrsets)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("pdns: %v", err)
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
_, err = d.sendRequest(http.MethodPatch, zone.URL, bytes.NewReader(body))
2018-09-17 13:16:03 +00:00
if err != nil {
return fmt.Errorf("pdns: %v", err)
}
return nil
2017-02-07 21:33:23 +00:00
}