-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathauth_test.go
More file actions
255 lines (241 loc) · 8.81 KB
/
auth_test.go
File metadata and controls
255 lines (241 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package bootstrap
import (
"context"
"encoding/base64"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/cloud"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/log"
"github.com/Azure/aks-secure-tls-bootstrap/client/internal/testutil"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
"github.com/Azure/go-autorest/autorest/azure"
)
func TestGetToken(t *testing.T) {
cases := []struct {
name string
customClientID string
setCloudProviderConfig func(t *testing.T, config *cloud.ProviderConfig)
// getTokenCredentialFunc is an override for cases that need to
// control GetToken behavior. When nil, the default getTokenCredentialFunc is used (which
// exercises real credential-creation logic, including any error paths).
getTokenCredentialFunc getTokenCredentialFunc
expectedToken string
expectedErr error
}{
{
name: "error getting azure environment config for specified cloud",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.CloudName = "invalid"
config.ClientID = "service-principal-id"
config.ClientSecret = "secret"
},
expectedToken: "",
expectedErr: errors.New(`getting azure environment config for cloud "invalid"`),
},
{
name: "error generating a service principal access token with client secret due to missing client secret",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = ""
},
expectedToken: "",
expectedErr: errors.New("generating service principal access token with client secret"),
},
{
name: "error b64-decoding client secret certificate data",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = "certificate:YW55IGNhcm5hbCBwbGVhc3U======" // invalid b64-encoding
},
expectedToken: "",
expectedErr: errors.New("b64-decoding certificate data in client secret"),
},
{
name: "error pfx-decoding client secret certificate data",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = "certificate:dGVzdAo=" // b64-encoding of "test"
},
expectedToken: "",
expectedErr: errors.New("decoding pfx certificate data in client secret"),
},
{
name: "error returned from GetToken",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
certData, err := testutil.GenerateCertAndKeyAsEncodedPFXData(testutil.CertTemplate{
CommonName: "aad",
Organization: "azure",
Expiration: time.Now().Add(time.Hour),
})
assert.NoError(t, err)
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = "certificate:" + certData
},
getTokenCredentialFunc: func(_ context.Context, _ *Config) (azcore.TokenCredential, error) {
fakeCred := &fake.TokenCredential{}
fakeCred.SetError(errors.New("generating service principal access token with certificate"))
return fakeCred, nil
},
expectedToken: "",
expectedErr: errors.New("generating service principal access token with certificate"),
},
{
name: "UserAssignedIdentityID is specified in cloud provider config",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.UserAssignedIdentityID = "kubelet-identity-id"
config.ClientID = clientIDForMSI
},
getTokenCredentialFunc: func(_ context.Context, _ *Config) (azcore.TokenCredential, error) {
return &fake.TokenCredential{}, nil
},
expectedToken: "fake_token",
expectedErr: nil,
},
{
name: "UserAssignedIdentityID is not specified in cloud provider config, but client ID indicates MSI usage",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.UserAssignedIdentityID = ""
config.ClientID = clientIDForMSI
},
expectedToken: "",
expectedErr: errors.New("client ID within cloud provider config indicates usage of a managed identity, though no user-assigned identity ID was provided"),
},
{
name: "a custom client ID is specified",
customClientID: "custom",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.UserAssignedIdentityID = "kubelet-identity-id"
config.ClientID = clientIDForMSI
},
getTokenCredentialFunc: func(_ context.Context, _ *Config) (azcore.TokenCredential, error) {
return &fake.TokenCredential{}, nil
},
expectedToken: "fake_token",
expectedErr: nil,
},
{
name: "service principal client secret does not contain certificate data",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = "secret"
},
getTokenCredentialFunc: func(_ context.Context, _ *Config) (azcore.TokenCredential, error) {
return &fake.TokenCredential{}, nil
},
expectedToken: "fake_token",
expectedErr: nil,
},
{
name: "service principal client secret contains certificate data",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
certData, err := testutil.GenerateCertAndKeyAsEncodedPFXData(testutil.CertTemplate{
CommonName: "aad",
Organization: "azure",
Expiration: time.Now().Add(time.Hour),
})
assert.NoError(t, err)
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = "certificate:" + certData
},
getTokenCredentialFunc: func(_ context.Context, _ *Config) (azcore.TokenCredential, error) {
return &fake.TokenCredential{}, nil
},
expectedToken: "fake_token",
expectedErr: nil,
},
{
name: "service principal client secret is b64-decoded and contains certificate data",
setCloudProviderConfig: func(t *testing.T, config *cloud.ProviderConfig) {
certData, err := testutil.GenerateCertAndKeyAsEncodedPFXData(testutil.CertTemplate{
CommonName: "aad",
Organization: "azure",
Expiration: time.Now().Add(time.Hour),
})
assert.NoError(t, err)
config.CloudName = azure.PublicCloud.Name
config.ClientID = "service-principal-id"
config.ClientSecret = base64.StdEncoding.EncodeToString([]byte("certificate:" + certData))
},
getTokenCredentialFunc: func(_ context.Context, _ *Config) (azcore.TokenCredential, error) {
return &fake.TokenCredential{}, nil
},
expectedToken: "fake_token",
expectedErr: nil,
},
}
testTenantID := "d87a2c3e-0c0c-42b2-a883-e48cd8723e22"
testResource := "resource"
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := log.NewTestContext()
client := &client{
getTokenCredentialFunc: c.getTokenCredentialFunc,
}
if client.getTokenCredentialFunc == nil {
client.getTokenCredentialFunc = getTokenCredential
}
cloudProviderConfig := &cloud.ProviderConfig{
TenantID: testTenantID,
}
c.setCloudProviderConfig(t, cloudProviderConfig)
token, err := client.getToken(ctx, &Config{
AADResource: testResource,
CloudProviderConfig: cloudProviderConfig,
UserAssignedIdentityID: c.customClientID,
})
if c.expectedErr != nil {
assert.Error(t, err)
assert.ErrorContains(t, err, c.expectedErr.Error())
assert.Empty(t, token)
} else {
assert.NoError(t, err)
assert.Equal(t, c.expectedToken, token)
}
})
}
}
func TestAADResourceToScope(t *testing.T) {
cases := []struct {
name string
resource string
expectedScope string
}{
{
name: "resource without trailing slash gets /.default appended",
resource: "https://management.azure.com",
expectedScope: "https://management.azure.com/.default",
},
{
name: "resource with trailing slash gets /.default appended (slash removed first)",
resource: "https://management.azure.com/",
expectedScope: "https://management.azure.com/.default",
},
{
name: "resource already ending in /.default is unchanged",
resource: "https://management.azure.com/.default",
expectedScope: "https://management.azure.com/.default",
},
{
name: "simple resource string",
resource: "resource",
expectedScope: "resource/.default",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
scope := aadResourceToScope(c.resource)
assert.Equal(t, c.expectedScope, scope)
})
}
}