2016-11-04 09:29:14 +00:00
|
|
|
package rackspace
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-15 17:07:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-16 15:52:57 +00:00
|
|
|
"github.com/xenolf/lego/platform/tester"
|
2016-11-04 09:29:14 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
"RACKSPACE_USER",
|
|
|
|
"RACKSPACE_API_KEY").
|
|
|
|
WithDomain("RACKSPACE_DOMAIN")
|
2018-10-12 17:29:18 +00:00
|
|
|
|
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
config, tearDown := setupTest()
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, provider.config)
|
|
|
|
|
|
|
|
assert.Equal(t, provider.token, "testToken", "The token should match")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewDNSProviderConfig_MissingCredErr(t *testing.T) {
|
|
|
|
_, err := NewDNSProviderConfig(NewDefaultConfig())
|
|
|
|
assert.EqualError(t, err, "rackspace: credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_Present(t *testing.T) {
|
|
|
|
config, tearDown := setupTest()
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = provider.Present("example.com", "token", "keyAuth")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDNSProvider_CleanUp(t *testing.T) {
|
|
|
|
config, tearDown := setupTest()
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
provider, err := NewDNSProviderConfig(config)
|
|
|
|
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = provider.CleanUp("example.com", "token", "keyAuth")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveNewDNSProvider_ValidEnv(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")
|
2016-11-04 09:29:14 +00:00
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
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
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
assert.Contains(t, provider.cloudDNSEndpoint, "https://dns.api.rackspacecloud.com/v1.0/", "The endpoint URL should contain the base")
|
2016-11-04 09:29:14 +00:00
|
|
|
}
|
|
|
|
|
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(), "", "112233445566==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-04 09:29:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
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)
|
|
|
|
|
2018-10-16 15:52:57 +00:00
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "112233445566==")
|
2018-10-12 17:29:18 +00:00
|
|
|
require.NoError(t, err)
|
2016-11-04 09:29:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func setupTest() (*Config, func()) {
|
|
|
|
apiURL, tearDown := startTestServers()
|
2016-11-04 09:29:14 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIUser = "testUser"
|
|
|
|
config.APIKey = "testKey"
|
|
|
|
config.BaseURL = apiURL
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
return config, tearDown
|
2016-11-04 09:29:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func startTestServers() (string, func()) {
|
|
|
|
dnsAPI := httptest.NewServer(dnsHandler())
|
|
|
|
identityAPI := httptest.NewServer(identityHandler(dnsAPI.URL + "/123456"))
|
|
|
|
|
|
|
|
return identityAPI.URL + "/", func() {
|
|
|
|
identityAPI.Close()
|
|
|
|
dnsAPI.Close()
|
|
|
|
}
|
2016-11-04 09:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func identityHandler(dnsEndpoint string) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
2016-11-04 09:29:14 +00:00
|
|
|
resp, found := jsonMap[string(reqBody)]
|
|
|
|
if !found {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2018-10-12 17:29:18 +00:00
|
|
|
|
2016-11-04 09:29:14 +00:00
|
|
|
resp = strings.Replace(resp, "https://dns.api.rackspacecloud.com/v1.0/123456", dnsEndpoint, 1)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
func dnsHandler() *http.ServeMux {
|
2016-11-04 09:29:14 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// Used by `getHostedZoneID()` finding `zoneID` "?name=example.com"
|
|
|
|
mux.HandleFunc("/123456/domains", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Query().Get("name") == "example.com" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, jsonMap["zoneDetails"])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/123456/domains/112233/records", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
// Used by `Present()` creating the TXT record
|
|
|
|
case http.MethodPost:
|
|
|
|
reqBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2018-10-12 17:29:18 +00:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2016-11-04 09:29:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
resp, found := jsonMap[string(reqBody)]
|
|
|
|
if !found {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
fmt.Fprintf(w, resp)
|
2018-10-12 17:29:18 +00:00
|
|
|
// Used by `findTxtRecord()` finding `record.ID` "?type=TXT&name=_acme-challenge.example.com"
|
2016-11-04 09:29:14 +00:00
|
|
|
case http.MethodGet:
|
|
|
|
if r.URL.Query().Get("type") == "TXT" && r.URL.Query().Get("name") == "_acme-challenge.example.com" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, jsonMap["recordDetails"])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
2018-10-12 17:29:18 +00:00
|
|
|
// Used by `CleanUp()` deleting the TXT record "?id=445566"
|
2016-11-04 09:29:14 +00:00
|
|
|
case http.MethodDelete:
|
|
|
|
if r.URL.Query().Get("id") == "TXT-654321" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, jsonMap["recordDelete"])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Printf("Not Found for Request: (%+v)\n\n", r)
|
|
|
|
})
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonMap = map[string]string{
|
|
|
|
`{"auth":{"RAX-KSKEY:apiKeyCredentials":{"username":"testUser","apiKey":"testKey"}}}`: `{"access":{"token":{"id":"testToken","expires":"1970-01-01T00:00:00.000Z","tenant":{"id":"123456","name":"123456"},"RAX-AUTH:authenticatedBy":["APIKEY"]},"serviceCatalog":[{"type":"rax:dns","endpoints":[{"publicURL":"https://dns.api.rackspacecloud.com/v1.0/123456","tenantId":"123456"}],"name":"cloudDNS"}],"user":{"id":"fakeUseID","name":"testUser"}}}`,
|
|
|
|
"zoneDetails": `{"domains":[{"name":"example.com","id":112233,"emailAddress":"hostmaster@example.com","updated":"1970-01-01T00:00:00.000+0000","created":"1970-01-01T00:00:00.000+0000"}],"totalEntries":1}`,
|
|
|
|
`{"records":[{"name":"_acme-challenge.example.com","type":"TXT","data":"pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM","ttl":300}]}`: `{"request":"{\"records\":[{\"name\":\"_acme-challenge.example.com\",\"type\":\"TXT\",\"data\":\"pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM\",\"ttl\":300}]}","status":"RUNNING","verb":"POST","jobId":"00000000-0000-0000-0000-0000000000","callbackUrl":"https://dns.api.rackspacecloud.com/v1.0/123456/status/00000000-0000-0000-0000-0000000000","requestUrl":"https://dns.api.rackspacecloud.com/v1.0/123456/domains/112233/records"}`,
|
|
|
|
"recordDetails": `{"records":[{"name":"_acme-challenge.example.com","id":"TXT-654321","type":"TXT","data":"pW9ZKG0xz_PCriK-nCMOjADy9eJcgGWIzkkj2fN4uZM","ttl":300,"updated":"1970-01-01T00:00:00.000+0000","created":"1970-01-01T00:00:00.000+0000"}]}`,
|
|
|
|
"recordDelete": `{"status":"RUNNING","verb":"DELETE","jobId":"00000000-0000-0000-0000-0000000000","callbackUrl":"https://dns.api.rackspacecloud.com/v1.0/123456/status/00000000-0000-0000-0000-0000000000","requestUrl":"https://dns.api.rackspacecloud.com/v1.0/123456/domains/112233/recordsid=TXT-654321"}`,
|
|
|
|
}
|