2016-02-29 04:48:41 +02:00
|
|
|
package route53
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
import (
|
2016-02-14 01:55:03 +02:00
|
|
|
"net/http"
|
2015-12-03 22:01:46 +02:00
|
|
|
"os"
|
|
|
|
"testing"
|
2016-02-14 01:55:03 +02:00
|
|
|
"time"
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
"github.com/mitchellh/goamz/aws"
|
|
|
|
"github.com/mitchellh/goamz/route53"
|
|
|
|
"github.com/mitchellh/goamz/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-02-07 02:38:40 +02:00
|
|
|
route53Secret string
|
|
|
|
route53Key string
|
|
|
|
awsCredentialFile string
|
|
|
|
homeDir string
|
|
|
|
testServer *testutil.HTTPServer
|
2015-12-03 22:01:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var ChangeResourceRecordSetsAnswer = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<ChangeResourceRecordSetsResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
|
|
|
<ChangeInfo>
|
|
|
|
<Id>/change/asdf</Id>
|
|
|
|
<Status>PENDING</Status>
|
|
|
|
<SubmittedAt>2014</SubmittedAt>
|
|
|
|
</ChangeInfo>
|
|
|
|
</ChangeResourceRecordSetsResponse>`
|
|
|
|
|
|
|
|
var ListHostedZonesAnswer = `<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<ListHostedZonesResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
|
|
|
<HostedZones>
|
|
|
|
<HostedZone>
|
|
|
|
<Id>/hostedzone/Z2K123214213123</Id>
|
|
|
|
<Name>example.com.</Name>
|
|
|
|
<CallerReference>D2224C5B-684A-DB4A-BB9A-E09E3BAFEA7A</CallerReference>
|
|
|
|
<Config>
|
|
|
|
<Comment>Test comment</Comment>
|
|
|
|
</Config>
|
|
|
|
<ResourceRecordSetCount>10</ResourceRecordSetCount>
|
|
|
|
</HostedZone>
|
|
|
|
<HostedZone>
|
|
|
|
<Id>/hostedzone/ZLT12321321124</Id>
|
|
|
|
<Name>sub.example.com.</Name>
|
|
|
|
<CallerReference>A970F076-FCB1-D959-B395-96474CC84EB8</CallerReference>
|
|
|
|
<Config>
|
|
|
|
<Comment>Test comment for subdomain host</Comment>
|
|
|
|
</Config>
|
|
|
|
<ResourceRecordSetCount>4</ResourceRecordSetCount>
|
|
|
|
</HostedZone>
|
|
|
|
</HostedZones>
|
|
|
|
<IsTruncated>false</IsTruncated>
|
|
|
|
<MaxItems>100</MaxItems>
|
|
|
|
</ListHostedZonesResponse>`
|
|
|
|
|
2016-02-03 08:04:12 +02:00
|
|
|
var GetChangeAnswer = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<GetChangeResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
|
|
|
|
<ChangeInfo>
|
|
|
|
<Id>/change/asdf</Id>
|
|
|
|
<Status>INSYNC</Status>
|
|
|
|
<SubmittedAt>2016-02-03T01:36:41.958Z</SubmittedAt>
|
|
|
|
</ChangeInfo>
|
|
|
|
</GetChangeResponse>`
|
|
|
|
|
2015-12-03 22:01:46 +02:00
|
|
|
var serverResponseMap = testutil.ResponseMap{
|
2016-02-14 08:24:19 +02:00
|
|
|
"/2013-04-01/hostedzone/": testutil.Response{Status: 200, Headers: nil, Body: ListHostedZonesAnswer},
|
|
|
|
"/2013-04-01/hostedzone/Z2K123214213123/rrset": testutil.Response{Status: 200, Headers: nil, Body: ChangeResourceRecordSetsAnswer},
|
|
|
|
"/2013-04-01/change/asdf": testutil.Response{Status: 200, Headers: nil, Body: GetChangeAnswer},
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
route53Key = os.Getenv("AWS_ACCESS_KEY_ID")
|
|
|
|
route53Secret = os.Getenv("AWS_SECRET_ACCESS_KEY")
|
2016-02-07 02:38:40 +02:00
|
|
|
awsCredentialFile = os.Getenv("AWS_CREDENTIAL_FILE")
|
|
|
|
homeDir = os.Getenv("HOME")
|
2015-12-03 22:01:46 +02:00
|
|
|
testServer = testutil.NewHTTPServer()
|
|
|
|
testServer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreRoute53Env() {
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", route53Key)
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", route53Secret)
|
2016-02-07 02:38:40 +02:00
|
|
|
os.Setenv("AWS_CREDENTIAL_FILE", awsCredentialFile)
|
|
|
|
os.Setenv("HOME", homeDir)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeRoute53TestServer() *testutil.HTTPServer {
|
|
|
|
testServer.Flush()
|
|
|
|
return testServer
|
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func makeRoute53Provider(server *testutil.HTTPServer) *DNSProvider {
|
2016-02-14 08:24:19 +02:00
|
|
|
auth := aws.Auth{AccessKey: "abc", SecretKey: "123", Token: ""}
|
2015-12-03 22:01:46 +02:00
|
|
|
client := route53.NewWithClient(auth, aws.Region{Route53Endpoint: server.URL}, testutil.DefaultClient)
|
2016-03-11 04:46:09 +02:00
|
|
|
return &DNSProvider{client: client}
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func TestNewDNSProviderValid(t *testing.T) {
|
2015-12-03 22:01:46 +02:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
|
2016-03-17 22:59:15 +02:00
|
|
|
os.Setenv("AWS_REGION", "")
|
|
|
|
_, err := NewDNSProviderCredentials("123", "123", "us-east-1")
|
2015-12-03 22:01:46 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
restoreRoute53Env()
|
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func TestNewDNSProviderValidEnv(t *testing.T) {
|
2015-12-03 22:01:46 +02:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "123")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
|
2016-03-17 22:59:15 +02:00
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
|
|
|
_, err := NewDNSProvider()
|
2015-12-03 22:01:46 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
restoreRoute53Env()
|
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func TestNewDNSProviderMissingAuthErr(t *testing.T) {
|
2015-12-03 22:01:46 +02:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
|
2016-02-07 02:38:40 +02:00
|
|
|
os.Setenv("AWS_CREDENTIAL_FILE", "") // in case test machine has this variable set
|
|
|
|
os.Setenv("HOME", "/") // in case test machine has ~/.aws/credentials
|
2016-02-14 01:55:03 +02:00
|
|
|
|
|
|
|
// The default AWS HTTP client retries three times with a deadline of 10 seconds.
|
|
|
|
// Replace the default HTTP client with one that does not retry and has a low timeout.
|
|
|
|
awsClient := aws.RetryingClient
|
|
|
|
aws.RetryingClient = &http.Client{Timeout: time.Millisecond}
|
|
|
|
|
2016-03-17 22:59:15 +02:00
|
|
|
_, err := NewDNSProviderCredentials("", "", "us-east-1")
|
2016-02-07 02:38:40 +02:00
|
|
|
assert.EqualError(t, err, "No valid AWS authentication found")
|
2015-12-03 22:01:46 +02:00
|
|
|
restoreRoute53Env()
|
2016-02-14 01:55:03 +02:00
|
|
|
|
|
|
|
// restore default AWS HTTP client
|
|
|
|
aws.RetryingClient = awsClient
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 04:46:09 +02:00
|
|
|
func TestNewDNSProviderInvalidRegionErr(t *testing.T) {
|
2016-03-17 22:59:15 +02:00
|
|
|
_, err := NewDNSProviderCredentials("123", "123", "us-east-3")
|
2015-12-03 22:01:46 +02:00
|
|
|
assert.EqualError(t, err, "Invalid AWS region name us-east-3")
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
func TestRoute53Present(t *testing.T) {
|
2015-12-03 22:01:46 +02:00
|
|
|
assert := assert.New(t)
|
|
|
|
testServer := makeRoute53TestServer()
|
|
|
|
provider := makeRoute53Provider(testServer)
|
2016-02-03 08:04:12 +02:00
|
|
|
testServer.ResponseMap(3, serverResponseMap)
|
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)
|
|
|
|
assert.NoError(err, "Expected Present to return no error")
|
2015-12-03 22:01:46 +02:00
|
|
|
|
2016-02-03 08:04:12 +02:00
|
|
|
httpReqs := testServer.WaitRequests(3)
|
2015-12-03 22:01:46 +02:00
|
|
|
httpReq := httpReqs[1]
|
|
|
|
|
|
|
|
assert.Equal("/2013-04-01/hostedzone/Z2K123214213123/rrset", httpReq.URL.Path,
|
2016-01-15 06:06:25 +02:00
|
|
|
"Expected Present to select the correct hostedzone")
|
2015-12-03 22:01:46 +02:00
|
|
|
|
|
|
|
}
|