2016-11-02 16:33:57 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2018-10-23 10:03:31 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-11-02 16:33:57 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-16 17:52:57 +02:00
|
|
|
"github.com/xenolf/lego/platform/tester"
|
2016-11-02 16:33:57 +02:00
|
|
|
)
|
|
|
|
|
2018-10-16 17:52:57 +02:00
|
|
|
var envTest = tester.NewEnvTest(
|
|
|
|
"AZURE_CLIENT_ID",
|
|
|
|
"AZURE_CLIENT_SECRET",
|
|
|
|
"AZURE_SUBSCRIPTION_ID",
|
|
|
|
"AZURE_TENANT_ID",
|
|
|
|
"AZURE_RESOURCE_GROUP").
|
|
|
|
WithDomain("AZURE_DOMAIN")
|
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{
|
|
|
|
"AZURE_CLIENT_ID": "A",
|
|
|
|
"AZURE_CLIENT_SECRET": "B",
|
2018-10-23 10:03:31 +02:00
|
|
|
"AZURE_TENANT_ID": "C",
|
|
|
|
"AZURE_SUBSCRIPTION_ID": "D",
|
2018-10-12 19:29:18 +02:00
|
|
|
"AZURE_RESOURCE_GROUP": "E",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 10:03:31 +02:00
|
|
|
desc: "missing client ID",
|
2018-10-12 19:29:18 +02:00
|
|
|
envVars: map[string]string{
|
|
|
|
"AZURE_CLIENT_ID": "",
|
|
|
|
"AZURE_CLIENT_SECRET": "B",
|
2018-10-23 10:03:31 +02:00
|
|
|
"AZURE_TENANT_ID": "C",
|
|
|
|
"AZURE_SUBSCRIPTION_ID": "D",
|
2018-10-12 19:29:18 +02:00
|
|
|
"AZURE_RESOURCE_GROUP": "E",
|
|
|
|
},
|
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
|
|
|
}
|