Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/auth/aws_imds_client.h>
7 #include <aws/testing/aws_test_harness.h>
8
9 #include <aws/auth/credentials.h>
10 #include <aws/auth/private/credentials_utils.h>
11 #include <aws/common/clock.h>
12 #include <aws/common/condition_variable.h>
13 #include <aws/common/device_random.h>
14 #include <aws/common/string.h>
15 #include <aws/common/thread.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 #if defined(_MSC_VER)
24 # pragma warning(disable : 4244)
25 #endif /* _MSC_VER */
26
27 #define IMDS_CLIENT_MAX_REQUESTS 15
28 struct aws_mock_imds_client_tester {
29 struct aws_byte_buf request_uris[IMDS_CLIENT_MAX_REQUESTS];
30 struct aws_array_list response_data_callbacks[IMDS_CLIENT_MAX_REQUESTS];
31 struct aws_allocator *allocator;
32 int current_request;
33 int response_code[IMDS_CLIENT_MAX_REQUESTS];
34 int token_request_idx;
35 int error_code;
36 uint64_t timestamp;
37 bool is_connection_acquire_successful;
38
39 struct aws_mutex lock;
40 struct aws_condition_variable signal;
41
42 struct aws_event_loop_group *el_group;
43 struct aws_client_bootstrap *bootstrap;
44 bool has_received_resource_callback;
45 bool has_received_shutdown_callback;
46 bool token_ttl_header_exist[IMDS_CLIENT_MAX_REQUESTS];
47 bool token_ttl_header_expected[IMDS_CLIENT_MAX_REQUESTS];
48 bool token_header_exist[IMDS_CLIENT_MAX_REQUESTS];
49 bool token_header_expected[IMDS_CLIENT_MAX_REQUESTS];
50 bool alternate_closed_connections;
51
52 struct aws_byte_buf resource;
53
54 int successful_requests;
55
56 /* Endpoint verification fields */
57 struct aws_byte_buf expected_host;
58 uint16_t expected_port;
59 bool verify_endpoint_host;
60 };
61
62 static struct aws_mock_imds_client_tester s_tester;
63
64 static void s_on_shutdown_complete(void *user_data) {
65 (void)user_data;
66
67 aws_mutex_lock(&s_tester.lock);
68 s_tester.has_received_shutdown_callback = true;
69 aws_mutex_unlock(&s_tester.lock);
70
71 aws_condition_variable_notify_one(&s_tester.signal);
72 }
73
74 static bool s_has_tester_received_shutdown_callback(void *user_data) {
75 (void)user_data;
76
77 return s_tester.has_received_shutdown_callback;
78 }
79
80 static void s_aws_wait_for_imds_client_shutdown_callback(void) {
81 aws_mutex_lock(&s_tester.lock);
82 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_has_tester_received_shutdown_callback, NULL);
83 aws_mutex_unlock(&s_tester.lock);
84 }
85
86 static struct aws_http_connection_manager *s_aws_http_connection_manager_new_mock(
87 struct aws_allocator *allocator,
88 const struct aws_http_connection_manager_options *options) {
89
90 (void)allocator;
91
92 /* Verify endpoint configuration if verification is enabled */
93 if (s_tester.verify_endpoint_host) {
94 struct aws_byte_cursor expected_host_cursor = aws_byte_cursor_from_buf(&s_tester.expected_host);
95 if (!aws_byte_cursor_eq(&options->host, &expected_host_cursor)) {
96 AWS_LOGF_ERROR(
97 AWS_LS_IMDS_CLIENT,
98 "Host mismatch: expected '" PRInSTR "' but got '" PRInSTR "'",
99 AWS_BYTE_CURSOR_PRI(expected_host_cursor),
100 AWS_BYTE_CURSOR_PRI(options->host));
101 /* Fail the test */
102 AWS_FATAL_ASSERT(false);
103 }
104 if (options->port != s_tester.expected_port) {
105 AWS_LOGF_ERROR(
106 AWS_LS_IMDS_CLIENT, "Port mismatch: expected %d but got %d", s_tester.expected_port, options->port);
107 /* Fail the test */
108 AWS_FATAL_ASSERT(false);
109 }
110 }
111
112 return (struct aws_http_connection_manager *)1;
113 }
114
115 static void s_aws_http_connection_manager_release_mock(struct aws_http_connection_manager *manager) {
116 (void)manager;
117
118 s_on_shutdown_complete(NULL);
119 }
120
121 static void s_aws_http_connection_manager_acquire_connection_mock(
122 struct aws_http_connection_manager *manager,
123 aws_http_connection_manager_on_connection_setup_fn *callback,
124 void *user_data) {
125
126 (void)manager;
127
128 if (s_tester.is_connection_acquire_successful) {
129 callback((struct aws_http_connection *)1, AWS_OP_SUCCESS, user_data);
130 } else {
131 aws_raise_error(AWS_ERROR_HTTP_UNKNOWN);
132 callback(NULL, AWS_OP_ERR, user_data);
133 }
134 }
135
136 static int s_aws_http_connection_manager_release_connection_mock(
137 struct aws_http_connection_manager *manager,
138 struct aws_http_connection *connection) {
139
140 (void)manager;
141 (void)connection;
142
143 return AWS_OP_SUCCESS;
144 }
145
146 static void s_invoke_mock_request_callbacks(
147 const struct aws_http_make_request_options *options,
148 struct aws_array_list *data_callbacks) {
149
150 size_t data_callback_count = aws_array_list_length(data_callbacks);
151
152 struct aws_http_header headers[1];
153 AWS_ZERO_ARRAY(headers);
154
155 headers[0].name = aws_byte_cursor_from_c_str("some-header");
156 headers[0].value = aws_byte_cursor_from_c_str("value");
157
158 options->on_response_headers(
159 (struct aws_http_stream *)1, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data);
160
161 if (options->on_response_header_block_done) {
162 options->on_response_header_block_done(
163 (struct aws_http_stream *)1, data_callback_count > 0, options->user_data);
164 }
165
166 for (size_t i = 0; i < data_callback_count; ++i) {
167 struct aws_byte_cursor data_callback_cursor;
168 if (aws_array_list_get_at(data_callbacks, &data_callback_cursor, i)) {
169 continue;
170 }
171
172 options->on_response_body((struct aws_http_stream *)1, &data_callback_cursor, options->user_data);
173 }
174
175 if (!s_tester.alternate_closed_connections) {
176 options->on_complete((struct aws_http_stream *)1, AWS_ERROR_SUCCESS, options->user_data);
177 } else {
178 options->on_complete(
179 (struct aws_http_stream *)1,
180 ((uint8_t)s_tester.current_request & 0x01) ? AWS_ERROR_HTTP_CONNECTION_CLOSED : AWS_ERROR_SUCCESS,
181 options->user_data);
182 }
183 }
184
185 static void s_validate_token_ttl_header(const struct aws_http_message *request);
186 static void s_validate_token_header(const struct aws_http_message *request);
187
188 static struct aws_http_stream *s_aws_http_connection_make_request_mock(
189 struct aws_http_connection *client_connection,
190 const struct aws_http_make_request_options *options) {
191
192 (void)client_connection;
193 (void)options;
194
195 struct aws_byte_cursor path;
196 AWS_ZERO_STRUCT(path);
197 aws_http_message_get_request_path(options->request, &path);
198
199 if (s_tester.current_request == s_tester.token_request_idx) {
200 /* verify token ttl header */
201 s_validate_token_ttl_header(options->request);
202 } else if (s_tester.current_request > s_tester.token_request_idx) {
203 /* verify token header */
204 s_validate_token_header(options->request);
205 }
206
207 int idx = s_tester.current_request++;
208 aws_byte_buf_append_dynamic(&(s_tester.request_uris[idx]), &path);
209 s_invoke_mock_request_callbacks(options, &s_tester.response_data_callbacks[idx]);
210 return (struct aws_http_stream *)1;
211 }
212
213 static int s_aws_http_stream_activate_mock(struct aws_http_stream *stream) {
214 (void)stream;
215 return AWS_OP_SUCCESS;
216 }
217
218 static struct aws_http_connection *s_aws_http_stream_get_connection_mock(const struct aws_http_stream *stream) {
219 (void)stream;
220 return (struct aws_http_connection *)1;
221 }
222
223 static int s_aws_http_stream_get_incoming_response_status_mock(
224 const struct aws_http_stream *stream,
225 int *out_status_code) {
226 (void)stream;
227 if (s_tester.response_code[s_tester.current_request - 1] != 0) {
228 *out_status_code = s_tester.response_code[s_tester.current_request - 1];
229 } else {
230 *out_status_code = AWS_HTTP_STATUS_CODE_200_OK;
231 }
232
233 return AWS_OP_SUCCESS;
234 }
235
236 static void s_aws_http_stream_release_mock(struct aws_http_stream *stream) {
237 (void)stream;
238 }
239
240 static void s_aws_http_connection_close_mock(struct aws_http_connection *connection) {
241 (void)connection;
242 }
243
244 static int s_aws_high_res_clock_get_ticks_mock(uint64_t *timestamp) {
245 *timestamp = s_tester.timestamp;
246 return AWS_OP_SUCCESS;
247 }
248
249 static struct aws_auth_http_system_vtable s_mock_function_table = {
250 .aws_http_connection_manager_new = s_aws_http_connection_manager_new_mock,
251 .aws_http_connection_manager_release = s_aws_http_connection_manager_release_mock,
252 .aws_http_connection_manager_acquire_connection = s_aws_http_connection_manager_acquire_connection_mock,
253 .aws_http_connection_manager_release_connection = s_aws_http_connection_manager_release_connection_mock,
254 .aws_http_connection_make_request = s_aws_http_connection_make_request_mock,
255 .aws_http_stream_activate = s_aws_http_stream_activate_mock,
256 .aws_http_stream_get_connection = s_aws_http_stream_get_connection_mock,
257 .aws_http_stream_get_incoming_response_status = s_aws_http_stream_get_incoming_response_status_mock,
258 .aws_http_stream_release = s_aws_http_stream_release_mock,
259 .aws_http_connection_close = s_aws_http_connection_close_mock,
260 .aws_high_res_clock_get_ticks = s_aws_high_res_clock_get_ticks_mock,
261 };
262
263 static int s_aws_imds_tester_init(struct aws_allocator *allocator) {
264
265 aws_auth_library_init(allocator);
266
267 AWS_ZERO_STRUCT(s_tester);
268
269 s_tester.allocator = allocator;
270 for (size_t i = 0; i < IMDS_CLIENT_MAX_REQUESTS; i++) {
271 if (aws_array_list_init_dynamic(
272 &s_tester.response_data_callbacks[i], allocator, 10, sizeof(struct aws_byte_cursor))) {
273 return AWS_OP_ERR;
274 }
275 if (aws_byte_buf_init(&s_tester.request_uris[i], allocator, 100)) {
276 return AWS_OP_ERR;
277 }
278 }
279
280 if (aws_mutex_init(&s_tester.lock)) {
281 return AWS_OP_ERR;
282 }
283
284 if (aws_condition_variable_init(&s_tester.signal)) {
285 return AWS_OP_ERR;
286 }
287
288 /* default to everything successful */
289 s_tester.is_connection_acquire_successful = true;
290
291 s_tester.el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
292 struct aws_client_bootstrap_options bootstrap_options = {
293 .event_loop_group = s_tester.el_group,
294 .user_data = NULL,
295 .host_resolution_config = NULL,
296 .host_resolver = NULL,
297 .on_shutdown_complete = NULL,
298 };
299 s_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
300 ASSERT_NOT_NULL(s_tester.bootstrap);
301
302 if (aws_byte_buf_init(&s_tester.resource, allocator, 256)) {
303 return AWS_OP_ERR;
304 }
305
306 return AWS_OP_SUCCESS;
307 }
308
309 static int s_aws_imds_tester_cleanup(void) {
310 for (size_t i = 0; i < IMDS_CLIENT_MAX_REQUESTS; i++) {
311 aws_array_list_clean_up(&s_tester.response_data_callbacks[i]);
312 aws_byte_buf_clean_up(&s_tester.request_uris[i]);
313 }
314
315 aws_condition_variable_clean_up(&s_tester.signal);
316 aws_mutex_clean_up(&s_tester.lock);
317
318 aws_client_bootstrap_release(s_tester.bootstrap);
319 aws_event_loop_group_release(s_tester.el_group);
320 aws_byte_buf_clean_up(&s_tester.resource);
321
322 /* Clean up endpoint verification buffer if it was used */
323 if (s_tester.expected_host.buffer) {
324 aws_byte_buf_clean_up(&s_tester.expected_host);
325 }
326
327 aws_auth_library_clean_up();
328
329 return AWS_OP_SUCCESS;
330 }
331
332 static bool s_has_tester_received_resource_callback(void *user_data) {
333 (void)user_data;
334
335 return s_tester.has_received_resource_callback;
336 }
337
338 static void s_aws_wait_for_resource_result(void) {
339 aws_mutex_lock(&s_tester.lock);
340 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_has_tester_received_resource_callback, NULL);
341 aws_mutex_unlock(&s_tester.lock);
342 }
343
344 static void s_get_resource_callback(const struct aws_byte_buf *resource, int error_code, void *user_data) {
345 (void)user_data;
346 aws_mutex_lock(&s_tester.lock);
347 s_tester.has_received_resource_callback = true;
348 s_tester.error_code = error_code;
349 if (resource && resource->len) {
350 struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(resource);
351 aws_byte_buf_append_dynamic(&s_tester.resource, &cursor);
352 s_tester.successful_requests++;
353 }
354 aws_condition_variable_notify_one(&s_tester.signal);
355 aws_mutex_unlock(&s_tester.lock);
356 }
357
358 static int s_imds_client_new_release(struct aws_allocator *allocator, void *ctx) {
359 (void)ctx;
360
361 s_aws_imds_tester_init(allocator);
362
363 struct aws_imds_client_options options = {
364 .bootstrap = s_tester.bootstrap,
365 .function_table = &s_mock_function_table,
366 .shutdown_options =
367 {
368 .shutdown_callback = s_on_shutdown_complete,
369 .shutdown_user_data = NULL,
370 },
371 };
372
373 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
374 aws_imds_client_release(client);
375
376 s_aws_wait_for_imds_client_shutdown_callback();
377
378 /* Because we mock the http connection manager, we never get a callback back from it */
379 aws_mem_release(allocator, client);
380
381 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
382
383 return 0;
384 }
385
386 AWS_TEST_CASE(imds_client_new_release, s_imds_client_new_release);
387
388 AWS_STATIC_STRING_FROM_LITERAL(s_ec2_metadata_root, "/latest/meta-data");
389 AWS_STATIC_STRING_FROM_LITERAL(s_expected_imds_token_uri, "/latest/api/token");
390 AWS_STATIC_STRING_FROM_LITERAL(s_expected_imds_resource_uri, "/latest/meta-data/iam/security-credentials/test-role");
391 AWS_STATIC_STRING_FROM_LITERAL(s_test_imds_token, "A00XXF3H00ZZ==");
392
393 static int s_imds_client_connect_failure(struct aws_allocator *allocator, void *ctx) {
394 (void)ctx;
395
396 s_aws_imds_tester_init(allocator);
397 s_tester.is_connection_acquire_successful = false;
398
399 struct aws_imds_client_options options = {
400 .bootstrap = s_tester.bootstrap,
401 .function_table = &s_mock_function_table,
402 .shutdown_options =
403 {
404 .shutdown_callback = s_on_shutdown_complete,
405 .shutdown_user_data = NULL,
406 },
407 };
408
409 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
410
411 aws_imds_client_get_resource_async(
412 client, aws_byte_cursor_from_string(s_ec2_metadata_root), s_get_resource_callback, NULL);
413
414 s_aws_wait_for_resource_result();
415
416 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
417 ASSERT_TRUE(s_tester.resource.len == 0);
418
419 aws_imds_client_release(client);
420
421 s_aws_wait_for_imds_client_shutdown_callback();
422
423 /* Because we mock the http connection manager, we never get a callback back from it */
424 aws_mem_release(allocator, client);
425
426 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
427
428 return 0;
429 }
430
431 AWS_TEST_CASE(imds_client_connect_failure, s_imds_client_connect_failure);
432
433 static void s_validate_token_ttl_header(const struct aws_http_message *request) {
434 const struct aws_http_headers *headers = aws_http_message_get_const_headers(request);
435 struct aws_byte_cursor ttl_header = aws_byte_cursor_from_c_str("x-aws-ec2-metadata-token-ttl-seconds");
436 struct aws_byte_cursor ttl_value;
437 int ret = aws_http_headers_get(headers, ttl_header, &ttl_value);
438 if (ret == AWS_OP_SUCCESS) {
439 s_tester.token_ttl_header_exist[s_tester.current_request] = true;
440 if (aws_byte_cursor_eq_c_str_ignore_case(&ttl_value, "21600")) {
441 s_tester.token_ttl_header_expected[s_tester.current_request] = true;
442 } else {
443 s_tester.token_ttl_header_expected[s_tester.current_request] = false;
444 }
445 } else {
446 s_tester.token_ttl_header_exist[s_tester.current_request] = false;
447 }
448 }
449
450 static void s_validate_token_header(const struct aws_http_message *request) {
451 const struct aws_http_headers *headers = aws_http_message_get_const_headers(request);
452 struct aws_byte_cursor token_header = aws_byte_cursor_from_c_str("x-aws-ec2-metadata-token");
453 struct aws_byte_cursor token_value;
454 int ret = aws_http_headers_get(headers, token_header, &token_value);
455 if (ret == AWS_OP_SUCCESS) {
456 s_tester.token_header_exist[s_tester.current_request] = true;
457 if (aws_byte_cursor_eq_c_str_ignore_case(&token_value, "A00XXF3H00ZZ==")) {
458 s_tester.token_header_expected[s_tester.current_request] = true;
459 } else {
460 s_tester.token_header_expected[s_tester.current_request] = false;
461 }
462 } else {
463 s_tester.token_header_exist[s_tester.current_request] = false;
464 }
465 }
466
467 static int s_validate_uri_path_and_resource(int expected_requests, bool get_resource) {
468
469 ASSERT_UINT_EQUALS(expected_requests, s_tester.current_request);
470
471 int idx = s_tester.token_request_idx;
472 if (s_tester.current_request >= 1) {
473 ASSERT_BIN_ARRAYS_EQUALS(
474 s_tester.request_uris[idx].buffer,
475 s_tester.request_uris[idx].len,
476 s_expected_imds_token_uri->bytes,
477 s_expected_imds_token_uri->len);
478 }
479 idx++;
480 if (s_tester.current_request >= 2) {
481 ASSERT_BIN_ARRAYS_EQUALS(
482 s_tester.request_uris[idx].buffer,
483 s_tester.request_uris[idx].len,
484 s_expected_imds_resource_uri->bytes,
485 s_expected_imds_resource_uri->len);
486 }
487
488 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
489
490 if (get_resource) {
491 ASSERT_TRUE(s_tester.resource.len != 0);
492 } else {
493 ASSERT_TRUE(s_tester.resource.len == 0);
494 }
495
496 return 0;
497 }
498
499 static int s_validate_uri_path(int expected_requests, struct aws_byte_cursor resource_uri) {
500
501 ASSERT_UINT_EQUALS(expected_requests, s_tester.current_request);
502
503 int idx = s_tester.token_request_idx;
504 if (s_tester.current_request >= 1) {
505 ASSERT_BIN_ARRAYS_EQUALS(
506 s_tester.request_uris[idx].buffer,
507 s_tester.request_uris[idx].len,
508 s_expected_imds_token_uri->bytes,
509 s_expected_imds_token_uri->len);
510 }
511 idx++;
512 if (s_tester.current_request >= 2) {
513 ASSERT_BIN_ARRAYS_EQUALS(
514 s_tester.request_uris[idx].buffer, s_tester.request_uris[idx].len, resource_uri.ptr, resource_uri.len);
515 }
516 return 0;
517 }
518
519 static int s_validate_uri_path_and_excpected_resource(
520 int expected_requests,
521 struct aws_byte_cursor resource_uri,
522 struct aws_byte_cursor expected_resource) {
523
524 ASSERT_UINT_EQUALS(expected_requests, s_tester.current_request);
525
526 int idx = s_tester.token_request_idx;
527 if (s_tester.current_request >= 1) {
528 ASSERT_BIN_ARRAYS_EQUALS(
529 s_tester.request_uris[idx].buffer,
530 s_tester.request_uris[idx].len,
531 s_expected_imds_token_uri->bytes,
532 s_expected_imds_token_uri->len);
533 }
534 idx++;
535 if (s_tester.current_request >= 2) {
536 ASSERT_BIN_ARRAYS_EQUALS(
537 s_tester.request_uris[idx].buffer, s_tester.request_uris[idx].len, resource_uri.ptr, resource_uri.len);
538 }
539
540 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
541
542 ASSERT_BIN_ARRAYS_EQUALS(
543 s_tester.resource.buffer, s_tester.resource.len, expected_resource.ptr, expected_resource.len);
544
545 return 0;
546 }
547
548 static int s_imds_client_token_request_failure(struct aws_allocator *allocator, void *ctx) {
549 (void)ctx;
550
551 s_aws_imds_tester_init(allocator);
552 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_400_BAD_REQUEST;
553 struct aws_imds_client_options options = {
554 .bootstrap = s_tester.bootstrap,
555 .function_table = &s_mock_function_table,
556 .shutdown_options =
557 {
558 .shutdown_callback = s_on_shutdown_complete,
559 .shutdown_user_data = NULL,
560 },
561 };
562
563 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
564
565 aws_imds_client_get_resource_async(
566 client, aws_byte_cursor_from_string(s_ec2_metadata_root), s_get_resource_callback, NULL);
567
568 s_aws_wait_for_resource_result();
569
570 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
571 ASSERT_TRUE(s_tester.resource.len == 0);
572
573 ASSERT_TRUE(s_validate_uri_path_and_resource(1, false /*no resource*/) == 0);
574 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
575 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
576 ASSERT_FALSE(s_tester.token_header_exist[0]);
577
578 aws_imds_client_release(client);
579
580 s_aws_wait_for_imds_client_shutdown_callback();
581
582 /* Because we mock the http connection manager, we never get a callback back from it */
583 aws_mem_release(allocator, client);
584
585 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
586
587 return 0;
588 }
589
590 AWS_TEST_CASE(imds_client_token_request_failure, s_imds_client_token_request_failure);
591
592 static int s_imds_client_insecure_fallback_request_failure(struct aws_allocator *allocator, void *ctx) {
593 (void)ctx;
594
595 s_aws_imds_tester_init(allocator);
596 /* secure data flow is not supported, fallback to insecurity */
597 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_403_FORBIDDEN;
598 /* Insecurity fails as well */
599 s_tester.response_code[1] = AWS_HTTP_STATUS_CODE_401_UNAUTHORIZED;
600 struct aws_imds_client_options options = {
601 .bootstrap = s_tester.bootstrap,
602 .function_table = &s_mock_function_table,
603 .shutdown_options =
604 {
605 .shutdown_callback = s_on_shutdown_complete,
606 .shutdown_user_data = NULL,
607 },
608 };
609
610 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
611
612 aws_imds_client_get_resource_async(
613 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
614
615 s_aws_wait_for_resource_result();
616
617 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
618 ASSERT_TRUE(s_tester.resource.len == 0);
619
620 ASSERT_TRUE(s_validate_uri_path_and_resource(2, false /*no resource*/) == 0);
621 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
622 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
623 ASSERT_FALSE(s_tester.token_header_exist[0]);
624
625 ASSERT_UINT_EQUALS(s_tester.error_code, AWS_AUTH_IMDS_CLIENT_SOURCE_FAILURE);
626
627 aws_imds_client_release(client);
628
629 s_aws_wait_for_imds_client_shutdown_callback();
630
631 /* Because we mock the http connection manager, we never get a callback back from it */
632 aws_mem_release(allocator, client);
633
634 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
635
636 return 0;
637 }
638
639 AWS_TEST_CASE(imds_client_insecure_fallback_request_failure, s_imds_client_insecure_fallback_request_failure);
640
641 AWS_STATIC_STRING_FROM_LITERAL(
642 s_good_response,
643 "{\"AccessKeyId\":\"SuccessfulAccessKey\", \n \"SecretAccessKey\":\"SuccessfulSecret\", \n "
644 "\"Token\":\"TokenSuccess\", \n \"Expiration\":\"2020-02-25T06:03:31Z\"}");
645 AWS_STATIC_STRING_FROM_LITERAL(s_access_key, "SuccessfulAccessKey");
646 AWS_STATIC_STRING_FROM_LITERAL(s_secret_key, "SuccessfulSecret");
647 AWS_STATIC_STRING_FROM_LITERAL(s_token, "TokenSuccess");
648 AWS_STATIC_STRING_FROM_LITERAL(s_expiration, "2020-02-25T06:03:31Z");
649
650 static int s_imds_client_v1_fallback_disabled_failure(struct aws_allocator *allocator, void *ctx) {
651 (void)ctx;
652
653 s_aws_imds_tester_init(allocator);
654 /* secure data flow is not supported */
655 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_403_FORBIDDEN;
656 /* v1 would have worked if fallback was not disabled */
657 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
658 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
659
660 struct aws_imds_client_options options = {
661 .bootstrap = s_tester.bootstrap,
662 .function_table = &s_mock_function_table,
663 .ec2_metadata_v1_disabled = true,
664 .shutdown_options =
665 {
666 .shutdown_callback = s_on_shutdown_complete,
667 .shutdown_user_data = NULL,
668 },
669 };
670
671 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
672
673 aws_imds_client_get_resource_async(
674 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
675
676 s_aws_wait_for_resource_result();
677
678 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
679 ASSERT_TRUE(s_tester.resource.len == 0);
680 /* There was no fallback so we didn't get any resource */
681 ASSERT_TRUE(s_validate_uri_path_and_resource(1, false /*no resource*/) == 0);
682 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
683 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
684 ASSERT_FALSE(s_tester.token_header_exist[0]);
685
686 ASSERT_UINT_EQUALS(s_tester.error_code, AWS_AUTH_IMDS_CLIENT_SOURCE_FAILURE);
687
688 aws_imds_client_release(client);
689
690 s_aws_wait_for_imds_client_shutdown_callback();
691
692 /* Because we mock the http connection manager, we never get a callback back from it */
693 aws_mem_release(allocator, client);
694
695 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
696
697 return 0;
698 }
699
700 AWS_TEST_CASE(imds_client_v1_fallback_disabled_failure, s_imds_client_v1_fallback_disabled_failure);
701
702 static int s_imds_client_resource_request_failure(struct aws_allocator *allocator, void *ctx) {
703 (void)ctx;
704
705 s_aws_imds_tester_init(allocator);
706
707 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
708 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
709
710 struct aws_imds_client_options options = {
711 .bootstrap = s_tester.bootstrap,
712 .function_table = &s_mock_function_table,
713 .shutdown_options =
714 {
715 .shutdown_callback = s_on_shutdown_complete,
716 .shutdown_user_data = NULL,
717 },
718 };
719
720 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
721 aws_imds_client_get_resource_async(
722 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
723
724 s_aws_wait_for_resource_result();
725
726 ASSERT_TRUE(s_validate_uri_path_and_resource(2, false /*no resource*/) == 0);
727 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
728 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
729 ASSERT_FALSE(s_tester.token_header_exist[0]);
730
731 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
732 ASSERT_TRUE(s_tester.token_header_exist[1]);
733 ASSERT_TRUE(s_tester.token_header_expected[1]);
734
735 aws_imds_client_release(client);
736
737 s_aws_wait_for_imds_client_shutdown_callback();
738
739 /* Because we mock the http connection manager, we never get a callback back from it */
740 aws_mem_release(allocator, client);
741
742 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
743 return 0;
744 }
745
746 AWS_TEST_CASE(imds_client_resource_request_failure, s_imds_client_resource_request_failure);
747
748 static int s_imds_client_resource_request_success(struct aws_allocator *allocator, void *ctx) {
749 (void)ctx;
750
751 s_aws_imds_tester_init(allocator);
752
753 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
754 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
755
756 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
757 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
758
759 struct aws_imds_client_options options = {
760 .bootstrap = s_tester.bootstrap,
761 .function_table = &s_mock_function_table,
762 .shutdown_options =
763 {
764 .shutdown_callback = s_on_shutdown_complete,
765 .shutdown_user_data = NULL,
766 },
767 };
768
769 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
770 aws_imds_client_get_resource_async(
771 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
772
773 s_aws_wait_for_resource_result();
774
775 ASSERT_TRUE(s_validate_uri_path_and_resource(2, true /*got resource*/) == 0);
776
777 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
778 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
779 ASSERT_FALSE(s_tester.token_header_exist[0]);
780
781 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
782 ASSERT_TRUE(s_tester.token_header_exist[1]);
783 ASSERT_TRUE(s_tester.token_header_expected[1]);
784
785 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
786
787 aws_imds_client_release(client);
788
789 s_aws_wait_for_imds_client_shutdown_callback();
790
791 /* Because we mock the http connection manager, we never get a callback back from it */
792 aws_mem_release(allocator, client);
793
794 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
795
796 return 0;
797 }
798
799 AWS_TEST_CASE(imds_client_resource_request_success, s_imds_client_resource_request_success);
800
801 static int s_imds_client_insecure_resource_request_success(struct aws_allocator *allocator, void *ctx) {
802 (void)ctx;
803
804 s_aws_imds_tester_init(allocator);
805 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_403_FORBIDDEN;
806
807 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
808 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
809
810 struct aws_imds_client_options options = {
811 .bootstrap = s_tester.bootstrap,
812 .function_table = &s_mock_function_table,
813 .shutdown_options =
814 {
815 .shutdown_callback = s_on_shutdown_complete,
816 .shutdown_user_data = NULL,
817 },
818 };
819
820 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
821 aws_imds_client_get_resource_async(
822 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
823
824 s_aws_wait_for_resource_result();
825
826 ASSERT_TRUE(s_validate_uri_path_and_resource(2, true /*got resource*/) == 0);
827
828 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
829 ASSERT_FALSE(s_tester.token_header_exist[1]);
830
831 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
832
833 aws_imds_client_release(client);
834
835 s_aws_wait_for_imds_client_shutdown_callback();
836
837 /* Because we mock the http connection manager, we never get a callback back from it */
838 aws_mem_release(allocator, client);
839
840 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
841
842 return 0;
843 }
844
845 AWS_TEST_CASE(imds_client_insecure_resource_request_success, s_imds_client_insecure_resource_request_success);
846
847 static int s_imds_client_insecure_then_secure_resource_request_success(struct aws_allocator *allocator, void *ctx) {
848 (void)ctx;
849
850 s_aws_imds_tester_init(allocator);
851 s_tester.token_request_idx = 1;
852 s_tester.response_code[0] = AWS_HTTP_STATUS_CODE_401_UNAUTHORIZED;
853 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
854 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &test_token_cursor);
855
856 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
857 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
858
859 struct aws_imds_client_options options = {
860 .bootstrap = s_tester.bootstrap,
861 .function_table = &s_mock_function_table,
862 .shutdown_options =
863 {
864 .shutdown_callback = s_on_shutdown_complete,
865 .shutdown_user_data = NULL,
866 },
867 .imds_version = IMDS_PROTOCOL_V1,
868 };
869
870 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
871 aws_imds_client_get_resource_async(
872 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
873 s_aws_wait_for_resource_result();
874
875 ASSERT_TRUE(s_tester.has_received_resource_callback == true);
876 ASSERT_TRUE(s_validate_uri_path_and_resource(3, true /*no creds*/) == 0);
877
878 ASSERT_FALSE(s_tester.token_ttl_header_exist[0]);
879 ASSERT_FALSE(s_tester.token_header_exist[0]);
880
881 ASSERT_TRUE(s_tester.token_ttl_header_exist[1]);
882 ASSERT_TRUE(s_tester.token_ttl_header_expected[1]);
883 ASSERT_FALSE(s_tester.token_header_exist[1]);
884
885 ASSERT_FALSE(s_tester.token_ttl_header_exist[2]);
886 ASSERT_TRUE(s_tester.token_header_exist[2]);
887 ASSERT_TRUE(s_tester.token_header_expected[2]);
888
889 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
890
891 aws_imds_client_release(client);
892
893 s_aws_wait_for_imds_client_shutdown_callback();
894
895 /* Because we mock the http connection manager, we never get a callback back from it */
896 aws_mem_release(allocator, client);
897
898 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
899
900 return 0;
901 }
902 AWS_TEST_CASE(
903 imds_client_insecure_then_secure_resource_request_success,
904 s_imds_client_insecure_then_secure_resource_request_success);
905
906 static int s_aws_http_stream_get_multiple_incoming_response_status_mock(
907 const struct aws_http_stream *stream,
908 int *out_status_code) {
909
910 (void)stream;
911 /* randomly return 403/200 */
912 uint32_t rand_output;
913 int ret[2] = {AWS_HTTP_STATUS_CODE_200_OK, AWS_HTTP_STATUS_CODE_403_FORBIDDEN};
914 aws_device_random_u32(&rand_output);
915 *out_status_code = ret[rand_output % 2];
916 return AWS_OP_SUCCESS;
917 }
918
919 static struct aws_http_stream *s_aws_http_connection_make_multiple_requests_mock(
920 struct aws_http_connection *client_connection,
921 const struct aws_http_make_request_options *options) {
922
923 (void)client_connection;
924 (void)options;
925
926 struct aws_byte_cursor path;
927 AWS_ZERO_STRUCT(path);
928 aws_http_message_get_request_path(options->request, &path);
929
930 if (aws_byte_cursor_eq_c_str_ignore_case(&path, "/latest/api/token")) {
931 s_validate_token_ttl_header(options->request);
932 s_invoke_mock_request_callbacks(options, &s_tester.response_data_callbacks[0]);
933 } else {
934 s_validate_token_header(options->request);
935 s_invoke_mock_request_callbacks(options, &s_tester.response_data_callbacks[1]);
936 }
937 return (struct aws_http_stream *)1;
938 }
939
940 static bool s_has_tester_received_expected_resources(void *user_data) {
941 return s_tester.successful_requests == (*(int *)user_data);
942 }
943
944 static void s_aws_wait_for_all_resources(int expected_resources_cnt) {
945 aws_mutex_lock(&s_tester.lock);
946 aws_condition_variable_wait_pred(
947 &s_tester.signal, &s_tester.lock, s_has_tester_received_expected_resources, &expected_resources_cnt);
948 aws_mutex_unlock(&s_tester.lock);
949 }
950
951 static void s_multiple_request_get_resource_callback(
952 const struct aws_byte_buf *resource,
953 int error_code,
954 void *user_data) {
955 (void)user_data;
956 (void)error_code;
957 aws_mutex_lock(&s_tester.lock);
958 s_tester.has_received_resource_callback = true;
959 if (resource && resource->len) {
960 struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(resource);
961 aws_byte_buf_reset(&s_tester.resource, true);
962 aws_byte_buf_append_dynamic(&s_tester.resource, &cursor);
963 s_tester.successful_requests++;
964 }
965 aws_condition_variable_notify_one(&s_tester.signal);
966 aws_mutex_unlock(&s_tester.lock);
967 }
968
969 static int s_imds_client_multiple_resource_requests_random_responses_finally_all_success(
970 struct aws_allocator *allocator,
971 void *ctx) {
972 (void)ctx;
973
974 s_aws_imds_tester_init(allocator);
975
976 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
977 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
978
979 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
980 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
981
982 struct aws_imds_client_options options = {
983 .bootstrap = s_tester.bootstrap,
984 .function_table = &s_mock_function_table,
985 .shutdown_options =
986 {
987 .shutdown_callback = s_on_shutdown_complete,
988 .shutdown_user_data = NULL,
989 },
990 .imds_version = IMDS_PROTOCOL_V2,
991 };
992
993 options.function_table->aws_http_stream_get_incoming_response_status =
994 s_aws_http_stream_get_multiple_incoming_response_status_mock;
995 options.function_table->aws_http_connection_make_request = s_aws_http_connection_make_multiple_requests_mock;
996
997 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
998 for (size_t i = 0; i < 5000; i++) {
999 aws_imds_client_get_resource_async(
1000 client,
1001 aws_byte_cursor_from_string(s_expected_imds_resource_uri),
1002 s_multiple_request_get_resource_callback,
1003 NULL);
1004 }
1005 s_aws_wait_for_all_resources(5000);
1006
1007 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
1008
1009 aws_imds_client_release(client);
1010
1011 s_aws_wait_for_imds_client_shutdown_callback();
1012
1013 /* Because we mock the http connection manager, we never get a callback back from it */
1014 aws_mem_release(allocator, client);
1015
1016 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1017
1018 return 0;
1019 }
1020 AWS_TEST_CASE(
1021 imds_client_multiple_resource_requests_random_responses_finally_all_success,
1022 s_imds_client_multiple_resource_requests_random_responses_finally_all_success);
1023
1024 static int s_imds_client_real_success(struct aws_allocator *allocator, void *ctx) {
1025 (void)ctx;
1026
1027 aws_auth_library_init(allocator);
1028
1029 struct aws_logger_standard_options logger_options = {
1030 .level = AWS_LOG_LEVEL_TRACE,
1031 .file = stderr,
1032 };
1033
1034 struct aws_logger logger;
1035 ASSERT_SUCCESS(aws_logger_init_standard(&logger, allocator, &logger_options));
1036 aws_logger_set(&logger);
1037
1038 s_aws_imds_tester_init(allocator);
1039
1040 struct aws_host_resolver_default_options resolver_options = {
1041 .el_group = s_tester.el_group,
1042 .max_entries = 8,
1043 };
1044 struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options);
1045
1046 struct aws_client_bootstrap_options bootstrap_options = {
1047 .event_loop_group = s_tester.el_group,
1048 .host_resolver = resolver,
1049 };
1050 struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
1051
1052 struct aws_imds_client_options options = {
1053 .bootstrap = bootstrap,
1054 .shutdown_options =
1055 {
1056 .shutdown_callback = s_on_shutdown_complete,
1057 .shutdown_user_data = NULL,
1058 },
1059 };
1060
1061 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1062 aws_imds_client_get_resource_async(
1063 client, aws_byte_cursor_from_string(s_ec2_metadata_root), s_get_resource_callback, NULL);
1064 s_aws_wait_for_resource_result();
1065
1066 aws_imds_client_release(client);
1067
1068 s_aws_wait_for_imds_client_shutdown_callback();
1069
1070 aws_client_bootstrap_release(bootstrap);
1071 aws_host_resolver_release(resolver);
1072
1073 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1074
1075 aws_auth_library_clean_up();
1076
1077 aws_logger_set(NULL);
1078 aws_logger_clean_up(&logger);
1079
1080 return 0;
1081 }
1082
1083 AWS_TEST_CASE(imds_client_real_success, s_imds_client_real_success);
1084
1085 AWS_STATIC_STRING_FROM_LITERAL(s_test_ami_id, "ami-5b70e32");
1086 static int s_imds_client_get_ami_id_success(struct aws_allocator *allocator, void *ctx) {
1087 (void)ctx;
1088
1089 s_aws_imds_tester_init(allocator);
1090
1091 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1092 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1093
1094 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_test_ami_id);
1095 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1096
1097 struct aws_imds_client_options options = {
1098 .bootstrap = s_tester.bootstrap,
1099 .function_table = &s_mock_function_table,
1100 .shutdown_options =
1101 {
1102 .shutdown_callback = s_on_shutdown_complete,
1103 .shutdown_user_data = NULL,
1104 },
1105 };
1106
1107 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1108 aws_imds_client_get_ami_id(client, s_get_resource_callback, NULL);
1109
1110 s_aws_wait_for_resource_result();
1111
1112 ASSERT_TRUE(
1113 s_validate_uri_path_and_excpected_resource(
1114 2, aws_byte_cursor_from_c_str("/latest/meta-data/ami-id"), good_response_cursor) == 0);
1115
1116 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
1117 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
1118 ASSERT_FALSE(s_tester.token_header_exist[0]);
1119
1120 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
1121 ASSERT_TRUE(s_tester.token_header_exist[1]);
1122 ASSERT_TRUE(s_tester.token_header_expected[1]);
1123
1124 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_test_ami_id);
1125
1126 aws_imds_client_release(client);
1127
1128 s_aws_wait_for_imds_client_shutdown_callback();
1129
1130 /* Because we mock the http connection manager, we never get a callback back from it */
1131 aws_mem_release(allocator, client);
1132
1133 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1134
1135 return 0;
1136 }
1137 AWS_TEST_CASE(imds_client_get_ami_id_success, s_imds_client_get_ami_id_success);
1138
1139 AWS_STATIC_STRING_FROM_LITERAL(s_test_ancestor_ami_ids, "ami-5b70e32\nami-5b70e33\nami-5b70e34");
1140 AWS_STATIC_STRING_FROM_LITERAL(s_test_ancestor_ami_id1, "ami-5b70e32");
1141 AWS_STATIC_STRING_FROM_LITERAL(s_test_ancestor_ami_id2, "ami-5b70e33");
1142 AWS_STATIC_STRING_FROM_LITERAL(s_test_ancestor_ami_id3, "ami-5b70e34");
1143 static struct aws_byte_cursor s_newline_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\n");
1144
1145 static int s_assert_get_ancestor_ami_ids(const struct aws_array_list *array) {
1146 s_tester.has_received_resource_callback = true;
1147 size_t len = aws_array_list_length(array);
1148 ASSERT_TRUE(len == 3);
1149 struct aws_byte_cursor cursor[3];
1150 for (size_t i = 0; i < len; i++) {
1151 aws_array_list_get_at(array, &cursor[i], i);
1152 aws_byte_buf_append_dynamic(&s_tester.resource, &cursor[i]);
1153 aws_byte_buf_append_dynamic(&s_tester.resource, &s_newline_cursor);
1154 }
1155 s_tester.resource.len--;
1156 ASSERT_CURSOR_VALUE_STRING_EQUALS(cursor[0], s_test_ancestor_ami_id1);
1157 ASSERT_CURSOR_VALUE_STRING_EQUALS(cursor[1], s_test_ancestor_ami_id2);
1158 ASSERT_CURSOR_VALUE_STRING_EQUALS(cursor[2], s_test_ancestor_ami_id3);
1159
1160 if (array) {
1161 s_tester.successful_requests++;
1162 }
1163 return 0;
1164 }
1165 static void s_get_ancestor_ami_ids_callback(const struct aws_array_list *array, int error_code, void *user_data) {
1166 (void)user_data;
1167 (void)error_code;
1168 aws_mutex_lock(&s_tester.lock);
1169 s_assert_get_ancestor_ami_ids(array);
1170 aws_condition_variable_notify_one(&s_tester.signal);
1171 aws_mutex_unlock(&s_tester.lock);
1172 }
1173
1174 static int s_imds_client_get_ancestor_ami_ids_success(struct aws_allocator *allocator, void *ctx) {
1175 (void)ctx;
1176
1177 s_aws_imds_tester_init(allocator);
1178
1179 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1180 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1181
1182 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_test_ancestor_ami_ids);
1183 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1184
1185 struct aws_imds_client_options options = {
1186 .bootstrap = s_tester.bootstrap,
1187 .function_table = &s_mock_function_table,
1188 .shutdown_options =
1189 {
1190 .shutdown_callback = s_on_shutdown_complete,
1191 .shutdown_user_data = NULL,
1192 },
1193 };
1194
1195 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1196 aws_imds_client_get_ancestor_ami_ids(client, s_get_ancestor_ami_ids_callback, NULL);
1197
1198 s_aws_wait_for_resource_result();
1199
1200 ASSERT_TRUE(
1201 s_validate_uri_path_and_excpected_resource(
1202 2, aws_byte_cursor_from_c_str("/latest/meta-data/ancestor-ami-ids"), good_response_cursor) == 0);
1203
1204 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
1205 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
1206 ASSERT_FALSE(s_tester.token_header_exist[0]);
1207
1208 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
1209 ASSERT_TRUE(s_tester.token_header_exist[1]);
1210 ASSERT_TRUE(s_tester.token_header_expected[1]);
1211
1212 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_test_ancestor_ami_ids);
1213
1214 aws_imds_client_release(client);
1215
1216 s_aws_wait_for_imds_client_shutdown_callback();
1217
1218 /* Because we mock the http connection manager, we never get a callback back from it */
1219 aws_mem_release(allocator, client);
1220
1221 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1222
1223 return 0;
1224 }
1225 AWS_TEST_CASE(imds_client_get_ancestor_ami_ids_success, s_imds_client_get_ancestor_ami_ids_success);
1226
1227 AWS_STATIC_STRING_FROM_LITERAL(
1228 s_iam_profile,
1229 "{\"LastUpdated\" : \"2020-06-03T20:42:19Z\", \n "
1230 "\"InstanceProfileArn\" : \"arn:aws:iam::030535792909:instance-profile/CloudWatchAgentServerRole\", \n "
1231 "\"InstanceProfileId\" : \"AIPAQOHATHEGTGNQ5THQB\"}");
1232
1233 AWS_STATIC_STRING_FROM_LITERAL(s_test_last_updated, "2020-06-03T20:42:19Z");
1234 AWS_STATIC_STRING_FROM_LITERAL(
1235 s_test_profile_arn,
1236 "arn:aws:iam::030535792909:instance-profile/CloudWatchAgentServerRole");
1237 AWS_STATIC_STRING_FROM_LITERAL(s_test_profile_id, "AIPAQOHATHEGTGNQ5THQB");
1238
1239 static int s_assert_get_iam_profile(const struct aws_imds_iam_profile *iam) {
1240 s_tester.has_received_resource_callback = true;
1241
1242 ASSERT_CURSOR_VALUE_STRING_EQUALS(iam->instance_profile_arn, s_test_profile_arn);
1243 ASSERT_CURSOR_VALUE_STRING_EQUALS(iam->instance_profile_id, s_test_profile_id);
1244 struct aws_byte_buf buf;
1245 aws_byte_buf_init(&buf, s_tester.allocator, 100);
1246 aws_date_time_to_utc_time_str(&iam->last_updated, AWS_DATE_FORMAT_ISO_8601, &buf);
1247 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&buf), s_test_last_updated);
1248 aws_byte_buf_clean_up(&buf);
1249 if (iam) {
1250 s_tester.successful_requests++;
1251 }
1252 return 0;
1253 }
1254
1255 static void s_get_iam_profile_callback(const struct aws_imds_iam_profile *iam, int error_code, void *user_data) {
1256 (void)user_data;
1257 (void)error_code;
1258 aws_mutex_lock(&s_tester.lock);
1259 s_assert_get_iam_profile(iam);
1260 aws_condition_variable_notify_one(&s_tester.signal);
1261 aws_mutex_unlock(&s_tester.lock);
1262 }
1263
1264 static int s_imds_client_get_iam_profile_success(struct aws_allocator *allocator, void *ctx) {
1265 (void)ctx;
1266
1267 s_aws_imds_tester_init(allocator);
1268
1269 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1270 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1271
1272 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_iam_profile);
1273 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1274
1275 struct aws_imds_client_options options = {
1276 .bootstrap = s_tester.bootstrap,
1277 .function_table = &s_mock_function_table,
1278 .shutdown_options =
1279 {
1280 .shutdown_callback = s_on_shutdown_complete,
1281 .shutdown_user_data = NULL,
1282 },
1283 };
1284
1285 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1286 aws_imds_client_get_iam_profile(client, s_get_iam_profile_callback, NULL);
1287
1288 s_aws_wait_for_resource_result();
1289
1290 ASSERT_TRUE(s_validate_uri_path(2, aws_byte_cursor_from_c_str("/latest/meta-data/iam/info")) == 0);
1291
1292 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
1293 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
1294 ASSERT_FALSE(s_tester.token_header_exist[0]);
1295
1296 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
1297 ASSERT_TRUE(s_tester.token_header_exist[1]);
1298 ASSERT_TRUE(s_tester.token_header_expected[1]);
1299
1300 aws_imds_client_release(client);
1301
1302 s_aws_wait_for_imds_client_shutdown_callback();
1303
1304 /* Because we mock the http connection manager, we never get a callback back from it */
1305 aws_mem_release(allocator, client);
1306
1307 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1308
1309 return 0;
1310 }
1311 AWS_TEST_CASE(imds_client_get_iam_profile_success, s_imds_client_get_iam_profile_success);
1312
1313 AWS_STATIC_STRING_FROM_LITERAL(
1314 s_instance_info,
1315 "{\"accountId\" : \"030535792909\", \n"
1316 "\"architecture\" : \"x86_64\", \n"
1317 "\"availabilityZone\" : \"us-west-2a\", \n"
1318 "\"billingProducts\" : [\"1234\", \"abcd\"], \n"
1319 "\"devpayProductCodes\" : null, \n"
1320 "\"marketplaceProductCodes\" : null, \n"
1321 "\"imageId\" : \"ami-5b70e323\", \n"
1322 "\"instanceId\" : \"i-022a93b5e640c0248\", \n"
1323 "\"instanceType\" : \"c4.8xlarge\", \n"
1324 "\"kernelId\" : null, \n"
1325 "\"pendingTime\" : \"2020-05-27T08:41:17Z\", \n"
1326 "\"privateIp\" : \"172.31.22.164\", \n"
1327 "\"ramdiskId\" : null, \n"
1328 "\"region\" : \"us-west-2\", \n"
1329 "\"version\" : \"2017-09-30\" \n}");
1330
1331 AWS_STATIC_STRING_FROM_LITERAL(s_account_id, "030535792909");
1332 AWS_STATIC_STRING_FROM_LITERAL(s_architecture, "x86_64");
1333 AWS_STATIC_STRING_FROM_LITERAL(s_availability_zone, "us-west-2a");
1334 AWS_STATIC_STRING_FROM_LITERAL(s_image_id, "ami-5b70e323");
1335 AWS_STATIC_STRING_FROM_LITERAL(s_instance_id, "i-022a93b5e640c0248");
1336 AWS_STATIC_STRING_FROM_LITERAL(s_instance_type, "c4.8xlarge");
1337 AWS_STATIC_STRING_FROM_LITERAL(s_pending_time, "2020-05-27T08:41:17Z");
1338 AWS_STATIC_STRING_FROM_LITERAL(s_private_ip, "172.31.22.164");
1339 AWS_STATIC_STRING_FROM_LITERAL(s_region, "us-west-2");
1340 AWS_STATIC_STRING_FROM_LITERAL(s_version, "2017-09-30");
1341 AWS_STATIC_STRING_FROM_LITERAL(s_billing_product1, "1234");
1342 AWS_STATIC_STRING_FROM_LITERAL(s_billing_product2, "abcd");
1343
1344 static int s_assert_get_instance_info(const struct aws_imds_instance_info *instance) {
1345 s_tester.has_received_resource_callback = true;
1346
1347 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->account_id, s_account_id);
1348 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->architecture, s_architecture);
1349 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->availability_zone, s_availability_zone);
1350 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->image_id, s_image_id);
1351 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->instance_id, s_instance_id);
1352 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->instance_type, s_instance_type);
1353 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->private_ip, s_private_ip);
1354 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->region, s_region);
1355 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->version, s_version);
1356 ASSERT_CURSOR_VALUE_STRING_EQUALS(instance->availability_zone, s_availability_zone);
1357
1358 ASSERT_TRUE(aws_array_list_length(&instance->billing_products) == 2);
1359 ASSERT_TRUE(aws_array_list_length(&instance->marketplace_product_codes) == 0);
1360
1361 struct aws_byte_cursor cursor[2];
1362 for (size_t i = 0; i < 2; i++) {
1363 aws_array_list_get_at(&instance->billing_products, &cursor[i], i);
1364 }
1365 ASSERT_CURSOR_VALUE_STRING_EQUALS(cursor[0], s_billing_product1);
1366 ASSERT_CURSOR_VALUE_STRING_EQUALS(cursor[1], s_billing_product2);
1367
1368 struct aws_byte_buf buf;
1369 aws_byte_buf_init(&buf, s_tester.allocator, 100);
1370 aws_date_time_to_utc_time_str(&instance->pending_time, AWS_DATE_FORMAT_ISO_8601, &buf);
1371 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&buf), s_pending_time);
1372 aws_byte_buf_clean_up(&buf);
1373 if (instance) {
1374 s_tester.successful_requests++;
1375 }
1376 return 0;
1377 }
1378
1379 static void s_get_instance_info_callback(
1380 const struct aws_imds_instance_info *instance,
1381 int error_code,
1382 void *user_data) {
1383 (void)user_data;
1384 (void)error_code;
1385 aws_mutex_lock(&s_tester.lock);
1386 s_assert_get_instance_info(instance);
1387 aws_condition_variable_notify_one(&s_tester.signal);
1388 aws_mutex_unlock(&s_tester.lock);
1389 }
1390
1391 static int s_imds_client_get_instance_info_success(struct aws_allocator *allocator, void *ctx) {
1392 (void)ctx;
1393
1394 s_aws_imds_tester_init(allocator);
1395
1396 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1397 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1398
1399 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_instance_info);
1400 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1401
1402 struct aws_imds_client_options options = {
1403 .bootstrap = s_tester.bootstrap,
1404 .function_table = &s_mock_function_table,
1405 .shutdown_options =
1406 {
1407 .shutdown_callback = s_on_shutdown_complete,
1408 .shutdown_user_data = NULL,
1409 },
1410 };
1411
1412 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1413 aws_imds_client_get_instance_info(client, s_get_instance_info_callback, NULL);
1414
1415 s_aws_wait_for_resource_result();
1416
1417 ASSERT_TRUE(s_validate_uri_path(2, aws_byte_cursor_from_c_str("/latest/dynamic/instance-identity/document")) == 0);
1418
1419 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
1420 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
1421 ASSERT_FALSE(s_tester.token_header_exist[0]);
1422
1423 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
1424 ASSERT_TRUE(s_tester.token_header_exist[1]);
1425 ASSERT_TRUE(s_tester.token_header_expected[1]);
1426
1427 aws_imds_client_release(client);
1428
1429 s_aws_wait_for_imds_client_shutdown_callback();
1430
1431 /* Because we mock the http connection manager, we never get a callback back from it */
1432 aws_mem_release(allocator, client);
1433
1434 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1435
1436 return 0;
1437 }
1438 AWS_TEST_CASE(imds_client_get_instance_info_success, s_imds_client_get_instance_info_success);
1439
1440 static int s_assert_get_credentials_info(const struct aws_credentials *creds) {
1441 s_tester.has_received_resource_callback = true;
1442
1443 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(creds), s_access_key);
1444 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_secret_access_key(creds), s_secret_key);
1445 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_session_token(creds), s_token);
1446
1447 struct aws_byte_buf buf;
1448 aws_byte_buf_init(&buf, s_tester.allocator, 100);
1449 struct aws_date_time date;
1450 aws_date_time_init_epoch_secs(&date, aws_credentials_get_expiration_timepoint_seconds(creds));
1451 aws_date_time_to_utc_time_str(&date, AWS_DATE_FORMAT_ISO_8601, &buf);
1452 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&buf), s_expiration);
1453 aws_byte_buf_clean_up(&buf);
1454 if (creds) {
1455 s_tester.successful_requests++;
1456 }
1457 return 0;
1458 }
1459
1460 static void s_get_credentails_callback(const struct aws_credentials *creds, int error_code, void *user_data) {
1461 (void)user_data;
1462 (void)error_code;
1463 aws_mutex_lock(&s_tester.lock);
1464 s_assert_get_credentials_info(creds);
1465 aws_condition_variable_notify_one(&s_tester.signal);
1466 aws_mutex_unlock(&s_tester.lock);
1467 }
1468
1469 static int s_imds_client_get_credentials_success(struct aws_allocator *allocator, void *ctx) {
1470 (void)ctx;
1471
1472 s_aws_imds_tester_init(allocator);
1473
1474 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1475 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1476
1477 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1478 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1479
1480 struct aws_imds_client_options options = {
1481 .bootstrap = s_tester.bootstrap,
1482 .function_table = &s_mock_function_table,
1483 .shutdown_options =
1484 {
1485 .shutdown_callback = s_on_shutdown_complete,
1486 .shutdown_user_data = NULL,
1487 },
1488 };
1489
1490 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1491 aws_imds_client_get_credentials(client, aws_byte_cursor_from_c_str("test_role"), s_get_credentails_callback, NULL);
1492
1493 s_aws_wait_for_resource_result();
1494
1495 ASSERT_TRUE(
1496 s_validate_uri_path(2, aws_byte_cursor_from_c_str("/latest/meta-data/iam/security-credentials/test_role")) ==
1497 0);
1498
1499 ASSERT_TRUE(s_tester.token_ttl_header_exist[0]);
1500 ASSERT_TRUE(s_tester.token_ttl_header_expected[0]);
1501 ASSERT_FALSE(s_tester.token_header_exist[0]);
1502
1503 ASSERT_FALSE(s_tester.token_ttl_header_exist[1]);
1504 ASSERT_TRUE(s_tester.token_header_exist[1]);
1505 ASSERT_TRUE(s_tester.token_header_expected[1]);
1506
1507 aws_imds_client_release(client);
1508
1509 s_aws_wait_for_imds_client_shutdown_callback();
1510
1511 /* Because we mock the http connection manager, we never get a callback back from it */
1512 aws_mem_release(allocator, client);
1513
1514 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1515
1516 return 0;
1517 }
1518 AWS_TEST_CASE(imds_client_get_credentials_success, s_imds_client_get_credentials_success);
1519
1520 static int s_imds_client_cache_token_refresh(struct aws_allocator *allocator, void *ctx) {
1521 (void)ctx;
1522
1523 s_aws_imds_tester_init(allocator);
1524
1525 s_tester.timestamp = 0;
1526
1527 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1528 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1529
1530 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1531 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1532 aws_array_list_push_back(&s_tester.response_data_callbacks[2], &good_response_cursor);
1533 aws_array_list_push_back(&s_tester.response_data_callbacks[3], &test_token_cursor);
1534 aws_array_list_push_back(&s_tester.response_data_callbacks[4], &good_response_cursor);
1535
1536 struct aws_imds_client_options options = {
1537 .bootstrap = s_tester.bootstrap,
1538 .function_table = &s_mock_function_table,
1539 .shutdown_options =
1540 {
1541 .shutdown_callback = s_on_shutdown_complete,
1542 .shutdown_user_data = NULL,
1543 },
1544 };
1545
1546 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1547 /* 1. request a resource */
1548 aws_imds_client_get_credentials(client, aws_byte_cursor_from_c_str("test_role"), s_get_credentails_callback, NULL);
1549 s_aws_wait_for_resource_result();
1550 /* Currently have 2 requests, one to get the token, the other to fetch resource */
1551 ASSERT_UINT_EQUALS(2, s_tester.current_request);
1552 ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, s_tester.error_code);
1553
1554 /* 2. Request another resource without change the timestamp. So that the cached token should be used */
1555 s_tester.has_received_resource_callback = false;
1556 aws_imds_client_get_credentials(client, aws_byte_cursor_from_c_str("test_role"), s_get_credentails_callback, NULL);
1557 s_aws_wait_for_resource_result();
1558 /* Currently have 3 requests, only one more to fetch the resource as the cached token being used. */
1559 ASSERT_UINT_EQUALS(3, s_tester.current_request);
1560 ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, s_tester.error_code);
1561
1562 /* 3. Update the time to expired time. Request another resource without change the timestamp. So that the cached
1563 * token should be expired, and will fetch another token. */
1564 s_tester.has_received_resource_callback = false;
1565 s_tester.timestamp = aws_timestamp_convert(21600, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL);
1566 aws_imds_client_get_credentials(client, aws_byte_cursor_from_c_str("test_role"), s_get_credentails_callback, NULL);
1567 s_aws_wait_for_resource_result();
1568 /* Currently have 3 requests, only one more to fetch the resource as the cached token being used. */
1569 ASSERT_UINT_EQUALS(5, s_tester.current_request);
1570 ASSERT_UINT_EQUALS(AWS_ERROR_SUCCESS, s_tester.error_code);
1571 aws_imds_client_release(client);
1572
1573 s_aws_wait_for_imds_client_shutdown_callback();
1574
1575 /* Because we mock the http connection manager, we never get a callback back from it */
1576 aws_mem_release(allocator, client);
1577
1578 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1579
1580 return 0;
1581 }
1582 AWS_TEST_CASE(imds_client_cache_token_refresh, s_imds_client_cache_token_refresh);
1583
1584 static int s_imds_client_ipv4_endpoint_mode(struct aws_allocator *allocator, void *ctx) {
1585 (void)ctx;
1586
1587 s_aws_imds_tester_init(allocator);
1588
1589 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1590 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1591
1592 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1593 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1594
1595 /* Enable endpoint verification for IPv4 default endpoint */
1596 s_tester.verify_endpoint_host = true;
1597 aws_byte_buf_init(&s_tester.expected_host, allocator, 20);
1598 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_c_str("169.254.169.254");
1599 aws_byte_buf_append_dynamic(&s_tester.expected_host, &host_cursor);
1600 s_tester.expected_port = 80;
1601
1602 struct aws_imds_client_options options = {
1603 .bootstrap = s_tester.bootstrap,
1604 .function_table = &s_mock_function_table,
1605 .imds_endpoint_mode = AWS_IMDS_ENDPOINT_MODE_IPV4,
1606 .shutdown_options =
1607 {
1608 .shutdown_callback = s_on_shutdown_complete,
1609 .shutdown_user_data = NULL,
1610 },
1611 };
1612
1613 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1614 ASSERT_NOT_NULL(client);
1615
1616 aws_imds_client_get_resource_async(
1617 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
1618
1619 s_aws_wait_for_resource_result();
1620
1621 ASSERT_TRUE(s_validate_uri_path_and_resource(2, true /*got resource*/) == 0);
1622 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
1623
1624 aws_imds_client_release(client);
1625 s_aws_wait_for_imds_client_shutdown_callback();
1626 aws_mem_release(allocator, client);
1627
1628 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1629
1630 return 0;
1631 }
1632
1633 AWS_TEST_CASE(imds_client_ipv4_endpoint_mode, s_imds_client_ipv4_endpoint_mode);
1634
1635 static int s_imds_client_ipv6_endpoint_mode(struct aws_allocator *allocator, void *ctx) {
1636 (void)ctx;
1637
1638 s_aws_imds_tester_init(allocator);
1639
1640 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1641 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1642
1643 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1644 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1645
1646 /* Enable endpoint verification for IPv6 default endpoint */
1647 s_tester.verify_endpoint_host = true;
1648 aws_byte_buf_init(&s_tester.expected_host, allocator, 20);
1649 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_c_str("fd00:ec2::254");
1650 aws_byte_buf_append_dynamic(&s_tester.expected_host, &host_cursor);
1651 s_tester.expected_port = 80;
1652
1653 struct aws_imds_client_options options = {
1654 .bootstrap = s_tester.bootstrap,
1655 .function_table = &s_mock_function_table,
1656 .imds_endpoint_mode = AWS_IMDS_ENDPOINT_MODE_IPV6,
1657 .shutdown_options =
1658 {
1659 .shutdown_callback = s_on_shutdown_complete,
1660 .shutdown_user_data = NULL,
1661 },
1662 };
1663
1664 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1665 ASSERT_NOT_NULL(client);
1666
1667 aws_imds_client_get_resource_async(
1668 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
1669
1670 s_aws_wait_for_resource_result();
1671
1672 ASSERT_TRUE(s_validate_uri_path_and_resource(2, true /*got resource*/) == 0);
1673 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
1674
1675 aws_imds_client_release(client);
1676 s_aws_wait_for_imds_client_shutdown_callback();
1677 aws_mem_release(allocator, client);
1678
1679 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1680
1681 return 0;
1682 }
1683
1684 AWS_TEST_CASE(imds_client_ipv6_endpoint_mode, s_imds_client_ipv6_endpoint_mode);
1685
1686 static int s_imds_client_custom_endpoint(struct aws_allocator *allocator, void *ctx) {
1687 (void)ctx;
1688
1689 s_aws_imds_tester_init(allocator);
1690
1691 struct aws_byte_cursor test_token_cursor = aws_byte_cursor_from_string(s_test_imds_token);
1692 aws_array_list_push_back(&s_tester.response_data_callbacks[0], &test_token_cursor);
1693
1694 struct aws_byte_cursor good_response_cursor = aws_byte_cursor_from_string(s_good_response);
1695 aws_array_list_push_back(&s_tester.response_data_callbacks[1], &good_response_cursor);
1696
1697 /* Enable endpoint verification */
1698 s_tester.verify_endpoint_host = true;
1699 aws_byte_buf_init(&s_tester.expected_host, allocator, 20);
1700 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_c_str("127.0.0.1");
1701 aws_byte_buf_append_dynamic(&s_tester.expected_host, &host_cursor);
1702 s_tester.expected_port = 8080;
1703
1704 struct aws_imds_client_options options = {
1705 .bootstrap = s_tester.bootstrap,
1706 .function_table = &s_mock_function_table,
1707 .imds_endpoint = aws_byte_cursor_from_c_str("http://127.0.0.1:8080"),
1708 .shutdown_options =
1709 {
1710 .shutdown_callback = s_on_shutdown_complete,
1711 .shutdown_user_data = NULL,
1712 },
1713 };
1714
1715 struct aws_imds_client *client = aws_imds_client_new(allocator, &options);
1716 ASSERT_NOT_NULL(client);
1717
1718 aws_imds_client_get_resource_async(
1719 client, aws_byte_cursor_from_string(s_expected_imds_resource_uri), s_get_resource_callback, NULL);
1720
1721 s_aws_wait_for_resource_result();
1722
1723 ASSERT_TRUE(s_validate_uri_path_and_resource(2, true /*got resource*/) == 0);
1724 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_byte_cursor_from_buf(&s_tester.resource), s_good_response);
1725
1726 aws_imds_client_release(client);
1727 s_aws_wait_for_imds_client_shutdown_callback();
1728 aws_mem_release(allocator, client);
1729
1730 ASSERT_SUCCESS(s_aws_imds_tester_cleanup());
1731
1732 return 0;
1733 }
1734
1735 AWS_TEST_CASE(imds_client_custom_endpoint, s_imds_client_custom_endpoint);