-
Notifications
You must be signed in to change notification settings - Fork 16
/
azure_test.go
295 lines (258 loc) · 8.02 KB
/
azure_test.go
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azureauth
import (
"context"
"encoding/base64"
"errors"
"fmt"
"regexp"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v4"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/msi/armmsi"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/hashicorp/vault-plugin-auth-azure/client"
)
// mockKeySet is used in tests to bypass signature validation and return only
// the jwt payload
type mockKeySet struct{}
func (s *mockKeySet) VerifySignature(_ context.Context, idToken string) ([]byte, error) {
parts := strings.Split(idToken, ".")
if len(parts) != 3 {
return nil, errors.New("invalid jwt")
}
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("error decoding payload: %v", err)
}
return payload, nil
}
func newMockVerifier() client.TokenVerifier {
config := &oidc.Config{
SkipClientIDCheck: true,
SkipExpiryCheck: false,
}
ks := new(mockKeySet)
return oidc.NewVerifier("", ks, config)
}
type mockComputeClient struct {
computeClientFunc computeClientFunc
}
type mockVMSSClient struct {
vmssClientFunc vmssClientFunc
}
type mockMSIClient struct {
msiClientFunc msiClientFunc
msiListFunc msiListFunc
}
type mockResourceClient struct {
resourceClientFunc resourceClientFunc
}
type mockProvidersClient struct {
providersClientFunc providersClientFunc
}
func (c *mockComputeClient) Get(ctx context.Context, _, vmName string, _ *armcompute.VirtualMachinesClientGetOptions) (armcompute.VirtualMachinesClientGetResponse, error) {
if c.computeClientFunc != nil {
return c.computeClientFunc(vmName)
}
return armcompute.VirtualMachinesClientGetResponse{}, nil
}
func (c *mockVMSSClient) Get(ctx context.Context, _, vmssName string, _ *armcompute.VirtualMachineScaleSetsClientGetOptions) (armcompute.VirtualMachineScaleSetsClientGetResponse, error) {
if c.vmssClientFunc != nil {
return c.vmssClientFunc(vmssName)
}
return armcompute.VirtualMachineScaleSetsClientGetResponse{}, nil
}
func (c *mockMSIClient) Get(ctx context.Context, _, resourceName string, _ *armmsi.UserAssignedIdentitiesClientGetOptions) (armmsi.UserAssignedIdentitiesClientGetResponse, error) {
if c.msiClientFunc != nil {
return c.msiClientFunc(resourceName)
}
return armmsi.UserAssignedIdentitiesClientGetResponse{}, nil
}
func (c *mockMSIClient) NewListByResourceGroupPager(resourceGroup string, _ *armmsi.UserAssignedIdentitiesClientListByResourceGroupOptions) *runtime.Pager[armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse] {
if c.msiListFunc != nil {
resp := c.msiListFunc(resourceGroup)
// the listfunc returns the response, here we wrap it in a pager, so that the mock-er only has to worry about the response we want.
return runtime.NewPager[armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse](runtime.PagingHandler[armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse]{
// since we only have one response, there are no more responses.
More: func(response armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse) bool { return false },
Fetcher: func(ctx context.Context, data *armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse) (armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse, error) {
return resp, nil
},
})
}
return nil
}
func (c *mockResourceClient) GetByID(ctx context.Context, resourceID, _ string, _ *armresources.ClientGetByIDOptions) (armresources.ClientGetByIDResponse, error) {
if c.resourceClientFunc != nil {
return c.resourceClientFunc(resourceID)
}
return armresources.ClientGetByIDResponse{}, nil
}
func (c *mockProvidersClient) Get(ctx context.Context, resourceID string, _ *armresources.ProvidersClientGetOptions) (armresources.ProvidersClientGetResponse, error) {
if c.providersClientFunc != nil {
return c.providersClientFunc(resourceID)
}
return armresources.ProvidersClientGetResponse{}, nil
}
type computeClientFunc func(vmName string) (armcompute.VirtualMachinesClientGetResponse, error)
type vmssClientFunc func(vmssName string) (armcompute.VirtualMachineScaleSetsClientGetResponse, error)
type msiClientFunc func(resourceName string) (armmsi.UserAssignedIdentitiesClientGetResponse, error)
type msiListFunc func(resoucename string) armmsi.UserAssignedIdentitiesClientListByResourceGroupResponse
type msGraphClientFunc func() (client.MSGraphClient, error)
type resourceClientFunc func(resourceID string) (armresources.ClientGetByIDResponse, error)
type providersClientFunc func(s string) (armresources.ProvidersClientGetResponse, error)
type mockProvider struct {
computeClientFunc
vmssClientFunc
msiClientFunc
msiListFunc
msGraphClientFunc
resourceClientFunc
providersClientFunc
}
func newMockProvider(c computeClientFunc, v vmssClientFunc, m msiClientFunc, ml msiListFunc, g msGraphClientFunc) *mockProvider {
return &mockProvider{
computeClientFunc: c,
vmssClientFunc: v,
msiClientFunc: m,
msiListFunc: ml,
msGraphClientFunc: g,
}
}
func (*mockProvider) TokenVerifier() client.TokenVerifier {
return newMockVerifier()
}
func (p *mockProvider) ComputeClient(subscriptionID string) (client.ComputeClient, error) {
return &mockComputeClient{
computeClientFunc: p.computeClientFunc,
}, nil
}
func (p *mockProvider) VMSSClient(subscriptionID string) (client.VMSSClient, error) {
return &mockVMSSClient{
vmssClientFunc: p.vmssClientFunc,
}, nil
}
func (p *mockProvider) MSIClient(subscriptionID string) (client.MSIClient, error) {
return &mockMSIClient{
msiClientFunc: p.msiClientFunc,
msiListFunc: p.msiListFunc,
}, nil
}
func (p *mockProvider) MSGraphClient() (client.MSGraphClient, error) {
return nil, nil
}
func (p *mockProvider) ResourceClient(subscriptionID string) (client.ResourceClient, error) {
return &mockResourceClient{
resourceClientFunc: p.resourceClientFunc,
}, nil
}
func (p *mockProvider) ProvidersClient(subscriptionID string) (client.ProvidersClient, error) {
return &mockProvidersClient{
providersClientFunc: p.providersClientFunc,
}, nil
}
func TestValidationRegex(t *testing.T) {
cases := []struct {
name string
in string
regex *regexp.Regexp
isMatch bool
}{
{
name: "normal subscriptionID",
in: "1234abcd-1234-1234-defa-5678fedc90ba",
regex: guidRx,
isMatch: true,
},
{
name: "bad subscriptionID",
in: "xyzg..",
regex: guidRx,
isMatch: false,
},
{
name: "short name",
in: "a",
regex: nameRx,
isMatch: true,
},
{
name: "tricky name",
in: "real/../../secret/top-secret",
regex: nameRx,
isMatch: false,
},
{
name: "tricky but valid name",
in: "real.._..secret_top-secret",
regex: nameRx,
isMatch: true,
},
{
name: "valid name",
in: "this-name-is-good-14",
regex: nameRx,
isMatch: true,
},
{
name: "valid with underscore",
in: "this_name_is_good_15",
regex: nameRx,
isMatch: true,
},
{
name: "invalid start",
in: "15abc",
regex: nameRx,
isMatch: false,
},
{
name: "invalid end, period",
in: "a.",
regex: nameRx,
isMatch: false,
},
{
name: "invalid end, hyphen",
in: "a-",
regex: nameRx,
isMatch: false,
},
{
name: "tricky resource group",
in: "real/../../secret/top-secret",
regex: rgRx,
isMatch: false,
},
{
name: "non-ascii resource group",
in: "сыноо",
regex: rgRx,
isMatch: true,
},
{
name: "paren resource group",
in: ".(сыноо)",
regex: rgRx,
isMatch: true,
},
{
name: "resource group, invalid end with period",
in: ".(сыноо-_).",
regex: rgRx,
isMatch: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := validateAzureField(tc.regex, tc.in)
if tc.isMatch != out {
t.Fail()
}
})
}
}