2018-10-18 22:32:00 +02:00
|
|
|
// Package dnsmadeeasy implements a DNS provider for solving the DNS-01 challenge using DNS Made Easy.
|
2016-06-28 23:00:05 +02:00
|
|
|
package dnsmadeeasy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2016-06-28 23:00:05 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 21:19:32 +02:00
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v3/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v3/providers/dns/dnsmadeeasy/internal"
|
2016-06-28 23:00:05 +02:00
|
|
|
)
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
BaseURL string
|
|
|
|
APIKey string
|
|
|
|
APISecret string
|
2018-10-12 19:29:18 +02:00
|
|
|
Sandbox bool
|
2018-09-15 19:07:24 +02:00
|
|
|
HTTPClient *http.Client
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2018-12-06 23:50:17 +02:00
|
|
|
TTL: env.GetOrDefaultInt("DNSMADEEASY_TTL", dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("DNSMADEEASY_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("DNSMADEEASY_POLLING_INTERVAL", dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("DNSMADEEASY_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 18:40:41 +02:00
|
|
|
// DNSProvider is an implementation of the challenge.Provider interface that uses
|
2018-09-15 19:07:24 +02:00
|
|
|
// DNSMadeEasy's DNS API to manage TXT records for a domain.
|
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
2018-12-06 23:50:17 +02:00
|
|
|
client *internal.Client
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for DNSMadeEasy DNS.
|
2018-09-15 19:07:24 +02:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// DNSMADEEASY_API_KEY and DNSMADEEASY_API_SECRET.
|
2016-06-28 23:00:05 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 17:32:50 +02:00
|
|
|
values, err := env.Get("DNSMADEEASY_API_KEY", "DNSMADEEASY_API_SECRET")
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: %w", err)
|
2018-06-11 17:32:50 +02:00
|
|
|
}
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2018-10-12 19:29:18 +02:00
|
|
|
config.Sandbox = env.GetOrDefaultBool("DNSMADEEASY_SANDBOX", false)
|
2018-09-15 19:07:24 +02:00
|
|
|
config.APIKey = values["DNSMADEEASY_API_KEY"]
|
|
|
|
config.APISecret = values["DNSMADEEASY_API_SECRET"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02: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 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
var baseURL string
|
|
|
|
if config.Sandbox {
|
|
|
|
baseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0"
|
|
|
|
} else {
|
|
|
|
if len(config.BaseURL) > 0 {
|
|
|
|
baseURL = config.BaseURL
|
|
|
|
} else {
|
|
|
|
baseURL = "https://api.dnsmadeeasy.com/V2.0"
|
|
|
|
}
|
2018-06-21 19:06:16 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
client, err := internal.NewClient(config.APIKey, config.APISecret)
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: %w", err)
|
2018-06-21 19:06:16 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
client.HTTPClient = config.HTTPClient
|
2018-10-12 19:29:18 +02:00
|
|
|
client.BaseURL = baseURL
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2016-06-28 23:00:05 +02:00
|
|
|
return &DNSProvider{
|
2018-09-15 19:07:24 +02:00
|
|
|
client: client,
|
|
|
|
config: config,
|
2016-06-28 23:00:05 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters
|
|
|
|
func (d *DNSProvider) Present(domainName, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, value := dns01.GetRecord(domainName, keyAuth)
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
2016-06-28 23:00:05 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to find zone for %s: %w", fqdn, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the domain details
|
2018-09-15 19:07:24 +02:00
|
|
|
domain, err := d.client.GetDomain(authZone)
|
2016-06-28 23:00:05 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to get domain for zone %s: %w", authZone, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the TXT record
|
|
|
|
name := strings.Replace(fqdn, "."+authZone, "", 1)
|
2018-12-06 23:50:17 +02:00
|
|
|
record := &internal.Record{Type: "TXT", Name: name, Value: value, TTL: d.config.TTL}
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = d.client.CreateRecord(domain, record)
|
2018-10-19 11:49:17 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to create record for %s: %w", name, err)
|
2018-10-19 11:49:17 +02:00
|
|
|
}
|
|
|
|
return nil
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT records matching the specified parameters
|
|
|
|
func (d *DNSProvider) CleanUp(domainName, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, _ := dns01.GetRecord(domainName, keyAuth)
|
2016-06-28 23:00:05 +02:00
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
2016-06-28 23:00:05 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to find zone for %s: %w", fqdn, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the domain details
|
2018-09-15 19:07:24 +02:00
|
|
|
domain, err := d.client.GetDomain(authZone)
|
2016-06-28 23:00:05 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to get domain for zone %s: %w", authZone, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// find matching records
|
|
|
|
name := strings.Replace(fqdn, "."+authZone, "", 1)
|
2018-09-15 19:07:24 +02:00
|
|
|
records, err := d.client.GetRecords(domain, name, "TXT")
|
2016-06-28 23:00:05 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsmadeeasy: unable to get records for domain %s: %w", domain.Name, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete records
|
2018-10-19 11:49:17 +02:00
|
|
|
var lastError error
|
2016-06-28 23:00:05 +02:00
|
|
|
for _, record := range *records {
|
2018-09-15 19:07:24 +02:00
|
|
|
err = d.client.DeleteRecord(record)
|
2016-06-28 23:00:05 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
lastError = fmt.Errorf("dnsmadeeasy: unable to delete record [id=%d, name=%s]: %w", record.ID, record.Name, err)
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:49:17 +02:00
|
|
|
return lastError
|
2016-06-28 23:00:05 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02: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 23:00:05 +02:00
|
|
|
}
|