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

223 lines
6.7 KiB
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Package route53 implements a DNS provider for solving the DNS-01 challenge
// using AWS Route 53 DNS.
package route53
import (
2018-07-23 15:30:03 +00:00
"errors"
2017-02-07 21:33:23 +00:00
"fmt"
"math/rand"
2017-10-31 09:42:03 +00:00
"os"
2017-02-07 21:33:23 +00:00
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
2018-05-31 07:30:04 +00:00
"github.com/xenolf/lego/acme"
2018-09-14 08:06:03 +00:00
"github.com/xenolf/lego/platform/config/env"
2017-02-07 21:33:23 +00:00
)
2018-07-23 15:30:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
MaxRetries int
TTL int
PropagationTimeout time.Duration
PollingInterval time.Duration
HostedZoneID string
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
2018-09-14 08:06:03 +00:00
propagationMins := env.GetOrDefaultInt("AWS_PROPAGATION_TIMEOUT", 2)
intervalSecs := env.GetOrDefaultInt("AWS_POLLING_INTERVAL", 4)
2018-07-23 15:30:03 +00:00
return &Config{
2018-09-14 08:06:03 +00:00
MaxRetries: env.GetOrDefaultInt("AWS_MAX_RETRIES", 5),
TTL: env.GetOrDefaultInt("AWS_TTL", 10),
PropagationTimeout: time.Second * time.Duration(propagationMins),
PollingInterval: time.Second * time.Duration(intervalSecs),
2018-07-23 15:30:03 +00:00
HostedZoneID: os.Getenv("AWS_HOSTED_ZONE_ID"),
}
}
2017-02-07 21:33:23 +00:00
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-07-23 15:30:03 +00:00
client *route53.Route53
config *Config
2017-02-07 21:33:23 +00:00
}
// customRetryer implements the client.Retryer interface by composing the
// DefaultRetryer. It controls the logic for retrying recoverable request
// errors (e.g. when rate limits are exceeded).
type customRetryer struct {
client.DefaultRetryer
}
// RetryRules overwrites the DefaultRetryer's method.
// It uses a basic exponential backoff algorithm that returns an initial
// delay of ~400ms with an upper limit of ~30 seconds which should prevent
// causing a high number of consecutive throttling errors.
// For reference: Route 53 enforces an account-wide(!) 5req/s query limit.
func (d customRetryer) RetryRules(r *request.Request) time.Duration {
retryCount := r.RetryCount
if retryCount > 7 {
retryCount = 7
}
delay := (1 << uint(retryCount)) * (rand.Intn(50) + 200)
return time.Duration(delay) * time.Millisecond
}
// NewDNSProvider returns a DNSProvider instance configured for the AWS
// Route 53 service.
//
// AWS Credentials are automatically detected in the following locations
// and prioritized in the following order:
// 1. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY,
// AWS_REGION, [AWS_SESSION_TOKEN]
// 2. Shared credentials file (defaults to ~/.aws/credentials)
// 3. Amazon EC2 IAM role
//
2017-10-31 09:42:03 +00:00
// If AWS_HOSTED_ZONE_ID is not set, Lego tries to determine the correct
// public hosted zone via the FQDN.
//
2017-02-07 21:33:23 +00:00
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
func NewDNSProvider() (*DNSProvider, error) {
2018-07-23 15:30:03 +00:00
return NewDNSProviderConfig(NewDefaultConfig())
}
// NewDNSProviderConfig takes a given config ans returns a custom configured
// DNSProvider instance
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("the configuration of the Route53 DNS provider is nil")
}
2017-10-31 09:42:03 +00:00
2017-02-07 21:33:23 +00:00
r := customRetryer{}
2018-07-23 15:30:03 +00:00
r.NumMaxRetries = config.MaxRetries
sessionCfg := request.WithRetryer(aws.NewConfig(), r)
session, err := session.NewSessionWithOptions(session.Options{Config: *sessionCfg})
2018-05-31 07:30:04 +00:00
if err != nil {
return nil, err
}
client := route53.New(session)
2017-02-07 21:33:23 +00:00
2017-10-31 09:42:03 +00:00
return &DNSProvider{
2018-07-23 15:30:03 +00:00
client: client,
config: config,
2017-10-31 09:42:03 +00:00
}, nil
2017-02-07 21:33:23 +00:00
}
2018-07-23 15:30:03 +00:00
// Timeout returns the timeout and interval to use when checking for DNS
// propagation.
func (r *DNSProvider) Timeout() (timeout, interval time.Duration) {
return r.config.PropagationTimeout, r.config.PollingInterval
}
2017-02-07 21:33:23 +00:00
// Present creates a TXT record using the specified parameters
func (r *DNSProvider) Present(domain, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
value = `"` + value + `"`
2018-07-23 15:30:03 +00:00
return r.changeRecord("UPSERT", fqdn, value, r.config.TTL)
2017-02-07 21:33:23 +00:00
}
// CleanUp removes the TXT record matching the specified parameters
func (r *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
2017-02-07 21:33:23 +00:00
value = `"` + value + `"`
2018-07-23 15:30:03 +00:00
return r.changeRecord("DELETE", fqdn, value, r.config.TTL)
2017-02-07 21:33:23 +00:00
}
func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
2017-10-31 09:42:03 +00:00
hostedZoneID, err := r.getHostedZoneID(fqdn)
2017-02-07 21:33:23 +00:00
if err != nil {
2018-05-31 07:30:04 +00:00
return fmt.Errorf("failed to determine Route 53 hosted zone ID: %v", err)
2017-02-07 21:33:23 +00:00
}
recordSet := newTXTRecordSet(fqdn, value, ttl)
reqParams := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(hostedZoneID),
ChangeBatch: &route53.ChangeBatch{
Comment: aws.String("Managed by Lego"),
Changes: []*route53.Change{
{
Action: aws.String(action),
ResourceRecordSet: recordSet,
},
},
},
}
resp, err := r.client.ChangeResourceRecordSets(reqParams)
if err != nil {
2018-05-31 07:30:04 +00:00
return fmt.Errorf("failed to change Route 53 record set: %v", err)
2017-02-07 21:33:23 +00:00
}
statusID := resp.ChangeInfo.Id
2018-07-23 15:30:03 +00:00
return acme.WaitFor(r.config.PropagationTimeout, r.config.PollingInterval, func() (bool, error) {
2017-02-07 21:33:23 +00:00
reqParams := &route53.GetChangeInput{
Id: statusID,
}
resp, err := r.client.GetChange(reqParams)
if err != nil {
2018-05-31 07:30:04 +00:00
return false, fmt.Errorf("failed to query Route 53 change status: %v", err)
2017-02-07 21:33:23 +00:00
}
2018-05-31 07:30:04 +00:00
if aws.StringValue(resp.ChangeInfo.Status) == route53.ChangeStatusInsync {
2017-02-07 21:33:23 +00:00
return true, nil
}
return false, nil
})
}
2017-10-31 09:42:03 +00:00
func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
2018-07-23 15:30:03 +00:00
if r.config.HostedZoneID != "" {
return r.config.HostedZoneID, nil
2017-10-31 09:42:03 +00:00
}
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 "", err
}
// .DNSName should not have a trailing dot
reqParams := &route53.ListHostedZonesByNameInput{
2018-05-31 07:30:04 +00:00
DNSName: aws.String(acme.UnFqdn(authZone)),
2017-02-07 21:33:23 +00:00
}
2017-10-31 09:42:03 +00:00
resp, err := r.client.ListHostedZonesByName(reqParams)
2017-02-07 21:33:23 +00:00
if err != nil {
return "", err
}
var hostedZoneID string
for _, hostedZone := range resp.HostedZones {
// .Name has a trailing dot
2018-05-31 07:30:04 +00:00
if !aws.BoolValue(hostedZone.Config.PrivateZone) && aws.StringValue(hostedZone.Name) == authZone {
hostedZoneID = aws.StringValue(hostedZone.Id)
2017-02-07 21:33:23 +00:00
break
}
}
if len(hostedZoneID) == 0 {
2018-05-31 07:30:04 +00:00
return "", fmt.Errorf("zone %s not found in Route 53 for domain %s", authZone, fqdn)
2017-02-07 21:33:23 +00:00
}
if strings.HasPrefix(hostedZoneID, "/hostedzone/") {
hostedZoneID = strings.TrimPrefix(hostedZoneID, "/hostedzone/")
}
return hostedZoneID, nil
}
func newTXTRecordSet(fqdn, value string, ttl int) *route53.ResourceRecordSet {
return &route53.ResourceRecordSet{
Name: aws.String(fqdn),
Type: aws.String("TXT"),
TTL: aws.Int64(int64(ttl)),
ResourceRecords: []*route53.ResourceRecord{
{Value: aws.String(value)},
},
}
}