2016-03-11 02:46:09 +00:00
|
|
|
// Package route53 implements a DNS provider for solving the DNS-01 challenge using route53 DNS.
|
2016-02-29 02:48:41 +00:00
|
|
|
package route53
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-01-22 17:50:18 +00:00
|
|
|
"time"
|
2016-01-22 17:18:53 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
|
|
|
"github.com/mitchellh/goamz/route53"
|
2016-02-29 02:48:41 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface
|
|
|
|
type DNSProvider struct {
|
2015-12-03 20:01:46 +00:00
|
|
|
client *route53.Route53
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance with a configured route53 client.
|
2016-02-07 00:38:40 +00:00
|
|
|
// Authentication is either done using the passed credentials or - when empty - falling back to
|
2016-02-15 02:59:43 +00:00
|
|
|
// the customary AWS credential mechanisms, including the file referenced by $AWS_CREDENTIAL_FILE
|
2016-02-07 00:38:40 +00:00
|
|
|
// (defaulting to $HOME/.aws/credentials) optionally scoped to $AWS_PROFILE, credentials
|
|
|
|
// supplied by the environment variables AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY [ + AWS_SECURITY_TOKEN ],
|
|
|
|
// and finally credentials available via the EC2 instance metadata service.
|
2016-03-11 02:46:09 +00:00
|
|
|
func NewDNSProvider(awsAccessKey, awsSecretKey, awsRegionName string) (*DNSProvider, error) {
|
2015-12-03 20:01:46 +00:00
|
|
|
region, ok := aws.Regions[awsRegionName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Invalid AWS region name %s", awsRegionName)
|
|
|
|
}
|
|
|
|
|
2016-02-07 00:38:40 +00:00
|
|
|
// use aws.GetAuth, which tries really hard to find credentails:
|
|
|
|
// - uses awsAccessKey and awsSecretKey, if provided
|
|
|
|
// - uses AWS_PROFILE / AWS_CREDENTIAL_FILE, if provided
|
|
|
|
// - uses AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY and optionally AWS_SECURITY_TOKEN, if provided
|
|
|
|
// - uses EC2 instance metadata credentials (http://169.254.169.254/latest/meta-data/…), if available
|
|
|
|
// ...and otherwise returns an error
|
2016-02-14 06:24:19 +00:00
|
|
|
auth, err := aws.GetAuth(awsAccessKey, awsSecretKey)
|
|
|
|
if err != nil {
|
2016-02-07 00:38:40 +00:00
|
|
|
return nil, err
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-02-14 06:24:19 +00:00
|
|
|
|
|
|
|
client := route53.New(auth, region)
|
2016-03-11 02:46:09 +00:00
|
|
|
return &DNSProvider{client: client}, 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-02-29 02:48:41 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2016-02-03 06:04:12 +00:00
|
|
|
value = `"` + value + `"`
|
2015-12-03 20:01:46 +00:00
|
|
|
return r.changeRecord("UPSERT", fqdn, value, ttl)
|
|
|
|
}
|
|
|
|
|
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-02-29 02:48:41 +00:00
|
|
|
fqdn, value, ttl := acme.DNS01Record(domain, keyAuth)
|
2016-02-03 06:04:12 +00:00
|
|
|
value = `"` + value + `"`
|
2015-12-03 20:01:46 +00:00
|
|
|
return r.changeRecord("DELETE", fqdn, value, ttl)
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
|
2015-12-03 20:01:46 +00:00
|
|
|
hostedZoneID, err := r.getHostedZoneID(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
recordSet := newTXTRecordSet(fqdn, value, ttl)
|
2016-02-14 06:24:19 +00:00
|
|
|
update := route53.Change{Action: action, Record: recordSet}
|
2015-12-03 20:01:46 +00:00
|
|
|
changes := []route53.Change{update}
|
|
|
|
req := route53.ChangeResourceRecordSetsRequest{Comment: "Created by Lego", Changes: changes}
|
2016-02-03 06:04:12 +00:00
|
|
|
resp, err := r.client.ChangeResourceRecordSets(hostedZoneID, &req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-11 02:20:25 +00:00
|
|
|
return acme.WaitFor(90, 5, func() (bool, error) {
|
2016-02-03 06:04:12 +00:00
|
|
|
status, err := r.client.GetChange(resp.ChangeInfo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if status == "INSYNC" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
})
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 02:46:09 +00:00
|
|
|
func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
|
2016-01-22 17:50:18 +00:00
|
|
|
zones := []route53.HostedZone{}
|
|
|
|
zoneResp, err := r.client.ListHostedZones("", 0)
|
2015-12-03 20:01:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-01-22 17:50:18 +00:00
|
|
|
zones = append(zones, zoneResp.HostedZones...)
|
|
|
|
|
|
|
|
for zoneResp.IsTruncated {
|
|
|
|
resp, err := r.client.ListHostedZones(zoneResp.Marker, 0)
|
|
|
|
if err != nil {
|
|
|
|
if rateExceeded(err) {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
zoneResp = resp
|
|
|
|
zones = append(zones, zoneResp.HostedZones...)
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
var hostedZone route53.HostedZone
|
2016-01-22 17:50:18 +00:00
|
|
|
for _, zone := range zones {
|
2015-12-03 20:01:46 +00:00
|
|
|
if strings.HasSuffix(fqdn, zone.Name) {
|
|
|
|
if len(zone.Name) > len(hostedZone.Name) {
|
|
|
|
hostedZone = zone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hostedZone.ID == "" {
|
2016-01-22 17:50:18 +00:00
|
|
|
return "", fmt.Errorf("No Route53 hosted zone found for domain %s", fqdn)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostedZone.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTXTRecordSet(fqdn, value string, ttl int) route53.ResourceRecordSet {
|
|
|
|
return route53.ResourceRecordSet{
|
|
|
|
Name: fqdn,
|
|
|
|
Type: "TXT",
|
|
|
|
Records: []string{value},
|
|
|
|
TTL: ttl,
|
|
|
|
}
|
2016-02-03 06:04:12 +00:00
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
2016-01-22 17:50:18 +00:00
|
|
|
|
|
|
|
// Route53 API has pretty strict rate limits (5req/s globally per account)
|
|
|
|
// Hence we check if we are being throttled to maybe retry the request
|
2016-01-15 04:06:25 +00:00
|
|
|
func rateExceeded(err error) bool {
|
2016-01-22 17:50:18 +00:00
|
|
|
if strings.Contains(err.Error(), "Throttling") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|