-
Notifications
You must be signed in to change notification settings - Fork 302
/
config_test.go
596 lines (490 loc) · 20.2 KB
/
config_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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// Copyright 2015-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package config
import (
"flag"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)
const (
clusterName = "defaultCluster"
region = "us-east-1"
credentialProviderCount = 2
awsToken = "session-token"
customProfileName = "customProfile"
customAwsAccessKey = "customAKID"
customAwsRegion = "us-west-1"
customAwsSecretKey = "customSKID"
defaultProfileName = "default"
defaultAwsAccessKey = "defaultAwsAccessKey"
defaultAwsRegion = "us-west-2"
defaultAwsSecretKey = "defaultAwsSecretKey"
envAwsAccessKey = "envAKID"
envAwsRegion = "eu-west-1"
envAwsSecretKey = "envSKID"
assumeRoleName = "assumeRoleWithCreds"
assumeRoleAccessKey = "assumeRoleAKID"
assumeRoleRegion = "us-east-2"
assumeRoleSecretKey = "assumeRoleSKID"
ec2InstanceRoleName = "ec2InstanceRole"
ec2InstanceRoleAccessKey = "ec2InstanceRoleAKID"
ec2InstanceRoleRegion = "ap-northeast-1"
ec2InstanceRoleSecretKey = "ec2InstanceRoleSKID"
)
//------------------------------------------------------------------------------
// ToAWSSession() --> REGION TESTS
//------------------------------------------------------------------------------
func TestRegionOrderOfResolutionECSConfig(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// set variable for test
ecsConfig.Region = region // takes precedence
ecsConfig.AWSProfile = customProfileName
os.Setenv("AWS_REGION", envAwsRegion)
os.Setenv("AWS_PROFILE", customProfileName)
os.Setenv("AWS_DEFAULT_PROFILE", assumeRoleName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, region)
}
func TestRegionOrderOfResolutionEnvVar(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// set variable for test
ecsConfig.AWSProfile = customProfileName
os.Setenv("AWS_REGION", envAwsRegion) // takes precedence
os.Setenv("AWS_PROFILE", customProfileName)
os.Setenv("AWS_DEFAULT_PROFILE", assumeRoleName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, envAwsRegion)
}
func TestRegionOrderOfResolutionDefaultEnvVar(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// set variable for test
ecsConfig.AWSProfile = customProfileName
os.Setenv("AWS_DEFAULT_REGION", envAwsRegion) // takes precedence
os.Setenv("AWS_PROFILE", customProfileName)
os.Setenv("AWS_DEFAULT_PROFILE", assumeRoleName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, envAwsRegion)
}
// 1a) Use AWS_REGION env variable
func TestRegionWhenUsingEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// set variable for test
os.Setenv("AWS_REGION", envAwsRegion)
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, envAwsRegion)
}
// 1b) Use AWS_DEFAULT_REGION env variable
func TestRegionWhenUsingDefaultEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// set variable for test
os.Setenv("AWS_DEFAULT_REGION", envAwsRegion)
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, envAwsRegion)
}
// 2) Use Region in ECS Config
func TestRegionWhenUsingECSConfigRegion(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// set variable for test
ecsConfig.Region = region
// invoke test and verify
testRegionInSession(t, ecsConfig, region)
}
// 3a) Use Region from profile in ECS Config
func TestRegionWhenUsingECSConfigProfile(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
ecsConfig.AWSProfile = customProfileName
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, customAwsRegion)
}
// 3b) Use Region from AWS_PROFILE
func TestRegionWhenUsingAWSProfileEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
os.Setenv("AWS_PROFILE", customProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, customAwsRegion)
}
// 3c) Use Region from AWS_DEFAULT_PROFILE
func TestRegionWhenUsingDefaultAWSProfileEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
os.Setenv("AWS_DEFAULT_PROFILE", defaultProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testRegionInSession(t, ecsConfig, defaultAwsRegion)
}
func TestRegionWhenNoneSpecified(t *testing.T) {
// defaults
os.Clearenv()
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
// NOTE: no region set
// invoke test and verify
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
_, err := ecsConfig.ToAWSSession(context)
assert.Error(t, err, "Expected error when region is not specified or resolved")
}
func testRegionInSession(t *testing.T, inputConfig *LocalConfig, expectedRegion string) {
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
awsSession, err := inputConfig.ToAWSSession(context)
if err != nil {
t.Fatal("Error generating a new session")
}
awsConfig := awsSession.Config
assert.Equal(t, expectedRegion, aws.StringValue(awsConfig.Region), "Expected region to match")
}
//-------------------------------END OF REGION TESTS----------------------------
// 1a) Use AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY env variables
func TestCredentialsWhenUsingEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.Region = region
// set variables for test
os.Setenv("AWS_ACCESS_KEY_ID", envAwsAccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", envAwsSecretKey)
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, envAwsAccessKey, envAwsSecretKey)
}
// 1b) Use AWS_ACCESS_KEY and AWS_SECRET_KEY env variables
func TestCredentialsWhenUsingDefaultEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.Region = region
// set variables for test
os.Setenv("AWS_ACCESS_KEY", envAwsAccessKey)
os.Setenv("AWS_SECRET_KEY", envAwsSecretKey)
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, envAwsAccessKey, envAwsSecretKey)
}
// 2) Use access and secrets keys from ECS Config
func TestCredentialsWhenUsingECSConfig(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.Region = region
// set variables for test
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
ecsConfig.AWSSessionToken = awsToken
// invoke test and verify
testCredentialsInSessionWithToken(t, ecsConfig, awsAccessKey, awsSecretKey, awsToken)
}
// 3a) Use credentials from profile in ECS Config
func TestCredentialsWhenUsingECSConfigProfile(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.AWSProfile = customProfileName
// set variables for test
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, customAwsAccessKey, customAwsSecretKey)
}
// 3b) Use credentials from AWS_PROFILE
func TestCredentialsWhenUsingAWSProfileEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
os.Setenv("AWS_PROFILE", customProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, customAwsAccessKey, customAwsSecretKey)
}
// 3c) Use Region from AWS_DEFAULT_PROFILE
func TestCredentialsWhenUsingDefaultAWSProfileEnvVariable(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
os.Setenv("AWS_DEFAULT_PROFILE", defaultProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, defaultAwsAccessKey, defaultAwsSecretKey)
}
// 3d) Use credentials from assume role profile
func TestCredentialsWhenUsingAssumeRoleAWSProfileFlag(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
flagSet.String(flags.AWSProfileFlag, customProfileName, "")
context := cli.NewContext(nil, flagSet, nil)
ecsConfig.AWSProfile = assumeRoleName
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
startingConfig := assumeRoleTestHelper()
// invoke test and verify
testCredentialsInSessionWithConfig(t, context, ecsConfig, startingConfig, assumeRoleAccessKey, assumeRoleSecretKey)
}
func TestCredentialsWhenUsingAssumeRoleEnvVar(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
os.Setenv("AWS_DEFAULT_PROFILE", assumeRoleName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
startingConfig := assumeRoleTestHelper()
// invoke test and verify
testCredentialsInSessionWithConfig(t, context, ecsConfig, startingConfig, assumeRoleAccessKey, assumeRoleSecretKey)
}
func assumeRoleTestHelper() *aws.Config {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
const respMsg = `
<AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
<AssumeRoleResult>
<AssumedRoleUser>
<Arn>arn:aws:sts::account_id:assumed-role/role/session_name</Arn>
<AssumedRoleId>AKID:session_name</AssumedRoleId>
</AssumedRoleUser>
<Credentials>
<AccessKeyId>` + assumeRoleAccessKey + `</AccessKeyId>
<SecretAccessKey>` + assumeRoleSecretKey + `</SecretAccessKey>
<SessionToken>SESSION_TOKEN</SessionToken>
<Expiration>%s</Expiration>
</Credentials>
</AssumeRoleResult>
<ResponseMetadata>
<RequestId>request-id</RequestId>
</ResponseMetadata>
</AssumeRoleResponse>
`
w.Write([]byte(fmt.Sprintf(respMsg, time.Now().Add(15*time.Minute).Format("2006-01-02T15:04:05Z"))))
}))
startingConfig := aws.Config{}
startingConfig.Endpoint = aws.String(server.URL)
startingConfig.DisableSSL = aws.Bool(true)
return &startingConfig
}
//4) Use credentials from EC2 Instance Role
func TestCredentialsWhenUsingEC2InstanceRole(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
os.Setenv("AWS_DEFAULT_PROFILE", ec2InstanceRoleName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
ec2Creds := `{
"Code": "Success",
"Type": "AWS-HMAC",
"AccessKeyId" : "` + ec2InstanceRoleAccessKey + `",
"SecretAccessKey" : "` + ec2InstanceRoleSecretKey + `",
"Token" : "token",
"Expiration" : "%s",
"LastUpdated" : "2009-11-23T0:00:00Z"
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/latest/meta-data/iam/security-credentials/" {
fmt.Fprintln(w, ec2InstanceRoleName)
} else if r.URL.Path == "/latest/meta-data/iam/security-credentials/"+ec2InstanceRoleName {
fmt.Fprintf(w, ec2Creds, "2014-12-16T01:51:37Z")
} else {
http.Error(w, "Not found", http.StatusNotFound)
}
}))
myCustomResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
return endpoints.ResolvedEndpoint{
URL: server.URL + "/latest",
SigningRegion: ec2InstanceRoleRegion,
}, nil
}
startingConfig := aws.Config{}
startingConfig.EndpointResolver = endpoints.ResolverFunc(myCustomResolver)
// invoke test and verify
testCredentialsInSessionWithConfig(t, context, ecsConfig, &startingConfig, ec2InstanceRoleAccessKey, ec2InstanceRoleSecretKey)
}
// Error if Session.Credentials are nil
func TestCredentialsWhenNoneSpecified(t *testing.T) {
// defaults
os.Clearenv()
os.Setenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI", "somevalue")
ecsConfig := NewLocalConfig(clusterName)
ecsConfig.Region = region
// NOTE: no credentials set
// invoke test and verify
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
awsSession, err := ecsConfig.ToAWSSession(context)
assert.NoError(t, err, "Unexpected error generating a new session")
awsConfig := awsSession.Config
_, err = awsConfig.Credentials.Get()
assert.Error(t, err, "Expected error getting credentials")
}
func TestCredentialOrderOfResolutionECSProfileFlag(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
os.Setenv("AWS_DEFAULT_PROFILE", defaultProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
os.Setenv("AWS_ACCESS_KEY_ID", envAwsAccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", envAwsSecretKey)
defer os.Clearenv()
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
flagSet.String(flags.ECSProfileFlag, "default", "")
context := cli.NewContext(nil, flagSet, nil)
// invoke test and verify
testCredentialsInSessionWithContext(t, context, ecsConfig, awsAccessKey, awsSecretKey)
}
func TestCredentialOrderOfResolutionAWSProfileFlag(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
ecsConfig.AWSProfile = customProfileName
os.Setenv("AWS_DEFAULT_PROFILE", defaultProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
os.Setenv("AWS_ACCESS_KEY_ID", envAwsAccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", envAwsSecretKey)
defer os.Clearenv()
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
flagSet.String(flags.AWSProfileFlag, customProfileName, "")
context := cli.NewContext(nil, flagSet, nil)
// invoke test and verify
testCredentialsInSessionWithContext(t, context, ecsConfig, customAwsAccessKey, customAwsSecretKey)
}
func TestCredentialOrderOfResolutionEnvVar(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
os.Setenv("AWS_DEFAULT_PROFILE", defaultProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
os.Setenv("AWS_ACCESS_KEY_ID", envAwsAccessKey)
os.Setenv("AWS_SECRET_ACCESS_KEY", envAwsSecretKey)
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, envAwsAccessKey, envAwsSecretKey)
}
func TestCredentialOrderOfResolutionECSConfig(t *testing.T) {
// defaults
ecsConfig := NewLocalConfig(clusterName)
// set variables for test
ecsConfig.AWSAccessKey = awsAccessKey
ecsConfig.AWSSecretKey = awsSecretKey
os.Setenv("AWS_DEFAULT_PROFILE", defaultProfileName)
os.Setenv("AWS_CONFIG_FILE", "aws_config_example.ini")
os.Setenv("AWS_SHARED_CREDENTIALS_FILE", "aws_credentials_example.ini")
defer os.Clearenv()
// invoke test and verify
testCredentialsInSession(t, ecsConfig, awsAccessKey, awsSecretKey)
}
func testCredentialsInSessionWithToken(t *testing.T, inputConfig *LocalConfig, expectedAccessKey, expectedSecretKey, expectedSessionToken string) {
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
awsSession, err := inputConfig.ToAWSSession(context)
assert.NoError(t, err, "Unexpected error generating a new session")
verifyCredentialsInSessionWithToken(t, awsSession, expectedAccessKey, expectedSecretKey, expectedSessionToken)
}
func testCredentialsInSession(t *testing.T, inputConfig *LocalConfig, expectedAccessKey, expectedSecretKey string) {
flagSet := flag.NewFlagSet("ecs-cli-up", 0)
context := cli.NewContext(nil, flagSet, nil)
awsSession, err := inputConfig.ToAWSSession(context)
assert.NoError(t, err, "Unexpected error generating a new session")
verifyCredentialsInSession(t, awsSession, expectedAccessKey, expectedSecretKey)
}
func testCredentialsInSessionWithContext(t *testing.T, context *cli.Context, inputConfig *LocalConfig, expectedAccessKey, expectedSecretKey string) {
awsSession, err := inputConfig.ToAWSSession(context)
assert.NoError(t, err, "Unexpected error generating a new session")
verifyCredentialsInSession(t, awsSession, expectedAccessKey, expectedSecretKey)
}
func testCredentialsInSessionWithConfig(t *testing.T, context *cli.Context, inputConfig *LocalConfig, ecsConfig *aws.Config,
expectedAccessKey, expectedSecretKey string) {
awsSession, err := inputConfig.toAWSSessionWithConfig(context, ecsConfig)
assert.NoError(t, err, "Unexpected error generating a new session")
verifyCredentialsInSession(t, awsSession, expectedAccessKey, expectedSecretKey)
}
func verifyCredentialsInSession(t *testing.T, awsSession *session.Session, expectedAccessKey, expectedSecretKey string) {
awsConfig := awsSession.Config
resolvedCredentials, err := awsConfig.Credentials.Get()
assert.NoError(t, err, "Unexpected error fetching credentials from the chain provider")
assert.Equal(t, expectedAccessKey, resolvedCredentials.AccessKeyID, "Expected access key to match")
assert.Equal(t, expectedSecretKey, resolvedCredentials.SecretAccessKey, "Expected secret key to match")
}
func verifyCredentialsInSessionWithToken(t *testing.T, awsSession *session.Session, expectedAccessKey, expectedSecretKey, expectedToken string) {
awsConfig := awsSession.Config
resolvedCredentials, err := awsConfig.Credentials.Get()
assert.NoError(t, err, "Unexpected error fetching credentials from the chain provider")
assert.Equal(t, expectedAccessKey, resolvedCredentials.AccessKeyID, "Expected access key to match")
assert.Equal(t, expectedSecretKey, resolvedCredentials.SecretAccessKey, "Expected secret key to match")
assert.Equal(t, expectedToken, resolvedCredentials.SessionToken, "Expected session token to match")
}