Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProcessProvider to ProfileProvider #245

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions source/credentials_provider_default_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
struct aws_tls_ctx *tls_ctx = NULL;
struct aws_credentials_provider *environment_provider = NULL;
struct aws_credentials_provider *profile_provider = NULL;
struct aws_credentials_provider *process_provider = NULL;
struct aws_credentials_provider *sts_provider = NULL;
struct aws_credentials_provider *ecs_or_imds_provider = NULL;
struct aws_credentials_provider *chain_provider = NULL;
Expand Down Expand Up @@ -263,7 +262,7 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
#endif /* BYO_CRYPTO */
}

enum { providers_size = 5 };
enum { providers_size = 4 };
struct aws_credentials_provider *providers[providers_size];
AWS_ZERO_ARRAY(providers);
size_t index = 0;
Expand Down Expand Up @@ -309,18 +308,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
aws_atomic_fetch_add(&impl->shutdowns_remaining, 1);
}

struct aws_credentials_provider_process_options process_options;
AWS_ZERO_STRUCT(process_options);
process_options.shutdown_options = sub_provider_shutdown_options;
process_options.config_profile_collection_cached = options->profile_collection_cached;
process_options.profile_to_use = options->profile_name_override;
process_provider = aws_credentials_provider_new_process(allocator, &process_options);
if (process_provider != NULL) {
providers[index++] = process_provider;
/* 1 shutdown call from the process provider's shutdown */
aws_atomic_fetch_add(&impl->shutdowns_remaining, 1);
}

/* Providers that will always make a network call unless explicitly disabled... */

ecs_or_imds_provider = s_aws_credentials_provider_new_ecs_or_imds(
Expand Down Expand Up @@ -348,7 +335,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
*/
aws_credentials_provider_release(environment_provider);
aws_credentials_provider_release(profile_provider);
aws_credentials_provider_release(process_provider);
aws_credentials_provider_release(sts_provider);
aws_credentials_provider_release(ecs_or_imds_provider);

Expand Down Expand Up @@ -390,7 +376,6 @@ struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
} else {
aws_credentials_provider_release(ecs_or_imds_provider);
aws_credentials_provider_release(profile_provider);
aws_credentials_provider_release(process_provider);
aws_credentials_provider_release(sts_provider);
aws_credentials_provider_release(environment_provider);
}
Expand Down
24 changes: 24 additions & 0 deletions source/credentials_provider_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ AWS_STRING_FROM_LITERAL(s_credential_source_name, "credential_source");
AWS_STRING_FROM_LITERAL(s_source_profile_name, "source_profile");
AWS_STRING_FROM_LITERAL(s_access_key_id_profile_var, "aws_access_key_id");
AWS_STRING_FROM_LITERAL(s_secret_access_key_profile_var, "aws_secret_access_key");
AWS_STATIC_STRING_FROM_LITERAL(s_credentials_process, "credential_process");

static struct aws_byte_cursor s_default_session_name_pfx =
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("aws-common-runtime-profile-config");
Expand Down Expand Up @@ -221,6 +222,22 @@ static struct aws_credentials_provider *s_create_profile_based_provider(
return provider;
}

static struct aws_credentials_provider *s_create_process_based_provider(
struct aws_allocator *allocator,
const struct aws_string *profile_name,
struct aws_profile_collection *profile_collection) {
AWS_LOGF_INFO(
AWS_LS_AUTH_CREDENTIALS_PROVIDER,
"static: profile %s attempting to create process-based credentials provider",
aws_string_c_str(profile_name));

struct aws_credentials_provider_process_options options = {
.profile_to_use = aws_byte_cursor_from_string(profile_name),
.config_profile_collection_cached = profile_collection,
};
return aws_credentials_provider_new_process(allocator, &options);
}

static struct aws_credentials_provider *s_credentials_provider_new_profile_internal(
struct aws_allocator *allocator,
const struct aws_credentials_provider_profile_options *options,
Expand Down Expand Up @@ -492,6 +509,7 @@ static struct aws_credentials_provider *s_credentials_provider_new_profile_inter
aws_string_c_str(profile_name));
goto on_finished;
}
/* check if sts provider is applicable */
const struct aws_profile_property *role_arn_property = aws_profile_get_property(profile, s_role_arn_name);
bool profile_contains_access_key = aws_profile_get_property(profile, s_access_key_id_profile_var) != NULL;
bool profile_contains_secret_access_key =
Expand All @@ -513,9 +531,15 @@ static struct aws_credentials_provider *s_credentials_provider_new_profile_inter
}

aws_hash_table_put(source_profiles_table, (void *)aws_string_c_str(profile_name), NULL, 0);

/* check if process provider is applicable */
const struct aws_profile_property *process_property = aws_profile_get_property(profile, s_credentials_process);

if (role_arn_property && (first_profile_in_chain || !profile_contains_credentials)) {
provider = s_create_sts_based_provider(
allocator, role_arn_property, profile, options, merged_profiles, source_profiles_table);
} else if (process_property) {
provider = s_create_process_based_provider(allocator, profile_name, merged_profiles);
} else {
provider = s_create_profile_based_provider(
allocator, credentials_file_path, config_file_path, profile_name, options->profile_collection_cached);
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ add_test_case(credentials_provider_process_new_failed)
add_test_case(credentials_provider_process_bad_command)
add_test_case(credentials_provider_process_incorrect_command_output)
add_test_case(credentials_provider_process_basic_success)
add_test_case(credentials_provider_process_basic_success_from_profile_provider)
add_test_case(credentials_provider_process_basic_success_cached)

add_net_test_case(credentials_provider_cognito_new_destroy)
Expand Down
48 changes: 48 additions & 0 deletions tests/credentials_provider_process_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,54 @@ static int s_credentials_provider_process_basic_success(struct aws_allocator *al
}
AWS_TEST_CASE(credentials_provider_process_basic_success, s_credentials_provider_process_basic_success);

static int s_credentials_provider_process_basic_success_from_profile_provider(
struct aws_allocator *allocator,
void *ctx) {
(void)ctx;

s_aws_process_tester_init(allocator);

struct aws_byte_buf content_buf;
struct aws_byte_buf existing_content = aws_byte_buf_from_c_str(aws_string_c_str(s_process_config_file_contents));
aws_byte_buf_init_copy(&content_buf, allocator, &existing_content);
struct aws_byte_cursor cursor = aws_byte_cursor_from_string(s_test_command);
ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);
cursor = aws_byte_cursor_from_c_str("\n");
ASSERT_TRUE(aws_byte_buf_append_dynamic(&content_buf, &cursor) == AWS_OP_SUCCESS);

struct aws_string *config_file_contents = aws_string_new_from_array(allocator, content_buf.buffer, content_buf.len);
ASSERT_TRUE(config_file_contents != NULL);
aws_byte_buf_clean_up(&content_buf);

s_aws_process_test_init_config_profile(allocator, config_file_contents);
aws_string_destroy(config_file_contents);

struct aws_credentials_provider_profile_options options = {
.shutdown_options =
{
.shutdown_callback = s_on_shutdown_complete,
.shutdown_user_data = NULL,
},
.profile_name_override = aws_byte_cursor_from_string(s_credentials_process_profile),
};
struct aws_credentials_provider *provider = aws_credentials_provider_new_profile(allocator, &options);

aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL);

s_aws_wait_for_credentials_result();

ASSERT_TRUE(s_tester.has_received_credentials_callback == true);
ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials));

aws_credentials_provider_release(provider);
s_aws_wait_for_provider_shutdown_callback();
s_aws_process_tester_cleanup();
return 0;
}
AWS_TEST_CASE(
credentials_provider_process_basic_success_from_profile_provider,
s_credentials_provider_process_basic_success_from_profile_provider);

static int s_credentials_provider_process_basic_success_cached(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

Expand Down
Loading