2019-03-11 16:56:48 +00:00
|
|
|
package stackpath
|
2018-10-09 19:58:32 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-10-09 19:58:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvClientID,
|
|
|
|
EnvClientSecret,
|
|
|
|
EnvStackID).
|
|
|
|
WithDomain(envDomain)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvClientID: "test@example.com",
|
|
|
|
EnvClientSecret: "123",
|
|
|
|
EnvStackID: "ID",
|
2018-10-09 19:58:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvClientID: "",
|
|
|
|
EnvClientSecret: "",
|
|
|
|
EnvStackID: "",
|
2018-10-09 19:58:32 +00:00
|
|
|
},
|
|
|
|
expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_ID,STACKPATH_CLIENT_SECRET,STACKPATH_STACK_ID",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client id",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvClientID: "",
|
|
|
|
EnvClientSecret: "123",
|
|
|
|
EnvStackID: "ID",
|
2018-10-09 19:58:32 +00:00
|
|
|
},
|
|
|
|
expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_ID",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing client secret",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvClientID: "test@example.com",
|
|
|
|
EnvClientSecret: "",
|
|
|
|
EnvStackID: "ID",
|
2018-10-09 19:58:32 +00:00
|
|
|
},
|
|
|
|
expected: "stackpath: some credentials information are missing: STACKPATH_CLIENT_SECRET",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing stack id",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvClientID: "test@example.com",
|
|
|
|
EnvClientSecret: "123",
|
|
|
|
EnvStackID: "",
|
2018-10-09 19:58:32 +00:00
|
|
|
},
|
|
|
|
expected: "stackpath: some credentials information are missing: STACKPATH_STACK_ID",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-09 19:58:32 +00:00
|
|
|
assert.NotNil(t, p)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
config *Config
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
"no_config": {
|
|
|
|
config: nil,
|
|
|
|
expectedErr: "stackpath: the configuration of the DNS provider is nil",
|
|
|
|
},
|
|
|
|
"no_client_id": {
|
|
|
|
config: &Config{
|
|
|
|
ClientSecret: "secret",
|
|
|
|
StackID: "stackID",
|
|
|
|
},
|
|
|
|
expectedErr: "stackpath: credentials missing",
|
|
|
|
},
|
|
|
|
"no_client_secret": {
|
|
|
|
config: &Config{
|
|
|
|
ClientID: "clientID",
|
|
|
|
StackID: "stackID",
|
|
|
|
},
|
|
|
|
expectedErr: "stackpath: credentials missing",
|
|
|
|
},
|
|
|
|
"no_stack_id": {
|
|
|
|
config: &Config{
|
|
|
|
ClientID: "clientID",
|
|
|
|
ClientSecret: "secret",
|
|
|
|
},
|
|
|
|
expectedErr: "stackpath: stack id missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for desc, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(test.config)
|
|
|
|
require.EqualError(t, err, test.expectedErr)
|
|
|
|
assert.Nil(t, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.ClientID = "CLIENT_ID"
|
|
|
|
config.ClientSecret = "CLIENT_SECRET"
|
|
|
|
config.StackID = "STACK_ID"
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2021-11-01 23:52:38 +00:00
|
|
|
require.NoError(t, err)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
|
|
|
provider.client = http.DefaultClient
|
|
|
|
provider.BaseURL, _ = url.Parse(server.URL + "/")
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
return provider, mux
|
2018-10-09 19:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_getZoneRecords(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
provider, mux := setupTest(t)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
2019-02-06 17:15:53 +00:00
|
|
|
mux.HandleFunc("/STACK_ID/zones/A/records", func(w http.ResponseWriter, _ *http.Request) {
|
2018-10-09 19:58:32 +00:00
|
|
|
content := `
|
|
|
|
{
|
|
|
|
"records": [
|
|
|
|
{"id":"1","name":"foo1","type":"TXT","ttl":120,"data":"txtTXTtxt"},
|
|
|
|
{"id":"2","name":"foo2","type":"TXT","ttl":121,"data":"TXTtxtTXT"}
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
_, err := w.Write([]byte(content))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
records, err := provider.getZoneRecords("foo1", &Zone{ID: "A", Domain: "test"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []Record{
|
|
|
|
{ID: "1", Name: "foo1", Type: "TXT", TTL: 120, Data: "txtTXTtxt"},
|
|
|
|
{ID: "2", Name: "foo2", Type: "TXT", TTL: 121, Data: "TXTtxtTXT"},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, records)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_getZoneRecords_apiError(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
provider, mux := setupTest(t)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
2019-02-06 17:15:53 +00:00
|
|
|
mux.HandleFunc("/STACK_ID/zones/A/records", func(w http.ResponseWriter, _ *http.Request) {
|
2018-10-09 19:58:32 +00:00
|
|
|
content := `
|
|
|
|
{
|
|
|
|
"code": 401,
|
|
|
|
"error": "an unauthorized request is attempted."
|
|
|
|
}`
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
_, err := w.Write([]byte(content))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := provider.getZoneRecords("foo1", &Zone{ID: "A", Domain: "test"})
|
|
|
|
|
|
|
|
expected := &ErrorResponse{Code: 401, Message: "an unauthorized request is attempted."}
|
|
|
|
assert.Equal(t, expected, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_getZones(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
provider, mux := setupTest(t)
|
2018-10-09 19:58:32 +00:00
|
|
|
|
2019-02-06 17:15:53 +00:00
|
|
|
mux.HandleFunc("/STACK_ID/zones", func(w http.ResponseWriter, _ *http.Request) {
|
2018-10-09 19:58:32 +00:00
|
|
|
content := `
|
|
|
|
{
|
|
|
|
"pageInfo": {
|
|
|
|
"totalCount": "5",
|
|
|
|
"hasPreviousPage": false,
|
|
|
|
"hasNextPage": false,
|
|
|
|
"startCursor": "1",
|
|
|
|
"endCursor": "1"
|
|
|
|
},
|
|
|
|
"zones": [
|
|
|
|
{
|
|
|
|
"stackId": "my_stack",
|
|
|
|
"accountId": "my_account",
|
|
|
|
"id": "A",
|
|
|
|
"domain": "foo.com",
|
|
|
|
"version": "1",
|
|
|
|
"labels": {
|
|
|
|
"property1": "val1",
|
|
|
|
"property2": "val2"
|
|
|
|
},
|
|
|
|
"created": "2018-10-07T02:31:49Z",
|
|
|
|
"updated": "2018-10-07T02:31:49Z",
|
|
|
|
"nameservers": [
|
|
|
|
"1.1.1.1"
|
|
|
|
],
|
|
|
|
"verified": "2018-10-07T02:31:49Z",
|
|
|
|
"status": "ACTIVE",
|
|
|
|
"disabled": false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
_, err := w.Write([]byte(content))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
zone, err := provider.getZones("sub.foo.com")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := &Zone{ID: "A", Domain: "foo.com"}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, zone)
|
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 15:52:57 +00:00
|
|
|
if !envTest.IsLiveTest() {
|
2018-10-12 17:29:18 +00:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 17:29:18 +00:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|