lego/providers/dns/ns1/ns1.go

167 lines
5 KiB
Go
Raw Normal View History

// Package ns1 implements a DNS provider for solving the DNS-01 challenge using NS1 DNS.
2019-03-11 16:56:48 +00:00
package ns1
import (
"errors"
"fmt"
"net/http"
"time"
2020-09-02 01:20:01 +00: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"
"gopkg.in/ns1/ns1-go.v2/rest"
"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
)
// 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 17:35:25 +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
}
2020-05-08 17:35:25 +00:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
},
}
}
2020-05-08 17:35:25 +00:00
// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
client *rest.Client
config *Config
}
// NewDNSProvider returns a DNSProvider instance configured for NS1.
// Credentials must be passed in the environment variables: NS1_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey)
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("ns1: %w", err)
}
config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]
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")
}
if config.APIKey == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("ns1: credentials missing")
}
client := rest.NewClient(config.HTTPClient, rest.SetAPIKey(config.APIKey))
return &DNSProvider{client: client, config: config}, nil
}
// Present creates a TXT record to fulfill the dns-01 challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
zone, err := d.getHostedZone(info.EffectiveFQDN)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("ns1: %w", err)
}
record, _, err := d.client.Records.Get(zone.Zone, dns01.UnFqdn(info.EffectiveFQDN), "TXT")
2018-10-02 20:21:02 +00:00
// Create a new record
2020-10-27 11:01:05 +00:00
if errors.Is(err, rest.ErrRecordMissing) || record == nil {
log.Infof("Create a new record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, info.EffectiveFQDN, domain)
2018-10-02 20:21:02 +00:00
2024-01-28 13:04:31 +00:00
record = dns.NewRecord(zone.Zone, dns01.UnFqdn(info.EffectiveFQDN), "TXT", nil, nil)
2018-10-02 20:21:02 +00:00
record.TTL = d.config.TTL
record.Answers = []*dns.Answer{{Rdata: []string{info.Value}}}
2018-10-02 20:21:02 +00:00
_, err = d.client.Records.Create(record)
if err != nil {
return fmt.Errorf("ns1: failed to create record [zone: %q, fqdn: %q]: %w", zone.Zone, info.EffectiveFQDN, err)
2018-10-02 20:21:02 +00:00
}
return nil
}
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("ns1: failed to get the existing record: %w", err)
2018-10-02 20:21:02 +00:00
}
// Update the existing records
record.Answers = append(record.Answers, &dns.Answer{Rdata: []string{info.Value}})
2018-10-02 20:21:02 +00:00
log.Infof("Update an existing record for [zone: %s, fqdn: %s, domain: %s]", zone.Zone, info.EffectiveFQDN, domain)
2018-10-02 20:21:02 +00:00
_, err = d.client.Records.Update(record)
if err != nil {
return fmt.Errorf("ns1: failed to update record [zone: %q, fqdn: %q]: %w", zone.Zone, info.EffectiveFQDN, err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
zone, err := d.getHostedZone(info.EffectiveFQDN)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("ns1: %w", err)
}
name := dns01.UnFqdn(info.EffectiveFQDN)
_, err = d.client.Records.Delete(zone.Zone, name, "TXT")
2018-10-02 20:21:02 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("ns1: failed to delete record [zone: %q, domain: %q]: %w", zone.Zone, name, err)
2018-10-02 20:21:02 +00:00
}
return nil
}
// 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
}
2018-09-21 15:47:58 +00:00
func (d *DNSProvider) getHostedZone(fqdn string) (*dns.Zone, error) {
2023-05-05 07:49:38 +00:00
authZone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
2023-05-05 07:49:38 +00:00
return nil, fmt.Errorf("could not find zone for FQDN %q: %w", fqdn, err)
}
2023-05-05 07:49:38 +00:00
authZone = dns01.UnFqdn(authZone)
2024-01-28 13:04:31 +00:00
zone, _, err := d.client.Zones.Get(authZone, false)
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("failed to get zone [authZone: %q, fqdn: %q]: %w", authZone, fqdn, err)
}
return zone, nil
}