2017-02-07 21:33:23 +00:00
|
|
|
package dnsmadeeasy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2018-09-17 13:16:03 +00:00
|
|
|
"errors"
|
2017-02-07 21:33:23 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2018-07-03 10:44:04 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2017-02-07 21:33:23 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
BaseURL string
|
|
|
|
APIKey string
|
|
|
|
APISecret string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt("DNSMADEEASY_TTL", 120),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("DNSMADEEASY_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("DNSMADEEASY_POLLING_INTERVAL", acme.DefaultPollingInterval),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("DNSMADEEASY_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface that uses
|
|
|
|
// DNSMadeEasy's DNS API to manage TXT records for a domain.
|
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *Client
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for DNSMadeEasy DNS.
|
2018-09-17 13:16:03 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// DNSMADEEASY_API_KEY and DNSMADEEASY_API_SECRET.
|
2017-02-07 21:33:23 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-07-03 10:44:04 +00:00
|
|
|
values, err := env.Get("DNSMADEEASY_API_KEY", "DNSMADEEASY_API_SECRET")
|
|
|
|
if err != nil {
|
2018-09-17 13:16:03 +00:00
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: %v", err)
|
2018-07-03 10:44:04 +00:00
|
|
|
}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
var baseURL string
|
2018-10-10 14:28:04 +00:00
|
|
|
if sandbox, _ := strconv.ParseBool(env.GetOrFile("DNSMADEEASY_SANDBOX")); sandbox {
|
2017-02-07 21:33:23 +00:00
|
|
|
baseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0"
|
|
|
|
} else {
|
|
|
|
baseURL = "https://api.dnsmadeeasy.com/V2.0"
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.BaseURL = baseURL
|
|
|
|
config.APIKey = values["DNSMADEEASY_API_KEY"]
|
|
|
|
config.APISecret = values["DNSMADEEASY_API_SECRET"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for DNS Made Easy.
|
|
|
|
// Deprecated
|
2017-02-07 21:33:23 +00:00
|
|
|
func NewDNSProviderCredentials(baseURL, apiKey, apiSecret string) (*DNSProvider, error) {
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.BaseURL = baseURL
|
|
|
|
config.APIKey = apiKey
|
|
|
|
config.APISecret = apiSecret
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
if config.BaseURL == "" {
|
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: base URL missing")
|
2018-07-03 10:44:04 +00:00
|
|
|
}
|
2018-09-17 13:16:03 +00:00
|
|
|
|
|
|
|
client, err := NewClient(config.APIKey, config.APISecret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dnsmadeeasy: %v", err)
|
2018-07-03 10:44:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
client.BaseURL = config.BaseURL
|
|
|
|
|
2017-02-07 21:33:23 +00:00
|
|
|
return &DNSProvider{
|
2018-09-17 13:16:03 +00:00
|
|
|
client: client,
|
|
|
|
config: config,
|
2017-02-07 21:33:23 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters
|
|
|
|
func (d *DNSProvider) Present(domainName, token, keyAuth string) error {
|
2018-09-17 13:16:03 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domainName, keyAuth)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the domain details
|
2018-09-17 13:16:03 +00:00
|
|
|
domain, err := d.client.GetDomain(authZone)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the TXT record
|
|
|
|
name := strings.Replace(fqdn, "."+authZone, "", 1)
|
2018-09-17 13:16:03 +00:00
|
|
|
record := &Record{Type: "TXT", Name: name, Value: value, TTL: d.config.TTL}
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
err = d.client.CreateRecord(domain, record)
|
2018-05-31 07:30:04 +00:00
|
|
|
return err
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT records matching the specified parameters
|
|
|
|
func (d *DNSProvider) CleanUp(domainName, token, keyAuth string) error {
|
2018-05-31 07:30:04 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domainName, keyAuth)
|
2017-02-07 21:33:23 +00:00
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch the domain details
|
2018-09-17 13:16:03 +00:00
|
|
|
domain, err := d.client.GetDomain(authZone)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find matching records
|
|
|
|
name := strings.Replace(fqdn, "."+authZone, "", 1)
|
2018-09-17 13:16:03 +00:00
|
|
|
records, err := d.client.GetRecords(domain, name, "TXT")
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete records
|
|
|
|
for _, record := range *records {
|
2018-09-17 13:16:03 +00:00
|
|
|
err = d.client.DeleteRecord(record)
|
2017-02-07 21:33:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +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
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|