2016-03-17 22:59:15 +02:00
|
|
|
// Package route53 implements a DNS provider for solving the DNS-01 challenge
|
2016-03-26 05:34:31 +02:00
|
|
|
// using AWS Route 53 DNS.
|
2016-02-29 04:48:41 +02:00
|
|
|
package route53
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2016-02-29 04:48:41 +02:00
|
|
|
"github.com/xenolf/lego/acme"
|
2015-12-03 22:01:46 +02:00
|
|
|
)
|
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
const (
|
|
|
|
maxRetries = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
// DNSProvider implements the acme.ChallengeProvider interface
|
2016-03-11 04:46:09 +02:00
|
|
|
type DNSProvider struct {
|
2015-12-03 22:01:46 +02:00
|
|
|
client *route53.Route53
|
|
|
|
}
|
|
|
|
|
2016-03-26 05:34:31 +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).
|
|
|
|
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.
|
|
|
|
// 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
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
r := customRetryer{}
|
|
|
|
r.NumMaxRetries = maxRetries
|
|
|
|
config := request.WithRetryer(aws.NewConfig(), r)
|
|
|
|
client := route53.New(session.New(config))
|
2016-02-14 08:24:19 +02:00
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
return &DNSProvider{client: client}, nil
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
// Present creates a TXT record using the specified parameters
|
2016-03-11 04:46:09 +02:00
|
|
|
func (r *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-02-29 04:48:41 +02:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2016-02-03 08:04:12 +02:00
|
|
|
value = `"` + value + `"`
|
2015-12-03 22:01:46 +02:00
|
|
|
return r.changeRecord("UPSERT", fqdn, value, ttl)
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2016-03-11 04:46:09 +02:00
|
|
|
func (r *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-02-29 04:48:41 +02:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2016-02-03 08:04:12 +02:00
|
|
|
value = `"` + value + `"`
|
2015-12-03 22:01:46 +02:00
|
|
|
return r.changeRecord("DELETE", fqdn, value, ttl)
|
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
|
2015-12-03 22:01:46 +02:00
|
|
|
hostedZoneID, err := r.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2015-12-03 22:01:46 +02:00
|
|
|
recordSet := newTXTRecordSet(fqdn, value, ttl)
|
2016-03-26 05:34:31 +02:00
|
|
|
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)
|
2016-02-03 08:04:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-08 01:21:01 +02:00
|
|
|
statusID := resp.ChangeInfo.Id
|
2016-03-26 05:34:31 +02:00
|
|
|
|
|
|
|
return acme.WaitFor(120*time.Second, 4*time.Second, func() (bool, error) {
|
|
|
|
reqParams := &route53.GetChangeInput{
|
2016-04-08 01:21:01 +02:00
|
|
|
Id: statusID,
|
2016-03-26 05:34:31 +02:00
|
|
|
}
|
|
|
|
resp, err := r.client.GetChange(reqParams)
|
2016-02-03 08:04:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-03-26 05:34:31 +02:00
|
|
|
if *resp.ChangeInfo.Status == route53.ChangeStatusInsync {
|
2016-02-03 08:04:12 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
|
2016-04-12 00:59:59 +02:00
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
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{
|
|
|
|
DNSName: aws.String(acme.UnFqdn(authZone)),
|
|
|
|
MaxItems: aws.String("1"),
|
2016-01-22 19:50:18 +02:00
|
|
|
}
|
2016-03-26 05:34:31 +02:00
|
|
|
resp, err := r.client.ListHostedZonesByName(reqParams)
|
2016-03-21 00:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
// .Name has a trailing dot
|
|
|
|
if len(resp.HostedZones) == 0 || *resp.HostedZones[0].Name != authZone {
|
2016-03-21 00:00:00 +02:00
|
|
|
return "", fmt.Errorf("Zone %s not found in Route53 for domain %s", authZone, fqdn)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 01:21:01 +02:00
|
|
|
zoneID := *resp.HostedZones[0].Id
|
|
|
|
if strings.HasPrefix(zoneID, "/hostedzone/") {
|
|
|
|
zoneID = strings.TrimPrefix(zoneID, "/hostedzone/")
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2016-02-03 08:04:12 +02:00
|
|
|
|
2016-04-08 01:21:01 +02:00
|
|
|
return zoneID, nil
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2016-01-22 19:50:18 +02:00
|
|
|
|
2016-03-26 05:34:31 +02:00
|
|
|
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)},
|
|
|
|
},
|
2016-01-22 19:50:18 +02:00
|
|
|
}
|
|
|
|
}
|