2019-03-11 18:56:48 +02:00
|
|
|
package route53
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
import (
|
2023-07-27 12:15:26 +02:00
|
|
|
"context"
|
2015-12-03 22:01:46 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-08 13:49:24 +02:00
|
|
|
"time"
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/route53"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2015-12-03 22:01:46 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-09 19:03:07 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-12-03 22:01:46 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = "R53_DOMAIN"
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAccessKeyID,
|
|
|
|
EnvSecretAccessKey,
|
|
|
|
EnvRegion,
|
|
|
|
EnvHostedZoneID,
|
|
|
|
EnvMaxRetries,
|
|
|
|
EnvTTL,
|
|
|
|
EnvPropagationTimeout,
|
2024-05-09 21:05:21 +02:00
|
|
|
EnvPollingInterval,
|
|
|
|
EnvWaitForRecordSetsChanged).
|
2020-03-12 00:51:10 +02:00
|
|
|
WithDomain(envDomain).
|
|
|
|
WithLiveTestRequirements(EnvAccessKeyID, EnvSecretAccessKey, EnvRegion, envDomain)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
func makeTestProvider(t *testing.T, serverURL string) *DNSProvider {
|
|
|
|
t.Helper()
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
cfg := aws.Config{
|
2024-06-12 02:55:35 +02:00
|
|
|
Credentials: credentials.NewStaticCredentialsProvider("abc", "123", " "),
|
|
|
|
Region: "mock-region",
|
|
|
|
BaseEndpoint: aws.String(serverURL),
|
|
|
|
RetryMaxAttempts: 1,
|
2016-03-26 05:34:31 +02:00
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2020-12-29 00:39:00 +02:00
|
|
|
return &DNSProvider{
|
2023-07-27 12:15:26 +02:00
|
|
|
client: route53.NewFromConfig(cfg),
|
2020-12-29 00:39:00 +02:00
|
|
|
config: NewDefaultConfig(),
|
|
|
|
}
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
func Test_loadCredentials_FromEnv(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
2020-12-29 00:39:00 +02:00
|
|
|
_ = os.Setenv(EnvAccessKeyID, "123")
|
|
|
|
_ = os.Setenv(EnvSecretAccessKey, "456")
|
|
|
|
_ = os.Setenv(EnvRegion, "us-east-1")
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
ctx := context.Background()
|
2016-02-14 01:55:03 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
cfg, err := awsconfig.LoadDefaultConfig(ctx)
|
2018-10-09 19:03:07 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
value, err := cfg.Credentials.Retrieve(ctx)
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err, "Expected credentials to be set from environment")
|
2018-10-09 19:03:07 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
expected := aws.Credentials{
|
2018-10-09 19:03:07 +02:00
|
|
|
AccessKeyID: "123",
|
|
|
|
SecretAccessKey: "456",
|
|
|
|
SessionToken: "",
|
2023-07-27 12:15:26 +02:00
|
|
|
Source: "EnvConfigCredentials",
|
2018-10-09 19:03:07 +02:00
|
|
|
}
|
2023-07-27 12:15:26 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
assert.Equal(t, expected, value)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
func Test_loadRegion_FromEnv(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
_ = os.Setenv(EnvRegion, "foo")
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
cfg, err := awsconfig.LoadDefaultConfig(context.Background())
|
2018-10-09 19:03:07 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
assert.Equal(t, "foo", cfg.Region, "Region")
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
func Test_getHostedZoneID_FromEnv(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
2017-07-17 21:50:53 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
expectedZoneID := "zoneID"
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
_ = os.Setenv(EnvHostedZoneID, expectedZoneID)
|
2017-07-17 21:50:53 +02:00
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2017-07-17 21:50:53 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
hostedZoneID, err := provider.getHostedZoneID(context.Background(), "whatever")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err, "HostedZoneID")
|
2017-07-17 21:50:53 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
assert.Equal(t, expectedZoneID, hostedZoneID)
|
2017-07-17 21:50:53 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
func TestNewDefaultConfig(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
2018-09-08 13:49:24 +02:00
|
|
|
|
2018-10-09 19:03:07 +02:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected *Config
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "default configuration",
|
|
|
|
expected: &Config{
|
2024-05-09 21:05:21 +02:00
|
|
|
MaxRetries: 5,
|
|
|
|
TTL: 10,
|
|
|
|
PropagationTimeout: 2 * time.Minute,
|
|
|
|
PollingInterval: 4 * time.Second,
|
|
|
|
WaitForRecordSetsChanged: true,
|
2018-10-09 19:03:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-05-09 21:05:21 +02:00
|
|
|
desc: "set values",
|
2018-10-09 19:03:07 +02:00
|
|
|
envVars: map[string]string{
|
2024-05-09 21:05:21 +02:00
|
|
|
EnvMaxRetries: "10",
|
|
|
|
EnvTTL: "99",
|
|
|
|
EnvPropagationTimeout: "60",
|
|
|
|
EnvPollingInterval: "60",
|
|
|
|
EnvHostedZoneID: "abc123",
|
|
|
|
EnvWaitForRecordSetsChanged: "false",
|
2018-10-09 19:03:07 +02:00
|
|
|
},
|
|
|
|
expected: &Config{
|
|
|
|
MaxRetries: 10,
|
|
|
|
TTL: 99,
|
|
|
|
PropagationTimeout: 60 * time.Second,
|
|
|
|
PollingInterval: 60 * time.Second,
|
|
|
|
HostedZoneID: "abc123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.ClearEnv()
|
2018-10-09 19:03:07 +02:00
|
|
|
for key, value := range test.envVars {
|
2023-07-27 12:15:26 +02:00
|
|
|
_ = os.Setenv(key, value)
|
2018-10-09 19:03:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, config)
|
|
|
|
})
|
|
|
|
}
|
2018-09-08 13:49:24 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2016-03-26 05:34:31 +02:00
|
|
|
mockResponses := MockResponseMap{
|
2023-07-27 12:15:26 +02:00
|
|
|
"/2013-04-01/hostedzonesbyname": {StatusCode: 200, Body: ListHostedZonesByNameResponse},
|
|
|
|
"/2013-04-01/hostedzone/ABCDEFG/rrset": {StatusCode: 200, Body: ChangeResourceRecordSetsResponse},
|
|
|
|
"/2013-04-01/change/123456": {StatusCode: 200, Body: GetChangeResponse},
|
2018-10-09 19:03:07 +02:00
|
|
|
"/2013-04-01/hostedzone/ABCDEFG/rrset?name=_acme-challenge.example.com.&type=TXT": {
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: "",
|
|
|
|
},
|
2016-03-26 05:34:31 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 01:52:38 +02:00
|
|
|
serverURL := setupTest(t, mockResponses)
|
2016-03-26 05:34:31 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
2021-11-02 01:52:38 +02:00
|
|
|
provider := makeTestProvider(t, serverURL)
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "123456d=="
|
|
|
|
|
|
|
|
err := provider.Present(domain, "", keyAuth)
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err, "Expected Present to return no error")
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2023-01-16 00:50:35 +02:00
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
func Test_createAWSConfig(t *testing.T) {
|
2023-01-16 00:50:35 +02:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
env map[string]string
|
|
|
|
config *Config
|
2023-07-27 12:15:26 +02:00
|
|
|
wantCreds aws.Credentials
|
2023-01-16 00:50:35 +02:00
|
|
|
wantDefaultChain bool
|
|
|
|
wantRegion string
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "config is nil",
|
|
|
|
wantErr: "config is nil",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "session token without access key id or secret access key",
|
|
|
|
config: &Config{SessionToken: "foo"},
|
|
|
|
wantErr: "SessionToken must be supplied with AccessKeyID and SecretAccessKey",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "access key id without secret access key",
|
|
|
|
config: &Config{AccessKeyID: "foo"},
|
|
|
|
wantErr: "AccessKeyID and SecretAccessKey must be supplied together",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "access key id without secret access key",
|
|
|
|
config: &Config{SecretAccessKey: "foo"},
|
|
|
|
wantErr: "AccessKeyID and SecretAccessKey must be supplied together",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "credentials from default chain",
|
|
|
|
config: &Config{},
|
|
|
|
wantDefaultChain: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "static credentials",
|
|
|
|
config: &Config{
|
|
|
|
AccessKeyID: "one",
|
|
|
|
SecretAccessKey: "two",
|
|
|
|
},
|
2023-07-27 12:15:26 +02:00
|
|
|
wantCreds: aws.Credentials{
|
2023-01-16 00:50:35 +02:00
|
|
|
AccessKeyID: "one",
|
|
|
|
SecretAccessKey: "two",
|
|
|
|
SessionToken: "",
|
2023-07-27 12:15:26 +02:00
|
|
|
Source: credentials.StaticCredentialsName,
|
2023-01-16 00:50:35 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "static credentials with session token",
|
|
|
|
config: &Config{
|
|
|
|
AccessKeyID: "one",
|
|
|
|
SecretAccessKey: "two",
|
|
|
|
SessionToken: "three",
|
|
|
|
},
|
2023-07-27 12:15:26 +02:00
|
|
|
wantCreds: aws.Credentials{
|
2023-01-16 00:50:35 +02:00
|
|
|
AccessKeyID: "one",
|
|
|
|
SecretAccessKey: "two",
|
|
|
|
SessionToken: "three",
|
2023-07-27 12:15:26 +02:00
|
|
|
Source: credentials.StaticCredentialsName,
|
2023-01-16 00:50:35 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "region from env",
|
|
|
|
config: &Config{},
|
|
|
|
env: map[string]string{
|
|
|
|
"AWS_REGION": "foo",
|
|
|
|
},
|
|
|
|
wantDefaultChain: true,
|
|
|
|
wantRegion: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "static region",
|
|
|
|
config: &Config{
|
|
|
|
Region: "one",
|
|
|
|
},
|
|
|
|
env: map[string]string{
|
|
|
|
"AWS_REGION": "foo",
|
|
|
|
},
|
|
|
|
wantDefaultChain: true,
|
|
|
|
wantRegion: "one",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.env)
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
cfg, err := createAWSConfig(ctx, test.config)
|
2023-01-16 00:50:35 +02:00
|
|
|
requireErr(t, err, test.wantErr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-27 12:15:26 +02:00
|
|
|
gotCreds, err := cfg.Credentials.Retrieve(ctx)
|
2023-01-16 00:50:35 +02:00
|
|
|
|
|
|
|
if test.wantDefaultChain {
|
2023-07-27 12:15:26 +02:00
|
|
|
assert.NotEqual(t, credentials.StaticCredentialsName, gotCreds.Source)
|
2023-01-16 00:50:35 +02:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.wantCreds, gotCreds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.wantRegion != "" {
|
2023-07-27 12:15:26 +02:00
|
|
|
assert.Equal(t, test.wantRegion, cfg.Region)
|
2023-01-16 00:50:35 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireErr(t *testing.T, err error, wantErr string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err != nil && wantErr == "":
|
|
|
|
// force the assertion error.
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
case err == nil && wantErr != "":
|
|
|
|
// force the assertion error.
|
|
|
|
require.EqualError(t, err, wantErr)
|
|
|
|
|
|
|
|
case err != nil && wantErr != "":
|
|
|
|
require.EqualError(t, err, wantErr)
|
|
|
|
}
|
|
|
|
}
|