2016-03-17 20:59:15 +00:00
|
|
|
// Package dnsimple implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using dnsimple DNS.
|
2016-02-29 02:48:41 +00:00
|
|
|
package dnsimple
|
2016-01-26 11:14:10 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2016-01-26 11:25:51 +00:00
|
|
|
"fmt"
|
2017-03-17 18:40:51 +00:00
|
|
|
"strconv"
|
2016-01-26 14:09:33 +00:00
|
|
|
"strings"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2016-01-26 11:25:51 +00:00
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
"github.com/dnsimple/dnsimple-go/dnsimple"
|
2016-02-29 02:48:41 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2018-09-15 17:07:24 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-01-26 11:14:10 +00:00
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
AccessToken string
|
|
|
|
BaseURL string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt("DNSIMPLE_TTL", 120),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("DNSIMPLE_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("DNSIMPLE_POLLING_INTERVAL", acme.DefaultPollingInterval),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2016-01-26 11:14:10 +00:00
|
|
|
client *dnsimple.Client
|
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for dnsimple.
|
2017-03-17 18:40:51 +00:00
|
|
|
// Credentials must be passed in the environment variables: DNSIMPLE_OAUTH_TOKEN.
|
|
|
|
//
|
|
|
|
// See: https://developer.dnsimple.com/v2/#authentication
|
2016-03-17 20:59:15 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2018-10-06 13:33:15 +00:00
|
|
|
config.AccessToken = env.GetOrFile("DNSIMPLE_OAUTH_TOKEN")
|
|
|
|
config.BaseURL = env.GetOrFile("DNSIMPLE_BASE_URL")
|
2017-03-17 18:40:51 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-17 20:59:15 +00:00
|
|
|
}
|
2016-01-26 11:25:51 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for DNSimple.
|
|
|
|
// Deprecated
|
2018-05-30 17:53:04 +00:00
|
|
|
func NewDNSProviderCredentials(accessToken, baseURL string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.AccessToken = accessToken
|
|
|
|
config.BaseURL = baseURL
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for DNSimple.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("dnsimple: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.AccessToken == "" {
|
|
|
|
return nil, fmt.Errorf("dnsimple: OAuth token is missing")
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
client := dnsimple.NewClient(dnsimple.NewOauthTokenCredentials(config.AccessToken))
|
|
|
|
client.UserAgent = acme.UserAgent
|
2017-03-17 18:40:51 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.BaseURL != "" {
|
|
|
|
client.BaseURL = config.BaseURL
|
2016-01-26 11:25:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-01-26 11:14:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-09-15 17:07:24 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-01-26 14:09:33 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
zoneName, err := d.getHostedZone(domain)
|
2017-03-17 18:40:51 +00:00
|
|
|
if err != nil {
|
2018-10-09 16:51:49 +00:00
|
|
|
return fmt.Errorf("dnsimple: %v", err)
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
accountID, err := d.getAccountID()
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
2018-10-09 16:51:49 +00:00
|
|
|
return fmt.Errorf("dnsimple: %v", err)
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
recordAttributes := newTxtRecord(zoneName, fqdn, value, d.config.TTL)
|
2018-09-15 17:07:24 +00:00
|
|
|
_, err = d.client.Zones.CreateRecord(accountID, zoneName, recordAttributes)
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
2018-10-09 16:51:49 +00:00
|
|
|
return fmt.Errorf("dnsimple: API call failed: %v", err)
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 11:14:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-02-29 02:48:41 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
2016-01-26 14:09:33 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
records, err := d.findTxtRecords(domain, fqdn)
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
2018-10-09 16:51:49 +00:00
|
|
|
return fmt.Errorf("dnsimple: %v", err)
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
accountID, err := d.getAccountID()
|
2017-03-17 18:40:51 +00:00
|
|
|
if err != nil {
|
2018-10-09 16:51:49 +00:00
|
|
|
return fmt.Errorf("dnsimple: %v", err)
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
var lastErr error
|
2016-01-26 14:09:33 +00:00
|
|
|
for _, rec := range records {
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.Zones.DeleteRecord(accountID, rec.ZoneID, rec.ID)
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
2018-10-09 16:51:49 +00:00
|
|
|
lastErr = fmt.Errorf("dnsimple: %v", err)
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 18:40:51 +00:00
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
return lastErr
|
2016-01-26 11:14:10 +00:00
|
|
|
}
|
2016-01-26 11:42:44 +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
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (string, error) {
|
2017-03-17 18:40:51 +00:00
|
|
|
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
2017-03-17 18:40:51 +00:00
|
|
|
return "", err
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
accountID, err := d.getAccountID()
|
2016-03-20 22:00:00 +00:00
|
|
|
if err != nil {
|
2017-03-17 18:40:51 +00:00
|
|
|
return "", err
|
2016-03-20 22:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
zoneName := acme.UnFqdn(authZone)
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
zones, err := d.client.Zones.ListZones(accountID, &dnsimple.ZoneListOptions{NameLike: zoneName})
|
2017-03-17 18:40:51 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return "", fmt.Errorf("API call failed: %v", err)
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var hostedZone dnsimple.Zone
|
|
|
|
for _, zone := range zones.Data {
|
|
|
|
if zone.Name == zoneName {
|
2016-03-20 22:00:00 +00:00
|
|
|
hostedZone = zone
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-20 22:00:00 +00:00
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
if hostedZone.ID == 0 {
|
2018-05-30 17:53:04 +00:00
|
|
|
return "", fmt.Errorf("zone %s not found in DNSimple for domain %s", authZone, domain)
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
return hostedZone.Name, nil
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) findTxtRecords(domain, fqdn string) ([]dnsimple.ZoneRecord, error) {
|
|
|
|
zoneName, err := d.getHostedZone(domain)
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
accountID, err := d.getAccountID()
|
2016-01-26 14:09:33 +00:00
|
|
|
if err != nil {
|
2017-03-17 18:40:51 +00:00
|
|
|
return nil, err
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
recordName := extractRecordName(fqdn, zoneName)
|
2017-03-17 18:40:51 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
result, err := d.client.Zones.ListRecords(accountID, zoneName, &dnsimple.ZoneRecordListOptions{Name: recordName, Type: "TXT", ListOptions: dnsimple.ListOptions{}})
|
2017-03-17 18:40:51 +00:00
|
|
|
if err != nil {
|
2018-09-24 19:07:20 +00:00
|
|
|
return nil, fmt.Errorf("API call has failed: %v", err)
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 18:40:51 +00:00
|
|
|
return result.Data, nil
|
2016-01-26 14:09:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
func newTxtRecord(zoneName, fqdn, value string, ttl int) dnsimple.ZoneRecord {
|
|
|
|
name := extractRecordName(fqdn, zoneName)
|
2016-01-26 14:09:33 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return dnsimple.ZoneRecord{
|
2016-01-26 14:09:33 +00:00
|
|
|
Type: "TXT",
|
|
|
|
Name: name,
|
|
|
|
Content: value,
|
|
|
|
TTL: ttl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 16:51:49 +00:00
|
|
|
func extractRecordName(fqdn, domain string) string {
|
2016-03-11 02:20:25 +00:00
|
|
|
name := acme.UnFqdn(fqdn)
|
2016-01-26 14:09:33 +00:00
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
2017-03-17 18:40:51 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) getAccountID() (string, error) {
|
|
|
|
whoamiResponse, err := d.client.Identity.Whoami()
|
2017-03-17 18:40:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if whoamiResponse.Data.Account == nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return "", fmt.Errorf("user tokens are not supported, please use an account token")
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-15 13:49:13 +00:00
|
|
|
return strconv.FormatInt(whoamiResponse.Data.Account.ID, 10), nil
|
2017-03-17 18:40:51 +00:00
|
|
|
}
|