2016-03-17 20:59:15 +00:00
|
|
|
// Package route53 implements a DNS provider for solving the DNS-01 challenge
|
2016-03-26 03:34:31 +00:00
|
|
|
// using AWS Route 53 DNS.
|
2016-02-29 02:48:41 +00:00
|
|
|
package route53
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-26 03:34:31 +00:00
|
|
|
"math/rand"
|
2017-07-17 19:50:53 +00:00
|
|
|
"os"
|
2015-12-03 20:01:46 +00:00
|
|
|
"strings"
|
2016-01-22 17:50:18 +00:00
|
|
|
"time"
|
2016-01-22 17:18:53 +00:00
|
|
|
|
2016-03-26 03:34:31 +00: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 02:48:41 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
const (
|
|
|
|
maxRetries = 5
|
2016-06-15 00:03:31 +00:00
|
|
|
route53TTL = 10
|
2016-03-26 03:34:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DNSProvider implements the acme.ChallengeProvider interface
|
2016-03-11 02:46:09 +00:00
|
|
|
type DNSProvider struct {
|
2017-07-17 19:50:53 +00:00
|
|
|
client *route53.Route53
|
|
|
|
hostedZoneID string
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:34:31 +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
|
2016-03-17 20:59:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:34:31 +00: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 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:34:31 +00: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
|
|
|
|
//
|
2017-07-17 19:50:53 +00:00
|
|
|
// If AWS_HOSTED_ZONE_ID is not set, Lego tries to determine the correct
|
|
|
|
// public hosted zone via the FQDN.
|
|
|
|
//
|
2016-03-26 03:34:31 +00:00
|
|
|
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2017-07-17 19:50:53 +00:00
|
|
|
hostedZoneID := os.Getenv("AWS_HOSTED_ZONE_ID")
|
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
r := customRetryer{}
|
|
|
|
r.NumMaxRetries = maxRetries
|
|
|
|
config := request.WithRetryer(aws.NewConfig(), r)
|
|
|
|
client := route53.New(session.New(config))
|
2016-02-14 06:24:19 +00:00
|
|
|
|
2017-07-17 19:50:53 +00:00
|
|
|
return &DNSProvider{
|
|
|
|
client: client,
|
|
|
|
hostedZoneID: hostedZoneID,
|
|
|
|
}, nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters
|
2016-03-11 02:46:09 +00:00
|
|
|
func (r *DNSProvider) Present(domain, token, keyAuth string) error {
|
2016-06-15 00:03:31 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-02-03 06:04:12 +00:00
|
|
|
value = `"` + value + `"`
|
2016-06-15 00:03:31 +00:00
|
|
|
return r.changeRecord("UPSERT", fqdn, value, route53TTL)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2016-03-11 02:46:09 +00:00
|
|
|
func (r *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2016-06-15 00:03:31 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-02-03 06:04:12 +00:00
|
|
|
value = `"` + value + `"`
|
2016-06-15 00:03:31 +00:00
|
|
|
return r.changeRecord("DELETE", fqdn, value, route53TTL)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
|
2017-07-17 19:50:53 +00:00
|
|
|
hostedZoneID, err := r.getHostedZoneID(fqdn)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
2016-04-21 13:47:43 +00:00
|
|
|
return fmt.Errorf("Failed to determine Route 53 hosted zone ID: %v", err)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-03-26 03:34:31 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
recordSet := newTXTRecordSet(fqdn, value, ttl)
|
2016-03-26 03:34:31 +00: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 06:04:12 +00:00
|
|
|
if err != nil {
|
2016-04-21 13:47:43 +00:00
|
|
|
return fmt.Errorf("Failed to change Route 53 record set: %v", err)
|
2016-02-03 06:04:12 +00:00
|
|
|
}
|
|
|
|
|
2016-04-07 23:21:01 +00:00
|
|
|
statusID := resp.ChangeInfo.Id
|
2016-03-26 03:34:31 +00:00
|
|
|
|
|
|
|
return acme.WaitFor(120*time.Second, 4*time.Second, func() (bool, error) {
|
|
|
|
reqParams := &route53.GetChangeInput{
|
2016-04-07 23:21:01 +00:00
|
|
|
Id: statusID,
|
2016-03-26 03:34:31 +00:00
|
|
|
}
|
|
|
|
resp, err := r.client.GetChange(reqParams)
|
2016-02-03 06:04:12 +00:00
|
|
|
if err != nil {
|
2016-04-21 13:47:43 +00:00
|
|
|
return false, fmt.Errorf("Failed to query Route 53 change status: %v", err)
|
2016-02-03 06:04:12 +00:00
|
|
|
}
|
2016-03-26 03:34:31 +00:00
|
|
|
if *resp.ChangeInfo.Status == route53.ChangeStatusInsync {
|
2016-02-03 06:04:12 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:50:53 +00:00
|
|
|
func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
|
|
|
|
if r.hostedZoneID != "" {
|
|
|
|
return r.hostedZoneID, nil
|
|
|
|
}
|
|
|
|
|
2016-04-11 22:59:59 +00:00
|
|
|
authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-01-22 17:50:18 +00:00
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
// .DNSName should not have a trailing dot
|
|
|
|
reqParams := &route53.ListHostedZonesByNameInput{
|
2016-04-21 13:47:43 +00:00
|
|
|
DNSName: aws.String(acme.UnFqdn(authZone)),
|
2016-01-22 17:50:18 +00:00
|
|
|
}
|
2017-07-17 19:50:53 +00:00
|
|
|
resp, err := r.client.ListHostedZonesByName(reqParams)
|
2016-03-20 22:00:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:47:43 +00:00
|
|
|
var hostedZoneID string
|
|
|
|
for _, hostedZone := range resp.HostedZones {
|
|
|
|
// .Name has a trailing dot
|
|
|
|
if !*hostedZone.Config.PrivateZone && *hostedZone.Name == authZone {
|
|
|
|
hostedZoneID = *hostedZone.Id
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hostedZoneID) == 0 {
|
|
|
|
return "", fmt.Errorf("Zone %s not found in Route 53 for domain %s", authZone, fqdn)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 13:47:43 +00:00
|
|
|
if strings.HasPrefix(hostedZoneID, "/hostedzone/") {
|
|
|
|
hostedZoneID = strings.TrimPrefix(hostedZoneID, "/hostedzone/")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-02-03 06:04:12 +00:00
|
|
|
|
2016-04-21 13:47:43 +00:00
|
|
|
return hostedZoneID, nil
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-01-22 17:50:18 +00:00
|
|
|
|
2016-03-26 03:34:31 +00: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 17:50:18 +00:00
|
|
|
}
|
|
|
|
}
|