Debian Codegraph @

crt/aws-c-auth/tests/credentials_utils_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/auth/private/credentials_utils.h>
7 #include <aws/common/environment.h>
8 #include <aws/sdkutils/aws_profile.h>
9 #include <aws/testing/aws_test_harness.h>
10
11 static int s_credentials_utils_construct_endpoint_test(struct aws_allocator *allocator, void *ctx) {
12 (void)ctx;
13
14 struct aws_string *service_name = aws_string_new_from_c_str(allocator, "sts");
15 struct aws_string *endpoint;
16 struct aws_string *region;
17
18 region = aws_string_new_from_c_str(allocator, "us-east-2");
19 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
20 allocator, &endpoint, region, service_name, service_name, service_name, NULL, NULL, NULL, false));
21 ASSERT_STR_EQUALS("sts.us-east-2.amazonaws.com", aws_string_c_str(endpoint));
22 aws_string_destroy(endpoint);
23 aws_string_destroy(region);
24
25 region = aws_string_new_from_c_str(allocator, "cn-northwest-1");
26 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
27 allocator, &endpoint, region, service_name, service_name, service_name, NULL, NULL, NULL, false));
28 ASSERT_STR_EQUALS("sts.cn-northwest-1.amazonaws.com.cn", aws_string_c_str(endpoint));
29 aws_string_destroy(endpoint);
30 aws_string_destroy(region);
31
32 region = aws_string_new_from_c_str(allocator, "us-iso-east-1");
33 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
34 allocator, &endpoint, region, service_name, service_name, service_name, NULL, NULL, NULL, false));
35 ASSERT_STR_EQUALS("sts.us-iso-east-1.c2s.ic.gov", aws_string_c_str(endpoint));
36 aws_string_destroy(endpoint);
37 aws_string_destroy(region);
38
39 region = aws_string_new_from_c_str(allocator, "us-isob-east-1");
40 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
41 allocator, &endpoint, region, service_name, service_name, service_name, NULL, NULL, NULL, false));
42 ASSERT_STR_EQUALS("sts.us-isob-east-1.sc2s.sgov.gov", aws_string_c_str(endpoint));
43 aws_string_destroy(endpoint);
44 aws_string_destroy(region);
45
46 region = aws_string_new_from_c_str(allocator, "eu-isoe-west-1");
47 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
48 allocator, &endpoint, region, service_name, service_name, service_name, NULL, NULL, NULL, false));
49 ASSERT_STR_EQUALS("sts.eu-isoe-west-1.cloud.adc-e.uk", aws_string_c_str(endpoint));
50 aws_string_destroy(endpoint);
51 aws_string_destroy(region);
52
53 region = aws_string_new_from_c_str(allocator, "us-isof-south-1");
54 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
55 allocator, &endpoint, region, service_name, service_name, service_name, NULL, NULL, NULL, false));
56 ASSERT_STR_EQUALS("sts.us-isof-south-1.csp.hci.ic.gov", aws_string_c_str(endpoint));
57 aws_string_destroy(endpoint);
58 aws_string_destroy(region);
59
60 aws_string_destroy(service_name);
61
62 return AWS_OP_SUCCESS;
63 }
64
65 AWS_TEST_CASE(credentials_utils_construct_endpoint_test, s_credentials_utils_construct_endpoint_test);
66
67 static int s_credentials_utils_endpoint_override_test(struct aws_allocator *allocator, void *ctx) {
68 (void)ctx;
69
70 struct aws_string *region = aws_string_new_from_c_str(allocator, "us-east-2");
71 struct aws_string *service_name = aws_string_new_from_c_str(allocator, "sts");
72 struct aws_string *service_name_env = aws_string_new_from_c_str(allocator, "STS");
73 struct aws_string *endpoint = NULL;
74
75 /* test service-specific endpoint override */
76 struct aws_string *endpoint_override_env = aws_string_new_from_c_str(allocator, "AWS_ENDPOINT_URL_STS");
77 struct aws_string *endpoint_override = aws_string_new_from_c_str(allocator, "test.endpoint.override.com");
78 aws_set_environment_value(endpoint_override_env, endpoint_override);
79 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
80 allocator, &endpoint, region, service_name, service_name_env, service_name, NULL, NULL, NULL, false));
81 ASSERT_STR_EQUALS(aws_string_c_str(endpoint_override), aws_string_c_str(endpoint));
82 aws_unset_environment_value(endpoint_override_env);
83 aws_string_destroy(endpoint_override);
84 aws_string_destroy(endpoint_override_env);
85 aws_string_destroy(endpoint);
86
87 /* test global endpoint override */
88 struct aws_string *endpoint_override_env_global = aws_string_new_from_c_str(allocator, "AWS_ENDPOINT_URL");
89 endpoint_override = aws_string_new_from_c_str(allocator, "global.endpoint.override.com");
90 aws_set_environment_value(endpoint_override_env_global, endpoint_override);
91 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
92 allocator, &endpoint, region, service_name, service_name_env, service_name, NULL, NULL, NULL, false));
93 ASSERT_STR_EQUALS(aws_string_c_str(endpoint_override), aws_string_c_str(endpoint));
94 aws_unset_environment_value(endpoint_override_env_global);
95 aws_string_destroy(endpoint_override);
96 aws_string_destroy(endpoint_override_env_global);
97 aws_string_destroy(endpoint);
98
99 /* test service-specific config endpoint override */
100 struct aws_byte_cursor service_override_config_contents =
101 aws_byte_cursor_from_c_str("[profile test-profile]\n"
102 "test = 123\n"
103 "services = test-services\n"
104 "[services test-services]\n"
105 "sts =\n"
106 " endpoint_url = test.sts.endpoint.com\n"
107 "[sso-session session]\n");
108 struct aws_byte_buf config_file;
109 AWS_ZERO_STRUCT(config_file);
110 aws_byte_buf_init_copy_from_cursor(&config_file, allocator, service_override_config_contents);
111 struct aws_profile_collection *profile_collection =
112 aws_profile_collection_new_from_buffer(allocator, &config_file, AWS_PST_CONFIG);
113 struct aws_string *profile_name = aws_string_new_from_c_str(allocator, "test-profile");
114 const struct aws_profile *profile = aws_profile_collection_get_profile(profile_collection, profile_name);
115
116 struct aws_string *expected_endpoint = aws_string_new_from_c_str(allocator, "test.sts.endpoint.com");
117
118 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
119 allocator,
120 &endpoint,
121 region,
122 service_name,
123 service_name,
124 service_name,
125 profile_collection,
126 profile,
127 NULL,
128 false));
129 ASSERT_STR_EQUALS(aws_string_c_str(expected_endpoint), aws_string_c_str(endpoint));
130 aws_string_destroy(expected_endpoint);
131 aws_byte_buf_clean_up(&config_file);
132 aws_profile_collection_release(profile_collection);
133 aws_string_destroy(endpoint);
134 aws_string_destroy(profile_name);
135 /* test global config endpoint override */
136 struct aws_byte_cursor global_override_config_contents =
137 aws_byte_cursor_from_c_str("[profile test-profile]\n"
138 "test = 123\n"
139 "endpoint_url = global.sts.endpoint.com\n");
140 AWS_ZERO_STRUCT(config_file);
141 aws_byte_buf_init_copy_from_cursor(&config_file, allocator, global_override_config_contents);
142 profile_collection = aws_profile_collection_new_from_buffer(allocator, &config_file, AWS_PST_CONFIG);
143 profile_name = aws_string_new_from_c_str(allocator, "test-profile");
144 profile = aws_profile_collection_get_profile(profile_collection, profile_name);
145 expected_endpoint = aws_string_new_from_c_str(allocator, "global.sts.endpoint.com");
146
147 ASSERT_SUCCESS(aws_credentials_provider_construct_endpoint(
148 allocator,
149 &endpoint,
150 region,
151 service_name,
152 service_name,
153 service_name,
154 profile_collection,
155 profile,
156 NULL,
157 false));
158 ASSERT_STR_EQUALS(aws_string_c_str(expected_endpoint), aws_string_c_str(endpoint));
159 aws_string_destroy(expected_endpoint);
160 aws_byte_buf_clean_up(&config_file);
161 aws_profile_collection_release(profile_collection);
162 aws_string_destroy(endpoint);
163 aws_string_destroy(profile_name);
164
165 aws_string_destroy(region);
166 aws_string_destroy(service_name);
167 aws_string_destroy(service_name_env);
168 return AWS_OP_SUCCESS;
169 }
170 AWS_TEST_CASE(credentials_utils_endpoint_override_test, s_credentials_utils_endpoint_override_test);
171
172 static int s_credentials_utils_parse_account_id_from_arn(struct aws_allocator *allocator, void *ctx) {
173 (void)ctx;
174 (void)allocator;
175
176 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(
177 aws_parse_account_id_from_arn(aws_byte_cursor_from_c_str("::::123456789012::")), "123456789012");
178 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(
179 aws_parse_account_id_from_arn(aws_byte_cursor_from_c_str("arn:a:b:c:123456789012::")), "123456789012");
180 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(aws_parse_account_id_from_arn(aws_byte_cursor_from_c_str("::::::")), "");
181 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(aws_parse_account_id_from_arn(aws_byte_cursor_from_c_str("invalid-arn")), "");
182 return AWS_OP_SUCCESS;
183 }
184
185 AWS_TEST_CASE(credentials_utils_parse_account_id_from_arn, s_credentials_utils_parse_account_id_from_arn);