Debian Codegraph @

crt/aws-c-auth/tests/credentials_provider_sts_web_identity_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
6 #include <aws/testing/aws_test_harness.h>
7
8 #include "shared_credentials_test_definitions.h"
9 #include <aws/auth/credentials.h>
10 #include <aws/auth/private/credentials_utils.h>
11 #include <aws/common/clock.h>
12 #include <aws/common/condition_variable.h>
13 #include <aws/common/date_time.h>
14 #include <aws/common/environment.h>
15 #include <aws/common/string.h>
16 #include <aws/common/thread.h>
17 #include <aws/http/request_response.h>
18 #include <aws/http/status_code.h>
19 #include <aws/io/channel_bootstrap.h>
20 #include <aws/io/event_loop.h>
21 #include <aws/io/logging.h>
22 #include <aws/io/socket.h>
23 #include <aws/io/stream.h>
24 #include <aws/io/tls_channel_handler.h>
25 #include <aws/sdkutils/aws_profile.h>
26
27 static struct aws_mock_sts_web_identity_tester {
28 struct aws_tls_ctx *tls_ctx;
29
30 struct aws_byte_buf request_body;
31
32 struct aws_array_list response_data_callbacks;
33 bool is_connection_acquire_successful;
34 bool is_request_successful;
35
36 struct aws_mutex lock;
37 struct aws_condition_variable signal;
38
39 struct aws_credentials *credentials;
40 bool has_received_credentials_callback;
41 bool has_received_shutdown_callback;
42
43 int attempts;
44 int response_code;
45 int error_code;
46
47 struct proxy_env_var_settings *proxy_config;
48 } s_tester;
49
50 static void s_on_shutdown_complete(void *user_data) {
51 (void)user_data;
52
53 aws_mutex_lock(&s_tester.lock);
54 s_tester.has_received_shutdown_callback = true;
55 aws_mutex_unlock(&s_tester.lock);
56
57 aws_condition_variable_notify_one(&s_tester.signal);
58 }
59
60 static bool s_has_tester_received_shutdown_callback(void *user_data) {
61 (void)user_data;
62
63 return s_tester.has_received_shutdown_callback;
64 }
65
66 static void s_aws_wait_for_provider_shutdown_callback(void) {
67 aws_mutex_lock(&s_tester.lock);
68 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_has_tester_received_shutdown_callback, NULL);
69 aws_mutex_unlock(&s_tester.lock);
70 }
71
72 struct mock_connection_manager {
73 struct aws_allocator *allocator;
74 aws_http_connection_manager_shutdown_complete_fn *shutdown_complete_callback;
75 void *shutdown_complete_user_data;
76
77 int count;
78 };
79
80 static struct aws_http_connection_manager *s_aws_http_connection_manager_new_mock(
81 struct aws_allocator *allocator,
82 const struct aws_http_connection_manager_options *options) {
83
84 if (s_tester.proxy_config != NULL) {
85 AWS_FATAL_ASSERT(options->proxy_ev_settings->env_var_type == s_tester.proxy_config->env_var_type);
86 AWS_FATAL_ASSERT(options->proxy_ev_settings->connection_type == s_tester.proxy_config->connection_type);
87 AWS_FATAL_ASSERT(options->proxy_ev_settings->tls_options == s_tester.proxy_config->tls_options);
88 }
89
90 struct mock_connection_manager *mock_manager = aws_mem_calloc(allocator, 1, sizeof(struct mock_connection_manager));
91 mock_manager->allocator = allocator;
92 mock_manager->shutdown_complete_callback = options->shutdown_complete_callback;
93 mock_manager->shutdown_complete_user_data = options->shutdown_complete_user_data;
94 return (struct aws_http_connection_manager *)mock_manager;
95 }
96
97 static void s_aws_http_connection_manager_release_mock(struct aws_http_connection_manager *manager) {
98 struct mock_connection_manager *mock_manager = (struct mock_connection_manager *)manager;
99 AWS_FATAL_ASSERT(mock_manager->count == 0 && "count should dropped to zero when the manager is gone.");
100 mock_manager->shutdown_complete_callback(mock_manager->shutdown_complete_user_data);
101 aws_mem_release(mock_manager->allocator, mock_manager);
102 }
103
104 static void s_aws_http_connection_manager_acquire_connection_mock(
105 struct aws_http_connection_manager *manager,
106 aws_http_connection_manager_on_connection_setup_fn *callback,
107 void *user_data) {
108 struct mock_connection_manager *mock_manager = (struct mock_connection_manager *)manager;
109
110 if (s_tester.is_connection_acquire_successful) {
111 mock_manager->count++;
112 callback((struct aws_http_connection *)1, AWS_OP_SUCCESS, user_data);
113 } else {
114 aws_raise_error(AWS_ERROR_HTTP_UNKNOWN);
115 callback(NULL, AWS_OP_ERR, user_data);
116 }
117 }
118
119 static int s_aws_http_connection_manager_release_connection_mock(
120 struct aws_http_connection_manager *manager,
121 struct aws_http_connection *connection) {
122
123 struct mock_connection_manager *mock_manager = (struct mock_connection_manager *)manager;
124 mock_manager->count--;
125 ASSERT_TRUE(
126 connection == (struct aws_http_connection *)1 && "the released connection should be the same as vended one");
127 ASSERT_TRUE(mock_manager->count >= 0 && "count should always be positive");
128
129 return AWS_OP_SUCCESS;
130 }
131
132 static void s_invoke_mock_request_callbacks(
133 const struct aws_http_make_request_options *options,
134 struct aws_array_list *data_callbacks,
135 bool is_request_successful) {
136
137 size_t data_callback_count = aws_array_list_length(data_callbacks);
138
139 struct aws_http_header headers[1];
140 AWS_ZERO_ARRAY(headers);
141
142 headers[0].name = aws_byte_cursor_from_c_str("some-header");
143 headers[0].value = aws_byte_cursor_from_c_str("value");
144 options->on_response_headers(
145 (struct aws_http_stream *)1, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data);
146
147 if (options->on_response_header_block_done) {
148 options->on_response_header_block_done(
149 (struct aws_http_stream *)1, data_callback_count > 0, options->user_data);
150 }
151
152 for (size_t i = 0; i < data_callback_count; ++i) {
153 struct aws_byte_cursor data_callback_cursor;
154 if (aws_array_list_get_at(data_callbacks, &data_callback_cursor, i)) {
155 continue;
156 }
157
158 options->on_response_body((struct aws_http_stream *)1, &data_callback_cursor, options->user_data);
159 }
160
161 options->on_complete(
162 (struct aws_http_stream *)1,
163 is_request_successful ? AWS_ERROR_SUCCESS : AWS_ERROR_HTTP_UNKNOWN,
164 options->user_data);
165 }
166
167 static struct aws_http_stream *s_aws_http_connection_make_request_mock(
168 struct aws_http_connection *client_connection,
169 const struct aws_http_make_request_options *options) {
170
171 (void)client_connection;
172 (void)options;
173
174 struct aws_byte_cursor path;
175 AWS_ZERO_STRUCT(path);
176 struct aws_input_stream *body_stream = aws_http_message_get_body_stream(options->request);
177 struct aws_allocator *allocator = s_tester.request_body.allocator;
178 aws_byte_buf_clean_up(&s_tester.request_body);
179 aws_byte_buf_init(&s_tester.request_body, allocator, 256);
180 aws_input_stream_read(body_stream, &s_tester.request_body);
181 s_invoke_mock_request_callbacks(options, &s_tester.response_data_callbacks, s_tester.is_request_successful);
182
183 s_tester.attempts++;
184 return (struct aws_http_stream *)1;
185 }
186
187 static int s_aws_http_stream_activate_mock(struct aws_http_stream *stream) {
188 (void)stream;
189 return AWS_OP_SUCCESS;
190 }
191
192 static int s_aws_http_stream_get_incoming_response_status_mock(
193 const struct aws_http_stream *stream,
194 int *out_status_code) {
195 (void)stream;
196
197 if (s_tester.response_code) {
198 *out_status_code = s_tester.response_code;
199 } else {
200 *out_status_code = AWS_HTTP_STATUS_CODE_200_OK;
201 }
202
203 return AWS_OP_SUCCESS;
204 }
205
206 static void s_aws_http_stream_release_mock(struct aws_http_stream *stream) {
207 (void)stream;
208 }
209
210 static void s_aws_http_connection_close_mock(struct aws_http_connection *connection) {
211 (void)connection;
212 }
213
214 static struct aws_http_connection *s_aws_http_stream_get_connection_mock(const struct aws_http_stream *stream) {
215 (void)stream;
216 return (struct aws_http_connection *)1;
217 }
218
219 static struct aws_auth_http_system_vtable s_mock_function_table = {
220 .aws_http_connection_manager_new = s_aws_http_connection_manager_new_mock,
221 .aws_http_connection_manager_release = s_aws_http_connection_manager_release_mock,
222 .aws_http_connection_manager_acquire_connection = s_aws_http_connection_manager_acquire_connection_mock,
223 .aws_http_connection_manager_release_connection = s_aws_http_connection_manager_release_connection_mock,
224 .aws_http_connection_make_request = s_aws_http_connection_make_request_mock,
225 .aws_http_stream_activate = s_aws_http_stream_activate_mock,
226 .aws_http_stream_get_connection = s_aws_http_stream_get_connection_mock,
227 .aws_http_stream_get_incoming_response_status = s_aws_http_stream_get_incoming_response_status_mock,
228 .aws_http_stream_release = s_aws_http_stream_release_mock,
229 .aws_http_connection_close = s_aws_http_connection_close_mock};
230
231 AWS_STATIC_STRING_FROM_LITERAL(s_sts_web_identity_foo_profile, "foo");
232 AWS_STATIC_STRING_FROM_LITERAL(s_sts_web_identity_region_env, "AWS_DEFAULT_REGION");
233 AWS_STATIC_STRING_FROM_LITERAL(s_sts_web_identity_role_arn_env, "AWS_ROLE_ARN");
234 AWS_STATIC_STRING_FROM_LITERAL(s_sts_web_identity_role_session_name_env, "AWS_ROLE_SESSION_NAME");
235 AWS_STATIC_STRING_FROM_LITERAL(s_sts_web_identity_token_file_path_env, "AWS_WEB_IDENTITY_TOKEN_FILE");
236 AWS_STATIC_STRING_FROM_LITERAL(s_sts_web_identity_token_contents, "my-test-token-contents-123-abc-xyz");
237
238 static int s_aws_sts_web_identity_test_unset_env_parameters(void) {
239 ASSERT_TRUE(aws_unset_environment_value(s_sts_web_identity_region_env) == AWS_OP_SUCCESS);
240 ASSERT_TRUE(aws_unset_environment_value(s_sts_web_identity_role_arn_env) == AWS_OP_SUCCESS);
241 ASSERT_TRUE(aws_unset_environment_value(s_sts_web_identity_role_session_name_env) == AWS_OP_SUCCESS);
242 ASSERT_TRUE(aws_unset_environment_value(s_sts_web_identity_token_file_path_env) == AWS_OP_SUCCESS);
243
244 return AWS_OP_SUCCESS;
245 }
246
247 static int s_aws_sts_web_identity_test_init_env_parameters(
248 struct aws_allocator *allocator,
249 const char *region,
250 const char *role_arn,
251 const char *role_session_name,
252 const char *web_identity_token_file) {
253
254 struct aws_string *region_str = aws_string_new_from_c_str(allocator, region);
255 ASSERT_TRUE(region_str != NULL);
256 ASSERT_TRUE(aws_set_environment_value(s_sts_web_identity_region_env, region_str) == AWS_OP_SUCCESS);
257 aws_string_destroy(region_str);
258
259 struct aws_string *role_arn_str = aws_string_new_from_c_str(allocator, role_arn);
260 ASSERT_TRUE(role_arn_str != NULL);
261 ASSERT_TRUE(aws_set_environment_value(s_sts_web_identity_role_arn_env, role_arn_str) == AWS_OP_SUCCESS);
262 aws_string_destroy(role_arn_str);
263
264 struct aws_string *role_session_name_str = aws_string_new_from_c_str(allocator, role_session_name);
265 ASSERT_TRUE(role_session_name_str != NULL);
266 ASSERT_TRUE(
267 aws_set_environment_value(s_sts_web_identity_role_session_name_env, role_session_name_str) == AWS_OP_SUCCESS);
268 aws_string_destroy(role_session_name_str);
269
270 struct aws_string *web_identity_token_file_str = aws_string_new_from_c_str(allocator, web_identity_token_file);
271 ASSERT_TRUE(web_identity_token_file_str != NULL);
272 ASSERT_TRUE(
273 aws_set_environment_value(s_sts_web_identity_token_file_path_env, web_identity_token_file_str) ==
274 AWS_OP_SUCCESS);
275 aws_string_destroy(web_identity_token_file_str);
276
277 return AWS_OP_SUCCESS;
278 }
279
280 static int s_aws_sts_web_identity_test_init_config_profile(
281 struct aws_allocator *allocator,
282 const struct aws_string *config_contents) {
283
284 struct aws_string *config_file_path_str = aws_create_process_unique_file_name(allocator);
285 ASSERT_TRUE(config_file_path_str != NULL);
286 ASSERT_TRUE(aws_create_profile_file(config_file_path_str, config_contents) == AWS_OP_SUCCESS);
287
288 ASSERT_TRUE(
289 aws_set_environment_value(s_default_config_path_env_variable_name, config_file_path_str) == AWS_OP_SUCCESS);
290
291 ASSERT_TRUE(
292 aws_set_environment_value(s_default_profile_env_variable_name, s_sts_web_identity_foo_profile) ==
293 AWS_OP_SUCCESS);
294
295 aws_string_destroy(config_file_path_str);
296 return AWS_OP_SUCCESS;
297 }
298
299 static int s_aws_sts_web_identity_tester_init(struct aws_allocator *allocator) {
300 aws_auth_library_init(allocator);
301
302 struct aws_tls_ctx_options tls_options;
303 aws_tls_ctx_options_init_default_client(&tls_options, allocator);
304 s_tester.tls_ctx = aws_tls_client_ctx_new(allocator, &tls_options);
305 ASSERT_NOT_NULL(s_tester.tls_ctx);
306
307 if (aws_array_list_init_dynamic(&s_tester.response_data_callbacks, allocator, 10, sizeof(struct aws_byte_cursor))) {
308 return AWS_OP_ERR;
309 }
310
311 if (aws_byte_buf_init(&s_tester.request_body, allocator, 256)) {
312 return AWS_OP_ERR;
313 }
314
315 if (aws_mutex_init(&s_tester.lock)) {
316 return AWS_OP_ERR;
317 }
318
319 if (aws_condition_variable_init(&s_tester.signal)) {
320 return AWS_OP_ERR;
321 }
322
323 /* default to everything successful */
324 s_tester.is_connection_acquire_successful = true;
325 s_tester.is_request_successful = true;
326
327 /* cleanup */
328 aws_tls_ctx_options_clean_up(&tls_options);
329
330 return AWS_OP_SUCCESS;
331 }
332
333 static void s_aws_sts_web_identity_tester_cleanup(void) {
334 aws_tls_ctx_release(s_tester.tls_ctx);
335 aws_array_list_clean_up(&s_tester.response_data_callbacks);
336 aws_byte_buf_clean_up(&s_tester.request_body);
337 aws_condition_variable_clean_up(&s_tester.signal);
338 aws_mutex_clean_up(&s_tester.lock);
339 aws_credentials_release(s_tester.credentials);
340 aws_auth_library_clean_up();
341 }
342
343 static bool s_has_tester_received_credentials_callback(void *user_data) {
344 (void)user_data;
345
346 return s_tester.has_received_credentials_callback;
347 }
348
349 static void s_aws_wait_for_credentials_result(void) {
350 aws_mutex_lock(&s_tester.lock);
351 aws_condition_variable_wait_pred(
352 &s_tester.signal, &s_tester.lock, s_has_tester_received_credentials_callback, NULL);
353 aws_mutex_unlock(&s_tester.lock);
354 }
355
356 static void s_get_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
357 (void)user_data;
358
359 aws_mutex_lock(&s_tester.lock);
360 s_tester.has_received_credentials_callback = true;
361 s_tester.credentials = credentials;
362 s_tester.error_code = error_code;
363 if (credentials != NULL) {
364 aws_credentials_acquire(credentials);
365 }
366 aws_condition_variable_notify_one(&s_tester.signal);
367 aws_mutex_unlock(&s_tester.lock);
368 }
369
370 static int s_credentials_provider_sts_web_identity_new_destroy_from_parameters(
371 struct aws_allocator *allocator,
372 void *ctx) {
373 (void)ctx;
374
375 s_aws_sts_web_identity_tester_init(allocator);
376
377 s_aws_sts_web_identity_test_unset_env_parameters();
378
379 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
380 ASSERT_TRUE(token_file_path_str != NULL);
381 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
382
383 struct aws_credentials_provider_sts_web_identity_options options = {
384 .bootstrap = NULL,
385 .tls_ctx = s_tester.tls_ctx,
386 .function_table = &s_mock_function_table,
387 .shutdown_options =
388 {
389 .shutdown_callback = s_on_shutdown_complete,
390 .shutdown_user_data = NULL,
391 },
392 .region = aws_byte_cursor_from_c_str("us-east-1"),
393 .role_arn = aws_byte_cursor_from_c_str("arn:aws:iam::1234567890:role/test-arn"),
394 .role_session_name = aws_byte_cursor_from_c_str("9876543210"),
395 .token_file_path = aws_byte_cursor_from_string(token_file_path_str),
396 };
397
398 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
399 ASSERT_NOT_NULL(provider);
400 aws_string_destroy(token_file_path_str);
401 aws_credentials_provider_release(provider);
402
403 s_aws_wait_for_provider_shutdown_callback();
404
405 s_aws_sts_web_identity_tester_cleanup();
406
407 return 0;
408 }
409 AWS_TEST_CASE(
410 credentials_provider_sts_web_identity_new_destroy_from_parameters,
411 s_credentials_provider_sts_web_identity_new_destroy_from_parameters);
412
413 static int s_credentials_provider_sts_web_identity_new_destroy_from_env(struct aws_allocator *allocator, void *ctx) {
414 (void)ctx;
415
416 s_aws_sts_web_identity_tester_init(allocator);
417
418 s_aws_sts_web_identity_test_unset_env_parameters();
419
420 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
421 ASSERT_TRUE(token_file_path_str != NULL);
422 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
423
424 s_aws_sts_web_identity_test_init_env_parameters(
425 allocator,
426 "us-east-1",
427 "arn:aws:iam::1234567890:role/test-arn",
428 "9876543210",
429 aws_string_c_str(token_file_path_str));
430 aws_string_destroy(token_file_path_str);
431
432 struct aws_credentials_provider_sts_web_identity_options options = {
433 .bootstrap = NULL,
434 .tls_ctx = s_tester.tls_ctx,
435 .function_table = &s_mock_function_table,
436 .shutdown_options =
437 {
438 .shutdown_callback = s_on_shutdown_complete,
439 .shutdown_user_data = NULL,
440 },
441 };
442
443 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
444 aws_credentials_provider_release(provider);
445
446 s_aws_wait_for_provider_shutdown_callback();
447
448 s_aws_sts_web_identity_tester_cleanup();
449
450 return 0;
451 }
452 AWS_TEST_CASE(
453 credentials_provider_sts_web_identity_new_destroy_from_env,
454 s_credentials_provider_sts_web_identity_new_destroy_from_env);
455
456 AWS_STATIC_STRING_FROM_LITERAL(
457 s_sts_web_identity_config_file_contents,
458 "[profile default]\n"
459 "region=us-east-1\n"
460 "role_arn=arn:aws:iam::1111111111:role/test-arn\n"
461 "role_session_name=2222222222\n"
462 "web_identity_token_file=/some/unreachable/path/toklen_file\n"
463 "[profile foo]\n"
464 "region=us-west-2\n"
465 "role_arn=arn:aws:iam::3333333333:role/test-arn\n"
466 "role_session_name=4444444444\n"
467 "web_identity_token_file=");
468
469 static int s_credentials_provider_sts_web_identity_new_destroy_from_config(struct aws_allocator *allocator, void *ctx) {
470 (void)ctx;
471
472 s_aws_sts_web_identity_tester_init(allocator);
473
474 s_aws_sts_web_identity_test_unset_env_parameters();
475
476 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
477 ASSERT_TRUE(token_file_path_str != NULL);
478 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
479
480 struct aws_byte_buf content_buf;
481 struct aws_byte_buf existing_content =
482 aws_byte_buf_from_c_str(aws_string_c_str(s_sts_web_identity_config_file_contents));
483 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
484 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(token_file_path_str);
485 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
486 cursor = aws_byte_cursor_from_c_str("\n");
487 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
488 aws_string_destroy(token_file_path_str);
489
490 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
491 ASSERT_TRUE(config_file_contents != NULL);
492 aws_byte_buf_clean_up(&content_buf);
493
494 s_aws_sts_web_identity_test_init_config_profile(allocator, config_file_contents);
495 aws_string_destroy(config_file_contents);
496
497 struct aws_credentials_provider_sts_web_identity_options options = {
498 .bootstrap = NULL,
499 .tls_ctx = s_tester.tls_ctx,
500 .function_table = &s_mock_function_table,
501 .shutdown_options =
502 {
503 .shutdown_callback = s_on_shutdown_complete,
504 .shutdown_user_data = NULL,
505 },
506 };
507
508 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
509 ASSERT_NOT_NULL(provider);
510 aws_credentials_provider_release(provider);
511
512 s_aws_wait_for_provider_shutdown_callback();
513
514 s_aws_sts_web_identity_tester_cleanup();
515
516 return 0;
517 }
518 AWS_TEST_CASE(
519 credentials_provider_sts_web_identity_new_destroy_from_config,
520 s_credentials_provider_sts_web_identity_new_destroy_from_config);
521
522 AWS_STATIC_STRING_FROM_LITERAL(
523 s_basic_config_file,
524 "[profile foo]\n"
525 "region=us-east-1\n");
526
527 static int s_credentials_provider_sts_web_identity_fail_with_empty_config_and_env(
528 struct aws_allocator *allocator,
529 void *ctx) {
530 (void)ctx;
531
532 s_aws_sts_web_identity_tester_init(allocator);
533
534 struct aws_byte_buf content_buf = aws_byte_buf_from_c_str(aws_string_c_str(s_basic_config_file));
535
536 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
537 ASSERT_TRUE(config_file_contents != NULL);
538 aws_byte_buf_clean_up(&content_buf);
539
540 s_aws_sts_web_identity_test_init_config_profile(allocator, config_file_contents);
541 aws_string_destroy(config_file_contents);
542
543 s_aws_sts_web_identity_test_init_env_parameters(allocator, "", "", "", "");
544
545 struct aws_credentials_provider_sts_web_identity_options options = {
546 .bootstrap = NULL,
547 .tls_ctx = s_tester.tls_ctx,
548 .function_table = &s_mock_function_table,
549 .shutdown_options =
550 {
551 .shutdown_callback = s_on_shutdown_complete,
552 .shutdown_user_data = NULL,
553 },
554 };
555
556 ASSERT_NULL(aws_credentials_provider_new_sts_web_identity(allocator, &options));
557 s_aws_sts_web_identity_tester_cleanup();
558 return 0;
559 }
560 AWS_TEST_CASE(
561 credentials_provider_sts_web_identity_fail_with_empty_config_and_env,
562 s_credentials_provider_sts_web_identity_fail_with_empty_config_and_env);
563
564 static int s_credentials_provider_sts_web_identity_new_destroy_from_cached_config(
565 struct aws_allocator *allocator,
566 void *ctx) {
567 (void)ctx;
568
569 s_aws_sts_web_identity_tester_init(allocator);
570
571 s_aws_sts_web_identity_test_unset_env_parameters();
572
573 /* create a config file */
574 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
575 ASSERT_TRUE(token_file_path_str != NULL);
576 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
577
578 struct aws_byte_buf content_buf;
579 struct aws_byte_buf existing_content =
580 aws_byte_buf_from_c_str(aws_string_c_str(s_sts_web_identity_config_file_contents));
581 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
582 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(token_file_path_str);
583 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
584 cursor = aws_byte_cursor_from_c_str("\n");
585 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
586 aws_string_destroy(token_file_path_str);
587
588 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
589 ASSERT_TRUE(config_file_contents != NULL);
590 aws_byte_buf_clean_up(&content_buf);
591
592 s_aws_sts_web_identity_test_init_config_profile(allocator, config_file_contents);
593 aws_string_destroy(config_file_contents);
594
595 struct aws_credentials_provider_sts_web_identity_options options = {
596 .bootstrap = NULL,
597 .tls_ctx = s_tester.tls_ctx,
598 .function_table = &s_mock_function_table,
599 .shutdown_options =
600 {
601 .shutdown_callback = s_on_shutdown_complete,
602 .shutdown_user_data = NULL,
603 },
604 };
605
606 /* read the config files */
607 struct aws_profile_collection *config_profiles = NULL;
608 struct aws_string *config_file_path = NULL;
609
610 ASSERT_TRUE(
611 aws_get_environment_value(allocator, s_default_config_path_env_variable_name, &config_file_path) ==
612 AWS_OP_SUCCESS);
613
614 config_profiles = aws_profile_collection_new_from_file(allocator, config_file_path, AWS_PST_CONFIG);
615 ASSERT_NOT_NULL(config_profiles);
616
617 options.config_profile_collection_cached = config_profiles;
618
619 /* unset environment and config file*/
620 struct aws_string *empty_content = aws_string_new_from_c_str(allocator, "");
621 ASSERT_TRUE(empty_content != NULL);
622 s_aws_sts_web_identity_test_init_config_profile(allocator, empty_content);
623 aws_string_destroy(empty_content);
624
625 s_aws_sts_web_identity_test_unset_env_parameters();
626
627 ASSERT_TRUE(aws_unset_environment_value(s_default_profile_env_variable_name) == AWS_OP_SUCCESS);
628
629 /* assert we can create sts web identity from cached config file */
630 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
631 ASSERT_NOT_NULL(provider);
632 aws_credentials_provider_release(provider);
633
634 s_aws_wait_for_provider_shutdown_callback();
635
636 s_aws_sts_web_identity_tester_cleanup();
637 aws_string_destroy(config_file_path);
638 aws_profile_collection_release(config_profiles);
639 return 0;
640 }
641 AWS_TEST_CASE(
642 credentials_provider_sts_web_identity_new_destroy_from_cached_config,
643 s_credentials_provider_sts_web_identity_new_destroy_from_cached_config);
644
645 static int s_credentials_provider_sts_web_identity_new_failed_without_env_and_config(
646 struct aws_allocator *allocator,
647 void *ctx) {
648 (void)ctx;
649
650 s_aws_sts_web_identity_tester_init(allocator);
651
652 struct aws_string *empty_content = aws_string_new_from_c_str(allocator, "");
653 ASSERT_TRUE(empty_content != NULL);
654 s_aws_sts_web_identity_test_init_config_profile(allocator, empty_content);
655 aws_string_destroy(empty_content);
656
657 s_aws_sts_web_identity_test_unset_env_parameters();
658
659 ASSERT_TRUE(aws_unset_environment_value(s_default_profile_env_variable_name) == AWS_OP_SUCCESS);
660
661 struct aws_credentials_provider_sts_web_identity_options options = {
662 .bootstrap = NULL,
663 .tls_ctx = s_tester.tls_ctx,
664 .function_table = &s_mock_function_table,
665 .shutdown_options =
666 {
667 .shutdown_callback = s_on_shutdown_complete,
668 .shutdown_user_data = NULL,
669 },
670 };
671
672 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
673 ASSERT_TRUE(provider == NULL);
674
675 s_aws_sts_web_identity_tester_cleanup();
676
677 return 0;
678 }
679
680 AWS_TEST_CASE(
681 credentials_provider_sts_web_identity_new_failed_without_env_and_config,
682 s_credentials_provider_sts_web_identity_new_failed_without_env_and_config);
683
684 AWS_STATIC_STRING_FROM_LITERAL(
685 s_expected_sts_web_identity_body_message,
686 "Action=AssumeRoleWithWebIdentity&Version=2011-06-15"
687 "&RoleArn=arn%3Aaws%3Aiam%3A%3A1234567890%3Arole%2Ftest-arn&RoleSessionName=9876543210&WebIdentityToken=my-test-"
688 "token-contents-123-abc-xyz");
689 AWS_STATIC_STRING_FROM_LITERAL(
690 s_expected_sts_web_identity_body_message_config,
691 "Action=AssumeRoleWithWebIdentity&Version=2011-06-15"
692 "&RoleArn=arn%3Aaws%3Aiam%3A%3A3333333333%3Arole%2Ftest-arn&RoleSessionName=4444444444&WebIdentityToken=my-test-"
693 "token-contents-123-abc-xyz");
694
695 AWS_STATIC_STRING_FROM_LITERAL(
696 s_good_response,
697 "<AssumeRoleWithWebIdentityResponse>"
698 " <AssumeRoleWithWebIdentityResult>"
699 " <AssumedRoleUser>"
700 " <Arn>arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/app1</Arn>"
701 " <AssumedRoleId>AROACLKWSDQRAOEXAMPLE:app1</AssumedRoleId>"
702 " </AssumedRoleUser>"
703 " <Credentials>"
704 " <SessionToken>TokenSuccess</SessionToken>"
705 " <SecretAccessKey>SuccessfulSecret</SecretAccessKey>"
706 " <Expiration>2020-02-25T06:03:31Z</Expiration>"
707 " <AccessKeyId>SuccessfulAccessKey</AccessKeyId>"
708 " </Credentials>"
709 " <Provider>www.amazon.com</Provider>"
710 " </AssumeRoleWithWebIdentityResult>"
711 " <ResponseMetadata>"
712 " <RequestId>ad4156e9-bce1-11e2-82e6-6b6efEXAMPLE</RequestId>"
713 " </ResponseMetadata>"
714 "</AssumeRoleWithWebIdentityResponse>");
715 AWS_STATIC_STRING_FROM_LITERAL(s_good_access_key_id, "SuccessfulAccessKey");
716 AWS_STATIC_STRING_FROM_LITERAL(s_good_secret_access_key, "SuccessfulSecret");
717 AWS_STATIC_STRING_FROM_LITERAL(s_good_session_token, "TokenSuccess");
718 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_expiration, "2020-02-25T06:03:31Z");
719 AWS_STATIC_STRING_FROM_LITERAL(s_good_account_id_response, "123456789012");
720
721 static int s_verify_credentials(bool request_made, bool from_config, bool got_credentials, int expected_attempts) {
722
723 if (request_made) {
724 if (from_config) {
725 ASSERT_CURSOR_VALUE_STRING_EQUALS(
726 aws_byte_cursor_from_buf(&s_tester.request_body), s_expected_sts_web_identity_body_message_config);
727 } else {
728 ASSERT_CURSOR_VALUE_STRING_EQUALS(
729 aws_byte_cursor_from_buf(&s_tester.request_body), s_expected_sts_web_identity_body_message);
730 }
731 }
732
733 ASSERT_TRUE(s_tester.has_received_credentials_callback);
734
735 if (got_credentials) {
736 ASSERT_TRUE(s_tester.credentials != NULL);
737 ASSERT_CURSOR_VALUE_STRING_EQUALS(
738 aws_credentials_get_access_key_id(s_tester.credentials), s_good_access_key_id);
739 ASSERT_CURSOR_VALUE_STRING_EQUALS(
740 aws_credentials_get_secret_access_key(s_tester.credentials), s_good_secret_access_key);
741 ASSERT_CURSOR_VALUE_STRING_EQUALS(
742 aws_credentials_get_session_token(s_tester.credentials), s_good_session_token);
743 ASSERT_CURSOR_VALUE_STRING_EQUALS(
744 aws_credentials_get_account_id(s_tester.credentials), s_good_account_id_response);
745 } else {
746 ASSERT_TRUE(s_tester.credentials == NULL);
747 }
748
749 ASSERT_TRUE(s_tester.attempts == expected_attempts);
750
751 return AWS_OP_SUCCESS;
752 }
753
754 static int s_credentials_provider_sts_web_identity_connect_failure(struct aws_allocator *allocator, void *ctx) {
755 (void)ctx;
756
757 s_aws_sts_web_identity_tester_init(allocator);
758 s_tester.is_connection_acquire_successful = false;
759
760 s_aws_sts_web_identity_test_unset_env_parameters();
761
762 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
763 ASSERT_TRUE(token_file_path_str != NULL);
764 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
765
766 s_aws_sts_web_identity_test_init_env_parameters(
767 allocator,
768 "us-east-1",
769 "arn:aws:iam::1234567890:role/test-arn",
770 "9876543210",
771 aws_string_c_str(token_file_path_str));
772 aws_string_destroy(token_file_path_str);
773
774 struct aws_credentials_provider_sts_web_identity_options options = {
775 .bootstrap = NULL,
776 .tls_ctx = s_tester.tls_ctx,
777 .function_table = &s_mock_function_table,
778 .shutdown_options =
779 {
780 .shutdown_callback = s_on_shutdown_complete,
781 .shutdown_user_data = NULL,
782 },
783 };
784
785 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
786
787 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
788
789 s_aws_wait_for_credentials_result();
790
791 ASSERT_SUCCESS(s_verify_credentials(
792 false /*no request*/, false /*from config*/, false /*get creds*/, 0 /*expected attempts*/));
793
794 aws_credentials_provider_release(provider);
795
796 s_aws_wait_for_provider_shutdown_callback();
797
798 s_aws_sts_web_identity_tester_cleanup();
799
800 return 0;
801 }
802 AWS_TEST_CASE(
803 credentials_provider_sts_web_identity_connect_failure,
804 s_credentials_provider_sts_web_identity_connect_failure);
805
806 static int s_credentials_provider_sts_web_identity_request_failure(struct aws_allocator *allocator, void *ctx) {
807 (void)ctx;
808
809 s_aws_sts_web_identity_tester_init(allocator);
810 s_tester.is_request_successful = false;
811
812 s_aws_sts_web_identity_test_unset_env_parameters();
813
814 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
815 ASSERT_TRUE(token_file_path_str != NULL);
816 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
817
818 s_aws_sts_web_identity_test_init_env_parameters(
819 allocator,
820 "us-east-1",
821 "arn:aws:iam::1234567890:role/test-arn",
822 "9876543210",
823 aws_string_c_str(token_file_path_str));
824 aws_string_destroy(token_file_path_str);
825
826 struct aws_credentials_provider_sts_web_identity_options options = {
827 .bootstrap = NULL,
828 .tls_ctx = s_tester.tls_ctx,
829 .function_table = &s_mock_function_table,
830 .shutdown_options =
831 {
832 .shutdown_callback = s_on_shutdown_complete,
833 .shutdown_user_data = NULL,
834 },
835 };
836
837 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
838
839 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
840
841 s_aws_wait_for_credentials_result();
842
843 ASSERT_SUCCESS(s_verify_credentials(
844 true /*request made*/, false /*from config*/, false /*get creds*/, 1 /*expected attempts*/));
845
846 aws_credentials_provider_release(provider);
847
848 s_aws_wait_for_provider_shutdown_callback();
849
850 s_aws_sts_web_identity_tester_cleanup();
851
852 return 0;
853 }
854 AWS_TEST_CASE(
855 credentials_provider_sts_web_identity_request_failure,
856 s_credentials_provider_sts_web_identity_request_failure);
857
858 AWS_STATIC_STRING_FROM_LITERAL(
859 s_bad_document_response,
860 "<AssumeRoleWithWebIdentityResponse xmlns=\"Not the right doc\">Test</AssumeRoleWithWebIdentityResponse>");
861
862 static int s_credentials_provider_sts_web_identity_bad_document_failure(struct aws_allocator *allocator, void *ctx) {
863 (void)ctx;
864
865 s_aws_sts_web_identity_tester_init(allocator);
866
867 s_aws_sts_web_identity_test_unset_env_parameters();
868
869 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
870 ASSERT_TRUE(token_file_path_str != NULL);
871 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
872
873 s_aws_sts_web_identity_test_init_env_parameters(
874 allocator,
875 "us-east-1",
876 "arn:aws:iam::1234567890:role/test-arn",
877 "9876543210",
878 aws_string_c_str(token_file_path_str));
879 aws_string_destroy(token_file_path_str);
880
881 struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_bad_document_response);
882 aws_array_list_push_back(&s_tester.response_data_callbacks, &bad_document_cursor);
883
884 struct aws_credentials_provider_sts_web_identity_options options = {
885 .bootstrap = NULL,
886 .tls_ctx = s_tester.tls_ctx,
887 .function_table = &s_mock_function_table,
888 .shutdown_options =
889 {
890 .shutdown_callback = s_on_shutdown_complete,
891 .shutdown_user_data = NULL,
892 },
893 };
894
895 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
896
897 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
898
899 s_aws_wait_for_credentials_result();
900
901 ASSERT_SUCCESS(s_verify_credentials(
902 true /*request made*/, false /*from config*/, false /*get creds*/, 1 /*expected attempts*/));
903
904 aws_credentials_provider_release(provider);
905
906 s_aws_wait_for_provider_shutdown_callback();
907
908 s_aws_sts_web_identity_tester_cleanup();
909
910 return 0;
911 }
912 AWS_TEST_CASE(
913 credentials_provider_sts_web_identity_bad_document_failure,
914 s_credentials_provider_sts_web_identity_bad_document_failure);
915
916 AWS_STATIC_STRING_FROM_LITERAL(
917 s_retryable_error_response_1,
918 "<Error>"
919 "<Code>IDPCommunicationError</Code>"
920 "<Message>XXX</Message>"
921 "<Resource>YYY</Resource>"
922 "<RequestId>4442587FB7D0A2F9</RequestId>"
923 "</Error>");
924
925 AWS_STATIC_STRING_FROM_LITERAL(
926 s_retryable_error_response_2,
927 "<Error>"
928 "<Code>InvalidIdentityToken</Code>"
929 "<Message>XXX</Message>"
930 "<Resource>YYY</Resource>"
931 "<RequestId>4442587FB7D0A2F9</RequestId>"
932 "</Error>");
933
934 static int s_credentials_provider_sts_web_identity_test_retry_error1(struct aws_allocator *allocator, void *ctx) {
935 (void)ctx;
936
937 s_aws_sts_web_identity_tester_init(allocator);
938 s_tester.response_code = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;
939 s_aws_sts_web_identity_test_unset_env_parameters();
940
941 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
942 ASSERT_TRUE(token_file_path_str != NULL);
943 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
944
945 s_aws_sts_web_identity_test_init_env_parameters(
946 allocator,
947 "us-east-1",
948 "arn:aws:iam::1234567890:role/test-arn",
949 "9876543210",
950 aws_string_c_str(token_file_path_str));
951 aws_string_destroy(token_file_path_str);
952
953 struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_retryable_error_response_1);
954 aws_array_list_push_back(&s_tester.response_data_callbacks, &bad_document_cursor);
955
956 struct aws_credentials_provider_sts_web_identity_options options = {
957 .bootstrap = NULL,
958 .tls_ctx = s_tester.tls_ctx,
959 .function_table = &s_mock_function_table,
960 .shutdown_options =
961 {
962 .shutdown_callback = s_on_shutdown_complete,
963 .shutdown_user_data = NULL,
964 },
965 };
966
967 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
968
969 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
970
971 s_aws_wait_for_credentials_result();
972
973 ASSERT_SUCCESS(s_verify_credentials(
974 true /*request made*/, false /*from config*/, false /*get creds*/, 3 /*expected attempts*/));
975
976 aws_credentials_provider_release(provider);
977
978 s_aws_wait_for_provider_shutdown_callback();
979
980 s_aws_sts_web_identity_tester_cleanup();
981
982 return 0;
983 }
984 AWS_TEST_CASE(
985 credentials_provider_sts_web_identity_test_retry_error1,
986 s_credentials_provider_sts_web_identity_test_retry_error1);
987
988 static int s_credentials_provider_sts_web_identity_test_retry_error2(struct aws_allocator *allocator, void *ctx) {
989 (void)ctx;
990
991 s_aws_sts_web_identity_tester_init(allocator);
992 s_tester.response_code = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;
993 s_aws_sts_web_identity_test_unset_env_parameters();
994
995 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
996 ASSERT_TRUE(token_file_path_str != NULL);
997 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
998
999 s_aws_sts_web_identity_test_init_env_parameters(
1000 allocator,
1001 "us-east-1",
1002 "arn:aws:iam::1234567890:role/test-arn",
1003 "9876543210",
1004 aws_string_c_str(token_file_path_str));
1005 aws_string_destroy(token_file_path_str);
1006
1007 struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_retryable_error_response_2);
1008 aws_array_list_push_back(&s_tester.response_data_callbacks, &bad_document_cursor);
1009
1010 struct aws_credentials_provider_sts_web_identity_options options = {
1011 .bootstrap = NULL,
1012 .tls_ctx = s_tester.tls_ctx,
1013 .function_table = &s_mock_function_table,
1014 .shutdown_options =
1015 {
1016 .shutdown_callback = s_on_shutdown_complete,
1017 .shutdown_user_data = NULL,
1018 },
1019 };
1020
1021 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
1022
1023 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1024
1025 s_aws_wait_for_credentials_result();
1026
1027 ASSERT_SUCCESS(s_verify_credentials(
1028 true /*request made*/, false /*from config*/, false /*get creds*/, 3 /*expected attempts*/));
1029
1030 aws_credentials_provider_release(provider);
1031
1032 s_aws_wait_for_provider_shutdown_callback();
1033
1034 s_aws_sts_web_identity_tester_cleanup();
1035
1036 return 0;
1037 }
1038 AWS_TEST_CASE(
1039 credentials_provider_sts_web_identity_test_retry_error2,
1040 s_credentials_provider_sts_web_identity_test_retry_error2);
1041
1042 static int s_credentials_provider_sts_web_identity_basic_success_env(struct aws_allocator *allocator, void *ctx) {
1043 (void)ctx;
1044
1045 s_aws_sts_web_identity_tester_init(allocator);
1046
1047 s_aws_sts_web_identity_test_unset_env_parameters();
1048
1049 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
1050 ASSERT_TRUE(token_file_path_str != NULL);
1051 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
1052
1053 s_aws_sts_web_identity_test_init_env_parameters(
1054 allocator,
1055 "us-east-1",
1056 "arn:aws:iam::1234567890:role/test-arn",
1057 "9876543210",
1058 aws_string_c_str(token_file_path_str));
1059 aws_string_destroy(token_file_path_str);
1060
1061 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1062 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
1063
1064 struct aws_credentials_provider_sts_web_identity_options options = {
1065 .bootstrap = NULL,
1066 .tls_ctx = s_tester.tls_ctx,
1067 .function_table = &s_mock_function_table,
1068 .shutdown_options =
1069 {
1070 .shutdown_callback = s_on_shutdown_complete,
1071 .shutdown_user_data = NULL,
1072 },
1073 };
1074
1075 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
1076
1077 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1078
1079 s_aws_wait_for_credentials_result();
1080
1081 ASSERT_SUCCESS(s_verify_credentials(
1082 true /*request made*/, false /*from config*/, true /*get creds*/, 1 /*expected attempts*/));
1083
1084 struct aws_date_time expiration;
1085 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
1086 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
1087 ASSERT_INT_EQUALS(
1088 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials), (uint64_t)expiration.timestamp);
1089
1090 aws_credentials_provider_release(provider);
1091
1092 s_aws_wait_for_provider_shutdown_callback();
1093
1094 s_aws_sts_web_identity_tester_cleanup();
1095
1096 return 0;
1097 }
1098
1099 AWS_TEST_CASE(
1100 credentials_provider_sts_web_identity_basic_success_env,
1101 s_credentials_provider_sts_web_identity_basic_success_env);
1102
1103 static int s_credentials_provider_sts_web_identity_basic_success_config(struct aws_allocator *allocator, void *ctx) {
1104 (void)ctx;
1105
1106 s_aws_sts_web_identity_tester_init(allocator);
1107
1108 s_aws_sts_web_identity_test_unset_env_parameters();
1109
1110 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
1111 ASSERT_TRUE(token_file_path_str != NULL);
1112 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
1113
1114 struct aws_byte_buf content_buf;
1115 struct aws_byte_buf existing_content =
1116 aws_byte_buf_from_c_str(aws_string_c_str(s_sts_web_identity_config_file_contents));
1117 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
1118 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(token_file_path_str);
1119 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
1120 cursor = aws_byte_cursor_from_c_str("\n");
1121 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
1122 aws_string_destroy(token_file_path_str);
1123
1124 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
1125 ASSERT_TRUE(config_file_contents != NULL);
1126 aws_byte_buf_clean_up(&content_buf);
1127
1128 s_aws_sts_web_identity_test_init_config_profile(allocator, config_file_contents);
1129 aws_string_destroy(config_file_contents);
1130
1131 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1132 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
1133
1134 struct aws_credentials_provider_sts_web_identity_options options = {
1135 .bootstrap = NULL,
1136 .tls_ctx = s_tester.tls_ctx,
1137 .function_table = &s_mock_function_table,
1138 .shutdown_options =
1139 {
1140 .shutdown_callback = s_on_shutdown_complete,
1141 .shutdown_user_data = NULL,
1142 },
1143 };
1144
1145 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
1146
1147 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1148
1149 s_aws_wait_for_credentials_result();
1150
1151 ASSERT_SUCCESS(
1152 s_verify_credentials(true /*request made*/, true /*from config*/, true /*get creds*/, 1 /*expected attempts*/));
1153
1154 struct aws_date_time expiration;
1155 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
1156 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
1157 ASSERT_INT_EQUALS(
1158 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials), (uint64_t)expiration.timestamp);
1159
1160 aws_credentials_provider_release(provider);
1161
1162 s_aws_wait_for_provider_shutdown_callback();
1163
1164 s_aws_sts_web_identity_tester_cleanup();
1165
1166 return 0;
1167 }
1168
1169 AWS_TEST_CASE(
1170 credentials_provider_sts_web_identity_basic_success_config,
1171 s_credentials_provider_sts_web_identity_basic_success_config);
1172
1173 AWS_STATIC_STRING_FROM_LITERAL(
1174 s_good_response_first_part,
1175 "<AssumeRoleWithWebIdentityResponse>"
1176 " <AssumeRoleWithWebIdentityResult>"
1177 " <AssumedRoleUser>"
1178 " <Arn>arn:aws:sts::123456789012:assumed-role/FederatedWebIdentityRole/app1</Arn>"
1179 " <AssumedRoleId>AROACLKWSDQRAOEXAMPLE:app1</AssumedRoleId>"
1180 " </AssumedRoleUser>"
1181 " <Credentials>");
1182 AWS_STATIC_STRING_FROM_LITERAL(
1183 s_good_response_second_part,
1184 " <SessionToken>TokenSuccess</SessionToken>"
1185 " <SecretAccessKey>SuccessfulSecret</SecretAccessKey>"
1186 " <Expiration>2020-02-25T06:03:31Z</Expiration>"
1187 " <AccessKeyId>SuccessfulAccessKey</AccessKeyId>"
1188 " </Credentials>"
1189 " <Provider>www.amazon.com</Provider>"
1190 " </AssumeRoleWithWebIdentityResult>"
1191 " <ResponseMetadata>");
1192 AWS_STATIC_STRING_FROM_LITERAL(
1193 s_good_response_third_part,
1194
1195 " <RequestId>ad4156e9-bce1-11e2-82e6-6b6efEXAMPLE</RequestId>"
1196 " </ResponseMetadata>"
1197 "</AssumeRoleWithWebIdentityResponse>");
1198
1199 static int s_credentials_provider_sts_web_identity_success_multi_part_doc(struct aws_allocator *allocator, void *ctx) {
1200 (void)ctx;
1201
1202 s_aws_sts_web_identity_tester_init(allocator);
1203
1204 s_aws_sts_web_identity_test_unset_env_parameters();
1205
1206 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
1207 ASSERT_TRUE(token_file_path_str != NULL);
1208 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
1209
1210 s_aws_sts_web_identity_test_init_env_parameters(
1211 allocator,
1212 "us-east-1",
1213 "arn:aws:iam::1234567890:role/test-arn",
1214 "9876543210",
1215 aws_string_c_str(token_file_path_str));
1216 aws_string_destroy(token_file_path_str);
1217
1218 struct aws_byte_cursor good_response_cursor1 = aws_byte_cursor_from_string(s_good_response_first_part);
1219 struct aws_byte_cursor good_response_cursor2 = aws_byte_cursor_from_string(s_good_response_second_part);
1220 struct aws_byte_cursor good_response_cursor3 = aws_byte_cursor_from_string(s_good_response_third_part);
1221 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor1);
1222 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor2);
1223 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor3);
1224
1225 struct aws_credentials_provider_sts_web_identity_options options = {
1226 .bootstrap = NULL,
1227 .tls_ctx = s_tester.tls_ctx,
1228 .function_table = &s_mock_function_table,
1229 .shutdown_options =
1230 {
1231 .shutdown_callback = s_on_shutdown_complete,
1232 .shutdown_user_data = NULL,
1233 },
1234 };
1235
1236 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
1237
1238 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1239
1240 s_aws_wait_for_credentials_result();
1241
1242 ASSERT_SUCCESS(s_verify_credentials(
1243 true /*request made*/, false /*from config*/, true /*get creds*/, 1 /*expected attempts*/));
1244
1245 struct aws_date_time expiration;
1246 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
1247 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
1248 ASSERT_INT_EQUALS(
1249 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials), (uint64_t)expiration.timestamp);
1250
1251 aws_credentials_provider_release(provider);
1252
1253 s_aws_wait_for_provider_shutdown_callback();
1254
1255 s_aws_sts_web_identity_tester_cleanup();
1256
1257 return 0;
1258 }
1259 AWS_TEST_CASE(
1260 credentials_provider_sts_web_identity_success_multi_part_doc,
1261 s_credentials_provider_sts_web_identity_success_multi_part_doc);
1262
1263 static int s_credentials_provider_sts_web_identity_real_new_destroy(struct aws_allocator *allocator, void *ctx) {
1264 (void)ctx;
1265
1266 aws_auth_library_init(allocator);
1267
1268 s_aws_sts_web_identity_test_unset_env_parameters();
1269
1270 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
1271 ASSERT_TRUE(token_file_path_str != NULL);
1272 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
1273
1274 s_aws_sts_web_identity_test_init_env_parameters(
1275 allocator,
1276 "us-east-1",
1277 "arn:aws:iam::1234567890:role/test-arn",
1278 "9876543210",
1279 aws_string_c_str(token_file_path_str));
1280 aws_string_destroy(token_file_path_str);
1281
1282 s_aws_sts_web_identity_tester_init(allocator);
1283
1284 struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
1285
1286 struct aws_host_resolver_default_options resolver_options = {
1287 .el_group = el_group,
1288 .max_entries = 8,
1289 };
1290 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1291
1292 struct aws_client_bootstrap_options bootstrap_options = {
1293 .event_loop_group = el_group,
1294 .host_resolver = resolver,
1295 };
1296 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1297
1298 struct aws_credentials_provider_sts_web_identity_options options = {
1299 .bootstrap = bootstrap,
1300 .tls_ctx = s_tester.tls_ctx,
1301 .shutdown_options =
1302 {
1303 .shutdown_callback = s_on_shutdown_complete,
1304 .shutdown_user_data = NULL,
1305 },
1306 };
1307
1308 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
1309
1310 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1311
1312 s_aws_wait_for_credentials_result();
1313
1314 aws_credentials_provider_release(provider);
1315
1316 s_aws_wait_for_provider_shutdown_callback();
1317
1318 aws_client_bootstrap_release(bootstrap);
1319 aws_host_resolver_release(resolver);
1320 aws_event_loop_group_release(el_group);
1321
1322 s_aws_sts_web_identity_tester_cleanup();
1323
1324 aws_auth_library_clean_up();
1325
1326 return 0;
1327 }
1328 AWS_TEST_CASE(
1329 credentials_provider_sts_web_identity_real_new_destroy,
1330 s_credentials_provider_sts_web_identity_real_new_destroy);
1331
1332 static int s_credentials_provider_sts_web_identity_proxy_routing_enabled_test(
1333 struct aws_allocator *allocator,
1334 void *ctx) {
1335 (void)ctx;
1336
1337 s_aws_sts_web_identity_tester_init(allocator);
1338
1339 struct proxy_env_var_settings proxy_config = {
1340 .env_var_type = AWS_HPEV_ENABLE,
1341 };
1342
1343 s_tester.proxy_config = &proxy_config;
1344
1345 s_aws_sts_web_identity_test_unset_env_parameters();
1346
1347 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
1348 ASSERT_TRUE(token_file_path_str != NULL);
1349 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
1350
1351 s_aws_sts_web_identity_test_init_env_parameters(
1352 allocator,
1353 "us-east-1",
1354 "arn:aws:iam::1234567890:role/test-arn",
1355 "9876543210",
1356 aws_string_c_str(token_file_path_str));
1357 aws_string_destroy(token_file_path_str);
1358
1359 struct aws_credentials_provider_sts_web_identity_options options = {
1360 .bootstrap = NULL,
1361 .tls_ctx = s_tester.tls_ctx,
1362 .function_table = &s_mock_function_table,
1363 .shutdown_options =
1364 {
1365 .shutdown_callback = s_on_shutdown_complete,
1366 .shutdown_user_data = NULL,
1367 },
1368 .proxy_ev_settings = &proxy_config,
1369 };
1370
1371 struct aws_credentials_provider *provider = aws_credentials_provider_new_sts_web_identity(allocator, &options);
1372
1373 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1374
1375 s_aws_wait_for_credentials_result();
1376
1377 ASSERT_TRUE(s_tester.credentials == NULL);
1378
1379 aws_credentials_provider_release(provider);
1380
1381 s_aws_wait_for_provider_shutdown_callback();
1382
1383 s_aws_sts_web_identity_tester_cleanup();
1384
1385 return 0;
1386 }
1387 AWS_TEST_CASE(
1388 credentials_provider_sts_web_identity_proxy_routing_enabled_test,
1389 s_credentials_provider_sts_web_identity_proxy_routing_enabled_test);
1390
1391 /*
1392 * aws_credentials_provider_new_profile should support web_identity_token_file
1393 * in a profile's configuration (without source_profile or credential_source).
1394 *
1395 * This is the same scenario as basic_success_config above, but exercises the
1396 * code path through the profile provider rather than calling
1397 * aws_credentials_provider_new_sts_web_identity directly.
1398 *
1399 * Config:
1400 * [profile foo]
1401 * region = us-west-2
1402 * role_arn = arn:aws:iam::3333333333:role/test-arn
1403 * role_session_name = 4444444444
1404 * web_identity_token_file = <path>
1405 *
1406 * Expected: profile provider detects web_identity_token_file, creates an STS
1407 * web identity provider internally, and returns credentials.
1408 */
1409 static int s_credentials_provider_profile_with_web_identity_config(struct aws_allocator *allocator, void *ctx) {
1410 (void)ctx;
1411
1412 s_aws_sts_web_identity_tester_init(allocator);
1413
1414 s_aws_sts_web_identity_test_unset_env_parameters();
1415
1416 /* Create a real token file on disk */
1417 struct aws_string *token_file_path_str = aws_create_process_unique_file_name(allocator);
1418 ASSERT_TRUE(token_file_path_str != NULL);
1419 ASSERT_TRUE(aws_create_profile_file(token_file_path_str, s_sts_web_identity_token_contents) == AWS_OP_SUCCESS);
1420
1421 /* Build config file content with the token file path */
1422 struct aws_byte_buf content_buf;
1423 struct aws_byte_buf existing_content =
1424 aws_byte_buf_from_c_str(aws_string_c_str(s_sts_web_identity_config_file_contents));
1425 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
1426 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(token_file_path_str);
1427 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
1428 cursor = aws_byte_cursor_from_c_str("\n");
1429 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
1430
1431 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
1432 ASSERT_TRUE(config_file_contents != NULL);
1433 aws_byte_buf_clean_up(&content_buf);
1434
1435 /* Write config file and set environment to point to it */
1436 struct aws_string *config_file_path_str = aws_create_process_unique_file_name(allocator);
1437 ASSERT_TRUE(config_file_path_str != NULL);
1438 ASSERT_TRUE(aws_create_profile_file(config_file_path_str, config_file_contents) == AWS_OP_SUCCESS);
1439 aws_string_destroy(config_file_contents);
1440
1441 /* Push the mock response for AssumeRoleWithWebIdentity */
1442 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1443 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
1444
1445 /* Use aws_credentials_provider_new_profile instead of aws_credentials_provider_new_sts_web_identity */
1446 struct aws_credentials_provider_profile_options options = {
1447 .config_file_name_override = aws_byte_cursor_from_string(config_file_path_str),
1448 .profile_name_override = aws_byte_cursor_from_c_str("foo"),
1449 .bootstrap = NULL,
1450 .tls_ctx = s_tester.tls_ctx,
1451 .function_table = &s_mock_function_table,
1452 .shutdown_options =
1453 {
1454 .shutdown_callback = s_on_shutdown_complete,
1455 .shutdown_user_data = NULL,
1456 },
1457 };
1458
1459 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1460
1461 ASSERT_NOT_NULL(provider);
1462
1463 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1464
1465 s_aws_wait_for_credentials_result();
1466
1467 ASSERT_SUCCESS(
1468 s_verify_credentials(true /*request made*/, true /*from config*/, true /*get creds*/, 1 /*expected attempts*/));
1469
1470 aws_credentials_provider_release(provider);
1471
1472 s_aws_wait_for_provider_shutdown_callback();
1473
1474 aws_file_delete(config_file_path_str);
1475 aws_file_delete(token_file_path_str);
1476 aws_string_destroy(config_file_path_str);
1477 aws_string_destroy(token_file_path_str);
1478 s_aws_sts_web_identity_tester_cleanup();
1479
1480 return 0;
1481 }
1482 AWS_TEST_CASE(
1483 credentials_provider_profile_with_web_identity_config,
1484 s_credentials_provider_profile_with_web_identity_config);