-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathEnvironmentAwsCredentialsProviderSpec.groovy
129 lines (104 loc) · 4.44 KB
/
EnvironmentAwsCredentialsProviderSpec.groovy
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
/*
* Copyright 2017-2019 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 io.micronaut.aws.sdk.v2
import io.micronaut.context.ApplicationContext
import software.amazon.awssdk.auth.credentials.AwsCredentials
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties
class EnvironmentAwsCredentialsProviderSpec extends Specification {
static final String TEST_KEY_ID = "testKeyId"
static final String TEST_SECRET_KEY = "testSecretKey"
static final String TEST_SESSION_TOKEN = "testSessionToken"
void "AWS accessKeyId and secretKey can be read from environment"() {
given:
ApplicationContext applicationContext = ApplicationContext.run([
"aws.accessKeyId": TEST_KEY_ID,
"aws.secretKey" : TEST_SECRET_KEY
])
when:
AwsCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials()
then:
awsCredentials.accessKeyId() == TEST_KEY_ID
awsCredentials.secretAccessKey() == TEST_SECRET_KEY
cleanup:
applicationContext.close()
}
void "AWS alternate accessKey and secretAccessKey can be read from environment"() {
given:
ApplicationContext applicationContext = ApplicationContext.run([
"aws.accessKeyId": null,
"aws.secretKey": null,
"aws.accessKey": TEST_KEY_ID,
"aws.secretAccessKey": TEST_SECRET_KEY
])
when:
AwsCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials()
then:
awsCredentials.accessKeyId() == TEST_KEY_ID
awsCredentials.secretAccessKey() == TEST_SECRET_KEY
cleanup:
applicationContext.close()
}
void "AWS accessKeyId, secretKey, and sessionToken can be read from environment"() {
given:
ApplicationContext applicationContext = ApplicationContext.run([
"aws.accessKeyId": TEST_KEY_ID,
"aws.secretKey": TEST_SECRET_KEY,
"aws.sessionToken": TEST_SESSION_TOKEN
])
when:
AwsSessionCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials() as AwsSessionCredentials
then:
awsCredentials.accessKeyId() == TEST_KEY_ID
awsCredentials.secretAccessKey() == TEST_SECRET_KEY
awsCredentials.sessionToken() == TEST_SESSION_TOKEN
cleanup:
applicationContext.close()
}
void "AWS accessKeyId, secretKey, and sessionToken can be read via yaml configuration"() {
given:
ApplicationContext applicationContext = ApplicationContext.run("yaml")
when:
AwsSessionCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials() as AwsSessionCredentials
then:
awsCredentials.accessKeyId() == "yamlAccessKeyId"
awsCredentials.secretAccessKey() == "yamlSecretKey"
awsCredentials.sessionToken() == "yamlSessionToken"
cleanup:
applicationContext.close()
}
@RestoreSystemProperties
void "credentials can still be read from default places like system properties"() {
given:
System.setProperty('aws.accessKeyId', TEST_KEY_ID)
System.setProperty('aws.secretKey', TEST_SECRET_KEY)
ApplicationContext applicationContext = ApplicationContext.run()
when:
AwsCredentials awsCredentials =
applicationContext.getBean(AwsCredentialsProvider).resolveCredentials()
then:
awsCredentials.accessKeyId() == TEST_KEY_ID
awsCredentials.secretAccessKey() == TEST_SECRET_KEY
cleanup:
applicationContext.close()
}
}