2023-02-01 15:22:25 +02:00
|
|
|
// Package websupport implements a DNS provider for solving the DNS-01 challenge using Websupport.
|
|
|
|
package websupport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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/websupport/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultTTL = 600
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "WEBSUPPORT_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
EnvSecret = envNamespace + "SECRET"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
Secret string
|
|
|
|
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
SequenceInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, defaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
|
|
|
SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
|
|
|
|
|
|
|
recordIDs map[string]int
|
|
|
|
recordIDsMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Websupport.
|
|
|
|
// Credentials must be passed in the environment variables: WEBSUPPORT_API_KEY, WEBSUPPORT_SECRET.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIKey, EnvSecret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
config.Secret = values[EnvSecret]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Websupport.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("websupport: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := internal.NewClient(config.APIKey, config.Secret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
|
|
|
config: config,
|
|
|
|
client: client,
|
|
|
|
recordIDs: make(map[string]int),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2023-02-01 15:22:25 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2023-02-01 15:22:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2023-02-01 15:22:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
record := internal.Record{
|
|
|
|
Type: "TXT",
|
|
|
|
Name: subDomain,
|
2023-03-07 10:39:05 +02:00
|
|
|
Content: info.Value,
|
2023-02-01 15:22:25 +02:00
|
|
|
TTL: d.config.TTL,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.client.AddRecord(dns01.UnFqdn(authZone), record)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: add record: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Status == internal.StatusSuccess {
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
d.recordIDs[token] = resp.Item.ID
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = internal.ParseError(resp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-02-01 15:22:25 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2023-02-01 15:22:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the record's unique ID
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
recordID, ok := d.recordIDs[token]
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
if !ok {
|
2023-03-07 10:39:05 +02:00
|
|
|
return fmt.Errorf("websupport: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token)
|
2023-02-01 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.client.DeleteRecord(dns01.UnFqdn(authZone), recordID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: delete record: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deletes record ID from map
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
delete(d.recordIDs, token)
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
if resp.Status == internal.StatusSuccess {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = internal.ParseError(resp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("websupport: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sequential All DNS challenges for this provider will be resolved sequentially.
|
|
|
|
// Returns the interval between each iteration.
|
|
|
|
func (d *DNSProvider) Sequential() time.Duration {
|
|
|
|
return d.config.SequenceInterval
|
|
|
|
}
|