2019-01-20 11:27:35 +00:00
|
|
|
package zoneee
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2023-05-05 07:49:38 +00:00
|
|
|
"path"
|
2019-01-20 11:27:35 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/zoneee/internal"
|
2019-01-20 11:27:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
|
|
|
var envTest = tester.NewEnvTest(EnvEndpoint, EnvAPIUser, EnvAPIKey).
|
|
|
|
WithLiveTestRequirements(EnvAPIUser, EnvAPIKey).
|
|
|
|
WithDomain(envDomain)
|
2019-01-20 11:27:35 +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
|
|
|
EnvAPIUser: "123",
|
|
|
|
EnvAPIKey: "456",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIUser: "",
|
|
|
|
EnvAPIKey: "",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
expected: "zoneee: some credentials information are missing: ZONEEE_API_USER,ZONEEE_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIUser: "",
|
|
|
|
EnvAPIKey: "456",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
expected: "zoneee: some credentials information are missing: ZONEEE_API_USER",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing API key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIUser: "123",
|
|
|
|
EnvAPIKey: "",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
expected: "zoneee: some credentials information are missing: ZONEEE_API_KEY",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid URL",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIUser: "123",
|
|
|
|
EnvAPIKey: "456",
|
|
|
|
EnvEndpoint: ":",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
2020-02-25 23:02:29 +00:00
|
|
|
expected: `zoneee: parse ":": missing protocol scheme`,
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2019-01-20 11:27:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiUser string
|
|
|
|
apiKey string
|
|
|
|
endpoint string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
apiUser: "456",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "zoneee: credentials missing: username",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
apiUser: "456",
|
|
|
|
expected: "zoneee: credentials missing: API key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
apiKey: "123",
|
|
|
|
expected: "zoneee: credentials missing: username",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
config.Username = test.apiUser
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
if test.endpoint != "" {
|
2019-01-20 11:27:35 +00:00
|
|
|
config.Endpoint = mustParse(test.endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if test.expected == "" {
|
2019-01-20 11:27:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
2020-02-08 10:38:57 +00:00
|
|
|
hostedZone := "example.com"
|
|
|
|
domain := "prefix." + hostedZone
|
2019-01-20 11:27:35 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
username string
|
|
|
|
apiKey string
|
|
|
|
handlers map[string]http.HandlerFunc
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
username: "bar",
|
|
|
|
apiKey: "foo",
|
|
|
|
handlers: map[string]http.HandlerFunc{
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt"): mockHandlerCreateRecord,
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid auth",
|
|
|
|
username: "nope",
|
|
|
|
apiKey: "foo",
|
|
|
|
handlers: map[string]http.HandlerFunc{
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt"): mockHandlerCreateRecord,
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
2023-05-05 07:49:38 +00:00
|
|
|
expectedError: "zoneee: unexpected status code: [status code: 401] body: Unauthorized",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error",
|
|
|
|
username: "bar",
|
|
|
|
apiKey: "foo",
|
2023-05-05 07:49:38 +00:00
|
|
|
expectedError: "zoneee: unexpected status code: [status code: 404] body: 404 page not found",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
2023-05-05 07:49:38 +00:00
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2019-01-20 11:27:35 +00:00
|
|
|
for uri, handler := range test.handlers {
|
2023-05-05 07:49:38 +00:00
|
|
|
handler := handler
|
2019-01-20 11:27:35 +00:00
|
|
|
mux.HandleFunc(uri, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Endpoint = mustParse(server.URL)
|
|
|
|
config.Username = test.username
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = p.Present(domain, "token", "key")
|
|
|
|
if test.expectedError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expectedError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Cleanup(t *testing.T) {
|
2020-02-08 10:38:57 +00:00
|
|
|
hostedZone := "example.com"
|
|
|
|
domain := "prefix." + hostedZone
|
2019-01-20 11:27:35 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
username string
|
|
|
|
apiKey string
|
|
|
|
handlers map[string]http.HandlerFunc
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
username: "bar",
|
|
|
|
apiKey: "foo",
|
|
|
|
handlers: map[string]http.HandlerFunc{
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt"): mockHandlerGetRecords([]internal.TXTRecord{{
|
2019-01-20 11:27:35 +00:00
|
|
|
ID: "1234",
|
|
|
|
Name: domain,
|
|
|
|
Destination: "LHDhK3oGRvkiefQnx7OOczTY5Tic_xZ6HcMOc_gmtoM",
|
|
|
|
Delete: true,
|
|
|
|
Modify: true,
|
|
|
|
}}),
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt", "1234"): mockHandlerDeleteRecord,
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no txt records",
|
|
|
|
username: "bar",
|
|
|
|
apiKey: "foo",
|
|
|
|
handlers: map[string]http.HandlerFunc{
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt"): mockHandlerGetRecords([]internal.TXTRecord{}),
|
|
|
|
path.Join("/", "dns", hostedZone, "txt", "1234"): mockHandlerDeleteRecord,
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
expectedError: "zoneee: txt record does not exist for LHDhK3oGRvkiefQnx7OOczTY5Tic_xZ6HcMOc_gmtoM",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "invalid auth",
|
|
|
|
username: "nope",
|
|
|
|
apiKey: "foo",
|
|
|
|
handlers: map[string]http.HandlerFunc{
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt"): mockHandlerGetRecords([]internal.TXTRecord{{
|
2019-01-20 11:27:35 +00:00
|
|
|
ID: "1234",
|
|
|
|
Name: domain,
|
|
|
|
Destination: "LHDhK3oGRvkiefQnx7OOczTY5Tic_xZ6HcMOc_gmtoM",
|
|
|
|
Delete: true,
|
|
|
|
Modify: true,
|
|
|
|
}}),
|
2023-05-05 07:49:38 +00:00
|
|
|
path.Join("/", "dns", hostedZone, "txt", "1234"): mockHandlerDeleteRecord,
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
2023-05-05 07:49:38 +00:00
|
|
|
expectedError: "zoneee: unexpected status code: [status code: 401] body: Unauthorized",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "error",
|
|
|
|
username: "bar",
|
|
|
|
apiKey: "foo",
|
2023-05-05 07:49:38 +00:00
|
|
|
expectedError: "zoneee: unexpected status code: [status code: 404] body: 404 page not found",
|
2019-01-20 11:27:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
2023-05-05 07:49:38 +00:00
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2019-01-20 11:27:35 +00:00
|
|
|
for uri, handler := range test.handlers {
|
2023-05-05 07:49:38 +00:00
|
|
|
handler := handler
|
2019-01-20 11:27:35 +00:00
|
|
|
mux.HandleFunc(uri, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Endpoint = mustParse(server.URL)
|
|
|
|
config.Username = test.username
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = p.CleanUp(domain, "token", "key")
|
|
|
|
if test.expectedError == "" {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expectedError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLivePresent(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
|
|
|
if !envTest.IsLiveTest() {
|
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
|
|
|
envTest.RestoreEnv()
|
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustParse(rawURL string) *url.URL {
|
|
|
|
uri, err := url.Parse(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return uri
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockHandlerCreateRecord(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodPost {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
2021-11-03 19:39:12 +00:00
|
|
|
return
|
2019-01-20 11:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
username, apiKey, ok := req.BasicAuth()
|
|
|
|
if username != "bar" || apiKey != "foo" || !ok {
|
2021-11-03 19:39:12 +00:00
|
|
|
rw.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "Please enter your username and API key."))
|
2019-01-20 11:27:35 +00:00
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
record := internal.TXTRecord{}
|
2019-01-20 11:27:35 +00:00
|
|
|
err := json.NewDecoder(req.Body).Decode(&record)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
record.ID = "1234"
|
|
|
|
record.Delete = true
|
|
|
|
record.Modify = true
|
|
|
|
record.ResourceURL = req.URL.String() + "/1234"
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
bytes, err := json.Marshal([]internal.TXTRecord{record})
|
2019-01-20 11:27:35 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = rw.Write(bytes); err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func mockHandlerGetRecords(records []internal.TXTRecord) http.HandlerFunc {
|
2019-01-20 11:27:35 +00:00
|
|
|
return func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
2021-11-03 19:39:12 +00:00
|
|
|
return
|
2019-01-20 11:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
username, apiKey, ok := req.BasicAuth()
|
|
|
|
if username != "bar" || apiKey != "foo" || !ok {
|
2021-11-03 19:39:12 +00:00
|
|
|
rw.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "Please enter your username and API key."))
|
2019-01-20 11:27:35 +00:00
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range records {
|
2021-03-04 19:16:59 +00:00
|
|
|
if value.ResourceURL == "" {
|
2019-01-20 11:27:35 +00:00
|
|
|
value.ResourceURL = req.URL.String() + "/" + value.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(records)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = rw.Write(bytes); err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockHandlerDeleteRecord(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.Method != http.MethodDelete {
|
|
|
|
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
username, apiKey, ok := req.BasicAuth()
|
|
|
|
if username != "bar" || apiKey != "foo" || !ok {
|
2021-11-03 19:39:12 +00:00
|
|
|
rw.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "Please enter your username and API key."))
|
2019-01-20 11:27:35 +00:00
|
|
|
http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|