2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-07 10:27:55 -04:00
|
|
|
package azureshared
|
2022-03-28 06:24:41 -04:00
|
|
|
|
|
|
|
import (
|
2022-06-07 10:27:55 -04:00
|
|
|
"net/url"
|
2022-03-28 06:24:41 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-03-28 06:24:41 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-06-07 10:27:55 -04:00
|
|
|
func TestApplicationCredentialsFromURI(t *testing.T) {
|
|
|
|
creds := ApplicationCredentials{
|
2023-04-03 09:01:25 -04:00
|
|
|
TenantID: "tenant-id",
|
|
|
|
AppClientID: "client-id",
|
|
|
|
ClientSecretValue: "client-secret",
|
|
|
|
Location: "location",
|
|
|
|
UamiResourceID: "subscriptions/9b352db0-82af-408c-a02c-36fbffbf7015/resourceGroups/resourceGroupName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/UAMIName",
|
|
|
|
PreferredAuthMethod: AuthMethodServicePrincipal,
|
|
|
|
}
|
|
|
|
credsWithoutSecret := ApplicationCredentials{
|
|
|
|
TenantID: "tenant-id",
|
|
|
|
Location: "location",
|
|
|
|
UamiResourceID: "subscriptions/9b352db0-82af-408c-a02c-36fbffbf7015/resourceGroups/resourceGroupName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/UAMIName",
|
|
|
|
PreferredAuthMethod: AuthMethodUserAssignedIdentity,
|
|
|
|
}
|
|
|
|
credsWithoutPreferrredAuthMethod := ApplicationCredentials{
|
2022-08-29 08:18:05 -04:00
|
|
|
TenantID: "tenant-id",
|
|
|
|
AppClientID: "client-id",
|
|
|
|
ClientSecretValue: "client-secret",
|
|
|
|
Location: "location",
|
2022-03-28 06:24:41 -04:00
|
|
|
}
|
|
|
|
testCases := map[string]struct {
|
|
|
|
cloudServiceAccountURI string
|
2022-06-07 10:27:55 -04:00
|
|
|
wantCreds ApplicationCredentials
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-28 06:24:41 -04:00
|
|
|
}{
|
|
|
|
"getApplicationCredentials works": {
|
2023-04-03 09:01:25 -04:00
|
|
|
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location&preferred_auth_method=serviceprincipal&uami_resource_id=subscriptions%2F9b352db0-82af-408c-a02c-36fbffbf7015%2FresourceGroups%2FresourceGroupName%2Fproviders%2FMicrosoft.ManagedIdentity%2FuserAssignedIdentities%2FUAMIName",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantCreds: creds,
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
2023-04-03 09:01:25 -04:00
|
|
|
"can parse URI without app registration / secret": {
|
|
|
|
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&location=location&preferred_auth_method=userassignedidentity&uami_resource_id=subscriptions%2F9b352db0-82af-408c-a02c-36fbffbf7015%2FresourceGroups%2FresourceGroupName%2Fproviders%2FMicrosoft.ManagedIdentity%2FuserAssignedIdentities%2FUAMIName",
|
|
|
|
wantCreds: credsWithoutSecret,
|
|
|
|
},
|
|
|
|
"can parse URI without preferred auth method": {
|
|
|
|
cloudServiceAccountURI: "serviceaccount://azure?tenant_id=tenant-id&client_id=client-id&client_secret=client-secret&location=location",
|
|
|
|
wantCreds: credsWithoutPreferrredAuthMethod,
|
|
|
|
},
|
2022-03-28 06:24:41 -04:00
|
|
|
"invalid URI fails": {
|
|
|
|
cloudServiceAccountURI: "\x00",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
|
|
|
"incorrect URI scheme fails": {
|
|
|
|
cloudServiceAccountURI: "invalid",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
|
|
|
"incorrect URI host fails": {
|
|
|
|
cloudServiceAccountURI: "serviceaccount://incorrect",
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-28 06:24:41 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-06-07 10:27:55 -04:00
|
|
|
creds, err := ApplicationCredentialsFromURI(tc.cloudServiceAccountURI)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-28 06:24:41 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantCreds, creds)
|
2022-03-28 06:24:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 10:27:55 -04:00
|
|
|
|
|
|
|
func TestToCloudServiceAccountURI(t *testing.T) {
|
2023-04-03 09:01:25 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
credentials ApplicationCredentials
|
|
|
|
wantURLValues url.Values
|
|
|
|
}{
|
|
|
|
"client id and secret without preferred auth method": {
|
|
|
|
credentials: ApplicationCredentials{
|
|
|
|
TenantID: "tenant-id",
|
|
|
|
AppClientID: "client-id",
|
|
|
|
ClientSecretValue: "client-secret",
|
|
|
|
Location: "location",
|
|
|
|
},
|
|
|
|
wantURLValues: url.Values{
|
|
|
|
"tenant_id": []string{"tenant-id"},
|
|
|
|
"client_id": []string{"client-id"},
|
|
|
|
"client_secret": []string{"client-secret"},
|
|
|
|
"location": []string{"location"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"client id and secret with preferred auth method": {
|
|
|
|
credentials: ApplicationCredentials{
|
|
|
|
TenantID: "tenant-id",
|
|
|
|
AppClientID: "client-id",
|
|
|
|
ClientSecretValue: "client-secret",
|
|
|
|
Location: "location",
|
|
|
|
PreferredAuthMethod: AuthMethodServicePrincipal,
|
|
|
|
},
|
|
|
|
wantURLValues: url.Values{
|
|
|
|
"tenant_id": []string{"tenant-id"},
|
|
|
|
"client_id": []string{"client-id"},
|
|
|
|
"client_secret": []string{"client-secret"},
|
|
|
|
"location": []string{"location"},
|
|
|
|
"preferred_auth_method": []string{"ServicePrincipal"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"only preferred auth method": {
|
|
|
|
credentials: ApplicationCredentials{
|
|
|
|
TenantID: "tenant-id",
|
|
|
|
Location: "location",
|
|
|
|
PreferredAuthMethod: AuthMethodUserAssignedIdentity,
|
|
|
|
},
|
|
|
|
wantURLValues: url.Values{
|
|
|
|
"tenant_id": []string{"tenant-id"},
|
|
|
|
"location": []string{"location"},
|
|
|
|
"preferred_auth_method": []string{"UserAssignedIdentity"},
|
|
|
|
},
|
|
|
|
},
|
2022-06-07 10:27:55 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:01:25 -04:00
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
cloudServiceAccountURI := tc.credentials.ToCloudServiceAccountURI()
|
|
|
|
uri, err := url.Parse(cloudServiceAccountURI)
|
|
|
|
require.NoError(err)
|
|
|
|
query := uri.Query()
|
|
|
|
assert.Equal("serviceaccount", uri.Scheme)
|
|
|
|
assert.Equal("azure", uri.Host)
|
|
|
|
assert.Equal(tc.wantURLValues, query)
|
|
|
|
})
|
|
|
|
}
|
2022-06-07 10:27:55 -04:00
|
|
|
}
|