2015-12-03 20:01:46 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
2016-02-13 23:55:03 +00:00
|
|
|
"net/http"
|
2015-12-03 20:01:46 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2016-02-13 23:55:03 +00:00
|
|
|
"time"
|
2015-12-03 20:01:46 +00: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 00:38:40 +00:00
|
|
|
route53Secret string
|
|
|
|
route53Key string
|
|
|
|
awsCredentialFile string
|
|
|
|
homeDir string
|
|
|
|
testServer *testutil.HTTPServer
|
2015-12-03 20:01:46 +00: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 06:04:12 +00: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 20:01:46 +00:00
|
|
|
var serverResponseMap = testutil.ResponseMap{
|
|
|
|
"/2013-04-01/hostedzone/": testutil.Response{200, nil, ListHostedZonesAnswer},
|
|
|
|
"/2013-04-01/hostedzone/Z2K123214213123/rrset": testutil.Response{200, nil, ChangeResourceRecordSetsAnswer},
|
2016-02-03 06:04:12 +00:00
|
|
|
"/2013-04-01/change/asdf": testutil.Response{200, nil, GetChangeAnswer},
|
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-02-07 00:38:40 +00:00
|
|
|
awsCredentialFile = os.Getenv("AWS_CREDENTIAL_FILE")
|
|
|
|
homeDir = os.Getenv("HOME")
|
2015-12-03 20:01:46 +00: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 00:38:40 +00:00
|
|
|
os.Setenv("AWS_CREDENTIAL_FILE", awsCredentialFile)
|
|
|
|
os.Setenv("HOME", homeDir)
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeRoute53TestServer() *testutil.HTTPServer {
|
|
|
|
testServer.Flush()
|
|
|
|
return testServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeRoute53Provider(server *testutil.HTTPServer) *DNSProviderRoute53 {
|
|
|
|
auth := aws.Auth{"abc", "123", ""}
|
|
|
|
client := route53.NewWithClient(auth, aws.Region{Route53Endpoint: server.URL}, testutil.DefaultClient)
|
|
|
|
return &DNSProviderRoute53{client: client}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderRoute53Valid(t *testing.T) {
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
|
|
|
|
_, err := NewDNSProviderRoute53("123", "123", "us-east-1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
restoreRoute53Env()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderRoute53ValidEnv(t *testing.T) {
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "123")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
|
|
|
|
_, err := NewDNSProviderRoute53("", "", "us-east-1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
restoreRoute53Env()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderRoute53MissingAuthErr(t *testing.T) {
|
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
|
2016-02-07 00:38:40 +00: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-13 23:55:03 +00: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}
|
|
|
|
|
2015-12-03 20:01:46 +00:00
|
|
|
_, err := NewDNSProviderRoute53("", "", "us-east-1")
|
2016-02-07 00:38:40 +00:00
|
|
|
assert.EqualError(t, err, "No valid AWS authentication found")
|
2015-12-03 20:01:46 +00:00
|
|
|
restoreRoute53Env()
|
2016-02-13 23:55:03 +00:00
|
|
|
|
|
|
|
// restore default AWS HTTP client
|
|
|
|
aws.RetryingClient = awsClient
|
2015-12-03 20:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderRoute53InvalidRegionErr(t *testing.T) {
|
|
|
|
_, err := NewDNSProviderRoute53("123", "123", "us-east-3")
|
|
|
|
assert.EqualError(t, err, "Invalid AWS region name us-east-3")
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:06:25 +00:00
|
|
|
func TestRoute53Present(t *testing.T) {
|
2015-12-03 20:01:46 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
testServer := makeRoute53TestServer()
|
|
|
|
provider := makeRoute53Provider(testServer)
|
2016-02-03 06:04:12 +00:00
|
|
|
testServer.ResponseMap(3, serverResponseMap)
|
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)
|
|
|
|
assert.NoError(err, "Expected Present to return no error")
|
2015-12-03 20:01:46 +00:00
|
|
|
|
2016-02-03 06:04:12 +00:00
|
|
|
httpReqs := testServer.WaitRequests(3)
|
2015-12-03 20:01:46 +00:00
|
|
|
httpReq := httpReqs[1]
|
|
|
|
|
|
|
|
assert.Equal("/2013-04-01/hostedzone/Z2K123214213123/rrset", httpReq.URL.Path,
|
2016-01-15 04:06:25 +00:00
|
|
|
"Expected Present to select the correct hostedzone")
|
2015-12-03 20:01:46 +00:00
|
|
|
|
|
|
|
}
|