2019-03-11 16:56:48 +00:00
|
|
|
package lightsail
|
2018-02-18 15:27:58 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-27 10:15:26 +00:00
|
|
|
"context"
|
2018-02-18 15:27:58 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2023-07-27 10:15:26 +00: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/lightsail"
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2023-07-27 10:15:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-07-01 00:24:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-18 15:27:58 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const (
|
|
|
|
envAwsNamespace = "AWS_"
|
|
|
|
|
|
|
|
envAwsAccessKeyID = envAwsNamespace + "ACCESS_KEY_ID"
|
|
|
|
envAwsSecretAccessKey = envAwsNamespace + "SECRET_ACCESS_KEY"
|
|
|
|
envAwsRegion = envAwsNamespace + "REGION"
|
|
|
|
envAwsHostedZoneID = envAwsNamespace + "HOSTED_ZONE_ID"
|
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
envAwsAccessKeyID,
|
|
|
|
envAwsSecretAccessKey,
|
|
|
|
envAwsRegion,
|
|
|
|
envAwsHostedZoneID).
|
|
|
|
WithDomain(EnvDNSZone).
|
|
|
|
WithLiveTestRequirements(envAwsAccessKeyID, envAwsSecretAccessKey, EnvDNSZone)
|
2018-02-18 15:27:58 +00:00
|
|
|
|
2023-07-27 10:15:26 +00:00
|
|
|
func makeProvider(serverURL string) *DNSProvider {
|
|
|
|
config := aws.Config{
|
2024-06-12 00:55:35 +00:00
|
|
|
Credentials: credentials.NewStaticCredentialsProvider("abc", "123", " "),
|
|
|
|
Region: "mock-region",
|
|
|
|
BaseEndpoint: aws.String(serverURL),
|
|
|
|
RetryMaxAttempts: 1,
|
2023-07-27 10:15:26 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-07-27 10:15:26 +00:00
|
|
|
return &DNSProvider{
|
|
|
|
client: lightsail.NewFromConfig(config),
|
|
|
|
config: NewDefaultConfig(),
|
|
|
|
}
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCredentialsFromEnv(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
2020-12-28 22:39:00 +00:00
|
|
|
_ = os.Setenv(envAwsAccessKeyID, "123")
|
|
|
|
_ = os.Setenv(envAwsSecretAccessKey, "123")
|
|
|
|
_ = os.Setenv(envAwsRegion, "us-east-1")
|
2018-02-18 15:27:58 +00:00
|
|
|
|
2023-07-27 10:15:26 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
cfg, err := awsconfig.LoadDefaultConfig(ctx)
|
2018-07-01 00:24:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-07-27 10:15:26 +00:00
|
|
|
cs, err := cfg.Credentials.Retrieve(ctx)
|
2018-07-01 00:24:20 +00:00
|
|
|
require.NoError(t, err, "Expected credentials to be set from environment")
|
2023-07-27 10:15:26 +00:00
|
|
|
|
|
|
|
expected := aws.Credentials{
|
|
|
|
AccessKeyID: "123",
|
|
|
|
SecretAccessKey: "123",
|
|
|
|
Source: "EnvConfigCredentials",
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, cs)
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2018-07-01 00:24:20 +00:00
|
|
|
mockResponses := map[string]MockResponse{
|
|
|
|
"/": {StatusCode: 200, Body: ""},
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 22:39:00 +00:00
|
|
|
serverURL := newMockServer(t, mockResponses)
|
2018-02-18 15:27:58 +00:00
|
|
|
|
2023-07-27 10:15:26 +00:00
|
|
|
provider := makeProvider(serverURL)
|
2018-02-18 15:27:58 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "123456d=="
|
|
|
|
|
2023-07-27 10:15:26 +00:00
|
|
|
err := provider.Present(domain, "", keyAuth)
|
2018-07-01 00:24:20 +00:00
|
|
|
require.NoError(t, err, "Expected Present to return no error")
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|