2018-02-18 15:27:58 +00:00
|
|
|
package lightsail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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/lightsail"
|
2018-07-01 00:24:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-16 15:52:57 +00:00
|
|
|
"github.com/xenolf/lego/platform/tester"
|
2018-02-18 15:27:58 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
"AWS_ACCESS_KEY_ID",
|
|
|
|
"AWS_SECRET_ACCESS_KEY",
|
|
|
|
"AWS_REGION",
|
|
|
|
"AWS_HOSTED_ZONE_ID").
|
|
|
|
WithDomain("DNS_ZONE").
|
|
|
|
WithLiveTestRequirements("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "DNS_ZONE")
|
2018-02-18 15:27:58 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func makeProvider(ts *httptest.Server) (*DNSProvider, error) {
|
2018-02-18 15:27:58 +00:00
|
|
|
config := &aws.Config{
|
|
|
|
Credentials: credentials.NewStaticCredentials("abc", "123", " "),
|
|
|
|
Endpoint: aws.String(ts.URL),
|
|
|
|
Region: aws.String("mock-region"),
|
|
|
|
MaxRetries: aws.Int(1),
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
conf := NewDefaultConfig()
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
client := lightsail.New(sess)
|
2018-09-15 17:07:24 +00:00
|
|
|
return &DNSProvider{client: client, config: conf}, nil
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCredentialsFromEnv(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
2018-02-18 15:27:58 +00:00
|
|
|
os.Setenv("AWS_ACCESS_KEY_ID", "123")
|
|
|
|
os.Setenv("AWS_SECRET_ACCESS_KEY", "123")
|
|
|
|
os.Setenv("AWS_REGION", "us-east-1")
|
|
|
|
|
|
|
|
config := &aws.Config{
|
|
|
|
CredentialsChainVerboseErrors: aws.Bool(true),
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = sess.Config.Credentials.Get()
|
|
|
|
require.NoError(t, err, "Expected credentials to be set from environment")
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2018-07-01 00:24:20 +00:00
|
|
|
mockResponses := map[string]MockResponse{
|
|
|
|
"/": {StatusCode: 200, Body: ""},
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts := newMockServer(t, mockResponses)
|
|
|
|
defer ts.Close()
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := makeProvider(ts)
|
2018-07-01 00:24:20 +00:00
|
|
|
require.NoError(t, err)
|
2018-02-18 15:27:58 +00:00
|
|
|
|
|
|
|
domain := "example.com"
|
|
|
|
keyAuth := "123456d=="
|
|
|
|
|
2018-07-01 00:24:20 +00:00
|
|
|
err = provider.Present(domain, "", keyAuth)
|
|
|
|
require.NoError(t, err, "Expected Present to return no error")
|
2018-02-18 15:27:58 +00:00
|
|
|
}
|