Debian Codegraph @

crt/aws-c-auth/tests/credentials_provider_sts_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/auth/credentials.h>
7 #include <aws/common/clock.h>
8 #include <aws/common/condition_variable.h>
9 #include <aws/common/environment.h>
10 #include <aws/common/string.h>
11 #include <aws/io/channel_bootstrap.h>
12 #include <aws/io/event_loop.h>
13 #include <aws/io/stream.h>
14 #include <aws/io/tls_channel_handler.h>
15
16 #include <aws/http/connection.h>
17 #include <aws/http/connection_manager.h>
18 #include <aws/http/proxy.h>
19 #include <aws/http/request_response.h>
20
21 #include <aws/auth/private/credentials_utils.h>
22 #include <aws/testing/aws_test_harness.h>
23
24 #include "aws/common/byte_buf.h"
25 #include "credentials_provider_utils.h"
26 #include "shared_credentials_test_definitions.h"
27
28 struct aws_mock_http_request {
29 struct aws_byte_buf path;
30 struct aws_byte_buf method;
31 struct aws_byte_buf host_header;
32 struct aws_byte_buf body;
33 bool had_auth_header;
34 struct aws_byte_buf auth_header;
35 int response_code;
36 };
37
38 #define MAX_REQUEST 10
39 struct aws_mock_sts_tester {
40 struct aws_allocator *allocator;
41 struct aws_mock_http_request mocked_requests[MAX_REQUEST];
42 int num_request;
43
44 int mock_response_code;
45 int mock_failure_code;
46 size_t fail_operations;
47
48 struct aws_array_list response_data_callbacks;
49
50 struct aws_mutex lock;
51 struct aws_condition_variable signal;
52
53 struct aws_credentials *credentials;
54 bool has_received_credentials_callback;
55 int mocked_connection_manager_shutdown_callback_count;
56 int expected_connection_manager_shutdown_callback_count;
57 int error_code;
58
59 bool fail_connection;
60
61 int provider_shutdown_callback_count;
62
63 struct aws_event_loop_group *el_group;
64
65 struct aws_host_resolver *resolver;
66
67 struct aws_client_bootstrap *bootstrap;
68
69 struct aws_tls_ctx *tls_ctx;
70
71 struct proxy_env_var_settings *proxy_config;
72 };
73
74 static struct aws_mock_sts_tester s_tester;
75
76 static void s_on_connection_manager_shutdown_complete(void *user_data) {
77 (void)user_data;
78
79 aws_mutex_lock(&s_tester.lock);
80 s_tester.mocked_connection_manager_shutdown_callback_count++;
81 aws_mutex_unlock(&s_tester.lock);
82
83 aws_condition_variable_notify_one(&s_tester.signal);
84 }
85
86 static bool s_has_tester_received_connection_manager_shutdown_callback(void *user_data) {
87 (void)user_data;
88
89 return s_tester.mocked_connection_manager_shutdown_callback_count ==
90 s_tester.expected_connection_manager_shutdown_callback_count;
91 }
92
93 static void s_aws_wait_for_connection_manager_shutdown_callback(void) {
94 aws_mutex_lock(&s_tester.lock);
95 aws_condition_variable_wait_pred(
96 &s_tester.signal, &s_tester.lock, s_has_tester_received_connection_manager_shutdown_callback, NULL);
97 aws_mutex_unlock(&s_tester.lock);
98 }
99
100 static void s_on_provider_shutdown(void *user_data) {
101 (void)user_data;
102
103 aws_mutex_lock(&s_tester.lock);
104 s_tester.provider_shutdown_callback_count++;
105 aws_mutex_unlock(&s_tester.lock);
106
107 aws_condition_variable_notify_one(&s_tester.signal);
108 }
109
110 static bool s_has_tester_received_provider_shutdown_callback(void *user_data) {
111 (void)user_data;
112
113 return s_tester.provider_shutdown_callback_count;
114 }
115
116 static void s_aws_wait_for_provider_shutdown_callback(void) {
117 aws_mutex_lock(&s_tester.lock);
118 aws_condition_variable_wait_pred(
119 &s_tester.signal, &s_tester.lock, s_has_tester_received_provider_shutdown_callback, NULL);
120 aws_mutex_unlock(&s_tester.lock);
121 }
122
123 static struct aws_http_connection_manager *s_aws_http_connection_manager_new_mock(
124 struct aws_allocator *allocator,
125 const struct aws_http_connection_manager_options *options) {
126
127 if (s_tester.proxy_config != NULL) {
128 AWS_FATAL_ASSERT(options->proxy_ev_settings->env_var_type == s_tester.proxy_config->env_var_type);
129 AWS_FATAL_ASSERT(options->proxy_ev_settings->connection_type == s_tester.proxy_config->connection_type);
130 AWS_FATAL_ASSERT(options->proxy_ev_settings->tls_options == s_tester.proxy_config->tls_options);
131 }
132
133 /* copy the shutdown callback */
134 struct aws_shutdown_callback_options *shutdown_callback =
135 aws_mem_calloc(allocator, 1, sizeof(struct aws_shutdown_callback_options));
136 shutdown_callback->shutdown_callback_fn = options->shutdown_complete_callback;
137 shutdown_callback->shutdown_callback_user_data = options->shutdown_complete_user_data;
138 return (struct aws_http_connection_manager *)shutdown_callback;
139 }
140
141 static void s_aws_http_connection_manager_release_mock(struct aws_http_connection_manager *manager) {
142 (void)manager;
143 /* invoke the shutdown callback */
144 struct aws_shutdown_callback_options *shutdown_callback = (struct aws_shutdown_callback_options *)manager;
145 if (shutdown_callback->shutdown_callback_fn != NULL) {
146 shutdown_callback->shutdown_callback_fn(shutdown_callback->shutdown_callback_user_data);
147 }
148 aws_mem_release(s_tester.allocator, shutdown_callback);
149 s_on_connection_manager_shutdown_complete(NULL);
150 }
151
152 static void s_aws_http_connection_manager_acquire_connection_mock(
153 struct aws_http_connection_manager *manager,
154 aws_http_connection_manager_on_connection_setup_fn *callback,
155 void *user_data) {
156
157 (void)manager;
158 (void)callback;
159 (void)user_data;
160
161 if (!s_tester.fail_connection) {
162 callback((struct aws_http_connection *)1, AWS_OP_SUCCESS, user_data);
163 } else {
164 aws_raise_error(AWS_ERROR_HTTP_UNKNOWN);
165 callback(NULL, AWS_OP_ERR, user_data);
166 }
167 }
168
169 static int s_aws_http_connection_manager_release_connection_mock(
170 struct aws_http_connection_manager *manager,
171 struct aws_http_connection *connection) {
172
173 (void)manager;
174 (void)connection;
175
176 return AWS_OP_SUCCESS;
177 }
178
179 static void s_invoke_mock_request_callbacks(
180 const struct aws_http_make_request_options *options,
181 bool is_request_successful) {
182
183 struct aws_http_header headers[1];
184 AWS_ZERO_ARRAY(headers);
185
186 headers[0].name = aws_byte_cursor_from_c_str("some-header");
187 headers[0].value = aws_byte_cursor_from_c_str("value");
188
189 if (options->on_response_headers) {
190 options->on_response_headers(
191 (struct aws_http_stream *)1, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data);
192 }
193
194 if (options->on_response_header_block_done) {
195 options->on_response_header_block_done((struct aws_http_stream *)1, true, options->user_data);
196 }
197
198 struct aws_byte_cursor data_callback_cur;
199 aws_array_list_get_at(&s_tester.response_data_callbacks, &data_callback_cur, s_tester.num_request - 1);
200 options->on_response_body((struct aws_http_stream *)1, &data_callback_cur, options->user_data);
201
202 options->on_complete(
203 (struct aws_http_stream *)1,
204 is_request_successful ? AWS_ERROR_SUCCESS : AWS_ERROR_HTTP_UNKNOWN,
205 options->user_data);
206 }
207
208 static struct aws_http_stream *s_aws_http_connection_make_request_mock(
209 struct aws_http_connection *client_connection,
210 const struct aws_http_make_request_options *options) {
211
212 (void)client_connection;
213 struct aws_mock_http_request *mocked_request = &s_tester.mocked_requests[s_tester.num_request++];
214 AWS_ZERO_STRUCT(*mocked_request);
215 struct aws_byte_cursor path;
216 AWS_ZERO_STRUCT(path);
217 aws_http_message_get_request_path(options->request, &path);
218 aws_byte_buf_clean_up(&mocked_request->path);
219 aws_byte_buf_init_copy_from_cursor(&mocked_request->path, s_tester.allocator, path);
220
221 struct aws_byte_cursor method;
222 AWS_ZERO_STRUCT(method);
223 aws_http_message_get_request_method(options->request, &method);
224 aws_byte_buf_clean_up(&mocked_request->method);
225 aws_byte_buf_init_copy_from_cursor(&mocked_request->method, s_tester.allocator, method);
226
227 size_t header_count = aws_http_message_get_header_count(options->request);
228
229 for (size_t i = 0; i < header_count; ++i) {
230 struct aws_http_header header;
231 AWS_ZERO_STRUCT(header);
232
233 aws_http_message_get_header(options->request, &header, i);
234
235 if (aws_byte_cursor_eq_c_str_ignore_case(&header.name, "host")) {
236 aws_byte_buf_clean_up(&mocked_request->host_header);
237 aws_byte_buf_init_copy_from_cursor(&mocked_request->host_header, s_tester.allocator, header.value);
238 }
239
240 if (aws_byte_cursor_eq_c_str_ignore_case(&header.name, "authorization")) {
241 mocked_request->had_auth_header = true;
242 aws_byte_buf_init_copy_from_cursor(&mocked_request->auth_header, s_tester.allocator, header.value);
243 }
244 }
245
246 struct aws_input_stream *input_stream = aws_http_message_get_body_stream(options->request);
247 int64_t body_len = 0;
248 if (input_stream != NULL) {
249 aws_input_stream_get_length(input_stream, &body_len);
250 aws_byte_buf_clean_up(&mocked_request->body);
251 aws_byte_buf_init(&mocked_request->body, s_tester.allocator, (size_t)body_len);
252 aws_input_stream_read(input_stream, &mocked_request->body);
253 }
254
255 if (s_tester.fail_operations) {
256 s_tester.fail_operations--;
257 mocked_request->response_code = s_tester.mock_failure_code;
258 } else {
259 mocked_request->response_code = s_tester.mock_response_code;
260 }
261
262 return (struct aws_http_stream *)options;
263 }
264
265 static int s_aws_http_stream_get_incoming_response_status_mock(
266 const struct aws_http_stream *stream,
267 int *out_status_code) {
268 (void)stream;
269
270 *out_status_code = s_tester.mocked_requests[s_tester.num_request - 1].response_code;
271
272 return AWS_OP_SUCCESS;
273 }
274
275 static int s_aws_http_stream_activate_mock(struct aws_http_stream *stream) {
276 struct aws_http_make_request_options *options = (struct aws_http_make_request_options *)stream;
277 bool fail_request = s_tester.fail_operations;
278 s_invoke_mock_request_callbacks(options, !fail_request);
279 return AWS_OP_SUCCESS;
280 }
281
282 static void s_aws_http_stream_release_mock(struct aws_http_stream *stream) {
283 (void)stream;
284 }
285
286 static void s_aws_http_connection_close_mock(struct aws_http_connection *connection) {
287 (void)connection;
288 }
289
290 static struct aws_auth_http_system_vtable s_mock_function_table = {
291 .aws_http_connection_manager_new = s_aws_http_connection_manager_new_mock,
292 .aws_http_connection_manager_release = s_aws_http_connection_manager_release_mock,
293 .aws_http_connection_manager_acquire_connection = s_aws_http_connection_manager_acquire_connection_mock,
294 .aws_http_connection_manager_release_connection = s_aws_http_connection_manager_release_connection_mock,
295 .aws_http_connection_make_request = s_aws_http_connection_make_request_mock,
296 .aws_http_stream_activate = s_aws_http_stream_activate_mock,
297 .aws_http_stream_get_incoming_response_status = s_aws_http_stream_get_incoming_response_status_mock,
298 .aws_http_stream_release = s_aws_http_stream_release_mock,
299 .aws_http_connection_close = s_aws_http_connection_close_mock};
300
301 AWS_STATIC_STRING_FROM_LITERAL(s_config_file_path_env_variable_name, "AWS_CONFIG_FILE");
302 AWS_STATIC_STRING_FROM_LITERAL(s_region_env_variable_name, "AWS_REGION");
303 AWS_STATIC_STRING_FROM_LITERAL(s_region_default_env_variable_name, "AWS_DEFAULT_REGION");
304
305 static int s_aws_sts_tester_init(struct aws_allocator *allocator) {
306 AWS_ZERO_STRUCT(s_tester);
307 s_tester.allocator = allocator;
308 s_tester.expected_connection_manager_shutdown_callback_count = 1;
309 aws_auth_library_init(allocator);
310
311 if (aws_mutex_init(&s_tester.lock)) {
312 return AWS_OP_ERR;
313 }
314
315 if (aws_condition_variable_init(&s_tester.signal)) {
316 return AWS_OP_ERR;
317 }
318
319 s_tester.el_group = aws_event_loop_group_new_default(allocator, 0, NULL);
320
321 struct aws_host_resolver_default_options resolver_options = {
322 .el_group = s_tester.el_group,
323 .max_entries = 8,
324 };
325 s_tester.resolver = aws_host_resolver_new_default(allocator, &resolver_options);
326
327 struct aws_client_bootstrap_options bootstrap_options = {
328 .event_loop_group = s_tester.el_group,
329 .host_resolver = s_tester.resolver,
330 };
331 s_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
332
333 struct aws_tls_ctx_options tls_options;
334 aws_tls_ctx_options_init_default_client(&tls_options, allocator);
335 s_tester.tls_ctx = aws_tls_client_ctx_new(allocator, &tls_options);
336 ASSERT_NOT_NULL(s_tester.tls_ctx);
337 aws_tls_ctx_options_clean_up(&tls_options);
338
339 if (aws_array_list_init_dynamic(&s_tester.response_data_callbacks, allocator, 10, sizeof(struct aws_byte_cursor))) {
340 return AWS_OP_ERR;
341 }
342
343 /*
344 * set the environment so that it doesn't mess with tests
345 */
346 aws_unset_environment_value(s_region_env_variable_name);
347 aws_unset_environment_value(s_region_default_env_variable_name);
348 struct aws_string *cur_directory = aws_string_new_from_c_str(allocator, ".");
349 aws_set_environment_value(s_config_file_path_env_variable_name, cur_directory);
350 aws_string_destroy(cur_directory);
351
352 return AWS_OP_SUCCESS;
353 }
354
355 static void s_cleanup_creds_callback_data(void) {
356 aws_mutex_lock(&s_tester.lock);
357 s_tester.has_received_credentials_callback = false;
358
359 if (s_tester.credentials) {
360 aws_credentials_release(s_tester.credentials);
361 s_tester.credentials = NULL;
362 }
363
364 for (int i = 0; i < MAX_REQUEST; i++) {
365 aws_byte_buf_clean_up(&s_tester.mocked_requests[i].path);
366 aws_byte_buf_clean_up(&s_tester.mocked_requests[i].method);
367 aws_byte_buf_clean_up(&s_tester.mocked_requests[i].host_header);
368 aws_byte_buf_clean_up(&s_tester.mocked_requests[i].auth_header);
369 aws_byte_buf_clean_up(&s_tester.mocked_requests[i].body);
370 }
371
372 aws_mutex_unlock(&s_tester.lock);
373 }
374
375 static int s_aws_sts_tester_cleanup(void) {
376
377 s_cleanup_creds_callback_data();
378
379 aws_condition_variable_clean_up(&s_tester.signal);
380 aws_mutex_clean_up(&s_tester.lock);
381
382 aws_array_list_clean_up(&s_tester.response_data_callbacks);
383
384 aws_client_bootstrap_release(s_tester.bootstrap);
385 aws_host_resolver_release(s_tester.resolver);
386 aws_event_loop_group_release(s_tester.el_group);
387 aws_tls_ctx_release(s_tester.tls_ctx);
388
389 aws_auth_library_clean_up();
390
391 return AWS_OP_SUCCESS;
392 }
393
394 static bool s_has_tester_received_credentials_callback(void *user_data) {
395 (void)user_data;
396
397 return s_tester.has_received_credentials_callback;
398 }
399
400 static void s_aws_wait_for_credentials_result(void) {
401 aws_mutex_lock(&s_tester.lock);
402 aws_condition_variable_wait_pred(
403 &s_tester.signal, &s_tester.lock, s_has_tester_received_credentials_callback, NULL);
404 aws_mutex_unlock(&s_tester.lock);
405 }
406
407 static void s_get_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
408 (void)user_data;
409
410 aws_mutex_lock(&s_tester.lock);
411 s_tester.has_received_credentials_callback = true;
412 s_tester.error_code = error_code;
413 s_tester.credentials = credentials;
414 aws_credentials_acquire(credentials);
415 aws_condition_variable_notify_one(&s_tester.signal);
416 aws_mutex_unlock(&s_tester.lock);
417 }
418
419 static struct aws_byte_cursor s_access_key_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("accessKey12345");
420 static struct aws_byte_cursor s_secret_key_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("secretKey12345");
421 static struct aws_byte_cursor s_session_token_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("sessionToken123456789");
422 static struct aws_byte_cursor s_role_arn_cur =
423 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("arn:aws:iam::67895:role/test_role");
424 static struct aws_byte_cursor s_session_name_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test_session");
425 static struct aws_byte_cursor s_external_id_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("externalId123_+=,.@:\\/-");
426
427 static struct aws_byte_cursor s_success_creds_doc =
428 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<AssumeRoleResponse xmlns=\"who cares\">\n"
429 " <AssumeRoleResult>\n"
430 " <fuzzMePlz>\n"
431 " </fuzzMePlz>\n"
432 " <Credentials>\n"
433 " <AccessKeyId>accessKeyIdResp</AccessKeyId>\n"
434 " <SecretAccessKey>secretKeyResp</SecretAccessKey>\n"
435 " <SessionToken>sessionTokenResp</SessionToken>\n"
436 " </Credentials>\n"
437 " <AssumedRoleUser>\n"
438 " <AssumedRoleId>assumeRoleIdResp</AssumedRoleId>\n"
439 " <Arn>arn:aws:sts::123456789012:assumed-role</Arn>\n"
440 " </AssumedRoleUser>\n"
441 " ... more stuff we don't care about\n"
442 " </AssumeRoleResult>\n"
443 "</AssumeRoleResponse>");
444
445 static struct aws_byte_cursor s_expected_payload =
446 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67895%"
447 "3Arole%2Ftest_role&RoleSessionName=test_session&DurationSeconds=900");
448
449 static struct aws_byte_cursor s_expected_payload_with_external_id =
450 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15"
451 "&Action=AssumeRole"
452 "&RoleArn=arn%3Aaws%3Aiam%3A%3A67895%3Arole%2Ftest_role"
453 "&RoleSessionName=test_session"
454 "&ExternalId=externalId123_%2B%3D%2C.%40%3A%5C%2F-"
455 "&DurationSeconds=900");
456
457 AWS_STATIC_STRING_FROM_LITERAL(s_access_key_id_response, "accessKeyIdResp");
458 AWS_STATIC_STRING_FROM_LITERAL(s_secret_access_key_response, "secretKeyResp");
459 AWS_STATIC_STRING_FROM_LITERAL(s_session_token_response, "sessionTokenResp");
460 AWS_STATIC_STRING_FROM_LITERAL(s_account_id_response, "123456789012");
461
462 static int s_verify_credentials(struct aws_credentials *credentials) {
463 ASSERT_NOT_NULL(credentials);
464 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(credentials), s_access_key_id_response);
465 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_secret_access_key(credentials), s_secret_access_key_response);
466 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_session_token(credentials), s_session_token_response);
467 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_account_id(credentials), s_account_id_response);
468
469 return AWS_OP_SUCCESS;
470 }
471
472 static int s_credentials_provider_sts_direct_config_succeeds_fn(struct aws_allocator *allocator, void *ctx) {
473 (void)ctx;
474
475 s_aws_sts_tester_init(allocator);
476
477 struct aws_credentials_provider_static_options static_options = {
478 .access_key_id = s_access_key_cur,
479 .secret_access_key = s_secret_key_cur,
480 .session_token = s_session_token_cur,
481 };
482 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
483
484 struct aws_credentials_provider_sts_options options = {
485 .creds_provider = static_provider,
486 .bootstrap = s_tester.bootstrap,
487 .tls_ctx = s_tester.tls_ctx,
488 .role_arn = s_role_arn_cur,
489 .session_name = s_session_name_cur,
490 .duration_seconds = 0,
491 .function_table = &s_mock_function_table,
492 .system_clock_fn = mock_aws_get_system_time,
493 };
494
495 mock_aws_set_system_time(0);
496
497 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
498 s_tester.mock_response_code = 200;
499
500 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
501
502 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
503
504 s_aws_wait_for_credentials_result();
505
506 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
507 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
508
509 const char *expected_method = "POST";
510 ASSERT_BIN_ARRAYS_EQUALS(
511 expected_method,
512 strlen(expected_method),
513 s_tester.mocked_requests[0].method.buffer,
514 s_tester.mocked_requests[0].method.len);
515
516 const char *expected_path = "/";
517 ASSERT_BIN_ARRAYS_EQUALS(
518 expected_path,
519 strlen(expected_path),
520 s_tester.mocked_requests[0].path.buffer,
521 s_tester.mocked_requests[0].path.len);
522
523 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
524 const char *expected_auth_header =
525 "AWS4-HMAC-SHA256 Credential=accessKey12345/19700101/us-east-1/sts/aws4_request, "
526 "SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-security-token, "
527 "Signature=06e941853a2cc010ed8ac7072754e0089be8aae59e319924fb0105b20dda77cd";
528 ASSERT_BIN_ARRAYS_EQUALS(
529 expected_auth_header,
530 strlen(expected_auth_header),
531 s_tester.mocked_requests[0].auth_header.buffer,
532 s_tester.mocked_requests[0].auth_header.len);
533
534 const char *expected_host_header = "sts.amazonaws.com";
535 ASSERT_BIN_ARRAYS_EQUALS(
536 expected_host_header,
537 strlen(expected_host_header),
538 s_tester.mocked_requests[0].host_header.buffer,
539 s_tester.mocked_requests[0].host_header.len);
540
541 ASSERT_BIN_ARRAYS_EQUALS(
542 s_expected_payload.ptr,
543 s_expected_payload.len,
544 s_tester.mocked_requests[0].body.buffer,
545 s_tester.mocked_requests[0].body.len);
546
547 aws_credentials_provider_release(sts_provider);
548 s_aws_wait_for_connection_manager_shutdown_callback();
549 aws_credentials_provider_release(static_provider);
550 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
551
552 return AWS_OP_SUCCESS;
553 }
554
555 AWS_TEST_CASE(credentials_provider_sts_direct_config_succeeds, s_credentials_provider_sts_direct_config_succeeds_fn)
556
557 static int s_credentials_provider_sts_direct_config_with_external_id_succeeds_fn(
558 struct aws_allocator *allocator,
559 void *ctx) {
560 (void)ctx;
561
562 s_aws_sts_tester_init(allocator);
563
564 struct aws_credentials_provider_static_options static_options = {
565 .access_key_id = s_access_key_cur,
566 .secret_access_key = s_secret_key_cur,
567 .session_token = s_session_token_cur,
568 };
569 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
570
571 struct aws_credentials_provider_sts_options options = {
572 .creds_provider = static_provider,
573 .bootstrap = s_tester.bootstrap,
574 .tls_ctx = s_tester.tls_ctx,
575 .role_arn = s_role_arn_cur,
576 .session_name = s_session_name_cur,
577 .external_id = s_external_id_cur,
578 .duration_seconds = 0,
579 .function_table = &s_mock_function_table,
580 .system_clock_fn = mock_aws_get_system_time,
581 };
582
583 mock_aws_set_system_time(0);
584
585 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
586 s_tester.mock_response_code = 200;
587
588 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
589
590 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
591
592 s_aws_wait_for_credentials_result();
593
594 ASSERT_BIN_ARRAYS_EQUALS(
595 s_expected_payload_with_external_id.ptr,
596 s_expected_payload_with_external_id.len,
597 s_tester.mocked_requests[0].body.buffer,
598 s_tester.mocked_requests[0].body.len);
599
600 aws_credentials_provider_release(sts_provider);
601 s_aws_wait_for_connection_manager_shutdown_callback();
602 aws_credentials_provider_release(static_provider);
603 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
604
605 return AWS_OP_SUCCESS;
606 }
607
608 AWS_TEST_CASE(
609 credentials_provider_sts_direct_config_with_external_id_succeeds,
610 s_credentials_provider_sts_direct_config_with_external_id_succeeds_fn)
611
612 static int s_credentials_provider_sts_direct_config_with_region_succeeds_fn(
613 struct aws_allocator *allocator,
614 void *ctx) {
615 (void)ctx;
616
617 s_aws_sts_tester_init(allocator);
618 /* verify that it picks the s_region_env_variable_name if both are set */
619 struct aws_string *region = aws_string_new_from_c_str(allocator, "us-west-2");
620 struct aws_string *default_region = aws_string_new_from_c_str(allocator, "us-east-1");
621 aws_set_environment_value(s_region_env_variable_name, region);
622 aws_set_environment_value(s_region_default_env_variable_name, default_region);
623 aws_string_destroy(default_region);
624 aws_string_destroy(region);
625
626 struct aws_credentials_provider_static_options static_options = {
627 .access_key_id = s_access_key_cur,
628 .secret_access_key = s_secret_key_cur,
629 .session_token = s_session_token_cur,
630 };
631 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
632
633 struct aws_credentials_provider_sts_options options = {
634 .creds_provider = static_provider,
635 .bootstrap = s_tester.bootstrap,
636 .tls_ctx = s_tester.tls_ctx,
637 .role_arn = s_role_arn_cur,
638 .session_name = s_session_name_cur,
639 .duration_seconds = 0,
640 .function_table = &s_mock_function_table,
641 .system_clock_fn = mock_aws_get_system_time,
642 };
643
644 mock_aws_set_system_time(0);
645
646 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
647 s_tester.mock_response_code = 200;
648
649 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
650
651 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
652
653 s_aws_wait_for_credentials_result();
654
655 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
656 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
657
658 const char *expected_method = "POST";
659 ASSERT_BIN_ARRAYS_EQUALS(
660 expected_method,
661 strlen(expected_method),
662 s_tester.mocked_requests[0].method.buffer,
663 s_tester.mocked_requests[0].method.len);
664
665 const char *expected_path = "/";
666 ASSERT_BIN_ARRAYS_EQUALS(
667 expected_path,
668 strlen(expected_path),
669 s_tester.mocked_requests[0].path.buffer,
670 s_tester.mocked_requests[0].path.len);
671
672 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
673
674 const char *expected_host_header = "sts.us-west-2.amazonaws.com";
675 ASSERT_BIN_ARRAYS_EQUALS(
676 expected_host_header,
677 strlen(expected_host_header),
678 s_tester.mocked_requests[0].host_header.buffer,
679 s_tester.mocked_requests[0].host_header.len);
680
681 const char *expected_auth_header =
682 "AWS4-HMAC-SHA256 Credential=accessKey12345/19700101/us-west-2/sts/aws4_request, "
683 "SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-security-token, "
684 "Signature=a4a164e00acb40795ea491092dfcfbbfcda817b6b0bb5d06d328f535ec163ac6";
685 ASSERT_BIN_ARRAYS_EQUALS(
686 expected_auth_header,
687 strlen(expected_auth_header),
688 s_tester.mocked_requests[0].auth_header.buffer,
689 s_tester.mocked_requests[0].auth_header.len);
690
691 ASSERT_BIN_ARRAYS_EQUALS(
692 s_expected_payload.ptr,
693 s_expected_payload.len,
694 s_tester.mocked_requests[0].body.buffer,
695 s_tester.mocked_requests[0].body.len);
696
697 aws_credentials_provider_release(sts_provider);
698 s_aws_wait_for_connection_manager_shutdown_callback();
699 aws_credentials_provider_release(static_provider);
700 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
701
702 return AWS_OP_SUCCESS;
703 }
704
705 AWS_TEST_CASE(
706 credentials_provider_sts_direct_config_with_region_succeeds,
707 s_credentials_provider_sts_direct_config_with_region_succeeds_fn)
708
709 static int s_credentials_provider_sts_direct_config_with_default_region_succeeds_fn(
710 struct aws_allocator *allocator,
711 void *ctx) {
712 (void)ctx;
713
714 s_aws_sts_tester_init(allocator);
715 struct aws_string *default_region = aws_string_new_from_c_str(allocator, "us-east-1");
716 aws_set_environment_value(s_region_default_env_variable_name, default_region);
717 aws_string_destroy(default_region);
718
719 struct aws_credentials_provider_static_options static_options = {
720 .access_key_id = s_access_key_cur,
721 .secret_access_key = s_secret_key_cur,
722 .session_token = s_session_token_cur,
723 };
724 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
725
726 struct aws_credentials_provider_sts_options options = {
727 .creds_provider = static_provider,
728 .bootstrap = s_tester.bootstrap,
729 .tls_ctx = s_tester.tls_ctx,
730 .role_arn = s_role_arn_cur,
731 .session_name = s_session_name_cur,
732 .duration_seconds = 0,
733 .function_table = &s_mock_function_table,
734 .system_clock_fn = mock_aws_get_system_time,
735 };
736
737 mock_aws_set_system_time(0);
738
739 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
740 s_tester.mock_response_code = 200;
741
742 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
743
744 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
745
746 s_aws_wait_for_credentials_result();
747
748 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
749 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
750
751 const char *expected_method = "POST";
752 ASSERT_BIN_ARRAYS_EQUALS(
753 expected_method,
754 strlen(expected_method),
755 s_tester.mocked_requests[0].method.buffer,
756 s_tester.mocked_requests[0].method.len);
757
758 const char *expected_path = "/";
759 ASSERT_BIN_ARRAYS_EQUALS(
760 expected_path,
761 strlen(expected_path),
762 s_tester.mocked_requests[0].path.buffer,
763 s_tester.mocked_requests[0].path.len);
764
765 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
766
767 const char *expected_host_header = "sts.us-east-1.amazonaws.com";
768 ASSERT_BIN_ARRAYS_EQUALS(
769 expected_host_header,
770 strlen(expected_host_header),
771 s_tester.mocked_requests[0].host_header.buffer,
772 s_tester.mocked_requests[0].host_header.len);
773
774 ASSERT_BIN_ARRAYS_EQUALS(
775 s_expected_payload.ptr,
776 s_expected_payload.len,
777 s_tester.mocked_requests[0].body.buffer,
778 s_tester.mocked_requests[0].body.len);
779
780 aws_credentials_provider_release(sts_provider);
781 s_aws_wait_for_connection_manager_shutdown_callback();
782 aws_credentials_provider_release(static_provider);
783 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
784
785 return AWS_OP_SUCCESS;
786 }
787
788 AWS_TEST_CASE(
789 credentials_provider_sts_direct_config_with_default_region_succeeds,
790 s_credentials_provider_sts_direct_config_with_default_region_succeeds_fn)
791
792 static int s_credentials_provider_sts_direct_config_with_region_from_config_succeeds_fn(
793 struct aws_allocator *allocator,
794 void *ctx) {
795 (void)ctx;
796
797 s_aws_sts_tester_init(allocator);
798
799 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, "[profile myProfile]\nregion=cn-north-1");
800
801 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
802
803 ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents));
804 aws_string_destroy(config_contents);
805
806 struct aws_credentials_provider_static_options static_options = {
807 .access_key_id = s_access_key_cur,
808 .secret_access_key = s_secret_key_cur,
809 .session_token = s_session_token_cur,
810 };
811 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
812
813 struct aws_credentials_provider_sts_options options = {
814 .creds_provider = static_provider,
815 .bootstrap = s_tester.bootstrap,
816 .tls_ctx = s_tester.tls_ctx,
817 .role_arn = s_role_arn_cur,
818 .session_name = s_session_name_cur,
819 .duration_seconds = 0,
820 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
821 .profile_name_override = aws_byte_cursor_from_c_str("myProfile"),
822 .function_table = &s_mock_function_table,
823 .system_clock_fn = mock_aws_get_system_time,
824 };
825
826 mock_aws_set_system_time(0);
827
828 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
829 s_tester.mock_response_code = 200;
830
831 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
832
833 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
834
835 s_aws_wait_for_credentials_result();
836
837 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
838 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
839
840 const char *expected_method = "POST";
841 ASSERT_BIN_ARRAYS_EQUALS(
842 expected_method,
843 strlen(expected_method),
844 s_tester.mocked_requests[0].method.buffer,
845 s_tester.mocked_requests[0].method.len);
846
847 const char *expected_path = "/";
848 ASSERT_BIN_ARRAYS_EQUALS(
849 expected_path,
850 strlen(expected_path),
851 s_tester.mocked_requests[0].path.buffer,
852 s_tester.mocked_requests[0].path.len);
853
854 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
855 const char *expected_host_header = "sts.cn-north-1.amazonaws.com.cn";
856 ASSERT_BIN_ARRAYS_EQUALS(
857 expected_host_header,
858 strlen(expected_host_header),
859 s_tester.mocked_requests[0].host_header.buffer,
860 s_tester.mocked_requests[0].host_header.len);
861
862 ASSERT_BIN_ARRAYS_EQUALS(
863 s_expected_payload.ptr,
864 s_expected_payload.len,
865 s_tester.mocked_requests[0].body.buffer,
866 s_tester.mocked_requests[0].body.len);
867
868 aws_credentials_provider_release(sts_provider);
869 s_aws_wait_for_connection_manager_shutdown_callback();
870 aws_credentials_provider_release(static_provider);
871 aws_file_delete(config_file_str);
872 aws_string_destroy(config_file_str);
873 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
874
875 return AWS_OP_SUCCESS;
876 }
877
878 AWS_TEST_CASE(
879 credentials_provider_sts_direct_config_with_region_from_config_succeeds,
880 s_credentials_provider_sts_direct_config_with_region_from_config_succeeds_fn)
881
882 static int s_credentials_provider_sts_direct_config_succeeds_after_retry_fn(
883 struct aws_allocator *allocator,
884 void *ctx) {
885 (void)ctx;
886
887 s_aws_sts_tester_init(allocator);
888
889 struct aws_credentials_provider_static_options static_options = {
890 .access_key_id = s_access_key_cur,
891 .secret_access_key = s_secret_key_cur,
892 .session_token = s_session_token_cur,
893 };
894 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
895
896 struct aws_credentials_provider_sts_options options = {
897 .creds_provider = static_provider,
898 .bootstrap = s_tester.bootstrap,
899 .tls_ctx = s_tester.tls_ctx,
900 .role_arn = s_role_arn_cur,
901 .session_name = s_session_name_cur,
902 .duration_seconds = 0,
903 .function_table = &s_mock_function_table,
904 .system_clock_fn = mock_aws_get_system_time,
905 };
906
907 mock_aws_set_system_time(0);
908 s_tester.mock_response_code = 200;
909 s_tester.mock_failure_code = 429;
910 s_tester.fail_operations = 2;
911 int expected_num_requests = 3;
912 for (int i = 0; i < expected_num_requests; i++) {
913 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
914 }
915
916 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
917
918 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
919
920 s_aws_wait_for_credentials_result();
921
922 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
923 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
924
925 const char *expected_method = "POST";
926 ASSERT_BIN_ARRAYS_EQUALS(
927 expected_method,
928 strlen(expected_method),
929 s_tester.mocked_requests[0].method.buffer,
930 s_tester.mocked_requests[0].method.len);
931
932 const char *expected_path = "/";
933 ASSERT_BIN_ARRAYS_EQUALS(
934 expected_path,
935 strlen(expected_path),
936 s_tester.mocked_requests[0].path.buffer,
937 s_tester.mocked_requests[0].path.len);
938
939 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
940
941 const char *expected_host_header = "sts.amazonaws.com";
942 ASSERT_BIN_ARRAYS_EQUALS(
943 expected_host_header,
944 strlen(expected_host_header),
945 s_tester.mocked_requests[0].host_header.buffer,
946 s_tester.mocked_requests[0].host_header.len);
947
948 ASSERT_BIN_ARRAYS_EQUALS(
949 s_expected_payload.ptr,
950 s_expected_payload.len,
951 s_tester.mocked_requests[0].body.buffer,
952 s_tester.mocked_requests[0].body.len);
953
954 aws_credentials_provider_release(sts_provider);
955 s_aws_wait_for_connection_manager_shutdown_callback();
956 aws_credentials_provider_release(static_provider);
957 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
958
959 return AWS_OP_SUCCESS;
960 }
961
962 AWS_TEST_CASE(
963 credentials_provider_sts_direct_config_succeeds_after_retry,
964 s_credentials_provider_sts_direct_config_succeeds_after_retry_fn)
965
966 static struct aws_byte_cursor s_malformed_creds_doc =
967 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<AssumeRoleResponse xmlns=\"who cares\">\n"
968 " <AssumeRoleResult>\n"
969 " <AssumeRoleUser>\n"
970 " <Credentials>\n"
971 " <AccessKeyId>accessKeyIdResp</AccessKeyId>\n"
972 " ");
973
974 static int s_credentials_provider_sts_direct_config_invalid_doc_fn(struct aws_allocator *allocator, void *ctx) {
975 (void)ctx;
976
977 s_aws_sts_tester_init(allocator);
978
979 struct aws_credentials_provider_static_options static_options = {
980 .access_key_id = s_access_key_cur,
981 .secret_access_key = s_secret_key_cur,
982 .session_token = s_session_token_cur,
983 };
984 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
985
986 struct aws_credentials_provider_sts_options options = {
987 .creds_provider = static_provider,
988 .bootstrap = s_tester.bootstrap,
989 .tls_ctx = s_tester.tls_ctx,
990 .role_arn = s_role_arn_cur,
991 .session_name = s_session_name_cur,
992 .duration_seconds = 0,
993 .function_table = &s_mock_function_table,
994 .system_clock_fn = mock_aws_get_system_time,
995 };
996
997 mock_aws_set_system_time(0);
998 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_malformed_creds_doc);
999 s_tester.mock_response_code = 200;
1000
1001 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
1002
1003 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
1004
1005 s_aws_wait_for_credentials_result();
1006
1007 ASSERT_NULL(s_tester.credentials);
1008
1009 const char *expected_method = "POST";
1010 ASSERT_BIN_ARRAYS_EQUALS(
1011 expected_method,
1012 strlen(expected_method),
1013 s_tester.mocked_requests[0].method.buffer,
1014 s_tester.mocked_requests[0].method.len);
1015
1016 const char *expected_path = "/";
1017 ASSERT_BIN_ARRAYS_EQUALS(
1018 expected_path,
1019 strlen(expected_path),
1020 s_tester.mocked_requests[0].path.buffer,
1021 s_tester.mocked_requests[0].path.len);
1022
1023 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
1024
1025 const char *expected_host_header = "sts.amazonaws.com";
1026 ASSERT_BIN_ARRAYS_EQUALS(
1027 expected_host_header,
1028 strlen(expected_host_header),
1029 s_tester.mocked_requests[0].host_header.buffer,
1030 s_tester.mocked_requests[0].host_header.len);
1031
1032 ASSERT_BIN_ARRAYS_EQUALS(
1033 s_expected_payload.ptr,
1034 s_expected_payload.len,
1035 s_tester.mocked_requests[0].body.buffer,
1036 s_tester.mocked_requests[0].body.len);
1037
1038 aws_credentials_provider_release(sts_provider);
1039 s_aws_wait_for_connection_manager_shutdown_callback();
1040 aws_credentials_provider_release(static_provider);
1041 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1042
1043 return AWS_OP_SUCCESS;
1044 }
1045
1046 AWS_TEST_CASE(
1047 credentials_provider_sts_direct_config_invalid_doc,
1048 s_credentials_provider_sts_direct_config_invalid_doc_fn)
1049
1050 static int s_credentials_provider_sts_direct_config_connection_failed_fn(struct aws_allocator *allocator, void *ctx) {
1051 (void)ctx;
1052
1053 s_aws_sts_tester_init(allocator);
1054
1055 struct aws_credentials_provider_static_options static_options = {
1056 .access_key_id = s_access_key_cur,
1057 .secret_access_key = s_secret_key_cur,
1058 .session_token = s_session_token_cur,
1059 };
1060 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
1061
1062 struct aws_credentials_provider_sts_options options = {
1063 .creds_provider = static_provider,
1064 .bootstrap = s_tester.bootstrap,
1065 .tls_ctx = s_tester.tls_ctx,
1066 .role_arn = s_role_arn_cur,
1067 .session_name = s_session_name_cur,
1068 .duration_seconds = 0,
1069 .function_table = &s_mock_function_table,
1070 .system_clock_fn = mock_aws_get_system_time,
1071 };
1072
1073 mock_aws_set_system_time(0);
1074
1075 s_tester.fail_connection = true;
1076
1077 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
1078
1079 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
1080
1081 s_aws_wait_for_credentials_result();
1082
1083 ASSERT_NULL(s_tester.credentials);
1084
1085 aws_credentials_provider_release(sts_provider);
1086 s_aws_wait_for_connection_manager_shutdown_callback();
1087 aws_credentials_provider_release(static_provider);
1088 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1089
1090 return AWS_OP_SUCCESS;
1091 }
1092
1093 AWS_TEST_CASE(
1094 credentials_provider_sts_direct_config_connection_failed,
1095 s_credentials_provider_sts_direct_config_connection_failed_fn)
1096
1097 static int s_credentials_provider_sts_direct_config_service_fails_fn(struct aws_allocator *allocator, void *ctx) {
1098 (void)ctx;
1099
1100 s_aws_sts_tester_init(allocator);
1101
1102 struct aws_credentials_provider_static_options static_options = {
1103 .access_key_id = s_access_key_cur,
1104 .secret_access_key = s_secret_key_cur,
1105 .session_token = s_session_token_cur,
1106 };
1107 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
1108
1109 struct aws_credentials_provider_sts_options options = {
1110 .creds_provider = static_provider,
1111 .bootstrap = s_tester.bootstrap,
1112 .tls_ctx = s_tester.tls_ctx,
1113 .role_arn = s_role_arn_cur,
1114 .session_name = s_session_name_cur,
1115 .duration_seconds = 0,
1116 .function_table = &s_mock_function_table,
1117 .system_clock_fn = mock_aws_get_system_time,
1118 };
1119
1120 mock_aws_set_system_time(0);
1121 s_tester.mock_response_code = 529;
1122 int expected_num_requests = 4;
1123 for (int i = 0; i < expected_num_requests; i++) {
1124 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_malformed_creds_doc);
1125 }
1126
1127 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
1128
1129 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
1130
1131 s_aws_wait_for_credentials_result();
1132
1133 ASSERT_NULL(s_tester.credentials);
1134
1135 aws_credentials_provider_release(sts_provider);
1136 s_aws_wait_for_connection_manager_shutdown_callback();
1137 aws_credentials_provider_release(static_provider);
1138 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1139
1140 return AWS_OP_SUCCESS;
1141 }
1142
1143 AWS_TEST_CASE(
1144 credentials_provider_sts_direct_config_service_fails,
1145 s_credentials_provider_sts_direct_config_service_fails_fn)
1146
1147 static const char *s_source_profile_config_file = "[default]\n"
1148 "aws_access_key_id=BLAHBLAH\n"
1149 "aws_secret_access_key=BLAHBLAHBLAH\n"
1150 "\n"
1151 "[roletest]\n"
1152 "role_arn=arn:aws:iam::67895:role/test_role\n"
1153 "source_profile=default\n"
1154 "role_session_name=test_session";
1155
1156 static const char *s_source_profile_external_id_config_file = "[default]\n"
1157 "aws_access_key_id=BLAHBLAH\n"
1158 "aws_secret_access_key=BLAHBLAHBLAH\n"
1159 "\n"
1160 "[roletest]\n"
1161 "role_arn=arn:aws:iam::67895:role/test_role\n"
1162 "source_profile=default\n"
1163 "role_session_name=test_session\n"
1164 "external_id=externalId123_+=,.@:\\/-\n";
1165
1166 static const char *s_source_profile_chain_config_file = "[default]\n"
1167 "aws_access_key_id=BLAHBLAH\n"
1168 "aws_secret_access_key=BLAHBLAHBLAH\n"
1169 "\n"
1170 "[roletest]\n"
1171 "role_arn=arn:aws:iam::67895:role/test_role\n"
1172 "source_profile=roletest2\n"
1173 "role_session_name=test_session\n"
1174 "[roletest2]\n"
1175 "role_arn=arn:aws:iam::67896:role/test_role\n"
1176 "source_profile=roletest3\n"
1177 "role_session_name=test_session2\n"
1178 "[roletest3]\n"
1179 "role_arn=arn:aws:iam::67897:role/test_role\n"
1180 "source_profile=default\n"
1181 "role_session_name=test_session3\n";
1182
1183 static int s_credentials_provider_sts_from_profile_config_with_chain_fn(struct aws_allocator *allocator, void *ctx) {
1184 (void)ctx;
1185
1186 aws_unset_environment_value(s_default_profile_env_variable_name);
1187 aws_unset_environment_value(s_default_config_path_env_variable_name);
1188 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1189
1190 s_aws_sts_tester_init(allocator);
1191 s_tester.expected_connection_manager_shutdown_callback_count = 3;
1192 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_source_profile_chain_config_file);
1193
1194 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1195 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1196
1197 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1198 aws_string_destroy(config_contents);
1199
1200 struct aws_credentials_provider_profile_options options = {
1201 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1202 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1203 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1204 .bootstrap = s_tester.bootstrap,
1205 .function_table = &s_mock_function_table,
1206 .shutdown_options =
1207 {
1208 .shutdown_callback = s_on_provider_shutdown,
1209 },
1210 };
1211 int expected_num_requests = 3;
1212 for (int i = 0; i < expected_num_requests; i++) {
1213 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1214 }
1215 s_tester.mock_response_code = 200;
1216
1217 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1218 ASSERT_NOT_NULL(provider);
1219
1220 aws_string_destroy(config_file_str);
1221 aws_string_destroy(creds_file_str);
1222
1223 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1224
1225 s_aws_wait_for_credentials_result();
1226
1227 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1228
1229 ASSERT_INT_EQUALS(expected_num_requests, s_tester.num_request);
1230 static struct aws_byte_cursor s_expected_request_body[] = {
1231 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67897%"
1232 "3Arole%2Ftest_role&RoleSessionName=test_session3&DurationSeconds=900"),
1233 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67896%"
1234 "3Arole%2Ftest_role&RoleSessionName=test_session2&DurationSeconds=900"),
1235 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67895%"
1236 "3Arole%2Ftest_role&RoleSessionName=test_session&DurationSeconds=900"),
1237 };
1238 const char *expected_method = "POST";
1239 const char *expected_path = "/";
1240 const char *expected_host_header = "sts.amazonaws.com";
1241
1242 for (int i = 0; i < s_tester.num_request; i++) {
1243 ASSERT_BIN_ARRAYS_EQUALS(
1244 expected_method,
1245 strlen(expected_method),
1246 s_tester.mocked_requests[i].method.buffer,
1247 s_tester.mocked_requests[i].method.len);
1248 ASSERT_BIN_ARRAYS_EQUALS(
1249 expected_path,
1250 strlen(expected_path),
1251 s_tester.mocked_requests[i].path.buffer,
1252 s_tester.mocked_requests[i].path.len);
1253 ASSERT_TRUE(s_tester.mocked_requests[i].had_auth_header);
1254 ASSERT_BIN_ARRAYS_EQUALS(
1255 expected_host_header,
1256 strlen(expected_host_header),
1257 s_tester.mocked_requests[i].host_header.buffer,
1258 s_tester.mocked_requests[i].host_header.len);
1259 ASSERT_BIN_ARRAYS_EQUALS(
1260 s_expected_request_body[i].ptr,
1261 s_expected_request_body[i].len,
1262 s_tester.mocked_requests[i].body.buffer,
1263 s_tester.mocked_requests[i].body.len);
1264 }
1265
1266 aws_credentials_provider_release(provider);
1267 s_aws_wait_for_connection_manager_shutdown_callback();
1268 s_aws_wait_for_provider_shutdown_callback();
1269 /* There used to be a bug that triggered the shutdown callback multiple times. Sleep for a few seconds
1270 * and validate that we don't trigger the shutdown callback multiple times */
1271 aws_thread_current_sleep(3000000000);
1272 ASSERT_INT_EQUALS(1, s_tester.provider_shutdown_callback_count);
1273 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1274
1275 return AWS_OP_SUCCESS;
1276 }
1277 AWS_TEST_CASE(
1278 credentials_provider_sts_from_profile_config_with_chain,
1279 s_credentials_provider_sts_from_profile_config_with_chain_fn)
1280
1281 static int s_credentials_provider_sts_from_profile_config_with_proxy_settings(
1282 struct aws_allocator *allocator,
1283 void *ctx) {
1284 (void)ctx;
1285
1286 aws_unset_environment_value(s_default_profile_env_variable_name);
1287 aws_unset_environment_value(s_default_config_path_env_variable_name);
1288 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1289
1290 s_aws_sts_tester_init(allocator);
1291 s_tester.expected_connection_manager_shutdown_callback_count = 3;
1292
1293 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_source_profile_chain_config_file);
1294
1295 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1296 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1297
1298 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1299 aws_string_destroy(config_contents);
1300
1301 struct proxy_env_var_settings proxy_config = {
1302 .env_var_type = AWS_HPEV_ENABLE,
1303 };
1304
1305 s_tester.proxy_config = &proxy_config;
1306 s_tester.fail_connection = true;
1307
1308 struct aws_credentials_provider_profile_options options = {
1309 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1310 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1311 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1312 .bootstrap = s_tester.bootstrap,
1313 .function_table = &s_mock_function_table,
1314 .shutdown_options =
1315 {
1316 .shutdown_callback = s_on_provider_shutdown,
1317 },
1318 .proxy_ev_settings = &proxy_config,
1319 };
1320
1321 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1322 ASSERT_NOT_NULL(provider);
1323
1324 aws_string_destroy(config_file_str);
1325 aws_string_destroy(creds_file_str);
1326
1327 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1328
1329 s_aws_wait_for_credentials_result();
1330
1331 ASSERT_TRUE(s_tester.credentials == NULL);
1332
1333 aws_credentials_provider_release(provider);
1334 s_aws_wait_for_connection_manager_shutdown_callback();
1335 s_aws_wait_for_provider_shutdown_callback();
1336
1337 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1338
1339 return AWS_OP_SUCCESS;
1340 }
1341 AWS_TEST_CASE(
1342 credentials_provider_sts_from_profile_config_with_proxy_settings,
1343 s_credentials_provider_sts_from_profile_config_with_proxy_settings)
1344
1345 AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_relative_uri, "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI");
1346 static const char *s_source_credentials_ecs_config_file = "[default]\n"
1347 "role_arn=arn:aws:iam::67895:role/test_role\n"
1348 "credential_source=EcsContainer\n"
1349 "role_session_name=test_session\n";
1350 static struct aws_byte_cursor s_ecs_good_response = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
1351 "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"SecretAccessKey\":\"SuccessfulSecret\", \n "
1352 "\"Token\":\"TokenSuccess\", \n \"Expiration\":\"2020-02-25T06:03:31Z\"}");
1353 static int s_credentials_provider_sts_from_profile_config_with_ecs_credentials_source_fn(
1354 struct aws_allocator *allocator,
1355 void *ctx) {
1356 (void)ctx;
1357
1358 aws_unset_environment_value(s_default_profile_env_variable_name);
1359 aws_unset_environment_value(s_default_config_path_env_variable_name);
1360 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1361
1362 /* set the relative uri environment variable*/
1363 char *relative_uri = "/test";
1364 struct aws_string *relative_uri_str = aws_string_new_from_c_str(allocator, relative_uri);
1365 ASSERT_SUCCESS(aws_set_environment_value(s_ecs_creds_env_relative_uri, relative_uri_str));
1366
1367 s_aws_sts_tester_init(allocator);
1368 /* one for ecs provider and one for sts provider */
1369 s_tester.expected_connection_manager_shutdown_callback_count = 2;
1370 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_source_credentials_ecs_config_file);
1371
1372 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1373 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1374
1375 ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents));
1376 aws_string_destroy(config_contents);
1377
1378 struct aws_credentials_provider_profile_options options = {
1379 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1380 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1381 .bootstrap = s_tester.bootstrap,
1382 .function_table = &s_mock_function_table,
1383 };
1384 int expected_num_requests = 2;
1385 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_ecs_good_response);
1386 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1387
1388 s_tester.mock_response_code = 200;
1389
1390 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1391 ASSERT_NOT_NULL(provider);
1392
1393 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1394
1395 s_aws_wait_for_credentials_result();
1396
1397 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1398
1399 ASSERT_INT_EQUALS(expected_num_requests, s_tester.num_request);
1400 struct aws_mock_http_request expected_mocked_request[2];
1401 /* expected ecs request*/
1402 expected_mocked_request[0] = (struct aws_mock_http_request){
1403 .method = aws_byte_buf_from_c_str("GET"),
1404 .host_header = aws_byte_buf_from_c_str("169.254.170.2"),
1405 .path = aws_byte_buf_from_c_str(relative_uri),
1406 .body = aws_byte_buf_from_c_str(""),
1407 .had_auth_header = false,
1408 };
1409 /* expected sts request */
1410 expected_mocked_request[1] = (struct aws_mock_http_request){
1411 .method = aws_byte_buf_from_c_str("POST"),
1412 .host_header = aws_byte_buf_from_c_str("sts.amazonaws.com"),
1413 .path = aws_byte_buf_from_c_str("/"),
1414 .body = aws_byte_buf_from_c_str("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67895%"
1415 "3Arole%2Ftest_role&RoleSessionName=test_session&DurationSeconds=900"),
1416 .had_auth_header = true,
1417 };
1418
1419 for (int i = 0; i < expected_num_requests; i++) {
1420 ASSERT_BIN_ARRAYS_EQUALS(
1421 expected_mocked_request[i].method.buffer,
1422 expected_mocked_request[i].method.len,
1423 s_tester.mocked_requests[i].method.buffer,
1424 s_tester.mocked_requests[i].method.len);
1425 ASSERT_BIN_ARRAYS_EQUALS(
1426 expected_mocked_request[i].path.buffer,
1427 expected_mocked_request[i].path.len,
1428 s_tester.mocked_requests[i].path.buffer,
1429 s_tester.mocked_requests[i].path.len);
1430 ASSERT_TRUE(s_tester.mocked_requests[i].had_auth_header == expected_mocked_request[i].had_auth_header);
1431 ASSERT_BIN_ARRAYS_EQUALS(
1432 expected_mocked_request[i].host_header.buffer,
1433 expected_mocked_request[i].host_header.len,
1434 s_tester.mocked_requests[i].host_header.buffer,
1435 s_tester.mocked_requests[i].host_header.len);
1436 ASSERT_BIN_ARRAYS_EQUALS(
1437 expected_mocked_request[i].body.buffer,
1438 expected_mocked_request[i].body.len,
1439 s_tester.mocked_requests[i].body.buffer,
1440 s_tester.mocked_requests[i].body.len);
1441 }
1442
1443 aws_credentials_provider_release(provider);
1444 s_aws_wait_for_connection_manager_shutdown_callback();
1445 aws_string_destroy(relative_uri_str);
1446 aws_string_destroy(config_file_str);
1447 aws_string_destroy(creds_file_str);
1448 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1449
1450 return AWS_OP_SUCCESS;
1451 }
1452 AWS_TEST_CASE(
1453 credentials_provider_sts_from_profile_config_with_ecs_credentials_source,
1454 s_credentials_provider_sts_from_profile_config_with_ecs_credentials_source_fn)
1455
1456 static const char *s_source_profile_chain_and_profile_config_file = "[default]\n"
1457 "aws_access_key_id=BLAHBLAH\n"
1458 "aws_secret_access_key=BLAHBLAHBLAH\n"
1459 "\n"
1460 "[roletest]\n"
1461 "role_arn=arn:aws:iam::67895:role/test_role\n"
1462 "source_profile=roletest2\n"
1463 "role_session_name=test_session\n"
1464 "[roletest2]\n"
1465 "role_arn=arn:aws:iam::67896:role/test_role\n"
1466 "source_profile=roletest3\n"
1467 "role_session_name=test_session2\n"
1468 "[roletest3]\n"
1469 "role_arn=arn:aws:iam::67897:role/test_role\n"
1470 "source_profile=default\n"
1471 "role_session_name=test_session3\n"
1472 "aws_access_key_id = BLAH\n"
1473 "aws_secret_access_key = BLAHBLAH\n";
1474 static int s_credentials_provider_sts_from_profile_config_with_chain_and_profile_creds_fn(
1475 struct aws_allocator *allocator,
1476 void *ctx) {
1477 (void)ctx;
1478
1479 aws_unset_environment_value(s_default_profile_env_variable_name);
1480 aws_unset_environment_value(s_default_config_path_env_variable_name);
1481 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1482
1483 s_aws_sts_tester_init(allocator);
1484 s_tester.expected_connection_manager_shutdown_callback_count = 2;
1485 struct aws_string *config_contents =
1486 aws_string_new_from_c_str(allocator, s_source_profile_chain_and_profile_config_file);
1487
1488 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1489 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1490
1491 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1492 aws_string_destroy(config_contents);
1493
1494 struct aws_credentials_provider_profile_options options = {
1495 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1496 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1497 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1498 .bootstrap = s_tester.bootstrap,
1499 .function_table = &s_mock_function_table,
1500 };
1501
1502 int expected_num_requests = 2;
1503 for (int i = 0; i < expected_num_requests; i++) {
1504 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1505 }
1506 s_tester.mock_response_code = 200;
1507
1508 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1509 ASSERT_NOT_NULL(provider);
1510
1511 aws_string_destroy(config_file_str);
1512 aws_string_destroy(creds_file_str);
1513
1514 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1515
1516 s_aws_wait_for_credentials_result();
1517
1518 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1519
1520 ASSERT_INT_EQUALS(expected_num_requests, s_tester.num_request);
1521 static struct aws_byte_cursor s_expected_request_body[] = {
1522 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67896%"
1523 "3Arole%2Ftest_role&RoleSessionName=test_session2&DurationSeconds=900"),
1524 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67895%"
1525 "3Arole%2Ftest_role&RoleSessionName=test_session&DurationSeconds=900"),
1526 };
1527 const char *expected_method = "POST";
1528 const char *expected_path = "/";
1529 const char *expected_host_header = "sts.amazonaws.com";
1530
1531 for (int i = 0; i < s_tester.num_request; i++) {
1532 ASSERT_BIN_ARRAYS_EQUALS(
1533 expected_method,
1534 strlen(expected_method),
1535 s_tester.mocked_requests[i].method.buffer,
1536 s_tester.mocked_requests[i].method.len);
1537 ASSERT_BIN_ARRAYS_EQUALS(
1538 expected_path,
1539 strlen(expected_path),
1540 s_tester.mocked_requests[i].path.buffer,
1541 s_tester.mocked_requests[i].path.len);
1542 ASSERT_TRUE(s_tester.mocked_requests[i].had_auth_header);
1543 ASSERT_BIN_ARRAYS_EQUALS(
1544 expected_host_header,
1545 strlen(expected_host_header),
1546 s_tester.mocked_requests[i].host_header.buffer,
1547 s_tester.mocked_requests[i].host_header.len);
1548 ASSERT_BIN_ARRAYS_EQUALS(
1549 s_expected_request_body[i].ptr,
1550 s_expected_request_body[i].len,
1551 s_tester.mocked_requests[i].body.buffer,
1552 s_tester.mocked_requests[i].body.len);
1553 }
1554
1555 aws_credentials_provider_release(provider);
1556 s_aws_wait_for_connection_manager_shutdown_callback();
1557 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1558
1559 return AWS_OP_SUCCESS;
1560 }
1561 AWS_TEST_CASE(
1562 credentials_provider_sts_from_profile_config_with_chain_and_profile_creds,
1563 s_credentials_provider_sts_from_profile_config_with_chain_and_profile_creds_fn)
1564
1565 static const char *s_source_profile_chain_and_partial_profile_config_file =
1566 "[default]\n"
1567 "aws_access_key_id=BLAHBLAH\n"
1568 "aws_secret_access_key=BLAHBLAHBLAH\n"
1569 "\n"
1570 "[roletest]\n"
1571 "role_arn=arn:aws:iam::67895:role/test_role\n"
1572 "source_profile=roletest2\n"
1573 "role_session_name=test_session\n"
1574 "[roletest2]\n"
1575 "role_arn=arn:aws:iam::67896:role/test_role\n"
1576 "source_profile=roletest3\n"
1577 "role_session_name=test_session2\n"
1578 "[roletest3]\n"
1579 "role_arn=arn:aws:iam::67897:role/test_role\n"
1580 "source_profile=default\n"
1581 "role_session_name=test_session3\n"
1582 "aws_access_key_id = BLAH\n";
1583 static int s_credentials_provider_sts_from_profile_config_with_chain_and_partial_profile_creds_fn(
1584 struct aws_allocator *allocator,
1585 void *ctx) {
1586 (void)ctx;
1587
1588 aws_unset_environment_value(s_default_profile_env_variable_name);
1589 aws_unset_environment_value(s_default_config_path_env_variable_name);
1590 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1591
1592 s_aws_sts_tester_init(allocator);
1593 s_tester.expected_connection_manager_shutdown_callback_count = 2;
1594 struct aws_string *config_contents =
1595 aws_string_new_from_c_str(allocator, s_source_profile_chain_and_partial_profile_config_file);
1596
1597 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1598 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1599
1600 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1601 aws_string_destroy(config_contents);
1602
1603 struct aws_credentials_provider_profile_options options = {
1604 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1605 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1606 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1607 .bootstrap = s_tester.bootstrap,
1608 .function_table = &s_mock_function_table,
1609 };
1610
1611 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1612 s_tester.mock_response_code = 200;
1613
1614 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1615 ASSERT_NOT_NULL(provider);
1616
1617 aws_string_destroy(config_file_str);
1618 aws_string_destroy(creds_file_str);
1619
1620 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1621
1622 s_aws_wait_for_credentials_result();
1623
1624 ASSERT_NULL(s_tester.credentials);
1625 ASSERT_INT_EQUALS(s_tester.error_code, AWS_AUTH_SIGNING_NO_CREDENTIALS);
1626 aws_credentials_provider_release(provider);
1627 s_aws_wait_for_connection_manager_shutdown_callback();
1628 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1629
1630 return AWS_OP_SUCCESS;
1631 }
1632 AWS_TEST_CASE(
1633 credentials_provider_sts_from_profile_config_with_chain_and_partial_profile_creds,
1634 s_credentials_provider_sts_from_profile_config_with_chain_and_partial_profile_creds_fn)
1635
1636 static const char *s_source_profile_self_assume_role_config_file = "[default]\n"
1637 "aws_access_key_id=BLAHBLAH\n"
1638 "aws_secret_access_key=BLAHBLAHBLAH\n"
1639 "\n"
1640 "[roletest]\n"
1641 "role_arn=arn:aws:iam::67895:role/test_role\n"
1642 "source_profile=roletest\n"
1643 "role_session_name=test_session\n"
1644 "aws_access_key_id = BLAH\n"
1645 "aws_secret_access_key = BLAHBLAH\n";
1646 static int s_credentials_provider_sts_from_self_referencing_profile_fn(struct aws_allocator *allocator, void *ctx) {
1647 (void)ctx;
1648
1649 aws_unset_environment_value(s_default_profile_env_variable_name);
1650 aws_unset_environment_value(s_default_config_path_env_variable_name);
1651 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1652
1653 s_aws_sts_tester_init(allocator);
1654 struct aws_string *config_contents =
1655 aws_string_new_from_c_str(allocator, s_source_profile_self_assume_role_config_file);
1656
1657 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1658 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1659
1660 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1661 aws_string_destroy(config_contents);
1662
1663 struct aws_credentials_provider_profile_options options = {
1664 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1665 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1666 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1667 .bootstrap = s_tester.bootstrap,
1668 .function_table = &s_mock_function_table,
1669 };
1670
1671 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1672 s_tester.mock_response_code = 200;
1673
1674 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1675 ASSERT_NOT_NULL(provider);
1676
1677 aws_string_destroy(config_file_str);
1678 aws_string_destroy(creds_file_str);
1679
1680 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1681
1682 s_aws_wait_for_credentials_result();
1683
1684 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1685
1686 ASSERT_INT_EQUALS(1, s_tester.num_request);
1687 static struct aws_byte_cursor s_expected_request_body[] = {
1688 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Version=2011-06-15&Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A67895%"
1689 "3Arole%2Ftest_role&RoleSessionName=test_session&DurationSeconds=900"),
1690 };
1691 const char *expected_method = "POST";
1692 const char *expected_path = "/";
1693 const char *expected_host_header = "sts.amazonaws.com";
1694
1695 for (int i = 0; i < s_tester.num_request; i++) {
1696 ASSERT_BIN_ARRAYS_EQUALS(
1697 expected_method,
1698 strlen(expected_method),
1699 s_tester.mocked_requests[i].method.buffer,
1700 s_tester.mocked_requests[i].method.len);
1701 ASSERT_BIN_ARRAYS_EQUALS(
1702 expected_path,
1703 strlen(expected_path),
1704 s_tester.mocked_requests[i].path.buffer,
1705 s_tester.mocked_requests[i].path.len);
1706 ASSERT_TRUE(s_tester.mocked_requests[i].had_auth_header);
1707 ASSERT_BIN_ARRAYS_EQUALS(
1708 expected_host_header,
1709 strlen(expected_host_header),
1710 s_tester.mocked_requests[i].host_header.buffer,
1711 s_tester.mocked_requests[i].host_header.len);
1712 ASSERT_BIN_ARRAYS_EQUALS(
1713 s_expected_request_body[i].ptr,
1714 s_expected_request_body[i].len,
1715 s_tester.mocked_requests[i].body.buffer,
1716 s_tester.mocked_requests[i].body.len);
1717 }
1718
1719 aws_credentials_provider_release(provider);
1720 s_aws_wait_for_connection_manager_shutdown_callback();
1721 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1722
1723 return AWS_OP_SUCCESS;
1724 }
1725 AWS_TEST_CASE(
1726 credentials_provider_sts_from_self_referencing_profile,
1727 s_credentials_provider_sts_from_self_referencing_profile_fn)
1728
1729 static const char *s_source_profile_chain_cycle_config_file = "[default]\n"
1730 "aws_access_key_id=BLAHBLAH\n"
1731 "aws_secret_access_key=BLAHBLAHBLAH\n"
1732 "\n"
1733 "[roletest]\n"
1734 "role_arn=arn:aws:iam::67895:role/test_role\n"
1735 "source_profile=roletest2\n"
1736 "role_session_name=test_session\n"
1737 "[roletest2]\n"
1738 "role_arn=arn:aws:iam::67896:role/test_role\n"
1739 "source_profile=roletest3\n"
1740 "role_session_name=test_session2\n"
1741 "[roletest3]\n"
1742 "role_arn=arn:aws:iam::67897:role/test_role\n"
1743 "source_profile=roletest2\n"
1744 "role_session_name=test_session3\n";
1745
1746 static int s_credentials_provider_sts_from_profile_config_with_chain_cycle_fn(
1747 struct aws_allocator *allocator,
1748 void *ctx) {
1749 (void)ctx;
1750
1751 aws_unset_environment_value(s_default_profile_env_variable_name);
1752 aws_unset_environment_value(s_default_config_path_env_variable_name);
1753 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1754
1755 s_aws_sts_tester_init(allocator);
1756
1757 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_source_profile_chain_cycle_config_file);
1758
1759 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1760 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1761
1762 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1763 aws_string_destroy(config_contents);
1764
1765 struct aws_credentials_provider_profile_options options = {
1766 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1767 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1768 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1769 .bootstrap = s_tester.bootstrap,
1770 .function_table = &s_mock_function_table,
1771 };
1772
1773 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1774 s_tester.mock_response_code = 200;
1775
1776 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1777 ASSERT_NULL(provider);
1778 ASSERT_INT_EQUALS(AWS_AUTH_PROFILE_STS_CREDENTIALS_PROVIDER_CYCLE_FAILURE, aws_last_error());
1779
1780 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1781 aws_string_destroy(config_file_str);
1782 aws_string_destroy(creds_file_str);
1783
1784 return AWS_OP_SUCCESS;
1785 }
1786 AWS_TEST_CASE(
1787 credentials_provider_sts_from_profile_config_with_chain_cycle,
1788 s_credentials_provider_sts_from_profile_config_with_chain_cycle_fn)
1789
1790 static const char *s_source_profile_chain_cycle_and_static_creds_config_file =
1791 "[roletest]\n"
1792 "role_arn=arn:aws:iam::67895:role/test_role\n"
1793 "source_profile=roletest2\n"
1794 "role_session_name=test_session\n"
1795 "aws_access_key_id=BLAHBLAH\n"
1796 "aws_secret_access_key=BLAHBLAHBLAH\n"
1797 "[roletest2]\n"
1798 "role_arn=arn:aws:iam::67896:role/test_role\n"
1799 "source_profile=roletest3\n"
1800 "role_session_name=test_session2\n"
1801 "[roletest3]\n"
1802 "role_arn=arn:aws:iam::67897:role/test_role\n"
1803 "source_profile=roletest\n"
1804 "role_session_name=test_session3\n";
1805
1806 static int s_credentials_provider_sts_from_profile_config_with_chain_cycle_and_profile_creds_fn(
1807 struct aws_allocator *allocator,
1808 void *ctx) {
1809 (void)ctx;
1810
1811 aws_unset_environment_value(s_default_profile_env_variable_name);
1812 aws_unset_environment_value(s_default_config_path_env_variable_name);
1813 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1814
1815 s_aws_sts_tester_init(allocator);
1816
1817 struct aws_string *config_contents =
1818 aws_string_new_from_c_str(allocator, s_source_profile_chain_cycle_and_static_creds_config_file);
1819
1820 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1821 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1822
1823 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1824 aws_string_destroy(config_contents);
1825
1826 struct aws_credentials_provider_profile_options options = {
1827 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1828 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1829 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1830 .bootstrap = s_tester.bootstrap,
1831 .function_table = &s_mock_function_table,
1832 };
1833
1834 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1835 s_tester.mock_response_code = 200;
1836
1837 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1838 ASSERT_NULL(provider);
1839 ASSERT_INT_EQUALS(AWS_AUTH_PROFILE_STS_CREDENTIALS_PROVIDER_CYCLE_FAILURE, aws_last_error());
1840
1841 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1842 aws_string_destroy(config_file_str);
1843 aws_string_destroy(creds_file_str);
1844
1845 return AWS_OP_SUCCESS;
1846 }
1847 AWS_TEST_CASE(
1848 credentials_provider_sts_from_profile_config_with_chain_cycle_and_profile_creds,
1849 s_credentials_provider_sts_from_profile_config_with_chain_cycle_and_profile_creds_fn)
1850
1851 static int s_credentials_provider_sts_from_profile_config_succeeds(
1852 struct aws_allocator *allocator,
1853 void *ctx,
1854 bool manual_tls) {
1855 (void)ctx;
1856
1857 aws_unset_environment_value(s_default_profile_env_variable_name);
1858 aws_unset_environment_value(s_default_config_path_env_variable_name);
1859 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1860
1861 s_aws_sts_tester_init(allocator);
1862
1863 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_source_profile_config_file);
1864
1865 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1866 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1867
1868 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1869 aws_string_destroy(config_contents);
1870
1871 struct aws_credentials_provider_profile_options options = {
1872 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1873 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1874 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1875 .bootstrap = s_tester.bootstrap,
1876 /* tls_ctx is optional, test it both ways */
1877 .tls_ctx = manual_tls ? s_tester.tls_ctx : NULL,
1878 .function_table = &s_mock_function_table,
1879 };
1880
1881 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1882 s_tester.mock_response_code = 200;
1883
1884 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1885 ASSERT_NOT_NULL(provider);
1886
1887 aws_string_destroy(config_file_str);
1888 aws_string_destroy(creds_file_str);
1889
1890 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1891
1892 s_aws_wait_for_credentials_result();
1893
1894 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1895
1896 const char *expected_method = "POST";
1897 ASSERT_BIN_ARRAYS_EQUALS(
1898 expected_method,
1899 strlen(expected_method),
1900 s_tester.mocked_requests[0].method.buffer,
1901 s_tester.mocked_requests[0].method.len);
1902
1903 const char *expected_path = "/";
1904 ASSERT_BIN_ARRAYS_EQUALS(
1905 expected_path,
1906 strlen(expected_path),
1907 s_tester.mocked_requests[0].path.buffer,
1908 s_tester.mocked_requests[0].path.len);
1909
1910 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
1911
1912 const char *expected_host_header = "sts.amazonaws.com";
1913 ASSERT_BIN_ARRAYS_EQUALS(
1914 expected_host_header,
1915 strlen(expected_host_header),
1916 s_tester.mocked_requests[0].host_header.buffer,
1917 s_tester.mocked_requests[0].host_header.len);
1918
1919 ASSERT_BIN_ARRAYS_EQUALS(
1920 s_expected_payload.ptr,
1921 s_expected_payload.len,
1922 s_tester.mocked_requests[0].body.buffer,
1923 s_tester.mocked_requests[0].body.len);
1924
1925 aws_credentials_provider_release(provider);
1926 s_aws_wait_for_connection_manager_shutdown_callback();
1927 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
1928
1929 return AWS_OP_SUCCESS;
1930 }
1931
1932 static int s_credentials_provider_sts_from_profile_config_succeeds_fn(struct aws_allocator *allocator, void *ctx) {
1933 return s_credentials_provider_sts_from_profile_config_succeeds(allocator, ctx, false /*manual_tls*/);
1934 }
1935
1936 AWS_TEST_CASE(
1937 credentials_provider_sts_from_profile_config_succeeds,
1938 s_credentials_provider_sts_from_profile_config_succeeds_fn)
1939
1940 static int credentials_provider_sts_from_profile_config_manual_tls_succeeds_fn(
1941 struct aws_allocator *allocator,
1942 void *ctx) {
1943 return s_credentials_provider_sts_from_profile_config_succeeds(allocator, ctx, true /*manual_tls*/);
1944 }
1945
1946 AWS_TEST_CASE(
1947 credentials_provider_sts_from_profile_config_manual_tls_succeeds,
1948 credentials_provider_sts_from_profile_config_manual_tls_succeeds_fn)
1949
1950 static int s_credentials_provider_sts_from_profile_config_with_external_id_fn(
1951 struct aws_allocator *allocator,
1952 void *ctx) {
1953 (void)ctx;
1954
1955 aws_unset_environment_value(s_default_profile_env_variable_name);
1956 aws_unset_environment_value(s_default_config_path_env_variable_name);
1957 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
1958
1959 s_aws_sts_tester_init(allocator);
1960
1961 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_source_profile_external_id_config_file);
1962
1963 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
1964 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
1965
1966 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
1967 aws_string_destroy(config_contents);
1968
1969 struct aws_credentials_provider_profile_options options = {
1970 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
1971 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
1972 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
1973 .bootstrap = s_tester.bootstrap,
1974 .tls_ctx = s_tester.tls_ctx,
1975 .function_table = &s_mock_function_table,
1976 };
1977
1978 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
1979 s_tester.mock_response_code = 200;
1980
1981 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
1982 ASSERT_NOT_NULL(provider);
1983
1984 aws_string_destroy(config_file_str);
1985 aws_string_destroy(creds_file_str);
1986
1987 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1988
1989 s_aws_wait_for_credentials_result();
1990
1991 ASSERT_BIN_ARRAYS_EQUALS(
1992 s_expected_payload_with_external_id.ptr,
1993 s_expected_payload_with_external_id.len,
1994 s_tester.mocked_requests[0].body.buffer,
1995 s_tester.mocked_requests[0].body.len);
1996
1997 aws_credentials_provider_release(provider);
1998 s_aws_wait_for_connection_manager_shutdown_callback();
1999 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
2000
2001 return AWS_OP_SUCCESS;
2002 }
2003 AWS_TEST_CASE(
2004 credentials_provider_sts_from_profile_config_with_external_id,
2005 s_credentials_provider_sts_from_profile_config_with_external_id_fn)
2006
2007 static const char *s_env_source_config_file = "[default]\n"
2008 "aws_access_key_id=BLAHBLAH\n"
2009 "aws_secret_access_key=BLAHBLAHBLAH\n"
2010 "\n"
2011 "[roletest]\n"
2012 "role_arn=arn:aws:iam::67895:role/test_role\n"
2013 "credential_source=Environment\n"
2014 "role_session_name=test_session";
2015
2016 AWS_STRING_FROM_LITERAL(s_env_access_key_val, "EnvAccessKeyId");
2017 AWS_STRING_FROM_LITERAL(s_env_secret_access_key_val, "EnvSecretAccessKeyId");
2018
2019 static int s_credentials_provider_sts_from_profile_config_environment_succeeds_fn(
2020 struct aws_allocator *allocator,
2021 void *ctx) {
2022 (void)ctx;
2023
2024 aws_unset_environment_value(s_default_profile_env_variable_name);
2025 aws_unset_environment_value(s_default_config_path_env_variable_name);
2026 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
2027
2028 aws_set_environment_value(s_access_key_id_env_var, s_env_access_key_val);
2029 aws_set_environment_value(s_secret_access_key_env_var, s_env_secret_access_key_val);
2030
2031 s_aws_sts_tester_init(allocator);
2032
2033 struct aws_string *config_contents = aws_string_new_from_c_str(allocator, s_env_source_config_file);
2034
2035 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
2036 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
2037
2038 ASSERT_SUCCESS(aws_create_profile_file(creds_file_str, config_contents));
2039 aws_string_destroy(config_contents);
2040
2041 struct aws_credentials_provider_profile_options options = {
2042 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
2043 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
2044 .profile_name_override = aws_byte_cursor_from_c_str("roletest"),
2045 .bootstrap = s_tester.bootstrap,
2046 .tls_ctx = s_tester.tls_ctx,
2047 .function_table = &s_mock_function_table,
2048 };
2049
2050 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
2051 s_tester.mock_response_code = 200;
2052
2053 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
2054 ASSERT_NOT_NULL(provider);
2055
2056 aws_string_destroy(creds_file_str);
2057 aws_string_destroy(config_file_str);
2058
2059 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
2060
2061 s_aws_wait_for_credentials_result();
2062
2063 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
2064
2065 const char *expected_method = "POST";
2066 ASSERT_BIN_ARRAYS_EQUALS(
2067 expected_method,
2068 strlen(expected_method),
2069 s_tester.mocked_requests[0].method.buffer,
2070 s_tester.mocked_requests[0].method.len);
2071
2072 const char *expected_path = "/";
2073 ASSERT_BIN_ARRAYS_EQUALS(
2074 expected_path,
2075 strlen(expected_path),
2076 s_tester.mocked_requests[0].path.buffer,
2077 s_tester.mocked_requests[0].path.len);
2078
2079 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
2080
2081 const char *expected_host_header = "sts.amazonaws.com";
2082 ASSERT_BIN_ARRAYS_EQUALS(
2083 expected_host_header,
2084 strlen(expected_host_header),
2085 s_tester.mocked_requests[0].host_header.buffer,
2086 s_tester.mocked_requests[0].host_header.len);
2087
2088 ASSERT_BIN_ARRAYS_EQUALS(
2089 s_expected_payload.ptr,
2090 s_expected_payload.len,
2091 s_tester.mocked_requests[0].body.buffer,
2092 s_tester.mocked_requests[0].body.len);
2093
2094 aws_credentials_provider_release(provider);
2095 s_aws_wait_for_connection_manager_shutdown_callback();
2096
2097 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
2098
2099 return AWS_OP_SUCCESS;
2100 }
2101
2102 AWS_TEST_CASE(
2103 credentials_provider_sts_from_profile_config_environment_succeeds,
2104 s_credentials_provider_sts_from_profile_config_environment_succeeds_fn)
2105
2106 #define HIGH_RES_BASE_TIME_NS 101000000000ULL
2107
2108 /*
2109 * In this test, we set up a cached provider with a longer and out-of-sync refresh period than the sts
2110 * provider that it wraps. We verify that the cached provider factors in the shorter-lived sts credentials
2111 * properly and refreshes when the credentials expire and not when the cache would expire.
2112 */
2113 static int s_credentials_provider_sts_cache_expiration_conflict(struct aws_allocator *allocator, void *ctx) {
2114 (void)ctx;
2115
2116 s_aws_sts_tester_init(allocator);
2117
2118 struct aws_credentials_provider_static_options static_options = {
2119 .access_key_id = s_access_key_cur,
2120 .secret_access_key = s_secret_key_cur,
2121 .session_token = s_session_token_cur,
2122 };
2123 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
2124
2125 struct aws_credentials_provider_sts_options options = {
2126 .creds_provider = static_provider,
2127 .bootstrap = s_tester.bootstrap,
2128 .tls_ctx = s_tester.tls_ctx,
2129 .role_arn = s_role_arn_cur,
2130 .session_name = s_session_name_cur,
2131 .duration_seconds = 0,
2132 .function_table = &s_mock_function_table,
2133 .system_clock_fn = mock_aws_get_system_time,
2134 };
2135
2136 /* make sure high res time and system time are sufficiently diverged that a mistake in the
2137 * respective calculations would fail the test */
2138 mock_aws_set_system_time(0);
2139 mock_aws_set_high_res_time(HIGH_RES_BASE_TIME_NS);
2140
2141 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
2142 s_tester.mock_response_code = 200;
2143
2144 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
2145
2146 struct aws_credentials_provider_cached_options cached_options = {
2147 .system_clock_fn = mock_aws_get_system_time,
2148 .high_res_clock_fn = mock_aws_get_high_res_time,
2149 .refresh_time_in_milliseconds = 1200 * 1000,
2150 .source = sts_provider,
2151 };
2152 struct aws_credentials_provider *cached_provider = aws_credentials_provider_new_cached(allocator, &cached_options);
2153
2154 aws_credentials_provider_get_credentials(cached_provider, s_get_credentials_callback, NULL);
2155
2156 s_aws_wait_for_credentials_result();
2157
2158 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
2159 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
2160
2161 const char *expected_method = "POST";
2162 ASSERT_BIN_ARRAYS_EQUALS(
2163 expected_method,
2164 strlen(expected_method),
2165 s_tester.mocked_requests[0].method.buffer,
2166 s_tester.mocked_requests[0].method.len);
2167
2168 const char *expected_path = "/";
2169 ASSERT_BIN_ARRAYS_EQUALS(
2170 expected_path,
2171 strlen(expected_path),
2172 s_tester.mocked_requests[0].path.buffer,
2173 s_tester.mocked_requests[0].path.len);
2174
2175 ASSERT_TRUE(s_tester.mocked_requests[0].had_auth_header);
2176
2177 const char *expected_host_header = "sts.amazonaws.com";
2178 ASSERT_BIN_ARRAYS_EQUALS(
2179 expected_host_header,
2180 strlen(expected_host_header),
2181 s_tester.mocked_requests[0].host_header.buffer,
2182 s_tester.mocked_requests[0].host_header.len);
2183
2184 ASSERT_BIN_ARRAYS_EQUALS(
2185 s_expected_payload.ptr,
2186 s_expected_payload.len,
2187 s_tester.mocked_requests[0].body.buffer,
2188 s_tester.mocked_requests[0].body.len);
2189
2190 /* advance each time to a little before expiration, verify we get creds with the same expiration */
2191 uint64_t before_expiration_time = aws_timestamp_convert(599, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
2192 mock_aws_set_system_time(before_expiration_time);
2193 mock_aws_set_high_res_time(HIGH_RES_BASE_TIME_NS + before_expiration_time);
2194
2195 s_cleanup_creds_callback_data();
2196
2197 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
2198 aws_credentials_provider_get_credentials(cached_provider, s_get_credentials_callback, NULL);
2199
2200 s_aws_wait_for_credentials_result();
2201 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 900);
2202
2203 /* advance each time to after expiration but before cached provider timeout, verify we get new creds */
2204 uint64_t after_expiration_time = aws_timestamp_convert(901, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
2205 mock_aws_set_system_time(after_expiration_time);
2206 mock_aws_set_high_res_time(HIGH_RES_BASE_TIME_NS + after_expiration_time);
2207
2208 s_cleanup_creds_callback_data();
2209
2210 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
2211 aws_credentials_provider_get_credentials(cached_provider, s_get_credentials_callback, NULL);
2212
2213 s_aws_wait_for_credentials_result();
2214 ASSERT_TRUE(aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == 1801);
2215
2216 aws_credentials_provider_release(cached_provider);
2217 aws_credentials_provider_release(sts_provider);
2218 s_aws_wait_for_connection_manager_shutdown_callback();
2219 aws_credentials_provider_release(static_provider);
2220
2221 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
2222
2223 return AWS_OP_SUCCESS;
2224 }
2225
2226 AWS_TEST_CASE(credentials_provider_sts_cache_expiration_conflict, s_credentials_provider_sts_cache_expiration_conflict)
2227
2228 static int s_credentials_provider_sts_proxy_routing_enabled_test(struct aws_allocator *allocator, void *ctx) {
2229 (void)ctx;
2230
2231 s_aws_sts_tester_init(allocator);
2232
2233 struct proxy_env_var_settings proxy_config = {
2234 .env_var_type = AWS_HPEV_ENABLE,
2235 };
2236
2237 s_tester.proxy_config = &proxy_config;
2238 s_tester.fail_connection = true;
2239
2240 struct aws_credentials_provider_static_options static_options = {
2241 .access_key_id = s_access_key_cur,
2242 .secret_access_key = s_secret_key_cur,
2243 .session_token = s_session_token_cur,
2244 };
2245 struct aws_credentials_provider *static_provider = aws_credentials_provider_new_static(allocator, &static_options);
2246
2247 struct aws_credentials_provider_sts_options options = {
2248 .creds_provider = static_provider,
2249 .bootstrap = s_tester.bootstrap,
2250 .tls_ctx = s_tester.tls_ctx,
2251 .role_arn = s_role_arn_cur,
2252 .session_name = s_session_name_cur,
2253 .duration_seconds = 0,
2254 .function_table = &s_mock_function_table,
2255 .system_clock_fn = mock_aws_get_system_time,
2256 .proxy_ev_settings = &proxy_config,
2257 };
2258
2259 mock_aws_set_system_time(0);
2260
2261 struct aws_credentials_provider *sts_provider = aws_credentials_provider_new_sts(allocator, &options);
2262
2263 aws_credentials_provider_get_credentials(sts_provider, s_get_credentials_callback, NULL);
2264
2265 s_aws_wait_for_credentials_result();
2266
2267 ASSERT_TRUE(s_tester.credentials == NULL);
2268
2269 aws_credentials_provider_release(sts_provider);
2270 s_aws_wait_for_connection_manager_shutdown_callback();
2271 aws_credentials_provider_release(static_provider);
2272 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
2273
2274 return AWS_OP_SUCCESS;
2275 }
2276 AWS_TEST_CASE(
2277 credentials_provider_sts_proxy_routing_enabled_test,
2278 s_credentials_provider_sts_proxy_routing_enabled_test)
2279
2280 /*
2281 * Profile chaining with web_identity_token_file in source_profile fails.
2282 *
2283 * Config:
2284 * [default]
2285 * role_arn = <customer-role>
2286 * source_profile = irsa-hop1
2287 *
2288 * [irsa-hop1]
2289 * role_arn = <intermediary-role>
2290 * web_identity_token_file = /path/to/token
2291 *
2292 * Expected: provider construction succeeds (two-hop chain: web_identity -> STS assume -> STS assume).
2293 */
2294 static const char *s_source_profile_web_identity_config_file =
2295 "[profile default]\n"
2296 "role_arn=arn:aws:iam::111122223333:role/customer-role\n"
2297 "source_profile=irsa-hop1\n"
2298 "\n"
2299 "[profile irsa-hop1]\n"
2300 "role_arn=arn:aws:iam::111122223333:role/intermediary-role\n"
2301 "region=us-east-1\n"
2302 "web_identity_token_file=";
2303
2304 static int s_credentials_provider_sts_from_profile_config_with_web_identity_source_fn(
2305 struct aws_allocator *allocator,
2306 void *ctx) {
2307 (void)ctx;
2308
2309 aws_unset_environment_value(s_default_profile_env_variable_name);
2310 aws_unset_environment_value(s_default_config_path_env_variable_name);
2311 aws_unset_environment_value(s_default_credentials_path_env_variable_name);
2312
2313 s_aws_sts_tester_init(allocator);
2314 s_tester.expected_connection_manager_shutdown_callback_count = 2;
2315
2316 /* Create a fake token file */
2317 struct aws_string *token_file_path = aws_create_process_unique_file_name(allocator);
2318 struct aws_string *token_contents = aws_string_new_from_c_str(allocator, "fake-oidc-token-12345");
2319 ASSERT_SUCCESS(aws_create_profile_file(token_file_path, token_contents));
2320 aws_string_destroy(token_contents);
2321
2322 /* Build config: static prefix + token_file_path + newline */
2323 struct aws_byte_buf config_buf;
2324 aws_byte_buf_init(&config_buf, allocator, 512);
2325 struct aws_byte_cursor cursor = aws_byte_cursor_from_c_str(s_source_profile_web_identity_config_file);
2326 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&config_buf, &cursor));
2327 cursor = aws_byte_cursor_from_string(token_file_path);
2328 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&config_buf, &cursor));
2329 cursor = aws_byte_cursor_from_c_str("\n");
2330 ASSERT_SUCCESS(aws_byte_buf_append_dynamic(&config_buf, &cursor));
2331
2332 struct aws_string *config_contents = aws_string_new_from_array(allocator, config_buf.buffer, config_buf.len);
2333 aws_byte_buf_clean_up(&config_buf);
2334
2335 struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator);
2336 struct aws_string *creds_file_str = aws_create_process_unique_file_name(allocator);
2337
2338 ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents));
2339 aws_string_destroy(config_contents);
2340
2341 struct aws_credentials_provider_profile_options options = {
2342 .config_file_name_override = aws_byte_cursor_from_string(config_file_str),
2343 .credentials_file_name_override = aws_byte_cursor_from_string(creds_file_str),
2344 .profile_name_override = aws_byte_cursor_from_c_str("default"),
2345 .bootstrap = s_tester.bootstrap,
2346 .tls_ctx = s_tester.tls_ctx,
2347 .function_table = &s_mock_function_table,
2348 .shutdown_options =
2349 {
2350 .shutdown_callback = s_on_provider_shutdown,
2351 },
2352 };
2353
2354 /* Push response data for the two STS calls:
2355 * 1st: AssumeRoleWithWebIdentity response (for irsa-hop1's web identity provider)
2356 * 2nd: AssumeRole response (for default's STS provider) */
2357 static struct aws_byte_cursor s_web_identity_response_doc =
2358 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<AssumeRoleWithWebIdentityResponse>"
2359 " <AssumeRoleWithWebIdentityResult>"
2360 " <AssumedRoleUser>"
2361 " <Arn>arn:aws:sts::123456789012:assumed-role/role/app</Arn>"
2362 " <AssumedRoleId>AROA:app</AssumedRoleId>"
2363 " </AssumedRoleUser>"
2364 " <Credentials>"
2365 " <AccessKeyId>accessKey12345</AccessKeyId>"
2366 " <SecretAccessKey>secretKey12345</SecretAccessKey>"
2367 " <SessionToken>sessionToken123456789</SessionToken>"
2368 " <Expiration>2099-01-01T00:00:00Z</Expiration>"
2369 " </Credentials>"
2370 " </AssumeRoleWithWebIdentityResult>"
2371 "</AssumeRoleWithWebIdentityResponse>");
2372 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_web_identity_response_doc);
2373 aws_array_list_push_back(&s_tester.response_data_callbacks, &s_success_creds_doc);
2374 s_tester.mock_response_code = 200;
2375
2376 /*
2377 * With the fix in place, the profile provider detects web_identity_token_file in irsa-hop1
2378 * and creates an STS web identity provider. The chain is:
2379 * 1. AssumeRoleWithWebIdentity (using the token file) for irsa-hop1's role_arn
2380 * 2. AssumeRole (using creds from step 1) for default's role_arn
2381 */
2382 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
2383 ASSERT_NOT_NULL(provider);
2384
2385 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
2386
2387 s_aws_wait_for_credentials_result();
2388
2389 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
2390
2391 aws_credentials_provider_release(provider);
2392 s_aws_wait_for_connection_manager_shutdown_callback();
2393 s_aws_wait_for_provider_shutdown_callback();
2394
2395 aws_file_delete(token_file_path);
2396 aws_file_delete(config_file_str);
2397
2398 aws_string_destroy(token_file_path);
2399 aws_string_destroy(config_file_str);
2400 aws_string_destroy(creds_file_str);
2401 ASSERT_SUCCESS(s_aws_sts_tester_cleanup());
2402
2403 return AWS_OP_SUCCESS;
2404 }
2405 AWS_TEST_CASE(
2406 credentials_provider_sts_from_profile_config_with_web_identity_source,
2407 s_credentials_provider_sts_from_profile_config_with_web_identity_source_fn)