2022-06-06 22:22:34 +02:00
|
|
|
// Package variomedia implements a DNS provider for solving the DNS-01 challenge using Variomedia DNS.
|
|
|
|
package variomedia
|
|
|
|
|
|
|
|
import (
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2022-06-06 22:22:34 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v4/platform/wait"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/variomedia/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "VARIOMEDIA_"
|
|
|
|
|
|
|
|
EnvAPIToken = envNamespace + "API_TOKEN"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
|
|
|
APIToken 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{
|
2023-05-05 09:49:38 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 300),
|
2022-06-06 22:22:34 +02:00
|
|
|
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]string
|
|
|
|
recordIDsMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("variomedia: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIToken = values[EnvAPIToken]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Variomedia.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config.APIToken == "" {
|
|
|
|
return nil, errors.New("variomedia: missing credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := internal.NewClient(config.APIToken)
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
|
|
|
config: config,
|
|
|
|
client: client,
|
|
|
|
recordIDs: make(map[string]string),
|
|
|
|
}, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-06-06 22:22:34 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("variomedia: could not find zone for domain %q: %w", domain, err)
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-12-04 17:11:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("variomedia: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-06-06 22:22:34 +02:00
|
|
|
record := internal.DNSRecord{
|
|
|
|
RecordType: "TXT",
|
2022-12-04 17:11:27 +02:00
|
|
|
Name: subDomain,
|
2022-06-06 22:22:34 +02:00
|
|
|
Domain: dns01.UnFqdn(authZone),
|
2023-03-07 10:39:05 +02:00
|
|
|
Data: info.Value,
|
2022-06-06 22:22:34 +02:00
|
|
|
TTL: d.config.TTL,
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
cdrr, err := d.client.CreateDNSRecord(ctx, record)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("variomedia: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err = d.waitJob(ctx, domain, cdrr.Data.ID)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("variomedia: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
d.recordIDs[token] = strings.TrimPrefix(cdrr.Data.Links.DNSRecord, "https://api.variomedia.de/dns-records/")
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record previously created.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-06-06 22:22:34 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-06-06 22:22:34 +02:00
|
|
|
// get the record's unique ID from when we created it
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
recordID, ok := d.recordIDs[token]
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
if !ok {
|
2023-03-07 10:39:05 +02:00
|
|
|
return fmt.Errorf("variomedia: unknown record ID for '%s'", info.EffectiveFQDN)
|
2022-06-06 22:22:34 +02:00
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
ddrr, err := d.client.DeleteDNSRecord(ctx, recordID)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("variomedia: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
err = d.waitJob(ctx, domain, ddrr.Data.ID)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("variomedia: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
func (d *DNSProvider) waitJob(ctx context.Context, domain string, id string) error {
|
2022-06-06 22:22:34 +02:00
|
|
|
return wait.For("variomedia: apply change on "+domain, d.config.PropagationTimeout, d.config.PollingInterval, func() (bool, error) {
|
2023-05-05 09:49:38 +02:00
|
|
|
result, err := d.client.GetJob(ctx, id)
|
2022-06-06 22:22:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("variomedia: [%s] %s: %s %s", domain, result.Data.ID, result.Data.Attributes.JobType, result.Data.Attributes.Status)
|
|
|
|
|
|
|
|
return result.Data.Attributes.Status == "done", nil
|
|
|
|
})
|
|
|
|
}
|