2016-08-29 19:37:19 +00:00
|
|
|
// Package linode implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using Linode DNS.
|
|
|
|
package linode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-06-11 15:32:50 +00:00
|
|
|
"fmt"
|
2016-08-29 19:37:19 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/timewasted/linode/dns"
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2016-08-29 19:37:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-09 16:22:45 +00:00
|
|
|
minTTL = 300
|
2016-08-29 19:37:19 +00:00
|
|
|
dnsUpdateFreqMins = 15
|
|
|
|
dnsUpdateFudgeSecs = 120
|
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +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-09 16:22:45 +00:00
|
|
|
TTL: env.GetOrDefaultInt("LINODE_TTL", minTTL),
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
type hostedZoneInfo struct {
|
2018-05-30 17:53:04 +00:00
|
|
|
domainID int
|
2016-08-29 19:37:19 +00:00
|
|
|
resourceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the acme.ChallengeProvider interface.
|
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2018-06-21 17:06:16 +00:00
|
|
|
client *dns.DNS
|
2016-08-29 19:37:19 +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-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("LINODE_API_KEY")
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("linode: %v", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values["LINODE_API_KEY"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for Linode.
|
|
|
|
// Deprecated
|
2016-08-29 19:37:19 +00:00
|
|
|
func NewDNSProviderCredentials(apiKey string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +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")
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:22:45 +00:00
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("linode: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
return &DNSProvider{
|
2018-09-15 17:07:24 +00:00
|
|
|
config: config,
|
|
|
|
client: dns.New(config.APIKey),
|
2016-08-29 19:37:19 +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-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2016-08-29 19:37:19 +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-09 16:22:45 +00:00
|
|
|
(minTTL * time.Second) +
|
2016-08-29 19:37:19 +00:00
|
|
|
(dnsUpdateFudgeSecs * time.Second)
|
2018-09-15 17:07:24 +00:00
|
|
|
interval = d.config.PollingInterval
|
2016-08-29 19:37:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-08-29 19:37:19 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
zone, err := d.getHostedZoneInfo(fqdn)
|
2016-08-29 19:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-23 11:01:40 +00:00
|
|
|
if _, err = d.client.CreateDomainResourceTXT(zone.domainID, acme.UnFqdn(fqdn), value, d.config.TTL); err != nil {
|
2016-08-29 19:37:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-08-29 19:37:19 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
zone, err := d.getHostedZoneInfo(fqdn)
|
2016-08-29 19:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all TXT records for the specified domain.
|
2018-09-15 17:07:24 +00:00
|
|
|
resources, err := d.client.GetResourcesByType(zone.domainID, "TXT")
|
2016-08-29 19:37:19 +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-15 17:07:24 +00:00
|
|
|
resp, err := d.client.DeleteDomainResource(resource.DomainID, resource.ResourceID)
|
2016-08-29 19:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-23 11:01:40 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
if resp.ResourceID != resource.ResourceID {
|
2018-05-30 17:53:04 +00:00
|
|
|
return errors.New("error deleting resource: resource IDs do not match")
|
2016-08-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) getHostedZoneInfo(fqdn string) (*hostedZoneInfo, error) {
|
2016-08-29 19:37:19 +00:00
|
|
|
// Lookup the zone that handles the specified FQDN.
|
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2016-08-29 19:37:19 +00:00
|
|
|
resourceName := strings.TrimSuffix(fqdn, "."+authZone)
|
|
|
|
|
|
|
|
// Query the authority zone.
|
2018-09-15 17:07:24 +00:00
|
|
|
domain, err := d.client.GetDomain(acme.UnFqdn(authZone))
|
2016-08-29 19:37:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &hostedZoneInfo{
|
2018-05-30 17:53:04 +00:00
|
|
|
domainID: domain.DomainID,
|
2016-08-29 19:37:19 +00:00
|
|
|
resourceName: resourceName,
|
|
|
|
}, nil
|
|
|
|
}
|