Debian Codegraph @

crt/aws-c-auth/source/aws_http_credentials_provider.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 #include <aws/auth/private/aws_http_credentials_provider.h>
8 #include <aws/auth/private/credentials_utils.h>
9 #include <aws/common/clock.h>
10 #include <aws/http/request_response.h>
11 #include <aws/http/status_code.h>
12 #include <aws/io/channel_bootstrap.h>
13 #include <aws/io/socket.h>
14 #include <aws/io/tls_channel_handler.h>
15
16 #define HTTP_RESPONSE_SIZE_INITIAL 2048
17 #define HTTP_RESPONSE_SIZE_LIMIT 10000
18 #define HTTP_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS 2
19 #define HTTP_MAX_ATTEMPTS 3
20 #define HTTP_RETRY_TIMEOUT_MS 100
21
22 struct aws_credentials_provider_http_impl {
23 struct aws_http_connection_manager *connection_manager;
24 const struct aws_auth_http_system_vtable *function_table;
25 struct aws_string *endpoint;
26 struct aws_retry_strategy *retry_strategy;
27 struct aws_http_credentials_provider_user_data *user_data;
28 };
29
30 static void s_http_query_context_reset_request_specific_data(struct aws_http_query_context *http_query_context) {
31 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
32 impl->user_data->request_vtable->reset_request_data_fn(http_query_context);
33 if (http_query_context->request) {
34 http_query_context->request = aws_http_message_release(http_query_context->request);
35 }
36 if (http_query_context->connection) {
37 int result = impl->function_table->aws_http_connection_manager_release_connection(
38 impl->connection_manager, http_query_context->connection);
39 (void)result;
40 AWS_ASSERT(result == AWS_OP_SUCCESS);
41 http_query_context->connection = NULL;
42 }
43 http_query_context->status_code = 0;
44 http_query_context->error_code = 0;
45 aws_byte_buf_clean_up(&http_query_context->path_and_query);
46 }
47
48 static void s_http_query_context_destroy(struct aws_http_query_context *http_query_context) {
49 if (http_query_context == NULL) {
50 return;
51 }
52
53 s_http_query_context_reset_request_specific_data(http_query_context);
54 aws_mem_release(http_query_context->allocator, http_query_context->request_data);
55 aws_byte_buf_clean_up(&http_query_context->payload);
56 aws_credentials_provider_release(http_query_context->provider);
57 aws_retry_token_release(http_query_context->retry_token);
58 aws_mem_release(http_query_context->allocator, http_query_context);
59 }
60
61 static struct aws_http_query_context *s_http_query_context_new(
62 struct aws_credentials_provider *provider,
63 aws_on_get_credentials_callback_fn callback,
64 void *user_data) {
65 struct aws_credentials_provider_http_impl *impl = provider->impl;
66
67 struct aws_http_query_context *http_query_context =
68 aws_mem_calloc(provider->allocator, 1, sizeof(struct aws_http_query_context));
69 http_query_context->allocator = provider->allocator;
70 http_query_context->provider = aws_credentials_provider_acquire(provider);
71 http_query_context->original_user_data = user_data;
72 http_query_context->original_callback = callback;
73 http_query_context->parameters = impl->user_data->parameters;
74 aws_byte_buf_init(&http_query_context->payload, provider->allocator, HTTP_RESPONSE_SIZE_INITIAL);
75
76 return http_query_context;
77 }
78
79 /*
80 * No matter the result, this always gets called assuming that http_query_context is successfully allocated
81 */
82 static void s_finalize_get_credentials_query(struct aws_http_query_context *http_query_context) {
83 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
84 impl->user_data->request_vtable->finalize_credentials_fn(http_query_context);
85
86 /* clean up */
87 s_http_query_context_destroy(http_query_context);
88 }
89
90 static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data);
91
92 static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) {
93 struct aws_http_query_context *http_query_context = user_data;
94
95 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
96 impl->function_table->aws_http_stream_release(stream);
97
98 /* set error code */
99 http_query_context->error_code = error_code;
100 impl->function_table->aws_http_stream_get_incoming_response_status(stream, &http_query_context->status_code);
101 if (error_code == AWS_OP_SUCCESS && http_query_context->status_code != AWS_HTTP_STATUS_CODE_200_OK) {
102 http_query_context->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_HTTP_STATUS_FAILURE;
103 }
104
105 /*
106 * If we can retry the request based on error response or http status code failure, retry it, otherwise, call the
107 * finalize function.
108 */
109 if (error_code || http_query_context->status_code != AWS_HTTP_STATUS_CODE_200_OK) {
110 enum aws_retry_error_type error_type =
111 aws_credentials_provider_compute_retry_error_type(http_query_context->status_code, error_code);
112
113 /* don't retry client errors at all. */
114 if (error_type != AWS_RETRY_ERROR_TYPE_CLIENT_ERROR) {
115 if (aws_retry_strategy_schedule_retry(
116 http_query_context->retry_token, error_type, s_on_retry_ready, http_query_context) ==
117 AWS_OP_SUCCESS) {
118 AWS_LOGF_INFO(
119 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
120 "(id=%p): successfully scheduled a retry",
121 (void *)http_query_context->provider);
122 return;
123 }
124 AWS_LOGF_ERROR(
125 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
126 "(id=%p): failed to schedule retry: %s",
127 (void *)http_query_context->provider,
128 aws_error_str(aws_last_error()));
129 http_query_context->error_code = aws_last_error();
130 }
131 } else {
132 int result = aws_retry_token_record_success(http_query_context->retry_token);
133 (void)result;
134 AWS_ASSERT(result == AWS_ERROR_SUCCESS);
135 }
136
137 s_finalize_get_credentials_query(http_query_context);
138 }
139
140 static int s_on_incoming_body_fn(struct aws_http_stream *stream, const struct aws_byte_cursor *body, void *user_data) {
141
142 (void)stream;
143
144 struct aws_http_query_context *http_query_context = user_data;
145
146 AWS_LOGF_TRACE(
147 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
148 "(id=%p) received %zu response bytes",
149 (void *)http_query_context->provider,
150 body->len);
151
152 if (body->len + http_query_context->payload.len > HTTP_RESPONSE_SIZE_LIMIT) {
153 AWS_LOGF_ERROR(
154 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
155 "(id=%p) response exceeded maximum allowed length",
156 (void *)http_query_context->provider);
157
158 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
159 }
160
161 if (aws_byte_buf_append_dynamic(&http_query_context->payload, body)) {
162 AWS_LOGF_ERROR(
163 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
164 "(id=%p) error appending response payload: %s",
165 (void *)http_query_context->provider,
166 aws_error_str(aws_last_error()));
167
168 return AWS_OP_ERR;
169 }
170
171 return AWS_OP_SUCCESS;
172 }
173
174 static void s_query_credentials(struct aws_http_query_context *http_query_context) {
175 AWS_FATAL_ASSERT(http_query_context->connection);
176 struct aws_http_stream *stream = NULL;
177 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
178
179 struct aws_http_make_request_options request_options = {
180 .self_size = sizeof(request_options),
181 .on_response_headers = NULL,
182 .on_response_header_block_done = NULL,
183 .on_response_body = s_on_incoming_body_fn,
184 .on_complete = s_on_stream_complete_fn,
185 .user_data = http_query_context,
186 .request = http_query_context->request,
187 };
188
189 stream = impl->function_table->aws_http_connection_make_request(http_query_context->connection, &request_options);
190 if (!stream) {
191 AWS_LOGF_ERROR(
192 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
193 "(id=%p) failed to make request with error: %s",
194 (void *)http_query_context->provider,
195 aws_error_debug_str(aws_last_error()));
196 goto on_error;
197 }
198
199 if (impl->function_table->aws_http_stream_activate(stream)) {
200 AWS_LOGF_ERROR(
201 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
202 "(id=%p) failed to activate the stream with error: %s",
203 (void *)http_query_context->provider,
204 aws_error_debug_str(aws_last_error()));
205 goto on_error;
206 }
207
208 return;
209
210 on_error:
211 http_query_context->error_code = aws_last_error();
212 impl->function_table->aws_http_stream_release(stream);
213 s_finalize_get_credentials_query(http_query_context);
214 }
215
216 static void s_on_acquire_connection(struct aws_http_connection *connection, int error_code, void *user_data) {
217 struct aws_http_query_context *http_query_context = user_data;
218
219 if (error_code) {
220 AWS_LOGF_ERROR(
221 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
222 "id=%p: failed to acquire a connection, error code %d(%s)",
223 (void *)http_query_context->provider,
224 error_code,
225 aws_error_str(error_code));
226 http_query_context->error_code = error_code;
227 s_finalize_get_credentials_query(http_query_context);
228 return;
229 }
230 AWS_LOGF_INFO(
231 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
232 "(id=%p): successfully acquired a connection",
233 (void *)http_query_context->provider);
234 http_query_context->connection = connection;
235
236 http_query_context->request = aws_http_message_new_request(http_query_context->allocator);
237 if (http_query_context->request == NULL) {
238 goto on_error;
239 }
240
241 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
242 if (impl->user_data->request_vtable->create_request_fn(http_query_context, user_data)) {
243 goto on_error;
244 }
245 s_query_credentials(http_query_context);
246 return;
247 on_error:
248 http_query_context->error_code = aws_last_error();
249 s_finalize_get_credentials_query(http_query_context);
250 }
251
252 static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) {
253 (void)token;
254 struct aws_http_query_context *http_query_context = user_data;
255
256 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
257
258 if (error_code) {
259 AWS_LOGF_ERROR(
260 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
261 "(id=%p): failed to schedule retry with error: %s",
262 (void *)http_query_context->provider,
263 aws_error_debug_str(error_code));
264 http_query_context->error_code = error_code;
265 s_finalize_get_credentials_query(http_query_context);
266 return;
267 }
268
269 /* clear the result from previous attempt */
270 s_http_query_context_reset_request_specific_data(http_query_context);
271
272 impl->function_table->aws_http_connection_manager_acquire_connection(
273 impl->connection_manager, s_on_acquire_connection, http_query_context);
274 }
275
276 static void s_on_retry_token_acquired(
277 struct aws_retry_strategy *strategy,
278 int error_code,
279 struct aws_retry_token *token,
280 void *user_data) {
281 struct aws_http_query_context *http_query_context = user_data;
282 (void)strategy;
283
284 if (error_code) {
285 AWS_LOGF_ERROR(
286 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
287 "(id=%p): failed to acquire retry token: %s",
288 (void *)http_query_context->provider,
289 aws_error_debug_str(error_code));
290 http_query_context->error_code = error_code;
291 s_finalize_get_credentials_query(http_query_context);
292 return;
293 }
294
295 http_query_context->retry_token = token;
296 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
297 impl->function_table->aws_http_connection_manager_acquire_connection(
298 impl->connection_manager, s_on_acquire_connection, user_data);
299 }
300
301 static int s_aws_http_credentials_provider_get_credentials(
302 struct aws_credentials_provider *provider,
303 aws_on_get_credentials_callback_fn callback,
304 void *user_data) {
305
306 struct aws_http_query_context *http_query_context = s_http_query_context_new(provider, callback, user_data);
307
308 struct aws_credentials_provider_http_impl *impl = http_query_context->provider->impl;
309 impl->user_data->request_vtable->create_request_data_fn(http_query_context);
310 if (aws_retry_strategy_acquire_retry_token(
311 impl->retry_strategy, NULL, s_on_retry_token_acquired, http_query_context, HTTP_RETRY_TIMEOUT_MS)) {
312 AWS_LOGF_ERROR(
313 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
314 "(id=%p): failed to acquire retry token: %s",
315 (void *)provider,
316 aws_error_debug_str(aws_last_error()));
317 goto on_error;
318 }
319
320 return AWS_OP_SUCCESS;
321
322 on_error:
323 s_http_query_context_destroy(http_query_context);
324 return aws_last_error();
325 }
326
327 static void s_on_connection_manager_shutdown(void *user_data) {
328 struct aws_credentials_provider *provider = user_data;
329
330 aws_credentials_provider_invoke_shutdown_callback(provider);
331 struct aws_credentials_provider_http_impl *impl = provider->impl;
332 impl->user_data->request_vtable->clean_up_parameters_fn(impl->user_data->parameters);
333 aws_mem_release(provider->allocator, provider->impl);
334 aws_mem_release(provider->allocator, provider);
335 }
336
337 static void s_credentials_provider_http_destroy(struct aws_credentials_provider *provider) {
338
339 struct aws_credentials_provider_http_impl *impl = provider->impl;
340 if (impl == NULL) {
341 return;
342 }
343 aws_string_destroy(impl->endpoint);
344 aws_retry_strategy_release(impl->retry_strategy);
345
346 /* aws_http_connection_manager_release will eventually leads to call of s_on_connection_manager_shutdown,
347 * which will do memory release for provider and impl. So We should be freeing impl
348 * related memory first, then call aws_http_connection_manager_release.
349 */
350 if (impl->connection_manager) {
351 impl->function_table->aws_http_connection_manager_release(impl->connection_manager);
352 } else {
353 /* If provider setup failed halfway through, connection_manager might not exist.
354 * In this case invoke shutdown completion callback directly to finish cleanup */
355 s_on_connection_manager_shutdown(provider);
356 }
357 }
358
359 static struct aws_credentials_provider_vtable s_aws_credentials_provider_http_vtable = {
360 .get_credentials = s_aws_http_credentials_provider_get_credentials,
361 .destroy = s_credentials_provider_http_destroy,
362 };
363
364 int aws_http_credentials_provider_init_base(
365 struct aws_allocator *allocator,
366 struct aws_credentials_provider *provider,
367 struct aws_credentials_provider_http_options *options,
368 struct aws_http_credentials_provider_user_data *user_data) {
369
370 struct aws_credentials_provider_http_impl *impl =
371 aws_mem_acquire(allocator, sizeof(struct aws_credentials_provider_http_impl));
372
373 AWS_ZERO_STRUCT(*impl);
374 impl->user_data = user_data;
375
376 aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_http_vtable, impl);
377
378 struct aws_tls_connection_options tls_connection_options;
379 AWS_ZERO_STRUCT(tls_connection_options);
380
381 if (!options->tls_ctx) {
382 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p): a TLS context must be provided", (void *)provider);
383 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
384 goto on_error;
385 }
386
387 if (!options->bootstrap) {
388 AWS_LOGF_ERROR(
389 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p): a bootstrap instance must be provided", (void *)provider);
390 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
391 goto on_error;
392 }
393
394 aws_tls_connection_options_init_from_ctx(&tls_connection_options, options->tls_ctx);
395 if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &options->endpoint)) {
396 AWS_LOGF_ERROR(
397 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
398 "(id=%p): failed to create a tls connection options with error %s",
399 (void *)provider,
400 aws_error_str(aws_last_error()));
401 goto on_error;
402 }
403
404 struct aws_socket_options socket_options;
405 AWS_ZERO_STRUCT(socket_options);
406 socket_options.type = AWS_SOCKET_STREAM;
407 socket_options.domain = AWS_SOCKET_IPV4;
408 socket_options.connect_timeout_ms = (uint32_t)aws_timestamp_convert(
409 HTTP_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL);
410
411 struct aws_http_connection_manager_options manager_options;
412 AWS_ZERO_STRUCT(manager_options);
413 manager_options.bootstrap = options->bootstrap;
414 manager_options.initial_window_size = HTTP_RESPONSE_SIZE_LIMIT;
415 manager_options.socket_options = &socket_options;
416 manager_options.host = options->endpoint;
417 manager_options.port = 443;
418 manager_options.max_connections = options->max_connections;
419 manager_options.shutdown_complete_callback = s_on_connection_manager_shutdown;
420 manager_options.shutdown_complete_user_data = provider;
421 manager_options.tls_connection_options = &tls_connection_options;
422 manager_options.proxy_ev_settings = options->proxy_ev_settings;
423
424 impl->function_table = options->function_table;
425 if (impl->function_table == NULL) {
426 impl->function_table = g_aws_credentials_provider_http_function_table;
427 }
428
429 impl->endpoint = aws_string_new_from_cursor(allocator, &options->endpoint);
430 impl->connection_manager = impl->function_table->aws_http_connection_manager_new(allocator, &manager_options);
431 if (impl->connection_manager == NULL) {
432 AWS_LOGF_ERROR(
433 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
434 "(id=%p): failed to create a connection manager %s",
435 (void *)provider,
436 aws_error_debug_str(aws_last_error()));
437 goto on_error;
438 }
439
440 provider->shutdown_options = options->shutdown_options;
441
442 if (options->retry_strategy == NULL) {
443 struct aws_standard_retry_options retry_options = {
444 .backoff_retry_options =
445 {
446 .el_group = options->bootstrap->event_loop_group,
447 .max_retries = HTTP_MAX_ATTEMPTS,
448 },
449 };
450
451 impl->retry_strategy = aws_retry_strategy_new_standard(allocator, &retry_options);
452 } else {
453 impl->retry_strategy = options->retry_strategy;
454 }
455
456 if (!impl->retry_strategy) {
457 AWS_LOGF_ERROR(
458 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
459 "(id=%p): failed to create a retry strategy with error %s",
460 (void *)provider,
461 aws_error_debug_str(aws_last_error()));
462 goto on_error;
463 }
464
465 aws_tls_connection_options_clean_up(&tls_connection_options);
466 return AWS_OP_SUCCESS;
467 on_error:
468 aws_tls_connection_options_clean_up(&tls_connection_options);
469 aws_mem_release(allocator, impl);
470 return aws_last_error();
471 }