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

171 lines
4.6 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Package linode implements a DNS provider for solving the DNS-01 challenge
// using Linode DNS.
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"
2018-05-31 07:30:04 +00:00
"github.com/xenolf/lego/acme"
2018-07-03 10:44:04 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
const (
dnsMinTTLSecs = 300
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),
TTL: env.GetOrDefaultInt("LINODE_TTL", 60),
}
}
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
// NewDNSProviderCredentials uses the supplied credentials
// to return a DNSProvider instance configured for Linode.
// Deprecated
2017-02-07 21:33:23 +00:00
func NewDNSProviderCredentials(apiKey string) (*DNSProvider, error) {
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.APIKey = apiKey
return NewDNSProviderConfig(config)
}
// 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
}
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) +
(dnsMinTTLSecs * time.Second) +
(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 {
2018-05-31 07:30:04 +00:00
fqdn, value, _ := acme.DNS01Record(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
}
2018-09-17 13:16:03 +00:00
if _, err = d.client.CreateDomainResourceTXT(zone.domainID, acme.UnFqdn(fqdn), value, 60); 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 {
2018-05-31 07:30:04 +00:00
fqdn, value, _ := acme.DNS01Record(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
}
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.
2018-05-31 07:30:04 +00:00
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
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.
2018-09-17 13:16:03 +00:00
domain, err := d.client.GetDomain(acme.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
}