Debian Codegraph @

crt/aws-c-auth/tests/credentials_provider_imds_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 <aws/auth/credentials.h>
9 #include <aws/auth/private/credentials_utils.h>
10 #include <aws/common/clock.h>
11 #include <aws/common/condition_variable.h>
12 #include <aws/common/environment.h>
13 #include <aws/common/string.h>
14 #include <aws/common/thread.h>
15 #include <aws/http/proxy.h>
16 #include <aws/http/request_response.h>
17 #include <aws/http/status_code.h>
18 #include <aws/io/channel_bootstrap.h>
19 #include <aws/io/event_loop.h>
20 #include <aws/io/logging.h>
21 #include <aws/io/socket.h>
22
23 /**
24 * The max requests SDK could make to IMDS V2 should be 4.
25 * 1. query_role_name -> Get 401, unauthorized, then switch to secure way.
26 * 2. query_token
27 * 3. query role name
28 * 4. query role
29 *
30 * By default, the requests made is 3.
31 * 1. query_token (unless gets 400, no matter succeed or not, next step is query role name w/o token)
32 * 2. query role name.
33 * 3. query role
34 *
35 * Well, IMDS could act crazy then client would keep switching between secure and insecure way.
36 * We will not handle this extreme case.
37 */
38 #define IMDS_MAX_REQUESTS (8)
39 struct aws_mock_imds_tester {
40 struct aws_allocator *allocator;
41 struct aws_byte_buf request_uris[IMDS_MAX_REQUESTS];
42 struct aws_array_list response_data_callbacks[IMDS_MAX_REQUESTS];
43
44 int current_request;
45 int response_code[IMDS_MAX_REQUESTS];
46 int token_request_idx;
47
48 bool is_connection_acquire_successful;
49
50 struct aws_mutex lock;
51 struct aws_condition_variable signal;
52
53 struct aws_credentials *credentials;
54 struct aws_event_loop_group *el_group;
55 struct aws_client_bootstrap *bootstrap;
56 bool has_received_credentials_callback;
57 bool has_received_shutdown_callback;
58
59 int error_code;
60
61 bool token_ttl_header_exist[IMDS_MAX_REQUESTS];
62 bool token_ttl_header_expected[IMDS_MAX_REQUESTS];
63 bool token_header_exist[IMDS_MAX_REQUESTS];
64 bool token_header_expected[IMDS_MAX_REQUESTS];
65 bool alternate_closed_connections;
66
67 struct proxy_env_var_settings *proxy_config;
68
69 /* Endpoint verification fields */
70 struct aws_byte_buf expected_host;
71 uint16_t expected_port;
72 bool verify_endpoint_host;
73 };
74
75 static struct aws_mock_imds_tester s_tester;
76
77 struct aws_credentials_provider_imds_impl {
78 struct aws_imds_client *client;
79 };
80
81 static void s_on_shutdown_complete(void *user_data) {
82 (void)user_data;
83
84 aws_mutex_lock(&s_tester.lock);
85 s_tester.has_received_shutdown_callback = true;
86 aws_mutex_unlock(&s_tester.lock);
87
88 aws_condition_variable_notify_one(&s_tester.signal);
89 }
90
91 static bool s_has_tester_received_shutdown_callback(void *user_data) {
92 (void)user_data;
93
94 return s_tester.has_received_shutdown_callback;
95 }
96
97 static void s_aws_wait_for_provider_shutdown_callback(void) {
98 aws_mutex_lock(&s_tester.lock);
99 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_has_tester_received_shutdown_callback, NULL);
100 aws_mutex_unlock(&s_tester.lock);
101 }
102
103 static struct aws_http_connection_manager *s_aws_http_connection_manager_new_mock(
104 struct aws_allocator *allocator,
105 const struct aws_http_connection_manager_options *options) {
106
107 (void)allocator;
108
109 /* Verify endpoint configuration if verification is enabled */
110 if (s_tester.verify_endpoint_host) {
111 struct aws_byte_cursor expected_host_cursor = aws_byte_cursor_from_buf(&s_tester.expected_host);
112 if (!aws_byte_cursor_eq(&options->host, &expected_host_cursor)) {
113 AWS_LOGF_ERROR(
114 AWS_LS_IMDS_CLIENT,
115 "Host mismatch: expected '" PRInSTR "' but got '" PRInSTR "'",
116 AWS_BYTE_CURSOR_PRI(expected_host_cursor),
117 AWS_BYTE_CURSOR_PRI(options->host));
118 /* Fail the test */
119 AWS_FATAL_ASSERT(false);
120 }
121 if (options->port != s_tester.expected_port) {
122 AWS_LOGF_ERROR(
123 AWS_LS_IMDS_CLIENT, "Port mismatch: expected %d but got %d", s_tester.expected_port, options->port);
124 /* Fail the test */
125 AWS_FATAL_ASSERT(false);
126 }
127 }
128
129 if (s_tester.proxy_config != NULL) {
130 AWS_FATAL_ASSERT(options->proxy_ev_settings->env_var_type == s_tester.proxy_config->env_var_type);
131 AWS_FATAL_ASSERT(options->proxy_ev_settings->connection_type == s_tester.proxy_config->connection_type);
132 AWS_FATAL_ASSERT(options->proxy_ev_settings->tls_options == s_tester.proxy_config->tls_options);
133 }
134
135 return (struct aws_http_connection_manager *)1;
136 }
137
138 static void s_aws_http_connection_manager_release_mock(struct aws_http_connection_manager *manager) {
139 (void)manager;
140
141 s_on_shutdown_complete(NULL);
142 }
143
144 static void s_aws_http_connection_manager_acquire_connection_mock(
145 struct aws_http_connection_manager *manager,
146 aws_http_connection_manager_on_connection_setup_fn *callback,
147 void *user_data) {
148
149 (void)manager;
150
151 if (s_tester.is_connection_acquire_successful) {
152 callback((struct aws_http_connection *)1, AWS_OP_SUCCESS, user_data);
153 } else {
154 aws_raise_error(AWS_ERROR_HTTP_UNKNOWN);
155 callback(NULL, AWS_OP_ERR, user_data);
156 }
157 }
158
159 static int s_aws_http_connection_manager_release_connection_mock(
160 struct aws_http_connection_manager *manager,
161 struct aws_http_connection *connection) {
162
163 (void)manager;
164 (void)connection;
165
166 return AWS_OP_SUCCESS;
167 }
168
169 static void s_invoke_mock_request_callbacks(
170 const struct aws_http_make_request_options *options,
171 struct aws_array_list *data_callbacks) {
172
173 size_t data_callback_count = aws_array_list_length(data_callbacks);
174
175 struct aws_http_header headers[1];
176 AWS_ZERO_ARRAY(headers);
177
178 headers[0].name = aws_byte_cursor_from_c_str("some-header");
179 headers[0].value = aws_byte_cursor_from_c_str("value");
180
181 options->on_response_headers(
182 (struct aws_http_stream *)1, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data);
183
184 if (options->on_response_header_block_done) {
185 options->on_response_header_block_done(
186 (struct aws_http_stream *)1, data_callback_count > 0, options->user_data);
187 }
188
189 for (size_t i = 0; i < data_callback_count; ++i) {
190 struct aws_byte_cursor data_callback_cursor;
191 if (aws_array_list_get_at(data_callbacks, &data_callback_cursor, i)) {
192 continue;
193 }
194
195 options->on_response_body((struct aws_http_stream *)1, &data_callback_cursor, options->user_data);
196 }
197
198 if (!s_tester.alternate_closed_connections) {
199 options->on_complete((struct aws_http_stream *)1, AWS_ERROR_SUCCESS, options->user_data);
200 } else {
201 options->on_complete(
202 (struct aws_http_stream *)1,
203 ((uint8_t)s_tester.current_request & 0x01) ? AWS_ERROR_HTTP_CONNECTION_CLOSED : AWS_ERROR_SUCCESS,
204 options->user_data);
205 }
206 }
207
208 static void s_validate_token_ttl_header(const struct aws_http_message *request);
209 static void s_validate_token_header(const struct aws_http_message *request);
210
211 static struct aws_http_stream *s_aws_http_connection_make_request_mock(
212 struct aws_http_connection *client_connection,
213 const struct aws_http_make_request_options *options) {
214
215 (void)client_connection;
216 (void)options;
217
218 struct aws_byte_cursor path;
219 AWS_ZERO_STRUCT(path);
220 aws_http_message_get_request_path(options->request, &path);
221
222 if (s_tester.current_request == s_tester.token_request_idx) {
223 /* verify token ttl header */
224 s_validate_token_ttl_header(options->request);
225 } else if (s_tester.current_request > s_tester.token_request_idx) {
226 /* verify token header */
227 s_validate_token_header(options->request);
228 }
229
230 int idx = s_tester.current_request++;
231 aws_byte_buf_append_dynamic(&(s_tester.request_uris[idx]), &path);
232 s_invoke_mock_request_callbacks(options, &s_tester.response_data_callbacks[idx]);
233 return (struct aws_http_stream *)1;
234 }
235
236 static int s_aws_http_stream_activate_mock(struct aws_http_stream *stream) {
237 (void)stream;
238 return AWS_OP_SUCCESS;
239 }
240
241 static struct aws_http_connection *s_aws_http_stream_get_connection_mock(const struct aws_http_stream *stream) {
242 (void)stream;
243 return (struct aws_http_connection *)1;
244 }
245
246 static int s_aws_http_stream_get_incoming_response_status_mock(
247 const struct aws_http_stream *stream,
248 int *out_status_code) {
249 (void)stream;
250 if (s_tester.response_code[s_tester.current_request - 1] != 0) {
251 *out_status_code = s_tester.response_code[s_tester.current_request - 1];
252 } else {
253 *out_status_code = AWS_HTTP_STATUS_CODE_200_OK;
254 }
255
256 return AWS_OP_SUCCESS;
257 }
258
259 static void s_aws_http_stream_release_mock(struct aws_http_stream *stream) {
260 (void)stream;
261 }
262
263 static void s_aws_http_connection_close_mock(struct aws_http_connection *connection) {
264 (void)connection;
265 }
266
267 static int s_aws_high_res_clock_get_ticks_mock(uint64_t *timestamp) {
268 return aws_high_res_clock_get_ticks(timestamp);
269 }
270
271 static struct aws_auth_http_system_vtable s_mock_function_table = {
272 .aws_http_connection_manager_new = s_aws_http_connection_manager_new_mock,
273 .aws_http_connection_manager_release = s_aws_http_connection_manager_release_mock,
274 .aws_http_connection_manager_acquire_connection = s_aws_http_connection_manager_acquire_connection_mock,
275 .aws_http_connection_manager_release_connection = s_aws_http_connection_manager_release_connection_mock,
276 .aws_http_connection_make_request = s_aws_http_connection_make_request_mock,
277 .aws_http_stream_activate = s_aws_http_stream_activate_mock,
278 .aws_http_stream_get_connection = s_aws_http_stream_get_connection_mock,
279 .aws_http_stream_get_incoming_response_status = s_aws_http_stream_get_incoming_response_status_mock,
280 .aws_http_stream_release = s_aws_http_stream_release_mock,
281 .aws_http_connection_close = s_aws_http_connection_close_mock,
282 .aws_high_res_clock_get_ticks = s_aws_high_res_clock_get_ticks_mock,
283 };
284
285 static int s_aws_imds_tester_init(struct aws_allocator *allocator) {
286
287 aws_auth_library_init(allocator);
288 AWS_ZERO_STRUCT(s_tester);
289
290 for (size_t i = 0; i < IMDS_MAX_REQUESTS; i++) {
291 if (aws_array_list_init_dynamic(
292 &s_tester.response_data_callbacks[i], allocator, 10, sizeof(struct aws_byte_cursor))) {
293 return AWS_OP_ERR;
294 }
295 if (aws_byte_buf_init(&s_tester.request_uris[i], allocator, 100)) {
296 return AWS_OP_ERR;
297 }
298 }
299
300 s_tester.allocator = allocator;
301 if (aws_mutex_init(&s_tester.lock)) {
302 return AWS_OP_ERR;
303 }
304
305 if (aws_condition_variable_init(&s_tester.signal)) {
306 return AWS_OP_ERR;
307 }
308
309 /* default to everything successful */
310 s_tester.is_connection_acquire_successful = true;
311
312 s_tester.el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
313 struct aws_client_bootstrap_options bootstrap_options = {
314 .event_loop_group = s_tester.el_group,
315 .user_data = NULL,
316 .host_resolution_config = NULL,
317 .host_resolver = NULL,
318 .on_shutdown_complete = NULL,
319 };
320 s_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
321 ASSERT_NOT_NULL(s_tester.bootstrap);
322
323 return AWS_OP_SUCCESS;
324 }
325
326 static int s_aws_imds_tester_cleanup(void) {
327 for (size_t i = 0; i < IMDS_MAX_REQUESTS; i++) {
328 aws_array_list_clean_up(&s_tester.response_data_callbacks[i]);
329 aws_byte_buf_clean_up(&s_tester.request_uris[i]);
330 }
331
332 aws_condition_variable_clean_up(&s_tester.signal);
333 aws_mutex_clean_up(&s_tester.lock);
334
335 aws_credentials_release(s_tester.credentials);
336
337 aws_client_bootstrap_release(s_tester.bootstrap);
338 aws_event_loop_group_release(s_tester.el_group);
339
340 /* Clean up endpoint verification buffer if it was used */
341 if (s_tester.expected_host.buffer) {
342 aws_byte_buf_clean_up(&s_tester.expected_host);
343 }
344
345 aws_auth_library_clean_up();
346
347 return AWS_OP_SUCCESS;
348 }
349
350 static bool s_has_tester_received_credentials_callback(void *user_data) {
351 (void)user_data;
352
353 return s_tester.has_received_credentials_callback;
354 }
355
356 static void s_aws_wait_for_credentials_result(void) {
357 aws_mutex_lock(&s_tester.lock);
358 aws_condition_variable_wait_pred(
359 &s_tester.signal, &s_tester.lock, s_has_tester_received_credentials_callback, NULL);
360 aws_mutex_unlock(&s_tester.lock);
361 }
362
363 static void s_get_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
364 (void)user_data;
365
366 aws_mutex_lock(&s_tester.lock);
367 s_tester.has_received_credentials_callback = true;
368 s_tester.error_code = error_code;
369 s_tester.credentials = credentials;
370 aws_credentials_acquire(credentials);
371 aws_condition_variable_notify_one(&s_tester.signal);
372 aws_mutex_unlock(&s_tester.lock);
373 }
374
375 static int s_credentials_provider_imds_new_destroy(struct aws_allocator *allocator, void *ctx) {
376 (void)ctx;
377
378 s_aws_imds_tester_init(allocator);
379
380 struct aws_credentials_provider_imds_options options = {
381 .bootstrap = s_tester.bootstrap,
382 .function_table = &s_mock_function_table,
383 .shutdown_options =
384 {
385 .shutdown_callback = s_on_shutdown_complete,
386 .shutdown_user_data = NULL,
387 },
388 };
389
390 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
391 aws_credentials_provider_release(provider);
392
393 s_aws_wait_for_provider_shutdown_callback();
394
395 /* Because we mock the http connection manager, we never get a callback back from it */
396 struct aws_credentials_provider_imds_impl *impl = provider->impl;
397 aws_mem_release(provider->allocator, impl->client);
398 aws_mem_release(provider->allocator, provider);
399
400 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
401
402 return 0;
403 }
404
405 AWS_TEST_CASE(credentials_provider_imds_new_destroy, s_credentials_provider_imds_new_destroy);
406
407 static int s_credentials_provider_imds_connect_failure(struct aws_allocator *allocator, void *ctx) {
408 (void)ctx;
409
410 s_aws_imds_tester_init(allocator);
411 s_tester.is_connection_acquire_successful = false;
412
413 struct aws_credentials_provider_imds_options options = {
414 .bootstrap = s_tester.bootstrap,
415 .function_table = &s_mock_function_table,
416 .shutdown_options =
417 {
418 .shutdown_callback = s_on_shutdown_complete,
419 .shutdown_user_data = NULL,
420 },
421 };
422
423 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
424
425 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
426
427 s_aws_wait_for_credentials_result();
428
429 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
430 ASSERT_TRUE(s_tester.credentials == NULL);
431
432 aws_credentials_provider_release(provider);
433
434 s_aws_wait_for_provider_shutdown_callback();
435
436 /* Because we mock the http connection manager, we never get a callback back from it */
437 struct aws_credentials_provider_imds_impl *impl = provider->impl;
438 aws_mem_release(provider->allocator, impl->client);
439 aws_mem_release(provider->allocator, provider);
440
441 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
442
443 return 0;
444 }
445
446 AWS_TEST_CASE(credentials_provider_imds_connect_failure, s_credentials_provider_imds_connect_failure);
447
448 AWS_STATIC_STRING_FROM_LITERAL(s_expected_imds_token_uri, "/latest/api/token");
449 AWS_STATIC_STRING_FROM_LITERAL(s_expected_imds_base_uri, "/latest/meta-data/iam/security-credentials/");
450 AWS_STATIC_STRING_FROM_LITERAL(s_expected_imds_role_uri, "/latest/meta-data/iam/security-credentials/test-role");
451 AWS_STATIC_STRING_FROM_LITERAL(s_test_role_response, "test-role");
452 AWS_STATIC_STRING_FROM_LITERAL(s_test_imds_token, "A00XXF3H00ZZ==");
453
454 static void s_validate_token_ttl_header(const struct aws_http_message *request) {
455 const struct aws_http_headers *headers = aws_http_message_get_const_headers(request);
456 struct aws_byte_cursor ttl_header = aws_byte_cursor_from_c_str("x-aws-ec2-metadata-token-ttl-seconds");
457 struct aws_byte_cursor ttl_value;
458 int ret = aws_http_headers_get(headers, ttl_header, &ttl_value);
459 if (ret == AWS_OP_SUCCESS) {
460 s_tester.token_ttl_header_exist[s_tester.current_request] = true;
461 if (aws_byte_cursor_eq_c_str_ignore_case(&ttl_value, "21600")) {
462 s_tester.token_ttl_header_expected[s_tester.current_request] = true;
463 } else {
464 s_tester.token_ttl_header_expected[s_tester.current_request] = false;
465 }
466 } else {
467 s_tester.token_ttl_header_exist[s_tester.current_request] = false;
468 }
469 }
470
471 static void s_validate_token_header(const struct aws_http_message *request) {
472 const struct aws_http_headers *headers = aws_http_message_get_const_headers(request);
473 struct aws_byte_cursor token_header = aws_byte_cursor_from_c_str("x-aws-ec2-metadata-token");
474 struct aws_byte_cursor token_value;
475 int ret = aws_http_headers_get(headers, token_header, &token_value);
476 if (ret == AWS_OP_SUCCESS) {
477 s_tester.token_header_exist[s_tester.current_request] = true;
478 if (aws_byte_cursor_eq_c_str_ignore_case(&token_value, "A00XXF3H00ZZ==")) {
479 s_tester.token_header_expected[s_tester.current_request] = true;
480 } else {
481 s_tester.token_header_expected[s_tester.current_request] = false;
482 }
483 } else {
484 s_tester.token_header_exist[s_tester.current_request] = false;
485 }
486 }
487
488 static int s_validate_uri_path_and_creds(int expected_requests, bool get_credentials) {
489
490 ASSERT_UINT_EQUALS(expected_requests, s_tester.current_request);
491
492 int idx = s_tester.token_request_idx;
493 if (s_tester.current_request >= 1) {
494 ASSERT_BIN_ARRAYS_EQUALS(
495 s_tester.request_uris[idx].buffer,
496 s_tester.request_uris[idx].len,
497 s_expected_imds_token_uri->bytes,
498 s_expected_imds_token_uri->len);
499 }
500 idx++;
501 if (s_tester.current_request >= 2) {
502 ASSERT_BIN_ARRAYS_EQUALS(
503 s_tester.request_uris[idx].buffer,
504 s_tester.request_uris[idx].len,
505 s_expected_imds_base_uri->bytes,
506 s_expected_imds_base_uri->len);
507 }
508 idx++;
509 if (s_tester.current_request >= 3) {
510 ASSERT_BIN_ARRAYS_EQUALS(
511 s_tester.request_uris[idx].buffer,
512 s_tester.request_uris[idx].len,
513 s_expected_imds_role_uri->bytes,
514 s_expected_imds_role_uri->len);
515 }
516
517 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
518
519 if (get_credentials) {
520 ASSERT_TRUE(s_tester.credentials != NULL);
521 } else {
522 ASSERT_TRUE(s_tester.credentials == NULL);
523 }
524
525 return 0;
526 }
527
528 static int s_credentials_provider_imds_token_request_failure(struct aws_allocator *allocator, void *ctx) {
529 (void)ctx;
530
531 s_aws_imds_tester_init(allocator);
532 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;
533 struct aws_credentials_provider_imds_options options = {
534 .bootstrap = s_tester.bootstrap,
535 .function_table = &s_mock_function_table,
536 .shutdown_options =
537 {
538 .shutdown_callback = s_on_shutdown_complete,
539 .shutdown_user_data = NULL,
540 },
541 };
542
543 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
544
545 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
546
547 s_aws_wait_for_credentials_result();
548
549 ASSERT_TRUE(s_validate_uri_path_and_creds(1, false /*no creds*/) == 0);
550 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
551 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
552 ASSERT_FALSE(s_tester.token_header_exist[0]);
553
554 aws_credentials_provider_release(provider);
555
556 s_aws_wait_for_provider_shutdown_callback();
557
558 /* Because we mock the http connection manager, we never get a callback back from it */
559 struct aws_credentials_provider_imds_impl *impl = provider->impl;
560 aws_mem_release(provider->allocator, impl->client);
561 aws_mem_release(provider->allocator, provider);
562
563 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
564
565 return 0;
566 }
567
568 AWS_TEST_CASE(credentials_provider_imds_token_request_failure, s_credentials_provider_imds_token_request_failure);
569
570 static int s_credentials_provider_imds_role_name_request_failure(struct aws_allocator *allocator, void *ctx) {
571 (void)ctx;
572
573 s_aws_imds_tester_init(allocator);
574
575 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
576 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
577
578 struct aws_credentials_provider_imds_options options = {
579 .bootstrap = s_tester.bootstrap,
580 .function_table = &s_mock_function_table,
581 .shutdown_options =
582 {
583 .shutdown_callback = s_on_shutdown_complete,
584 .shutdown_user_data = NULL,
585 },
586 };
587
588 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
589
590 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
591
592 s_aws_wait_for_credentials_result();
593
594 ASSERT_TRUE(s_validate_uri_path_and_creds(2, false /*no creds*/) == 0);
595 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
596 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
597 ASSERT_FALSE(s_tester.token_header_exist[0]);
598
599 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
600 ASSERT_TRUE(s_tester.token_header_exist[1]);
601 ASSERT_TRUE(s_tester.token_header_expected[1]);
602
603 aws_credentials_provider_release(provider);
604
605 s_aws_wait_for_provider_shutdown_callback();
606
607 /* Because we mock the http connection manager, we never get a callback back from it */
608 struct aws_credentials_provider_imds_impl *impl = provider->impl;
609 aws_mem_release(provider->allocator, impl->client);
610 aws_mem_release(provider->allocator, provider);
611
612 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
613
614 return 0;
615 }
616
617 AWS_TEST_CASE(
618 credentials_provider_imds_role_name_request_failure,
619 s_credentials_provider_imds_role_name_request_failure);
620
621 static int s_credentials_provider_imds_role_request_failure(struct aws_allocator *allocator, void *ctx) {
622 (void)ctx;
623
624 s_aws_imds_tester_init(allocator);
625
626 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
627 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
628
629 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
630 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
631
632 struct aws_credentials_provider_imds_options options = {
633 .bootstrap = s_tester.bootstrap,
634 .function_table = &s_mock_function_table,
635 .shutdown_options =
636 {
637 .shutdown_callback = s_on_shutdown_complete,
638 .shutdown_user_data = NULL,
639 },
640 };
641
642 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
643
644 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
645
646 s_aws_wait_for_credentials_result();
647
648 ASSERT_TRUE(s_validate_uri_path_and_creds(3, false /*no creds*/) == 0);
649 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
650 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
651 ASSERT_FALSE(s_tester.token_header_exist[0]);
652
653 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
654 ASSERT_TRUE(s_tester.token_header_exist[1]);
655 ASSERT_TRUE(s_tester.token_header_expected[1]);
656
657 ASSERT_FALSE(s_tester.token_ttl_header_exist[2]);
658 ASSERT_TRUE(s_tester.token_header_exist[2]);
659 ASSERT_TRUE(s_tester.token_header_expected[2]);
660
661 aws_credentials_provider_release(provider);
662
663 s_aws_wait_for_provider_shutdown_callback();
664
665 /* Because we mock the http connection manager, we never get a callback back from it */
666 struct aws_credentials_provider_imds_impl *impl = provider->impl;
667 aws_mem_release(provider->allocator, impl->client);
668 aws_mem_release(provider->allocator, provider);
669
670 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
671
672 return 0;
673 }
674
675 AWS_TEST_CASE(credentials_provider_imds_role_request_failure, s_credentials_provider_imds_role_request_failure);
676
677 AWS_STATIC_STRING_FROM_LITERAL(s_bad_document_response, "{\"NotTheExpectedDocumentFormat\":\"Error\"}");
678
679 static int s_credentials_provider_imds_bad_document_failure(struct aws_allocator *allocator, void *ctx) {
680 (void)ctx;
681
682 s_aws_imds_tester_init(allocator);
683
684 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
685 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
686
687 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
688 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
689
690 struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_bad_document_response);
691 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &bad_document_cursor);
692
693 struct aws_credentials_provider_imds_options options = {
694 .bootstrap = s_tester.bootstrap,
695 .function_table = &s_mock_function_table,
696 .shutdown_options =
697 {
698 .shutdown_callback = s_on_shutdown_complete,
699 .shutdown_user_data = NULL,
700 },
701 };
702
703 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
704
705 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
706
707 s_aws_wait_for_credentials_result();
708
709 ASSERT_TRUE(s_validate_uri_path_and_creds(3, false /*no creds*/) == 0);
710
711 aws_credentials_provider_release(provider);
712
713 s_aws_wait_for_provider_shutdown_callback();
714
715 /* Because we mock the http connection manager, we never get a callback back from it */
716 struct aws_credentials_provider_imds_impl *impl = provider->impl;
717 aws_mem_release(provider->allocator, impl->client);
718 aws_mem_release(provider->allocator, provider);
719
720 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
721
722 return 0;
723 }
724
725 AWS_TEST_CASE(credentials_provider_imds_bad_document_failure, s_credentials_provider_imds_bad_document_failure);
726
727 AWS_STATIC_STRING_FROM_LITERAL(
728 s_good_response,
729 "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"SecretAccessKey\":\"SuccessfulSecret\", \n "
730 "\"Token\":\"TokenSuccess\", \n \"Expiration\":\"2020-02-25T06:03:31Z\"}");
731 AWS_STATIC_STRING_FROM_LITERAL(s_good_access_key_id, "SuccessfulAccessKey");
732 AWS_STATIC_STRING_FROM_LITERAL(s_good_secret_access_key, "SuccessfulSecret");
733 AWS_STATIC_STRING_FROM_LITERAL(s_good_session_token, "TokenSuccess");
734
735 static int s_verify_credentials(struct aws_credentials *credentials) {
736 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(credentials), s_good_access_key_id);
737 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_secret_access_key(credentials), s_good_secret_access_key);
738 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_session_token(credentials), s_good_session_token);
739
740 return AWS_OP_SUCCESS;
741 }
742
743 static int s_credentials_provider_imds_secure_success(struct aws_allocator *allocator, void *ctx) {
744 (void)ctx;
745
746 s_aws_imds_tester_init(allocator);
747
748 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
749 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
750
751 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
752 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
753
754 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
755 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
756
757 struct aws_credentials_provider_imds_options options = {
758 .bootstrap = s_tester.bootstrap,
759 .function_table = &s_mock_function_table,
760 .shutdown_options =
761 {
762 .shutdown_callback = s_on_shutdown_complete,
763 .shutdown_user_data = NULL,
764 },
765 };
766
767 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
768
769 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
770
771 s_aws_wait_for_credentials_result();
772
773 ASSERT_TRUE(s_validate_uri_path_and_creds(3, true /*got creds*/) == 0);
774
775 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
776 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
777 ASSERT_FALSE(s_tester.token_header_exist[0]);
778
779 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
780 ASSERT_TRUE(s_tester.token_header_exist[1]);
781 ASSERT_TRUE(s_tester.token_header_expected[1]);
782
783 ASSERT_FALSE(s_tester.token_ttl_header_exist[2]);
784 ASSERT_TRUE(s_tester.token_header_exist[2]);
785 ASSERT_TRUE(s_tester.token_header_expected[2]);
786
787 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
788 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
789
790 aws_credentials_provider_release(provider);
791
792 s_aws_wait_for_provider_shutdown_callback();
793
794 /* Because we mock the http connection manager, we never get a callback back from it */
795 struct aws_credentials_provider_imds_impl *impl = provider->impl;
796 aws_mem_release(provider->allocator, impl->client);
797 aws_mem_release(provider->allocator, provider);
798
799 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
800
801 return 0;
802 }
803
804 AWS_TEST_CASE(credentials_provider_imds_secure_success, s_credentials_provider_imds_secure_success);
805
806 static int s_credentials_provider_imds_connection_closed_success(struct aws_allocator *allocator, void *ctx) {
807 (void)ctx;
808
809 s_aws_imds_tester_init(allocator);
810 s_tester.alternate_closed_connections = true;
811
812 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
813 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
814 /* this one will fail, replay the body. */
815 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_token_cursor);
816
817 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
818 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &test_role_cursor);
819
820 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
821 aws_array_list_push_back(&s_tester.response_data_callbacks[3], &good_response_cursor);
822 /* this one will fail replay the body */
823 aws_array_list_push_back(&s_tester.response_data_callbacks[4], &good_response_cursor);
824
825 struct aws_credentials_provider_imds_options options = {
826 .bootstrap = s_tester.bootstrap,
827 .function_table = &s_mock_function_table,
828 .shutdown_options =
829 {
830 .shutdown_callback = s_on_shutdown_complete,
831 .shutdown_user_data = NULL,
832 },
833 };
834
835 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
836
837 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
838
839 s_aws_wait_for_credentials_result();
840
841 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
842
843 aws_credentials_provider_release(provider);
844
845 s_aws_wait_for_provider_shutdown_callback();
846
847 /* Because we mock the http connection manager, we never get a callback back from it */
848 struct aws_credentials_provider_imds_impl *impl = provider->impl;
849 aws_mem_release(provider->allocator, impl->client);
850 aws_mem_release(provider->allocator, provider);
851
852 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
853
854 return 0;
855 }
856
857 AWS_TEST_CASE(
858 credentials_provider_imds_connection_closed_success,
859 s_credentials_provider_imds_connection_closed_success);
860
861 static int s_credentials_provider_imds_insecure_success(struct aws_allocator *allocator, void *ctx) {
862 (void)ctx;
863
864 s_aws_imds_tester_init(allocator);
865 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_403_FORBIDDEN;
866
867 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
868 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
869
870 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
871 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
872
873 struct aws_credentials_provider_imds_options options = {
874 .bootstrap = s_tester.bootstrap,
875 .function_table = &s_mock_function_table,
876 .shutdown_options =
877 {
878 .shutdown_callback = s_on_shutdown_complete,
879 .shutdown_user_data = NULL,
880 },
881 };
882
883 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
884
885 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
886
887 s_aws_wait_for_credentials_result();
888
889 ASSERT_TRUE(s_validate_uri_path_and_creds(3, true /*no creds*/) == 0);
890
891 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
892 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
893 ASSERT_FALSE(s_tester.token_header_exist[0]);
894
895 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
896 ASSERT_FALSE(s_tester.token_header_exist[1]);
897
898 ASSERT_FALSE(s_tester.token_ttl_header_exist[2]);
899 ASSERT_FALSE(s_tester.token_header_exist[2]);
900
901 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
902
903 aws_credentials_provider_release(provider);
904
905 s_aws_wait_for_provider_shutdown_callback();
906
907 /* Because we mock the http connection manager, we never get a callback back from it */
908 struct aws_credentials_provider_imds_impl *impl = provider->impl;
909 aws_mem_release(provider->allocator, impl->client);
910 aws_mem_release(provider->allocator, provider);
911
912 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
913
914 return 0;
915 }
916
917 AWS_TEST_CASE(credentials_provider_imds_insecure_success, s_credentials_provider_imds_insecure_success);
918
919 static int s_credentials_provider_imds_insecure_then_secure_success(struct aws_allocator *allocator, void *ctx) {
920 (void)ctx;
921
922 s_aws_imds_tester_init(allocator);
923 s_tester.token_request_idx = 1;
924 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_401_UNAUTHORIZED;
925
926 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
927 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_token_cursor);
928
929 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
930 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &test_role_cursor);
931
932 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
933 aws_array_list_push_back(&s_tester.response_data_callbacks[3], &good_response_cursor);
934
935 struct aws_credentials_provider_imds_options options = {
936 .bootstrap = s_tester.bootstrap,
937 .function_table = &s_mock_function_table,
938 .shutdown_options =
939 {
940 .shutdown_callback = s_on_shutdown_complete,
941 .shutdown_user_data = NULL,
942 },
943 .imds_version = IMDS_PROTOCOL_V1,
944 };
945
946 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
947
948 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
949
950 s_aws_wait_for_credentials_result();
951 ASSERT_TRUE(s_validate_uri_path_and_creds(4, true /*no creds*/) == 0);
952
953 ASSERT_FALSE(s_tester.token_ttl_header_exist[0]);
954 ASSERT_FALSE(s_tester.token_header_exist[0]);
955
956 ASSERT_TRUE(s_tester.token_ttl_header_exist[1]);
957 ASSERT_TRUE(s_tester.token_ttl_header_expected[1]);
958 ASSERT_FALSE(s_tester.token_header_exist[1]);
959
960 ASSERT_FALSE(s_tester.token_ttl_header_exist[2]);
961 ASSERT_TRUE(s_tester.token_header_exist[2]);
962 ASSERT_TRUE(s_tester.token_header_expected[2]);
963
964 ASSERT_FALSE(s_tester.token_ttl_header_exist[3]);
965 ASSERT_TRUE(s_tester.token_header_exist[3]);
966 ASSERT_TRUE(s_tester.token_header_expected[3]);
967
968 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
969
970 aws_credentials_provider_release(provider);
971
972 s_aws_wait_for_provider_shutdown_callback();
973
974 /* Because we mock the http connection manager, we never get a callback back from it */
975 struct aws_credentials_provider_imds_impl *impl = provider->impl;
976 aws_mem_release(provider->allocator, impl->client);
977 aws_mem_release(provider->allocator, provider);
978
979 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
980
981 return 0;
982 }
983
984 AWS_TEST_CASE(
985 credentials_provider_imds_insecure_then_secure_success,
986 s_credentials_provider_imds_insecure_then_secure_success);
987
988 AWS_STATIC_STRING_FROM_LITERAL(s_test_role_response_first_half, "test-");
989 AWS_STATIC_STRING_FROM_LITERAL(s_test_role_response_second_half, "role");
990
991 static int s_credentials_provider_imds_success_multi_part_role_name(struct aws_allocator *allocator, void *ctx) {
992 (void)ctx;
993
994 s_aws_imds_tester_init(allocator);
995
996 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
997 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
998
999 struct aws_byte_cursor test_role_cursor1 = aws_byte_cursor_from_string(s_test_role_response_first_half);
1000 struct aws_byte_cursor test_role_cursor2 = aws_byte_cursor_from_string(s_test_role_response_second_half);
1001 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor1);
1002 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor2);
1003
1004 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1005 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
1006
1007 struct aws_credentials_provider_imds_options options = {
1008 .bootstrap = s_tester.bootstrap,
1009 .function_table = &s_mock_function_table,
1010 .shutdown_options =
1011 {
1012 .shutdown_callback = s_on_shutdown_complete,
1013 .shutdown_user_data = NULL,
1014 },
1015 };
1016
1017 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1018
1019 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1020
1021 s_aws_wait_for_credentials_result();
1022
1023 s_validate_uri_path_and_creds(3, true);
1024
1025 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1026
1027 aws_credentials_provider_release(provider);
1028
1029 s_aws_wait_for_provider_shutdown_callback();
1030
1031 /* Because we mock the http connection manager, we never get a callback back from it */
1032 struct aws_credentials_provider_imds_impl *impl = provider->impl;
1033 aws_mem_release(provider->allocator, impl->client);
1034 aws_mem_release(provider->allocator, provider);
1035
1036 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1037
1038 return 0;
1039 }
1040
1041 AWS_TEST_CASE(
1042 credentials_provider_imds_success_multi_part_role_name,
1043 s_credentials_provider_imds_success_multi_part_role_name);
1044
1045 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_first_part, "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"Secret");
1046 AWS_STATIC_STRING_FROM_LITERAL(s_good_response_second_part, "AccessKey\":\"SuccessfulSecr");
1047 AWS_STATIC_STRING_FROM_LITERAL(
1048 s_good_response_third_part,
1049 "et\", \n \"Token\":\"TokenSuccess\"\n, \"Expiration\":\"2020-02-25T06:03:31Z\"}");
1050
1051 static int s_credentials_provider_imds_success_multi_part_doc(struct aws_allocator *allocator, void *ctx) {
1052 (void)ctx;
1053
1054 s_aws_imds_tester_init(allocator);
1055
1056 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1057 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1058
1059 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
1060 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
1061
1062 struct aws_byte_cursor good_response_cursor1 = aws_byte_cursor_from_string(s_good_response_first_part);
1063 struct aws_byte_cursor good_response_cursor2 = aws_byte_cursor_from_string(s_good_response_second_part);
1064 struct aws_byte_cursor good_response_cursor3 = aws_byte_cursor_from_string(s_good_response_third_part);
1065 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor1);
1066 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor2);
1067 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor3);
1068
1069 struct aws_credentials_provider_imds_options options = {
1070 .bootstrap = s_tester.bootstrap,
1071 .function_table = &s_mock_function_table,
1072 .shutdown_options =
1073 {
1074 .shutdown_callback = s_on_shutdown_complete,
1075 .shutdown_user_data = NULL,
1076 },
1077 };
1078
1079 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1080
1081 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1082
1083 s_aws_wait_for_credentials_result();
1084
1085 s_validate_uri_path_and_creds(3, true);
1086
1087 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1088
1089 aws_credentials_provider_release(provider);
1090
1091 s_aws_wait_for_provider_shutdown_callback();
1092
1093 /* Because we mock the http connection manager, we never get a callback back from it */
1094 struct aws_credentials_provider_imds_impl *impl = provider->impl;
1095 aws_mem_release(provider->allocator, impl->client);
1096 aws_mem_release(provider->allocator, provider);
1097
1098 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1099
1100 return 0;
1101 }
1102
1103 AWS_TEST_CASE(credentials_provider_imds_success_multi_part_doc, s_credentials_provider_imds_success_multi_part_doc);
1104
1105 static int s_credentials_provider_imds_real_new_destroy(struct aws_allocator *allocator, void *ctx) {
1106 (void)ctx;
1107
1108 aws_auth_library_init(allocator);
1109
1110 struct aws_logger_standard_options logger_options = {
1111 .level = AWS_LOG_LEVEL_TRACE,
1112 .file = stderr,
1113 };
1114
1115 struct aws_logger logger;
1116 ASSERT_SUCCESS(aws_logger_init_standard(&logger, allocator, &logger_options));
1117 aws_logger_set(&logger);
1118
1119 s_aws_imds_tester_init(allocator);
1120
1121 struct aws_host_resolver_default_options resolver_options = {
1122 .el_group = s_tester.el_group,
1123 .max_entries = 8,
1124 };
1125 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1126
1127 struct aws_client_bootstrap_options bootstrap_options = {
1128 .event_loop_group = s_tester.el_group,
1129 .host_resolver = resolver,
1130 };
1131 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1132
1133 struct aws_credentials_provider_imds_options options = {
1134 .bootstrap = bootstrap,
1135 .shutdown_options =
1136 {
1137 .shutdown_callback = s_on_shutdown_complete,
1138 .shutdown_user_data = NULL,
1139 },
1140 };
1141
1142 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1143
1144 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1145
1146 s_aws_wait_for_credentials_result();
1147
1148 aws_credentials_provider_release(provider);
1149
1150 s_aws_wait_for_provider_shutdown_callback();
1151
1152 aws_client_bootstrap_release(bootstrap);
1153 aws_host_resolver_release(resolver);
1154
1155 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1156
1157 aws_auth_library_clean_up();
1158
1159 aws_logger_set(NULL);
1160 aws_logger_clean_up(&logger);
1161
1162 return 0;
1163 }
1164
1165 AWS_TEST_CASE(credentials_provider_imds_real_new_destroy, s_credentials_provider_imds_real_new_destroy);
1166
1167 static int s_credentials_provider_imds_real_success(struct aws_allocator *allocator, void *ctx) {
1168 (void)ctx;
1169
1170 aws_auth_library_init(allocator);
1171
1172 struct aws_logger_standard_options logger_options = {
1173 .level = AWS_LOG_LEVEL_TRACE,
1174 .file = stderr,
1175 };
1176
1177 struct aws_logger logger;
1178 ASSERT_SUCCESS(aws_logger_init_standard(&logger, allocator, &logger_options));
1179 aws_logger_set(&logger);
1180
1181 s_aws_imds_tester_init(allocator);
1182
1183 struct aws_host_resolver_default_options resolver_options = {
1184 .el_group = s_tester.el_group,
1185 .max_entries = 8,
1186 };
1187 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1188
1189 struct aws_client_bootstrap_options bootstrap_options = {
1190 .event_loop_group = s_tester.el_group,
1191 .host_resolver = resolver,
1192 };
1193 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1194
1195 struct aws_credentials_provider_imds_options options = {
1196 .bootstrap = bootstrap,
1197 .shutdown_options =
1198 {
1199 .shutdown_callback = s_on_shutdown_complete,
1200 .shutdown_user_data = NULL,
1201 },
1202 };
1203
1204 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1205
1206 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1207
1208 s_aws_wait_for_credentials_result();
1209
1210 ASSERT_TRUE(s_tester.credentials != NULL);
1211
1212 aws_credentials_provider_release(provider);
1213
1214 s_aws_wait_for_provider_shutdown_callback();
1215
1216 aws_client_bootstrap_release(bootstrap);
1217 aws_host_resolver_release(resolver);
1218
1219 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1220
1221 aws_auth_library_clean_up();
1222
1223 aws_logger_set(NULL);
1224 aws_logger_clean_up(&logger);
1225
1226 return 0;
1227 }
1228
1229 AWS_TEST_CASE(credentials_provider_imds_real_success, s_credentials_provider_imds_real_success);
1230
1231 static int s_credentials_provider_imds_proxy_routing_enabled_test(struct aws_allocator *allocator, void *ctx) {
1232 (void)ctx;
1233
1234 s_aws_imds_tester_init(allocator);
1235
1236 struct proxy_env_var_settings proxy_config = {
1237 .env_var_type = AWS_HPEV_ENABLE,
1238 };
1239
1240 s_tester.proxy_config = &proxy_config;
1241
1242 struct aws_credentials_provider_imds_options options = {
1243 .bootstrap = s_tester.bootstrap,
1244 .function_table = &s_mock_function_table,
1245 .shutdown_options =
1246 {
1247 .shutdown_callback = s_on_shutdown_complete,
1248 .shutdown_user_data = NULL,
1249 },
1250 .proxy_ev_settings = &proxy_config,
1251 };
1252
1253 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1254
1255 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1256
1257 s_aws_wait_for_credentials_result();
1258
1259 ASSERT_TRUE(s_tester.credentials == NULL);
1260
1261 aws_credentials_provider_release(provider);
1262
1263 s_aws_wait_for_provider_shutdown_callback();
1264
1265 struct aws_credentials_provider_imds_impl *impl = provider->impl;
1266 aws_mem_release(provider->allocator, impl->client);
1267 aws_mem_release(provider->allocator, provider);
1268
1269 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1270
1271 return 0;
1272 }
1273 AWS_TEST_CASE(
1274 credentials_provider_imds_proxy_routing_enabled_test,
1275 s_credentials_provider_imds_proxy_routing_enabled_test);
1276
1277 static int s_credentials_provider_imds_ipv6_endpoint_mode(struct aws_allocator *allocator, void *ctx) {
1278 (void)ctx;
1279
1280 s_aws_imds_tester_init(allocator);
1281
1282 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1283 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1284
1285 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
1286 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
1287
1288 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1289 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
1290
1291 /* Enable endpoint verification for IPv4 default endpoint */
1292 s_tester.verify_endpoint_host = true;
1293 aws_byte_buf_init(&s_tester.expected_host, allocator, 20);
1294 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_c_str("fd00:ec2::254");
1295 aws_byte_buf_append_dynamic(&s_tester.expected_host, &host_cursor);
1296 s_tester.expected_port = 80;
1297
1298 struct aws_credentials_provider_imds_options options = {
1299 .bootstrap = s_tester.bootstrap,
1300 .function_table = &s_mock_function_table,
1301 .imds_endpoint_mode = AWS_IMDS_ENDPOINT_MODE_IPV6,
1302 .shutdown_options =
1303 {
1304 .shutdown_callback = s_on_shutdown_complete,
1305 .shutdown_user_data = NULL,
1306 },
1307 };
1308
1309 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1310 ASSERT_NOT_NULL(provider);
1311
1312 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1313
1314 s_aws_wait_for_credentials_result();
1315
1316 ASSERT_TRUE(s_validate_uri_path_and_creds(3, true /*got creds*/) == 0);
1317 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1318
1319 aws_credentials_provider_release(provider);
1320
1321 s_aws_wait_for_provider_shutdown_callback();
1322
1323 /* Because we mock the http connection manager, we never get a callback back from it */
1324 struct aws_credentials_provider_imds_impl *impl = provider->impl;
1325 aws_mem_release(provider->allocator, impl->client);
1326 aws_mem_release(provider->allocator, provider);
1327
1328 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1329
1330 return 0;
1331 }
1332
1333 AWS_TEST_CASE(credentials_provider_imds_ipv6_endpoint_mode, s_credentials_provider_imds_ipv6_endpoint_mode);
1334
1335 static int s_credentials_provider_imds_custom_endpoint(struct aws_allocator *allocator, void *ctx) {
1336 (void)ctx;
1337
1338 s_aws_imds_tester_init(allocator);
1339
1340 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1341 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1342
1343 struct aws_byte_cursor test_role_cursor = aws_byte_cursor_from_string(s_test_role_response);
1344 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_role_cursor);
1345
1346 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1347 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
1348
1349 /* Enable endpoint verification */
1350 s_tester.verify_endpoint_host = true;
1351 aws_byte_buf_init(&s_tester.expected_host, allocator, 20);
1352 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_c_str("127.0.0.1");
1353 aws_byte_buf_append_dynamic(&s_tester.expected_host, &host_cursor);
1354 s_tester.expected_port = 8080;
1355
1356 struct aws_credentials_provider_imds_options options = {
1357 .bootstrap = s_tester.bootstrap,
1358 .function_table = &s_mock_function_table,
1359 .imds_endpoint = aws_byte_cursor_from_c_str("http://127.0.0.1:8080"),
1360 .shutdown_options =
1361 {
1362 .shutdown_callback = s_on_shutdown_complete,
1363 .shutdown_user_data = NULL,
1364 },
1365 };
1366
1367 struct aws_credentials_provider *provider = aws_credentials_provider_new_imds(allocator, &options);
1368 ASSERT_NOT_NULL(provider);
1369
1370 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
1371
1372 s_aws_wait_for_credentials_result();
1373
1374 ASSERT_TRUE(s_validate_uri_path_and_creds(3, true /*got creds*/) == 0);
1375 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));
1376
1377 aws_credentials_provider_release(provider);
1378
1379 s_aws_wait_for_provider_shutdown_callback();
1380
1381 /* Because we mock the http connection manager, we never get a callback back from it */
1382 struct aws_credentials_provider_imds_impl *impl = provider->impl;
1383 aws_mem_release(provider->allocator, impl->client);
1384 aws_mem_release(provider->allocator, provider);
1385
1386 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1387
1388 return 0;
1389 }
1390
1391 AWS_TEST_CASE(credentials_provider_imds_custom_endpoint, s_credentials_provider_imds_custom_endpoint);