2020-05-10 06:03:54 +02:00
|
|
|
// Package luadns implements a DNS provider for solving the DNS-01 challenge using LuaDNS.
|
|
|
|
package luadns
|
|
|
|
|
|
|
|
import (
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2020-05-10 06:03:54 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/luadns/internal"
|
2020-05-10 06:03:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const minTTL = 300
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "LUADNS_"
|
|
|
|
|
|
|
|
EnvAPIUsername = envNamespace + "API_USERNAME"
|
|
|
|
EnvAPIToken = envNamespace + "API_TOKEN"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
|
|
|
APIUsername string
|
|
|
|
APIToken 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(EnvTTL, minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
|
|
|
|
|
|
|
recordsMu sync.Mutex
|
|
|
|
records map[string]*internal.DNSRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for LuaDNS.
|
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// LUADNS_API_USERNAME and LUADNS_API_TOKEN.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIUsername, EnvAPIToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("luadns: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIUsername = values[EnvAPIUsername]
|
|
|
|
config.APIToken = values[EnvAPIToken]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for LuaDNS.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("luadns: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIUsername == "" || config.APIToken == "" {
|
|
|
|
return nil, errors.New("luadns: credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("luadns: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := internal.NewClient(config.APIUsername, config.APIToken)
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
|
|
|
config: config,
|
|
|
|
client: client,
|
|
|
|
recordsMu: sync.Mutex{},
|
|
|
|
records: make(map[string]*internal.DNSRecord),
|
|
|
|
}, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-05-10 06:03:54 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
zones, err := d.client.ListZones(ctx)
|
2020-05-10 06:03:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("luadns: failed to get zones: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2022-09-17 20:04:47 +02:00
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("luadns: could not find zone for domain %q: %w", domain, err)
|
2022-09-17 20:04:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:09:51 +02:00
|
|
|
zone := findZone(zones, dns01.UnFqdn(authZone))
|
2020-05-10 06:03:54 +02:00
|
|
|
if zone == nil {
|
|
|
|
return fmt.Errorf("luadns: no matching zone found for domain %s", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
newRecord := internal.DNSRecord{
|
2023-03-07 10:39:05 +02:00
|
|
|
Name: info.EffectiveFQDN,
|
2020-05-10 06:03:54 +02:00
|
|
|
Type: "TXT",
|
2023-03-07 10:39:05 +02:00
|
|
|
Content: info.Value,
|
2020-05-10 06:03:54 +02:00
|
|
|
TTL: d.config.TTL,
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
record, err := d.client.CreateRecord(ctx, *zone, newRecord)
|
2020-05-10 06:03:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("luadns: failed to create record: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.recordsMu.Lock()
|
|
|
|
d.records[token] = record
|
|
|
|
d.recordsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-05-10 06:03:54 +02:00
|
|
|
|
|
|
|
d.recordsMu.Lock()
|
|
|
|
record, ok := d.records[token]
|
|
|
|
d.recordsMu.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
2023-03-07 10:39:05 +02:00
|
|
|
return fmt.Errorf("luadns: unknown record ID for '%s'", info.EffectiveFQDN)
|
2020-05-10 06:03:54 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err := d.client.DeleteRecord(context.Background(), record)
|
2020-05-10 06:03:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("luadns: failed to delete record: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete record from map
|
|
|
|
d.recordsMu.Lock()
|
|
|
|
delete(d.records, token)
|
|
|
|
d.recordsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func findZone(zones []internal.DNSZone, domain string) *internal.DNSZone {
|
|
|
|
var result *internal.DNSZone
|
|
|
|
|
|
|
|
for _, zone := range zones {
|
|
|
|
if zone.Name != "" && strings.HasSuffix(domain, zone.Name) {
|
|
|
|
if result == nil || len(zone.Name) > len(result.Name) {
|
|
|
|
result = &zone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|