lego/providers/dns/dnsimple/dnsimple.go

230 lines
6.2 KiB
Go
Raw Normal View History

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