Debian Codegraph @

crt/aws-c-auth/source/credentials_provider_sso.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_http_credentials_provider.h>
9 #include <aws/auth/private/aws_profile.h>
10 #include <aws/auth/private/credentials_utils.h>
11 #include <aws/auth/private/sso_token_providers.h>
12 #include <aws/auth/private/sso_token_utils.h>
13
14 #include <aws/common/clock.h>
15 #include <aws/common/uri.h>
16 #include <aws/io/socket.h>
17
18 #if defined(_MSC_VER)
19 # pragma warning(disable : 4204)
20 #endif /* _MSC_VER */
21
22 AWS_STATIC_STRING_FROM_LITERAL(s_sso_service_host_prefix, "portal.sso");
23 AWS_STATIC_STRING_FROM_LITERAL(s_sso_service_name, "sso");
24 AWS_STATIC_STRING_FROM_LITERAL(s_sso_service_env_name, "SSO");
25
26 AWS_STATIC_STRING_FROM_LITERAL(s_sso_account_id, "sso_account_id");
27 AWS_STATIC_STRING_FROM_LITERAL(s_sso_region, "sso_region");
28 AWS_STATIC_STRING_FROM_LITERAL(s_sso_role_name, "sso_role_name");
29 AWS_STATIC_STRING_FROM_LITERAL(s_sso_session, "sso_session");
30
31 struct sso_parameters {
32 struct aws_allocator *allocator;
33 struct aws_string *endpoint;
34 struct aws_string *sso_account_id;
35 struct aws_string *sso_role_name;
36 struct aws_credentials_provider *token_provider;
37 };
38
39 struct sso_request_data {
40 struct aws_string *token;
41 };
42
43 static void s_parameters_destroy(void *parameters) {
44 if (!parameters) {
45 return;
46 }
47 struct sso_parameters *sso_parameters = parameters;
48 aws_string_destroy(sso_parameters->endpoint);
49 aws_string_destroy(sso_parameters->sso_account_id);
50 aws_string_destroy(sso_parameters->sso_role_name);
51 aws_credentials_provider_release(sso_parameters->token_provider);
52 aws_mem_release(sso_parameters->allocator, sso_parameters);
53 }
54
55 /**
56 * Read the config file and construct profile or sso_session token provider based on sso_session property.
57 *
58 * If the profile contains sso_session property, a valid config example is as follow.
59 * [profile sso-profile]
60 * sso_session = dev
61 * sso_account_id = 012345678901
62 * sso_role_name = SampleRole
63 *
64 * [sso-session dev]
65 * sso_region = us-east-1
66 * sso_start_url = https://d-abc123.awsapps.com/start
67 *
68 * If the profile does't contains sso_session, the legacy valid config example is as follow.
69 * [profile sso-profile]
70 * sso_account_id = 012345678901
71 * sso_region = us-east-1
72 * sso_role_name = SampleRole
73 * sso_start_url = https://d-abc123.awsapps.com/start-beta
74 */
75 static struct sso_parameters *s_parameters_new(
76 struct aws_allocator *allocator,
77 const struct aws_credentials_provider_sso_options *options) {
78
79 struct sso_parameters *parameters = aws_mem_calloc(allocator, 1, sizeof(struct sso_parameters));
80 parameters->allocator = allocator;
81
82 struct aws_profile_collection *config_profile_collection = NULL;
83 struct aws_string *profile_name = NULL;
84 bool success = false;
85
86 profile_name = aws_get_profile_name(allocator, &options->profile_name_override);
87 if (!profile_name) {
88 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: failed to resolve profile name");
89 goto on_finish;
90 }
91 if (options->config_file_cached) {
92 /* Use cached config file */
93 config_profile_collection = aws_profile_collection_acquire(options->config_file_cached);
94 } else {
95 /* load config file */
96 config_profile_collection =
97 aws_load_profile_collection_from_config_file(allocator, options->config_file_name_override);
98 }
99
100 if (!config_profile_collection) {
101 goto on_finish;
102 }
103
104 const struct aws_profile *profile = aws_profile_collection_get_profile(config_profile_collection, profile_name);
105 if (!profile) {
106 AWS_LOGF_ERROR(
107 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: failed to load \"%s\" profile", aws_string_c_str(profile_name));
108 goto on_finish;
109 }
110
111 const struct aws_profile_property *sso_account_id = aws_profile_get_property(profile, s_sso_account_id);
112 const struct aws_profile_property *sso_role_name = aws_profile_get_property(profile, s_sso_role_name);
113 const struct aws_profile_property *sso_region = NULL;
114
115 if (!sso_account_id) {
116 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: sso_account_id is missing");
117 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE);
118 goto on_finish;
119 }
120 if (!sso_role_name) {
121 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: sso_role_name is missing");
122 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE);
123 goto on_finish;
124 }
125
126 const struct aws_profile_property *sso_session_property = aws_profile_get_property(profile, s_sso_session);
127 /* create the appropriate token provider based on sso_session property is available or not */
128 if (sso_session_property) {
129 /* construct sso_session token provider */
130 struct aws_token_provider_sso_session_options token_provider_options = {
131 .config_file_name_override = options->config_file_name_override,
132 .config_file_cached = config_profile_collection,
133 .profile_name_override = options->profile_name_override,
134 .bootstrap = options->bootstrap,
135 .tls_ctx = options->tls_ctx,
136 .system_clock_fn = options->system_clock_fn,
137 };
138 parameters->token_provider = aws_token_provider_new_sso_session(allocator, &token_provider_options);
139 if (!parameters->token_provider) {
140 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: unable to create a sso token provider");
141 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE);
142 goto on_finish;
143 }
144 sso_region = aws_profile_get_property(
145 aws_profile_collection_get_section(
146 config_profile_collection,
147 AWS_PROFILE_SECTION_TYPE_SSO_SESSION,
148 aws_profile_property_get_value(sso_session_property)),
149 s_sso_region);
150 } else {
151 /* construct profile token provider */
152 struct aws_token_provider_sso_profile_options token_provider_options = {
153 .config_file_name_override = options->config_file_name_override,
154 .config_file_cached = config_profile_collection,
155 .profile_name_override = options->profile_name_override,
156 .system_clock_fn = options->system_clock_fn,
157 };
158
159 parameters->token_provider = aws_token_provider_new_sso_profile(allocator, &token_provider_options);
160 if (!parameters->token_provider) {
161 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: unable to create a profile token provider");
162 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE);
163 goto on_finish;
164 }
165 sso_region = aws_profile_get_property(profile, s_sso_region);
166 }
167
168 if (!sso_region) {
169 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso: sso_region is missing");
170 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE);
171 goto on_finish;
172 }
173
174 parameters->sso_account_id = aws_string_new_from_string(allocator, aws_profile_property_get_value(sso_account_id));
175 parameters->sso_role_name = aws_string_new_from_string(allocator, aws_profile_property_get_value(sso_role_name));
176 /* determine endpoint */
177 if (aws_credentials_provider_construct_endpoint(
178 allocator,
179 &parameters->endpoint,
180 aws_profile_property_get_value(sso_region),
181 s_sso_service_host_prefix,
182 s_sso_service_env_name,
183 s_sso_service_name,
184 config_profile_collection,
185 profile,
186 NULL,
187 false)) {
188 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to construct sso endpoint");
189 goto on_finish;
190 }
191 AWS_LOGF_DEBUG(
192 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Successfully loaded all required parameters for sso credentials provider.");
193 success = true;
194
195 on_finish:
196 if (!success) {
197 s_parameters_destroy(parameters);
198 parameters = NULL;
199 }
200 aws_string_destroy(profile_name);
201 aws_profile_collection_release(config_profile_collection);
202
203 return parameters;
204 }
205
206 /* Request headers. */
207 AWS_STATIC_STRING_FROM_LITERAL(s_sso_token_header, "x-amz-sso_bearer_token");
208 AWS_STATIC_STRING_FROM_LITERAL(s_sso_user_agent_header, "User-Agent");
209 AWS_STATIC_STRING_FROM_LITERAL(s_sso_user_agent_header_value, "aws-sdk-crt/sso-credentials-provider");
210
211 static void s_on_get_token_callback(struct aws_credentials *credentials, int error_code, void *query_context) {
212 struct aws_http_query_context *http_query_context = query_context;
213 struct sso_parameters *parameters = http_query_context->parameters;
214
215 if (error_code) {
216 AWS_LOGF_ERROR(
217 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
218 "id=%p: failed to acquire a token, error code %d(%s)",
219 (void *)http_query_context->provider,
220 error_code,
221 aws_error_str(error_code));
222 http_query_context->error_code = error_code;
223 return;
224 }
225
226 struct aws_byte_cursor token = aws_credentials_get_token(credentials);
227 AWS_LOGF_INFO(
228 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
229 "(id=%p): successfully acquired a token",
230 (void *)http_query_context->provider);
231
232 struct sso_request_data *sso_request_data = http_query_context->request_data;
233 sso_request_data->token = aws_string_new_from_cursor(http_query_context->allocator, &token);
234
235 struct aws_byte_cursor account_id_cursor = aws_byte_cursor_from_string(parameters->sso_account_id);
236 struct aws_byte_cursor role_name_cursor = aws_byte_cursor_from_string(parameters->sso_role_name);
237 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_c_str("/federation/credentials?account_id=");
238 struct aws_byte_cursor role_name_param_cursor = aws_byte_cursor_from_c_str("&role_name=");
239 if (aws_byte_buf_init_copy_from_cursor(
240 &http_query_context->path_and_query, http_query_context->provider->allocator, path_cursor) ||
241 aws_byte_buf_append_encoding_uri_param(&http_query_context->path_and_query, &account_id_cursor) ||
242 aws_byte_buf_append_dynamic(&http_query_context->path_and_query, &role_name_param_cursor) ||
243 aws_byte_buf_append_encoding_uri_param(&http_query_context->path_and_query, &role_name_cursor)) {
244 goto on_error;
245 }
246
247 struct aws_http_header host_header = {
248 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
249 .value = aws_byte_cursor_from_string(parameters->endpoint),
250 };
251
252 if (aws_http_message_add_header(http_query_context->request, host_header)) {
253 AWS_LOGF_ERROR(
254 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
255 "(id=%p) failed to add http header with error: %s",
256 (void *)http_query_context->provider,
257 aws_error_debug_str(aws_last_error()));
258 goto on_error;
259 }
260
261 struct aws_http_header auth_header = {
262 .name = aws_byte_cursor_from_string(s_sso_token_header),
263 .value = aws_byte_cursor_from_string(sso_request_data->token),
264 };
265 struct aws_http_header user_agent_header = {
266 .name = aws_byte_cursor_from_string(s_sso_user_agent_header),
267 .value = aws_byte_cursor_from_string(s_sso_user_agent_header_value),
268 };
269
270 if (aws_http_message_add_header(http_query_context->request, auth_header) ||
271 aws_http_message_add_header(http_query_context->request, user_agent_header)) {
272 goto on_error;
273 }
274
275 if (aws_http_message_set_request_method(http_query_context->request, aws_http_method_get)) {
276 AWS_LOGF_ERROR(
277 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
278 "(id=%p) failed to set request method with error: %s",
279 (void *)http_query_context->provider,
280 aws_error_debug_str(aws_last_error()));
281 goto on_error;
282 }
283
284 if (aws_http_message_set_request_path(
285 http_query_context->request, aws_byte_cursor_from_buf(&http_query_context->path_and_query))) {
286 AWS_LOGF_ERROR(
287 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
288 "(id=%p) failed to set request path with error: %s",
289 (void *)http_query_context->provider,
290 aws_error_debug_str(aws_last_error()));
291 goto on_error;
292 }
293
294 return;
295 on_error:
296 http_query_context->error_code = aws_last_error();
297 }
298
299 static int s_sso_create_request_fn(struct aws_http_query_context *http_query_context, void *user_data) {
300 struct sso_parameters *parameters = http_query_context->parameters;
301
302 if (aws_credentials_provider_get_credentials(parameters->token_provider, s_on_get_token_callback, user_data)) {
303 int last_error_code = aws_last_error();
304 AWS_LOGF_ERROR(
305 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
306 "id=%p: failed to get a token, error code %d(%s)",
307 (void *)http_query_context->provider,
308 last_error_code,
309 aws_error_str(last_error_code));
310
311 http_query_context->error_code = last_error_code;
312 return AWS_OP_ERR;
313 }
314 return AWS_OP_SUCCESS;
315 }
316
317 static void s_sso_finalize_credentials_fn(struct aws_http_query_context *http_query_context) {
318 struct sso_parameters *parameters = http_query_context->parameters;
319 struct aws_credentials *credentials = NULL;
320 struct aws_credentials *creds_with_accound_id = NULL;
321
322 if (http_query_context->error_code == AWS_ERROR_SUCCESS) {
323 /* parse credentials */
324 struct aws_parse_credentials_from_json_doc_options parse_options = {
325 .access_key_id_name = "accessKeyId",
326 .secret_access_key_name = "secretAccessKey",
327 .token_name = "sessionToken",
328 .expiration_name = "expiration",
329 .top_level_object_name = "roleCredentials",
330 .token_required = true,
331 .expiration_required = true,
332 .expiration_format = AWS_PCEF_NUMBER_UNIX_EPOCH_MS,
333 };
334
335 credentials = aws_parse_credentials_from_json_document(
336 http_query_context->allocator, aws_byte_cursor_from_buf(&http_query_context->payload), &parse_options);
337 }
338
339 if (!credentials) {
340 AWS_LOGF_ERROR(
341 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
342 "(id=%p) failed to query credentials",
343 (void *)http_query_context->provider);
344
345 if (http_query_context->error_code == AWS_ERROR_SUCCESS) {
346 http_query_context->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_SSO_SOURCE_FAILURE;
347 }
348 } else {
349 AWS_LOGF_INFO(
350 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
351 "(id=%p) successfully queried credentials",
352 (void *)http_query_context->provider);
353 struct aws_credentials_options creds_option = {
354 .access_key_id_cursor = aws_credentials_get_access_key_id(credentials),
355 .secret_access_key_cursor = aws_credentials_get_secret_access_key(credentials),
356 .session_token_cursor = aws_credentials_get_session_token(credentials),
357 .account_id_cursor = aws_byte_cursor_from_string(parameters->sso_account_id),
358 .expiration_timepoint_seconds = aws_credentials_get_expiration_timepoint_seconds(credentials),
359 };
360
361 creds_with_accound_id = aws_credentials_new_with_options(http_query_context->allocator, &creds_option);
362 }
363
364 /* pass the credentials back */
365 http_query_context->original_callback(
366 creds_with_accound_id, http_query_context->error_code, http_query_context->original_user_data);
367
368 /* clean up */
369 aws_credentials_release(credentials);
370 aws_credentials_release(creds_with_accound_id);
371 }
372
373 static void s_sso_request_data_create(struct aws_http_query_context *query_context) {
374 struct sso_request_data *request_data =
375 aws_mem_acquire(query_context->allocator, sizeof(struct sso_request_data *));
376 AWS_ZERO_STRUCT(*request_data);
377 query_context->request_data = request_data;
378 }
379
380 static void s_sso_request_data_reset(struct aws_http_query_context *query_context) {
381 struct sso_request_data *sso_request_data = query_context->request_data;
382 if (sso_request_data->token) {
383 aws_string_destroy_secure(sso_request_data->token);
384 sso_request_data->token = NULL;
385 }
386 }
387
388 static struct aws_http_credentials_provider_request_vtable s_sso_request_vtable = {
389 .clean_up_parameters_fn = s_parameters_destroy,
390 .finalize_credentials_fn = s_sso_finalize_credentials_fn,
391 .create_request_fn = s_sso_create_request_fn,
392 .create_request_data_fn = s_sso_request_data_create,
393 .reset_request_data_fn = s_sso_request_data_reset,
394 };
395
396 struct aws_credentials_provider *aws_credentials_provider_new_sso(
397 struct aws_allocator *allocator,
398 const struct aws_credentials_provider_sso_options *options) {
399
400 struct sso_parameters *parameters = s_parameters_new(allocator, options);
401 if (!parameters) {
402 return NULL;
403 }
404
405 struct aws_credentials_provider *provider = NULL;
406 struct aws_credentials_provider_http_options *http_options = NULL;
407 struct aws_http_credentials_provider_user_data *sso_user_data = NULL;
408
409 aws_mem_acquire_many(
410 allocator,
411 3,
412 &provider,
413 sizeof(struct aws_credentials_provider),
414 &http_options,
415 sizeof(struct aws_credentials_provider_http_options),
416 &sso_user_data,
417 sizeof(struct aws_http_credentials_provider_user_data));
418
419 AWS_ZERO_STRUCT(*http_options);
420 AWS_ZERO_STRUCT(*provider);
421 AWS_ZERO_STRUCT(*sso_user_data);
422
423 http_options->shutdown_options = options->shutdown_options;
424 http_options->bootstrap = options->bootstrap;
425 http_options->tls_ctx = options->tls_ctx;
426 http_options->function_table = options->function_table;
427 http_options->endpoint = aws_byte_cursor_from_string(parameters->endpoint);
428 http_options->max_connections = 2;
429 http_options->proxy_ev_settings = options->proxy_ev_settings;
430
431 sso_user_data->parameters = parameters;
432 sso_user_data->request_vtable = &s_sso_request_vtable;
433
434 if (aws_http_credentials_provider_init_base(allocator, provider, http_options, sso_user_data)) {
435 goto on_error;
436 }
437
438 return provider;
439 on_error:
440 aws_credentials_provider_destroy(provider);
441 return NULL;
442 }