2016-03-17 22:59:15 +02:00
|
|
|
// Package dyn implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using Dyn Managed DNS.
|
2016-03-18 04:30:21 +02:00
|
|
|
package dyn
|
2016-02-07 09:10:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2016-02-07 09:10:36 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2016-03-19 05:20:58 +02:00
|
|
|
"time"
|
2016-03-18 04:30:21 +02:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 17:32:50 +02:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-02-07 09:10:36 +02:00
|
|
|
)
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
CustomerName string
|
|
|
|
UserName string
|
|
|
|
Password string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
2016-02-07 09:10:36 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt("DYN_TTL", 120),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("DYN_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("DYN_POLLING_INTERVAL", acme.DefaultPollingInterval),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("DYN_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
},
|
|
|
|
}
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 04:30:21 +02:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface that uses
|
2016-02-07 09:10:36 +02:00
|
|
|
// Dyn's Managed DNS API to manage TXT records for a domain.
|
2016-03-18 04:30:21 +02:00
|
|
|
type DNSProvider struct {
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
|
|
|
token string
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2016-03-17 22:59:15 +02:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Dyn DNS.
|
2018-09-15 19:07:24 +02:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// DYN_CUSTOMER_NAME, DYN_USER_NAME and DYN_PASSWORD.
|
2016-03-17 22:59:15 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 17:32:50 +02:00
|
|
|
values, err := env.Get("DYN_CUSTOMER_NAME", "DYN_USER_NAME", "DYN_PASSWORD")
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("dyn: %v", err)
|
2018-06-11 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.CustomerName = values["DYN_CUSTOMER_NAME"]
|
|
|
|
config.UserName = values["DYN_USER_NAME"]
|
|
|
|
config.Password = values["DYN_PASSWORD"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-17 22:59:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for Dyn DNS.
|
|
|
|
// Deprecated
|
2016-03-17 22:59:15 +02:00
|
|
|
func NewDNSProviderCredentials(customerName, userName, password string) (*DNSProvider, error) {
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.CustomerName = customerName
|
|
|
|
config.UserName = userName
|
|
|
|
config.Password = password
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Dyn DNS
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("dyn: the configuration of the DNS provider is nil")
|
2016-03-17 22:59:15 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if config.CustomerName == "" || config.UserName == "" || config.Password == "" {
|
|
|
|
return nil, fmt.Errorf("dyn: credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config}, nil
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// Present creates a TXT record using the specified parameters
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-02-07 09:10:36 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = d.login()
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"rdata": map[string]string{
|
|
|
|
"txtdata": value,
|
|
|
|
},
|
|
|
|
"ttl": strconv.Itoa(d.config.TTL),
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
resource := fmt.Sprintf("TXTRecord/%s/%s/", authZone, fqdn)
|
|
|
|
_, err = d.sendRequest(http.MethodPost, resource, data)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = d.publish(authZone, "Added TXT record for ACME dns-01 challenge using lego client")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
return d.logout()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = d.login()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2018-03-19 18:41:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
resource := fmt.Sprintf("TXTRecord/%s/%s/", authZone, fqdn)
|
|
|
|
url := fmt.Sprintf("%s/%s", defaultBaseURL, resource)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Auth-Token", d.token)
|
2016-02-07 09:10:36 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dyn: %v", err)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
resp.Body.Close()
|
2016-02-07 09:10:36 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("dyn: API request failed to delete TXT record HTTP status code %d", resp.StatusCode)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
err = d.publish(authZone, "Removed TXT record for ACME dns-01 challenge using lego client")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dyn: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.logout()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts a new Dyn API Session. Authenticates using customerName, userName,
|
|
|
|
// password and receives a token to be used in for subsequent requests.
|
|
|
|
func (d *DNSProvider) login() error {
|
|
|
|
payload := &creds{Customer: d.config.CustomerName, User: d.config.UserName, Pass: d.config.Password}
|
2018-06-21 19:06:16 +02:00
|
|
|
dynRes, err := d.sendRequest(http.MethodPost, "Session", payload)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var s session
|
|
|
|
err = json.Unmarshal(dynRes.Data, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.token = s.Token
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroys Dyn Session
|
2016-03-18 04:30:21 +02:00
|
|
|
func (d *DNSProvider) logout() error {
|
2016-02-07 09:10:36 +02:00
|
|
|
if len(d.token) == 0 {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
url := fmt.Sprintf("%s/Session", defaultBaseURL)
|
2018-06-21 19:06:16 +02:00
|
|
|
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Auth-Token", d.token)
|
2016-03-19 05:20:58 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("API request failed to delete session with HTTP status code %d", resp.StatusCode)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.token = ""
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-27 04:36:48 +02:00
|
|
|
func (d *DNSProvider) publish(zone, notes string) error {
|
2016-02-07 09:10:36 +02:00
|
|
|
pub := &publish{Publish: true, Notes: notes}
|
2016-04-27 04:36:48 +02:00
|
|
|
resource := fmt.Sprintf("Zone/%s/", zone)
|
2016-02-07 09:10:36 +02:00
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
_, err := d.sendRequest(http.MethodPut, resource, pub)
|
2018-05-30 19:53:04 +02:00
|
|
|
return err
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
func (d *DNSProvider) sendRequest(method, resource string, payload interface{}) (*dynResponse, error) {
|
|
|
|
url := fmt.Sprintf("%s/%s", defaultBaseURL, resource)
|
2016-04-27 04:36:48 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
body, err := json.Marshal(payload)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, err
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
req, err := http.NewRequest(method, url, bytes.NewReader(body))
|
2016-04-27 04:36:48 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
if len(d.token) > 0 {
|
|
|
|
req.Header.Set("Auth-Token", d.token)
|
2016-04-27 04:36:48 +02:00
|
|
|
}
|
2016-02-07 09:10:36 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, err
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
defer resp.Body.Close()
|
2018-06-21 19:06:16 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if resp.StatusCode >= 500 {
|
|
|
|
return nil, fmt.Errorf("API request failed with HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
2016-03-19 05:20:58 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
var dynRes dynResponse
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&dynRes)
|
2016-02-07 09:10:36 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, err
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, fmt.Errorf("API request failed with HTTP status code %d: %s", resp.StatusCode, dynRes.Messages)
|
|
|
|
} else if resp.StatusCode == 307 {
|
|
|
|
// TODO add support for HTTP 307 response and long running jobs
|
|
|
|
return nil, fmt.Errorf("API request returned HTTP 307. This is currently unsupported")
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if dynRes.Status == "failure" {
|
|
|
|
// TODO add better error handling
|
|
|
|
return nil, fmt.Errorf("API request failed: %s", dynRes.Messages)
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
return &dynRes, nil
|
2016-02-07 09:10:36 +02:00
|
|
|
}
|