2018-12-06 21:50:17 +00:00
|
|
|
// Package lightsail implements a DNS provider for solving the DNS-01 challenge using AWS Lightsail DNS.
|
2019-03-11 16:56:48 +00:00
|
|
|
package lightsail
|
2018-02-18 15:27:58 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-02-18 15:27:58 +00:00
|
|
|
"math/rand"
|
|
|
|
"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"
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2018-02-18 15:27:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxRetries = 5
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "LIGHTSAIL_"
|
|
|
|
|
2021-06-27 19:51:05 +00:00
|
|
|
EnvRegion = envNamespace + "REGION"
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvDNSZone = "DNS_ZONE"
|
|
|
|
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +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).
|
2018-02-18 15:27:58 +00:00
|
|
|
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-06-21 17:06:16 +00:00
|
|
|
func (c customRetryer) RetryRules(r *request.Request) time.Duration {
|
2018-02-18 15:27:58 +00:00
|
|
|
retryCount := r.RetryCount
|
|
|
|
if retryCount > 7 {
|
|
|
|
retryCount = 7
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := (1 << uint(retryCount)) * (rand.Intn(50) + 200)
|
|
|
|
return time.Duration(delay) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Config struct {
|
|
|
|
DNSZone string
|
|
|
|
Region string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
DNSZone: env.GetOrFile(EnvDNSZone),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
|
|
|
Region: env.GetOrDefaultString(EnvRegion, "us-east-1"),
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-09-15 17:07:24 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
client *lightsail.Lightsail
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for the AWS Lightsail service.
|
2018-02-18 15:27:58 +00:00
|
|
|
//
|
|
|
|
// AWS Credentials are automatically detected in the following locations
|
|
|
|
// and prioritized in the following order:
|
2022-08-22 15:05:31 +00:00
|
|
|
// 1. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY,
|
2018-09-15 17:07:24 +00:00
|
|
|
// [AWS_SESSION_TOKEN], [DNS_ZONE], [LIGHTSAIL_REGION]
|
2022-08-22 15:05:31 +00:00
|
|
|
// 2. Shared credentials file (defaults to ~/.aws/credentials)
|
|
|
|
// 3. Amazon EC2 IAM role
|
2018-02-18 15:27:58 +00:00
|
|
|
//
|
|
|
|
// public hosted zone via the FQDN.
|
|
|
|
//
|
|
|
|
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return NewDNSProviderConfig(NewDefaultConfig())
|
|
|
|
}
|
2018-07-01 00:24:20 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for AWS Lightsail.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("lightsail: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
retryer := customRetryer{}
|
|
|
|
retryer.NumMaxRetries = maxRetries
|
|
|
|
|
|
|
|
conf := aws.NewConfig().WithRegion(config.Region)
|
|
|
|
sess, err := session.NewSession(request.WithRetryer(conf, retryer))
|
2018-07-01 00:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-18 15:27:58 +00:00
|
|
|
|
|
|
|
return &DNSProvider{
|
2018-09-15 17:07:24 +00:00
|
|
|
config: config,
|
|
|
|
client: lightsail.New(sess),
|
2018-02-18 15:27:58 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-07-01 00:24:20 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
err := d.newTxtRecord(fqdn, `"`+value+`"`)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("lightsail: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-02-18 15:27:58 +00:00
|
|
|
params := &lightsail.DeleteDomainEntryInput{
|
2018-09-15 17:07:24 +00:00
|
|
|
DomainName: aws.String(d.config.DNSZone),
|
2018-02-18 15:27:58 +00:00
|
|
|
DomainEntry: &lightsail.DomainEntry{
|
|
|
|
Name: aws.String(fqdn),
|
|
|
|
Type: aws.String("TXT"),
|
2018-09-15 17:07:24 +00:00
|
|
|
Target: aws.String(`"` + value + `"`),
|
2018-02-18 15:27:58 +00:00
|
|
|
},
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.DeleteDomainEntry(params)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("lightsail: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func (d *DNSProvider) newTxtRecord(fqdn, value string) error {
|
2018-02-18 15:27:58 +00:00
|
|
|
params := &lightsail.CreateDomainEntryInput{
|
2018-09-15 17:07:24 +00:00
|
|
|
DomainName: aws.String(d.config.DNSZone),
|
2018-02-18 15:27:58 +00:00
|
|
|
DomainEntry: &lightsail.DomainEntry{
|
|
|
|
Name: aws.String(fqdn),
|
|
|
|
Target: aws.String(value),
|
|
|
|
Type: aws.String("TXT"),
|
|
|
|
},
|
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.CreateDomainEntry(params)
|
2018-02-18 15:27:58 +00:00
|
|
|
return err
|
|
|
|
}
|