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"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-03-26 03:34:31 +00:00
|
|
|
route53Secret string
|
|
|
|
route53Key string
|
|
|
|
route53Region string
|
2017-07-17 19:50:53 +00:00
|
|
|
route53Zone string
|
2015-12-03 20:01:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
route53Key = os.Getenv("AWS_ACCESS_KEY_ID")
|
|
|
|
route53Secret = os.Getenv("AWS_SECRET_ACCESS_KEY")
|
2016-03-26 03:34:31 +00:00
|
|
|
route53Region = os.Getenv("AWS_REGION")
|
2017-07-17 19:50:53 +00:00
|
|
|
route53Zone = os.Getenv("AWS_HOSTED_ZONE_ID")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
func restoreEnv() {
|
2015-12-03 20:01:46 +00:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", route53Key)
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", route53Secret)
|
2016-03-26 03:34:31 +00:00
|
|
|
os.Setenv("AWS_REGION", route53Region)
|
2017-07-17 19:50:53 +00:00
|
|
|
os.Setenv("AWS_HOSTED_ZONE_ID", route53Zone)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
func makeRoute53Provider(ts *httptest.Server) *DNSProvider {
|
|
|
|
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
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
client := route53.New(session.New(config))
|
2016-03-11 02:46:09 +00:00
|
|
|
return &DNSProvider{client: client}
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
func TestCredentialsFromEnv(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")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
|
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
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
sess := session.New(config)
|
|
|
|
_, err := sess.Config.Credentials.Get()
|
|
|
|
assert.NoError(t, err, "Expected credentials to be set from environment")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 03:34:31 +00:00
|
|
|
func TestRegionFromEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2016-03-26 03:34:31 +00:00
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
|
|
|
|
|
|
|
sess := session.New(aws.NewConfig())
|
2018-05-30 17:53:04 +00:00
|
|
|
assert.Equal(t, "us-east-1", aws.StringValue(sess.Config.Region), "Expected Region to be set from environment")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 19:50:53 +00:00
|
|
|
func TestHostedZoneIDFromEnv(t *testing.T) {
|
2018-06-11 15:32:50 +00:00
|
|
|
defer restoreEnv()
|
2017-07-17 19:50:53 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
const testZoneID = "testzoneid"
|
2017-07-17 19:50:53 +00:00
|
|
|
os.Setenv("AWS_HOSTED_ZONE_ID", testZoneID)
|
|
|
|
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
assert.NoError(t, err, "Expected no error constructing DNSProvider")
|
|
|
|
|
|
|
|
fqdn, err := provider.getHostedZoneID("whatever")
|
|
|
|
assert.NoError(t, err, "Expected FQDN to be resolved to environment variable value")
|
|
|
|
|
|
|
|
assert.Equal(t, testZoneID, fqdn)
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
func TestRoute53Present(t *testing.T) {
|
2016-03-26 03:34:31 +00:00
|
|
|
mockResponses := MockResponseMap{
|
|
|
|
"/2013-04-01/hostedzonesbyname": MockResponse{StatusCode: 200, Body: ListHostedZonesByNameResponse},
|
|
|
|
"/2013-04-01/hostedzone/ABCDEFG/rrset/": MockResponse{StatusCode: 200, Body: ChangeResourceRecordSetsResponse},
|
|
|
|
"/2013-04-01/change/123456": MockResponse{StatusCode: 200, Body: GetChangeResponse},
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := newMockServer(t, mockResponses)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
provider := makeRoute53Provider(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)
|
2016-03-26 03:34:31 +00:00
|
|
|
assert.NoError(t, err, "Expected Present to return no error")
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|