2018-10-18 20:32:00 +00:00
|
|
|
// Package dnsmadeeasy implements a DNS provider for solving the DNS-01 challenge using DNS Made Easy.
|
2016-06-28 21:00:05 +00:00
|
|
|
package dnsmadeeasy
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2016-06-28 21:00:05 +00:00
|
|
|
"crypto/tls"
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2016-06-28 21:00:05 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
2016-06-28 21:00:05 +00:00
|
|
|
"strings"
|
|
|
|
"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/dnsmadeeasy/internal"
|
2016-06-28 21:00:05 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "DNSMADEEASY_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
EnvAPISecret = envNamespace + "API_SECRET"
|
|
|
|
EnvSandbox = envNamespace + "SANDBOX"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Config struct {
|
|
|
|
BaseURL string
|
|
|
|
APIKey string
|
|
|
|
APISecret string
|
2018-10-12 17:29:18 +00:00
|
|
|
Sandbox bool
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient *http.Client
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-09-15 17:07:24 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
2018-12-06 21:50:17 +00:00
|
|
|
client *internal.Client
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for DNSMadeEasy DNS.
|
2018-09-15 17:07:24 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// DNSMADEEASY_API_KEY and DNSMADEEASY_API_SECRET.
|
2016-06-28 21:00:05 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIKey, EnvAPISecret)
|
2018-06-11 15:32:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: %w", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.Sandbox = env.GetOrDefaultBool(EnvSandbox, false)
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
config.APISecret = values[EnvAPISecret]
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for DNS Made Easy.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("dnsmadeeasy: the configuration of the DNS provider is nil")
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
var baseURL string
|
|
|
|
if config.Sandbox {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL = internal.DefaultSandboxBaseURL
|
2018-10-12 17:29:18 +00:00
|
|
|
} else {
|
2023-05-05 07:49:38 +00:00
|
|
|
if config.BaseURL == "" {
|
|
|
|
baseURL = internal.DefaultProdBaseURL
|
2018-10-12 17:29:18 +00:00
|
|
|
} else {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL = config.BaseURL
|
2018-10-12 17:29:18 +00:00
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
client, err := internal.NewClient(config.APIKey, config.APISecret)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: %w", err)
|
2018-06-21 17:06:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
client.HTTPClient = config.HTTPClient
|
2023-05-05 07:49:38 +00:00
|
|
|
client.BaseURL, err = url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2016-06-28 21:00:05 +00:00
|
|
|
return &DNSProvider{
|
2018-09-15 17:07:24 +00:00
|
|
|
client: client,
|
|
|
|
config: config,
|
2016-06-28 21:00:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2016-06-28 21:00:05 +00:00
|
|
|
func (d *DNSProvider) Present(domainName, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domainName, keyAuth)
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2016-06-28 21:00:05 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return fmt.Errorf("dnsmadeeasy: could not find zone for domain %q: %w", domainName, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-28 21:00:05 +00:00
|
|
|
// fetch the domain details
|
2023-05-05 07:49:38 +00:00
|
|
|
domain, err := d.client.GetDomain(ctx, authZone)
|
2016-06-28 21:00:05 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to get domain for zone %s: %w", authZone, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the TXT record
|
2023-03-07 08:39:05 +00:00
|
|
|
name := strings.Replace(info.EffectiveFQDN, "."+authZone, "", 1)
|
|
|
|
record := &internal.Record{Type: "TXT", Name: name, Value: info.Value, TTL: d.config.TTL}
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.client.CreateRecord(ctx, domain, record)
|
2018-10-19 09:49:17 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to create record for %s: %w", name, err)
|
2018-10-19 09:49:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT records matching the specified parameters.
|
2016-06-28 21:00:05 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domainName, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domainName, keyAuth)
|
2016-06-28 21:00:05 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2016-06-28 21:00:05 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return fmt.Errorf("dnsmadeeasy: could not find zone for domain %q: %w", domainName, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-28 21:00:05 +00:00
|
|
|
// fetch the domain details
|
2023-05-05 07:49:38 +00:00
|
|
|
domain, err := d.client.GetDomain(ctx, authZone)
|
2016-06-28 21:00:05 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to get domain for zone %s: %w", authZone, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find matching records
|
2023-03-07 08:39:05 +00:00
|
|
|
name := strings.Replace(info.EffectiveFQDN, "."+authZone, "", 1)
|
2023-05-05 07:49:38 +00:00
|
|
|
records, err := d.client.GetRecords(ctx, domain, name, "TXT")
|
2016-06-28 21:00:05 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to get records for domain %s: %w", domain.Name, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete records
|
2018-10-19 09:49:17 +00:00
|
|
|
var lastError error
|
2016-06-28 21:00:05 +00:00
|
|
|
for _, record := range *records {
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.client.DeleteRecord(ctx, record)
|
2016-06-28 21:00:05 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
lastError = fmt.Errorf("dnsmadeeasy: unable to delete record [id=%d, name=%s]: %w", record.ID, record.Name, err)
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 09:49:17 +00:00
|
|
|
return lastError
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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
|
2016-06-28 21:00:05 +00:00
|
|
|
}
|