2018-02-02 19:22:33 +00:00
|
|
|
package gandiv5
|
|
|
|
|
|
|
|
import (
|
2018-10-08 14:51:29 +00:00
|
|
|
"fmt"
|
2018-02-02 19:22:33 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-10-12 17:29:18 +00:00
|
|
|
"os"
|
2018-02-02 19:22:33 +00:00
|
|
|
"regexp"
|
|
|
|
"testing"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-08 14:51:29 +00:00
|
|
|
"github.com/xenolf/lego/log"
|
2018-10-12 17:29:18 +00:00
|
|
|
)
|
2018-10-08 14:51:29 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
var (
|
|
|
|
envTestAPIKey string
|
2018-02-02 19:22:33 +00:00
|
|
|
)
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func init() {
|
|
|
|
envTestAPIKey = os.Getenv("GANDIV5_API_KEY")
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreEnv() {
|
|
|
|
os.Setenv("GANDIV5_API_KEY", envTestAPIKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"GANDIV5_API_KEY": "123",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing api key",
|
|
|
|
envVars: map[string]string{
|
|
|
|
"GANDIV5_API_KEY": "",
|
|
|
|
},
|
|
|
|
expected: "gandi: some credentials information are missing: GANDIV5_API_KEY",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
for key, value := range test.envVars {
|
|
|
|
if len(value) == 0 {
|
|
|
|
os.Unsetenv(key)
|
|
|
|
} else {
|
|
|
|
os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.inProgressFQDNs)
|
|
|
|
} 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: "gandiv5: no API Key given",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer restoreEnv()
|
|
|
|
os.Unsetenv("GANDIV5_API_KEY")
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = test.apiKey
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
require.NotNil(t, p.inProgressFQDNs)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:22:33 +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) {
|
|
|
|
fakeKeyAuth := "XXXX"
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
regexpToken, err := regexp.Compile(`"rrset_values":\[".+"\]`)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// start fake RPC server
|
2018-10-08 14:51:29 +00:00
|
|
|
handler := http.NewServeMux()
|
|
|
|
handler.HandleFunc("/domains/example.com/records/_acme-challenge.abc.def/TXT", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Infof("request: %s %s", req.Method, req.URL)
|
|
|
|
|
|
|
|
if req.Header.Get(apiKeyHeader) == "" {
|
|
|
|
http.Error(rw, `{"message": "missing API key"}`, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Method == http.MethodPost && req.Header.Get("Content-Type") != "application/json" {
|
|
|
|
http.Error(rw, `{"message": "invalid content type"}`, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body, errS := ioutil.ReadAll(req.Body)
|
|
|
|
if errS != nil {
|
|
|
|
http.Error(rw, fmt.Sprintf(`{"message": "read body error: %v"}`, errS), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body = regexpToken.ReplaceAllLiteral(body, []byte(`"rrset_values":["TOKEN"]`))
|
|
|
|
|
|
|
|
responses, ok := serverResponses[req.Method]
|
|
|
|
if !ok {
|
|
|
|
http.Error(rw, fmt.Sprintf(`{"message": "Server response for request not found: %#q"}`, string(body)), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := responses[string(body)]
|
|
|
|
|
|
|
|
_, errS = rw.Write([]byte(resp))
|
|
|
|
if errS != nil {
|
|
|
|
http.Error(rw, fmt.Sprintf(`{"message": "failed to write response: %v"}`, errS), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
handler.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Infof("request: %s %s", req.Method, req.URL)
|
|
|
|
http.Error(rw, fmt.Sprintf(`{"message": "URL doesn't match: %s"}`, req.URL), http.StatusNotFound)
|
|
|
|
})
|
|
|
|
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
defer server.Close()
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// define function to override findZoneByFqdn with
|
|
|
|
fakeFindZoneByFqdn := func(fqdn string, nameserver []string) (string, error) {
|
|
|
|
return "example.com.", nil
|
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = "123412341234123412341234"
|
2018-10-08 14:51:29 +00:00
|
|
|
config.BaseURL = server.URL
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// override findZoneByFqdn function
|
|
|
|
savedFindZoneByFqdn := findZoneByFqdn
|
2018-02-02 19:22:33 +00:00
|
|
|
defer func() {
|
2018-09-15 17:07:24 +00:00
|
|
|
findZoneByFqdn = savedFindZoneByFqdn
|
2018-02-02 19:22:33 +00:00
|
|
|
}()
|
2018-09-15 17:07:24 +00:00
|
|
|
findZoneByFqdn = fakeFindZoneByFqdn
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// run Present
|
|
|
|
err = provider.Present("abc.def.example.com", "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// run CleanUp
|
|
|
|
err = provider.CleanUp("abc.def.example.com", "", fakeKeyAuth)
|
2018-06-11 15:32:50 +00:00
|
|
|
require.NoError(t, err)
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// serverResponses is the JSON Request->Response map used by the
|
|
|
|
// fake JSON server.
|
2018-10-08 14:51:29 +00:00
|
|
|
var serverResponses = map[string]map[string]string{
|
|
|
|
http.MethodGet: {
|
|
|
|
``: `{"rrset_ttl":300,"rrset_values":[],"rrset_name":"_acme-challenge.abc.def","rrset_type":"TXT"}`,
|
|
|
|
},
|
|
|
|
http.MethodPut: {
|
|
|
|
`{"rrset_ttl":300,"rrset_values":["TOKEN"]}`: `{"message": "Zone Record Created"}`,
|
|
|
|
},
|
|
|
|
http.MethodDelete: {
|
|
|
|
``: ``,
|
|
|
|
},
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|