2019-03-11 16:56:48 +00:00
|
|
|
package auroradns
|
2016-10-12 00:42:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2016-10-12 00:42:20 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2016-10-12 00:42:20 +00:00
|
|
|
)
|
|
|
|
|
2022-07-04 13:27:27 +00:00
|
|
|
var envTest = tester.NewEnvTest(EnvAPIKey, EnvSecret)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
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-12 17:29:18 +00:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2022-07-04 13:27:27 +00:00
|
|
|
config.APIKey = "asdf1234"
|
|
|
|
config.Secret = "key"
|
2018-10-12 17:29:18 +00:00
|
|
|
config.BaseURL = server.URL
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
2021-11-01 23:52:38 +00:00
|
|
|
require.NoError(t, err)
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
return provider, mux
|
2018-10-12 17:29:18 +00:00
|
|
|
}
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2022-07-04 13:27:27 +00:00
|
|
|
EnvAPIKey: "123",
|
|
|
|
EnvSecret: "456",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2022-07-04 13:27:27 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvSecret: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
2022-07-04 13:27:27 +00:00
|
|
|
expected: "aurora: some credentials information are missing: AURORA_API_KEY,AURORA_SECRET",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2022-07-04 13:27:27 +00:00
|
|
|
desc: "missing api key",
|
2018-10-12 17:29:18 +00:00
|
|
|
envVars: map[string]string{
|
2022-07-04 13:27:27 +00:00
|
|
|
EnvAPIKey: "",
|
|
|
|
EnvSecret: "456",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
2022-07-04 13:27:27 +00:00
|
|
|
expected: "aurora: some credentials information are missing: AURORA_API_KEY",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
2022-07-04 13:27:27 +00:00
|
|
|
desc: "missing secret key",
|
2018-10-12 17:29:18 +00:00
|
|
|
envVars: map[string]string{
|
2022-07-04 13:27:27 +00:00
|
|
|
EnvAPIKey: "123",
|
|
|
|
EnvSecret: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
2022-07-04 13:27:27 +00:00
|
|
|
expected: "aurora: some credentials information are missing: AURORA_SECRET",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12 17:29:18 +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)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2022-07-04 13:27:27 +00:00
|
|
|
apiKey string
|
|
|
|
secret string
|
2018-10-12 17:29:18 +00:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
2022-07-04 13:27:27 +00:00
|
|
|
apiKey: "123",
|
|
|
|
secret: "456",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
2022-07-04 13:27:27 +00:00
|
|
|
apiKey: "",
|
|
|
|
secret: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
expected: "aurora: some credentials information are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing user id",
|
2022-07-04 13:27:27 +00:00
|
|
|
apiKey: "",
|
|
|
|
secret: "456",
|
2018-10-12 17:29:18 +00:00
|
|
|
expected: "aurora: some credentials information are missing",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing key",
|
2022-07-04 13:27:27 +00:00
|
|
|
apiKey: "123",
|
|
|
|
secret: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
expected: "aurora: some credentials information are missing",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
2022-07-04 13:27:27 +00:00
|
|
|
config.APIKey = test.apiKey
|
|
|
|
config.Secret = test.secret
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.client)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
provider, mux := setupTest(t)
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
mux.HandleFunc("/zones", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method, "method")
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `[{
|
2016-10-12 00:42:20 +00:00
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"name": "example.com"
|
|
|
|
}]`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2021-08-25 09:44:11 +00:00
|
|
|
reqBody, err := io.ReadAll(r.Body)
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, `{"type":"TXT","name":"_acme-challenge","content":"w6uP8Tcg6K2QR905Rms8iXTlksL6OD1KOWBxTK7wxPI","ttl":300}`, string(reqBody))
|
2016-10-12 00:42:20 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{
|
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"ttl": 300
|
|
|
|
}`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err := provider.Present("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to create TXT record")
|
2016-10-12 00:42:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
provider, mux := setupTest(t)
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodGet, r.Method)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `[{
|
2016-10-12 00:42:20 +00:00
|
|
|
"id": "c56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"name": "example.com"
|
|
|
|
}]`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodPost, r.Method)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{
|
2016-10-12 00:42:20 +00:00
|
|
|
"id": "ec56a4180-65aa-42ec-a945-5fd21dec0538",
|
|
|
|
"type": "TXT",
|
|
|
|
"name": "_acme-challenge",
|
|
|
|
"ttl": 300
|
|
|
|
}`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
mux.HandleFunc("/zones/c56a4180-65aa-42ec-a945-5fd21dec0538/records/ec56a4180-65aa-42ec-a945-5fd21dec0538", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
assert.Equal(t, http.MethodDelete, r.Method)
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-06-11 15:32:50 +00:00
|
|
|
assert.Equal(t, "application/json", r.Header.Get("Content-Type"), "Content-Type")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprintf(w, `{}`)
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
err := provider.Present("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to create TXT record")
|
2016-10-12 00:42:20 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
err = provider.CleanUp("example.com", "", "foobar")
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err, "fail to remove TXT record")
|
2016-10-12 00:42:20 +00:00
|
|
|
}
|