Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/testing/aws_test_harness.h>
7
8 #include "shared_credentials_test_definitions.h"
9
10 #include <aws/auth/credentials.h>
11 #include <aws/auth/private/credentials_utils.h>
12 #include <aws/common/byte_buf.h>
13 #include <aws/common/clock.h>
14 #include <aws/common/condition_variable.h>
15 #include <aws/common/date_time.h>
16 #include <aws/common/environment.h>
17 #include <aws/common/string.h>
18 #include <aws/common/thread.h>
19 #include <aws/common/uri.h>
20 #include <aws/http/proxy.h>
21 #include <aws/http/request_response.h>
22 #include <aws/http/status_code.h>
23 #include <aws/io/channel_bootstrap.h>
24 #include <aws/io/event_loop.h>
25 #include <aws/io/logging.h>
26 #include <aws/io/socket.h>
27 #include <aws/io/tls_channel_handler.h>
28
29 AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_relative_uri, "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI");
30 AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_full_uri, "AWS_CONTAINER_CREDENTIALS_FULL_URI");
31 AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_token_file, "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE");
32 AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_token, "AWS_CONTAINER_AUTHORIZATION_TOKEN");
33
34 struct aws_mock_ecs_tester {
35 struct aws_allocator *allocator;
36
37 struct aws_string *request_path_and_query;
38 struct aws_string *request_authorization_header;
39 struct aws_string *selected_host;
40 int attempts;
41
42 struct aws_array_list response_data_callbacks;
43 bool is_connection_acquire_successful;
44 bool is_request_successful;
45
46 struct aws_mutex lock;
47 struct aws_condition_variable signal;
48
49 struct aws_credentials *credentials;
50 bool has_received_credentials_callback;
51 bool has_received_shutdown_callback;
52 bool selected_tls;
53 uint32_t selected_port;
54
55 int error_code;
56
57 struct aws_event_loop_group *el_group;
58 struct aws_host_resolver *host_resolver;
59 struct aws_client_bootstrap *bootstrap;
60 struct aws_tls_ctx *tls_ctx;
61
62 struct proxy_env_var_settings *proxy_config;
63 };
64
65 static struct aws_mock_ecs_tester s_tester;
66
67 static void s_on_shutdown_complete(void *user_data) {
68 (void)user_data;
69
70 aws_mutex_lock(&s_tester.lock);
71 s_tester.has_received_shutdown_callback = true;
72 aws_mutex_unlock(&s_tester.lock);
73
74 aws_condition_variable_notify_one(&s_tester.signal);
75 }
76
77 static bool s_has_tester_received_shutdown_callback(void *user_data) {
78 (void)user_data;
79
80 return s_tester.has_received_shutdown_callback;
81 }
82
83 static void s_aws_wait_for_provider_shutdown_callback(void) {
84 aws_mutex_lock(&s_tester.lock);
85 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_has_tester_received_shutdown_callback, NULL);
86 aws_mutex_unlock(&s_tester.lock);
87 }
88
89 static struct aws_http_connection_manager *s_aws_http_connection_manager_new_mock(
90 struct aws_allocator *allocator,
91 const struct aws_http_connection_manager_options *options) {
92
93 (void)allocator;
94 (void)options;
95
96 aws_mutex_lock(&s_tester.lock);
97 s_tester.selected_host = aws_string_new_from_cursor(allocator, &options->host);
98 s_tester.selected_port = options->port;
99 s_tester.selected_tls = options->tls_connection_options != NULL;
100 aws_mutex_unlock(&s_tester.lock);
101
102 if (s_tester.proxy_config != NULL) {
103 AWS_FATAL_ASSERT(options->proxy_ev_settings->env_var_type == s_tester.proxy_config->env_var_type);
104 AWS_FATAL_ASSERT(options->proxy_ev_settings->connection_type == s_tester.proxy_config->connection_type);
105 AWS_FATAL_ASSERT(options->proxy_ev_settings->tls_options == s_tester.proxy_config->tls_options);
106 }
107
108 return (struct aws_http_connection_manager *)1;
109 }
110
111 static void s_aws_http_connection_manager_release_mock(struct aws_http_connection_manager *manager) {
112 (void)manager;
113
114 s_on_shutdown_complete(NULL);
115 }
116
117 static void s_aws_http_connection_manager_acquire_connection_mock(
118 struct aws_http_connection_manager *manager,
119 aws_http_connection_manager_on_connection_setup_fn *callback,
120 void *user_data) {
121
122 (void)manager;
123 (void)callback;
124 (void)user_data;
125
126 if (s_tester.is_connection_acquire_successful) {
127 callback((struct aws_http_connection *)1, AWS_OP_SUCCESS, user_data);
128 } else {
129 aws_raise_error(AWS_ERROR_HTTP_UNKNOWN);
130 callback(NULL, AWS_OP_ERR, user_data);
131 }
132 }
133
134 static int s_aws_http_connection_manager_release_connection_mock(
135 struct aws_http_connection_manager *manager,
136 struct aws_http_connection *connection) {
137
138 (void)manager;
139 (void)connection;
140
141 return AWS_OP_SUCCESS;
142 }
143
144 static void s_invoke_mock_request_callbacks(
145 const struct aws_http_make_request_options *options,
146 struct aws_array_list *data_callbacks,
147 bool is_request_successful) {
148
149 size_t data_callback_count = aws_array_list_length(data_callbacks);
150
151 struct aws_http_header headers[1];
152 AWS_ZERO_ARRAY(headers);
153
154 headers[0].name = aws_byte_cursor_from_c_str("some-header");
155 headers[0].value = aws_byte_cursor_from_c_str("value");
156
157 options->on_response_headers(
158 (struct aws_http_stream *)1, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data);
159
160 if (options->on_response_header_block_done) {
161 options->on_response_header_block_done(
162 (struct aws_http_stream *)1, data_callback_count > 0, options->user_data);
163 }
164
165 for (size_t i = 0; i < data_callback_count; ++i) {
166 struct aws_byte_cursor data_callback_cursor;
167 if (aws_array_list_get_at(data_callbacks, &data_callback_cursor, i)) {
168 continue;
169 }
170
171 options->on_response_body((struct aws_http_stream *)1, &data_callback_cursor, options->user_data);
172 }
173
174 options->on_complete(
175 (struct aws_http_stream *)1,
176 is_request_successful ? AWS_ERROR_SUCCESS : AWS_ERROR_HTTP_UNKNOWN,
177 options->user_data);
178 }
179
180 static struct aws_http_stream *s_aws_http_connection_make_request_mock(
181 struct aws_http_connection *client_connection,
182 const struct aws_http_make_request_options *options) {
183
184 (void)client_connection;
185 (void)options;
186
187 s_tester.attempts++;
188 struct aws_byte_cursor path;
189 AWS_ZERO_STRUCT(path);
190 aws_http_message_get_request_path(options->request, &path);
191 if (s_tester.request_path_and_query != NULL) {
192 aws_string_destroy(s_tester.request_path_and_query);
193 s_tester.request_path_and_query = NULL;
194 }
195 s_tester.request_path_and_query = aws_string_new_from_cursor(s_tester.allocator, &path);
196 struct aws_byte_cursor authorization_header_value;
197 AWS_ZERO_STRUCT(authorization_header_value);
198 if (aws_http_headers_get(
199 aws_http_message_get_headers(options->request),
200 aws_byte_cursor_from_c_str("Authorization"),
201 &authorization_header_value) == AWS_OP_SUCCESS) {
202 if (s_tester.request_authorization_header != NULL) {
203 aws_string_destroy(s_tester.request_authorization_header);
204 s_tester.request_authorization_header = NULL;
205 }
206 s_tester.request_authorization_header =
207 aws_string_new_from_cursor(s_tester.allocator, &authorization_header_value);
208 }
209
210 s_invoke_mock_request_callbacks(options, &s_tester.response_data_callbacks, s_tester.is_request_successful);
211
212 return (struct aws_http_stream *)1;
213 }
214
215 static int s_aws_http_stream_activate_mock(struct aws_http_stream *stream) {
216 (void)stream;
217 return AWS_OP_SUCCESS;
218 }
219
220 static int s_aws_http_stream_get_incoming_response_status_mock(
221 const struct aws_http_stream *stream,
222 int *out_status_code) {
223 (void)stream;
224
225 *out_status_code = AWS_HTTP_STATUS_CODE_200_OK;
226
227 return AWS_OP_SUCCESS;
228 }
229
230 static void s_aws_http_stream_release_mock(struct aws_http_stream *stream) {
231 (void)stream;
232 }
233
234 static void s_aws_http_connection_close_mock(struct aws_http_connection *connection) {
235 (void)connection;
236 }
237
238 static struct aws_auth_http_system_vtable s_mock_function_table = {
239 .aws_http_connection_manager_new = s_aws_http_connection_manager_new_mock,
240 .aws_http_connection_manager_release = s_aws_http_connection_manager_release_mock,
241 .aws_http_connection_manager_acquire_connection = s_aws_http_connection_manager_acquire_connection_mock,
242 .aws_http_connection_manager_release_connection = s_aws_http_connection_manager_release_connection_mock,
243 .aws_http_connection_make_request = s_aws_http_connection_make_request_mock,
244 .aws_http_stream_activate = s_aws_http_stream_activate_mock,
245 .aws_http_stream_get_incoming_response_status = s_aws_http_stream_get_incoming_response_status_mock,
246 .aws_http_stream_release = s_aws_http_stream_release_mock,
247 .aws_http_connection_close = s_aws_http_connection_close_mock};
248
249 static int s_aws_ecs_tester_init(struct aws_allocator *allocator) {
250 aws_auth_library_init(allocator);
251 s_tester.allocator = allocator;
252 if (aws_array_list_init_dynamic(&s_tester.response_data_callbacks, allocator, 10, sizeof(struct aws_byte_cursor))) {
253 return AWS_OP_ERR;
254 }
255
256 if (aws_mutex_init(&s_tester.lock)) {
257 return AWS_OP_ERR;
258 }
259
260 if (aws_condition_variable_init(&s_tester.signal)) {
261 return AWS_OP_ERR;
262 }
263
264 /* default to everything successful */
265 s_tester.is_connection_acquire_successful = true;
266 s_tester.is_request_successful = true;
267
268 s_tester.el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
269
270 struct aws_host_resolver_default_options resolver_options = {
271 .el_group = s_tester.el_group,
272 .max_entries = 8,
273 };
274 s_tester.host_resolver = aws_host_resolver_new_default(allocator, &resolver_options);
275
276 struct aws_client_bootstrap_options bootstrap_options = {
277 .event_loop_group = s_tester.el_group,
278 .host_resolver = s_tester.host_resolver,
279 };
280 s_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
281
282 struct aws_tls_ctx_options tls_options;
283 aws_tls_ctx_options_init_default_client(&tls_options, allocator);
284 struct aws_tls_ctx *tls_ctx = aws_tls_client_ctx_new(allocator, &tls_options);
285 ASSERT_NOT_NULL(tls_ctx);
286 s_tester.tls_ctx = tls_ctx;
287 aws_tls_ctx_options_clean_up(&tls_options);
288
289 /* ensure pre-existing environment doesn't interfere with tests */
290 aws_unset_environment_value(s_ecs_creds_env_relative_uri);
291 aws_unset_environment_value(s_ecs_creds_env_full_uri);
292 aws_unset_environment_value(s_ecs_creds_env_token_file);
293 aws_unset_environment_value(s_ecs_creds_env_token);
294
295 return AWS_OP_SUCCESS;
296 }
297
298 static void s_aws_ecs_tester_reset(void) {
299 aws_array_list_clean_up(&s_tester.response_data_callbacks);
300 aws_string_destroy(s_tester.request_path_and_query);
301 aws_string_destroy(s_tester.request_authorization_header);
302 aws_string_destroy(s_tester.selected_host);
303 aws_condition_variable_clean_up(&s_tester.signal);
304 aws_mutex_clean_up(&s_tester.lock);
305 aws_credentials_release(s_tester.credentials);
306 aws_client_bootstrap_release(s_tester.bootstrap);
307 aws_host_resolver_release(s_tester.host_resolver);
308 aws_event_loop_group_release(s_tester.el_group);
309 aws_tls_ctx_release(s_tester.tls_ctx);
310 AWS_ZERO_STRUCT(s_tester);
311 }
312
313 static void s_aws_ecs_tester_cleanup(void) {
314 s_aws_ecs_tester_reset();
315 aws_auth_library_clean_up();
316 }
317
318 static bool s_has_tester_received_credentials_callback(void *user_data) {
319 (void)user_data;
320
321 return s_tester.has_received_credentials_callback;
322 }
323
324 static void s_aws_wait_for_credentials_result(void) {
325 aws_mutex_lock(&s_tester.lock);
326 aws_condition_variable_wait_pred(
327 &s_tester.signal, &s_tester.lock, s_has_tester_received_credentials_callback, NULL);
328 aws_mutex_unlock(&s_tester.lock);
329 }
330
331 static void s_get_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
332 (void)user_data;
333
334 aws_mutex_lock(&s_tester.lock);
335 s_tester.has_received_credentials_callback = true;
336 s_tester.error_code = error_code;
337 s_tester.credentials = credentials;
338 aws_credentials_acquire(credentials);
339 aws_condition_variable_notify_one(&s_tester.signal);
340 aws_mutex_unlock(&s_tester.lock);
341 }
342
343 static int s_credentials_provider_ecs_new_destroy(struct aws_allocator *allocator, void *ctx) {
344 (void)ctx;
345
346 s_aws_ecs_tester_init(allocator);
347
348 struct aws_credentials_provider_ecs_options options = {
349 .bootstrap = s_tester.bootstrap,
350 .function_table = &s_mock_function_table,
351 .shutdown_options =
352 {
353 .shutdown_callback = s_on_shutdown_complete,
354 .shutdown_user_data = NULL,
355 },
356 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
357 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
358 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
359 };
360
361 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
362 aws_credentials_provider_release(provider);
363
364 s_aws_wait_for_provider_shutdown_callback();
365
366 /* Because we mock the http connection manager, we never get a callback back from it */
367 aws_mem_release(provider->allocator, provider);
368
369 s_aws_ecs_tester_cleanup();
370
371 return 0;
372 }
373
374 AWS_TEST_CASE(credentials_provider_ecs_new_destroy, s_credentials_provider_ecs_new_destroy);
375
376 static int s_credentials_provider_ecs_connect_failure(struct aws_allocator *allocator, void *ctx) {
377 (void)ctx;
378
379 s_aws_ecs_tester_init(allocator);
380 s_tester.is_connection_acquire_successful = false;
381
382 struct aws_credentials_provider_ecs_options options = {
383 .bootstrap = s_tester.bootstrap,
384 .function_table = &s_mock_function_table,
385 .shutdown_options =
386 {
387 .shutdown_callback = s_on_shutdown_complete,
388 .shutdown_user_data = NULL,
389 },
390 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
391 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
392 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
393 .tls_ctx = s_tester.tls_ctx,
394 };
395
396 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
397
398 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
399
400 s_aws_wait_for_credentials_result();
401
402 aws_mutex_lock(&s_tester.lock);
403 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
404 ASSERT_TRUE(s_tester.credentials == NULL);
405 ASSERT_UINT_EQUALS(443, s_tester.selected_port);
406 aws_mutex_unlock(&s_tester.lock);
407
408 aws_credentials_provider_release(provider);
409
410 s_aws_wait_for_provider_shutdown_callback();
411
412 /* Because we mock the http connection manager, we never get a callback back from it */
413 aws_mem_release(provider->allocator, provider);
414
415 s_aws_ecs_tester_cleanup();
416
417 return 0;
418 }
419
420 AWS_TEST_CASE(credentials_provider_ecs_connect_failure, s_credentials_provider_ecs_connect_failure);
421
422 static int s_credentials_provider_ecs_request_failure(struct aws_allocator *allocator, void *ctx) {
423 (void)ctx;
424
425 s_aws_ecs_tester_init(allocator);
426 s_tester.is_request_successful = false;
427
428 struct aws_credentials_provider_ecs_options options = {
429 .bootstrap = s_tester.bootstrap,
430 .function_table = &s_mock_function_table,
431 .shutdown_options =
432 {
433 .shutdown_callback = s_on_shutdown_complete,
434 .shutdown_user_data = NULL,
435 },
436 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
437 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
438 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
439 .tls_ctx = s_tester.tls_ctx,
440 };
441
442 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
443
444 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
445
446 s_aws_wait_for_credentials_result();
447
448 aws_mutex_lock(&s_tester.lock);
449 ASSERT_STR_EQUALS("/path/to/resource/?a=b&c=d", aws_string_c_str(s_tester.request_path_and_query));
450 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
451 ASSERT_TRUE(s_tester.credentials == NULL);
452 ASSERT_UINT_EQUALS(443, s_tester.selected_port);
453 ASSERT_UINT_EQUALS(4, s_tester.attempts);
454 aws_mutex_unlock(&s_tester.lock);
455
456 aws_credentials_provider_release(provider);
457
458 s_aws_wait_for_provider_shutdown_callback();
459
460 /* Because we mock the http connection manager, we never get a callback back from it */
461 aws_mem_release(provider->allocator, provider);
462
463 s_aws_ecs_tester_cleanup();
464
465 return 0;
466 }
467
468 AWS_TEST_CASE(credentials_provider_ecs_request_failure, s_credentials_provider_ecs_request_failure);
469
470 AWS_STATIC_STRING_FROM_LITERAL(s_bad_document_response, "{\"NotTheExpectedDocumentFormat\":\"Error\"}");
471
472 static int s_credentials_provider_ecs_bad_document_failure(struct aws_allocator *allocator, void *ctx) {
473 (void)ctx;
474
475 s_aws_ecs_tester_init(allocator);
476
477 struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_bad_document_response);
478 aws_array_list_push_back(&s_tester.response_data_callbacks, &bad_document_cursor);
479
480 struct aws_credentials_provider_ecs_options options = {
481 .bootstrap = s_tester.bootstrap,
482 .function_table = &s_mock_function_table,
483 .shutdown_options =
484 {
485 .shutdown_callback = s_on_shutdown_complete,
486 .shutdown_user_data = NULL,
487 },
488 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
489 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
490 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
491 .tls_ctx = s_tester.tls_ctx,
492 };
493
494 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
495
496 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
497
498 s_aws_wait_for_credentials_result();
499
500 aws_mutex_lock(&s_tester.lock);
501 ASSERT_STR_EQUALS("/path/to/resource/?a=b&c=d", aws_string_c_str(s_tester.request_path_and_query));
502
503 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
504 ASSERT_TRUE(s_tester.credentials == NULL);
505 ASSERT_UINT_EQUALS(443, s_tester.selected_port);
506 aws_mutex_unlock(&s_tester.lock);
507
508 aws_credentials_provider_release(provider);
509
510 s_aws_wait_for_provider_shutdown_callback();
511
512 /* Because we mock the http connection manager, we never get a callback back from it */
513 aws_mem_release(provider->allocator, provider);
514
515 s_aws_ecs_tester_cleanup();
516
517 return 0;
518 }
519
520 AWS_TEST_CASE(credentials_provider_ecs_bad_document_failure, s_credentials_provider_ecs_bad_document_failure);
521
522 static int s_credentials_provider_ecs_bad_host_failure(struct aws_allocator *allocator, void *ctx) {
523 (void)ctx;
524
525 s_aws_ecs_tester_init(allocator);
526
527 struct aws_credentials_provider_ecs_options options = {
528 .bootstrap = s_tester.bootstrap,
529 .function_table = &s_mock_function_table,
530 .shutdown_options =
531 {
532 .shutdown_callback = s_on_shutdown_complete,
533 .shutdown_user_data = NULL,
534 },
535 .host = aws_byte_cursor_from_c_str("www.google.com"),
536 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
537 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
538 };
539
540 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
541
542 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
543
544 s_aws_wait_for_credentials_result();
545
546 aws_mutex_lock(&s_tester.lock);
547 ASSERT_NULL(s_tester.credentials);
548 ASSERT_INT_EQUALS(AWS_AUTH_CREDENTIALS_PROVIDER_ECS_INVALID_HOST, s_tester.error_code);
549 aws_mutex_unlock(&s_tester.lock);
550
551 aws_credentials_provider_release(provider);
552
553 s_aws_wait_for_provider_shutdown_callback();
554
555 /* Because we mock the http connection manager, we never get a callback back from it */
556 aws_mem_release(provider->allocator, provider);
557
558 s_aws_ecs_tester_cleanup();
559
560 return 0;
561 }
562
563 AWS_TEST_CASE(credentials_provider_ecs_bad_host_failure, s_credentials_provider_ecs_bad_host_failure);
564
565 AWS_STATIC_STRING_FROM_LITERAL(
566 s_good_response,
567 "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"SecretAccessKey\":\"SuccessfulSecret\", \n "
568 "\"Token\":\"TokenSuccess\", \n \"Expiration\":\"2020-02-25T06:03:31Z\"}");
569 AWS_STATIC_STRING_FROM_LITERAL(
570 s_good_response_with_account_id,
571 "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"SecretAccessKey\":\"SuccessfulSecret\", \n "
572 "\"Token\":\"TokenSuccess\", \n \"Expiration\":\"2020-02-25T06:03:31Z\", \"AccountId\":\"AccountId123\"}");
573 AWS_STATIC_STRING_FROM_LITERAL(s_good_access_key_id, "SuccessfulAccessKey");
574 AWS_STATIC_STRING_FROM_LITERAL(s_good_secret_access_key, "SuccessfulSecret");
575 AWS_STATIC_STRING_FROM_LITERAL(s_good_session_token, "TokenSuccess");
576 AWS_STATIC_STRING_FROM_LITERAL(s_good_account_id, "AccountId123");
577 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_expiration, "2020-02-25T06:03:31Z");
578
579 /* Check that expected URI and Authorization token were used to make request.
580 * URI must be super explicit, specifying scheme and port. */
581 static int s_check_ecs_tester_request_uri_and_authorization(const char *expected_uri_cstr, const char *expected_token) {
582 struct aws_byte_cursor expected_uri_cursor = aws_byte_cursor_from_c_str(expected_uri_cstr);
583 struct aws_uri expected_uri;
584 ASSERT_SUCCESS(aws_uri_init_parse(&expected_uri, s_tester.allocator, &expected_uri_cursor));
585 ASSERT_TRUE(aws_uri_scheme(&expected_uri)->len != 0);
586 ASSERT_TRUE(aws_uri_port(&expected_uri) != 0);
587
588 ASSERT_CURSOR_VALUE_STRING_EQUALS(*aws_uri_host_name(&expected_uri), s_tester.selected_host);
589 ASSERT_CURSOR_VALUE_STRING_EQUALS(*aws_uri_path_and_query(&expected_uri), s_tester.request_path_and_query);
590 ASSERT_INT_EQUALS(
591 aws_byte_cursor_eq_c_str_ignore_case(aws_uri_scheme(&expected_uri), "https"), s_tester.selected_tls);
592 ASSERT_UINT_EQUALS(aws_uri_port(&expected_uri), s_tester.selected_port);
593
594 if (expected_token != NULL) {
595 ASSERT_STR_EQUALS(expected_token, aws_string_c_str(s_tester.request_authorization_header));
596 } else {
597 ASSERT_NULL(s_tester.request_authorization_header);
598 }
599
600 aws_uri_clean_up(&expected_uri);
601 return 0;
602 }
603
604 static int s_do_ecs_success_test(
605 struct aws_allocator *allocator,
606 struct aws_credentials_provider_ecs_options *options,
607 const char *expected_uri,
608 const char *expected_token,
609 const struct aws_string *expected_account_id) {
610 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, options);
611
612 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
613
614 s_aws_wait_for_credentials_result();
615
616 aws_mutex_lock(&s_tester.lock);
617 ASSERT_SUCCESS(s_check_ecs_tester_request_uri_and_authorization(expected_uri, expected_token));
618 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
619 ASSERT_TRUE(s_tester.credentials != NULL);
620 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(s_tester.credentials), s_good_access_key_id);
621 ASSERT_CURSOR_VALUE_STRING_EQUALS(
622 aws_credentials_get_secret_access_key(s_tester.credentials), s_good_secret_access_key);
623 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_session_token(s_tester.credentials), s_good_session_token);
624 if (expected_account_id) {
625 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_account_id(s_tester.credentials), expected_account_id);
626 } else {
627 ASSERT_TRUE(aws_credentials_get_account_id(s_tester.credentials).len == 0);
628 }
629
630 struct aws_date_time expiration;
631 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
632 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
633 ASSERT_TRUE(
634 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == (uint64_t)expiration.timestamp);
635 aws_mutex_unlock(&s_tester.lock);
636
637 aws_credentials_provider_release(provider);
638
639 s_aws_wait_for_provider_shutdown_callback();
640
641 /* Because we mock the http connection manager, we never get a callback back from it */
642 aws_mem_release(provider->allocator, provider);
643
644 return AWS_OP_SUCCESS;
645 }
646
647 static int s_do_ecs_env_success_test(
648 struct aws_allocator *allocator,
649 struct aws_credentials_provider_ecs_environment_options *options,
650 const char *relative_uri,
651 const char *full_uri,
652 const char *auth_token,
653 const char *auth_token_file_content,
654 const char *expected_uri,
655 const char *expected_token) {
656
657 struct aws_string *relative_uri_str = NULL;
658 if (relative_uri != NULL) {
659 relative_uri_str = aws_string_new_from_c_str(allocator, relative_uri);
660 ASSERT_SUCCESS(aws_set_environment_value(s_ecs_creds_env_relative_uri, relative_uri_str));
661 }
662 struct aws_string *full_uri_str = NULL;
663 if (full_uri != NULL) {
664 full_uri_str = aws_string_new_from_c_str(allocator, full_uri);
665 ASSERT_SUCCESS(aws_set_environment_value(s_ecs_creds_env_full_uri, full_uri_str));
666 }
667 struct aws_string *auth_token_str = NULL;
668 if (auth_token != NULL) {
669 auth_token_str = aws_string_new_from_c_str(allocator, auth_token);
670 ASSERT_SUCCESS(aws_set_environment_value(s_ecs_creds_env_token, auth_token_str));
671 }
672 struct aws_string *auth_token_file_contents_str = NULL;
673 struct aws_string *auth_token_file_path = NULL;
674 if (auth_token_file_content != NULL) {
675 auth_token_file_contents_str = aws_string_new_from_c_str(allocator, auth_token_file_content);
676
677 auth_token_file_path = aws_create_process_unique_file_name(allocator);
678 ASSERT_NOT_NULL(auth_token_file_path);
679 ASSERT_TRUE(aws_create_profile_file(auth_token_file_path, auth_token_file_contents_str) == AWS_OP_SUCCESS);
680 ASSERT_SUCCESS(aws_set_environment_value(s_ecs_creds_env_token_file, auth_token_file_path));
681 }
682
683 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs_from_environment(allocator, options);
684
685 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
686
687 s_aws_wait_for_credentials_result();
688
689 aws_mutex_lock(&s_tester.lock);
690 ASSERT_SUCCESS(s_check_ecs_tester_request_uri_and_authorization(expected_uri, expected_token));
691
692 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
693 ASSERT_TRUE(s_tester.credentials != NULL);
694 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(s_tester.credentials), s_good_access_key_id);
695 ASSERT_CURSOR_VALUE_STRING_EQUALS(
696 aws_credentials_get_secret_access_key(s_tester.credentials), s_good_secret_access_key);
697 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_session_token(s_tester.credentials), s_good_session_token);
698
699 struct aws_date_time expiration;
700 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
701 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
702 ASSERT_TRUE(
703 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == (uint64_t)expiration.timestamp);
704 aws_mutex_unlock(&s_tester.lock);
705
706 aws_credentials_provider_release(provider);
707
708 s_aws_wait_for_provider_shutdown_callback();
709
710 /* Because we mock the http connection manager, we never get a callback back from it */
711 aws_mem_release(provider->allocator, provider);
712 if (auth_token_file_path != NULL) {
713 aws_file_delete(auth_token_file_path);
714 }
715 aws_string_destroy(relative_uri_str);
716 aws_string_destroy(full_uri_str);
717 aws_string_destroy(auth_token_str);
718 aws_string_destroy(auth_token_file_contents_str);
719 aws_string_destroy(auth_token_file_path);
720
721 return AWS_OP_SUCCESS;
722 }
723
724 static int s_credentials_provider_ecs_basic_success(struct aws_allocator *allocator, void *ctx) {
725 (void)ctx;
726
727 s_aws_ecs_tester_init(allocator);
728
729 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
730 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
731
732 struct aws_credentials_provider_ecs_options options = {
733 .bootstrap = s_tester.bootstrap,
734 .function_table = &s_mock_function_table,
735 .shutdown_options =
736 {
737 .shutdown_callback = s_on_shutdown_complete,
738 .shutdown_user_data = NULL,
739 },
740 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
741 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
742 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
743 .tls_ctx = s_tester.tls_ctx,
744 };
745
746 ASSERT_SUCCESS(s_do_ecs_success_test(
747 allocator,
748 &options,
749 "https://www.xxx123321testmocknonexsitingawsservice.com:443/path/to/resource/?a=b&c=d" /*expected_uri*/,
750 "test-token-1234-abcd" /*expected_token*/,
751 NULL /*expected_account_id*/));
752
753 s_aws_ecs_tester_cleanup();
754
755 return 0;
756 }
757
758 AWS_TEST_CASE(credentials_provider_ecs_basic_success, s_credentials_provider_ecs_basic_success);
759
760 AWS_STATIC_STRING_FROM_LITERAL(
761 s_good_response_no_token,
762 "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"SecretAccessKey\":\"SuccessfulSecret\", \n "
763 "\"Expiration\":\"2020-02-25T06:03:31Z\"}");
764
765 static int s_credentials_provider_ecs_basic_success_no_session_token(struct aws_allocator *allocator, void *ctx) {
766 (void)ctx;
767
768 s_aws_ecs_tester_init(allocator);
769
770 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response_no_token);
771 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
772
773 struct aws_credentials_provider_ecs_options options = {
774 .bootstrap = s_tester.bootstrap,
775 .function_table = &s_mock_function_table,
776 .shutdown_options =
777 {
778 .shutdown_callback = s_on_shutdown_complete,
779 .shutdown_user_data = NULL,
780 },
781 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
782 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
783 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
784 .tls_ctx = s_tester.tls_ctx,
785 };
786
787 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
788 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
789 s_aws_wait_for_credentials_result();
790
791 aws_mutex_lock(&s_tester.lock);
792 ASSERT_SUCCESS(s_check_ecs_tester_request_uri_and_authorization(
793 "https://www.xxx123321testmocknonexsitingawsservice.com:443/path/to/resource/?a=b&c=d",
794 "test-token-1234-abcd"));
795 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
796 ASSERT_TRUE(s_tester.credentials != NULL);
797 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(s_tester.credentials), s_good_access_key_id);
798 ASSERT_CURSOR_VALUE_STRING_EQUALS(
799 aws_credentials_get_secret_access_key(s_tester.credentials), s_good_secret_access_key);
800 ASSERT_TRUE(aws_credentials_get_session_token(s_tester.credentials).len == 0);
801
802 struct aws_date_time expiration;
803 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
804 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
805 ASSERT_TRUE(
806 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == (uint64_t)expiration.timestamp);
807 aws_mutex_unlock(&s_tester.lock);
808
809 aws_credentials_provider_release(provider);
810 s_aws_wait_for_provider_shutdown_callback();
811 aws_mem_release(provider->allocator, provider);
812
813 s_aws_ecs_tester_cleanup();
814
815 return 0;
816 }
817
818 AWS_TEST_CASE(
819 credentials_provider_ecs_basic_success_no_session_token,
820 s_credentials_provider_ecs_basic_success_no_session_token);
821
822 static int s_credentials_provider_ecs_basic_success_with_account_id(struct aws_allocator *allocator, void *ctx) {
823 (void)ctx;
824
825 s_aws_ecs_tester_init(allocator);
826
827 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response_with_account_id);
828 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
829
830 struct aws_credentials_provider_ecs_options options = {
831 .bootstrap = s_tester.bootstrap,
832 .function_table = &s_mock_function_table,
833 .shutdown_options =
834 {
835 .shutdown_callback = s_on_shutdown_complete,
836 .shutdown_user_data = NULL,
837 },
838 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
839 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
840 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
841 .tls_ctx = s_tester.tls_ctx,
842 };
843
844 ASSERT_SUCCESS(s_do_ecs_success_test(
845 allocator,
846 &options,
847 "https://www.xxx123321testmocknonexsitingawsservice.com:443/path/to/resource/?a=b&c=d" /*expected_uri*/,
848 "test-token-1234-abcd" /*expected_token*/,
849 s_good_account_id /*expected_account_id*/));
850
851 s_aws_ecs_tester_cleanup();
852
853 return 0;
854 }
855
856 AWS_TEST_CASE(
857 credentials_provider_ecs_basic_success_with_account_id,
858 s_credentials_provider_ecs_basic_success_with_account_id);
859
860 static int s_credentials_provider_ecs_basic_success_token_file(struct aws_allocator *allocator, void *ctx) {
861 (void)ctx;
862
863 s_aws_ecs_tester_init(allocator);
864
865 struct aws_string *auth_token = aws_string_new_from_c_str(allocator, "test-token-1234-abcd");
866 struct aws_string *token_file_path = aws_create_process_unique_file_name(allocator);
867 ASSERT_NOT_NULL(token_file_path);
868 ASSERT_TRUE(aws_create_profile_file(token_file_path, auth_token) == AWS_OP_SUCCESS);
869
870 /* test that static auth token is not preferred over file token */
871 struct aws_string *bad_auth_token = aws_string_new_from_c_str(allocator, "badtoken");
872
873 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
874 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
875 struct aws_credentials_provider_ecs_options options = {
876 .bootstrap = s_tester.bootstrap,
877 .function_table = &s_mock_function_table,
878 .shutdown_options =
879 {
880 .shutdown_callback = s_on_shutdown_complete,
881 .shutdown_user_data = NULL,
882 },
883 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
884 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
885 .auth_token = aws_byte_cursor_from_string(auth_token),
886 .auth_token_file_path = aws_byte_cursor_from_string(token_file_path),
887 .tls_ctx = s_tester.tls_ctx,
888 };
889
890 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
891
892 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
893
894 s_aws_wait_for_credentials_result();
895
896 aws_mutex_lock(&s_tester.lock);
897 ASSERT_SUCCESS(s_check_ecs_tester_request_uri_and_authorization(
898 "https://www.xxx123321testmocknonexsitingawsservice.com:443/path/to/resource/?a=b&c=d",
899 aws_string_c_str(auth_token)));
900
901 s_tester.has_received_credentials_callback = false;
902 aws_credentials_release(s_tester.credentials);
903
904 aws_mutex_unlock(&s_tester.lock);
905
906 /* update the file with updated token */
907 struct aws_string *auth_token2 = aws_string_new_from_c_str(allocator, "test-token2-4321-qwer");
908 ASSERT_TRUE(aws_create_profile_file(token_file_path, auth_token2) == AWS_OP_SUCCESS);
909
910 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
911
912 s_aws_wait_for_credentials_result();
913
914 aws_mutex_lock(&s_tester.lock);
915 ASSERT_SUCCESS(s_check_ecs_tester_request_uri_and_authorization(
916 "https://www.xxx123321testmocknonexsitingawsservice.com:443/path/to/resource/?a=b&c=d",
917 aws_string_c_str(auth_token2)));
918
919 aws_mutex_unlock(&s_tester.lock);
920
921 aws_credentials_provider_release(provider);
922
923 s_aws_wait_for_provider_shutdown_callback();
924
925 /* Because we mock the http connection manager, we never get a callback back from it */
926 aws_mem_release(provider->allocator, provider);
927
928 s_aws_ecs_tester_cleanup();
929 aws_file_delete(token_file_path);
930 aws_string_destroy(auth_token);
931 aws_string_destroy(auth_token2);
932 aws_string_destroy(token_file_path);
933 aws_string_destroy(bad_auth_token);
934 return 0;
935 }
936 AWS_TEST_CASE(credentials_provider_ecs_basic_success_token_file, s_credentials_provider_ecs_basic_success_token_file);
937
938 static int s_credentials_provider_ecs_basic_success_uri_env(struct aws_allocator *allocator, void *ctx) {
939 (void)ctx;
940 aws_auth_library_init(allocator);
941 const struct test_case {
942 const char *relative_uri;
943 const char *full_uri;
944 const char *expected_uri;
945 const char *auth_token;
946 const char *auth_token_file_content;
947 const char *expected_auth_token;
948 } test_cases[] = {
949 /* simple full uri*/
950 {
951 .full_uri = "http://127.0.0.1/credentials",
952 .expected_uri = "http://127.0.0.1:80/credentials",
953 },
954 /* explicit port */
955 {
956 .full_uri = "http://127.0.0.1:8080/credentials",
957 .expected_uri = "http://127.0.0.1:8080/credentials",
958 },
959 /* https */
960 {
961 .full_uri = "https://www.xxx123321testmocknonexsitingawsservice.com/credentials",
962 .expected_uri = "https://www.xxx123321testmocknonexsitingawsservice.com:443/credentials",
963 },
964 /* path and query */
965 {
966 .full_uri = "http://127.0.0.1/path/to/resource/?a=b&c=d",
967 .expected_uri = "http://127.0.0.1:80/path/to/resource/?a=b&c=d",
968 },
969 /* relative URI */
970 {
971 .relative_uri = "/path/to/resource/?a=b&c=d",
972 .expected_uri = "http://169.254.170.2:80/path/to/resource/?a=b&c=d",
973 },
974 /* relative URI takes priority, when both RELATIVE and FULL are set */
975 {
976 .relative_uri = "/from-relative-uri",
977 .full_uri = "http://127.0.0.1/from-full-uri",
978 .expected_uri = "http://169.254.170.2:80/from-relative-uri",
979 },
980 /* auth token is properly set */
981 {
982 .full_uri = "http://127.0.0.1:8080/credentials",
983 .expected_uri = "http://127.0.0.1:8080/credentials",
984 .auth_token = "testToken",
985 .expected_auth_token = "testToken",
986 },
987 /* auth_token is respected */
988 {
989 .full_uri = "http://127.0.0.1:8080/credentials",
990 .expected_uri = "http://127.0.0.1:8080/credentials",
991 .auth_token = "testToken",
992 .expected_auth_token = "testToken",
993 },
994 /* auth_token_file_path is respected */
995 {
996 .full_uri = "http://127.0.0.1:8080/credentials",
997 .expected_uri = "http://127.0.0.1:8080/credentials",
998 .auth_token_file_content = "testToken",
999 .expected_auth_token = "testToken",
1000 },
1001 /* auth_token_file_path is preferred */
1002 {
1003 .full_uri = "http://127.0.0.1:8080/credentials",
1004 .expected_uri = "http://127.0.0.1:8080/credentials",
1005 .auth_token = "BadToken",
1006 .auth_token_file_content = "testToken",
1007 .expected_auth_token = "testToken",
1008 },
1009 /* IPv4 loopback address */
1010 {
1011 .full_uri = "http://127.1.2.3:456/credentials",
1012 .expected_uri = "http://127.1.2.3:456/credentials",
1013 },
1014 /* IPv4 EKS container host address */
1015 {
1016 .full_uri = "http://169.254.170.23:8080/credentials",
1017 .expected_uri = "http://169.254.170.23:8080/credentials",
1018 },
1019 /* IPv4 ECS container host address */
1020 {
1021 .full_uri = "http://169.254.170.2:8080/credentials",
1022 .expected_uri = "http://169.254.170.2:8080/credentials",
1023 },
1024 /* IPv6 loopback address */
1025 {
1026 .full_uri = "http://[::1]:8080/credentials",
1027 .expected_uri = "http://[::1]:8080/credentials",
1028 },
1029 /* IPv6 EKS host */
1030 {
1031 .full_uri = "http://[fd00:ec2::23]:8080/credentials",
1032 .expected_uri = "http://[fd00:ec2::23]:8080/credentials",
1033 },
1034 };
1035
1036 for (size_t case_idx = 0; case_idx < AWS_ARRAY_SIZE(test_cases); ++case_idx) {
1037 struct test_case case_i = test_cases[case_idx];
1038 printf(
1039 "CASE[%zu]: AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=%s AWS_CONTAINER_CREDENTIALS_FULL_URI=%s\n, "
1040 "AWS_CONTAINER_AUTHORIZATION_TOKEN=%s\n, auth_token_file_content=%s\n",
1041 case_idx,
1042 case_i.relative_uri ? case_i.relative_uri : "<UNSET>",
1043 case_i.full_uri ? case_i.full_uri : "<UNSET>",
1044 case_i.auth_token ? case_i.auth_token : "<UNSET>",
1045 case_i.auth_token_file_content ? case_i.auth_token_file_content : "<UNSET>");
1046
1047 /* This unsets previous env vars */
1048 ASSERT_SUCCESS(s_aws_ecs_tester_init(allocator));
1049
1050 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1051 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
1052
1053 struct aws_credentials_provider_ecs_environment_options options = {
1054 .bootstrap = s_tester.bootstrap,
1055 .function_table = &s_mock_function_table,
1056 .shutdown_options =
1057 {
1058 .shutdown_callback = s_on_shutdown_complete,
1059 .shutdown_user_data = NULL,
1060 },
1061 .tls_ctx = s_tester.tls_ctx,
1062 };
1063
1064 ASSERT_SUCCESS(s_do_ecs_env_success_test(
1065 allocator,
1066 &options,
1067 case_i.relative_uri,
1068 case_i.full_uri,
1069 case_i.auth_token,
1070 case_i.auth_token_file_content,
1071 case_i.expected_uri,
1072 case_i.expected_auth_token));
1073
1074 s_aws_ecs_tester_reset();
1075 }
1076
1077 s_aws_ecs_tester_cleanup();
1078 return 0;
1079 }
1080 AWS_TEST_CASE(credentials_provider_ecs_basic_success_uri_env, s_credentials_provider_ecs_basic_success_uri_env);
1081
1082 static int s_credentials_provider_ecs_no_auth_token_success(struct aws_allocator *allocator, void *ctx) {
1083 (void)ctx;
1084
1085 s_aws_ecs_tester_init(allocator);
1086
1087 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1088 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor);
1089
1090 struct aws_credentials_provider_ecs_options options = {
1091 .bootstrap = s_tester.bootstrap,
1092 .function_table = &s_mock_function_table,
1093 .shutdown_options =
1094 {
1095 .shutdown_callback = s_on_shutdown_complete,
1096 .shutdown_user_data = NULL,
1097 },
1098 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
1099 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
1100 .tls_ctx = s_tester.tls_ctx,
1101 };
1102
1103 ASSERT_SUCCESS(s_do_ecs_success_test(
1104 allocator,
1105 &options,
1106 "https://www.xxx123321testmocknonexsitingawsservice.com:443/path/to/resource/?a=b&c=d" /*expected_uri*/,
1107 NULL /*expected_token*/,
1108 NULL /*expected_account_id=*/));
1109
1110 s_aws_ecs_tester_cleanup();
1111
1112 return 0;
1113 }
1114
1115 AWS_TEST_CASE(credentials_provider_ecs_no_auth_token_success, s_credentials_provider_ecs_no_auth_token_success);
1116
1117 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_first_part, "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"Secret");
1118 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_second_part, "AccessKey\":\"SuccessfulSecret\", \n \"Token\":\"Token");
1119 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_third_part, "Success\", \n \"Expiration\":\"2020-02-25T06:03:31Z\"}");
1120
1121 static int s_credentials_provider_ecs_success_multi_part_doc(struct aws_allocator *allocator, void *ctx) {
1122 (void)ctx;
1123
1124 s_aws_ecs_tester_init(allocator);
1125
1126 struct aws_byte_cursor good_response_cursor1 = aws_byte_cursor_from_string(s_good_response_first_part);
1127 struct aws_byte_cursor good_response_cursor2 = aws_byte_cursor_from_string(s_good_response_second_part);
1128 struct aws_byte_cursor good_response_cursor3 = aws_byte_cursor_from_string(s_good_response_third_part);
1129 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor1);
1130 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor2);
1131 aws_array_list_push_back(&s_tester.response_data_callbacks, &good_response_cursor3);
1132
1133 struct aws_credentials_provider_ecs_options options = {
1134 .bootstrap = s_tester.bootstrap,
1135 .function_table = &s_mock_function_table,
1136 .shutdown_options =
1137 {
1138 .shutdown_callback = s_on_shutdown_complete,
1139 .shutdown_user_data = NULL,
1140 },
1141 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
1142 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
1143 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
1144 .tls_ctx = s_tester.tls_ctx,
1145 };
1146
1147 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
1148
1149 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1150
1151 s_aws_wait_for_credentials_result();
1152
1153 aws_mutex_lock(&s_tester.lock);
1154 ASSERT_STR_EQUALS("/path/to/resource/?a=b&c=d", aws_string_c_str(s_tester.request_path_and_query));
1155
1156 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
1157 ASSERT_TRUE(s_tester.credentials != NULL);
1158 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(s_tester.credentials), s_good_access_key_id);
1159 ASSERT_CURSOR_VALUE_STRING_EQUALS(
1160 aws_credentials_get_secret_access_key(s_tester.credentials), s_good_secret_access_key);
1161 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_session_token(s_tester.credentials), s_good_session_token);
1162
1163 struct aws_date_time expiration;
1164 struct aws_byte_cursor date_cursor = aws_byte_cursor_from_string(s_good_response_expiration);
1165 aws_date_time_init_from_str_cursor(&expiration, &date_cursor, AWS_DATE_FORMAT_ISO_8601);
1166 ASSERT_TRUE(
1167 aws_credentials_get_expiration_timepoint_seconds(s_tester.credentials) == (uint64_t)expiration.timestamp);
1168 aws_mutex_unlock(&s_tester.lock);
1169
1170 aws_credentials_provider_release(provider);
1171
1172 s_aws_wait_for_provider_shutdown_callback();
1173
1174 /* Because we mock the http connection manager, we never get a callback back from it */
1175 aws_mem_release(provider->allocator, provider);
1176 s_aws_ecs_tester_cleanup();
1177
1178 return 0;
1179 }
1180
1181 AWS_TEST_CASE(credentials_provider_ecs_success_multi_part_doc, s_credentials_provider_ecs_success_multi_part_doc);
1182
1183 static int s_credentials_provider_ecs_real_new_destroy(struct aws_allocator *allocator, void *ctx) {
1184 (void)ctx;
1185
1186 aws_auth_library_init(allocator);
1187
1188 s_aws_ecs_tester_init(allocator);
1189
1190 struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
1191
1192 struct aws_host_resolver_default_options resolver_options = {
1193 .el_group = el_group,
1194 .max_entries = 8,
1195 };
1196 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1197
1198 struct aws_client_bootstrap_options bootstrap_options = {
1199 .event_loop_group = el_group,
1200 .host_resolver = resolver,
1201 };
1202 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1203
1204 struct aws_credentials_provider_ecs_options options = {
1205 .bootstrap = bootstrap,
1206 .shutdown_options =
1207 {
1208 .shutdown_callback = s_on_shutdown_complete,
1209 .shutdown_user_data = NULL,
1210 },
1211 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
1212 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
1213 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
1214 };
1215
1216 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
1217
1218 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1219
1220 s_aws_wait_for_credentials_result();
1221
1222 aws_credentials_provider_release(provider);
1223
1224 s_aws_wait_for_provider_shutdown_callback();
1225
1226 aws_client_bootstrap_release(bootstrap);
1227 aws_host_resolver_release(resolver);
1228 aws_event_loop_group_release(el_group);
1229
1230 s_aws_ecs_tester_cleanup();
1231
1232 aws_auth_library_clean_up();
1233
1234 return 0;
1235 }
1236
1237 AWS_TEST_CASE(credentials_provider_ecs_real_new_destroy, s_credentials_provider_ecs_real_new_destroy);
1238
1239 static int s_credentials_provider_ecs_real_success(struct aws_allocator *allocator, void *ctx) {
1240 (void)ctx;
1241
1242 aws_auth_library_init(allocator);
1243
1244 s_aws_ecs_tester_init(allocator);
1245
1246 struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
1247
1248 struct aws_host_resolver_default_options resolver_options = {
1249 .el_group = el_group,
1250 .max_entries = 8,
1251 };
1252 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1253
1254 struct aws_client_bootstrap_options bootstrap_options = {
1255 .event_loop_group = el_group,
1256 .host_resolver = resolver,
1257 };
1258 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1259
1260 struct aws_credentials_provider_ecs_options options = {
1261 .bootstrap = bootstrap,
1262 .shutdown_options =
1263 {
1264 .shutdown_callback = s_on_shutdown_complete,
1265 .shutdown_user_data = NULL,
1266 },
1267 .host = aws_byte_cursor_from_c_str("www.xxx123321testmocknonexsitingawsservice.com"),
1268 .path_and_query = aws_byte_cursor_from_c_str("/path/to/resource/?a=b&c=d"),
1269 .auth_token = aws_byte_cursor_from_c_str("test-token-1234-abcd"),
1270 };
1271
1272 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
1273
1274 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1275
1276 s_aws_wait_for_credentials_result();
1277
1278 aws_mutex_lock(&s_tester.lock);
1279 ASSERT_TRUE(s_tester.credentials != NULL);
1280 aws_mutex_unlock(&s_tester.lock);
1281
1282 aws_credentials_provider_release(provider);
1283
1284 s_aws_wait_for_provider_shutdown_callback();
1285
1286 s_aws_ecs_tester_cleanup();
1287
1288 aws_client_bootstrap_release(bootstrap);
1289 aws_host_resolver_release(resolver);
1290 aws_event_loop_group_release(el_group);
1291
1292 aws_auth_library_clean_up();
1293
1294 return 0;
1295 }
1296
1297 AWS_TEST_CASE(credentials_provider_ecs_real_success, s_credentials_provider_ecs_real_success);
1298
1299 static int s_credentials_provider_ecs_proxy_routing_enabled_test(struct aws_allocator *allocator, void *ctx) {
1300 (void)ctx;
1301
1302 s_aws_ecs_tester_init(allocator);
1303
1304 struct proxy_env_var_settings proxy_config = {
1305 .env_var_type = AWS_HPEV_ENABLE,
1306 };
1307
1308 s_tester.proxy_config = &proxy_config;
1309
1310 struct aws_credentials_provider_ecs_options options = {
1311 .bootstrap = s_tester.bootstrap,
1312 .function_table = &s_mock_function_table,
1313 .shutdown_options =
1314 {
1315 .shutdown_callback = s_on_shutdown_complete,
1316 .shutdown_user_data = NULL,
1317 },
1318 .host = aws_byte_cursor_from_c_str("127.0.0.1"),
1319 .path_and_query = aws_byte_cursor_from_c_str("/path"),
1320 .tls_ctx = s_tester.tls_ctx,
1321 .proxy_ev_settings = &proxy_config,
1322 };
1323
1324 struct aws_credentials_provider *provider = aws_credentials_provider_new_ecs(allocator, &options);
1325
1326 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1327
1328 s_aws_wait_for_credentials_result();
1329
1330 ASSERT_TRUE(s_tester.credentials == NULL);
1331
1332 aws_credentials_provider_release(provider);
1333
1334 s_aws_wait_for_provider_shutdown_callback();
1335
1336 aws_mem_release(provider->allocator, provider);
1337
1338 s_aws_ecs_tester_cleanup();
1339
1340 return 0;
1341 }
1342 AWS_TEST_CASE(
1343 credentials_provider_ecs_proxy_routing_enabled_test,
1344 s_credentials_provider_ecs_proxy_routing_enabled_test);