-
Notifications
You must be signed in to change notification settings - Fork 37
/
awsauth_test.go
455 lines (417 loc) · 16.8 KB
/
awsauth_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package awsbase
import (
"net/http"
"testing"
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/hashicorp/aws-sdk-go-base/v2/internal/test"
"github.com/hashicorp/aws-sdk-go-base/v2/mockdata"
"github.com/hashicorp/aws-sdk-go-base/v2/servicemocks"
)
func TestGetAccountIDAndPartition(t *testing.T) {
var testCases = []struct {
Description string
AuthProviderName string
EC2MetadataEndpoints []*servicemocks.MetadataResponse
IAMEndpoints []*servicemocks.MockEndpoint
STSEndpoints []*servicemocks.MockEndpoint
ErrCount int
ExpectedAccountID string
ExpectedPartition string
}{
{
Description: "EC2 Metadata over iam:GetUser when using EC2 Instance Profile",
AuthProviderName: ec2rolecreds.ProviderName,
EC2MetadataEndpoints: append(servicemocks.Ec2metadata_securityCredentialsEndpoints, servicemocks.Ec2metadata_instanceIdEndpoint, servicemocks.Ec2metadata_iamInfoEndpoint),
IAMEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusOK, Body: servicemocks.IamResponse_GetUser_valid, ContentType: "text/xml"},
},
},
ExpectedAccountID: servicemocks.Ec2metadata_iamInfoEndpoint_expectedAccountID,
ExpectedPartition: servicemocks.Ec2metadata_iamInfoEndpoint_expectedPartition,
},
{
Description: "Mimic the metadata service mocked by Hologram (https://github.com/AdRoll/hologram)",
AuthProviderName: ec2rolecreds.ProviderName,
EC2MetadataEndpoints: servicemocks.Ec2metadata_securityCredentialsEndpoints,
IAMEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusForbidden, Body: servicemocks.IamResponse_GetUser_unauthorized, ContentType: "text/xml"},
},
},
STSEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
ExpectedAccountID: servicemocks.MockStsGetCallerIdentityAccountID,
ExpectedPartition: servicemocks.MockStsGetCallerIdentityPartition,
},
{
Description: "iam:ListRoles if iam:GetUser AccessDenied and sts:GetCallerIdentity fails",
IAMEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusForbidden, Body: servicemocks.IamResponse_GetUser_unauthorized, ContentType: "text/xml"},
},
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=ListRoles&MaxItems=1&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusOK, Body: servicemocks.IamResponse_ListRoles_valid, ContentType: "text/xml"},
},
},
STSEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidEndpointAccessDenied,
},
ExpectedAccountID: servicemocks.IamResponse_ListRoles_valid_expectedAccountID,
ExpectedPartition: servicemocks.IamResponse_ListRoles_valid_expectedPartition,
},
{
Description: "iam:ListRoles if iam:GetUser ValidationError and sts:GetCallerIdentity fails",
IAMEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusBadRequest, Body: servicemocks.IamResponse_GetUser_federatedFailure, ContentType: "text/xml"},
},
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=ListRoles&MaxItems=1&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusOK, Body: servicemocks.IamResponse_ListRoles_valid, ContentType: "text/xml"},
},
},
STSEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidEndpointAccessDenied,
},
ExpectedAccountID: servicemocks.IamResponse_ListRoles_valid_expectedAccountID,
ExpectedPartition: servicemocks.IamResponse_ListRoles_valid_expectedPartition,
},
{
Description: "Error when all endpoints fail",
IAMEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusBadRequest, Body: servicemocks.IamResponse_GetUser_federatedFailure, ContentType: "text/xml"},
},
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=ListRoles&MaxItems=1&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusForbidden, Body: servicemocks.IamResponse_ListRoles_unauthorized, ContentType: "text/xml"},
},
},
STSEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidEndpointAccessDenied,
},
ErrCount: 1,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Description, func(t *testing.T) {
ctx := test.Context(t)
resetEnv := servicemocks.UnsetEnv(t)
defer resetEnv()
// capture the test server's close method, to call after the test returns
awsTs := servicemocks.AwsMetadataApiMock(testCase.EC2MetadataEndpoints)
defer awsTs()
closeIam, iamConfig, _ := mockdata.GetMockedAwsApiSession("IAM", testCase.IAMEndpoints)
defer closeIam()
closeSts, stsConfig, _ := mockdata.GetMockedAwsApiSession("STS", testCase.STSEndpoints)
defer closeSts()
iamConn := iam.NewFromConfig(iamConfig)
stsConn := sts.NewFromConfig(stsConfig)
accountID, partition, err := getAccountIDAndPartition(ctx, iamConn, stsConn, testCase.AuthProviderName)
if err != nil && testCase.ErrCount == 0 {
t.Fatalf("Expected no error, received error: %s", err)
}
if err == nil && testCase.ErrCount > 0 {
t.Fatalf("Expected %d error(s), received none", testCase.ErrCount)
}
if accountID != testCase.ExpectedAccountID {
t.Fatalf("Parsed account ID doesn't match with expected (%q != %q)", accountID, testCase.ExpectedAccountID)
}
if partition != testCase.ExpectedPartition {
t.Fatalf("Parsed partition doesn't match with expected (%q != %q)", partition, testCase.ExpectedPartition)
}
})
}
}
func TestGetAccountIDAndPartitionFromEC2Metadata(t *testing.T) {
t.Run("EC2 metadata success", func(t *testing.T) {
ctx := test.Context(t)
resetEnv := servicemocks.UnsetEnv(t)
defer resetEnv()
awsTs := servicemocks.AwsMetadataApiMock(append(
servicemocks.Ec2metadata_securityCredentialsEndpoints,
servicemocks.Ec2metadata_instanceIdEndpoint,
servicemocks.Ec2metadata_iamInfoEndpoint,
))
defer awsTs()
id, partition, err := getAccountIDAndPartitionFromEC2Metadata(ctx)
if err != nil {
t.Fatalf("Getting account ID from EC2 metadata API failed: %s", err)
}
if id != servicemocks.Ec2metadata_iamInfoEndpoint_expectedAccountID {
t.Fatalf("Expected account ID: %s, given: %s", servicemocks.Ec2metadata_iamInfoEndpoint_expectedAccountID, id)
}
if partition != servicemocks.Ec2metadata_iamInfoEndpoint_expectedPartition {
t.Fatalf("Expected partition: %s, given: %s", servicemocks.Ec2metadata_iamInfoEndpoint_expectedPartition, partition)
}
})
}
func TestGetAccountIDAndPartitionFromIAMGetUser(t *testing.T) {
var testCases = []struct {
Description string
MockEndpoints []*servicemocks.MockEndpoint
ErrCount int
ExpectedAccountID string
ExpectedPartition string
}{
{
Description: "Ignore iam:GetUser failure with federated user",
MockEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusBadRequest, Body: servicemocks.IamResponse_GetUser_federatedFailure, ContentType: "text/xml"},
},
},
ErrCount: 0,
},
{
Description: "Ignore iam:GetUser failure with unauthorized user",
MockEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusForbidden, Body: servicemocks.IamResponse_GetUser_unauthorized, ContentType: "text/xml"},
},
},
ErrCount: 0,
},
{
Description: "iam:GetUser success",
MockEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=GetUser&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusOK, Body: servicemocks.IamResponse_GetUser_valid, ContentType: "text/xml"},
},
},
ExpectedAccountID: servicemocks.IamResponse_GetUser_valid_expectedAccountID,
ExpectedPartition: servicemocks.IamResponse_GetUser_valid_expectedPartition,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Description, func(t *testing.T) {
ctx := test.Context(t)
closeIam, config, _ := mockdata.GetMockedAwsApiSession("IAM", testCase.MockEndpoints)
defer closeIam()
iamClient := iam.NewFromConfig(config)
accountID, partition, err := getAccountIDAndPartitionFromIAMGetUser(ctx, iamClient)
if err != nil && testCase.ErrCount == 0 {
t.Fatalf("Expected no error, received error: %s", err)
}
if err == nil && testCase.ErrCount > 0 {
t.Fatalf("Expected %d error(s), received none", testCase.ErrCount)
}
if accountID != testCase.ExpectedAccountID {
t.Fatalf("Parsed account ID doesn't match with expected (%q != %q)", accountID, testCase.ExpectedAccountID)
}
if partition != testCase.ExpectedPartition {
t.Fatalf("Parsed partition doesn't match with expected (%q != %q)", partition, testCase.ExpectedPartition)
}
})
}
}
func TestGetAccountIDAndPartitionFromIAMListRoles(t *testing.T) {
var testCases = []struct {
Description string
MockEndpoints []*servicemocks.MockEndpoint
ErrCount int
ExpectedAccountID string
ExpectedPartition string
}{
{
Description: "iam:ListRoles unauthorized",
MockEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=ListRoles&MaxItems=1&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusForbidden, Body: servicemocks.IamResponse_ListRoles_unauthorized, ContentType: "text/xml"},
},
},
ErrCount: 1,
},
{
Description: "iam:ListRoles success",
MockEndpoints: []*servicemocks.MockEndpoint{
{
Request: &servicemocks.MockRequest{Method: "POST", Uri: "/", Body: "Action=ListRoles&MaxItems=1&Version=2010-05-08"},
Response: &servicemocks.MockResponse{StatusCode: http.StatusOK, Body: servicemocks.IamResponse_ListRoles_valid, ContentType: "text/xml"},
},
},
ExpectedAccountID: servicemocks.IamResponse_ListRoles_valid_expectedAccountID,
ExpectedPartition: servicemocks.IamResponse_ListRoles_valid_expectedPartition,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Description, func(t *testing.T) {
ctx := test.Context(t)
closeIam, config, _ := mockdata.GetMockedAwsApiSession("IAM", testCase.MockEndpoints)
defer closeIam()
iamClient := iam.NewFromConfig(config)
accountID, partition, err := getAccountIDAndPartitionFromIAMListRoles(ctx, iamClient)
if err != nil && testCase.ErrCount == 0 {
t.Fatalf("Expected no error, received error: %s", err)
}
if err == nil && testCase.ErrCount > 0 {
t.Fatalf("Expected %d error(s), received none", testCase.ErrCount)
}
if accountID != testCase.ExpectedAccountID {
t.Fatalf("Parsed account ID doesn't match with expected (%q != %q)", accountID, testCase.ExpectedAccountID)
}
if partition != testCase.ExpectedPartition {
t.Fatalf("Parsed partition doesn't match with expected (%q != %q)", partition, testCase.ExpectedPartition)
}
})
}
}
func TestGetAccountIDAndPartitionFromSTSGetCallerIdentity(t *testing.T) {
var testCases = []struct {
Description string
MockEndpoints []*servicemocks.MockEndpoint
ErrCount int
ExpectedAccountID string
ExpectedPartition string
}{
{
Description: "sts:GetCallerIdentity unauthorized",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidEndpointAccessDenied,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity ExpiredToken with invalid JSON response",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidBodyExpiredToken,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity ExpiredToken with valid JSON response",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidBodyExpiredToken,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity ExpiredTokenException with invalid JSON response",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidBodyExpiredTokenException,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity ExpiredTokenException with valid JSON response",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidBodyExpiredTokenException,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity RequestExpired with invalid JSON response",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityInvalidBodyRequestExpired,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity RequestExpired with valid JSON response",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidBodyRequestExpired,
},
ErrCount: 1,
},
{
Description: "sts:GetCallerIdentity success",
MockEndpoints: []*servicemocks.MockEndpoint{
servicemocks.MockStsGetCallerIdentityValidEndpoint,
},
ExpectedAccountID: servicemocks.MockStsGetCallerIdentityAccountID,
ExpectedPartition: servicemocks.MockStsGetCallerIdentityPartition,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.Description, func(t *testing.T) {
ctx := test.Context(t)
closeSts, config, _ := mockdata.GetMockedAwsApiSession("STS", testCase.MockEndpoints)
defer closeSts()
stsClient := sts.NewFromConfig(config)
accountID, partition, err := getAccountIDAndPartitionFromSTSGetCallerIdentity(ctx, stsClient)
if err != nil && testCase.ErrCount == 0 {
t.Fatalf("Expected no error, received error: %s", err)
}
if err == nil && testCase.ErrCount > 0 {
t.Fatalf("Expected %d error(s), received none", testCase.ErrCount)
}
if accountID != testCase.ExpectedAccountID {
t.Fatalf("Parsed account ID doesn't match with expected (%q != %q)", accountID, testCase.ExpectedAccountID)
}
if partition != testCase.ExpectedPartition {
t.Fatalf("Parsed partition doesn't match with expected (%q != %q)", partition, testCase.ExpectedPartition)
}
})
}
}
func TestAWSParseAccountIDAndPartitionFromARN(t *testing.T) {
var testCases = []struct {
InputARN string
ErrCount int
ExpectedAccountID string
ExpectedPartition string
}{
{
InputARN: "invalid-arn",
ErrCount: 1,
},
{
InputARN: "arn:aws:iam::123456789012:instance-profile/name",
ExpectedAccountID: "123456789012",
ExpectedPartition: "aws",
},
{
InputARN: "arn:aws:iam::123456789012:user/name",
ExpectedAccountID: "123456789012",
ExpectedPartition: "aws",
},
{
InputARN: "arn:aws:sts::123456789012:assumed-role/name",
ExpectedAccountID: "123456789012",
ExpectedPartition: "aws",
},
{
InputARN: "arn:aws-us-gov:sts::123456789012:assumed-role/name",
ExpectedAccountID: "123456789012",
ExpectedPartition: "aws-us-gov",
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.InputARN, func(t *testing.T) {
accountID, partition, err := parseAccountIDAndPartitionFromARN(testCase.InputARN)
if err != nil && testCase.ErrCount == 0 {
t.Fatalf("Expected no error when parsing ARN, received error: %s", err)
}
if err == nil && testCase.ErrCount > 0 {
t.Fatalf("Expected %d error(s) when parsing ARN, received none", testCase.ErrCount)
}
if accountID != testCase.ExpectedAccountID {
t.Fatalf("Parsed account ID doesn't match with expected (%q != %q)", accountID, testCase.ExpectedAccountID)
}
if partition != testCase.ExpectedPartition {
t.Fatalf("Parsed partition doesn't match with expected (%q != %q)", partition, testCase.ExpectedPartition)
}
})
}
}