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