Debian Codegraph @

crt/aws-c-auth/tests/credentials_provider_login_tests.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/testing/aws_test_harness.h>
6
7 #include "aws/auth/private/login_token_utils.h"
8 #include "credentials_provider_utils.h"
9 #include "shared_credentials_test_definitions.h"
10
11 #include <aws/common/clock.h>
12 #include <aws/common/environment.h>
13 #include <aws/http/status_code.h>
14
15 AWS_STATIC_STRING_FROM_LITERAL(s_login_profile, "login");
16 AWS_STATIC_STRING_FROM_LITERAL(s_login_session, "arn:aws:sts::123456789012:assumed-role/Admin/shinji");
17 AWS_STATIC_STRING_FROM_LITERAL(
18 s_login_config_contents,
19 "[profile login]\n"
20 "login_session=arn:aws:sts::123456789012:assumed-role/Admin/shinji\n"
21 "region = us-east-1\n");
22
23 AWS_STATIC_STRING_FROM_LITERAL(s_expected_login_request_path, "/v1/token");
24 AWS_STATIC_STRING_FROM_LITERAL(s_good_access_key_id, "Ritsuko");
25 AWS_STATIC_STRING_FROM_LITERAL(s_good_secret_access_key, "Ryoji");
26 AWS_STATIC_STRING_FROM_LITERAL(s_good_session_token, "PenPen");
27 AWS_STATIC_STRING_FROM_LITERAL(s_good_account_id, "123456789012");
28 static uint64_t s_login_token_expiration_s = 1426138519;
29 static int s_expire_token_assert_range = 5000;
30
31 static int s_aws_credentials_provider_login_test_init_config_profile(
32 struct aws_allocator *allocator,
33 const struct aws_string *config_contents) {
34
35 struct aws_string *config_file_path_str = aws_create_process_unique_file_name(allocator);
36 ASSERT_TRUE(config_file_path_str != NULL);
37 ASSERT_TRUE(aws_create_profile_file(config_file_path_str, config_contents) == AWS_OP_SUCCESS);
38
39 ASSERT_TRUE(
40 aws_set_environment_value(s_default_config_path_env_variable_name, config_file_path_str) == AWS_OP_SUCCESS);
41
42 ASSERT_TRUE(aws_set_environment_value(s_default_profile_env_variable_name, s_login_profile) == AWS_OP_SUCCESS);
43
44 aws_string_destroy(config_file_path_str);
45 return AWS_OP_SUCCESS;
46 }
47
48 static int s_credentials_provider_login_failed_invalid_config(struct aws_allocator *allocator, void *ctx) {
49 (void)ctx;
50
51 const struct {
52 const char *name;
53 const char *text;
54 } valid_config_examples[] = {
55 {"empty", ""},
56
57 {"profile without any login config", "[profile login]\naccessKey=access"},
58
59 {"profile with region but no session any login config", "[profile login]\nregion=us-east-1"},
60 };
61 aws_credentials_provider_http_mock_tester_init(allocator);
62
63 struct aws_credentials_provider_login_options options = {
64 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
65 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
66 .function_table = &aws_credentials_provider_http_mock_function_table,
67 .shutdown_options =
68 {
69 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
70 .shutdown_user_data = NULL,
71 },
72 };
73 for (int i = 0; i < AWS_ARRAY_SIZE(valid_config_examples); i++) {
74 printf("invalid config example [%d]: %s\n", i, valid_config_examples[i].name);
75 struct aws_string *content = aws_string_new_from_c_str(allocator, valid_config_examples[i].text);
76 ASSERT_TRUE(content != NULL);
77 s_aws_credentials_provider_login_test_init_config_profile(allocator, content);
78 aws_string_destroy(content);
79 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
80 ASSERT_NULL(provider);
81 }
82
83 aws_credentials_provider_http_mock_tester_cleanup();
84
85 return 0;
86 }
87
88 AWS_TEST_CASE(credentials_provider_login_failed_invalid_config, s_credentials_provider_login_failed_invalid_config);
89
90 static int s_credentials_provider_login_create_destroy_valid_config(struct aws_allocator *allocator, void *ctx) {
91 (void)ctx;
92
93 const struct {
94 const char *name;
95 const char *text;
96 } valid_config_examples[] = {
97 {"profile with region and login session",
98 "[profile login]\n"
99 "region=us-east-1\n"
100 "login_session=arn:aws:sts::123456789012:assumed-role/Admin/shinji\n"},
101 };
102
103 aws_credentials_provider_http_mock_tester_init(allocator);
104
105 struct aws_credentials_provider_login_options options = {
106 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
107 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
108 .function_table = &aws_credentials_provider_http_mock_function_table,
109 .shutdown_options =
110 {
111 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
112 .shutdown_user_data = NULL,
113 },
114 };
115 for (int i = 0; i < AWS_ARRAY_SIZE(valid_config_examples); i++) {
116 printf("valid config example [%d]: %s\n", i, valid_config_examples[i].name);
117 struct aws_string *content = aws_string_new_from_c_str(allocator, valid_config_examples[i].text);
118 ASSERT_TRUE(content != NULL);
119 s_aws_credentials_provider_login_test_init_config_profile(allocator, content);
120 aws_string_destroy(content);
121 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
122 ASSERT_NOT_NULL(provider);
123 aws_credentials_provider_release(provider);
124 }
125
126 aws_credentials_provider_http_mock_tester_cleanup();
127
128 return 0;
129 }
130 AWS_TEST_CASE(
131 credentials_provider_login_create_destroy_valid_config,
132 s_credentials_provider_login_create_destroy_valid_config);
133
134 static int s_verify_credentials(bool request_made, bool got_credentials, int expected_attempts) {
135 ASSERT_TRUE(credentials_provider_http_mock_tester.has_received_credentials_callback);
136
137 if (got_credentials) {
138 ASSERT_TRUE(credentials_provider_http_mock_tester.credentials != NULL);
139 ASSERT_CURSOR_VALUE_STRING_EQUALS(
140 aws_credentials_get_access_key_id(credentials_provider_http_mock_tester.credentials), s_good_access_key_id);
141 ASSERT_CURSOR_VALUE_STRING_EQUALS(
142 aws_credentials_get_secret_access_key(credentials_provider_http_mock_tester.credentials),
143 s_good_secret_access_key);
144 ASSERT_CURSOR_VALUE_STRING_EQUALS(
145 aws_credentials_get_session_token(credentials_provider_http_mock_tester.credentials), s_good_session_token);
146 ASSERT_CURSOR_VALUE_STRING_EQUALS(
147 aws_credentials_get_account_id(credentials_provider_http_mock_tester.credentials), s_good_account_id);
148 uint64_t credentials_expiration =
149 aws_credentials_get_expiration_timepoint_seconds(credentials_provider_http_mock_tester.credentials);
150 struct aws_date_time now;
151 aws_date_time_init_now(&now);
152 uint64_t approx_should_expire = (uint64_t)aws_date_time_as_epoch_secs(&now) + 90;
153 ASSERT_TRUE(abs((int)(credentials_expiration - approx_should_expire)) < s_expire_token_assert_range);
154 } else {
155 ASSERT_TRUE(credentials_provider_http_mock_tester.error_code);
156 ASSERT_TRUE(credentials_provider_http_mock_tester.credentials == NULL);
157 }
158
159 if (request_made) {
160 ASSERT_CURSOR_VALUE_STRING_EQUALS(
161 aws_byte_cursor_from_buf(&credentials_provider_http_mock_tester.request_path),
162 s_expected_login_request_path);
163 }
164 ASSERT_INT_EQUALS(credentials_provider_http_mock_tester.attempts, expected_attempts);
165
166 return AWS_OP_SUCCESS;
167 }
168
169 static int s_credentials_provider_login_connect_failure(struct aws_allocator *allocator, void *ctx) {
170 (void)ctx;
171
172 aws_credentials_provider_http_mock_tester_init(allocator);
173 credentials_provider_http_mock_tester.is_connection_acquire_successful = false;
174
175 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
176
177 struct aws_credentials_provider_login_options options = {
178 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
179 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
180 .function_table = &aws_credentials_provider_http_mock_function_table,
181 .shutdown_options =
182 {
183 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
184 .shutdown_user_data = NULL,
185 },
186 };
187
188 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
189 ASSERT_NOT_NULL(provider);
190
191 aws_credentials_provider_get_credentials(
192 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
193
194 aws_credentials_provider_http_mock_wait_for_credentials_result();
195 ASSERT_SUCCESS(s_verify_credentials(false /*no request*/, false /*get creds*/, 0 /*expected attempts*/));
196
197 aws_credentials_provider_release(provider);
198
199 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
200
201 aws_credentials_provider_http_mock_tester_cleanup();
202
203 return 0;
204 }
205 AWS_TEST_CASE(credentials_provider_login_connect_failure, s_credentials_provider_login_connect_failure);
206
207 static int s_credentials_provider_login_failure_token_missing(struct aws_allocator *allocator, void *ctx) {
208 (void)ctx;
209
210 aws_credentials_provider_http_mock_tester_init(allocator);
211 credentials_provider_http_mock_tester.is_request_successful = false;
212
213 /* redirect $HOME */
214 struct aws_string *tmp_home;
215 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
216
217 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
218
219 struct aws_credentials_provider_login_options options = {
220 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
221 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
222 .function_table = &aws_credentials_provider_http_mock_function_table,
223 .shutdown_options =
224 {
225 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
226 .shutdown_user_data = NULL,
227 },
228 };
229
230 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
231 ASSERT_NOT_NULL(provider);
232
233 aws_credentials_provider_get_credentials(
234 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
235
236 aws_credentials_provider_http_mock_wait_for_credentials_result();
237
238 ASSERT_SUCCESS(s_verify_credentials(false /*no request*/, false /*get creds*/, 0 /*expected attempts*/));
239
240 aws_credentials_provider_release(provider);
241
242 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
243
244 aws_credentials_provider_http_mock_tester_cleanup();
245 aws_directory_delete(tmp_home, true);
246 aws_string_destroy(tmp_home);
247
248 return 0;
249 }
250 AWS_TEST_CASE(credentials_provider_login_failure_token_missing, s_credentials_provider_login_failure_token_missing);
251
252 AWS_STATIC_STRING_FROM_LITERAL(
253 s_login_token_expired,
254 "{\"accessToken\":{\"accessKeyId\":\"shinji\",\"secretAccessKey\":\"rei\",\"sessionToken\":\"asuka\",\"accountId\":"
255 "\"123456789012\",\"expiresAt\":\"2000-09-13T00:00:00Z\"},\"tokenType\":\"urn:aws:params:oauth:token-type:access_"
256 "token_sigv4\",\"clientId\":\"arn:aws:signin:::devtools/"
257 "same-device\",\"refreshToken\":\"Kaworu\",\"idToken\":\"toji\",\"dpopKey\":\"-----BEGIN EC PRIVATE "
258 "KEY-----\nGendo\n-----END EC PRIVATE KEY-----\n\"}");
259
260 static int s_credentials_provider_login_failure_token_expired(struct aws_allocator *allocator, void *ctx) {
261 (void)ctx;
262
263 aws_credentials_provider_http_mock_tester_init(allocator);
264 credentials_provider_http_mock_tester.is_request_successful = false;
265
266 /* redirect $HOME */
267 struct aws_string *tmp_home;
268 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
269
270 /* create token file */
271 struct aws_byte_buf token_path;
272 AWS_ZERO_STRUCT(token_path);
273 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
274 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
275 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
276 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_expired));
277
278 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
279 uint64_t nano_expiration =
280 aws_timestamp_convert(s_login_token_expiration_s + 100, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
281 mock_aws_set_system_time(nano_expiration);
282 struct aws_credentials_provider_login_options options = {
283 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
284 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
285 .function_table = &aws_credentials_provider_http_mock_function_table,
286 .shutdown_options =
287 {
288 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
289 .shutdown_user_data = NULL,
290 },
291 .system_clock_fn = mock_aws_get_system_time,
292 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
293 };
294
295 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
296 ASSERT_NOT_NULL(provider);
297
298 aws_credentials_provider_get_credentials(
299 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
300
301 aws_credentials_provider_http_mock_wait_for_credentials_result();
302
303 ASSERT_SUCCESS(s_verify_credentials(false /*no request*/, false /*get creds*/, 0 /*expected attempts*/));
304 ASSERT_INT_EQUALS(
305 credentials_provider_http_mock_tester.error_code, AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_TOKEN_EXPIRED);
306
307 aws_credentials_provider_release(provider);
308
309 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
310
311 aws_credentials_provider_http_mock_tester_cleanup();
312 aws_directory_delete(tmp_home, true);
313 aws_string_destroy(tmp_home);
314 aws_byte_buf_clean_up(&token_path);
315 aws_string_destroy(token_path_string);
316 return 0;
317 }
318 AWS_TEST_CASE(credentials_provider_login_failure_token_expired, s_credentials_provider_login_failure_token_expired);
319
320 AWS_STATIC_STRING_FROM_LITERAL(
321 s_login_empty_token,
322 "{\"accessToken\": \"\",\"tokenType\":\"urn:aws:params:oauth:token-type:access_"
323 "token_sigv4\",\"clientId\":\"arn:aws:signin:::devtools/"
324 "same-device\",\"refreshToken\":\"Kaworu\",\"idToken\":\"toji\",\"dpopKey\":\"-----BEGIN EC PRIVATE "
325 "KEY-----\nGendo\n-----END EC PRIVATE KEY-----\n\"}");
326 static int s_credentials_provider_login_failure_token_empty(struct aws_allocator *allocator, void *ctx) {
327 (void)ctx;
328
329 aws_credentials_provider_http_mock_tester_init(allocator);
330 credentials_provider_http_mock_tester.is_request_successful = false;
331
332 /* redirect $HOME */
333 struct aws_string *tmp_home;
334 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
335
336 /* create token file */
337 struct aws_byte_buf token_path;
338 AWS_ZERO_STRUCT(token_path);
339 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
340 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
341 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
342 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_empty_token));
343
344 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
345 mock_aws_set_system_time(0);
346 struct aws_credentials_provider_login_options options = {
347 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
348 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
349 .function_table = &aws_credentials_provider_http_mock_function_table,
350 .shutdown_options =
351 {
352 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
353 .shutdown_user_data = NULL,
354 },
355 .system_clock_fn = mock_aws_get_system_time,
356 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
357 };
358
359 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
360 ASSERT_NOT_NULL(provider);
361
362 aws_credentials_provider_get_credentials(
363 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
364
365 aws_credentials_provider_http_mock_wait_for_credentials_result();
366
367 ASSERT_SUCCESS(s_verify_credentials(false /*no request*/, false /*get creds*/, 0 /*expected attempts*/));
368 ASSERT_INT_EQUALS(
369 credentials_provider_http_mock_tester.error_code, AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_FAILED_TO_CREATE_TOKEN);
370
371 aws_credentials_provider_release(provider);
372
373 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
374
375 aws_credentials_provider_http_mock_tester_cleanup();
376 aws_directory_delete(tmp_home, true);
377 aws_string_destroy(tmp_home);
378 aws_byte_buf_clean_up(&token_path);
379 aws_string_destroy(token_path_string);
380 return 0;
381 }
382 AWS_TEST_CASE(credentials_provider_login_failure_token_empty, s_credentials_provider_login_failure_token_empty);
383
384 AWS_STATIC_STRING_FROM_LITERAL(
385 s_login_token_pembad,
386 "{\"accessToken\":{\"accessKeyId\":\"shinji\",\"secretAccessKey\":\"rei\",\"sessionToken\":\"asuka\",\"accountId\":"
387 "\"123456789012\",\"expiresAt\":\"3000-09-13T00:00:00Z\"},\"tokenType\":\"urn:aws:params:oauth:token-type:access_"
388 "token_sigv4\",\"clientId\":\"arn:aws:signin:::devtools/"
389 "same-device\",\"refreshToken\":\"Kaworu\",\"idToken\":\"toji\",\"dpopKey\":\"-----BEGIN EC PRIVATE "
390 "KEY-----\nGendo\n-----END EC PRIVATE KEY-----\n\"}");
391
392 static int s_credentials_provider_login_bad_pem(struct aws_allocator *allocator, void *ctx) {
393 (void)ctx;
394
395 aws_credentials_provider_http_mock_tester_init(allocator);
396 credentials_provider_http_mock_tester.is_request_successful = false;
397 credentials_provider_http_mock_tester.response_code = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;
398
399 /* redirect $HOME */
400 struct aws_string *tmp_home;
401 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
402
403 /* create token file */
404 struct aws_byte_buf token_path;
405 AWS_ZERO_STRUCT(token_path);
406 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
407 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
408 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
409 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_pembad));
410
411 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
412
413 mock_aws_set_system_time(0);
414 struct aws_credentials_provider_login_options options = {
415 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
416 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
417 .function_table = &aws_credentials_provider_http_mock_function_table,
418 .shutdown_options =
419 {
420 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
421 .shutdown_user_data = NULL,
422 },
423 .system_clock_fn = mock_aws_get_system_time,
424 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
425 };
426
427 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
428 ASSERT_NOT_NULL(provider);
429
430 aws_credentials_provider_get_credentials(
431 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
432
433 aws_credentials_provider_http_mock_wait_for_credentials_result();
434
435 ASSERT_SUCCESS(s_verify_credentials(false /*request made*/, false /*get creds*/, 0 /*expected attempts*/));
436
437 aws_credentials_provider_release(provider);
438
439 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
440
441 aws_credentials_provider_http_mock_tester_cleanup();
442
443 aws_directory_delete(tmp_home, true);
444 aws_string_destroy(tmp_home);
445 aws_byte_buf_clean_up(&token_path);
446 aws_string_destroy(token_path_string);
447 return 0;
448 }
449 AWS_TEST_CASE(credentials_provider_login_bad_pem, s_credentials_provider_login_bad_pem);
450
451 AWS_STATIC_STRING_FROM_LITERAL(
452 s_login_token_good,
453 "{\"accessToken\":{\"accessKeyId\":\"shinji\",\"secretAccessKey\":\"rei\",\"sessionToken\":\"asuka\",\"accountId\":"
454 "\"123456789012\",\"expiresAt\":\"3000-09-13T00:00:00Z\"},\"tokenType\":\"urn:aws:params:oauth:token-type:access_"
455 "token_sigv4\",\"clientId\":\"arn:aws:signin:::devtools/"
456 "same-device\",\"refreshToken\":\"Kaworu\",\"idToken\":\"toji\",\"dpopKey\":\"-----BEGIN EC PRIVATE "
457 "KEY-----\nMHcCAQEEIBDOA9C+wyEeYZYLa9fPAzzZLi43suMHc7GqUSD2wP9VoAoGCCqGSM49\nAwEHoUQDQgAE8p+"
458 "kv53xQZSVnOvFFQZgYXafB8IQXc0boBiNwruEaIzNEi5/7m0I\nP8t5hwdP1bEyoLcHx9sIDcPy8W9str/Kow==\n-----END EC PRIVATE "
459 "KEY-----\n\"}");
460
461 static int s_credentials_provider_login_request_failure(struct aws_allocator *allocator, void *ctx) {
462 (void)ctx;
463
464 aws_credentials_provider_http_mock_tester_init(allocator);
465 credentials_provider_http_mock_tester.is_request_successful = false;
466 credentials_provider_http_mock_tester.response_code = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;
467
468 /* redirect $HOME */
469 struct aws_string *tmp_home;
470 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
471
472 /* create token file */
473 struct aws_byte_buf token_path;
474 AWS_ZERO_STRUCT(token_path);
475 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
476 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
477 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
478 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_good));
479
480 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
481
482 mock_aws_set_system_time(0);
483 struct aws_credentials_provider_login_options options = {
484 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
485 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
486 .function_table = &aws_credentials_provider_http_mock_function_table,
487 .shutdown_options =
488 {
489 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
490 .shutdown_user_data = NULL,
491 },
492 .system_clock_fn = mock_aws_get_system_time,
493 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
494 };
495
496 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
497 ASSERT_NOT_NULL(provider);
498
499 aws_credentials_provider_get_credentials(
500 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
501
502 aws_credentials_provider_http_mock_wait_for_credentials_result();
503
504 ASSERT_SUCCESS(s_verify_credentials(true /*request made*/, false /*get creds*/, 1 /*expected attempts*/));
505
506 aws_credentials_provider_release(provider);
507
508 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
509
510 aws_credentials_provider_http_mock_tester_cleanup();
511
512 aws_directory_delete(tmp_home, true);
513 aws_string_destroy(tmp_home);
514 aws_byte_buf_clean_up(&token_path);
515 aws_string_destroy(token_path_string);
516 return 0;
517 }
518 AWS_TEST_CASE(credentials_provider_login_request_failure, s_credentials_provider_login_request_failure);
519
520 AWS_STATIC_STRING_FROM_LITERAL(s_bad_json_response, "{ \"third\": \"impact\" }");
521 static int s_credentials_provider_login_bad_response(struct aws_allocator *allocator, void *ctx) {
522 (void)ctx;
523
524 aws_credentials_provider_http_mock_tester_init(allocator);
525
526 /* redirect $HOME */
527 struct aws_string *tmp_home;
528 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
529
530 /* create token file */
531 struct aws_byte_buf token_path;
532 AWS_ZERO_STRUCT(token_path);
533 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
534 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
535 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
536 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_good));
537
538 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
539
540 struct aws_byte_cursor bad_json_cursor = aws_byte_cursor_from_string(s_bad_json_response);
541 aws_array_list_push_back(&credentials_provider_http_mock_tester.response_data_callbacks, &bad_json_cursor);
542
543 mock_aws_set_system_time(0);
544 struct aws_credentials_provider_login_options options = {
545 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
546 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
547 .function_table = &aws_credentials_provider_http_mock_function_table,
548 .shutdown_options =
549 {
550 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
551 .shutdown_user_data = NULL,
552 },
553 .system_clock_fn = mock_aws_get_system_time,
554 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
555 };
556
557 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
558 ASSERT_NOT_NULL(provider);
559
560 aws_credentials_provider_get_credentials(
561 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
562
563 aws_credentials_provider_http_mock_wait_for_credentials_result();
564
565 ASSERT_SUCCESS(s_verify_credentials(true /*request made*/, false /*get creds*/, 1 /*expected attempts*/));
566
567 aws_credentials_provider_release(provider);
568
569 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
570
571 aws_credentials_provider_http_mock_tester_cleanup();
572
573 aws_directory_delete(tmp_home, true);
574 aws_string_destroy(tmp_home);
575 aws_byte_buf_clean_up(&token_path);
576 aws_string_destroy(token_path_string);
577 return 0;
578 }
579 AWS_TEST_CASE(credentials_provider_login_bad_response, s_credentials_provider_login_bad_response);
580
581 static int s_credentials_provider_login_retryable_error(struct aws_allocator *allocator, void *ctx) {
582 (void)ctx;
583
584 aws_credentials_provider_http_mock_tester_init(allocator);
585 credentials_provider_http_mock_tester.response_code = AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR;
586
587 /* redirect $HOME */
588 struct aws_string *tmp_home;
589 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
590
591 /* create token file */
592 struct aws_byte_buf token_path;
593 AWS_ZERO_STRUCT(token_path);
594 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
595 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
596 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
597 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_good));
598
599 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
600
601 struct aws_byte_cursor bad_json_cursor = aws_byte_cursor_from_string(s_bad_json_response);
602 aws_array_list_push_back(&credentials_provider_http_mock_tester.response_data_callbacks, &bad_json_cursor);
603
604 mock_aws_set_system_time(0);
605 struct aws_credentials_provider_login_options options = {
606 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
607 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
608 .function_table = &aws_credentials_provider_http_mock_function_table,
609 .shutdown_options =
610 {
611 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
612 .shutdown_user_data = NULL,
613 },
614 .system_clock_fn = mock_aws_get_system_time,
615 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
616 };
617
618 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
619 ASSERT_NOT_NULL(provider);
620
621 aws_credentials_provider_get_credentials(
622 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
623
624 aws_credentials_provider_http_mock_wait_for_credentials_result();
625
626 ASSERT_SUCCESS(s_verify_credentials(true /*request made*/, false /*get creds*/, 4 /*expected attempts*/));
627
628 aws_credentials_provider_release(provider);
629
630 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
631
632 aws_credentials_provider_http_mock_tester_cleanup();
633
634 aws_directory_delete(tmp_home, true);
635 aws_string_destroy(tmp_home);
636 aws_byte_buf_clean_up(&token_path);
637 aws_string_destroy(token_path_string);
638 return 0;
639 }
640 AWS_TEST_CASE(credentials_provider_login_retryable_error, s_credentials_provider_login_retryable_error);
641
642 AWS_STATIC_STRING_FROM_LITERAL(
643 s_good_response,
644 "{\"accessToken\":{\"accessKeyId\":\"Ritsuko\",\"secretAccessKey\":\"Ryoji\",\"sessionToken\":\"PenPen\"},"
645 "\"tokenType\":\"urn:aws:params:oauth:token-type:access_token_sigv4\",\"expiresIn\":900,\"refreshToken\":"
646 "\"Kensuke\"}");
647 static int s_credentials_provider_login_basic_success(struct aws_allocator *allocator, void *ctx) {
648 (void)ctx;
649
650 aws_credentials_provider_http_mock_tester_init(allocator);
651
652 /* redirect $HOME */
653 struct aws_string *tmp_home;
654 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
655
656 /* create token file */
657 struct aws_byte_buf token_path;
658 AWS_ZERO_STRUCT(token_path);
659 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
660 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
661 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
662 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_good));
663
664 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
665
666 /* set the response */
667 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
668 aws_array_list_push_back(&credentials_provider_http_mock_tester.response_data_callbacks, &good_response_cursor);
669
670 mock_aws_set_system_time(0);
671 struct aws_credentials_provider_login_options options = {
672 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
673 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
674 .function_table = &aws_credentials_provider_http_mock_function_table,
675 .shutdown_options =
676 {
677 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
678 .shutdown_user_data = NULL,
679 },
680 .system_clock_fn = mock_aws_get_system_time,
681 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
682 };
683
684 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
685 ASSERT_NOT_NULL(provider);
686
687 aws_credentials_provider_get_credentials(
688 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
689
690 aws_credentials_provider_http_mock_wait_for_credentials_result();
691
692 ASSERT_SUCCESS(s_verify_credentials(true /*request made*/, true /*get creds*/, 1 /*expected attempts*/));
693
694 aws_credentials_provider_release(provider);
695
696 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
697
698 aws_credentials_provider_http_mock_tester_cleanup();
699
700 aws_directory_delete(tmp_home, true);
701 aws_string_destroy(tmp_home);
702 aws_byte_buf_clean_up(&token_path);
703 aws_string_destroy(token_path_string);
704 return 0;
705 }
706 AWS_TEST_CASE(credentials_provider_login_basic_success, s_credentials_provider_login_basic_success);
707
708 static int s_credentials_provider_login_basic_success_after_failure(struct aws_allocator *allocator, void *ctx) {
709 (void)ctx;
710
711 aws_credentials_provider_http_mock_tester_init(allocator);
712 credentials_provider_http_mock_tester.failure_count = 2;
713 credentials_provider_http_mock_tester.failure_response_code = AWS_HTTP_STATUS_CODE_500_INTERNAL_SERVER_ERROR;
714 /* redirect $HOME */
715 struct aws_string *tmp_home;
716 ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home));
717
718 /* create token file */
719 struct aws_byte_buf token_path;
720 AWS_ZERO_STRUCT(token_path);
721 aws_login_token_construct_token_path(allocator, s_login_session, tmp_home, &token_path);
722 struct aws_string *token_path_string = aws_string_new_from_buf(allocator, &token_path);
723 ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path_string));
724 ASSERT_SUCCESS(aws_create_profile_file(token_path_string, s_login_token_good));
725
726 s_aws_credentials_provider_login_test_init_config_profile(allocator, s_login_config_contents);
727
728 /* set the response */
729 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
730 aws_array_list_push_back(&credentials_provider_http_mock_tester.response_data_callbacks, &good_response_cursor);
731
732 mock_aws_set_system_time(0);
733 struct aws_credentials_provider_login_options options = {
734 .bootstrap = credentials_provider_http_mock_tester.bootstrap,
735 .tls_ctx = credentials_provider_http_mock_tester.tls_ctx,
736 .function_table = &aws_credentials_provider_http_mock_function_table,
737 .shutdown_options =
738 {
739 .shutdown_callback = aws_credentials_provider_http_mock_on_shutdown_complete,
740 .shutdown_user_data = NULL,
741 },
742 .system_clock_fn = mock_aws_get_system_time,
743 .login_cache_directory_override = aws_byte_cursor_from_string(tmp_home),
744 };
745
746 struct aws_credentials_provider *provider = aws_credentials_provider_new_login(allocator, &options);
747 ASSERT_NOT_NULL(provider);
748
749 aws_credentials_provider_get_credentials(
750 provider, aws_credentials_provider_http_mock_get_credentials_callback, NULL);
751
752 aws_credentials_provider_http_mock_wait_for_credentials_result();
753
754 ASSERT_SUCCESS(s_verify_credentials(true /*request made*/, true /*get creds*/, 3 /*expected attempts*/));
755
756 aws_credentials_provider_release(provider);
757
758 aws_credentials_provider_http_mock_wait_for_shutdown_callback();
759
760 aws_credentials_provider_http_mock_tester_cleanup();
761
762 aws_directory_delete(tmp_home, true);
763 aws_string_destroy(tmp_home);
764 aws_byte_buf_clean_up(&token_path);
765 aws_string_destroy(token_path_string);
766 return 0;
767 }
768 AWS_TEST_CASE(
769 credentials_provider_login_basic_success_after_failure,
770 s_credentials_provider_login_basic_success_after_failure);