lego/providers/dns/versio/versio.go

194 lines
5.4 KiB
Go
Raw Permalink Normal View History

2019-06-27 17:30:10 +00:00
// Package versio implements a DNS provider for solving the DNS-01 challenge using versio DNS.
package versio
import (
2023-05-05 07:49:38 +00:00
"context"
2019-06-27 17:30:10 +00:00
"errors"
"fmt"
"net/http"
"net/url"
"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"
2023-05-05 07:49:38 +00:00
"github.com/go-acme/lego/v4/providers/dns/versio/internal"
2019-06-27 17:30:10 +00:00
)
// Environment variables names.
const (
envNamespace = "VERSIO_"
EnvUsername = envNamespace + "USERNAME"
EnvPassword = envNamespace + "PASSWORD"
EnvEndpoint = envNamespace + "ENDPOINT"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvSequenceInterval = envNamespace + "SEQUENCE_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
)
2020-05-08 17:35:25 +00:00
// Config is used to configure the creation of the DNSProvider.
2019-06-27 17:30:10 +00:00
type Config struct {
BaseURL *url.URL
TTL int
Username string
Password string
PropagationTimeout time.Duration
PollingInterval time.Duration
SequenceInterval time.Duration
HTTPClient *http.Client
}
2020-05-08 17:35:25 +00:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
2019-06-27 17:30:10 +00:00
func NewDefaultConfig() *Config {
2023-05-05 07:49:38 +00:00
baseURL, err := url.Parse(env.GetOrDefaultString(EnvEndpoint, internal.DefaultBaseURL))
2019-06-27 17:30:10 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
baseURL, _ = url.Parse(internal.DefaultBaseURL)
2019-06-27 17:30:10 +00:00
}
return &Config{
BaseURL: baseURL,
TTL: env.GetOrDefaultInt(EnvTTL, 300),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
SequenceInterval: env.GetOrDefaultSecond(EnvSequenceInterval, dns01.DefaultPropagationTimeout),
2019-06-27 17:30:10 +00:00
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
2019-06-27 17:30:10 +00:00
},
}
}
2020-05-08 17:35:25 +00:00
// DNSProvider implements the challenge.Provider interface.
2019-06-27 17:30:10 +00:00
type DNSProvider struct {
2023-05-05 07:49:38 +00:00
config *Config
client *internal.Client
2019-06-27 17:30:10 +00:00
dnsEntriesMu sync.Mutex
}
// NewDNSProvider returns a DNSProvider instance.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvUsername, EnvPassword)
2019-06-27 17:30:10 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("versio: %w", err)
2019-06-27 17:30:10 +00:00
}
config := NewDefaultConfig()
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
2019-06-27 17:30:10 +00:00
return NewDNSProviderConfig(config)
}
2019-08-20 16:40:41 +00:00
// NewDNSProviderConfig return a DNSProvider instance configured for Versio.
2019-06-27 17:30:10 +00:00
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("versio: the configuration of the DNS provider is nil")
}
if config.Username == "" {
return nil, errors.New("versio: the versio username is missing")
}
if config.Password == "" {
return nil, errors.New("versio: the versio password is missing")
}
2023-05-05 07:49:38 +00:00
client := internal.NewClient(config.Username, config.Password)
if config.BaseURL != nil {
client.BaseURL = config.BaseURL
}
if config.HTTPClient != nil {
client.HTTPClient = config.HTTPClient
}
return &DNSProvider{config: config, client: client}, nil
2019-06-27 17:30:10 +00:00
}
// 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
}
2020-05-08 17:35:25 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge.
2019-06-27 17:30:10 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2019-06-27 17:30:10 +00:00
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
2019-06-27 17:30:10 +00:00
if err != nil {
return fmt.Errorf("versio: could not find zone for domain %q: %w", domain, err)
2019-06-27 17:30:10 +00:00
}
// use mutex to prevent race condition from getDNSRecords until postDNSRecords
d.dnsEntriesMu.Lock()
defer d.dnsEntriesMu.Unlock()
2023-05-05 07:49:38 +00:00
ctx := context.Background()
2019-06-27 17:30:10 +00:00
zoneName := dns01.UnFqdn(authZone)
2023-05-05 07:49:38 +00:00
domains, err := d.client.GetDomain(ctx, zoneName)
2019-06-27 17:30:10 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("versio: %w", err)
2019-06-27 17:30:10 +00:00
}
2023-05-05 07:49:38 +00:00
txtRecord := internal.Record{
2019-06-27 17:30:10 +00:00
Type: "TXT",
Name: info.EffectiveFQDN,
Value: `"` + info.Value + `"`,
2019-06-27 17:30:10 +00:00
TTL: d.config.TTL,
}
2023-05-05 07:49:38 +00:00
// Add new txtRecord to existing array of DNSRecords.
// We'll need all the dns_records to add a new TXT record.
msg := &domains.DomainInfo
2019-06-27 17:30:10 +00:00
msg.DNSRecords = append(msg.DNSRecords, txtRecord)
2023-05-05 07:49:38 +00:00
_, err = d.client.UpdateDomain(ctx, zoneName, msg)
2019-06-27 17:30:10 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("versio: %w", err)
2019-06-27 17:30:10 +00:00
}
return nil
}
2020-05-08 17:35:25 +00:00
// CleanUp removes the TXT record matching the specified parameters.
2019-06-27 17:30:10 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2022-11-25 17:12:21 +00:00
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
2019-06-27 17:30:10 +00:00
if err != nil {
return fmt.Errorf("versio: could not find zone for domain %q: %w", domain, err)
2019-06-27 17:30:10 +00:00
}
// use mutex to prevent race condition from getDNSRecords until postDNSRecords
d.dnsEntriesMu.Lock()
defer d.dnsEntriesMu.Unlock()
2023-05-05 07:49:38 +00:00
ctx := context.Background()
2019-06-27 17:30:10 +00:00
zoneName := dns01.UnFqdn(authZone)
2023-05-05 07:49:38 +00:00
domains, err := d.client.GetDomain(ctx, zoneName)
2019-06-27 17:30:10 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("versio: %w", err)
2019-06-27 17:30:10 +00:00
}
// loop through the existing entries and remove the specific record
2023-05-05 07:49:38 +00:00
msg := &internal.DomainInfo{}
for _, e := range domains.DomainInfo.DNSRecords {
if e.Name != info.EffectiveFQDN {
2019-06-27 17:30:10 +00:00
msg.DNSRecords = append(msg.DNSRecords, e)
}
}
2023-05-05 07:49:38 +00:00
_, err = d.client.UpdateDomain(ctx, zoneName, msg)
2019-06-27 17:30:10 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("versio: %w", err)
2019-06-27 17:30:10 +00:00
}
return nil
}