2020-05-28 14:57:29 +00:00
|
|
|
// Package arvancloud implements a DNS provider for solving the DNS-01 challenge using ArvanCloud DNS.
|
|
|
|
package arvancloud
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-05-28 14:57:29 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"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"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/arvancloud/internal"
|
2020-05-28 14:57:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const minTTL = 600
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "ARVANCLOUD_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
APIKey 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
|
|
|
|
|
|
|
|
recordIDs map[string]string
|
|
|
|
recordIDsMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for ArvanCloud.
|
|
|
|
// Credentials must be passed in the environment variable: ARVANCLOUD_API_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("arvancloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for ArvanCloud.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("arvancloud: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIKey == "" {
|
|
|
|
return nil, errors.New("arvancloud: credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("arvancloud: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := internal.NewClient(config.APIKey)
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{
|
|
|
|
config: config,
|
|
|
|
client: client,
|
|
|
|
recordIDs: make(map[string]string),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-06-30 18:42:28 +00:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
2020-05-28 14:57:29 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return fmt.Errorf("arvancloud: could not find zone for domain %q: %w", domain, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
authZone = dns01.UnFqdn(authZone)
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 17:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("arvancloud: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:57:29 +00:00
|
|
|
record := internal.DNSRecord{
|
|
|
|
Type: "txt",
|
2022-11-28 17:53:13 +00:00
|
|
|
Name: subDomain,
|
2023-03-07 08:39:05 +00:00
|
|
|
Value: internal.TXTRecordValue{Text: info.Value},
|
2020-05-28 14:57:29 +00:00
|
|
|
TTL: d.config.TTL,
|
|
|
|
UpstreamHTTPS: "default",
|
|
|
|
IPFilterMode: &internal.IPFilterMode{
|
|
|
|
Count: "single",
|
|
|
|
GeoFilter: "none",
|
|
|
|
Order: "none",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
newRecord, err := d.client.CreateRecord(context.Background(), authZone, record)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2023-03-07 08:39:05 +00:00
|
|
|
return fmt.Errorf("arvancloud: failed to add TXT record: fqdn=%s: %w", info.EffectiveFQDN, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
d.recordIDs[token] = newRecord.ID
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-05-28 14:57:29 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2020-05-28 14:57:29 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return fmt.Errorf("arvancloud: could not find zone for domain %q: %w", domain, err)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
authZone = dns01.UnFqdn(authZone)
|
|
|
|
|
2020-05-28 14:57:29 +00:00
|
|
|
// gets 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 08:39:05 +00:00
|
|
|
return fmt.Errorf("arvancloud: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token)
|
2020-05-28 14:57:29 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if err := d.client.DeleteRecord(context.Background(), authZone, recordID); err != nil {
|
2020-05-28 14:57:29 +00:00
|
|
|
return fmt.Errorf("arvancloud: failed to delate TXT record: id=%s: %w", recordID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deletes record ID from map
|
|
|
|
d.recordIDsMu.Lock()
|
|
|
|
delete(d.recordIDs, token)
|
|
|
|
d.recordIDsMu.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|