2019-06-27 19:30:10 +02:00
|
|
|
// Package versio implements a DNS provider for solving the DNS-01 challenge using versio DNS.
|
|
|
|
package versio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"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"
|
2019-06-27 19:30:10 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02: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 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2019-06-27 19:30:10 +02: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 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2019-06-27 19:30:10 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
2020-03-12 00:51:10 +02:00
|
|
|
baseURL, err := url.Parse(env.GetOrDefaultString(EnvEndpoint, defaultBaseURL))
|
2019-06-27 19:30:10 +02:00
|
|
|
if err != nil {
|
|
|
|
baseURL, _ = url.Parse(defaultBaseURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Config{
|
|
|
|
BaseURL: baseURL,
|
2020-03-12 00:51:10 +02:00
|
|
|
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 19:30:10 +02:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-12 00:51:10 +02:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2019-06-27 19:30:10 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2019-06-27 19:30:10 +02:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
dnsEntriesMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-12 00:51:10 +02:00
|
|
|
values, err := env.Get(EnvUsername, EnvPassword)
|
2019-06-27 19:30:10 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.Username = values[EnvUsername]
|
|
|
|
config.Password = values[EnvPassword]
|
2019-06-27 19:30:10 +02:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
2019-08-20 18:40:41 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Versio.
|
2019-06-27 19:30:10 +02: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")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, 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
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2019-06-27 19:30:10 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// use mutex to prevent race condition from getDNSRecords until postDNSRecords
|
|
|
|
d.dnsEntriesMu.Lock()
|
|
|
|
defer d.dnsEntriesMu.Unlock()
|
|
|
|
|
|
|
|
zoneName := dns01.UnFqdn(authZone)
|
|
|
|
domains, err := d.getDNSRecords(zoneName)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
txtRecord := record{
|
|
|
|
Type: "TXT",
|
|
|
|
Name: fqdn,
|
|
|
|
Value: `"` + value + `"`,
|
|
|
|
TTL: d.config.TTL,
|
|
|
|
}
|
|
|
|
// Add new txtRercord to existing array of DNSRecords
|
|
|
|
msg := &domains.Record
|
|
|
|
msg.DNSRecords = append(msg.DNSRecords, txtRecord)
|
|
|
|
|
|
|
|
err = d.postDNSRecords(zoneName, msg)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2019-06-27 19:30:10 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2022-11-25 19:12:21 +02:00
|
|
|
|
2019-06-27 19:30:10 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// use mutex to prevent race condition from getDNSRecords until postDNSRecords
|
|
|
|
d.dnsEntriesMu.Lock()
|
|
|
|
defer d.dnsEntriesMu.Unlock()
|
|
|
|
|
|
|
|
zoneName := dns01.UnFqdn(authZone)
|
|
|
|
domains, err := d.getDNSRecords(zoneName)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// loop through the existing entries and remove the specific record
|
|
|
|
msg := &dnsRecord{}
|
|
|
|
for _, e := range domains.Record.DNSRecords {
|
|
|
|
if e.Name != fqdn {
|
|
|
|
msg.DNSRecords = append(msg.DNSRecords, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.postDNSRecords(zoneName, msg)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("versio: %w", err)
|
2019-06-27 19:30:10 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|