Debian Codegraph @

crt/aws-c-auth/source/credentials_provider_login.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 #include <aws/auth/credentials.h>
6 #include <inttypes.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/login_token_utils.h>
12 #include <aws/common/environment.h>
13 #include <aws/io/socket.h>
14 #include <aws/io/stream.h>
15
16 #if defined(_MSC_VER)
17 # pragma warning(disable : 4204)
18 #endif /* _MSC_VER */
19
20 AWS_STATIC_STRING_FROM_LITERAL(s_login_service_host_prefix, "signin");
21 AWS_STATIC_STRING_FROM_LITERAL(s_login_service_name, "signin");
22 AWS_STATIC_STRING_FROM_LITERAL(s_login_service_env_name, "SIGNIN");
23
24 AWS_STATIC_STRING_FROM_LITERAL(s_login_session, "login_session");
25 AWS_STATIC_STRING_FROM_LITERAL(s_login_region, "region");
26 AWS_STATIC_STRING_FROM_LITERAL(s_login_cache_env_var, "AWS_LOGIN_CACHE_DIRECTORY");
27 AWS_STATIC_STRING_FROM_LITERAL(s_login_endpoint, "/v1/token");
28 AWS_STATIC_STRING_FROM_LITERAL(s_scheme_literal, "https://");
29
30 struct login_parameters {
31 struct aws_allocator *allocator;
32 struct aws_string *endpoint;
33 struct aws_string *login_session;
34 struct aws_string *login_region;
35 struct aws_string *login_directory_override;
36 };
37
38 struct login_request_data {
39 struct aws_login_token *token;
40 struct aws_input_stream *body_stream;
41 struct aws_byte_buf underlying_body;
42 };
43
44 static void s_parameters_destroy(void *parameters) {
45 if (!parameters) {
46 return;
47 }
48 struct login_parameters *login_parameters = parameters;
49 aws_string_destroy(login_parameters->endpoint);
50 aws_string_destroy(login_parameters->login_session);
51 aws_string_destroy(login_parameters->login_region);
52 aws_string_destroy(login_parameters->login_directory_override);
53 aws_mem_release(login_parameters->allocator, login_parameters);
54 }
55
56 static struct login_parameters *s_parameters_new(
57 struct aws_allocator *allocator,
58 const struct aws_credentials_provider_login_options *options) {
59
60 struct login_parameters *parameters = aws_mem_calloc(allocator, 1, sizeof(struct login_parameters));
61 parameters->allocator = allocator;
62 bool success = false;
63
64 struct aws_profile_collection *config_profile_collection = NULL;
65 struct aws_string *profile_name = NULL;
66
67 profile_name = aws_get_profile_name(allocator, &options->profile_name_override);
68 if (!profile_name) {
69 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: failed to resolve profile name");
70 goto on_finish;
71 }
72 if (options->config_file_cached) {
73 /* Use cached config file */
74 config_profile_collection = aws_profile_collection_acquire(options->config_file_cached);
75 } else {
76 /* load config file */
77 config_profile_collection =
78 aws_load_profile_collection_from_config_file(allocator, options->config_file_name_override);
79 }
80
81 if (!config_profile_collection) {
82 goto on_finish;
83 }
84
85 const struct aws_profile *profile = aws_profile_collection_get_profile(config_profile_collection, profile_name);
86 if (!profile) {
87 AWS_LOGF_ERROR(
88 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: failed to load \"%s\" profile", aws_string_c_str(profile_name));
89 goto on_finish;
90 }
91
92 if (options->login_session.len > 0) {
93 parameters->login_session = aws_string_new_from_cursor(allocator, &options->login_session);
94 } else {
95 const struct aws_profile_property *login_session = aws_profile_get_property(profile, s_login_session);
96 if (login_session) {
97 parameters->login_session =
98 aws_string_new_from_string(allocator, aws_profile_property_get_value(login_session));
99 }
100 }
101
102 if (!parameters->login_session) {
103 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: login_session is missing");
104 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_SESSION_MISSING);
105 goto on_finish;
106 }
107
108 if (options->login_region.len > 0) {
109 parameters->login_region = aws_string_new_from_cursor(allocator, &options->login_region);
110 } else {
111 const struct aws_profile_property *login_region = aws_profile_get_property(profile, s_login_region);
112 if (login_region) {
113 parameters->login_region =
114 aws_string_new_from_string(allocator, aws_profile_property_get_value(login_region));
115 }
116 }
117
118 if (!parameters->login_region) {
119 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: region is missing");
120 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_REGION_MISSING);
121 goto on_finish;
122 }
123
124 if (options->login_cache_directory_override.len > 0) {
125 parameters->login_directory_override =
126 aws_string_new_from_cursor(allocator, &options->login_cache_directory_override);
127 } else {
128 if (aws_get_environment_value(allocator, s_login_cache_env_var, &parameters->login_directory_override)) {
129 AWS_LOGF_TRACE(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: no override for cache dir found");
130 }
131 }
132
133 const struct aws_byte_cursor suffix_override = aws_byte_cursor_from_c_str("aws.amazon.com");
134 struct aws_string *region = aws_string_new_from_cursor(allocator, &options->login_region);
135
136 /* determine endpoint */
137 if (aws_credentials_provider_construct_endpoint(
138 allocator,
139 &parameters->endpoint,
140 region,
141 s_login_service_host_prefix,
142 s_login_service_env_name,
143 s_login_service_name,
144 config_profile_collection,
145 profile,
146 &suffix_override,
147 true)) {
148 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to construct login endpoint");
149 aws_string_destroy(region);
150 goto on_finish;
151 }
152 AWS_LOGF_DEBUG(
153 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
154 "Successfully loaded all required parameters for login credentials provider.");
155 aws_string_destroy(region);
156 success = true;
157
158 on_finish:
159 if (!success) {
160 s_parameters_destroy(parameters);
161 parameters = NULL;
162 }
163 aws_string_destroy(profile_name);
164 aws_profile_collection_release(config_profile_collection);
165
166 return parameters;
167 }
168
169 static int s_get_jwt_endpoint(
170 struct aws_allocator *allocator,
171 struct login_parameters *parameters,
172 struct aws_byte_cursor *path_cursor,
173 struct aws_byte_buf *endpoint_buf) {
174 struct aws_byte_cursor scheme_cursor = aws_byte_cursor_from_string(s_scheme_literal);
175 struct aws_byte_cursor endpoint_cursor = aws_byte_cursor_from_string(parameters->endpoint);
176 if (aws_byte_buf_init_copy_from_cursor(endpoint_buf, allocator, scheme_cursor) ||
177 aws_byte_buf_append_dynamic(endpoint_buf, &endpoint_cursor) ||
178 aws_byte_buf_append_dynamic(endpoint_buf, path_cursor)) {
179 return AWS_OP_ERR;
180 }
181 return AWS_OP_SUCCESS;
182 }
183
184 int s_login_create_request_fn(struct aws_http_query_context *http_query_context, void *user_data) {
185 (void)user_data;
186 bool success = false;
187 struct aws_byte_buf dpop_header_value;
188 AWS_ZERO_STRUCT(dpop_header_value);
189 struct aws_byte_buf body_buf;
190 AWS_ZERO_STRUCT(body_buf);
191 struct aws_byte_buf endpoint_buf;
192 AWS_ZERO_STRUCT(endpoint_buf);
193 struct aws_byte_buf file_path_buf;
194 AWS_ZERO_STRUCT(file_path_buf);
195
196 struct login_parameters *parameters = http_query_context->parameters;
197 struct aws_allocator *allocator = parameters->allocator;
198
199 /*
200 * load the token from disk and fail if it fails for any reason.
201 */
202 if (aws_login_token_construct_token_path(
203 allocator, parameters->login_session, parameters->login_directory_override, &file_path_buf)) {
204 AWS_LOGF_ERROR(
205 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
206 "(id=%p) failed to load login token with error: %s",
207 (void *)http_query_context->provider,
208 aws_error_debug_str(aws_last_error()));
209 goto on_finish;
210 }
211
212 struct aws_login_token *token = aws_login_token_new_from_file(allocator, &file_path_buf);
213 if (!token) {
214 AWS_LOGF_ERROR(
215 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
216 "(id=%p) failed to load login token with error: %s",
217 (void *)http_query_context->provider,
218 aws_error_debug_str(aws_last_error()));
219 aws_mem_release(allocator, token);
220 goto on_finish;
221 }
222
223 struct login_request_data *request_data = http_query_context->request_data;
224 request_data->token = token;
225
226 /* Set path and query */
227 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_string(s_login_endpoint);
228
229 if (aws_byte_buf_init_copy_from_cursor(&http_query_context->path_and_query, allocator, path_cursor)) {
230 goto on_finish;
231 }
232
233 /* Set content-type header */
234 struct aws_http_header content_type_header = {
235 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Type"),
236 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("application/json"),
237 };
238
239 /* Set DPoP header */
240 s_get_jwt_endpoint(allocator, parameters, &path_cursor, &endpoint_buf);
241 struct aws_byte_cursor endpoint = aws_byte_cursor_from_buf(&endpoint_buf);
242
243 if (aws_login_token_get_dpop_header(allocator, token, endpoint, &dpop_header_value)) {
244 AWS_LOGF_ERROR(
245 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
246 "(id=%p) failed to generate dpop header with error: %s",
247 (void *)http_query_context->provider,
248 aws_error_debug_str(aws_last_error()));
249 goto on_finish;
250 }
251
252 struct aws_http_header dpop_header = {
253 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("DPoP"),
254 .value = aws_byte_cursor_from_buf(&dpop_header_value),
255 };
256
257 /* Set body stream */
258 if (aws_login_token_get_body(allocator, token, &body_buf)) {
259 AWS_LOGF_ERROR(
260 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
261 "(id=%p) failed to log in body with error: %s",
262 (void *)http_query_context->provider,
263 aws_error_debug_str(aws_last_error()));
264 goto on_finish;
265 }
266
267 struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&body_buf);
268 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body_cursor);
269 aws_http_message_set_body_stream(http_query_context->request, body_stream);
270 request_data->body_stream = body_stream;
271 request_data->underlying_body = body_buf;
272
273 /* Set content-length header */
274 int64_t stream_length = 0;
275 if (aws_input_stream_get_length(body_stream, &stream_length)) {
276 AWS_FATAL_ASSERT(false);
277 }
278 char content_length_buffer[64] = "";
279 snprintf(content_length_buffer, sizeof(content_length_buffer), "%" PRIu64, (uint64_t)stream_length);
280 struct aws_byte_cursor content_length_cursor =
281 aws_byte_cursor_from_array(content_length_buffer, strlen(content_length_buffer));
282 struct aws_http_header cl_header = {
283 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
284 .value = content_length_cursor,
285 };
286 if (aws_http_message_add_header(http_query_context->request, cl_header)) {
287 AWS_FATAL_ASSERT(false);
288 }
289
290 /* Set Host header */
291 struct aws_http_header host_header = {
292 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
293 .value = aws_byte_cursor_from_string(parameters->endpoint),
294 };
295
296 /* Set all parameters on request */
297 if (aws_http_message_add_header(http_query_context->request, content_type_header) ||
298 aws_http_message_add_header(http_query_context->request, dpop_header) ||
299 aws_http_message_add_header(http_query_context->request, host_header)) {
300 AWS_LOGF_ERROR(
301 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
302 "(id=%p) failed to add http header with error: %s",
303 (void *)http_query_context->provider,
304 aws_error_debug_str(aws_last_error()));
305 goto on_finish;
306 }
307
308 if (aws_http_message_set_request_method(http_query_context->request, aws_http_method_post)) {
309 AWS_LOGF_ERROR(
310 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
311 "(id=%p) failed to set request method with error: %s",
312 (void *)http_query_context->provider,
313 aws_error_debug_str(aws_last_error()));
314 goto on_finish;
315 }
316
317 if (aws_http_message_set_request_path(
318 http_query_context->request, aws_byte_cursor_from_buf(&http_query_context->path_and_query))) {
319 AWS_LOGF_ERROR(
320 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
321 "(id=%p) failed to set request path with error: %s",
322 (void *)http_query_context->provider,
323 aws_error_debug_str(aws_last_error()));
324 goto on_finish;
325 }
326
327 success = true;
328 on_finish:
329 aws_byte_buf_clean_up(&dpop_header_value);
330 aws_byte_buf_clean_up(&endpoint_buf);
331 aws_byte_buf_clean_up(&file_path_buf);
332 return success ? AWS_OP_SUCCESS : AWS_OP_ERR;
333 }
334
335 static void s_login_finalize_credentials_fn(struct aws_http_query_context *http_query_context) {
336 struct login_parameters *parameters = http_query_context->parameters;
337 struct aws_credentials *credentials = NULL;
338 struct aws_login_refresh_response *refresh_token = NULL;
339 struct aws_byte_buf file_path_buf;
340 AWS_ZERO_STRUCT(file_path_buf);
341
342 /*
343 * if the request is successful we want to parse the response, extract the refreshed parts and write back to
344 * disk the changed parts.
345 */
346 if (http_query_context->error_code == AWS_ERROR_SUCCESS) {
347 struct aws_byte_cursor payload = aws_byte_cursor_from_buf(&http_query_context->payload);
348 refresh_token = aws_login_refresh_new_from_json_document(http_query_context->allocator, payload);
349 if (!refresh_token) {
350 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: failed to parse refresh response");
351 http_query_context->error_code = aws_last_error();
352 goto on_finalize;
353 }
354
355 struct aws_byte_cursor access_key_id = aws_login_refresh_get_access_key_id(refresh_token);
356 struct aws_byte_cursor secret_access_key = aws_login_refresh_get_secret_access_key(refresh_token);
357 struct aws_byte_cursor session_token = aws_login_refresh_get_session_token(refresh_token);
358 struct aws_date_time expires_at = aws_login_refresh_get_expires_at(refresh_token);
359 struct aws_byte_cursor refresh_token_value = aws_login_refresh_get_refresh_token(refresh_token);
360
361 struct login_request_data *request_data = http_query_context->request_data;
362 struct aws_credentials_options creds_option = {
363 .access_key_id_cursor = access_key_id,
364 .secret_access_key_cursor = secret_access_key,
365 .session_token_cursor = session_token,
366 .account_id_cursor = aws_login_token_get_account_id(request_data->token),
367 .expiration_timepoint_seconds = (uint64_t)aws_date_time_as_epoch_secs(&expires_at),
368 };
369 credentials = aws_credentials_new_with_options(http_query_context->allocator, &creds_option);
370
371 aws_login_token_set_access_key_id(request_data->token, access_key_id);
372 aws_login_token_set_secret_access_key(request_data->token, secret_access_key);
373 aws_login_token_set_session_token(request_data->token, session_token);
374 aws_login_token_set_expires_at(request_data->token, expires_at);
375 aws_login_token_set_refresh_token(request_data->token, refresh_token_value);
376
377 if (aws_login_token_construct_token_path(
378 parameters->allocator,
379 parameters->login_session,
380 parameters->login_directory_override,
381 &file_path_buf)) {
382 AWS_LOGF_ERROR(
383 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
384 "(id=%p) failed to load login token with error: %s",
385 (void *)http_query_context->provider,
386 aws_error_debug_str(aws_last_error()));
387 goto on_finalize;
388 }
389 if (aws_login_token_write_token_file(request_data->token, http_query_context->allocator, &file_path_buf)) {
390 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "login: failed to write to token file");
391 }
392 aws_byte_buf_clean_up(&file_path_buf);
393 }
394
395 on_finalize:
396 /* pass the credentials back */
397 http_query_context->original_callback(
398 credentials, http_query_context->error_code, http_query_context->original_user_data);
399
400 /* clean up */
401 aws_login_refresh_destroy(refresh_token);
402 aws_mem_release(http_query_context->allocator, refresh_token);
403 aws_credentials_release(credentials);
404 }
405
406 static void s_login_request_data_create(struct aws_http_query_context *query_context) {
407 struct login_request_data *login_request_data =
408 aws_mem_calloc(query_context->allocator, 1, sizeof(struct login_request_data));
409 query_context->request_data = login_request_data;
410 }
411
412 void s_login_request_data_reset(struct aws_http_query_context *query_context) {
413 struct login_request_data *login_request_data = query_context->request_data;
414 struct aws_allocator *allocator = query_context->allocator;
415 if (login_request_data->token) {
416 aws_login_token_destroy(login_request_data->token);
417 aws_mem_release(allocator, login_request_data->token);
418 }
419 if (login_request_data->body_stream) {
420 aws_input_stream_release(login_request_data->body_stream);
421 }
422 aws_byte_buf_clean_up(&login_request_data->underlying_body);
423 }
424
425 static struct aws_http_credentials_provider_request_vtable s_login_request_vtable = {
426 .clean_up_parameters_fn = s_parameters_destroy,
427 .finalize_credentials_fn = s_login_finalize_credentials_fn,
428 .create_request_fn = s_login_create_request_fn,
429 .create_request_data_fn = s_login_request_data_create,
430 .reset_request_data_fn = s_login_request_data_reset,
431 };
432
433 struct aws_credentials_provider *aws_credentials_provider_new_login(
434 struct aws_allocator *allocator,
435 const struct aws_credentials_provider_login_options *options) {
436
437 struct login_parameters *parameters = s_parameters_new(allocator, options);
438 if (!parameters) {
439 aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_LOGIN_SESSION_MISSING);
440 return NULL;
441 }
442
443 struct aws_credentials_provider *provider = NULL;
444 struct aws_http_credentials_provider_user_data *login_user_data = NULL;
445
446 aws_mem_acquire_many(
447 allocator,
448 2,
449 &provider,
450 sizeof(struct aws_credentials_provider),
451 &login_user_data,
452 sizeof(struct aws_http_credentials_provider_user_data));
453
454 AWS_ZERO_STRUCT(*provider);
455 AWS_ZERO_STRUCT(*login_user_data);
456
457 struct aws_credentials_provider_http_options http_options;
458 AWS_ZERO_STRUCT(http_options);
459
460 http_options.shutdown_options = options->shutdown_options;
461 http_options.bootstrap = options->bootstrap;
462 http_options.tls_ctx = options->tls_ctx;
463 http_options.function_table = options->function_table;
464 http_options.endpoint = aws_byte_cursor_from_string(parameters->endpoint);
465 http_options.max_connections = 2;
466 http_options.proxy_ev_settings = options->proxy_ev_settings;
467
468 login_user_data->parameters = parameters;
469 login_user_data->request_vtable = &s_login_request_vtable;
470
471 if (aws_http_credentials_provider_init_base(allocator, provider, &http_options, login_user_data)) {
472 goto on_error;
473 }
474
475 return provider;
476 on_error:
477 aws_credentials_provider_destroy(provider);
478 return NULL;
479 }