traefik/vendor/github.com/xenolf/lego/providers/dns/ns1/ns1.go

163 lines
4.7 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// Package ns1 implements a DNS provider for solving the DNS-01 challenge using NS1 DNS.
2017-02-07 21:33:23 +00:00
package ns1
import (
2018-09-17 13:16:03 +00:00
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
"net/http"
2018-07-23 15:30:03 +00:00
"strings"
2017-02-07 21:33:23 +00:00
"time"
2019-01-07 17:30:06 +00:00
"github.com/xenolf/lego/challenge/dns01"
2018-10-10 14:28:04 +00:00
"github.com/xenolf/lego/log"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
"gopkg.in/ns1/ns1-go.v2/rest"
"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
)
2018-09-17 13:16:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
APIKey string
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("NS1_TTL", dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond("NS1_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond("NS1_POLLING_INTERVAL", dns01.DefaultPollingInterval),
2018-09-17 13:16:03 +00:00
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("NS1_HTTP_TIMEOUT", 10*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 {
client *rest.Client
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 NS1.
// Credentials must be passed in the environment variables: NS1_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("NS1_API_KEY")
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("ns1: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-07-03 10:44:04 +00:00
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIKey = values["NS1_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 NS1.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("ns1: 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("ns1: credentials missing")
}
2017-02-07 21:33:23 +00:00
2018-09-17 13:16:03 +00:00
client := rest.NewClient(config.HTTPClient, rest.SetAPIKey(config.APIKey))
return &DNSProvider{client: client, config: config}, nil
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)
2017-02-07 21:33:23 +00:00
2018-09-21 16:38:02 +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("ns1: %v", err)
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
record, _, err := d.client.Records.Get(zone.Zone, dns01.UnFqdn(fqdn), "TXT")
2018-10-10 14:28:04 +00:00
// Create a new record
if err == rest.ErrRecordMissing || record == nil {
log.Infof("Create a new record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, fqdn)
2019-01-07 17:30:06 +00:00
record = dns.NewRecord(zone.Zone, dns01.UnFqdn(fqdn), "TXT")
2018-10-10 14:28:04 +00:00
record.TTL = d.config.TTL
record.Answers = []*dns.Answer{{Rdata: []string{value}}}
_, err = d.client.Records.Create(record)
if err != nil {
return fmt.Errorf("ns1: failed to create record [zone: %q, fqdn: %q]: %v", zone.Zone, fqdn, err)
}
return nil
}
if err != nil {
return fmt.Errorf("ns1: failed to get the existing record: %v", err)
}
// Update the existing records
record.Answers = append(record.Answers, &dns.Answer{Rdata: []string{value}})
log.Infof("Update an existing record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, fqdn, domain)
_, err = d.client.Records.Update(record)
if err != nil {
return fmt.Errorf("ns1: failed to update record [zone: %q, fqdn: %q]: %v", zone.Zone, fqdn, err)
2017-02-07 21:33:23 +00:00
}
return nil
}
// 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-09-21 16:38:02 +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("ns1: %v", err)
2017-02-07 21:33:23 +00:00
}
2019-01-07 17:30:06 +00:00
name := dns01.UnFqdn(fqdn)
2018-07-03 10:44:04 +00:00
_, err = d.client.Records.Delete(zone.Zone, name, "TXT")
2018-10-10 14:28:04 +00:00
if err != nil {
return fmt.Errorf("ns1: failed to delete record [zone: %q, domain: %q]: %v", zone.Zone, name, err)
}
return nil
2018-09-17 13:16:03 +00:00
}
// Timeout returns the timeout and interval to use when checking for DNS propagation.
// Adjusting here to cope with spikes in propagation times.
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
return d.config.PropagationTimeout, d.config.PollingInterval
2017-02-07 21:33:23 +00:00
}
2018-09-21 16:38:02 +00:00
func (d *DNSProvider) getHostedZone(fqdn string) (*dns.Zone, error) {
authZone, err := getAuthZone(fqdn)
2018-07-23 15:30:03 +00:00
if err != nil {
2018-09-21 16:38:02 +00:00
return nil, fmt.Errorf("failed to extract auth zone from fqdn %q: %v", fqdn, err)
2018-07-23 15:30:03 +00:00
}
zone, _, err := d.client.Zones.Get(authZone)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-09-21 16:38:02 +00:00
return nil, fmt.Errorf("failed to get zone [authZone: %q, fqdn: %q]: %v", authZone, fqdn, err)
2017-02-07 21:33:23 +00:00
}
return zone, nil
}
2018-07-23 15:30:03 +00:00
func getAuthZone(fqdn string) (string, error) {
2019-01-07 17:30:06 +00:00
authZone, err := dns01.FindZoneByFqdn(fqdn)
2018-07-23 15:30:03 +00:00
if err != nil {
return "", err
}
2018-09-21 16:38:02 +00:00
return strings.TrimSuffix(authZone, "."), nil
2018-07-23 15:30:03 +00:00
}