2018-04-06 15:04:03 +00:00
|
|
|
// Package lightsail implements a DNS provider for solving the DNS-01 challenge
|
|
|
|
// using AWS Lightsail DNS.
|
|
|
|
package lightsail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
2018-07-03 10:44:04 +00:00
|
|
|
"os"
|
2018-04-06 15:04:03 +00:00
|
|
|
"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/lightsail"
|
2018-05-31 07:30:04 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2018-04-06 15:04:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxRetries = 5
|
|
|
|
)
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
// DNSProvider implements the acme.ChallengeProvider interface
|
2018-04-06 15:04:03 +00:00
|
|
|
type DNSProvider struct {
|
2018-07-03 10:44:04 +00:00
|
|
|
client *lightsail.Lightsail
|
|
|
|
dnsZone string
|
2018-04-06 15:04:03 +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.
|
2018-07-03 10:44:04 +00:00
|
|
|
func (c customRetryer) RetryRules(r *request.Request) time.Duration {
|
2018-04-06 15:04:03 +00:00
|
|
|
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
|
|
|
|
// Lightsail 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_SESSION_TOKEN], [DNS_ZONE]
|
|
|
|
// 2. Shared credentials file (defaults to ~/.aws/credentials)
|
|
|
|
// 3. Amazon EC2 IAM role
|
|
|
|
//
|
|
|
|
// public hosted zone via the FQDN.
|
|
|
|
//
|
|
|
|
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
r := customRetryer{}
|
|
|
|
r.NumMaxRetries = maxRetries
|
2018-07-03 10:44:04 +00:00
|
|
|
|
|
|
|
config := aws.NewConfig().WithRegion("us-east-1")
|
|
|
|
sess, err := session.NewSession(request.WithRetryer(config, r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-06 15:04:03 +00:00
|
|
|
|
|
|
|
return &DNSProvider{
|
2018-07-03 10:44:04 +00:00
|
|
|
dnsZone: os.Getenv("DNS_ZONE"),
|
|
|
|
client: lightsail.New(sess),
|
2018-04-06 15:04:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters
|
2018-07-03 10:44:04 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-05-31 07:30:04 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2018-04-06 15:04:03 +00:00
|
|
|
value = `"` + value + `"`
|
2018-07-03 10:44:04 +00:00
|
|
|
|
|
|
|
err := d.newTxtRecord(domain, fqdn, value)
|
2018-04-06 15:04:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters
|
2018-07-03 10:44:04 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-05-31 07:30:04 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2018-04-06 15:04:03 +00:00
|
|
|
value = `"` + value + `"`
|
|
|
|
params := &lightsail.DeleteDomainEntryInput{
|
2018-07-03 10:44:04 +00:00
|
|
|
DomainName: aws.String(d.dnsZone),
|
2018-04-06 15:04:03 +00:00
|
|
|
DomainEntry: &lightsail.DomainEntry{
|
|
|
|
Name: aws.String(fqdn),
|
|
|
|
Type: aws.String("TXT"),
|
|
|
|
Target: aws.String(value),
|
|
|
|
},
|
|
|
|
}
|
2018-07-03 10:44:04 +00:00
|
|
|
_, err := d.client.DeleteDomainEntry(params)
|
2018-04-06 15:04:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-03 10:44:04 +00:00
|
|
|
func (d *DNSProvider) newTxtRecord(domain string, fqdn string, value string) error {
|
2018-04-06 15:04:03 +00:00
|
|
|
params := &lightsail.CreateDomainEntryInput{
|
2018-07-03 10:44:04 +00:00
|
|
|
DomainName: aws.String(d.dnsZone),
|
2018-04-06 15:04:03 +00:00
|
|
|
DomainEntry: &lightsail.DomainEntry{
|
|
|
|
Name: aws.String(fqdn),
|
|
|
|
Target: aws.String(value),
|
|
|
|
Type: aws.String("TXT"),
|
|
|
|
},
|
|
|
|
}
|
2018-07-03 10:44:04 +00:00
|
|
|
_, err := d.client.CreateDomainEntry(params)
|
2018-04-06 15:04:03 +00:00
|
|
|
return err
|
|
|
|
}
|