17
17
import static org .assertj .core .api .Assertions .assertThat ;
18
18
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
19
19
import static org .assertj .core .api .Assertions .within ;
20
- import static software .amazon .awssdk .auth .credentials .internal .ProcessCredentialsTestUtils .copyErrorCaseProcessCredentialsScript ;
21
- import static software .amazon .awssdk .auth .credentials .internal .ProcessCredentialsTestUtils .copyHappyCaseProcessCredentialsScript ;
22
20
23
21
import java .io .File ;
24
22
import java .io .FileOutputStream ;
30
28
import java .time .Instant ;
31
29
import java .time .temporal .ChronoUnit ;
32
30
import java .util .Arrays ;
31
+ import java .util .List ;
33
32
import java .util .Optional ;
34
33
import org .junit .jupiter .api .AfterAll ;
35
34
import org .junit .jupiter .api .BeforeAll ;
36
35
import org .junit .jupiter .api .Test ;
36
+ import org .junit .jupiter .params .ParameterizedTest ;
37
+ import org .junit .jupiter .params .provider .Arguments ;
38
+ import org .junit .jupiter .params .provider .MethodSource ;
37
39
import software .amazon .awssdk .utils .DateUtils ;
38
40
import software .amazon .awssdk .utils .IoUtils ;
39
41
import software .amazon .awssdk .utils .Platform ;
40
42
41
- class ProcessCredentialsProviderTest {
43
+ public class ProcessCredentialsProviderTest {
42
44
43
45
private static final String PROCESS_RESOURCE_PATH = "/resources/process/" ;
44
46
private static final String RANDOM_SESSION_TOKEN = "RANDOM_TOKEN" ;
@@ -52,13 +54,13 @@ class ProcessCredentialsProviderTest {
52
54
private static String errorScriptLocation ;
53
55
54
56
@ BeforeAll
55
- public static void setup () {
57
+ static void setup () {
56
58
scriptLocation = copyHappyCaseProcessCredentialsScript ();
57
59
errorScriptLocation = copyErrorCaseProcessCredentialsScript ();
58
60
}
59
61
60
62
@ AfterAll
61
- public static void teardown () {
63
+ static void teardown () {
62
64
if (scriptLocation != null && !new File (scriptLocation ).delete ()) {
63
65
throw new IllegalStateException ("Failed to delete file: " + scriptLocation );
64
66
}
@@ -68,14 +70,47 @@ public static void teardown() {
68
70
}
69
71
}
70
72
73
+ @ ParameterizedTest (name = "{index} - {0}" )
74
+ @ MethodSource ("staticCredentialsValues" )
75
+ void staticCredentialsCanBeLoaded (String description , String staticAccountId , Optional <String > expectedValue ,
76
+ String cmd ) {
77
+ ProcessCredentialsProvider .Builder providerBuilder = ProcessCredentialsProvider .builder ().command (cmd );
78
+ if (staticAccountId != null ) {
79
+ providerBuilder .staticAccountId (staticAccountId );
80
+ }
81
+ AwsCredentials credentials = providerBuilder .build ().resolveCredentials ();
82
+
83
+ verifyCredentials (credentials );
84
+ assertThat (credentials ).isNotInstanceOf (AwsSessionCredentials .class );
85
+
86
+ if (expectedValue .isPresent ()) {
87
+ assertThat (credentials .accountId ()).isPresent ().hasValue (expectedValue .get ());
88
+ } else {
89
+ assertThat (credentials .accountId ()).isNotPresent ();
90
+ }
91
+ }
92
+
93
+ private static List <Arguments > staticCredentialsValues () {
94
+ return Arrays .asList (
95
+ Arguments .of ("when only containing access key id, secret" , null , Optional .empty (),
96
+ String .format ("%s accessKeyId secretAccessKey" , scriptLocation )),
97
+ Arguments .of ("when output has account id" , null , Optional .of (ACCOUNT_ID ),
98
+ String .format ("%s %s %s acctid=%s" , scriptLocation , ACCESS_KEY_ID , SECRET_ACCESS_KEY , ACCOUNT_ID )),
99
+ Arguments .of ("when output has account id, static account id configured" , "staticAccountId" , Optional .of (ACCOUNT_ID ),
100
+ String .format ("%s %s %s acctid=%s" , scriptLocation , ACCESS_KEY_ID , SECRET_ACCESS_KEY , ACCOUNT_ID )),
101
+ Arguments .of ("when only static account id is configured" , "staticAccountId" , Optional .of ("staticAccountId" ),
102
+ String .format ("%s %s %s" , scriptLocation , ACCESS_KEY_ID , SECRET_ACCESS_KEY ))
103
+ );
104
+ }
105
+
71
106
@ Test
72
107
void staticCredentialsCanBeLoaded () {
73
108
AwsCredentials credentials =
74
- ProcessCredentialsProvider .builder ()
75
- .command (String .format ("%s accessKeyId secretAccessKey" , scriptLocation ))
76
- .build ()
77
- .resolveCredentials ();
78
-
109
+ ProcessCredentialsProvider .builder ()
110
+ .command (String .format ("%s accessKeyId secretAccessKey" , scriptLocation ))
111
+ .build ()
112
+ .resolveCredentials ();
113
+
79
114
assertThat (credentials ).isNotInstanceOf (AwsSessionCredentials .class );
80
115
assertThat (credentials .accessKeyId ()).isEqualTo (ACCESS_KEY_ID );
81
116
assertThat (credentials .secretAccessKey ()).isEqualTo (SECRET_ACCESS_KEY );
@@ -110,17 +145,17 @@ public void staticCredentials_commandAsListOfStrings_CanBeLoaded() {
110
145
assertThat (credentials .secretAccessKey ()).isEqualTo ("secretAccessKey" );
111
146
assertThat (credentials .providerName ()).isPresent ().contains ("ProcessCredentialsProvider" );
112
147
}
113
-
148
+
114
149
@ Test
115
150
void sessionCredentialsCanBeLoaded () {
116
151
String expiration = DateUtils .formatIso8601Date (Instant .now ());
117
152
ProcessCredentialsProvider credentialsProvider =
118
- ProcessCredentialsProvider .builder ()
119
- .command (String .format ("%s %s %s token=%s exp=%s" ,
120
- scriptLocation , ACCESS_KEY_ID , SECRET_ACCESS_KEY ,
121
- SESSION_TOKEN , expiration ))
122
- .credentialRefreshThreshold (Duration .ofSeconds (1 ))
123
- .build ();
153
+ ProcessCredentialsProvider .builder ()
154
+ .command (String .format ("%s %s %s token=%s exp=%s" ,
155
+ scriptLocation , ACCESS_KEY_ID , SECRET_ACCESS_KEY ,
156
+ SESSION_TOKEN , expiration ))
157
+ .credentialRefreshThreshold (Duration .ofSeconds (1 ))
158
+ .build ();
124
159
125
160
AwsCredentials credentials = credentialsProvider .resolveCredentials ();
126
161
verifySessionCredentials (credentials , expiration );
@@ -142,18 +177,39 @@ void sessionCredentialsWithAccountIdCanBeLoaded() {
142
177
assertThat (credentials .accountId ()).isPresent ().isEqualTo (Optional .of (ACCOUNT_ID ));
143
178
}
144
179
180
+ @ Test
181
+ void sessionCredentialsWithStaticAccountIdCanBeLoaded () {
182
+ String expiration = DateUtils .formatIso8601Date (Instant .now ());
183
+ ProcessCredentialsProvider credentialsProvider =
184
+ ProcessCredentialsProvider .builder ()
185
+ .command (String .format ("%s %s %s token=sessionToken exp=%s" ,
186
+ scriptLocation , ACCESS_KEY_ID , SECRET_ACCESS_KEY , expiration ))
187
+ .credentialRefreshThreshold (Duration .ofSeconds (1 ))
188
+ .staticAccountId ("staticAccountId" )
189
+ .build ();
190
+
191
+ AwsCredentials credentials = credentialsProvider .resolveCredentials ();
192
+ verifySessionCredentials (credentials , expiration );
193
+ assertThat (credentials .accountId ()).isPresent ().hasValue ("staticAccountId" );
194
+ }
195
+
145
196
private void verifySessionCredentials (AwsCredentials credentials , String expiration ) {
197
+ verifyCredentials (credentials );
198
+
146
199
assertThat (credentials ).isInstanceOf (AwsSessionCredentials .class );
147
200
AwsSessionCredentials sessionCredentials = (AwsSessionCredentials ) credentials ;
148
-
149
- assertThat (sessionCredentials .accessKeyId ()).isEqualTo (ACCESS_KEY_ID );
150
- assertThat (sessionCredentials .secretAccessKey ()).isEqualTo (SECRET_ACCESS_KEY );
151
201
assertThat (sessionCredentials .sessionToken ()).isEqualTo (SESSION_TOKEN );
202
+
152
203
assertThat (sessionCredentials .expirationTime ()).isPresent ();
153
204
Instant exp = sessionCredentials .expirationTime ().get ();
154
205
assertThat (exp ).isCloseTo (expiration , within (1 , ChronoUnit .MICROS ));
155
206
}
156
207
208
+ private void verifyCredentials (AwsCredentials credentials ) {
209
+ assertThat (credentials .accessKeyId ()).isEqualTo (ACCESS_KEY_ID );
210
+ assertThat (credentials .secretAccessKey ()).isEqualTo (SECRET_ACCESS_KEY );
211
+ }
212
+
157
213
@ Test
158
214
void resultsAreCached () {
159
215
ProcessCredentialsProvider credentialsProvider =
@@ -212,7 +268,7 @@ void lackOfExpirationIsCachedForever() {
212
268
213
269
assertThat (request1 ).isEqualTo (request2 );
214
270
}
215
-
271
+
216
272
@ Test
217
273
public void processOutputLimitIsEnforced () {
218
274
ProcessCredentialsProvider credentialsProvider =
@@ -228,7 +284,6 @@ public void processOutputLimitIsEnforced() {
228
284
229
285
@ Test
230
286
void processOutputLimitDefaultPassesLargeInput () {
231
-
232
287
String longSessionToken = "lYzvmByqdS1E69QQVEavDDHabQ2GuYKYABKRA4xLbAXpdnFtV030UH4" +
233
288
"bQoZWCDcfADFvBwBm3ixEFTYMjn5XQozpFV2QAsWHirCVcEJ5DC60KPCNBcDi4KLNJfbsp3r6kKTOmYOeqhEyiC4emDX33X2ppZsa5" +
234
289
"1iwr6ShIZPOUPmuR4WDglmWubgO2q5tZv48xA5idkcHEmtGdoL343sY24q4gMh21eeBnF6ikjZdfvZ0Mn86UQ8r05AD346rSwM5bFs" +
0 commit comments