2019-03-11 18:56:48 +02:00
|
|
|
package designate
|
2019-02-07 17:10:07 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-08-09 11:48:13 +02:00
|
|
|
"os"
|
2019-02-07 17:10:07 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2020-08-09 11:48:13 +02:00
|
|
|
"github.com/gophercloud/utils/openstack/clientconfig"
|
2019-02-07 17:10:07 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-09 11:48:13 +02:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-02-07 17:10:07 +02:00
|
|
|
)
|
|
|
|
|
2020-08-09 11:48:13 +02:00
|
|
|
const (
|
|
|
|
envDomain = envNamespace + "DOMAIN"
|
|
|
|
envOSClientConfigFile = "OS_CLIENT_CONFIG_FILE"
|
|
|
|
)
|
2020-03-12 00:51:10 +02:00
|
|
|
|
2019-02-07 17:10:07 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-08-09 11:48:13 +02:00
|
|
|
EnvCloud,
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAuthURL,
|
|
|
|
EnvUsername,
|
|
|
|
EnvPassword,
|
2021-02-28 01:45:58 +02:00
|
|
|
EnvUserID,
|
|
|
|
EnvAppCredID,
|
|
|
|
EnvAppCredName,
|
|
|
|
EnvAppCredSecret,
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvTenantName,
|
|
|
|
EnvRegionName,
|
2020-08-09 11:48:13 +02:00
|
|
|
EnvProjectID,
|
|
|
|
envOSClientConfigFile).
|
2020-03-12 00:51:10 +02:00
|
|
|
WithDomain(envDomain)
|
2019-02-07 17:10:07 +02:00
|
|
|
|
2020-08-09 11:48:13 +02:00
|
|
|
func TestNewDNSProvider_fromEnv(t *testing.T) {
|
2020-12-29 00:39:00 +02:00
|
|
|
serverURL := setupTestProvider(t)
|
2019-02-07 17:10:07 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-12-29 00:39:00 +02:00
|
|
|
EnvAuthURL: serverURL + "/v2.0/",
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvUsername: "B",
|
|
|
|
EnvPassword: "C",
|
|
|
|
EnvRegionName: "D",
|
2020-11-04 00:28:25 +02:00
|
|
|
EnvProjectID: "E",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing credentials",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAuthURL: "",
|
|
|
|
EnvUsername: "",
|
|
|
|
EnvPassword: "",
|
|
|
|
EnvRegionName: "",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
2021-02-28 01:45:58 +02:00
|
|
|
expected: "designate: Missing environment variable [OS_AUTH_URL]",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing auth url",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvAuthURL: "",
|
|
|
|
EnvUsername: "B",
|
|
|
|
EnvPassword: "C",
|
|
|
|
EnvRegionName: "D",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
2021-02-28 01:45:58 +02:00
|
|
|
expected: "designate: Missing environment variable [OS_AUTH_URL]",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
envVars: map[string]string{
|
2020-12-29 00:39:00 +02:00
|
|
|
EnvAuthURL: serverURL + "/v2.0/",
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvUsername: "",
|
|
|
|
EnvPassword: "C",
|
|
|
|
EnvRegionName: "D",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
2021-02-28 01:45:58 +02:00
|
|
|
expected: "designate: Missing one of the following environment variables [OS_USERID, OS_USERNAME]",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
envVars: map[string]string{
|
2020-12-29 00:39:00 +02:00
|
|
|
EnvAuthURL: serverURL + "/v2.0/",
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvUsername: "B",
|
|
|
|
EnvPassword: "",
|
|
|
|
EnvRegionName: "D",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
2021-02-28 01:45:58 +02:00
|
|
|
expected: "designate: Missing environment variable [OS_PASSWORD]",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
{
|
2021-02-28 01:45:58 +02:00
|
|
|
desc: "missing application credential secret",
|
2019-02-07 17:10:07 +02:00
|
|
|
envVars: map[string]string{
|
2020-12-29 00:39:00 +02:00
|
|
|
EnvAuthURL: serverURL + "/v2.0/",
|
2021-02-28 01:45:58 +02:00
|
|
|
EnvRegionName: "D",
|
|
|
|
EnvAppCredID: "F",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
2021-02-28 01:45:58 +02:00
|
|
|
expected: "designate: Missing environment variable [OS_APPLICATION_CREDENTIAL_SECRET]",
|
2019-02-07 17:10:07 +02: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 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2019-02-07 17:10:07 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:48:13 +02:00
|
|
|
func TestNewDNSProvider_fromCloud(t *testing.T) {
|
2020-12-29 00:39:00 +02:00
|
|
|
serverURL := setupTestProvider(t)
|
2020-08-09 11:48:13 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
osCloud string
|
|
|
|
cloud clientconfig.Cloud
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
osCloud: "good_cloud",
|
|
|
|
cloud: clientconfig.Cloud{
|
|
|
|
AuthInfo: &clientconfig.AuthInfo{
|
2020-12-29 00:39:00 +02:00
|
|
|
AuthURL: serverURL + "/v2.0/",
|
2020-08-09 11:48:13 +02:00
|
|
|
Username: "B",
|
|
|
|
Password: "C",
|
|
|
|
ProjectName: "E",
|
|
|
|
ProjectID: "F",
|
|
|
|
},
|
|
|
|
RegionName: "D",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing auth url",
|
|
|
|
osCloud: "missing_auth_url",
|
|
|
|
cloud: clientconfig.Cloud{
|
|
|
|
AuthInfo: &clientconfig.AuthInfo{
|
|
|
|
Username: "B",
|
|
|
|
Password: "C",
|
|
|
|
ProjectName: "E",
|
|
|
|
ProjectID: "F",
|
|
|
|
},
|
|
|
|
RegionName: "D",
|
|
|
|
},
|
|
|
|
expected: "designate: Missing input for argument [auth_url]",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing username",
|
|
|
|
osCloud: "missing_username",
|
|
|
|
cloud: clientconfig.Cloud{
|
|
|
|
AuthInfo: &clientconfig.AuthInfo{
|
2020-12-29 00:39:00 +02:00
|
|
|
AuthURL: serverURL + "/v2.0/",
|
2020-08-09 11:48:13 +02:00
|
|
|
Password: "C",
|
|
|
|
ProjectName: "E",
|
|
|
|
ProjectID: "F",
|
|
|
|
},
|
|
|
|
RegionName: "D",
|
|
|
|
},
|
|
|
|
expected: "designate: failed to authenticate: Missing input for argument [Username]",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "missing password",
|
|
|
|
osCloud: "missing_auth_url",
|
|
|
|
cloud: clientconfig.Cloud{
|
|
|
|
AuthInfo: &clientconfig.AuthInfo{
|
2020-12-29 00:39:00 +02:00
|
|
|
AuthURL: serverURL + "/v2.0/",
|
2020-08-09 11:48:13 +02:00
|
|
|
Username: "B",
|
|
|
|
ProjectName: "E",
|
|
|
|
ProjectID: "F",
|
|
|
|
},
|
|
|
|
RegionName: "D",
|
|
|
|
},
|
|
|
|
expected: "designate: failed to authenticate: Exactly one of PasswordCredentials and TokenCredentials must be provided",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(map[string]string{
|
|
|
|
EnvCloud: test.osCloud,
|
|
|
|
envOSClientConfigFile: createCloudsYaml(t, test.osCloud, test.cloud),
|
|
|
|
})
|
|
|
|
|
|
|
|
p, err := NewDNSProvider()
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2020-08-09 11:48:13 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 17:10:07 +02:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
2020-12-29 00:39:00 +02:00
|
|
|
serverURL := setupTestProvider(t)
|
2019-02-07 17:10:07 +02:00
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
tenantName string
|
|
|
|
password string
|
|
|
|
userName string
|
|
|
|
authURL string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
tenantName: "A",
|
|
|
|
password: "B",
|
|
|
|
userName: "C",
|
2020-12-29 00:39:00 +02:00
|
|
|
authURL: serverURL + "/v2.0/",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "wrong auth url",
|
|
|
|
tenantName: "A",
|
|
|
|
password: "B",
|
|
|
|
userName: "C",
|
2020-12-29 00:39:00 +02:00
|
|
|
authURL: serverURL,
|
|
|
|
expected: "designate: failed to authenticate: No supported version available from endpoint " + serverURL + "/",
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.opts.TenantName = test.tenantName
|
|
|
|
config.opts.Password = test.password
|
|
|
|
config.opts.Username = test.userName
|
|
|
|
config.opts.IdentityEndpoint = test.authURL
|
|
|
|
|
|
|
|
p, err := NewDNSProviderConfig(config)
|
|
|
|
|
2021-03-04 21:16:59 +02:00
|
|
|
if test.expected == "" {
|
2019-02-07 17:10:07 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:48:13 +02:00
|
|
|
// createCloudsYaml creates a temporary cloud file for testing purpose.
|
|
|
|
func createCloudsYaml(t *testing.T, cloudName string, cloud clientconfig.Cloud) string {
|
2020-12-29 00:39:00 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2022-08-22 17:05:31 +02:00
|
|
|
file, err := os.CreateTemp("", "lego_test")
|
2020-08-09 11:48:13 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() { _ = os.RemoveAll(file.Name()) })
|
|
|
|
|
|
|
|
clouds := clientconfig.Clouds{
|
|
|
|
Clouds: map[string]clientconfig.Cloud{
|
|
|
|
cloudName: cloud,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.NewEncoder(file).Encode(&clouds)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return file.Name()
|
|
|
|
}
|
|
|
|
|
2020-12-29 00:39:00 +02:00
|
|
|
func setupTestProvider(t *testing.T) string {
|
|
|
|
t.Helper()
|
|
|
|
|
2019-02-07 17:10:07 +02:00
|
|
|
mux := http.NewServeMux()
|
2023-05-05 09:49:38 +02:00
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2019-02-07 17:10:07 +02:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
_, _ = w.Write([]byte(`{
|
|
|
|
"access": {
|
|
|
|
"token": {
|
|
|
|
"id": "a",
|
|
|
|
"expires": "9015-06-05T16:24:57.637Z"
|
|
|
|
},
|
|
|
|
"user": {
|
|
|
|
"name": "a",
|
|
|
|
"roles": [ ],
|
2020-11-04 00:28:25 +02:00
|
|
|
"role_links": [ ]
|
2019-02-07 17:10:07 +02:00
|
|
|
},
|
|
|
|
"serviceCatalog": [
|
|
|
|
{
|
|
|
|
"endpoints": [
|
|
|
|
{
|
|
|
|
"adminURL": "http://23.253.72.207:9696/",
|
|
|
|
"region": "D",
|
|
|
|
"internalURL": "http://23.253.72.207:9696/",
|
|
|
|
"id": "97c526db8d7a4c88bbb8d68db1bdcdb8",
|
|
|
|
"publicURL": "http://23.253.72.207:9696/"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"endpoints_links": [ ],
|
|
|
|
"type": "dns",
|
|
|
|
"name": "designate"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`))
|
2022-08-22 17:05:31 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-02-07 17:10:07 +02:00
|
|
|
})
|
2020-08-09 11:48:13 +02:00
|
|
|
|
2020-12-29 00:39:00 +02:00
|
|
|
return server.URL
|
2019-02-07 17:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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(1 * time.Second)
|
|
|
|
|
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|