2018-12-06 23:50:17 +02:00
|
|
|
// Package route53 implements a DNS provider for solving the DNS-01 challenge using AWS Route 53 DNS.
|
2016-02-29 04:48:41 +02:00
|
|
|
package route53
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
import (
|
2018-07-18 17:37:35 +02:00
|
|
|
"errors"
|
2015-12-03 22:01:46 +02:00
|
|
|
"fmt"
|
2016-03-26 05:34:31 +02:00
|
|
|
"math/rand"
|
2015-12-03 22:01:46 +02:00
|
|
|
"strings"
|
2016-01-22 19:50:18 +02:00
|
|
|
"time"
|
2016-01-22 19:18:53 +02:00
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
"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"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v4/platform/wait"
|
2015-12-03 22:01:46 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "AWS_"
|
|
|
|
|
|
|
|
EnvAccessKeyID = envNamespace + "ACCESS_KEY_ID"
|
|
|
|
EnvSecretAccessKey = envNamespace + "SECRET_ACCESS_KEY"
|
|
|
|
EnvRegion = envNamespace + "REGION"
|
|
|
|
EnvHostedZoneID = envNamespace + "HOSTED_ZONE_ID"
|
|
|
|
EnvMaxRetries = envNamespace + "MAX_RETRIES"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-07-18 17:37:35 +02:00
|
|
|
type Config struct {
|
|
|
|
MaxRetries int
|
|
|
|
TTL int
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
HostedZoneID string
|
2020-02-01 13:10:40 +02:00
|
|
|
Client *route53.Route53
|
2018-07-18 17:37:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-07-18 17:37:35 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
MaxRetries: env.GetOrDefaultInt(EnvMaxRetries, 5),
|
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 10),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 4*time.Second),
|
|
|
|
HostedZoneID: env.GetOrFile(EnvHostedZoneID),
|
2018-07-18 17:37:35 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-03-11 04:46:09 +02:00
|
|
|
type DNSProvider struct {
|
2018-07-18 17:37:35 +02:00
|
|
|
client *route53.Route53
|
|
|
|
config *Config
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02: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).
|
2016-03-26 05:34:31 +02:00
|
|
|
type customRetryer struct {
|
|
|
|
client.DefaultRetryer
|
2016-03-17 22:59:15 +02:00
|
|
|
}
|
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
// RetryRules overwrites the DefaultRetryer's method.
|
2018-10-09 19:03:07 +02:00
|
|
|
// 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.
|
2016-03-26 05:34:31 +02:00
|
|
|
// 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
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
delay := (1 << uint(retryCount)) * (rand.Intn(50) + 200)
|
|
|
|
return time.Duration(delay) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for the AWS Route 53 service.
|
2016-03-26 05:34:31 +02:00
|
|
|
//
|
2018-10-09 19:03:07 +02:00
|
|
|
// AWS Credentials are automatically detected in the following locations and prioritized in the following order:
|
2016-03-26 05:34:31 +02:00
|
|
|
// 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
|
|
|
|
//
|
2018-10-09 19:03:07 +02:00
|
|
|
// If AWS_HOSTED_ZONE_ID is not set, Lego tries to determine the correct public hosted zone via the FQDN.
|
2017-07-17 21:50:53 +02:00
|
|
|
//
|
2016-03-26 05:34:31 +02:00
|
|
|
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-07-18 17:37:35 +02:00
|
|
|
return NewDNSProviderConfig(NewDefaultConfig())
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDNSProviderConfig takes a given config ans returns a custom configured DNSProvider instance.
|
2018-07-18 17:37:35 +02:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, errors.New("route53: the configuration of the Route53 DNS provider is nil")
|
2018-07-18 17:37:35 +02:00
|
|
|
}
|
2017-07-17 21:50:53 +02:00
|
|
|
|
2020-02-01 13:10:40 +02:00
|
|
|
if config.Client != nil {
|
|
|
|
return &DNSProvider{client: config.Client, config: config}, nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
retry := customRetryer{}
|
|
|
|
retry.NumMaxRetries = config.MaxRetries
|
|
|
|
sessionCfg := request.WithRetryer(aws.NewConfig(), retry)
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
sess, err := session.NewSessionWithOptions(session.Options{Config: *sessionCfg})
|
2018-04-12 15:08:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-14 08:24:19 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
cl := route53.New(sess)
|
|
|
|
return &DNSProvider{client: cl, config: config}, nil
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
2018-10-09 19:03:07 +02:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-07-18 17:37:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-10-09 19:03:07 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
hostedZoneID, err := d.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("route53: failed to determine hosted zone ID: %w", err)
|
2018-10-09 19:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
records, err := d.getExistingRecordSets(hostedZoneID, fqdn)
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("route53: %w", err)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
realValue := `"` + value + `"`
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
var found bool
|
|
|
|
for _, record := range records {
|
|
|
|
if aws.StringValue(record.Value) == realValue {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
records = append(records, &route53.ResourceRecord{Value: aws.String(realValue)})
|
|
|
|
}
|
|
|
|
|
|
|
|
recordSet := &route53.ResourceRecordSet{
|
|
|
|
Name: aws.String(fqdn),
|
|
|
|
Type: aws.String("TXT"),
|
|
|
|
TTL: aws.Int64(int64(d.config.TTL)),
|
|
|
|
ResourceRecords: records,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.changeRecord(route53.ChangeActionUpsert, hostedZoneID, recordSet)
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("route53: %w", err)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
return nil
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-10-09 19:03:07 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 23:50:17 +02:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2018-10-09 19:03:07 +02:00
|
|
|
|
|
|
|
hostedZoneID, err := d.getHostedZoneID(fqdn)
|
2015-12-03 22:01:46 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("failed to determine Route 53 hosted zone ID: %w", err)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
records, err := d.getExistingRecordSets(hostedZoneID, fqdn)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("route53: %w", err)
|
2018-10-09 19:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(records) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
recordSet := &route53.ResourceRecordSet{
|
|
|
|
Name: aws.String(fqdn),
|
|
|
|
Type: aws.String("TXT"),
|
|
|
|
TTL: aws.Int64(int64(d.config.TTL)),
|
|
|
|
ResourceRecords: records,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.changeRecord(route53.ChangeActionDelete, hostedZoneID, recordSet)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("route53: %w", err)
|
2018-10-09 19:03:07 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) changeRecord(action, hostedZoneID string, recordSet *route53.ResourceRecordSet) error {
|
|
|
|
recordSetInput := &route53.ChangeResourceRecordSetsInput{
|
2016-03-26 05:34:31 +02:00
|
|
|
HostedZoneId: aws.String(hostedZoneID),
|
|
|
|
ChangeBatch: &route53.ChangeBatch{
|
|
|
|
Comment: aws.String("Managed by Lego"),
|
2018-10-09 19:03:07 +02:00
|
|
|
Changes: []*route53.Change{{
|
|
|
|
Action: aws.String(action),
|
|
|
|
ResourceRecordSet: recordSet,
|
|
|
|
}},
|
2016-03-26 05:34:31 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
resp, err := d.client.ChangeResourceRecordSets(recordSetInput)
|
2016-02-03 08:04:12 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("failed to change record set: %w", err)
|
2016-02-03 08:04:12 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
changeID := resp.ChangeInfo.Id
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
return wait.For("route53", d.config.PropagationTimeout, d.config.PollingInterval, func() (bool, error) {
|
2018-10-09 19:03:07 +02:00
|
|
|
reqParams := &route53.GetChangeInput{Id: changeID}
|
|
|
|
|
|
|
|
resp, err := d.client.GetChange(reqParams)
|
2016-02-03 08:04:12 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return false, fmt.Errorf("failed to query change status: %w", err)
|
2016-02-03 08:04:12 +02:00
|
|
|
}
|
2018-10-09 19:03:07 +02:00
|
|
|
|
2018-05-30 19:53:04 +02:00
|
|
|
if aws.StringValue(resp.ChangeInfo.Status) == route53.ChangeStatusInsync {
|
2016-02-03 08:04:12 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
2018-10-09 19:03:07 +02:00
|
|
|
return false, fmt.Errorf("unable to retrieve change: ID=%s", aws.StringValue(changeID))
|
2016-02-03 08:04:12 +02:00
|
|
|
})
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 01:48:18 +02:00
|
|
|
func (d *DNSProvider) getExistingRecordSets(hostedZoneID, fqdn string) ([]*route53.ResourceRecord, error) {
|
2018-10-09 19:03:07 +02:00
|
|
|
listInput := &route53.ListResourceRecordSetsInput{
|
|
|
|
HostedZoneId: aws.String(hostedZoneID),
|
|
|
|
StartRecordName: aws.String(fqdn),
|
|
|
|
StartRecordType: aws.String("TXT"),
|
|
|
|
}
|
|
|
|
|
|
|
|
recordSetsOutput, err := d.client.ListResourceRecordSets(listInput)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if recordSetsOutput == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []*route53.ResourceRecord
|
|
|
|
|
|
|
|
for _, recordSet := range recordSetsOutput.ResourceRecordSets {
|
|
|
|
if aws.StringValue(recordSet.Name) == fqdn {
|
|
|
|
records = append(records, recordSet.ResourceRecords...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
|
|
|
|
if d.config.HostedZoneID != "" {
|
|
|
|
return d.config.HostedZoneID, nil
|
2017-07-17 21:50:53 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
2015-12-03 22:01:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-01-22 19:50:18 +02:00
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
// .DNSName should not have a trailing dot
|
|
|
|
reqParams := &route53.ListHostedZonesByNameInput{
|
2018-12-06 23:50:17 +02:00
|
|
|
DNSName: aws.String(dns01.UnFqdn(authZone)),
|
2016-01-22 19:50:18 +02:00
|
|
|
}
|
2018-10-09 19:03:07 +02:00
|
|
|
resp, err := d.client.ListHostedZonesByName(reqParams)
|
2016-03-21 00:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:47:43 +02:00
|
|
|
var hostedZoneID string
|
|
|
|
for _, hostedZone := range resp.HostedZones {
|
|
|
|
// .Name has a trailing dot
|
2018-05-30 19:53:04 +02:00
|
|
|
if !aws.BoolValue(hostedZone.Config.PrivateZone) && aws.StringValue(hostedZone.Name) == authZone {
|
|
|
|
hostedZoneID = aws.StringValue(hostedZone.Id)
|
2016-04-21 15:47:43 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hostedZoneID) == 0 {
|
2018-09-15 19:07:24 +02:00
|
|
|
return "", fmt.Errorf("zone %s not found for domain %s", authZone, fqdn)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 15:47:43 +02:00
|
|
|
if strings.HasPrefix(hostedZoneID, "/hostedzone/") {
|
|
|
|
hostedZoneID = strings.TrimPrefix(hostedZoneID, "/hostedzone/")
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2016-02-03 08:04:12 +02:00
|
|
|
|
2016-04-21 15:47:43 +02:00
|
|
|
return hostedZoneID, nil
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|