traefik/vendor/github.com/xenolf/lego/providers/dns/linode/linode.go

165 lines
4.4 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// Package linode implements a DNS provider for solving the DNS-01 challenge using Linode DNS.
2017-02-07 21:33:23 +00:00
package linode
import (
"errors"
2018-07-03 10:44:04 +00:00
"fmt"
2017-02-07 21:33:23 +00:00
"strings"
"time"
"github.com/timewasted/linode/dns"
2019-01-07 17:30:06 +00:00
"github.com/xenolf/lego/challenge/dns01"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
const (
2018-10-10 14:28:04 +00:00
minTTL = 300
2017-02-07 21:33:23 +00:00
dnsUpdateFreqMins = 15
dnsUpdateFudgeSecs = 120
)
2018-09-17 13:16:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
APIKey string
PollingInterval time.Duration
TTL int
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
PollingInterval: env.GetOrDefaultSecond("LINODE_POLLING_INTERVAL", 15*time.Second),
2018-10-10 14:28:04 +00:00
TTL: env.GetOrDefaultInt("LINODE_TTL", minTTL),
2018-09-17 13:16:03 +00:00
}
}
2017-02-07 21:33:23 +00:00
type hostedZoneInfo struct {
2018-05-31 07:30:04 +00:00
domainID int
2017-02-07 21:33:23 +00:00
resourceName string
}
2018-05-31 07:30:04 +00:00
// DNSProvider implements the acme.ChallengeProvider interface.
2017-02-07 21:33:23 +00:00
type DNSProvider struct {
2018-09-17 13:16:03 +00:00
config *Config
2018-07-03 10:44:04 +00:00
client *dns.DNS
2017-02-07 21:33:23 +00:00
}
// NewDNSProvider returns a DNSProvider instance configured for Linode.
// Credentials must be passed in the environment variable: LINODE_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("LINODE_API_KEY")
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("linode: %v", err)
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIKey = values["LINODE_API_KEY"]
return NewDNSProviderConfig(config)
2017-02-07 21:33:23 +00:00
}
2018-09-17 13:16:03 +00:00
// NewDNSProviderConfig return a DNSProvider instance configured for Linode.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("linode: the configuration of the DNS provider is nil")
}
if len(config.APIKey) == 0 {
return nil, errors.New("linode: credentials missing")
2017-02-07 21:33:23 +00:00
}
2018-10-10 14:28:04 +00:00
if config.TTL < minTTL {
return nil, fmt.Errorf("linode: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
}
2017-02-07 21:33:23 +00:00
return &DNSProvider{
2018-09-17 13:16:03 +00:00
config: config,
client: dns.New(config.APIKey),
2017-02-07 21:33:23 +00:00
}, nil
}
// Timeout returns the timeout and interval to use when checking for DNS
// propagation. Adjusting here to cope with spikes in propagation times.
2018-09-17 13:16:03 +00:00
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
2017-02-07 21:33:23 +00:00
// Since Linode only updates their zone files every X minutes, we need
// to figure out how many minutes we have to wait until we hit the next
// interval of X. We then wait another couple of minutes, just to be
// safe. Hopefully at some point during all of this, the record will
// have propagated throughout Linode's network.
minsRemaining := dnsUpdateFreqMins - (time.Now().Minute() % dnsUpdateFreqMins)
timeout = (time.Duration(minsRemaining) * time.Minute) +
2018-10-10 14:28:04 +00:00
(minTTL * time.Second) +
2017-02-07 21:33:23 +00:00
(dnsUpdateFudgeSecs * time.Second)
2018-09-17 13:16:03 +00:00
interval = d.config.PollingInterval
2017-02-07 21:33:23 +00:00
return
}
// Present creates a TXT record using the specified parameters.
2018-09-17 13:16:03 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(domain, keyAuth)
2018-09-17 13:16:03 +00:00
zone, err := d.getHostedZoneInfo(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
2019-01-07 17:30:06 +00:00
if _, err = d.client.CreateDomainResourceTXT(zone.domainID, dns01.UnFqdn(fqdn), value, d.config.TTL); err != nil {
2017-02-07 21:33:23 +00:00
return err
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
2018-09-17 13:16:03 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(domain, keyAuth)
2018-09-17 13:16:03 +00:00
zone, err := d.getHostedZoneInfo(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
// Get all TXT records for the specified domain.
2018-09-17 13:16:03 +00:00
resources, err := d.client.GetResourcesByType(zone.domainID, "TXT")
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
// Remove the specified resource, if it exists.
for _, resource := range resources {
if resource.Name == zone.resourceName && resource.Target == value {
2018-09-17 13:16:03 +00:00
resp, err := d.client.DeleteDomainResource(resource.DomainID, resource.ResourceID)
2017-02-07 21:33:23 +00:00
if err != nil {
return err
}
2018-10-10 14:28:04 +00:00
2017-02-07 21:33:23 +00:00
if resp.ResourceID != resource.ResourceID {
2018-05-31 07:30:04 +00:00
return errors.New("error deleting resource: resource IDs do not match")
2017-02-07 21:33:23 +00:00
}
break
}
}
return nil
}
2018-09-17 13:16:03 +00:00
func (d *DNSProvider) getHostedZoneInfo(fqdn string) (*hostedZoneInfo, error) {
2017-02-07 21:33:23 +00:00
// Lookup the zone that handles the specified FQDN.
2019-01-07 17:30:06 +00:00
authZone, err := dns01.FindZoneByFqdn(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
2018-09-17 13:16:03 +00:00
2017-02-07 21:33:23 +00:00
resourceName := strings.TrimSuffix(fqdn, "."+authZone)
// Query the authority zone.
2019-01-07 17:30:06 +00:00
domain, err := d.client.GetDomain(dns01.UnFqdn(authZone))
2017-02-07 21:33:23 +00:00
if err != nil {
return nil, err
}
return &hostedZoneInfo{
2018-05-31 07:30:04 +00:00
domainID: domain.DomainID,
2017-02-07 21:33:23 +00:00
resourceName: resourceName,
}, nil
}