2019-03-11 16:56:48 +00:00
|
|
|
package gandi
|
2016-03-12 16:13:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"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/require"
|
2016-03-12 16:13:24 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
var envTest = tester.NewEnvTest(EnvAPIKey)
|
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{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "123",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
2020-03-11 22:51:10 +00:00
|
|
|
EnvAPIKey: "",
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
expected: "gandi: some credentials information are missing: GANDI_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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.inProgressFQDNs)
|
|
|
|
require.NotNil(t, p.inProgressAuthZones)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
apiKey string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
apiKey: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
expected: "gandi: no API Key given",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
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.inProgressFQDNs)
|
|
|
|
require.NotNil(t, p.inProgressAuthZones)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:13:24 +00:00
|
|
|
// TestDNSProvider runs Present and CleanUp against a fake Gandi RPC
|
|
|
|
// Server, whose responses are predetermined for particular requests.
|
|
|
|
func TestDNSProvider(t *testing.T) {
|
2018-12-06 21:50:17 +00:00
|
|
|
// serverResponses is the XML-RPC Request->Response map used by the
|
|
|
|
// fake RPC server. It was generated by recording a real RPC session
|
|
|
|
// which resulted in the successful issue of a cert, and then
|
|
|
|
// anonymizing the RPC data.
|
2020-07-09 23:48:18 +00:00
|
|
|
serverResponses := map[string]string{
|
2018-12-06 21:50:17 +00:00
|
|
|
// Present Request->Response 1 (getZoneID)
|
2020-05-08 17:35:25 +00:00
|
|
|
presentGetZoneIDRequestMock: presentGetZoneIDResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// Present Request->Response 2 (cloneZone)
|
2020-05-08 17:35:25 +00:00
|
|
|
presentCloneZoneRequestMock: presentCloneZoneResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// Present Request->Response 3 (newZoneVersion)
|
2020-05-08 17:35:25 +00:00
|
|
|
presentNewZoneVersionRequestMock: presentNewZoneVersionResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// Present Request->Response 4 (addTXTRecord)
|
2020-05-08 17:35:25 +00:00
|
|
|
presentAddTXTRecordRequestMock: presentAddTXTRecordResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// Present Request->Response 5 (setZoneVersion)
|
2020-05-08 17:35:25 +00:00
|
|
|
presentSetZoneVersionRequestMock: presentSetZoneVersionResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// Present Request->Response 6 (setZone)
|
2020-05-08 17:35:25 +00:00
|
|
|
presentSetZoneRequestMock: presentSetZoneResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// CleanUp Request->Response 1 (setZone)
|
2020-05-08 17:35:25 +00:00
|
|
|
cleanupSetZoneRequestMock: cleanupSetZoneResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
// CleanUp Request->Response 2 (deleteZone)
|
2020-05-08 17:35:25 +00:00
|
|
|
cleanupDeleteZoneRequestMock: cleanupDeleteZoneResponseMock,
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:13:24 +00:00
|
|
|
fakeKeyAuth := "XXXX"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
regexpDate := regexp.MustCompile(`\[ACME Challenge [^\]:]*:[^\]]*\]`)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-12 16:13:24 +00:00
|
|
|
// start fake RPC server
|
2021-11-01 23:52:38 +00:00
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-06-11 15:32:50 +00:00
|
|
|
require.Equal(t, "text/xml", r.Header.Get("Content-Type"), "invalid content type")
|
|
|
|
|
2021-08-25 09:44:11 +00:00
|
|
|
req, errS := io.ReadAll(r.Body)
|
2018-09-24 19:07:20 +00:00
|
|
|
require.NoError(t, errS)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
|
|
|
req = regexpDate.ReplaceAllLiteral(req, []byte(`[ACME Challenge 01 Jan 16 00:00 +0000]`))
|
2016-03-12 16:13:24 +00:00
|
|
|
resp, ok := serverResponses[string(req)]
|
2023-05-05 07:49:38 +00:00
|
|
|
require.Truef(t, ok, "Server response for request not found: %s", string(req))
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
_, errS = io.Copy(w, strings.NewReader(resp))
|
|
|
|
require.NoError(t, errS)
|
2016-03-12 16:13:24 +00:00
|
|
|
}))
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Cleanup(server.Close)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-24 17:13:18 +00:00
|
|
|
// define function to override findZoneByFqdn with
|
2018-12-06 21:50:17 +00:00
|
|
|
fakeFindZoneByFqdn := func(fqdn string) (string, error) {
|
2016-03-24 17:13:18 +00:00
|
|
|
return "example.com.", nil
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2021-11-01 23:52:38 +00:00
|
|
|
config.BaseURL = server.URL + "/"
|
2018-09-15 17:07:24 +00:00
|
|
|
config.APIKey = "123412341234123412341234"
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// override findZoneByFqdn function
|
2018-12-06 21:50:17 +00:00
|
|
|
savedFindZoneByFqdn := provider.findZoneByFqdn
|
2021-11-01 23:52:38 +00:00
|
|
|
t.Cleanup(func() {
|
2018-12-06 21:50:17 +00:00
|
|
|
provider.findZoneByFqdn = savedFindZoneByFqdn
|
2021-11-01 23:52:38 +00:00
|
|
|
})
|
2018-12-06 21:50:17 +00:00
|
|
|
provider.findZoneByFqdn = fakeFindZoneByFqdn
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2016-03-12 16:13:24 +00:00
|
|
|
// run Present
|
|
|
|
err = provider.Present("abc.def.example.com", "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2016-03-12 16:13:24 +00:00
|
|
|
// run CleanUp
|
|
|
|
err = provider.CleanUp("abc.def.example.com", "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-12 16:13:24 +00:00
|
|
|
}
|