2018-12-06 23:50:17 +02:00
|
|
|
// Package ns1 implements a DNS provider for solving the DNS-01 challenge using NS1 DNS.
|
2019-03-11 18:56:48 +02:00
|
|
|
package ns1
|
2016-10-18 10:20:15 +02:00
|
|
|
|
|
|
|
import (
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2016-10-18 10:20:15 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2016-10-18 10:20:15 +02:00
|
|
|
"gopkg.in/ns1/ns1-go.v2/rest"
|
|
|
|
"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "NS1_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-12 00:51:10 +02:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
|
2018-09-15 19:07:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-10-18 10:20:15 +02:00
|
|
|
type DNSProvider struct {
|
|
|
|
client *rest.Client
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for NS1.
|
|
|
|
// Credentials must be passed in the environment variables: NS1_API_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-12 00:51:10 +02:00
|
|
|
values, err := env.Get(EnvAPIKey)
|
2018-06-11 17:32:50 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("ns1: %w", err)
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02: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")
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if config.APIKey == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("ns1: credentials missing")
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
2016-10-18 10:20:15 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
client := rest.NewClient(config.HTTPClient, rest.SetAPIKey(config.APIKey))
|
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2016-10-18 10:20:15 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
zone, err := d.getHostedZone(info.EffectiveFQDN)
|
2016-10-18 10:20:15 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("ns1: %w", err)
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
record, _, err := d.client.Records.Get(zone.Zone, dns01.UnFqdn(info.EffectiveFQDN), "TXT")
|
2018-10-02 22:21:02 +02:00
|
|
|
|
|
|
|
// Create a new record
|
2020-10-27 13:01:05 +02:00
|
|
|
if errors.Is(err, rest.ErrRecordMissing) || record == nil {
|
2023-03-07 10:39:05 +02:00
|
|
|
log.Infof("Create a new record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, info.EffectiveFQDN, domain)
|
2018-10-02 22:21:02 +02:00
|
|
|
|
2024-02-16 22:24:58 +02:00
|
|
|
// Work through a bug in the NS1 API library that causes 400 Input validation failed (Value None for field '<obj>.filters' is not of type ...)
|
|
|
|
// So the `tags` and `blockedTags` parameters should be initialized to empty.
|
|
|
|
record = dns.NewRecord(zone.Zone, dns01.UnFqdn(info.EffectiveFQDN), "TXT", make(map[string]string), make([]string, 0))
|
2018-10-02 22:21:02 +02:00
|
|
|
record.TTL = d.config.TTL
|
2023-03-07 10:39:05 +02:00
|
|
|
record.Answers = []*dns.Answer{{Rdata: []string{info.Value}}}
|
2018-10-02 22:21:02 +02:00
|
|
|
|
|
|
|
_, err = d.client.Records.Create(record)
|
|
|
|
if err != nil {
|
2023-03-07 10:39:05 +02:00
|
|
|
return fmt.Errorf("ns1: failed to create record [zone: %q, fqdn: %q]: %w", zone.Zone, info.EffectiveFQDN, err)
|
2018-10-02 22:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("ns1: failed to get the existing record: %w", err)
|
2018-10-02 22:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the existing records
|
2023-03-07 10:39:05 +02:00
|
|
|
record.Answers = append(record.Answers, &dns.Answer{Rdata: []string{info.Value}})
|
2018-10-02 22:21:02 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
log.Infof("Update an existing record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, info.EffectiveFQDN, domain)
|
2018-10-02 22:21:02 +02:00
|
|
|
|
|
|
|
_, err = d.client.Records.Update(record)
|
|
|
|
if err != nil {
|
2023-03-07 10:39:05 +02:00
|
|
|
return fmt.Errorf("ns1: failed to update record [zone: %q, fqdn: %q]: %w", zone.Zone, info.EffectiveFQDN, err)
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2016-10-18 10:20:15 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
zone, err := d.getHostedZone(info.EffectiveFQDN)
|
2016-10-18 10:20:15 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("ns1: %w", err)
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
name := dns01.UnFqdn(info.EffectiveFQDN)
|
2018-06-21 19:06:16 +02:00
|
|
|
_, err = d.client.Records.Delete(zone.Zone, name, "TXT")
|
2018-10-02 22:21:02 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("ns1: failed to delete record [zone: %q, domain: %q]: %w", zone.Zone, name, err)
|
2018-10-02 22:21:02 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-09-15 19:07:24 +02: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
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:47:58 +02:00
|
|
|
func (d *DNSProvider) getHostedZone(fqdn string) (*dns.Zone, error) {
|
2023-05-05 09:49:38 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
2018-07-14 12:31:57 +02:00
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return nil, fmt.Errorf("could not find zone: %w", err)
|
2018-07-14 12:31:57 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
authZone = dns01.UnFqdn(authZone)
|
|
|
|
|
2024-01-28 15:04:31 +02:00
|
|
|
zone, _, err := d.client.Zones.Get(authZone, false)
|
2016-10-18 10:20:15 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("failed to get zone [authZone: %q, fqdn: %q]: %w", authZone, fqdn, err)
|
2016-10-18 10:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return zone, nil
|
|
|
|
}
|