Debian Codegraph @

crt/aws-c-sdkutils/source/aws_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/common/byte_buf.h>
7 #include <aws/common/environment.h>
8 #include <aws/common/file.h>
9 #include <aws/common/hash_table.h>
10 #include <aws/common/logging.h>
11 #include <aws/common/ref_count.h>
12 #include <aws/common/string.h>
13 #include <aws/sdkutils/aws_profile.h>
14
15 #define PROPERTIES_TABLE_DEFAULT_SIZE 4
16 #define PROFILE_TABLE_DEFAULT_SIZE 5
17
18 struct aws_profile_property {
19 struct aws_allocator *allocator;
20 struct aws_string *name;
21 struct aws_string *value;
22 struct aws_hash_table sub_properties;
23 bool is_empty_valued;
24 };
25
26 struct aws_profile {
27 struct aws_allocator *allocator;
28 struct aws_string *name;
29 struct aws_hash_table properties;
30 bool has_profile_prefix;
31 };
32
33 struct aws_profile_collection {
34 struct aws_allocator *allocator;
35 enum aws_profile_source_type profile_source;
36 /*
37 * Array of aws_hash_table for each section type.
38 * Each table is a map from section identifier to aws_profile.
39 * key: struct aws_string*
40 * value: struct aws_profile*
41 */
42 struct aws_hash_table sections[AWS_PROFILE_SECTION_TYPE_COUNT];
43 struct aws_ref_count ref_count;
44 };
45
46 /*
47 * Character-based profile parse helper functions
48 */
49 static bool s_is_assignment_operator(uint8_t value) {
50 return (char)value == '=';
51 }
52
53 static bool s_is_not_assignment_operator(uint8_t value) {
54 return !s_is_assignment_operator(value);
55 }
56
57 static bool s_is_identifier(uint8_t value) {
58 char value_as_char = (char)value;
59
60 if ((value_as_char >= 'A' && value_as_char <= 'Z') || (value_as_char >= 'a' && value_as_char <= 'z') ||
61 (value_as_char >= '0' && value_as_char <= '9') || value_as_char == '\\' || value_as_char == '_' ||
62 value_as_char == '-' || value_as_char == '/' || value_as_char == '.' || value_as_char == '%' ||
63 value_as_char == '@' || value_as_char == ':' || value_as_char == '+') {
64 /* [A-Za-z0-9_\-/.%@:\+]+, keep '\\' for backward compatibility */
65 return true;
66 }
67
68 return false;
69 }
70
71 static bool s_is_whitespace(uint8_t value) {
72 char value_as_char = (char)value;
73
74 switch (value_as_char) {
75 case '\t':
76 case '\n':
77 case '\r':
78 case ' ':
79 return true;
80
81 default:
82 return false;
83 }
84 }
85
86 static bool s_is_comment_token(uint8_t value) {
87 char char_value = (char)value;
88
89 return char_value == '#' || char_value == ';';
90 }
91
92 static bool s_is_not_comment_token(uint8_t value) {
93 return !s_is_comment_token(value);
94 }
95
96 static bool s_is_profile_start(uint8_t value) {
97 return (char)value == '[';
98 }
99
100 static bool s_is_not_profile_end(uint8_t value) {
101 return (char)value != ']';
102 }
103
104 static bool s_is_carriage_return(uint8_t value) {
105 return (char)value == '\r';
106 }
107
108 /*
109 * Line and string based parse helper functions
110 */
111 static bool s_is_comment_line(const struct aws_byte_cursor *line_cursor) {
112 char first_char = *line_cursor->ptr;
113 return first_char == '#' || first_char == ';';
114 }
115
116 static bool s_is_whitespace_line(const struct aws_byte_cursor *line_cursor) {
117 return aws_byte_cursor_left_trim_pred(line_cursor, s_is_whitespace).len == 0;
118 }
119
120 AWS_STATIC_STRING_FROM_LITERAL(s_default_profile_name, "default");
121
122 static bool s_is_default_profile_name(const struct aws_byte_cursor *profile_name) {
123 return aws_string_eq_byte_cursor(s_default_profile_name, profile_name);
124 }
125
126 /*
127 * Consume helpers
128 */
129
130 /*
131 * Consumes characters as long as a predicate is satisfied. "parsed" is optional and contains the consumed range as
132 * output. Returns true if anything was consumed.
133 *
134 * On success, start is updated to the new position.
135 */
136 static bool s_parse_by_character_predicate(
137 struct aws_byte_cursor *start,
138 aws_byte_predicate_fn *predicate,
139 struct aws_byte_cursor *parsed,
140 size_t maximum_allowed) {
141
142 uint8_t *current_ptr = start->ptr;
143 uint8_t *end_ptr = start->ptr + start->len;
144 if (maximum_allowed > 0 && maximum_allowed < start->len) {
145 end_ptr = start->ptr + maximum_allowed;
146 }
147
148 while (current_ptr < end_ptr) {
149 if (!predicate(*current_ptr)) {
150 break;
151 }
152
153 ++current_ptr;
154 }
155
156 size_t consumed = current_ptr - start->ptr;
157 if (parsed != NULL) {
158 parsed->ptr = start->ptr;
159 parsed->len = consumed;
160 }
161
162 aws_byte_cursor_advance(start, consumed);
163
164 return consumed > 0;
165 }
166
167 /*
168 * Consumes characters if they match a token string. "parsed" is optional and contains the consumed range as output.
169 * Returns true if anything was consumed.
170 *
171 * On success, start is updated to the new position.
172 */
173 static bool s_parse_by_token(
174 struct aws_byte_cursor *start,
175 const struct aws_string *token,
176 struct aws_byte_cursor *parsed) {
177
178 bool matched = false;
179
180 if (token->len <= start->len) {
181 matched = strncmp((const char *)start->ptr, aws_string_c_str(token), token->len) == 0;
182 }
183
184 if (parsed != NULL) {
185 parsed->ptr = start->ptr;
186 parsed->len = matched ? token->len : 0;
187 }
188
189 if (matched) {
190 aws_byte_cursor_advance(start, token->len);
191 }
192
193 return matched;
194 }
195
196 /*
197 * Parse context and logging
198 */
199
200 struct profile_file_parse_context {
201 const struct aws_string *source_file_path;
202 struct aws_profile_collection *profile_collection;
203 struct aws_profile *current_profile;
204 struct aws_profile_property *current_property;
205 struct aws_byte_cursor current_line;
206 int parse_error;
207 int current_line_number;
208 bool has_seen_profile;
209 };
210
211 AWS_STATIC_STRING_FROM_LITERAL(s_none_string, "<None>");
212
213 static void s_log_parse_context(enum aws_log_level log_level, const struct profile_file_parse_context *context) {
214 AWS_LOGF(
215 log_level,
216 AWS_LS_SDKUTILS_PROFILE,
217 "Profile Parse context:\n Source File:%s\n Line: %d\n Current Profile: %s\n Current Property: %s",
218 context->source_file_path ? context->source_file_path->bytes : s_none_string->bytes,
219 context->current_line_number,
220 context->current_profile ? context->current_profile->name->bytes : s_none_string->bytes,
221 context->current_property ? context->current_property->name->bytes : s_none_string->bytes);
222 }
223
224 /*
225 * aws_profile_property APIs
226 */
227
228 static void s_profile_property_destroy(struct aws_profile_property *property) {
229 if (property == NULL) {
230 return;
231 }
232
233 aws_string_destroy(property->name);
234 aws_string_destroy(property->value);
235
236 aws_hash_table_clean_up(&property->sub_properties);
237
238 aws_mem_release(property->allocator, property);
239 }
240
241 struct aws_profile_property *aws_profile_property_new(
242 struct aws_allocator *allocator,
243 const struct aws_byte_cursor *name,
244 const struct aws_byte_cursor *value) {
245
246 struct aws_profile_property *property =
247 (struct aws_profile_property *)aws_mem_acquire(allocator, sizeof(struct aws_profile_property));
248 if (property == NULL) {
249 return NULL;
250 }
251
252 AWS_ZERO_STRUCT(*property);
253 property->allocator = allocator;
254
255 if (aws_hash_table_init(
256 &property->sub_properties,
257 allocator,
258 0,
259 aws_hash_string,
260 aws_hash_callback_string_eq,
261 aws_hash_callback_string_destroy,
262 aws_hash_callback_string_destroy)) {
263 goto on_error;
264 }
265
266 property->value = aws_string_new_from_array(allocator, value->ptr, value->len);
267 if (property->value == NULL) {
268 goto on_error;
269 }
270
271 property->name = aws_string_new_from_array(allocator, name->ptr, name->len);
272 if (property->name == NULL) {
273 goto on_error;
274 }
275
276 property->is_empty_valued = value->len == 0;
277
278 return property;
279
280 on_error:
281 s_profile_property_destroy(property);
282
283 return NULL;
284 }
285
286 AWS_STATIC_STRING_FROM_LITERAL(s_newline, "\n");
287
288 /*
289 * Continuations are applied to the property value by concatenating the old value and the new value, with a '\n'
290 * in between.
291 */
292 static int s_profile_property_add_continuation(
293 struct aws_profile_property *property,
294 const struct aws_byte_cursor *continuation_value) {
295
296 int result = AWS_OP_ERR;
297 struct aws_byte_buf concatenation;
298 if (aws_byte_buf_init(&concatenation, property->allocator, property->value->len + continuation_value->len + 1)) {
299 return result;
300 }
301
302 struct aws_byte_cursor old_value = aws_byte_cursor_from_string(property->value);
303 if (aws_byte_buf_append(&concatenation, &old_value)) {
304 goto on_generic_failure;
305 }
306
307 struct aws_byte_cursor newline = aws_byte_cursor_from_string(s_newline);
308 if (aws_byte_buf_append(&concatenation, &newline)) {
309 goto on_generic_failure;
310 }
311
312 if (aws_byte_buf_append(&concatenation, continuation_value)) {
313 goto on_generic_failure;
314 }
315
316 struct aws_string *new_value =
317 aws_string_new_from_array(property->allocator, concatenation.buffer, concatenation.len);
318 if (new_value == NULL) {
319 goto on_generic_failure;
320 }
321
322 result = AWS_OP_SUCCESS;
323 aws_string_destroy(property->value);
324 property->value = new_value;
325
326 on_generic_failure:
327 aws_byte_buf_clean_up(&concatenation);
328
329 return result;
330 }
331
332 static int s_profile_property_add_sub_property(
333 struct aws_profile_property *property,
334 const struct aws_byte_cursor *key,
335 const struct aws_byte_cursor *value,
336 const struct profile_file_parse_context *context) {
337
338 struct aws_string *key_string = aws_string_new_from_array(property->allocator, key->ptr, key->len);
339 if (key_string == NULL) {
340 return AWS_OP_ERR;
341 }
342
343 struct aws_string *value_string = aws_string_new_from_array(property->allocator, value->ptr, value->len);
344 if (value_string == NULL) {
345 goto on_failure;
346 }
347
348 int was_present = 0;
349 aws_hash_table_remove(&property->sub_properties, key_string, NULL, &was_present);
350 if (was_present) {
351 AWS_LOGF_DEBUG(
352 AWS_LS_SDKUTILS_PROFILE,
353 "subproperty \"%s\" of property \"%s\" had value overridden with new value",
354 key_string->bytes,
355 property->name->bytes);
356 s_log_parse_context(AWS_LL_WARN, context);
357 }
358
359 if (aws_hash_table_put(&property->sub_properties, key_string, value_string, NULL)) {
360 goto on_failure;
361 }
362
363 return AWS_OP_SUCCESS;
364
365 on_failure:
366
367 if (value_string) {
368 aws_string_destroy(value_string);
369 }
370
371 aws_string_destroy(key_string);
372
373 return AWS_OP_ERR;
374 }
375
376 static int s_profile_property_merge(struct aws_profile_property *dest, const struct aws_profile_property *source) {
377
378 AWS_ASSERT(dest != NULL && source != NULL);
379
380 /*
381 * Source value overwrites any existing dest value
382 */
383 if (source->value) {
384 struct aws_string *new_value = aws_string_new_from_string(dest->allocator, source->value);
385 if (new_value == NULL) {
386 return AWS_OP_ERR;
387 }
388
389 if (dest->value) {
390 AWS_LOGF_DEBUG(
391 AWS_LS_SDKUTILS_PROFILE,
392 "property \"%s\" has value \"%s\" replaced during merge",
393 dest->name->bytes,
394 dest->value->bytes);
395 aws_string_destroy(dest->value);
396 }
397
398 dest->value = new_value;
399 }
400
401 dest->is_empty_valued = source->is_empty_valued;
402
403 /*
404 * Iterate sub properties, stomping on conflicts
405 */
406 struct aws_hash_iter source_iter = aws_hash_iter_begin(&source->sub_properties);
407 while (!aws_hash_iter_done(&source_iter)) {
408 struct aws_string *source_sub_property = (struct aws_string *)source_iter.element.value;
409
410 struct aws_string *dest_key =
411 aws_string_new_from_string(dest->allocator, (struct aws_string *)source_iter.element.key);
412 if (dest_key == NULL) {
413 return AWS_OP_ERR;
414 }
415
416 struct aws_string *dest_sub_property = aws_string_new_from_string(dest->allocator, source_sub_property);
417 if (dest_sub_property == NULL) {
418 aws_string_destroy(dest_key);
419 return AWS_OP_ERR;
420 }
421
422 int was_present = 0;
423 aws_hash_table_remove(&dest->sub_properties, dest_key, NULL, &was_present);
424 if (was_present) {
425 AWS_LOGF_DEBUG(
426 AWS_LS_SDKUTILS_PROFILE,
427 "subproperty \"%s\" of property \"%s\" had value overridden during property merge",
428 dest_key->bytes,
429 dest->name->bytes);
430 }
431
432 if (aws_hash_table_put(&dest->sub_properties, dest_key, dest_sub_property, NULL)) {
433 aws_string_destroy(dest_sub_property);
434 aws_string_destroy(dest_key);
435 return AWS_OP_ERR;
436 }
437
438 aws_hash_iter_next(&source_iter);
439 }
440
441 return AWS_OP_SUCCESS;
442 }
443
444 /*
445 * Helper destroy function for aws_profile's hash table of properties
446 */
447 static void s_property_hash_table_value_destroy(void *value) {
448 s_profile_property_destroy((struct aws_profile_property *)value);
449 }
450
451 /*
452 * aws_profile APIs
453 */
454
455 void aws_profile_destroy(struct aws_profile *profile) {
456 if (profile == NULL) {
457 return;
458 }
459
460 aws_string_destroy(profile->name);
461
462 aws_hash_table_clean_up(&profile->properties);
463
464 aws_mem_release(profile->allocator, profile);
465 }
466
467 struct aws_profile *aws_profile_new(
468 struct aws_allocator *allocator,
469 const struct aws_byte_cursor *name,
470 bool has_profile_prefix) {
471
472 struct aws_profile *profile = (struct aws_profile *)aws_mem_acquire(allocator, sizeof(struct aws_profile));
473 if (profile == NULL) {
474 return NULL;
475 }
476
477 AWS_ZERO_STRUCT(*profile);
478
479 profile->name = aws_string_new_from_array(allocator, name->ptr, name->len);
480 if (profile->name == NULL) {
481 goto cleanup;
482 }
483
484 if (aws_hash_table_init(
485 &profile->properties,
486 allocator,
487 PROPERTIES_TABLE_DEFAULT_SIZE,
488 aws_hash_string,
489 aws_hash_callback_string_eq,
490 NULL, /* The key is owned by the value (and destroy cleans it up), so we don't have to */
491 s_property_hash_table_value_destroy)) {
492
493 goto cleanup;
494 }
495
496 profile->allocator = allocator;
497 profile->has_profile_prefix = has_profile_prefix;
498
499 return profile;
500
501 cleanup:
502 aws_profile_destroy(profile);
503
504 return NULL;
505 }
506
507 /*
508 * Adds a property to a profile.
509 *
510 * If a property already exists then the old one is removed and replaced by the
511 * new one.
512 */
513 static struct aws_profile_property *s_profile_add_property(
514 struct aws_profile *profile,
515 const struct aws_byte_cursor *key_cursor,
516 const struct aws_byte_cursor *value_cursor) {
517
518 struct aws_profile_property *property = aws_profile_property_new(profile->allocator, key_cursor, value_cursor);
519 if (property == NULL) {
520 goto on_property_new_failure;
521 }
522
523 if (aws_hash_table_put(&profile->properties, property->name, property, NULL)) {
524 goto on_hash_table_put_failure;
525 }
526
527 return property;
528
529 on_hash_table_put_failure:
530 s_profile_property_destroy(property);
531
532 on_property_new_failure:
533 return NULL;
534 }
535
536 const struct aws_profile_property *aws_profile_get_property(
537 const struct aws_profile *profile,
538 const struct aws_string *property_name) {
539
540 struct aws_hash_element *element = NULL;
541 aws_hash_table_find(&profile->properties, property_name, &element);
542
543 if (element == NULL) {
544 return NULL;
545 }
546
547 return element->value;
548 }
549
550 const struct aws_string *aws_profile_property_get_value(const struct aws_profile_property *property) {
551 AWS_PRECONDITION(property);
552 return property->value;
553 }
554
555 static int s_profile_merge(struct aws_profile *dest_profile, const struct aws_profile *source_profile) {
556
557 AWS_ASSERT(dest_profile != NULL && source_profile != NULL);
558
559 dest_profile->has_profile_prefix = source_profile->has_profile_prefix;
560
561 struct aws_hash_iter source_iter = aws_hash_iter_begin(&source_profile->properties);
562 while (!aws_hash_iter_done(&source_iter)) {
563 struct aws_profile_property *source_property = (struct aws_profile_property *)source_iter.element.value;
564 struct aws_profile_property *dest_property = (struct aws_profile_property *)aws_profile_get_property(
565 dest_profile, (struct aws_string *)source_iter.element.key);
566 if (dest_property == NULL) {
567
568 struct aws_byte_cursor empty_value;
569 AWS_ZERO_STRUCT(empty_value);
570
571 struct aws_byte_cursor property_name = aws_byte_cursor_from_string(source_iter.element.key);
572 dest_property = aws_profile_property_new(dest_profile->allocator, &property_name, &empty_value);
573 if (dest_property == NULL) {
574 return AWS_OP_ERR;
575 }
576
577 if (aws_hash_table_put(&dest_profile->properties, dest_property->name, dest_property, NULL)) {
578 s_profile_property_destroy(dest_property);
579 return AWS_OP_ERR;
580 }
581 }
582
583 if (s_profile_property_merge(dest_property, source_property)) {
584 return AWS_OP_ERR;
585 }
586
587 aws_hash_iter_next(&source_iter);
588 }
589
590 return AWS_OP_SUCCESS;
591 }
592
593 /*
594 * Hash table destroy helper for profile collection's profiles member
595 */
596 static void s_profile_hash_table_value_destroy(void *value) {
597 aws_profile_destroy((struct aws_profile *)value);
598 }
599
600 /*
601 * aws_profile_collection APIs
602 */
603
604 void aws_profile_collection_destroy(struct aws_profile_collection *profile_collection) {
605 aws_profile_collection_release(profile_collection);
606 }
607
608 static void s_aws_profile_collection_destroy_internal(struct aws_profile_collection *profile_collection) {
609 for (int i = 0; i < AWS_PROFILE_SECTION_TYPE_COUNT; i++) {
610 aws_hash_table_clean_up(&profile_collection->sections[i]);
611 }
612 aws_mem_release(profile_collection->allocator, profile_collection);
613 }
614
615 AWS_STATIC_STRING_FROM_LITERAL(s_profile_token, "profile");
616 AWS_STATIC_STRING_FROM_LITERAL(s_sso_session_token, "sso-session");
617 AWS_STATIC_STRING_FROM_LITERAL(s_services_token, "services");
618
619 const struct aws_profile *aws_profile_collection_get_profile(
620 const struct aws_profile_collection *profile_collection,
621 const struct aws_string *profile_name) {
622 return aws_profile_collection_get_section(profile_collection, AWS_PROFILE_SECTION_TYPE_PROFILE, profile_name);
623 }
624
625 const struct aws_profile *aws_profile_collection_get_section(
626 const struct aws_profile_collection *profile_collection,
627 const enum aws_profile_section_type section_type,
628 const struct aws_string *section_name) {
629 struct aws_hash_element *element = NULL;
630 aws_hash_table_find(&profile_collection->sections[section_type], section_name, &element);
631 if (element == NULL) {
632 return NULL;
633 }
634 return element->value;
635 }
636
637 static int s_profile_collection_add_profile(
638 struct aws_profile_collection *profile_collection,
639 const enum aws_profile_section_type section_type,
640 const struct aws_byte_cursor *profile_name,
641 bool has_profile_prefix,
642 const struct profile_file_parse_context *context,
643 struct aws_profile **current_profile_out) {
644
645 *current_profile_out = NULL;
646 struct aws_string *key =
647 aws_string_new_from_array(profile_collection->allocator, profile_name->ptr, profile_name->len);
648 if (key == NULL) {
649 return AWS_OP_ERR;
650 }
651
652 struct aws_profile *existing_profile = NULL;
653 struct aws_hash_element *element = NULL;
654 aws_hash_table_find(&profile_collection->sections[section_type], key, &element);
655 if (element != NULL) {
656 existing_profile = element->value;
657 }
658
659 aws_string_destroy(key);
660
661 if (section_type == AWS_PROFILE_SECTION_TYPE_PROFILE && profile_collection->profile_source == AWS_PST_CONFIG &&
662 s_is_default_profile_name(profile_name)) {
663 /*
664 * In a config file, "profile default" always supercedes "default"
665 */
666 if (!has_profile_prefix && existing_profile && existing_profile->has_profile_prefix) {
667 /*
668 * existing one supercedes: ignore this (and its properties) completely by failing the add
669 * which sets the current profile to NULL
670 */
671 AWS_LOGF_DEBUG(
672 AWS_LS_SDKUTILS_PROFILE,
673 "Existing prefixed default config profile supercedes unprefixed default profile");
674 s_log_parse_context(AWS_LL_WARN, context);
675
676 return AWS_OP_SUCCESS;
677 }
678
679 if (has_profile_prefix && existing_profile && !existing_profile->has_profile_prefix) {
680 /*
681 * stomp over existing: remove it, then proceed with add
682 * element destroy function will clean up the profile and key
683 */
684 AWS_LOGF_DEBUG(
685 AWS_LS_SDKUTILS_PROFILE, "Prefixed default config profile replacing unprefixed default profile");
686 s_log_parse_context(AWS_LL_WARN, context);
687
688 aws_hash_table_remove(&profile_collection->sections[section_type], element->key, NULL, NULL);
689 existing_profile = NULL;
690 }
691 }
692
693 if (existing_profile) {
694 *current_profile_out = existing_profile;
695 return AWS_OP_SUCCESS;
696 }
697
698 struct aws_profile *new_profile = aws_profile_new(profile_collection->allocator, profile_name, has_profile_prefix);
699 if (new_profile == NULL) {
700 goto on_aws_profile_new_failure;
701 }
702
703 if (aws_hash_table_put(&profile_collection->sections[section_type], new_profile->name, new_profile, NULL)) {
704 goto on_hash_table_put_failure;
705 }
706
707 *current_profile_out = new_profile;
708 return AWS_OP_SUCCESS;
709
710 on_hash_table_put_failure:
711 aws_profile_destroy(new_profile);
712
713 on_aws_profile_new_failure:
714 return AWS_OP_ERR;
715 }
716
717 static int s_profile_collection_merge(
718 struct aws_profile_collection *dest_collection,
719 const struct aws_profile_collection *source_collection) {
720
721 AWS_ASSERT(dest_collection != NULL && source_collection);
722 for (int i = 0; i < AWS_PROFILE_SECTION_TYPE_COUNT; i++) {
723 struct aws_hash_iter source_iter = aws_hash_iter_begin(&source_collection->sections[i]);
724 while (!aws_hash_iter_done(&source_iter)) {
725 struct aws_profile *source_profile = (struct aws_profile *)source_iter.element.value;
726 struct aws_profile *dest_profile = (struct aws_profile *)aws_profile_collection_get_profile(
727 dest_collection, (struct aws_string *)source_iter.element.key);
728
729 if (dest_profile == NULL) {
730
731 struct aws_byte_cursor name_cursor = aws_byte_cursor_from_string(source_iter.element.key);
732 dest_profile =
733 aws_profile_new(dest_collection->allocator, &name_cursor, source_profile->has_profile_prefix);
734 if (dest_profile == NULL) {
735 return AWS_OP_ERR;
736 }
737
738 if (aws_hash_table_put(&dest_collection->sections[i], dest_profile->name, dest_profile, NULL)) {
739 aws_profile_destroy(dest_profile);
740 return AWS_OP_ERR;
741 }
742 }
743
744 if (s_profile_merge(dest_profile, source_profile)) {
745 return AWS_OP_ERR;
746 }
747
748 aws_hash_iter_next(&source_iter);
749 }
750 }
751
752 return AWS_OP_SUCCESS;
753 }
754
755 struct aws_profile_collection *aws_profile_collection_new_from_merge(
756 struct aws_allocator *allocator,
757 const struct aws_profile_collection *config_profiles,
758 const struct aws_profile_collection *credentials_profiles) {
759
760 struct aws_profile_collection *merged =
761 (struct aws_profile_collection *)(aws_mem_acquire(allocator, sizeof(struct aws_profile_collection)));
762 if (merged == NULL) {
763 return NULL;
764 }
765
766 AWS_ZERO_STRUCT(*merged);
767 aws_ref_count_init(
768 &merged->ref_count, merged, (aws_simple_completion_callback *)s_aws_profile_collection_destroy_internal);
769 for (int i = 0; i < AWS_PROFILE_SECTION_TYPE_COUNT; i++) {
770 size_t max_profiles = 0;
771 if (config_profiles != NULL) {
772 max_profiles += aws_hash_table_get_entry_count(&config_profiles->sections[i]);
773 }
774 if (credentials_profiles != NULL) {
775 max_profiles += aws_hash_table_get_entry_count(&credentials_profiles->sections[i]);
776 }
777
778 merged->allocator = allocator;
779 merged->profile_source = AWS_PST_NONE;
780
781 if (aws_hash_table_init(
782 &merged->sections[i],
783 allocator,
784 max_profiles,
785 aws_hash_string,
786 aws_hash_callback_string_eq,
787 NULL,
788 s_profile_hash_table_value_destroy)) {
789 goto cleanup;
790 }
791 }
792
793 if (config_profiles != NULL) {
794 if (s_profile_collection_merge(merged, config_profiles)) {
795 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Failed to merge config profile set");
796 goto cleanup;
797 }
798 }
799
800 if (credentials_profiles != NULL) {
801 if (s_profile_collection_merge(merged, credentials_profiles)) {
802 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Failed to merge credentials profile set");
803 goto cleanup;
804 }
805 }
806
807 return merged;
808
809 cleanup:
810 s_aws_profile_collection_destroy_internal(merged);
811
812 return NULL;
813 }
814
815 /*
816 * Profile parsing
817 */
818
819 /*
820 * The comment situation in config files is messy. Some line types require a comment to have at least one
821 * whitespace in front of it, while other line types only require a comment token (;, #) On top of that, some
822 * line types do not allow comments at all (get folded into the value).
823 *
824 */
825
826 /*
827 * a trailing comment is started by ';' or '#'
828 * Only certain types of lines allow comments without prefixing whitespace
829 */
830 static struct aws_byte_cursor s_trim_trailing_comment(const struct aws_byte_cursor *line) {
831
832 struct aws_byte_cursor line_copy = *line;
833 struct aws_byte_cursor trimmed;
834 s_parse_by_character_predicate(&line_copy, s_is_not_comment_token, &trimmed, 0);
835
836 return trimmed;
837 }
838
839 /*
840 * A trailing whitespace comment is started by " ;", " #", "\t;", or "\t#"
841 * Certain types of lines require comments be whitespace-prefixed
842 */
843 static struct aws_byte_cursor s_trim_trailing_whitespace_comment(const struct aws_byte_cursor *line) {
844 struct aws_byte_cursor trimmed;
845 trimmed.ptr = line->ptr;
846
847 uint8_t *current_ptr = line->ptr;
848 uint8_t *end_ptr = line->ptr + line->len;
849
850 while (current_ptr < end_ptr) {
851 if (s_is_whitespace(*current_ptr)) {
852 /*
853 * Look ahead 1
854 */
855 if (current_ptr + 1 < end_ptr && s_is_comment_token(*(current_ptr + 1))) {
856 break;
857 }
858 }
859
860 current_ptr++;
861 }
862
863 trimmed.len = current_ptr - line->ptr;
864
865 return trimmed;
866 }
867
868 static int s_parse_section_type_prefix(
869 struct aws_byte_cursor *profile_cursor,
870 enum aws_profile_section_type *out_section_type) {
871 /* check profile prefix */
872 if (s_parse_by_token(profile_cursor, s_profile_token, NULL)) {
873 if (s_parse_by_character_predicate(profile_cursor, s_is_whitespace, NULL, 1)) {
874 *out_section_type = AWS_PROFILE_SECTION_TYPE_PROFILE;
875 return AWS_OP_SUCCESS;
876 }
877 return aws_raise_error(AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE);
878 }
879
880 /* check sso-session prefix */
881 if (s_parse_by_token(profile_cursor, s_sso_session_token, NULL)) {
882 if (s_parse_by_character_predicate(profile_cursor, s_is_whitespace, NULL, 1)) {
883 *out_section_type = AWS_PROFILE_SECTION_TYPE_SSO_SESSION;
884 return AWS_OP_SUCCESS;
885 }
886 return aws_raise_error(AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE);
887 }
888 /* check services prefix */
889 if (s_parse_by_token(profile_cursor, s_services_token, NULL)) {
890 if (s_parse_by_character_predicate(profile_cursor, s_is_whitespace, NULL, 1)) {
891 *out_section_type = AWS_PROFILE_SECTION_TYPE_SERVICES;
892 return AWS_OP_SUCCESS;
893 }
894 return aws_raise_error(AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE);
895 }
896 return aws_raise_error(AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE);
897 }
898
899 /**
900 * Attempts to parse profile declaration lines
901 *
902 * Return false if this is not a profile declaration, true otherwise (stop parsing the line)
903 */
904 static bool s_parse_profile_declaration(
905 const struct aws_byte_cursor *line_cursor,
906 struct profile_file_parse_context *context) {
907
908 /*
909 * Strip comment and right-side whitespace
910 */
911 struct aws_byte_cursor profile_line_cursor = s_trim_trailing_comment(line_cursor);
912 struct aws_byte_cursor profile_cursor = aws_byte_cursor_right_trim_pred(&profile_line_cursor, s_is_whitespace);
913
914 /*
915 * "[" + <whitespace>? + <"profile ">? + <profile name = identifier> + <whitespace>? + "]"
916 */
917 if (!s_parse_by_character_predicate(&profile_cursor, s_is_profile_start, NULL, 1)) {
918 /*
919 * This isn't a profile declaration, try something else
920 */
921 return false;
922 }
923
924 context->has_seen_profile = true;
925 context->current_profile = NULL;
926 context->current_property = NULL;
927
928 s_parse_by_character_predicate(&profile_cursor, s_is_whitespace, NULL, 0);
929 enum aws_profile_section_type section_type = AWS_PROFILE_SECTION_TYPE_PROFILE;
930
931 /*
932 * Check if the profile name starts with a valid prefix. We need to check for
933 * prefix and at least one whitespace character. A partial match like
934 * ("[profilefoo]" for example) should rewind and use the whole name properly.
935 */
936 struct aws_byte_cursor backtrack_cursor = profile_cursor;
937 bool has_valid_section_type_prefix = false;
938 if (s_parse_section_type_prefix(&profile_cursor, &section_type) != AWS_OP_SUCCESS) {
939 profile_cursor = backtrack_cursor;
940 } else {
941 has_valid_section_type_prefix = true;
942 if (context->profile_collection->profile_source == AWS_PST_CREDENTIALS) {
943 AWS_LOGF_WARN(
944 AWS_LS_SDKUTILS_PROFILE,
945 "Profile declarations in credentials files are not allowed to begin with the section-type keyword");
946 s_log_parse_context(AWS_LL_WARN, context);
947
948 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
949 return true;
950 }
951
952 s_parse_by_character_predicate(&profile_cursor, s_is_whitespace, NULL, 0);
953 }
954 struct aws_byte_cursor profile_name;
955 if (!s_parse_by_character_predicate(&profile_cursor, s_is_identifier, &profile_name, 0)) {
956 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Profile declarations must contain a valid identifier for a name");
957 s_log_parse_context(AWS_LL_WARN, context);
958
959 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
960 return true;
961 }
962
963 if (context->profile_collection->profile_source == AWS_PST_CONFIG && !has_valid_section_type_prefix &&
964 !s_is_default_profile_name(&profile_name)) {
965 AWS_LOGF_WARN(
966 AWS_LS_SDKUTILS_PROFILE,
967 "Non-default profile declarations in config files must use the \"profile\" keyword");
968 s_log_parse_context(AWS_LL_WARN, context);
969
970 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
971 return true;
972 }
973
974 s_parse_by_character_predicate(&profile_cursor, s_is_whitespace, NULL, 0);
975
976 /*
977 * Special case the right side bracket check. We need to distinguish between a missing right bracket
978 * (fatal error) and invalid profile name (spaces, non-identifier characters).
979 *
980 * Do so by consuming all non right-bracket characters. If the remainder is empty it is missing,
981 * otherwise it is an invalid profile name (non-empty invalid_chars) or a good definition
982 * (empty invalid_chars cursor).
983 */
984 struct aws_byte_cursor invalid_chars;
985 s_parse_by_character_predicate(&profile_cursor, s_is_not_profile_end, &invalid_chars, 0);
986 if (profile_cursor.len == 0) {
987 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Profile declaration missing required ending bracket");
988 s_log_parse_context(AWS_LL_WARN, context);
989
990 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
991 return true;
992 }
993
994 if (invalid_chars.len > 0) {
995 AWS_LOGF_WARN(
996 AWS_LS_SDKUTILS_PROFILE,
997 "Profile declaration contains invalid characters: \"" PRInSTR "\"",
998 AWS_BYTE_CURSOR_PRI(invalid_chars));
999 s_log_parse_context(AWS_LL_WARN, context);
1000
1001 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
1002 return true;
1003 }
1004
1005 /*
1006 * Apply to the profile collection
1007 */
1008 if (s_profile_collection_add_profile(
1009 context->profile_collection,
1010 section_type,
1011 &profile_name,
1012 has_valid_section_type_prefix && section_type == AWS_PROFILE_SECTION_TYPE_PROFILE,
1013 context,
1014 &context->current_profile)) {
1015 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Failed to add profile to profile collection");
1016 s_log_parse_context(AWS_LL_ERROR, context);
1017
1018 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1019 return true;
1020 }
1021
1022 return true;
1023 }
1024
1025 /**
1026 * Attempts to parse property continuation lines
1027 *
1028 * Return false if this is not a property continuation line, true otherwise (stop parsing the line)
1029 */
1030 static bool s_parse_property_continuation(
1031 const struct aws_byte_cursor *line_cursor,
1032 struct profile_file_parse_context *context) {
1033
1034 /*
1035 * Strip right-side whitespace only. Comments cannot be made on continuation lines. They
1036 * get folded into the value.
1037 */
1038 struct aws_byte_cursor continuation_cursor = aws_byte_cursor_right_trim_pred(line_cursor, s_is_whitespace);
1039
1040 /*
1041 * Can't be a continuation without at least one whitespace on the left
1042 */
1043 if (!s_parse_by_character_predicate(&continuation_cursor, s_is_whitespace, NULL, 0)) {
1044 return false;
1045 }
1046
1047 /*
1048 * This should never happen since it should have been caught as a whitespace line
1049 */
1050 if (continuation_cursor.len == 0) {
1051 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Property continuation internal parsing error");
1052 s_log_parse_context(AWS_LL_ERROR, context);
1053
1054 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
1055 return true;
1056 }
1057
1058 /*
1059 * A continuation without a current property is bad
1060 */
1061 if (context->current_profile == NULL || context->current_property == NULL) {
1062 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Property continuation seen outside of a current property");
1063 s_log_parse_context(AWS_LL_WARN, context);
1064
1065 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1066 return true;
1067 }
1068
1069 if (s_profile_property_add_continuation(context->current_property, &continuation_cursor)) {
1070 AWS_LOGF_DEBUG(AWS_LS_SDKUTILS_PROFILE, "Property continuation could not be applied to the current property");
1071 s_log_parse_context(AWS_LL_WARN, context);
1072
1073 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
1074 return true;
1075 }
1076
1077 if (context->current_property->is_empty_valued) {
1078
1079 struct aws_byte_cursor key_cursor;
1080 if (!s_parse_by_character_predicate(&continuation_cursor, s_is_not_assignment_operator, &key_cursor, 0)) {
1081 AWS_LOGF_WARN(
1082 AWS_LS_SDKUTILS_PROFILE, "Empty-valued property continuation must contain the assignment operator");
1083 s_log_parse_context(AWS_LL_WARN, context);
1084
1085 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1086 return true;
1087 }
1088
1089 if (!s_parse_by_character_predicate(&continuation_cursor, s_is_assignment_operator, NULL, 1)) {
1090 AWS_LOGF_WARN(
1091 AWS_LS_SDKUTILS_PROFILE, "Empty-valued property continuation must contain the assignment operator");
1092 s_log_parse_context(AWS_LL_WARN, context);
1093
1094 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1095 return true;
1096 }
1097
1098 struct aws_byte_cursor trimmed_key_cursor = aws_byte_cursor_right_trim_pred(&key_cursor, s_is_whitespace);
1099 struct aws_byte_cursor id_check_cursor = aws_byte_cursor_trim_pred(&trimmed_key_cursor, s_is_identifier);
1100 if (id_check_cursor.len > 0) {
1101 AWS_LOGF_WARN(
1102 AWS_LS_SDKUTILS_PROFILE,
1103 "Empty-valued property continuation must have a valid identifier to the left of the assignment");
1104 s_log_parse_context(AWS_LL_WARN, context);
1105
1106 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
1107 return true;
1108 }
1109
1110 s_parse_by_character_predicate(&continuation_cursor, s_is_whitespace, NULL, 0);
1111
1112 /*
1113 * everything left in the continuation_cursor is the sub property value
1114 */
1115 if (s_profile_property_add_sub_property(
1116 context->current_property, &trimmed_key_cursor, &continuation_cursor, context)) {
1117 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Internal error adding sub property to current property");
1118 s_log_parse_context(AWS_LL_ERROR, context);
1119
1120 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1121 }
1122 }
1123
1124 return true;
1125 }
1126
1127 /**
1128 * Attempts to parse property lines
1129 *
1130 * Return false if this is not a property line, true otherwise (stop parsing the line)
1131 */
1132 static bool s_parse_property(const struct aws_byte_cursor *line_cursor, struct profile_file_parse_context *context) {
1133
1134 /*
1135 * Strip whitespace-prefixed comment and right-side whitespace
1136 */
1137 struct aws_byte_cursor property_line_cursor = s_trim_trailing_whitespace_comment(line_cursor);
1138 struct aws_byte_cursor property_cursor = aws_byte_cursor_right_trim_pred(&property_line_cursor, s_is_whitespace);
1139
1140 context->current_property = NULL;
1141
1142 struct aws_byte_cursor key_cursor;
1143 if (!s_parse_by_character_predicate(&property_cursor, s_is_not_assignment_operator, &key_cursor, 0)) {
1144 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Property definition does not contain the assignment operator");
1145 s_log_parse_context(AWS_LL_WARN, context);
1146
1147 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1148 return true;
1149 }
1150
1151 struct aws_byte_cursor trimmed_key_cursor = aws_byte_cursor_right_trim_pred(&key_cursor, s_is_whitespace);
1152 struct aws_byte_cursor id_check_cursor = aws_byte_cursor_trim_pred(&trimmed_key_cursor, s_is_identifier);
1153 if (id_check_cursor.len > 0) {
1154 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Property definition does not begin with a valid identifier");
1155 s_log_parse_context(AWS_LL_WARN, context);
1156
1157 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
1158 return true;
1159 }
1160
1161 if (!s_parse_by_character_predicate(&property_cursor, s_is_assignment_operator, NULL, 1)) {
1162 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Property definition does not contain the assignment operator");
1163 s_log_parse_context(AWS_LL_WARN, context);
1164
1165 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1166 return true;
1167 }
1168
1169 s_parse_by_character_predicate(&property_cursor, s_is_whitespace, NULL, 0);
1170
1171 /*
1172 * If appropriate, apply to the profile collection, property_cursor contains the trimmed value, if one exists
1173 */
1174 if (context->current_profile != NULL) {
1175 context->current_property =
1176 s_profile_add_property(context->current_profile, &trimmed_key_cursor, &property_cursor);
1177 if (context->current_property == NULL) {
1178 AWS_LOGF_ERROR(
1179 AWS_LS_SDKUTILS_PROFILE,
1180 "Failed to add property \"" PRInSTR "\" to current profile \"%s\"",
1181 AWS_BYTE_CURSOR_PRI(trimmed_key_cursor),
1182 context->current_profile->name->bytes);
1183 s_log_parse_context(AWS_LL_ERROR, context);
1184
1185 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1186 }
1187 } else {
1188 /*
1189 * By definition, if we haven't seen any profiles yet, this is a fatal error
1190 */
1191 if (context->has_seen_profile) {
1192 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Property definition seen outside a profile");
1193 s_log_parse_context(AWS_LL_WARN, context);
1194
1195 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_RECOVERABLE;
1196 } else {
1197 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Property definition seen before any profiles");
1198 s_log_parse_context(AWS_LL_WARN, context);
1199
1200 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1201 }
1202 }
1203
1204 return true;
1205 }
1206
1207 static void s_parse_and_apply_line_to_profile_collection(
1208 struct profile_file_parse_context *context,
1209 const struct aws_byte_cursor *line_cursor) {
1210
1211 /*
1212 * Ignore line feed on windows
1213 */
1214 struct aws_byte_cursor line = aws_byte_cursor_right_trim_pred(line_cursor, s_is_carriage_return);
1215 if (line.len == 0 || s_is_comment_line(&line) || s_is_whitespace_line(&line)) {
1216 return;
1217 }
1218
1219 AWS_LOGF_TRACE(
1220 AWS_LS_SDKUTILS_PROFILE,
1221 "Parsing aws profile line in profile \"%s\", current property: \"%s\"",
1222 context->current_profile ? context->current_profile->name->bytes : s_none_string->bytes,
1223 context->current_property ? context->current_property->name->bytes : s_none_string->bytes);
1224
1225 if (s_parse_profile_declaration(&line, context)) {
1226 return;
1227 }
1228
1229 if (s_parse_property_continuation(&line, context)) {
1230 return;
1231 }
1232
1233 if (s_parse_property(&line, context)) {
1234 return;
1235 }
1236
1237 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PROFILE, "Unidentifiable line type encountered while parsing profile file");
1238 s_log_parse_context(AWS_LL_WARN, context);
1239
1240 context->parse_error = AWS_ERROR_SDKUTILS_PARSE_FATAL;
1241 }
1242
1243 static struct aws_profile_collection *s_aws_profile_collection_new_internal(
1244 struct aws_allocator *allocator,
1245 const struct aws_byte_buf *buffer,
1246 enum aws_profile_source_type source,
1247 const struct aws_string *path) {
1248
1249 struct aws_profile_collection *profile_collection =
1250 (struct aws_profile_collection *)aws_mem_acquire(allocator, sizeof(struct aws_profile_collection));
1251 if (profile_collection == NULL) {
1252 return NULL;
1253 }
1254
1255 AWS_ZERO_STRUCT(*profile_collection);
1256
1257 profile_collection->profile_source = source;
1258 profile_collection->allocator = allocator;
1259
1260 aws_ref_count_init(
1261 &profile_collection->ref_count,
1262 profile_collection,
1263 (aws_simple_completion_callback *)s_aws_profile_collection_destroy_internal);
1264
1265 for (int i = 0; i < AWS_PROFILE_SECTION_TYPE_COUNT; i++) {
1266 if (aws_hash_table_init(
1267 &profile_collection->sections[i],
1268 allocator,
1269 PROFILE_TABLE_DEFAULT_SIZE,
1270 aws_hash_string,
1271 aws_hash_callback_string_eq,
1272 NULL, /* The key is owned by the value (and destroy cleans it up), so we don't have to */
1273 s_profile_hash_table_value_destroy)) {
1274 goto cleanup;
1275 }
1276 }
1277
1278 struct aws_byte_cursor current_position = aws_byte_cursor_from_buf(buffer);
1279
1280 if (current_position.len > 0) {
1281 struct aws_byte_cursor line_cursor;
1282 AWS_ZERO_STRUCT(line_cursor);
1283
1284 struct profile_file_parse_context context;
1285 AWS_ZERO_STRUCT(context);
1286 context.current_line_number = 1;
1287 context.profile_collection = profile_collection;
1288 context.source_file_path = path;
1289
1290 while (aws_byte_cursor_next_split(&current_position, '\n', &line_cursor)) {
1291 context.current_line = line_cursor;
1292
1293 s_parse_and_apply_line_to_profile_collection(&context, &line_cursor);
1294 if (context.parse_error == AWS_ERROR_SDKUTILS_PARSE_FATAL) {
1295 AWS_LOGF_WARN(AWS_LS_SDKUTILS_PROFILE, "Fatal error while parsing aws profile collection");
1296 goto cleanup;
1297 }
1298
1299 aws_byte_cursor_advance(&current_position, line_cursor.len + 1);
1300 ++context.current_line_number;
1301 }
1302 }
1303
1304 return profile_collection;
1305
1306 cleanup:
1307 s_aws_profile_collection_destroy_internal(profile_collection);
1308
1309 return NULL;
1310 }
1311
1312 struct aws_profile_collection *aws_profile_collection_acquire(struct aws_profile_collection *collection) {
1313 if (collection != NULL) {
1314 aws_ref_count_acquire(&collection->ref_count);
1315 }
1316
1317 return collection;
1318 }
1319
1320 struct aws_profile_collection *aws_profile_collection_release(struct aws_profile_collection *collection) {
1321 if (collection != NULL) {
1322 aws_ref_count_release(&collection->ref_count);
1323 }
1324
1325 return NULL;
1326 }
1327
1328 struct aws_profile_collection *aws_profile_collection_new_from_file(
1329 struct aws_allocator *allocator,
1330 const struct aws_string *file_path,
1331 enum aws_profile_source_type source) {
1332
1333 struct aws_byte_buf file_contents;
1334 AWS_ZERO_STRUCT(file_contents);
1335
1336 AWS_LOGF_DEBUG(AWS_LS_SDKUTILS_PROFILE, "Creating profile collection from file at \"%s\"", file_path->bytes);
1337
1338 if (aws_byte_buf_init_from_file(&file_contents, allocator, aws_string_c_str(file_path)) != 0) {
1339 AWS_LOGF_DEBUG(AWS_LS_SDKUTILS_PROFILE, "Failed to read file at \"%s\"", file_path->bytes);
1340 return NULL;
1341 }
1342
1343 struct aws_profile_collection *profile_collection =
1344 s_aws_profile_collection_new_internal(allocator, &file_contents, source, file_path);
1345
1346 aws_byte_buf_clean_up(&file_contents);
1347
1348 return profile_collection;
1349 }
1350
1351 struct aws_profile_collection *aws_profile_collection_new_from_buffer(
1352 struct aws_allocator *allocator,
1353 const struct aws_byte_buf *buffer,
1354 enum aws_profile_source_type source) {
1355
1356 return s_aws_profile_collection_new_internal(allocator, buffer, source, NULL);
1357 }
1358
1359 static struct aws_string *s_process_profile_file_path(struct aws_allocator *allocator, const struct aws_string *path) {
1360 struct aws_string *final_path = NULL;
1361
1362 /*
1363 * Make a copy to mess with
1364 */
1365 struct aws_string *path_copy = aws_string_new_from_string(allocator, path);
1366 if (path_copy == NULL) {
1367 return NULL;
1368 }
1369
1370 struct aws_string *home_directory = NULL;
1371
1372 /*
1373 * Fake directory cursor for final directory construction
1374 */
1375 char local_platform_separator = aws_get_platform_directory_separator();
1376 struct aws_byte_cursor separator_cursor;
1377 AWS_ZERO_STRUCT(separator_cursor);
1378 separator_cursor.ptr = (uint8_t *)&local_platform_separator;
1379 separator_cursor.len = 1;
1380
1381 for (size_t i = 0; i < path_copy->len; ++i) {
1382 char value = path_copy->bytes[i];
1383 if (aws_is_any_directory_separator(value)) {
1384 ((char *)(path_copy->bytes))[i] = local_platform_separator;
1385 }
1386 }
1387
1388 /*
1389 * Process a split on the local separator, which we now know is the only one present in the string.
1390 *
1391 * While this does not conform fully to the SEP governing profile file path resolution, it covers
1392 * a useful, cross-platform subset of functionality that the full implementation will be backwards compatible with.
1393 */
1394 struct aws_array_list path_segments;
1395 if (aws_array_list_init_dynamic(&path_segments, allocator, 10, sizeof(struct aws_byte_cursor))) {
1396 goto on_array_list_init_failure;
1397 }
1398
1399 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_string(path_copy);
1400 if (aws_byte_cursor_split_on_char(&path_cursor, local_platform_separator, &path_segments)) {
1401 goto on_split_failure;
1402 }
1403
1404 size_t final_string_length = 0;
1405 size_t path_segment_count = aws_array_list_length(&path_segments);
1406 for (size_t i = 0; i < path_segment_count; ++i) {
1407 struct aws_byte_cursor segment_cursor;
1408 AWS_ZERO_STRUCT(segment_cursor);
1409
1410 if (aws_array_list_get_at(&path_segments, &segment_cursor, i)) {
1411 continue;
1412 }
1413
1414 /*
1415 * Current support: if and only if the first segment is just '~' then replace it
1416 * with the current home directory based on SEP home directory resolution rules.
1417 *
1418 * Support for (pathological but proper) paths with embedded ~ ("../../~/etc...") and
1419 * cross-user ~ ("~someone/.aws/credentials") can come later. As it stands, they will
1420 * potentially succeed on unix platforms but not Windows.
1421 */
1422 if (i == 0 && segment_cursor.len == 1 && *segment_cursor.ptr == '~') {
1423 if (home_directory == NULL) {
1424 home_directory = aws_get_home_directory(allocator);
1425
1426 if (AWS_UNLIKELY(!home_directory)) {
1427 goto on_empty_path;
1428 }
1429 }
1430
1431 final_string_length += home_directory->len;
1432 } else {
1433 final_string_length += segment_cursor.len;
1434 }
1435 }
1436
1437 if (path_segment_count > 1) {
1438 final_string_length += path_segment_count - 1;
1439 }
1440
1441 if (final_string_length == 0) {
1442 goto on_empty_path;
1443 }
1444
1445 /*
1446 * Build the final path from the split + a possible home directory resolution
1447 */
1448 struct aws_byte_buf result;
1449 aws_byte_buf_init(&result, allocator, final_string_length);
1450 for (size_t i = 0; i < path_segment_count; ++i) {
1451 struct aws_byte_cursor segment_cursor;
1452 AWS_ZERO_STRUCT(segment_cursor);
1453
1454 if (aws_array_list_get_at(&path_segments, &segment_cursor, i)) {
1455 continue;
1456 }
1457
1458 /*
1459 * See above for explanation
1460 */
1461 if (i == 0 && segment_cursor.len == 1 && *segment_cursor.ptr == '~') {
1462 if (home_directory == NULL) {
1463 goto on_home_directory_failure;
1464 }
1465 struct aws_byte_cursor home_cursor = aws_byte_cursor_from_string(home_directory);
1466 if (aws_byte_buf_append(&result, &home_cursor)) {
1467 goto on_byte_buf_write_failure;
1468 }
1469 } else {
1470 if (aws_byte_buf_append(&result, &segment_cursor)) {
1471 goto on_byte_buf_write_failure;
1472 }
1473 }
1474
1475 /*
1476 * Add the separator after all but the last segment
1477 */
1478 if (i + 1 < path_segment_count) {
1479 if (aws_byte_buf_append(&result, &separator_cursor)) {
1480 goto on_byte_buf_write_failure;
1481 }
1482 }
1483 }
1484
1485 final_path = aws_string_new_from_array(allocator, result.buffer, result.len);
1486
1487 /*
1488 * clean up
1489 */
1490 on_byte_buf_write_failure:
1491 aws_byte_buf_clean_up(&result);
1492
1493 on_empty_path:
1494 on_home_directory_failure:
1495 on_split_failure:
1496 aws_array_list_clean_up(&path_segments);
1497
1498 on_array_list_init_failure:
1499 aws_string_destroy(path_copy);
1500
1501 if (home_directory != NULL) {
1502 aws_string_destroy(home_directory);
1503 }
1504
1505 return final_path;
1506 }
1507
1508 AWS_STATIC_STRING_FROM_LITERAL(s_default_credentials_path, "~/.aws/credentials");
1509 AWS_STATIC_STRING_FROM_LITERAL(s_credentials_file_path_env_variable_name, "AWS_SHARED_CREDENTIALS_FILE");
1510
1511 AWS_STATIC_STRING_FROM_LITERAL(s_default_config_path, "~/.aws/config");
1512 AWS_STATIC_STRING_FROM_LITERAL(s_config_file_path_env_variable_name, "AWS_CONFIG_FILE");
1513
1514 static struct aws_string *s_get_raw_file_path(
1515 struct aws_allocator *allocator,
1516 const struct aws_byte_cursor *override_path,
1517 const struct aws_string *override_env_var_name,
1518 const struct aws_string *default_path) {
1519
1520 if (override_path != NULL && override_path->ptr != NULL) {
1521 return aws_string_new_from_array(allocator, override_path->ptr, override_path->len);
1522 }
1523
1524 struct aws_string *env_override_path = NULL;
1525 if (aws_get_environment_value(allocator, override_env_var_name, &env_override_path) == 0 &&
1526 env_override_path != NULL) {
1527 return env_override_path;
1528 }
1529
1530 return aws_string_new_from_string(allocator, default_path);
1531 }
1532
1533 struct aws_string *aws_get_credentials_file_path(
1534 struct aws_allocator *allocator,
1535 const struct aws_byte_cursor *override_path) {
1536
1537 struct aws_string *raw_path = s_get_raw_file_path(
1538 allocator, override_path, s_credentials_file_path_env_variable_name, s_default_credentials_path);
1539
1540 struct aws_string *final_path = s_process_profile_file_path(allocator, raw_path);
1541
1542 aws_string_destroy(raw_path);
1543
1544 return final_path;
1545 }
1546
1547 struct aws_string *aws_get_config_file_path(
1548 struct aws_allocator *allocator,
1549 const struct aws_byte_cursor *override_path) {
1550
1551 struct aws_string *raw_path =
1552 s_get_raw_file_path(allocator, override_path, s_config_file_path_env_variable_name, s_default_config_path);
1553
1554 struct aws_string *final_path = s_process_profile_file_path(allocator, raw_path);
1555
1556 aws_string_destroy(raw_path);
1557
1558 return final_path;
1559 }
1560
1561 AWS_STATIC_STRING_FROM_LITERAL(s_default_profile_env_variable_name, "AWS_PROFILE");
1562
1563 struct aws_string *aws_get_profile_name(struct aws_allocator *allocator, const struct aws_byte_cursor *override_name) {
1564 /**
1565 * Profile name is resolved in the following order.
1566 * 1. If the override_path variable is provided.
1567 * 2. Check `AWS_PROFILE` environment variable and use the value if it is not empty.
1568 * 3. Use "default". */
1569 struct aws_string *profile_name = NULL;
1570 if (override_name != NULL && override_name->ptr != NULL) {
1571 profile_name = aws_string_new_from_array(allocator, override_name->ptr, override_name->len);
1572 } else {
1573 /* Try to fetch profile from AWS_PROFILE environment variable */
1574 aws_get_environment_value(allocator, s_default_profile_env_variable_name, &profile_name);
1575 /* Use default profile if it doesn't exist. */
1576 if (profile_name == NULL) {
1577 profile_name = aws_string_new_from_string(allocator, s_default_profile_name);
1578 }
1579 }
1580
1581 return profile_name;
1582 }
1583
1584 size_t aws_profile_get_property_count(const struct aws_profile *profile) {
1585 return aws_hash_table_get_entry_count(&profile->properties);
1586 }
1587
1588 size_t aws_profile_collection_get_profile_count(const struct aws_profile_collection *profile_collection) {
1589 return aws_hash_table_get_entry_count(&profile_collection->sections[AWS_PROFILE_SECTION_TYPE_PROFILE]);
1590 }
1591
1592 size_t aws_profile_collection_get_section_count(
1593 const struct aws_profile_collection *profile_collection,
1594 const enum aws_profile_section_type section_type) {
1595 return aws_hash_table_get_entry_count(&profile_collection->sections[section_type]);
1596 }
1597
1598 size_t aws_profile_property_get_sub_property_count(const struct aws_profile_property *property) {
1599 return aws_hash_table_get_entry_count(&property->sub_properties);
1600 }
1601
1602 const struct aws_string *aws_profile_property_get_sub_property(
1603 const struct aws_profile_property *property,
1604 const struct aws_string *sub_property_name) {
1605 struct aws_hash_element *element = NULL;
1606
1607 if (aws_hash_table_find(&property->sub_properties, sub_property_name, &element) || element == NULL) {
1608 return NULL;
1609 }
1610
1611 return (const struct aws_string *)element->value;
1612 }
1613
1614 const struct aws_string *aws_profile_get_name(const struct aws_profile *profile) {
1615 AWS_PRECONDITION(profile);
1616 return profile->name;
1617 }