2016-02-29 02:48:41 +00:00
|
|
|
package route53
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
import (
|
2016-03-26 03:34:31 +00:00
|
|
|
"net/http/httptest"
|
2015-12-03 20:01:46 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-08 11:49:24 +00:00
|
|
|
"time"
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/route53"
|
2015-12-03 20:01:46 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-09 17:03:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-12 17:29:18 +00:00
|
|
|
envTestAwsSecretAccessKey string
|
|
|
|
envTestAwsAccessKeyID string
|
|
|
|
envTestAwsRegion string
|
|
|
|
envTestAwsHostedZoneID string
|
|
|
|
|
|
|
|
envTestAwsMaxRetries string
|
|
|
|
envTestAwsTTL string
|
|
|
|
envTestAwsPropagationTimeout string
|
|
|
|
envTestAwsPollingInterval string
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-10-12 17:29:18 +00:00
|
|
|
envTestAwsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
|
|
|
|
envTestAwsSecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
|
|
|
|
envTestAwsRegion = os.Getenv("AWS_REGION")
|
|
|
|
envTestAwsHostedZoneID = os.Getenv("AWS_HOSTED_ZONE_ID")
|
|
|
|
|
|
|
|
envTestAwsMaxRetries = os.Getenv("AWS_MAX_RETRIES")
|
|
|
|
envTestAwsTTL = os.Getenv("AWS_TTL")
|
|
|
|
envTestAwsPropagationTimeout = os.Getenv("AWS_PROPAGATION_TIMEOUT")
|
|
|
|
envTestAwsPollingInterval = os.Getenv("AWS_POLLING_INTERVAL")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2018-10-12 17:29:18 +00:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", envTestAwsAccessKeyID)
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", envTestAwsSecretAccessKey)
|
|
|
|
os.Setenv("AWS_REGION", envTestAwsRegion)
|
|
|
|
os.Setenv("AWS_HOSTED_ZONE_ID", envTestAwsHostedZoneID)
|
|
|
|
|
|
|
|
os.Setenv("AWS_MAX_RETRIES", envTestAwsMaxRetries)
|
|
|
|
os.Setenv("AWS_TTL", envTestAwsTTL)
|
|
|
|
os.Setenv("AWS_PROPAGATION_TIMEOUT", envTestAwsPropagationTimeout)
|
|
|
|
os.Setenv("AWS_POLLING_INTERVAL", envTestAwsPollingInterval)
|
2018-10-09 17:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanEnv() {
|
|
|
|
os.Unsetenv("AWS_ACCESS_KEY_ID")
|
|
|
|
os.Unsetenv("AWS_SECRET_ACCESS_KEY")
|
|
|
|
os.Unsetenv("AWS_REGION")
|
|
|
|
os.Unsetenv("AWS_HOSTED_ZONE_ID")
|
2018-09-08 11:49:24 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
os.Unsetenv("AWS_MAX_RETRIES")
|
|
|
|
os.Unsetenv("AWS_TTL")
|
|
|
|
os.Unsetenv("AWS_PROPAGATION_TIMEOUT")
|
|
|
|
os.Unsetenv("AWS_POLLING_INTERVAL")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func makeTestProvider(ts *httptest.Server) *DNSProvider {
|
2016-03-26 03:34:31 +00:00
|
|
|
config := &aws.Config{
|
|
|
|
Credentials: credentials.NewStaticCredentials("abc", "123", " "),
|
|
|
|
Endpoint: aws.String(ts.URL),
|
|
|
|
Region: aws.String("mock-region"),
|
|
|
|
MaxRetries: aws.Int(1),
|
|
|
|
}
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
client := route53.New(sess)
|
2018-07-18 15:37:35 +00:00
|
|
|
cfg := NewDefaultConfig()
|
|
|
|
return &DNSProvider{client: client, config: cfg}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
func Test_loadCredentials_FromEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2015-12-03 20:01:46 +00:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "123")
|
2018-10-09 17:03:07 +00:00
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "456")
|
2016-03-17 20:59:15 +00:00
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
config := &aws.Config{
|
|
|
|
CredentialsChainVerboseErrors: aws.Bool(true),
|
|
|
|
}
|
2016-02-13 23:55:03 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value, err := sess.Config.Credentials.Get()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err, "Expected credentials to be set from environment")
|
2018-10-09 17:03:07 +00:00
|
|
|
|
|
|
|
expected := credentials.Value{
|
|
|
|
AccessKeyID: "123",
|
|
|
|
SecretAccessKey: "456",
|
|
|
|
SessionToken: "",
|
|
|
|
ProviderName: "EnvConfigCredentials",
|
|
|
|
}
|
|
|
|
assert.Equal(t, expected, value)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
func Test_loadRegion_FromEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2018-10-09 17:03:07 +00:00
|
|
|
os.Setenv("AWS_REGION", route53.CloudWatchRegionUsEast1)
|
2016-03-26 03:34:31 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
sess, err := session.NewSession(aws.NewConfig())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
region := aws.StringValue(sess.Config.Region)
|
|
|
|
assert.Equal(t, route53.CloudWatchRegionUsEast1, region, "Region")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
func Test_getHostedZoneID_FromEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2017-07-17 19:50:53 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
expectedZoneID := "zoneID"
|
|
|
|
|
|
|
|
os.Setenv("AWS_HOSTED_ZONE_ID", expectedZoneID)
|
2017-07-17 19:50:53 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2017-07-17 19:50:53 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
hostedZoneID, err := provider.getHostedZoneID("whatever")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err, "HostedZoneID")
|
2017-07-17 19:50:53 +00:00
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
assert.Equal(t, expectedZoneID, hostedZoneID)
|
2017-07-17 19:50:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
func TestNewDefaultConfig(t *testing.T) {
|
2018-09-08 11:49:24 +00:00
|
|
|
defer restoreEnv()
|
|
|
|
|
2018-10-09 17:03:07 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected *Config
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "default configuration",
|
|
|
|
expected: &Config{
|
|
|
|
MaxRetries: 5,
|
|
|
|
TTL: 10,
|
|
|
|
PropagationTimeout: 2 * time.Minute,
|
|
|
|
PollingInterval: 4 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"AWS_MAX_RETRIES": "10",
|
|
|
|
"AWS_TTL": "99",
|
|
|
|
"AWS_PROPAGATION_TIMEOUT": "60",
|
|
|
|
"AWS_POLLING_INTERVAL": "60",
|
|
|
|
"AWS_HOSTED_ZONE_ID": "abc123",
|
|
|
|
},
|
|
|
|
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) {
|
|
|
|
cleanEnv()
|
|
|
|
for key, value := range test.envVars {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, config)
|
|
|
|
})
|
|
|
|
}
|
2018-09-08 11:49:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2016-03-26 03:34:31 +00:00
|
|
|
mockResponses := MockResponseMap{
|
2018-10-09 17:03:07 +00: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},
|
|
|
|
"/2013-04-01/hostedzone/ABCDEFG/rrset?name=_acme-challenge.example.com.&type=TXT": {
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: "",
|
|
|
|
},
|
2016-03-26 03:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := newMockServer(t, mockResponses)
|
|
|
|
defer ts.Close()
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
provider := makeTestProvider(ts)
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "123456d=="
|
|
|
|
|
|
|
|
err := provider.Present(domain, "", keyAuth)
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err, "Expected Present to return no error")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|