Debian Codegraph @

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

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/testing/aws_test_harness.h>
7
8 #include "shared_credentials_test_definitions.h"
9 #include <aws/auth/credentials.h>
10 #include <aws/auth/private/credentials_utils.h>
11 #include <aws/common/clock.h>
12 #include <aws/common/condition_variable.h>
13 #include <aws/common/date_time.h>
14 #include <aws/common/environment.h>
15 #include <aws/common/string.h>
16 #include <aws/io/logging.h>
17 #include <aws/sdkutils/aws_profile.h>
18
19 static struct aws_mock_process_tester {
20 struct aws_mutex lock;
21 struct aws_condition_variable signal;
22 struct aws_credentials *credentials;
23 bool has_received_credentials_callback;
24 bool has_received_shutdown_callback;
25 int error_code;
26 } s_tester;
27
28 static void s_on_shutdown_complete(void *user_data) {
29 (void)user_data;
30
31 aws_mutex_lock(&s_tester.lock);
32 s_tester.has_received_shutdown_callback = true;
33 aws_mutex_unlock(&s_tester.lock);
34
35 aws_condition_variable_notify_one(&s_tester.signal);
36 }
37
38 static bool s_has_tester_received_shutdown_callback(void *user_data) {
39 (void)user_data;
40
41 return s_tester.has_received_shutdown_callback;
42 }
43
44 static void s_aws_wait_for_provider_shutdown_callback(void) {
45 aws_mutex_lock(&s_tester.lock);
46 aws_condition_variable_wait_pred(&s_tester.signal, &s_tester.lock, s_has_tester_received_shutdown_callback, NULL);
47 aws_mutex_unlock(&s_tester.lock);
48 }
49
50 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_process_profile, "foo");
51
52 static int s_aws_process_test_init_config_profile(
53 struct aws_allocator *allocator,
54 const struct aws_string *config_contents) {
55
56 struct aws_string *config_file_path_str = aws_create_process_unique_file_name(allocator);
57 ASSERT_TRUE(config_file_path_str != NULL);
58 ASSERT_TRUE(aws_create_profile_file(config_file_path_str, config_contents) == AWS_OP_SUCCESS);
59
60 ASSERT_TRUE(
61 aws_set_environment_value(s_default_config_path_env_variable_name, config_file_path_str) == AWS_OP_SUCCESS);
62
63 ASSERT_TRUE(
64 aws_set_environment_value(s_default_profile_env_variable_name, s_credentials_process_profile) ==
65 AWS_OP_SUCCESS);
66
67 aws_string_destroy(config_file_path_str);
68 return AWS_OP_SUCCESS;
69 }
70
71 static int s_aws_process_tester_init(struct aws_allocator *allocator) {
72 aws_auth_library_init(allocator);
73
74 if (aws_mutex_init(&s_tester.lock)) {
75 return AWS_OP_ERR;
76 }
77
78 if (aws_condition_variable_init(&s_tester.signal)) {
79 return AWS_OP_ERR;
80 }
81
82 return AWS_OP_SUCCESS;
83 }
84
85 static void s_aws_process_tester_cleanup(void) {
86 aws_condition_variable_clean_up(&s_tester.signal);
87 aws_mutex_clean_up(&s_tester.lock);
88 aws_credentials_release(s_tester.credentials);
89 aws_auth_library_clean_up();
90 }
91
92 static bool s_has_tester_received_credentials_callback(void *user_data) {
93 (void)user_data;
94
95 return s_tester.has_received_credentials_callback;
96 }
97
98 static void s_aws_wait_for_credentials_result(void) {
99 aws_mutex_lock(&s_tester.lock);
100 aws_condition_variable_wait_pred(
101 &s_tester.signal, &s_tester.lock, s_has_tester_received_credentials_callback, NULL);
102 aws_mutex_unlock(&s_tester.lock);
103 }
104
105 static void s_get_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) {
106 (void)user_data;
107
108 aws_mutex_lock(&s_tester.lock);
109 s_tester.has_received_credentials_callback = true;
110 s_tester.credentials = credentials;
111 s_tester.error_code = error_code;
112 if (credentials != NULL) {
113 aws_credentials_acquire(credentials);
114 }
115 aws_condition_variable_notify_one(&s_tester.signal);
116 aws_mutex_unlock(&s_tester.lock);
117 }
118
119 #ifdef _WIN32
120 AWS_STATIC_STRING_FROM_LITERAL(
121 s_test_command,
122 "echo {\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
123 "\"SecretAccessKey\": \"SecretAccessKey321\", \"SessionToken\":\"TokenSuccess\", "
124 "\"Expiration\":\"2020-02-25T06:03:31Z\"}");
125 #else
126 AWS_STATIC_STRING_FROM_LITERAL(
127 s_test_command,
128 "echo '{\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
129 "\"SecretAccessKey\": \"SecretAccessKey321\", \"SessionToken\":\"TokenSuccess\", "
130 "\"Expiration\":\"2020-02-25T06:03:31Z\"}'");
131 #endif
132
133 #ifdef _WIN32
134 AWS_STATIC_STRING_FROM_LITERAL(
135 s_test_command_without_token,
136 "echo {\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
137 "\"SecretAccessKey\": \"SecretAccessKey321\", "
138 "\"Expiration\":\"2020-02-25T06:03:31Z\"}");
139 #else
140 AWS_STATIC_STRING_FROM_LITERAL(
141 s_test_command_without_token,
142 "echo '{\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
143 "\"SecretAccessKey\": \"SecretAccessKey321\", "
144 "\"Expiration\":\"2020-02-25T06:03:31Z\"}'");
145 #endif
146
147 #ifdef _WIN32
148 AWS_STATIC_STRING_FROM_LITERAL(
149 s_test_command_with_account_id,
150 "echo {\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
151 "\"SecretAccessKey\": \"SecretAccessKey321\", \"SessionToken\":\"TokenSuccess\", "
152 "\"Expiration\":\"2020-02-25T06:03:31Z\", \"AccountId\":\"AccountId123\"}");
153 #else
154 AWS_STATIC_STRING_FROM_LITERAL(
155 s_test_command_with_account_id,
156 "echo '{\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
157 "\"SecretAccessKey\": \"SecretAccessKey321\", \"SessionToken\":\"TokenSuccess\", "
158 "\"Expiration\":\"2020-02-25T06:03:31Z\", \"AccountId\":\"AccountId123\"}'");
159 #endif
160
161 #ifdef _WIN32
162 AWS_STATIC_STRING_FROM_LITERAL(
163 s_test_command_with_logging_on_stderr,
164 "("
165 "echo Logging on stderr >&2"
166 " && "
167 "echo {\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
168 "\"SecretAccessKey\": \"SecretAccessKey321\", \"SessionToken\":\"TokenSuccess\", "
169 "\"Expiration\":\"2020-02-25T06:03:31Z\"}"
170 ")");
171 #else
172 AWS_STATIC_STRING_FROM_LITERAL(
173 s_test_command_with_logging_on_stderr,
174 "("
175 "echo 'Logging on stderr' >&2"
176 " && "
177 "echo '{\"Version\": 1, \"AccessKeyId\": \"AccessKey123\", "
178 "\"SecretAccessKey\": \"SecretAccessKey321\", \"SessionToken\":\"TokenSuccess\", "
179 "\"Expiration\":\"2020-02-25T06:03:31Z\"}'"
180 ")");
181 #endif
182
183 AWS_STATIC_STRING_FROM_LITERAL(s_bad_test_command, "/i/dont/know/what/is/this/command");
184 AWS_STATIC_STRING_FROM_LITERAL(s_bad_command_output, "echo \"Hello, World!\"");
185
186 AWS_STATIC_STRING_FROM_LITERAL(s_good_access_key_id, "AccessKey123");
187 AWS_STATIC_STRING_FROM_LITERAL(s_good_secret_access_key, "SecretAccessKey321");
188 AWS_STATIC_STRING_FROM_LITERAL(s_good_session_token, "TokenSuccess");
189 AWS_STATIC_STRING_FROM_LITERAL(s_good_account_id, "AccountId123");
190
191 static uint64_t s_good_expiration = 1582610611;
192
193 AWS_STATIC_STRING_FROM_LITERAL(
194 s_process_config_file_contents,
195 "[profile default]\n"
196 "region=us-east-1\n"
197 "[profile foo]\n"
198 "region=us-west-2\n"
199 "credential_process=");
200
201 static int s_credentials_provider_process_helper(
202 struct aws_string *config_file_contents,
203 struct aws_allocator *allocator) {
204
205 s_aws_process_tester_init(allocator);
206
207 s_aws_process_test_init_config_profile(allocator, config_file_contents);
208
209 struct aws_credentials_provider_process_options options = {
210 .shutdown_options =
211 {
212 .shutdown_callback = s_on_shutdown_complete,
213 .shutdown_user_data = NULL,
214 },
215 .profile_to_use = aws_byte_cursor_from_string(s_credentials_process_profile),
216 };
217 struct aws_credentials_provider *provider = aws_credentials_provider_new_process(allocator, &options);
218
219 aws_credentials_provider_release(provider);
220 s_aws_wait_for_provider_shutdown_callback();
221 s_aws_process_tester_cleanup();
222 return 0;
223 }
224
225 static int s_credentials_provider_process_new_destroy_from_config(struct aws_allocator *allocator, void *ctx) {
226 (void)ctx;
227
228 struct aws_byte_buf content_buf;
229 struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
230 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
231 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(s_test_command);
232 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
233 cursor = aws_byte_cursor_from_c_str("\n");
234 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
235
236 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
237 ASSERT_TRUE(config_file_contents != NULL);
238 aws_byte_buf_clean_up(&content_buf);
239
240 ASSERT_SUCCESS(s_credentials_provider_process_helper(config_file_contents, allocator));
241 aws_string_destroy(config_file_contents);
242 return 0;
243 }
244 AWS_TEST_CASE(
245 credentials_provider_process_new_destroy_from_config,
246 s_credentials_provider_process_new_destroy_from_config);
247
248 static int s_credentials_provider_process_new_destroy_from_config_without_token(
249 struct aws_allocator *allocator,
250 void *ctx) {
251 (void)ctx;
252
253 struct aws_byte_buf content_buf;
254 struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
255 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
256 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(s_test_command_without_token);
257 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
258 cursor = aws_byte_cursor_from_c_str("\n");
259 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
260
261 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
262 ASSERT_TRUE(config_file_contents != NULL);
263 aws_byte_buf_clean_up(&content_buf);
264
265 ASSERT_SUCCESS(s_credentials_provider_process_helper(config_file_contents, allocator));
266 aws_string_destroy(config_file_contents);
267 return 0;
268 }
269 AWS_TEST_CASE(
270 credentials_provider_process_new_destroy_from_config_without_token,
271 s_credentials_provider_process_new_destroy_from_config_without_token);
272
273 AWS_STATIC_STRING_FROM_LITERAL(
274 s_process_config_file_no_process_contents,
275 "[profile default]\n"
276 "region=us-east-1\n"
277 "[profile foo]\n"
278 "region=us-west-2\n");
279
280 static int s_credentials_provider_process_new_failed(struct aws_allocator *allocator, void *ctx) {
281 (void)ctx;
282
283 s_aws_process_tester_init(allocator);
284
285 s_aws_process_test_init_config_profile(allocator, s_process_config_file_no_process_contents);
286
287 struct aws_credentials_provider_process_options options = {
288 .shutdown_options =
289 {
290 .shutdown_callback = s_on_shutdown_complete,
291 .shutdown_user_data = NULL,
292 },
293 .profile_to_use = aws_byte_cursor_from_string(s_credentials_process_profile),
294 };
295 struct aws_credentials_provider *provider = aws_credentials_provider_new_process(allocator, &options);
296 ASSERT_NULL(provider);
297
298 s_aws_process_tester_cleanup();
299 return 0;
300 }
301 AWS_TEST_CASE(credentials_provider_process_new_failed, s_credentials_provider_process_new_failed);
302
303 static int s_test_command_expect_failure(struct aws_allocator *allocator, const struct aws_string *command) {
304
305 s_aws_process_tester_init(allocator);
306
307 struct aws_byte_buf content_buf;
308 struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
309 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
310 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(command);
311 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
312 cursor = aws_byte_cursor_from_c_str("\n");
313 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
314
315 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
316 ASSERT_TRUE(config_file_contents != NULL);
317 aws_byte_buf_clean_up(&content_buf);
318
319 s_aws_process_test_init_config_profile(allocator, config_file_contents);
320 aws_string_destroy(config_file_contents);
321
322 struct aws_credentials_provider_process_options options = {
323 .shutdown_options =
324 {
325 .shutdown_callback = s_on_shutdown_complete,
326 .shutdown_user_data = NULL,
327 },
328 .profile_to_use = aws_byte_cursor_from_string(s_credentials_process_profile),
329 };
330 struct aws_credentials_provider *provider = aws_credentials_provider_new_process(allocator, &options);
331 ASSERT_NOT_NULL(provider);
332 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
333
334 s_aws_wait_for_credentials_result();
335
336 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
337 ASSERT_TRUE(s_tester.credentials == NULL);
338
339 aws_credentials_provider_release(provider);
340 s_aws_wait_for_provider_shutdown_callback();
341 s_aws_process_tester_cleanup();
342 return 0;
343 }
344
345 static int s_credentials_provider_process_bad_command(struct aws_allocator *allocator, void *ctx) {
346 (void)ctx;
347 return s_test_command_expect_failure(allocator, s_bad_test_command);
348 }
349 AWS_TEST_CASE(credentials_provider_process_bad_command, s_credentials_provider_process_bad_command);
350
351 static int s_credentials_provider_process_incorrect_command_output(struct aws_allocator *allocator, void *ctx) {
352 (void)ctx;
353 return s_test_command_expect_failure(allocator, s_bad_command_output);
354 }
355 AWS_TEST_CASE(
356 credentials_provider_process_incorrect_command_output,
357 s_credentials_provider_process_incorrect_command_output);
358
359 static int s_verify_credentials(struct aws_credentials *credentials, struct aws_credentials *expected_credentials) {
360 ASSERT_NOT_NULL(credentials);
361 ASSERT_NOT_NULL(expected_credentials);
362
363 ASSERT_BIN_ARRAYS_EQUALS(
364 aws_credentials_get_access_key_id(credentials).ptr,
365 aws_credentials_get_access_key_id(credentials).len,
366 aws_credentials_get_access_key_id(expected_credentials).ptr,
367 aws_credentials_get_access_key_id(expected_credentials).len);
368
369 ASSERT_BIN_ARRAYS_EQUALS(
370 aws_credentials_get_secret_access_key(credentials).ptr,
371 aws_credentials_get_secret_access_key(credentials).len,
372 aws_credentials_get_secret_access_key(expected_credentials).ptr,
373 aws_credentials_get_secret_access_key(expected_credentials).len);
374
375 ASSERT_BIN_ARRAYS_EQUALS(
376 aws_credentials_get_session_token(credentials).ptr,
377 aws_credentials_get_session_token(credentials).len,
378 aws_credentials_get_session_token(expected_credentials).ptr,
379 aws_credentials_get_session_token(expected_credentials).len);
380
381 ASSERT_BIN_ARRAYS_EQUALS(
382 aws_credentials_get_account_id(credentials).ptr,
383 aws_credentials_get_account_id(credentials).len,
384 aws_credentials_get_account_id(expected_credentials).ptr,
385 aws_credentials_get_account_id(expected_credentials).len);
386
387 ASSERT_UINT_EQUALS(
388 aws_credentials_get_expiration_timepoint_seconds(credentials),
389 aws_credentials_get_expiration_timepoint_seconds(expected_credentials));
390
391 return AWS_OP_SUCCESS;
392 }
393
394 static int s_test_command_expect_success(
395 struct aws_allocator *allocator,
396 const struct aws_string *command,
397 struct aws_credentials *expected_credentials) {
398 s_aws_process_tester_init(allocator);
399
400 struct aws_byte_buf content_buf;
401 struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
402 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
403 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(command);
404 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
405 cursor = aws_byte_cursor_from_c_str("\n");
406 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
407
408 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
409 ASSERT_TRUE(config_file_contents != NULL);
410 aws_byte_buf_clean_up(&content_buf);
411
412 s_aws_process_test_init_config_profile(allocator, config_file_contents);
413 aws_string_destroy(config_file_contents);
414
415 struct aws_credentials_provider_process_options options = {
416 .shutdown_options =
417 {
418 .shutdown_callback = s_on_shutdown_complete,
419 .shutdown_user_data = NULL,
420 },
421 .profile_to_use = aws_byte_cursor_from_string(s_credentials_process_profile),
422 };
423 struct aws_credentials_provider *provider = aws_credentials_provider_new_process(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_SUCCESS(s_verify_credentials(s_tester.credentials, expected_credentials));
431
432 aws_credentials_provider_release(provider);
433 s_aws_wait_for_provider_shutdown_callback();
434 s_aws_process_tester_cleanup();
435 return 0;
436 }
437
438 static int s_credentials_provider_process_basic_success(struct aws_allocator *allocator, void *ctx) {
439 (void)ctx;
440 struct aws_credentials *expected_credentials = aws_credentials_new_from_string(
441 allocator, s_good_access_key_id, s_good_secret_access_key, s_good_session_token, s_good_expiration);
442 ASSERT_SUCCESS(s_test_command_expect_success(allocator, s_test_command, expected_credentials));
443 aws_credentials_release(expected_credentials);
444 return AWS_OP_SUCCESS;
445 }
446 AWS_TEST_CASE(credentials_provider_process_basic_success, s_credentials_provider_process_basic_success);
447
448 static int s_credentials_provider_process_basic_success_without_session_token(
449 struct aws_allocator *allocator,
450 void *ctx) {
451 (void)ctx;
452 struct aws_credentials *expected_credentials = aws_credentials_new_from_string(
453 allocator, s_good_access_key_id, s_good_secret_access_key, NULL, s_good_expiration);
454 ASSERT_SUCCESS(s_test_command_expect_success(allocator, s_test_command_without_token, expected_credentials));
455 aws_credentials_release(expected_credentials);
456 return AWS_OP_SUCCESS;
457 }
458 AWS_TEST_CASE(
459 credentials_provider_process_basic_success_without_session_token,
460 s_credentials_provider_process_basic_success_without_session_token);
461
462 static int s_credentials_provider_process_basic_success_with_account_id(struct aws_allocator *allocator, void *ctx) {
463 (void)ctx;
464 struct aws_credentials_options creds_option = {
465 .access_key_id_cursor = aws_byte_cursor_from_string(s_good_access_key_id),
466 .secret_access_key_cursor = aws_byte_cursor_from_string(s_good_secret_access_key),
467 .session_token_cursor = aws_byte_cursor_from_string(s_good_session_token),
468 .account_id_cursor = aws_byte_cursor_from_string(s_good_account_id),
469 .expiration_timepoint_seconds = s_good_expiration,
470 };
471 struct aws_credentials *expected_credentials = aws_credentials_new_with_options(allocator, &creds_option);
472 ASSERT_SUCCESS(s_test_command_expect_success(allocator, s_test_command_with_account_id, expected_credentials));
473 aws_credentials_release(expected_credentials);
474 return AWS_OP_SUCCESS;
475 }
476 AWS_TEST_CASE(
477 credentials_provider_process_basic_success_with_account_id,
478 s_credentials_provider_process_basic_success_with_account_id);
479
480 /* Test that stderr is ignored, if the process otherwise succeeds with exit code 0 and valid JSON to stdout.
481 * Once upon a time stderr and stdout were merged, and mundane logging to stderr would break things. */
482 static int s_credentials_provider_process_success_ignores_stderr(struct aws_allocator *allocator, void *ctx) {
483 (void)ctx;
484 struct aws_credentials *expected_credentials = aws_credentials_new_from_string(
485 allocator, s_good_access_key_id, s_good_secret_access_key, s_good_session_token, s_good_expiration);
486 ASSERT_SUCCESS(
487 s_test_command_expect_success(allocator, s_test_command_with_logging_on_stderr, expected_credentials));
488 aws_credentials_release(expected_credentials);
489 return AWS_OP_SUCCESS;
490 }
491 AWS_TEST_CASE(
492 credentials_provider_process_success_ignores_stderr,
493 s_credentials_provider_process_success_ignores_stderr);
494
495 static int s_credentials_provider_process_basic_success_from_profile_provider(
496 struct aws_allocator *allocator,
497 void *ctx) {
498 (void)ctx;
499
500 s_aws_process_tester_init(allocator);
501
502 struct aws_byte_buf content_buf;
503 struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
504 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
505 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(s_test_command);
506 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
507 cursor = aws_byte_cursor_from_c_str("\n");
508 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
509
510 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
511 ASSERT_TRUE(config_file_contents != NULL);
512 aws_byte_buf_clean_up(&content_buf);
513
514 s_aws_process_test_init_config_profile(allocator, config_file_contents);
515 aws_string_destroy(config_file_contents);
516
517 struct aws_credentials_provider_profile_options options = {
518 .shutdown_options =
519 {
520 .shutdown_callback = s_on_shutdown_complete,
521 .shutdown_user_data = NULL,
522 },
523 .profile_name_override = aws_byte_cursor_from_string(s_credentials_process_profile),
524 };
525 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
526
527 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
528
529 s_aws_wait_for_credentials_result();
530
531 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
532 struct aws_credentials *expected_credentials = aws_credentials_new_from_string(
533 allocator, s_good_access_key_id, s_good_secret_access_key, s_good_session_token, s_good_expiration);
534 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials, expected_credentials));
535 aws_credentials_release(expected_credentials);
536
537 aws_credentials_provider_release(provider);
538 s_aws_wait_for_provider_shutdown_callback();
539 s_aws_process_tester_cleanup();
540 return 0;
541 }
542 AWS_TEST_CASE(
543 credentials_provider_process_basic_success_from_profile_provider,
544 s_credentials_provider_process_basic_success_from_profile_provider);
545
546 static int s_credentials_provider_process_basic_success_cached(struct aws_allocator *allocator, void *ctx) {
547 (void)ctx;
548
549 s_aws_process_tester_init(allocator);
550
551 struct aws_byte_buf content_buf;
552 struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
553 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
554 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(s_test_command);
555 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
556 cursor = aws_byte_cursor_from_c_str("\n");
557 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
558
559 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
560 ASSERT_TRUE(config_file_contents != NULL);
561 aws_byte_buf_clean_up(&content_buf);
562
563 s_aws_process_test_init_config_profile(allocator, config_file_contents);
564 aws_string_destroy(config_file_contents);
565
566 struct aws_profile_collection *profile_collection = NULL;
567 struct aws_string *config_file_path;
568 aws_get_environment_value(allocator, s_default_config_path_env_variable_name, &config_file_path);
569 profile_collection = aws_profile_collection_new_from_file(allocator, config_file_path, AWS_PST_CONFIG);
570
571 /* Update profile and config file */
572 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
573 cursor = aws_byte_cursor_from_string(s_bad_test_command);
574 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
575 cursor = aws_byte_cursor_from_c_str("\n");
576 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
577
578 config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
579 ASSERT_TRUE(config_file_contents != NULL);
580
581 if (aws_create_profile_file(config_file_path, config_file_contents)) {
582 return AWS_OP_ERR;
583 }
584 aws_string_destroy(config_file_contents);
585 aws_byte_buf_clean_up(&content_buf);
586
587 /* provider should used the cached credentials */
588 struct aws_credentials_provider_process_options options = {
589 .shutdown_options =
590 {
591 .shutdown_callback = s_on_shutdown_complete,
592 .shutdown_user_data = NULL,
593 },
594 .profile_to_use = aws_byte_cursor_from_string(s_credentials_process_profile),
595 .config_profile_collection_cached = profile_collection,
596 };
597
598 struct aws_credentials_provider *provider = aws_credentials_provider_new_process(allocator, &options);
599
600 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
601
602 s_aws_wait_for_credentials_result();
603
604 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
605 struct aws_credentials *expected_credentials = aws_credentials_new_from_string(
606 allocator, s_good_access_key_id, s_good_secret_access_key, s_good_session_token, s_good_expiration);
607 ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials, expected_credentials));
608 aws_credentials_release(expected_credentials);
609
610 aws_string_destroy(config_file_path);
611 aws_profile_collection_release(profile_collection);
612 aws_credentials_provider_release(provider);
613 s_aws_wait_for_provider_shutdown_callback();
614 s_aws_process_tester_cleanup();
615 return 0;
616 }
617 AWS_TEST_CASE(credentials_provider_process_basic_success_cached, s_credentials_provider_process_basic_success_cached);
618
619 AWS_STATIC_STRING_FROM_LITERAL(s_profile_access_key_id, "ProfileAccessKey");
620 AWS_STATIC_STRING_FROM_LITERAL(s_profile_secret_access_key, "ProfileSecretKey");
621
622 AWS_STATIC_STRING_FROM_LITERAL(
623 s_config_with_both_profile_and_process_creds,
624 "[profile default]\n"
625 "region=us-east-1\n"
626 "aws_access_key_id=ProfileAccessKey\n"
627 "aws_secret_access_key=ProfileSecretKey\n"
628 "credential_process=");
629
630 static int s_credentials_provider_profile_takes_priority_over_process(struct aws_allocator *allocator, void *ctx) {
631 (void)ctx;
632
633 s_aws_process_tester_init(allocator);
634
635 struct aws_byte_buf content_buf;
636 struct aws_byte_buf existing_content =
637 aws_byte_buf_from_c_str(aws_string_c_str(s_config_with_both_profile_and_process_creds));
638 aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
639 struct aws_byte_cursor cursor = aws_byte_cursor_from_string(s_test_command);
640 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
641 cursor = aws_byte_cursor_from_c_str("\n");
642 ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
643
644 struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
645 ASSERT_TRUE(config_file_contents != NULL);
646 aws_byte_buf_clean_up(&content_buf);
647
648 s_aws_process_test_init_config_profile(allocator, config_file_contents);
649 aws_string_destroy(config_file_contents);
650
651 aws_unset_environment_value(s_default_profile_env_variable_name);
652
653 struct aws_string *empty_creds = aws_create_process_unique_file_name(allocator);
654 struct aws_string *empty = aws_string_new_from_c_str(allocator, "");
655 aws_create_profile_file(empty_creds, empty);
656 aws_set_environment_value(s_default_credentials_path_env_variable_name, empty_creds);
657 aws_string_destroy(empty_creds);
658 aws_string_destroy(empty);
659
660 struct aws_credentials_provider_profile_options options = {
661 .shutdown_options =
662 {
663 .shutdown_callback = s_on_shutdown_complete,
664 .shutdown_user_data = NULL,
665 },
666 };
667 struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);
668
669 aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);
670
671 s_aws_wait_for_credentials_result();
672
673 ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
674 ASSERT_TRUE(s_tester.credentials != NULL);
675 ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_access_key_id(s_tester.credentials), s_profile_access_key_id);
676 ASSERT_CURSOR_VALUE_STRING_EQUALS(
677 aws_credentials_get_secret_access_key(s_tester.credentials), s_profile_secret_access_key);
678
679 aws_credentials_provider_release(provider);
680 s_aws_wait_for_provider_shutdown_callback();
681 s_aws_process_tester_cleanup();
682
683 return 0;
684 }
685
686 AWS_TEST_CASE(
687 credentials_provider_profile_takes_priority_over_process,
688 s_credentials_provider_profile_takes_priority_over_process);