2018-12-06 23:50:17 +02:00
|
|
|
// Package lightsail implements a DNS provider for solving the DNS-01 challenge using AWS Lightsail DNS.
|
2019-03-11 18:56:48 +02:00
|
|
|
package lightsail
|
2018-02-18 17:27:58 +02:00
|
|
|
|
|
|
|
import (
|
2023-07-27 12:15:26 +02:00
|
|
|
"context"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-02-18 17:27:58 +02:00
|
|
|
"math/rand"
|
2022-09-17 11:22:42 +02:00
|
|
|
"strconv"
|
2018-02-18 17:27:58 +02:00
|
|
|
"time"
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws/retry"
|
|
|
|
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/lightsail"
|
|
|
|
awstypes "github.com/aws/aws-sdk-go-v2/service/lightsail/types"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2018-02-18 17:27:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxRetries = 5
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "LIGHTSAIL_"
|
|
|
|
|
2021-06-27 21:51:05 +02:00
|
|
|
EnvRegion = envNamespace + "REGION"
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvDNSZone = "DNS_ZONE"
|
|
|
|
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
|
|
|
DNSZone string
|
|
|
|
Region string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-09-15 19:07:24 +02:00
|
|
|
type DNSProvider struct {
|
2023-07-27 12:15:26 +02:00
|
|
|
client *lightsail.Client
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for the AWS Lightsail service.
|
2018-02-18 17:27:58 +02:00
|
|
|
//
|
|
|
|
// AWS Credentials are automatically detected in the following locations
|
|
|
|
// and prioritized in the following order:
|
2022-08-22 17:05:31 +02:00
|
|
|
// 1. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY,
|
2018-09-15 19:07:24 +02:00
|
|
|
// [AWS_SESSION_TOKEN], [DNS_ZONE], [LIGHTSAIL_REGION]
|
2022-08-22 17:05:31 +02:00
|
|
|
// 2. Shared credentials file (defaults to ~/.aws/credentials)
|
|
|
|
// 3. Amazon EC2 IAM role
|
2018-02-18 17:27:58 +02:00
|
|
|
//
|
|
|
|
// public hosted zone via the FQDN.
|
|
|
|
//
|
|
|
|
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2022-09-17 11:22:42 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
|
|
|
|
config.DNSZone = env.GetOrFile(EnvDNSZone)
|
|
|
|
config.Region = env.GetOrDefaultString(EnvRegion, "us-east-1")
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
2018-07-01 02:24:20 +02:00
|
|
|
|
2018-09-15 19:07:24 +02: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")
|
|
|
|
}
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
cfg, err := awsconfig.LoadDefaultConfig(ctx,
|
|
|
|
awsconfig.WithRegion(config.Region),
|
|
|
|
awsconfig.WithRetryer(func() aws.Retryer {
|
|
|
|
return retry.NewStandard(func(options *retry.StandardOptions) {
|
|
|
|
options.MaxAttempts = maxRetries
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
options.Backoff = retry.BackoffDelayerFunc(func(attempt int, err error) (time.Duration, error) {
|
|
|
|
retryCount := attempt
|
|
|
|
if retryCount > 7 {
|
|
|
|
retryCount = 7
|
|
|
|
}
|
|
|
|
|
|
|
|
delay := (1 << uint(retryCount)) * (rand.Intn(50) + 200)
|
|
|
|
return time.Duration(delay) * time.Millisecond, nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
)
|
2018-07-01 02:24:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-18 17:27:58 +02:00
|
|
|
|
|
|
|
return &DNSProvider{
|
2018-09-15 19:07:24 +02:00
|
|
|
config: config,
|
2023-07-27 12:15:26 +02:00
|
|
|
client: lightsail.NewFromConfig(cfg),
|
2018-02-18 17:27:58 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2023-07-27 12:15:26 +02:00
|
|
|
func (d *DNSProvider) Present(domain, _, keyAuth string) error {
|
|
|
|
ctx := context.Background()
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-07-01 02:24:20 +02:00
|
|
|
|
2022-09-17 11:22:42 +02:00
|
|
|
params := &lightsail.CreateDomainEntryInput{
|
|
|
|
DomainName: aws.String(d.config.DNSZone),
|
2023-07-27 12:15:26 +02:00
|
|
|
DomainEntry: &awstypes.DomainEntry{
|
2023-03-07 10:39:05 +02:00
|
|
|
Name: aws.String(info.EffectiveFQDN),
|
|
|
|
Target: aws.String(strconv.Quote(info.Value)),
|
2022-09-17 11:22:42 +02:00
|
|
|
Type: aws.String("TXT"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
_, err := d.client.CreateDomainEntry(ctx, params)
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("lightsail: %w", err)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
2022-09-17 11:22:42 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil
|
2018-02-18 17:27:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2023-07-27 12:15:26 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, _, keyAuth string) error {
|
|
|
|
ctx := context.Background()
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-02-18 17:27:58 +02:00
|
|
|
params := &lightsail.DeleteDomainEntryInput{
|
2018-09-15 19:07:24 +02:00
|
|
|
DomainName: aws.String(d.config.DNSZone),
|
2023-07-27 12:15:26 +02:00
|
|
|
DomainEntry: &awstypes.DomainEntry{
|
2023-03-07 10:39:05 +02:00
|
|
|
Name: aws.String(info.EffectiveFQDN),
|
2018-02-18 17:27:58 +02:00
|
|
|
Type: aws.String("TXT"),
|
2023-03-07 10:39:05 +02:00
|
|
|
Target: aws.String(strconv.Quote(info.Value)),
|
2018-02-18 17:27:58 +02:00
|
|
|
},
|
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
_, err := d.client.DeleteDomainEntry(ctx, params)
|
2018-09-15 19:07:24 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("lightsail: %w", err)
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
2022-09-17 11:22:42 +02:00
|
|
|
|
2018-09-15 19:07:24 +02: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 17:27:58 +02:00
|
|
|
}
|