|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package auth |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/Azure/go-autorest/autorest/adal" |
| 23 | + "github.com/Azure/go-autorest/autorest/azure" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestGetServicePrincipalTokenFromMSIWithUserAssignedID(t *testing.T) { |
| 28 | + configs := []*AzureAuthConfig{ |
| 29 | + { |
| 30 | + UseManagedIdentityExtension: true, |
| 31 | + UserAssignedIdentityID: "UserAssignedIdentityID", |
| 32 | + }, |
| 33 | + // The Azure service principal is ignored when |
| 34 | + // UseManagedIdentityExtension is set to true |
| 35 | + { |
| 36 | + UseManagedIdentityExtension: true, |
| 37 | + UserAssignedIdentityID: "UserAssignedIdentityID", |
| 38 | + TenantID: "TenantID", |
| 39 | + AADClientID: "AADClientID", |
| 40 | + AADClientSecret: "AADClientSecret", |
| 41 | + }, |
| 42 | + } |
| 43 | + env := &azure.PublicCloud |
| 44 | + |
| 45 | + for _, config := range configs { |
| 46 | + token, err := GetServicePrincipalToken(config, env) |
| 47 | + assert.NoError(t, err) |
| 48 | + |
| 49 | + msiEndpoint, err := adal.GetMSIVMEndpoint() |
| 50 | + assert.NoError(t, err) |
| 51 | + |
| 52 | + spt, err := adal.NewServicePrincipalTokenFromMSIWithUserAssignedID(msiEndpoint, |
| 53 | + env.ServiceManagementEndpoint, config.UserAssignedIdentityID) |
| 54 | + assert.NoError(t, err) |
| 55 | + assert.Equal(t, token, spt) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestGetServicePrincipalTokenFromMSI(t *testing.T) { |
| 60 | + configs := []*AzureAuthConfig{ |
| 61 | + { |
| 62 | + UseManagedIdentityExtension: true, |
| 63 | + }, |
| 64 | + // The Azure service principal is ignored when |
| 65 | + // UseManagedIdentityExtension is set to true |
| 66 | + { |
| 67 | + UseManagedIdentityExtension: true, |
| 68 | + TenantID: "TenantID", |
| 69 | + AADClientID: "AADClientID", |
| 70 | + AADClientSecret: "AADClientSecret", |
| 71 | + }, |
| 72 | + } |
| 73 | + env := &azure.PublicCloud |
| 74 | + |
| 75 | + for _, config := range configs { |
| 76 | + token, err := GetServicePrincipalToken(config, env) |
| 77 | + assert.NoError(t, err) |
| 78 | + |
| 79 | + msiEndpoint, err := adal.GetMSIVMEndpoint() |
| 80 | + assert.NoError(t, err) |
| 81 | + |
| 82 | + spt, err := adal.NewServicePrincipalTokenFromMSI(msiEndpoint, env.ServiceManagementEndpoint) |
| 83 | + assert.NoError(t, err) |
| 84 | + assert.Equal(t, token, spt) |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +func TestGetServicePrincipalToken(t *testing.T) { |
| 90 | + config := &AzureAuthConfig{ |
| 91 | + TenantID: "TenantID", |
| 92 | + AADClientID: "AADClientID", |
| 93 | + AADClientSecret: "AADClientSecret", |
| 94 | + } |
| 95 | + env := &azure.PublicCloud |
| 96 | + |
| 97 | + token, err := GetServicePrincipalToken(config, env) |
| 98 | + assert.NoError(t, err) |
| 99 | + |
| 100 | + oauthConfig, err := adal.NewOAuthConfigWithAPIVersion(env.ActiveDirectoryEndpoint, config.TenantID, nil) |
| 101 | + assert.NoError(t, err) |
| 102 | + |
| 103 | + spt, err := adal.NewServicePrincipalToken(*oauthConfig, config.AADClientID, config.AADClientSecret, env.ServiceManagementEndpoint) |
| 104 | + assert.NoError(t, err) |
| 105 | + |
| 106 | + assert.Equal(t, token, spt) |
| 107 | +} |
| 108 | + |
| 109 | +func TestParseAzureEngironment(t *testing.T) { |
| 110 | + cases := []struct { |
| 111 | + cloudName string |
| 112 | + resourceManagerEndpoint string |
| 113 | + identitySystem string |
| 114 | + expected *azure.Environment |
| 115 | + }{ |
| 116 | + { |
| 117 | + cloudName: "", |
| 118 | + resourceManagerEndpoint: "", |
| 119 | + identitySystem: "", |
| 120 | + expected: &azure.PublicCloud, |
| 121 | + }, |
| 122 | + { |
| 123 | + cloudName: "AZURECHINACLOUD", |
| 124 | + resourceManagerEndpoint: "", |
| 125 | + identitySystem: "", |
| 126 | + expected: &azure.ChinaCloud, |
| 127 | + }, |
| 128 | + } |
| 129 | + |
| 130 | + for _, c := range cases { |
| 131 | + env, err := ParseAzureEnvironment(c.cloudName, c.resourceManagerEndpoint, c.identitySystem) |
| 132 | + assert.NoError(t, err) |
| 133 | + assert.Equal(t, env, c.expected) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestAzureStackOverrides(t *testing.T) { |
| 138 | + env := &azure.PublicCloud |
| 139 | + resourceManagerEndpoint := "https://management.test.com/" |
| 140 | + |
| 141 | + azureStackOverrides(env, resourceManagerEndpoint, "") |
| 142 | + assert.Equal(t, env.ManagementPortalURL, "https://portal.test.com/") |
| 143 | + assert.Equal(t, env.ServiceManagementEndpoint, env.TokenAudience) |
| 144 | + assert.Equal(t, env.ResourceManagerVMDNSSuffix, "cloudapp.test.com") |
| 145 | + assert.Equal(t, env.ActiveDirectoryEndpoint, "https://login.microsoftonline.com/") |
| 146 | + |
| 147 | + azureStackOverrides(env, resourceManagerEndpoint, "adfs") |
| 148 | + assert.Equal(t, env.ManagementPortalURL, "https://portal.test.com/") |
| 149 | + assert.Equal(t, env.ServiceManagementEndpoint, env.TokenAudience) |
| 150 | + assert.Equal(t, env.ResourceManagerVMDNSSuffix, "cloudapp.test.com") |
| 151 | + assert.Equal(t, env.ActiveDirectoryEndpoint, "https://login.microsoftonline.com") |
| 152 | +} |
0 commit comments