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