Debian Codegraph @

crt/aws-c-auth/source/credentials_provider_profile.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/auth/credentials.h>
7
8 #include <aws/auth/private/aws_profile.h>
9 #include <aws/auth/private/credentials_utils.h>
10 #include <aws/common/hash_table.h>
11 #include <aws/common/process.h>
12 #include <aws/common/string.h>
13 #include <aws/io/tls_channel_handler.h>
14
15 #ifdef _MSC_VER
16 /* allow non-constant declared initializers. */
17 # pragma warning(disable : 4204)
18 #endif
19
20 /*
21 * Profile provider implementation
22 */
23
24 AWS_STRING_FROM_LITERAL(s_role_arn_name, "role_arn");
25 AWS_STRING_FROM_LITERAL(s_role_session_name_name, "role_session_name");
26 AWS_STRING_FROM_LITERAL(s_external_id_name, "external_id");
27 AWS_STRING_FROM_LITERAL(s_credential_source_name, "credential_source");
28 AWS_STRING_FROM_LITERAL(s_source_profile_name, "source_profile");
29 AWS_STRING_FROM_LITERAL(s_access_key_id_profile_var, "aws_access_key_id");
30 AWS_STRING_FROM_LITERAL(s_secret_access_key_profile_var, "aws_secret_access_key");
31 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_process, "credential_process");
32 AWS_STATIC_STRING_FROM_LITERAL(s_web_identity_token_file_name, "web_identity_token_file");
33
34 static struct aws_byte_cursor s_default_session_name_pfx =
35 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("aws-common-runtime-profile-config");
36 static struct aws_byte_cursor s_ec2_imds_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Ec2InstanceMetadata");
37 static struct aws_byte_cursor s_environment_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Environment");
38 static struct aws_byte_cursor s_ecs_credentials_provider_name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("EcsContainer");
39
40 #define MAX_SESSION_NAME_LEN ((size_t)64)
41
42 struct aws_credentials_provider_profile_file_impl {
43 struct aws_string *config_file_path;
44 struct aws_string *credentials_file_path;
45 struct aws_string *profile_name;
46 struct aws_profile_collection *profile_collection_cached;
47 };
48
49 static int s_profile_file_credentials_provider_get_credentials_async(
50 struct aws_credentials_provider *provider,
51 aws_on_get_credentials_callback_fn callback,
52 void *user_data) {
53
54 struct aws_credentials_provider_profile_file_impl *impl = provider->impl;
55 struct aws_credentials *credentials = NULL;
56 struct aws_profile_collection *merged_profiles = NULL;
57
58 if (impl->profile_collection_cached) {
59 /* Use cached profile collection */
60 merged_profiles = aws_profile_collection_acquire(impl->profile_collection_cached);
61 } else {
62 /*
63 * Parse config file from file, if it exists
64 */
65 struct aws_profile_collection *config_profiles =
66 aws_profile_collection_new_from_file(provider->allocator, impl->config_file_path, AWS_PST_CONFIG);
67
68 if (config_profiles != NULL) {
69 AWS_LOGF_DEBUG(
70 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
71 "(id=%p) Profile credentials provider successfully built config profile collection from file at (%s)",
72 (void *)provider,
73 aws_string_c_str(impl->config_file_path));
74 } else {
75 AWS_LOGF_DEBUG(
76 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
77 "(id=%p) Profile credentials provider failed to build config profile collection from file at (%s)",
78 (void *)provider,
79 aws_string_c_str(impl->config_file_path));
80 }
81
82 /*
83 * Parse credentials file, if it exists
84 */
85 struct aws_profile_collection *credentials_profiles =
86 aws_profile_collection_new_from_file(provider->allocator, impl->credentials_file_path, AWS_PST_CREDENTIALS);
87
88 if (credentials_profiles != NULL) {
89 AWS_LOGF_DEBUG(
90 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
91 "(id=%p) Profile credentials provider successfully built credentials profile collection from file at "
92 "(%s)",
93 (void *)provider,
94 aws_string_c_str(impl->credentials_file_path));
95 } else {
96 AWS_LOGF_DEBUG(
97 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
98 "(id=%p) Profile credentials provider failed to build credentials profile collection from file at (%s)",
99 (void *)provider,
100 aws_string_c_str(impl->credentials_file_path));
101 }
102
103 /*
104 * Merge the (up to) two sources into a single unified profile
105 */
106 merged_profiles =
107 aws_profile_collection_new_from_merge(provider->allocator, config_profiles, credentials_profiles);
108
109 aws_profile_collection_release(config_profiles);
110 aws_profile_collection_release(credentials_profiles);
111 }
112
113 if (merged_profiles != NULL) {
114 const struct aws_profile *profile = aws_profile_collection_get_profile(merged_profiles, impl->profile_name);
115 if (profile != NULL) {
116 AWS_LOGF_INFO(
117 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
118 "(id=%p) Profile credentials provider attempting to pull credentials from profile \"%s\"",
119 (void *)provider,
120 aws_string_c_str(impl->profile_name));
121 credentials = aws_credentials_new_from_profile(provider->allocator, profile);
122 } else {
123 AWS_LOGF_INFO(
124 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
125 "(id=%p) Profile credentials provider could not find a profile named \"%s\"",
126 (void *)provider,
127 aws_string_c_str(impl->profile_name));
128 }
129 } else {
130 AWS_LOGF_ERROR(
131 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
132 "(id=%p) Profile credentials provider failed to merge config and credentials profile collections",
133 (void *)provider);
134 }
135
136 int error_code = AWS_ERROR_SUCCESS;
137 if (credentials == NULL) {
138 error_code = AWS_AUTH_CREDENTIALS_PROVIDER_PROFILE_SOURCE_FAILURE;
139 }
140
141 if (error_code == AWS_ERROR_SUCCESS) {
142 AWS_LOGF_INFO(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Loaded credentials from profile provider");
143 } else {
144 AWS_LOGF_INFO(
145 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
146 "Failed to load credentials from profile provider: %s",
147 aws_error_str(error_code));
148 }
149
150 callback(credentials, error_code, user_data);
151
152 /*
153 * clean up
154 */
155 aws_credentials_release(credentials);
156 aws_profile_collection_release(merged_profiles);
157
158 return AWS_OP_SUCCESS;
159 }
160
161 static void s_profile_file_credentials_provider_destroy(struct aws_credentials_provider *provider) {
162 struct aws_credentials_provider_profile_file_impl *impl = provider->impl;
163 if (impl == NULL) {
164 return;
165 }
166
167 aws_string_destroy(impl->config_file_path);
168 aws_string_destroy(impl->credentials_file_path);
169 aws_string_destroy(impl->profile_name);
170 aws_profile_collection_release(impl->profile_collection_cached);
171 aws_credentials_provider_invoke_shutdown_callback(provider);
172
173 aws_mem_release(provider->allocator, provider);
174 }
175
176 static struct aws_credentials_provider_vtable s_aws_credentials_provider_profile_file_vtable = {
177 .get_credentials = s_profile_file_credentials_provider_get_credentials_async,
178 .destroy = s_profile_file_credentials_provider_destroy,
179 };
180
181 /* load a purely config/credentials file based provider. */
182 static struct aws_credentials_provider *s_create_profile_based_provider(
183 struct aws_allocator *allocator,
184 struct aws_string *credentials_file_path,
185 struct aws_string *config_file_path,
186 const struct aws_string *profile_name,
187 struct aws_profile_collection *profile_collection_cached) {
188 struct aws_credentials_provider *provider = NULL;
189 struct aws_credentials_provider_profile_file_impl *impl = NULL;
190
191 AWS_LOGF_INFO(
192 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
193 "static: profile %s attempting to create profile-based credentials provider",
194 aws_string_c_str(profile_name));
195
196 aws_mem_acquire_many(
197 allocator,
198 2,
199 &provider,
200 sizeof(struct aws_credentials_provider),
201 &impl,
202 sizeof(struct aws_credentials_provider_profile_file_impl));
203
204 if (!provider) {
205 return NULL;
206 }
207
208 AWS_ZERO_STRUCT(*provider);
209 AWS_ZERO_STRUCT(*impl);
210
211 aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_profile_file_vtable, impl);
212 if (credentials_file_path) {
213 impl->credentials_file_path = aws_string_clone_or_reuse(allocator, credentials_file_path);
214 }
215 if (config_file_path) {
216 impl->config_file_path = aws_string_clone_or_reuse(allocator, config_file_path);
217 }
218 impl->profile_name = aws_string_clone_or_reuse(allocator, profile_name);
219 impl->profile_collection_cached = aws_profile_collection_acquire(profile_collection_cached);
220 return provider;
221 }
222
223 static struct aws_credentials_provider *s_create_process_based_provider(
224 struct aws_allocator *allocator,
225 const struct aws_string *profile_name,
226 struct aws_profile_collection *profile_collection) {
227 AWS_LOGF_INFO(
228 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
229 "static: profile %s attempting to create process-based credentials provider",
230 aws_string_c_str(profile_name));
231
232 struct aws_credentials_provider_process_options options = {
233 .profile_to_use = aws_byte_cursor_from_string(profile_name),
234 .config_profile_collection_cached = profile_collection,
235 };
236 return aws_credentials_provider_new_process(allocator, &options);
237 }
238
239 static struct aws_credentials_provider *s_credentials_provider_new_profile_internal(
240 struct aws_allocator *allocator,
241 const struct aws_credentials_provider_profile_options *options,
242 struct aws_hash_table *source_profiles_table);
243
244 /*
245 * Create a credentials provider for a profile that has role_arn set.
246 *
247 * The credential source for the assume-role operation is resolved in the following
248 * priority order:
249 *
250 * 2. Profile: assume role with source_profile
251 * 3. Profile: assume role with credential_source (Ec2InstanceMetadata, Environment, EcsContainer)
252 * 4. Profile: web identity token (web_identity_token_file)
253 *
254 * If none of these are present, the function returns NULL.
255 */
256 static struct aws_credentials_provider *s_create_sts_based_provider(
257 struct aws_allocator *allocator,
258 const struct aws_profile_property *role_arn_property,
259 const struct aws_profile *profile,
260 const struct aws_credentials_provider_profile_options *options,
261 struct aws_profile_collection *merged_profiles,
262 struct aws_hash_table *source_profiles_table) {
263 struct aws_credentials_provider *provider = NULL;
264
265 /* role_arn */
266 AWS_LOGF_INFO(
267 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
268 "static: profile %s has role_arn property is set to %s, attempting to "
269 "create an STS credentials provider.",
270 aws_string_c_str(aws_profile_get_name(profile)),
271 aws_string_c_str(aws_profile_property_get_value(role_arn_property)));
272
273 const struct aws_profile_property *source_profile_property =
274 aws_profile_get_property(profile, s_source_profile_name);
275 const struct aws_profile_property *credential_source_property =
276 aws_profile_get_property(profile, s_credential_source_name);
277 const struct aws_profile_property *web_identity_token_file_property =
278 aws_profile_get_property(profile, s_web_identity_token_file_name);
279
280 /* role_session_name */
281 const struct aws_profile_property *role_session_name = aws_profile_get_property(profile, s_role_session_name_name);
282 char session_name_array[MAX_SESSION_NAME_LEN + 1];
283 AWS_ZERO_ARRAY(session_name_array);
284
285 if (role_session_name) {
286 size_t to_write = aws_profile_property_get_value(role_session_name)->len;
287 if (to_write > MAX_SESSION_NAME_LEN) {
288 AWS_LOGF_WARN(
289 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
290 "static: session_name property is %d bytes long, "
291 "but the max is %d. Truncating",
292 (int)aws_profile_property_get_value(role_session_name)->len,
293 (int)MAX_SESSION_NAME_LEN);
294 to_write = MAX_SESSION_NAME_LEN;
295 }
296 memcpy(session_name_array, aws_string_bytes(aws_profile_property_get_value(role_session_name)), to_write);
297 } else {
298 memcpy(session_name_array, s_default_session_name_pfx.ptr, s_default_session_name_pfx.len);
299 snprintf(
300 session_name_array + s_default_session_name_pfx.len,
301 sizeof(session_name_array) - s_default_session_name_pfx.len,
302 "-%d",
303 aws_get_pid());
304 }
305
306 AWS_LOGF_DEBUG(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "static: computed session_name as %s", session_name_array);
307
308 /* external_id */
309 struct aws_byte_cursor external_id = {0};
310 const struct aws_profile_property *external_id_property = aws_profile_get_property(profile, s_external_id_name);
311 if (external_id_property) {
312 external_id = aws_byte_cursor_from_string(aws_profile_property_get_value(external_id_property));
313 }
314
315 /* Automatically create a TLS context if necessary. We'd prefer that users pass one in, but can't force
316 * them to because aws_credentials_provider_profile_options didn't always have a tls_ctx member. */
317 struct aws_tls_ctx *tls_ctx = NULL;
318 if (options->tls_ctx) {
319 tls_ctx = aws_tls_ctx_acquire(options->tls_ctx);
320 } else {
321 #ifdef BYO_CRYPTO
322 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "a TLS context must be provided to query STS");
323 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
324 goto done;
325 #else
326 AWS_LOGF_INFO(
327 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "TLS context not provided, initializing a new one for querying STS");
328 struct aws_tls_ctx_options tls_options;
329 aws_tls_ctx_options_init_default_client(&tls_options, allocator);
330 tls_ctx = aws_tls_client_ctx_new(allocator, &tls_options);
331 aws_tls_ctx_options_clean_up(&tls_options);
332 if (!tls_ctx) {
333 goto done;
334 }
335 #endif
336 }
337
338 struct aws_credentials_provider_sts_options sts_options = {
339 .bootstrap = options->bootstrap,
340 .tls_ctx = tls_ctx,
341 .role_arn = aws_byte_cursor_from_string(aws_profile_property_get_value(role_arn_property)),
342 .external_id = external_id,
343 .session_name = aws_byte_cursor_from_c_str(session_name_array),
344 .profile_name_override = options->profile_name_override,
345 .config_file_name_override = options->config_file_name_override,
346 .profile_collection_cached = options->profile_collection_cached,
347 .duration_seconds = 0,
348 .function_table = options->function_table,
349 .proxy_ev_settings = options->proxy_ev_settings,
350 };
351
352 if (source_profile_property) {
353
354 AWS_LOGF_DEBUG(
355 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
356 "static: source_profile set to %s",
357 aws_string_c_str(aws_profile_property_get_value(source_profile_property)));
358
359 struct aws_credentials_provider_profile_options profile_provider_options = {
360 .bootstrap = options->bootstrap,
361 .profile_name_override =
362 aws_byte_cursor_from_string(aws_profile_property_get_value(source_profile_property)),
363 /* reuse profile collection instead of reading it again */
364 .profile_collection_cached = merged_profiles,
365 .tls_ctx = options->tls_ctx,
366 .function_table = options->function_table,
367 .proxy_ev_settings = options->proxy_ev_settings,
368 };
369 sts_options.creds_provider =
370 s_credentials_provider_new_profile_internal(allocator, &profile_provider_options, source_profiles_table);
371
372 if (!sts_options.creds_provider) {
373 goto done;
374 }
375
376 provider = aws_credentials_provider_new_sts(allocator, &sts_options);
377
378 aws_credentials_provider_release(sts_options.creds_provider);
379
380 if (!provider) {
381 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "static: failed to load STS credentials provider");
382 }
383 } else if (credential_source_property) {
384 AWS_LOGF_INFO(
385 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
386 "static: credential_source property set to %s",
387 aws_string_c_str(aws_profile_property_get_value(credential_source_property)));
388
389 if (aws_string_eq_byte_cursor_ignore_case(
390 aws_profile_property_get_value(credential_source_property), &s_ec2_imds_name)) {
391 struct aws_credentials_provider_imds_options imds_options = {
392 .bootstrap = options->bootstrap,
393 .function_table = options->function_table,
394 .proxy_ev_settings = options->proxy_ev_settings,
395 };
396
397 struct aws_credentials_provider *imds_provider =
398 aws_credentials_provider_new_imds(allocator, &imds_options);
399
400 if (!imds_provider) {
401 goto done;
402 }
403
404 sts_options.creds_provider = imds_provider;
405 provider = aws_credentials_provider_new_sts(allocator, &sts_options);
406
407 aws_credentials_provider_release(imds_provider);
408
409 } else if (aws_string_eq_byte_cursor_ignore_case(
410 aws_profile_property_get_value(credential_source_property), &s_environment_name)) {
411 struct aws_credentials_provider_environment_options env_options;
412 AWS_ZERO_STRUCT(env_options);
413
414 struct aws_credentials_provider *env_provider =
415 aws_credentials_provider_new_environment(allocator, &env_options);
416
417 if (!env_provider) {
418 goto done;
419 }
420
421 sts_options.creds_provider = env_provider;
422 provider = aws_credentials_provider_new_sts(allocator, &sts_options);
423
424 aws_credentials_provider_release(env_provider);
425 } else if (aws_string_eq_byte_cursor_ignore_case(
426 aws_profile_property_get_value(credential_source_property), &s_ecs_credentials_provider_name)) {
427 struct aws_credentials_provider_ecs_environment_options ecs_options = {
428 .bootstrap = options->bootstrap,
429 .function_table = options->function_table,
430 .tls_ctx = tls_ctx,
431 .proxy_ev_settings = options->proxy_ev_settings,
432 };
433 struct aws_credentials_provider *ecs_provider =
434 aws_credentials_provider_new_ecs_from_environment(allocator, &ecs_options);
435 if (!ecs_provider) {
436 goto done;
437 }
438 sts_options.creds_provider = ecs_provider;
439 provider = aws_credentials_provider_new_sts(allocator, &sts_options);
440
441 aws_credentials_provider_release(ecs_provider);
442 } else {
443 AWS_LOGF_ERROR(
444 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
445 "static: invalid credential_source property: %s",
446 aws_string_c_str(aws_profile_property_get_value(credential_source_property)));
447 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
448 }
449 } else if (web_identity_token_file_property) {
450 /*
451 * Neither source_profile nor credential_source is set.
452 * Check if web_identity_token_file is available as the credential source.
453 */
454 AWS_LOGF_INFO(
455 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
456 "static: profile %s has role_arn and web_identity_token_file, attempting to create an STS web identity "
457 "credentials provider.",
458 aws_string_c_str(aws_profile_get_name(profile)));
459
460 struct aws_credentials_provider_sts_web_identity_options web_identity_options = {
461 .bootstrap = options->bootstrap,
462 .tls_ctx = tls_ctx,
463 .function_table = options->function_table,
464 .proxy_ev_settings = options->proxy_ev_settings,
465 .config_profile_collection_cached = merged_profiles,
466 .profile_name_override = aws_byte_cursor_from_string(aws_profile_get_name(profile)),
467 .shutdown_options = options->shutdown_options,
468 };
469 provider = aws_credentials_provider_new_sts_web_identity(allocator, &web_identity_options);
470 }
471 done:
472 aws_tls_ctx_release(tls_ctx);
473 return provider;
474 }
475
476 /*
477 * Resolve credentials from a profile. The credential source is determined by the following
478 * priority order:
479 *
480 * 1. Profile: static credentials (aws_access_key_id / aws_secret_access_key)
481 * 2. Profile: assume role with source_profile
482 * 3. Profile: assume role with credential_source
483 * 4. Profile: web identity token (web_identity_token_file)
484 * 5. Profile: SSO (not yet supported from profile provider)
485 * 6. Profile: legacy SSO (not yet supported from profile provider)
486 * 7. Profile: login (not yet supported from profile provider)
487 * 8. Profile: process (credential_process)
488 */
489 static struct aws_credentials_provider *s_credentials_provider_new_profile_internal(
490 struct aws_allocator *allocator,
491 const struct aws_credentials_provider_profile_options *options,
492 struct aws_hash_table *source_profiles_table) {
493
494 struct aws_credentials_provider *provider = NULL;
495 struct aws_profile_collection *config_profiles = NULL;
496 struct aws_profile_collection *credentials_profiles = NULL;
497 struct aws_profile_collection *merged_profiles = NULL;
498 struct aws_string *credentials_file_path = NULL;
499 struct aws_string *config_file_path = NULL;
500 struct aws_string *profile_name = NULL;
501 bool first_profile_in_chain = false;
502 if (source_profiles_table == NULL) {
503 source_profiles_table = aws_mem_calloc(allocator, 1, sizeof(struct aws_hash_table));
504 first_profile_in_chain = true;
505 /* source_profiles_table is an hashtable of (char *) -> NULL to detect recursion loop */
506 if (aws_hash_table_init(
507 source_profiles_table, allocator, 3, aws_hash_c_string, aws_hash_callback_c_str_eq, NULL, NULL)) {
508 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "hash_table_init failed");
509 goto on_finished;
510 }
511 }
512
513 profile_name = aws_get_profile_name(allocator, &options->profile_name_override);
514 if (!profile_name) {
515 AWS_LOGF_ERROR(
516 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "static: Profile credentials parser failed to resolve profile name");
517 goto on_finished;
518 }
519
520 if (options->profile_collection_cached) {
521 /* Use cached profile collection */
522 merged_profiles = aws_profile_collection_acquire(options->profile_collection_cached);
523 } else {
524 /* Load profile collection from files */
525
526 credentials_file_path = aws_get_credentials_file_path(allocator, &options->credentials_file_name_override);
527 if (!credentials_file_path) {
528 AWS_LOGF_ERROR(
529 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
530 "static: Profile credentials parser failed resolve credentials file path");
531 goto on_finished;
532 }
533
534 config_file_path = aws_get_config_file_path(allocator, &options->config_file_name_override);
535 if (!config_file_path) {
536 AWS_LOGF_ERROR(
537 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "static: Profile credentials parser failed resolve config file path");
538 goto on_finished;
539 }
540
541 config_profiles = aws_profile_collection_new_from_file(allocator, config_file_path, AWS_PST_CONFIG);
542 credentials_profiles =
543 aws_profile_collection_new_from_file(allocator, credentials_file_path, AWS_PST_CREDENTIALS);
544
545 if (!(config_profiles || credentials_profiles)) {
546 AWS_LOGF_ERROR(
547 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
548 "static: Profile credentials parser could not load or parse"
549 " a credentials or config file.");
550 goto on_finished;
551 }
552
553 merged_profiles = aws_profile_collection_new_from_merge(allocator, config_profiles, credentials_profiles);
554 }
555 const struct aws_profile *profile = aws_profile_collection_get_profile(merged_profiles, profile_name);
556
557 if (!profile) {
558 AWS_LOGF_ERROR(
559 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
560 "static: Profile credentials provider could not load"
561 " a profile at %s.",
562 aws_string_c_str(profile_name));
563 goto on_finished;
564 }
565 /* check if sts provider is applicable */
566 const struct aws_profile_property *role_arn_property = aws_profile_get_property(profile, s_role_arn_name);
567 bool profile_contains_access_key = aws_profile_get_property(profile, s_access_key_id_profile_var) != NULL;
568 bool profile_contains_secret_access_key =
569 aws_profile_get_property(profile, s_secret_access_key_profile_var) != NULL;
570 bool profile_contains_credentials = profile_contains_access_key || profile_contains_secret_access_key;
571
572 struct aws_hash_element *element = NULL;
573 if (aws_hash_table_find(source_profiles_table, (void *)aws_string_c_str(profile_name), &element) == AWS_OP_ERR) {
574 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "hash_table_find failed");
575 goto on_finished;
576 }
577 if (element != NULL) {
578 /* self-reference chain of length 1 is allowed with static credentials */
579 if (aws_hash_table_get_entry_count(source_profiles_table) > 1 || !profile_contains_credentials) {
580 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "assumeRole chain contains a circular reference");
581 aws_raise_error(AWS_AUTH_PROFILE_STS_CREDENTIALS_PROVIDER_CYCLE_FAILURE);
582 goto on_finished;
583 }
584 }
585
586 aws_hash_table_put(source_profiles_table, (void *)aws_string_c_str(profile_name), NULL, 0);
587
588 /* check if process provider is applicable */
589 const struct aws_profile_property *process_property = aws_profile_get_property(profile, s_credentials_process);
590
591 if (role_arn_property && (first_profile_in_chain || !profile_contains_credentials)) {
592 provider = s_create_sts_based_provider(
593 allocator, role_arn_property, profile, options, merged_profiles, source_profiles_table);
594 } else if (process_property && !profile_contains_credentials) {
595 /* TODO: SSO (priority 9), legacy SSO (priority 10), and login (priority 11) credentials providers
596 * are not yet supported within the profile credentials provider. They should be resolved here
597 * before falling through to credential_process (priority 12) per the credentials-provider-chain SEP. */
598 provider = s_create_process_based_provider(allocator, profile_name, merged_profiles);
599 } else {
600 provider = s_create_profile_based_provider(
601 allocator, credentials_file_path, config_file_path, profile_name, options->profile_collection_cached);
602 }
603
604 on_finished:
605 aws_profile_collection_release(config_profiles);
606 aws_profile_collection_release(credentials_profiles);
607 aws_profile_collection_release(merged_profiles);
608
609 aws_string_destroy(credentials_file_path);
610 aws_string_destroy(config_file_path);
611 aws_string_destroy(profile_name);
612 if (first_profile_in_chain) {
613 aws_hash_table_clean_up(source_profiles_table);
614 aws_mem_release(allocator, source_profiles_table);
615 }
616 if (provider) {
617 provider->shutdown_options = options->shutdown_options;
618 }
619
620 return provider;
621 }
622
623 struct aws_credentials_provider *aws_credentials_provider_new_profile(
624 struct aws_allocator *allocator,
625 const struct aws_credentials_provider_profile_options *options) {
626
627 return s_credentials_provider_new_profile_internal(allocator, options, NULL);
628 }