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"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/tester"
|
2022-01-17 09:05:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
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-08-09 12:17:49 +00:00
|
|
|
EnvEnvironment,
|
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
|
|
|
},
|
2021-07-12 07:27:49 +00:00
|
|
|
expected: "failed to get SPT 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
|
|
|
|
2022-01-17 09:05:59 +00:00
|
|
|
if test.expected != "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.EqualError(t, err, test.expected)
|
2022-01-17 09:05:59 +00:00
|
|
|
return
|
2018-10-12 17:29:18 +00:00
|
|
|
}
|
2022-01-17 09:05:59 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.provider)
|
|
|
|
|
|
|
|
assert.IsType(t, p.provider, new(dnsProviderPublic))
|
2018-10-12 17:29:18 +00:00
|
|
|
})
|
|
|
|
}
|
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
|
2022-01-17 09:05:59 +00:00
|
|
|
privateZone bool
|
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
|
|
|
|
}{
|
|
|
|
{
|
2022-01-17 09:05:59 +00:00
|
|
|
desc: "success (public)",
|
2018-10-12 17:29:18 +00:00
|
|
|
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",
|
2022-01-17 09:05:59 +00:00
|
|
|
privateZone: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "success (private)",
|
|
|
|
clientID: "A",
|
|
|
|
clientSecret: "B",
|
|
|
|
tenantID: "C",
|
|
|
|
subscriptionID: "D",
|
|
|
|
resourceGroup: "E",
|
|
|
|
privateZone: true,
|
2018-10-12 17:29:18 +00:00
|
|
|
},
|
|
|
|
{
|
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)
|
2021-11-03 19:39:12 +00:00
|
|
|
return
|
2018-10-23 08:03:31 +00:00
|
|
|
}
|
|
|
|
},
|
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
|
2022-01-17 09:05:59 +00:00
|
|
|
config.PrivateZone = test.privateZone
|
2016-11-02 14:33:57 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
|
|
|
|
2018-10-23 08:03:31 +00:00
|
|
|
if test.handler == nil {
|
2021-11-01 23:52:38 +00:00
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
|
2018-10-23 08:03:31 +00:00
|
|
|
} else {
|
2021-11-01 23:52:38 +00:00
|
|
|
mux.HandleFunc("/", test.handler)
|
2018-10-23 08:03:31 +00:00
|
|
|
}
|
|
|
|
config.MetadataEndpoint = server.URL
|
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
p, err := NewDNSProviderConfig(config)
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2022-01-17 09:05:59 +00:00
|
|
|
if test.expected != "" {
|
2018-10-12 17:29:18 +00:00
|
|
|
require.EqualError(t, err, test.expected)
|
2022-01-17 09:05:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, p)
|
|
|
|
require.NotNil(t, p.provider)
|
|
|
|
|
|
|
|
if test.privateZone {
|
|
|
|
assert.IsType(t, p.provider, new(dnsProviderPrivate))
|
|
|
|
} else {
|
|
|
|
assert.IsType(t, p.provider, new(dnsProviderPublic))
|
2018-10-12 17:29:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
}
|