2016-10-18 08:20:15 +00:00
|
|
|
// Package ns1 implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using NS1 DNS.
|
|
|
|
package ns1
|
|
|
|
|
|
|
|
import (
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2016-10-18 08:20:15 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-07-14 10:31:57 +00:00
|
|
|
"strings"
|
2016-10-18 08:20:15 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-10-18 08:20:15 +00:00
|
|
|
"gopkg.in/ns1/ns1-go.v2/rest"
|
|
|
|
"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
|
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +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{
|
|
|
|
TTL: env.GetOrDefaultInt("NS1_TTL", 120),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("NS1_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("NS1_POLLING_INTERVAL", acme.DefaultPollingInterval),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("NS1_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 08:20:15 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
client *rest.Client
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2016-10-18 08:20:15 +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-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("NS1_API_KEY")
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("ns1: %v", err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values["NS1_API_KEY"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for NS1.
|
|
|
|
// Deprecated
|
2016-10-18 08:20:15 +00:00
|
|
|
func NewDNSProviderCredentials(key string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = key
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.APIKey == "" {
|
|
|
|
return nil, fmt.Errorf("ns1: credentials missing")
|
|
|
|
}
|
2016-10-18 08:20:15 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
client := rest.NewClient(config.HTTPClient, rest.SetAPIKey(config.APIKey))
|
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfil the dns-01 challenge.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-09-15 17:07:24 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-10-18 08:20:15 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
zone, err := d.getHostedZone(domain)
|
2016-10-18 08:20:15 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("ns1: %v", err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
record := d.newTxtRecord(zone, fqdn, value, d.config.TTL)
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err = d.client.Records.Create(record)
|
2016-10-18 08:20:15 +00:00
|
|
|
if err != nil && err != rest.ErrRecordExists {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("ns1: %v", err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-10-18 08:20:15 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
zone, err := d.getHostedZone(domain)
|
2016-10-18 08:20:15 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("ns1: %v", err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := acme.UnFqdn(fqdn)
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err = d.client.Records.Delete(zone.Zone, name, "TXT")
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("ns1: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (*dns.Zone, error) {
|
2018-07-14 10:31:57 +00:00
|
|
|
authZone, err := getAuthZone(domain)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("ns1: %v", err)
|
2018-07-14 10:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zone, _, err := d.client.Zones.Get(authZone)
|
2016-10-18 08:20:15 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("ns1: %v", err)
|
2016-10-18 08:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return zone, nil
|
|
|
|
}
|
|
|
|
|
2018-07-14 10:31:57 +00:00
|
|
|
func getAuthZone(fqdn string) (string, error) {
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(authZone, ".") {
|
|
|
|
authZone = authZone[:len(authZone)-len(".")]
|
|
|
|
}
|
|
|
|
|
|
|
|
return authZone, err
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) newTxtRecord(zone *dns.Zone, fqdn, value string, ttl int) *dns.Record {
|
2016-10-18 08:20:15 +00:00
|
|
|
name := acme.UnFqdn(fqdn)
|
|
|
|
|
|
|
|
return &dns.Record{
|
|
|
|
Type: "TXT",
|
|
|
|
Zone: zone.Zone,
|
|
|
|
Domain: name,
|
|
|
|
TTL: ttl,
|
|
|
|
Answers: []*dns.Answer{
|
|
|
|
{Rdata: []string{value}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|