2019-03-11 18:56:48 +02:00
|
|
|
package azure
|
2016-11-02 16:33:57 +02:00
|
|
|
|
|
|
|
import (
|
2018-10-23 10:03:31 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-11-02 16:33:57 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-30 21:19:32 +02:00
|
|
|
"github.com/go-acme/lego/v3/platform/tester"
|
2018-10-12 19:29:18 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-11-02 16:33:57 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
const envDomain = envNamespace + "DOMAIN"
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
2020-08-09 14:17:49 +02:00
|
|
|
EnvEnvironment,
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvClientID,
|
|
|
|
EnvClientSecret,
|
|
|
|
EnvSubscriptionID,
|
|
|
|
EnvTenantID,
|
|
|
|
EnvResourceGroup).
|
|
|
|
WithDomain(envDomain)
|
2016-11-02 16:33:57 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestNewDNSProvider(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
envVars map[string]string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvClientID: "A",
|
|
|
|
EnvClientSecret: "B",
|
|
|
|
EnvTenantID: "C",
|
|
|
|
EnvSubscriptionID: "D",
|
|
|
|
EnvResourceGroup: "E",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 10:03:31 +02:00
|
|
|
desc: "missing client ID",
|
2018-10-12 19:29:18 +02:00
|
|
|
envVars: map[string]string{
|
2020-03-12 00:51:10 +02:00
|
|
|
EnvClientID: "",
|
|
|
|
EnvClientSecret: "B",
|
|
|
|
EnvTenantID: "C",
|
|
|
|
EnvSubscriptionID: "D",
|
|
|
|
EnvResourceGroup: "E",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
2018-10-23 10:03:31 +02:00
|
|
|
expected: "failed to get oauth token from client credentials: parameter 'clientID' cannot be empty",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
defer envTest.RestoreEnv()
|
|
|
|
envTest.ClearEnv()
|
|
|
|
|
|
|
|
envTest.Apply(test.envVars)
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
p, err := NewDNSProvider()
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestNewDNSProviderConfig(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
clientID string
|
|
|
|
clientSecret string
|
|
|
|
subscriptionID string
|
|
|
|
tenantID string
|
|
|
|
resourceGroup string
|
2018-10-23 10:03:31 +02:00
|
|
|
handler func(w http.ResponseWriter, r *http.Request)
|
2018-10-12 19:29:18 +02:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "success",
|
|
|
|
clientID: "A",
|
|
|
|
clientSecret: "B",
|
2018-10-23 10:03:31 +02:00
|
|
|
tenantID: "C",
|
|
|
|
subscriptionID: "D",
|
2018-10-12 19:29:18 +02:00
|
|
|
resourceGroup: "E",
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 10:03:31 +02:00
|
|
|
desc: "SubscriptionID missing",
|
2018-10-12 19:29:18 +02:00
|
|
|
clientID: "A",
|
|
|
|
clientSecret: "B",
|
2018-10-23 10:03:31 +02:00
|
|
|
tenantID: "C",
|
2018-10-12 19:29:18 +02:00
|
|
|
subscriptionID: "",
|
2018-10-23 10:03:31 +02:00
|
|
|
resourceGroup: "",
|
|
|
|
expected: "azure: SubscriptionID is missing",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
{
|
2018-10-23 10:03:31 +02:00
|
|
|
desc: "ResourceGroup missing",
|
2018-10-12 19:29:18 +02:00
|
|
|
clientID: "A",
|
|
|
|
clientSecret: "B",
|
2018-10-23 10:03:31 +02:00
|
|
|
tenantID: "C",
|
|
|
|
subscriptionID: "D",
|
|
|
|
resourceGroup: "",
|
|
|
|
expected: "azure: ResourceGroup is missing",
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
|
|
|
{
|
2018-10-23 10:03:31 +02:00
|
|
|
desc: "use metadata",
|
2018-10-12 19:29:18 +02:00
|
|
|
clientID: "A",
|
|
|
|
clientSecret: "B",
|
2018-10-23 10:03:31 +02:00
|
|
|
tenantID: "C",
|
|
|
|
subscriptionID: "",
|
2018-10-12 19:29:18 +02:00
|
|
|
resourceGroup: "",
|
2019-02-06 19:15:53 +02:00
|
|
|
handler: func(w http.ResponseWriter, _ *http.Request) {
|
2018-10-23 10:03:31 +02:00
|
|
|
_, err := w.Write([]byte("foo"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
},
|
2018-10-12 19:29:18 +02:00
|
|
|
},
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.ClientID = test.clientID
|
|
|
|
config.ClientSecret = test.clientSecret
|
|
|
|
config.SubscriptionID = test.subscriptionID
|
|
|
|
config.TenantID = test.tenantID
|
|
|
|
config.ResourceGroup = test.resourceGroup
|
2016-11-02 16:33:57 +02:00
|
|
|
|
2018-10-23 10:03:31 +02:00
|
|
|
handler := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(handler)
|
|
|
|
defer server.Close()
|
|
|
|
if test.handler == nil {
|
|
|
|
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
} else {
|
|
|
|
handler.HandleFunc("/", test.handler)
|
|
|
|
}
|
|
|
|
config.MetadataEndpoint = server.URL
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
p, err := NewDNSProviderConfig(config)
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
if len(test.expected) == 0 {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.config)
|
|
|
|
} else {
|
|
|
|
require.EqualError(t, err, test.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLivePresent(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-11-02 16:33:57 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 19:29:18 +02:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.Present(envTest.GetDomain(), "", "123d==")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
func TestLiveCleanUp(t *testing.T) {
|
2018-10-16 17:52:57 +02:00
|
|
|
if !envTest.IsLiveTest() {
|
2016-11-02 16:33:57 +02:00
|
|
|
t.Skip("skipping live test")
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
envTest.RestoreEnv()
|
2018-10-12 19:29:18 +02:00
|
|
|
provider, err := NewDNSProvider()
|
|
|
|
require.NoError(t, err)
|
2016-11-02 16:33:57 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
2016-11-02 16:33:57 +02:00
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
|
2018-10-12 19:29:18 +02:00
|
|
|
require.NoError(t, err)
|
2016-11-02 16:33:57 +02:00
|
|
|
}
|