2019-07-05 01:38:43 +00:00
|
|
|
// Package namesilo implements a DNS provider for solving the DNS-01 challenge using namesilo DNS.
|
|
|
|
package namesilo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2019-07-05 01:38:43 +00:00
|
|
|
"github.com/nrdcg/namesilo"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultTTL = 3600
|
|
|
|
maxTTL = 2592000
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "NAMESILO_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2019-07-05 01:38:43 +00:00
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2019-07-05 01:38:43 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, defaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2019-07-05 01:38:43 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
client *namesilo.Client
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for namesilo.
|
|
|
|
// API_KEY must be passed in the environment variables: NAMESILO_API_KEY.
|
|
|
|
//
|
|
|
|
// See: https://www.namesilo.com/api_reference.php
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIKey)
|
2019-07-05 01:38:43 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("namesilo: %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
2019-07-05 01:38:43 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:40:41 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Namesilo.
|
2019-07-05 01:38:43 +00:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("namesilo: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TTL < defaultTTL || config.TTL > maxTTL {
|
|
|
|
return nil, fmt.Errorf("namesilo: TTL should be in [%d, %d]", defaultTTL, maxTTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
transport, err := namesilo.NewTokenTransport(config.APIKey)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("namesilo: %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{client: namesilo.NewClient(transport.Client()), config: config}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2019-07-05 01:38:43 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
zoneName, err := getZoneNameByDomain(info.EffectiveFQDN)
|
2019-07-05 01:38:43 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namesilo: %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
subdomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zoneName)
|
2022-11-28 17:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("namesilo: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-05 20:54:41 +00:00
|
|
|
err = d.CleanUp(domain, token, keyAuth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-05 01:38:43 +00:00
|
|
|
_, err = d.client.DnsAddRecord(&namesilo.DnsAddRecordParams{
|
|
|
|
Domain: zoneName,
|
|
|
|
Type: "TXT",
|
2022-11-28 17:53:13 +00:00
|
|
|
Host: subdomain,
|
2023-03-07 08:39:05 +00:00
|
|
|
Value: info.Value,
|
2019-07-05 01:38:43 +00:00
|
|
|
TTL: d.config.TTL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namesilo: failed to add record %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2023-02-05 20:54:41 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, _, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2019-07-05 01:38:43 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
zoneName, err := getZoneNameByDomain(info.EffectiveFQDN)
|
2019-07-05 01:38:43 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namesilo: %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.client.DnsListRecords(&namesilo.DnsListRecordsParams{Domain: zoneName})
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namesilo: %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
subdomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zoneName)
|
2022-11-28 17:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("namesilo: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-07-05 01:38:43 +00:00
|
|
|
var lastErr error
|
|
|
|
for _, r := range resp.Reply.ResourceRecord {
|
2023-03-07 08:39:05 +00:00
|
|
|
if r.Type == "TXT" && (r.Host == subdomain || r.Host == dns01.UnFqdn(info.EffectiveFQDN)) {
|
2019-07-05 01:38:43 +00:00
|
|
|
_, err := d.client.DnsDeleteRecord(&namesilo.DnsDeleteRecordParams{Domain: zoneName, ID: r.RecordID})
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
lastErr = fmt.Errorf("namesilo: %w", err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func getZoneNameByDomain(domain string) (string, error) {
|
2022-11-25 17:12:21 +00:00
|
|
|
zone, err := dns01.FindZoneByFqdn(domain)
|
2019-07-05 01:38:43 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return "", fmt.Errorf("failed to find zone for domain: %s, %w", domain, err)
|
2019-07-05 01:38:43 +00:00
|
|
|
}
|
|
|
|
return dns01.UnFqdn(zone), nil
|
|
|
|
}
|