2018-12-06 23:50:17 +02:00
|
|
|
// Package dnsimple implements a DNS provider for solving the DNS-01 challenge using dnsimple DNS.
|
2019-03-11 18:56:48 +02:00
|
|
|
package dnsimple
|
2016-01-26 13:14:10 +02:00
|
|
|
|
|
|
|
import (
|
2018-12-06 23:50:17 +02:00
|
|
|
"context"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2016-01-26 13:25:51 +02:00
|
|
|
"fmt"
|
2017-03-17 20:40:51 +02:00
|
|
|
"strconv"
|
2018-09-15 19:07:24 +02:00
|
|
|
"time"
|
2016-01-26 13:25:51 +02:00
|
|
|
|
2017-03-17 20:40:51 +02:00
|
|
|
"github.com/dnsimple/dnsimple-go/dnsimple"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2018-12-06 23:50:17 +02:00
|
|
|
"golang.org/x/oauth2"
|
2016-01-26 13:14:10 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "DNSIMPLE_"
|
|
|
|
|
|
|
|
EnvOAuthToken = envNamespace + "OAUTH_TOKEN"
|
|
|
|
EnvBaseURL = envNamespace + "BASE_URL"
|
2022-04-26 00:08:38 +02:00
|
|
|
EnvDebug = envNamespace + "DEBUG"
|
2020-03-12 00:51:10 +02:00
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
2022-04-26 00:08:38 +02:00
|
|
|
Debug bool
|
2018-09-15 19:07:24 +02:00
|
|
|
AccessToken string
|
|
|
|
BaseURL string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
2022-04-26 00:08:38 +02:00
|
|
|
Debug: env.GetOrDefaultBool(EnvDebug, false),
|
2020-03-12 00:51:10 +02:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-03-11 04:46:09 +02:00
|
|
|
type DNSProvider struct {
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
2016-01-26 13:14:10 +02:00
|
|
|
client *dnsimple.Client
|
|
|
|
}
|
|
|
|
|
2016-03-17 22:59:15 +02:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for dnsimple.
|
2020-05-08 19:35:25 +02:00
|
|
|
// Credentials must be passed in the environment variable: DNSIMPLE_OAUTH_TOKEN.
|
2017-03-17 20:40:51 +02:00
|
|
|
//
|
|
|
|
// See: https://developer.dnsimple.com/v2/#authentication
|
2016-03-17 22:59:15 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.AccessToken = env.GetOrFile(EnvOAuthToken)
|
|
|
|
config.BaseURL = env.GetOrFile(EnvBaseURL)
|
2017-03-17 20:40:51 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-17 22:59:15 +02:00
|
|
|
}
|
2016-01-26 13:25:51 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// 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 == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("dnsimple: OAuth token is missing")
|
2017-03-17 20:40:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.AccessToken})
|
|
|
|
client := dnsimple.NewClient(oauth2.NewClient(context.Background(), ts))
|
2020-05-10 17:22:48 +02:00
|
|
|
client.SetUserAgent("go-acme/lego")
|
2017-03-17 20:40:51 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
if config.BaseURL != "" {
|
|
|
|
client.BaseURL = config.BaseURL
|
2016-01-26 13:25:51 +02:00
|
|
|
}
|
|
|
|
|
2022-04-26 00:08:38 +02:00
|
|
|
client.Debug = config.Debug
|
|
|
|
|
2018-10-09 18:51:49 +02:00
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-01-26 13:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2016-01-26 16:09:33 +02:00
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
zoneName, err := d.getHostedZone(fqdn)
|
2017-03-17 20:40:51 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsimple: %w", err)
|
2017-03-17 20:40:51 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
accountID, err := d.getAccountID()
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsimple: %w", err)
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:53:13 +02:00
|
|
|
recordAttributes, err := newTxtRecord(zoneName, fqdn, value, d.config.TTL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dnsimple: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:22:48 +02:00
|
|
|
_, err = d.client.Zones.CreateRecord(context.Background(), accountID, zoneName, recordAttributes)
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsimple: API call failed: %w", err)
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2016-01-26 13:14:10 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2016-01-26 16:09:33 +02:00
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
records, err := d.findTxtRecords(fqdn)
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsimple: %w", err)
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
accountID, err := d.getAccountID()
|
2017-03-17 20:40:51 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("dnsimple: %w", err)
|
2017-03-17 20:40:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 18:51:49 +02:00
|
|
|
var lastErr error
|
2016-01-26 16:09:33 +02:00
|
|
|
for _, rec := range records {
|
2020-05-10 17:22:48 +02:00
|
|
|
_, err := d.client.Zones.DeleteRecord(context.Background(), accountID, rec.ZoneID, rec.ID)
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
lastErr = fmt.Errorf("dnsimple: %w", err)
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-17 20:40:51 +02:00
|
|
|
|
2018-10-09 18:51:49 +02:00
|
|
|
return lastErr
|
2016-01-26 13:14:10 +02:00
|
|
|
}
|
2016-01-26 13:42:44 +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
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (string, error) {
|
2022-11-25 19:12:21 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(domain)
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
2017-03-17 20:40:51 +02:00
|
|
|
return "", err
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
accountID, err := d.getAccountID()
|
2016-03-21 00:00:00 +02:00
|
|
|
if err != nil {
|
2017-03-17 20:40:51 +02:00
|
|
|
return "", err
|
2016-03-21 00:00:00 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
zoneName := dns01.UnFqdn(authZone)
|
2017-03-17 20:40:51 +02:00
|
|
|
|
2020-05-10 17:22:48 +02:00
|
|
|
zones, err := d.client.Zones.ListZones(context.Background(), accountID, &dnsimple.ZoneListOptions{NameLike: &zoneName})
|
2017-03-17 20:40:51 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return "", fmt.Errorf("API call failed: %w", err)
|
2017-03-17 20:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var hostedZone dnsimple.Zone
|
|
|
|
for _, zone := range zones.Data {
|
|
|
|
if zone.Name == zoneName {
|
2016-03-21 00:00:00 +02:00
|
|
|
hostedZone = zone
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 00:00:00 +02:00
|
|
|
|
2017-03-17 20:40:51 +02:00
|
|
|
if hostedZone.ID == 0 {
|
2018-05-30 19:53:04 +02:00
|
|
|
return "", fmt.Errorf("zone %s not found in DNSimple for domain %s", authZone, domain)
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 20:40:51 +02:00
|
|
|
return hostedZone.Name, nil
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2022-11-25 19:12:21 +02:00
|
|
|
func (d *DNSProvider) findTxtRecords(fqdn string) ([]dnsimple.ZoneRecord, error) {
|
|
|
|
zoneName, err := d.getHostedZone(fqdn)
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
accountID, err := d.getAccountID()
|
2016-01-26 16:09:33 +02:00
|
|
|
if err != nil {
|
2017-03-17 20:40:51 +02:00
|
|
|
return nil, err
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:53:13 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-17 20:40:51 +02:00
|
|
|
|
2022-11-28 19:53:13 +02:00
|
|
|
result, err := d.client.Zones.ListRecords(context.Background(), accountID, zoneName, &dnsimple.ZoneRecordListOptions{Name: &subDomain, Type: dnsimple.String("TXT"), ListOptions: dnsimple.ListOptions{}})
|
2017-03-17 20:40:51 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("API call has failed: %w", err)
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 20:40:51 +02:00
|
|
|
return result.Data, nil
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 19:53:13 +02:00
|
|
|
func newTxtRecord(zoneName, fqdn, value string, ttl int) (dnsimple.ZoneRecordAttributes, error) {
|
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
|
|
|
|
if err != nil {
|
|
|
|
return dnsimple.ZoneRecordAttributes{}, err
|
|
|
|
}
|
2016-01-26 16:09:33 +02:00
|
|
|
|
2020-05-10 17:22:48 +02:00
|
|
|
return dnsimple.ZoneRecordAttributes{
|
2016-01-26 16:09:33 +02:00
|
|
|
Type: "TXT",
|
2022-11-28 19:53:13 +02:00
|
|
|
Name: &subDomain,
|
2016-01-26 16:09:33 +02:00
|
|
|
Content: value,
|
|
|
|
TTL: ttl,
|
2022-11-28 19:53:13 +02:00
|
|
|
}, nil
|
2016-01-26 16:09:33 +02:00
|
|
|
}
|
2017-03-17 20:40:51 +02:00
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) getAccountID() (string, error) {
|
2020-05-10 17:22:48 +02:00
|
|
|
whoamiResponse, err := d.client.Identity.Whoami(context.Background())
|
2017-03-17 20:40:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if whoamiResponse.Data.Account == nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return "", errors.New("user tokens are not supported, please use an account token")
|
2017-03-17 20:40:51 +02:00
|
|
|
}
|
|
|
|
|
2018-04-15 15:49:13 +02:00
|
|
|
return strconv.FormatInt(whoamiResponse.Data.Account.ID, 10), nil
|
2017-03-17 20:40:51 +02:00
|
|
|
}
|