| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "aws/s3/s3express_credentials_provider.h" |
| 6 |
#include "aws/s3/private/s3_client_impl.h" |
| 7 |
#include "aws/s3/private/s3express_credentials_provider_impl.h" |
| 8 |
#include <aws/auth/credentials.h> |
| 9 |
#include <aws/s3/private/s3_request_messages.h> |
| 10 |
#include <aws/s3/private/s3_util.h> |
| 11 |
#include <aws/s3/s3_client.h> |
| 12 |
|
| 13 |
#include <aws/cal/hash.h> |
| 14 |
#include <aws/common/clock.h> |
| 15 |
#include <aws/common/lru_cache.h> |
| 16 |
#include <aws/common/uri.h> |
| 17 |
#include <aws/common/xml_parser.h> |
| 18 |
#include <aws/http/request_response.h> |
| 19 |
#include <aws/http/status_code.h> |
| 20 |
#include <aws/io/channel_bootstrap.h> |
| 21 |
#include <aws/io/event_loop.h> |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
|
| 25 |
static struct aws_byte_cursor s_create_session_path_query = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/?session="); |
| 26 |
static const size_t s_default_cache_capacity = 100; |
| 27 |
|
| 28 |
|
| 29 |
static const uint64_t s_expired_threshold_secs = 5; |
| 30 |
static const uint64_t s_about_to_expire_threshold_secs = 60; |
| 31 |
static const uint64_t s_background_refresh_interval_secs = 60; |
| 32 |
|
| 33 |
struct aws_query_callback_node { |
| 34 |
struct aws_linked_list_node node; |
| 35 |
aws_on_get_credentials_callback_fn *get_cred_callback; |
| 36 |
void *get_cred_user_data; |
| 37 |
}; |
| 38 |
|
| 39 |
struct aws_s3express_session_creator { |
| 40 |
struct aws_allocator *allocator; |
| 41 |
|
| 42 |
|
| 43 |
struct aws_string *hash_key; |
| 44 |
|
| 45 |
struct aws_s3express_credentials_provider *provider; |
| 46 |
struct aws_byte_buf response_buf; |
| 47 |
|
| 48 |
|
| 49 |
struct aws_string *region; |
| 50 |
struct aws_string *host; |
| 51 |
struct aws_http_headers *headers; |
| 52 |
|
| 53 |
struct { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct aws_s3express_session *session; |
| 59 |
|
| 60 |
struct aws_linked_list query_queue; |
| 61 |
struct aws_s3_meta_request *meta_request; |
| 62 |
} synced_data; |
| 63 |
}; |
| 64 |
|
| 65 |
static struct aws_s3express_session *s_aws_s3express_session_new( |
| 66 |
struct aws_s3express_credentials_provider *provider, |
| 67 |
const struct aws_string *hash_key, |
| 68 |
const struct aws_string *region, |
| 69 |
const struct aws_string *host, |
| 70 |
struct aws_http_headers *headers, |
| 71 |
struct aws_credentials *credentials) { |
| 72 |
|
| 73 |
struct aws_s3express_session *session = |
| 74 |
aws_mem_calloc(provider->allocator, 1, sizeof(struct aws_s3express_session)); |
| 75 |
session->allocator = provider->allocator; |
| 76 |
session->impl = provider->impl; |
| 77 |
session->hash_key = aws_string_new_from_string(provider->allocator, hash_key); |
| 78 |
session->host = aws_string_new_from_string(provider->allocator, host); |
| 79 |
if (headers != NULL) { |
| 80 |
aws_http_headers_acquire(headers); |
| 81 |
session->headers = headers; |
| 82 |
} |
| 83 |
if (region) { |
| 84 |
session->region = aws_string_new_from_string(provider->allocator, region); |
| 85 |
} |
| 86 |
session->s3express_credentials = credentials; |
| 87 |
aws_credentials_acquire(credentials); |
| 88 |
return session; |
| 89 |
} |
| 90 |
|
| 91 |
static void s_aws_s3express_session_destroy(struct aws_s3express_session *session) { |
| 92 |
if (!session) { |
| 93 |
return; |
| 94 |
} |
| 95 |
if (session->creator) { |
| 96 |
|
| 97 |
|
| 98 |
session->creator->synced_data.session = NULL; |
| 99 |
} |
| 100 |
aws_string_destroy(session->hash_key); |
| 101 |
aws_string_destroy(session->region); |
| 102 |
aws_string_destroy(session->host); |
| 103 |
aws_http_headers_release(session->headers); |
| 104 |
aws_credentials_release(session->s3express_credentials); |
| 105 |
aws_mem_release(session->allocator, session); |
| 106 |
} |
| 107 |
|
| 108 |
static bool s_s3express_session_is_valid(struct aws_s3express_session *session, uint64_t now_seconds) { |
| 109 |
AWS_ASSERT(session->s3express_credentials); |
| 110 |
if (session->impl->mock_test.s3express_session_is_valid_override) { |
| 111 |
|
| 112 |
return session->impl->mock_test.s3express_session_is_valid_override(session, now_seconds); |
| 113 |
} |
| 114 |
uint64_t expire_secs = aws_credentials_get_expiration_timepoint_seconds(session->s3express_credentials); |
| 115 |
uint64_t threshold_secs = 0; |
| 116 |
int overflow = aws_add_u64_checked(now_seconds, s_expired_threshold_secs, &threshold_secs); |
| 117 |
AWS_ASSERT(!overflow); |
| 118 |
(void)overflow; |
| 119 |
|
| 120 |
return threshold_secs < expire_secs; |
| 121 |
} |
| 122 |
|
| 123 |
static bool s_s3express_session_about_to_expire(struct aws_s3express_session *session, uint64_t now_seconds) { |
| 124 |
AWS_ASSERT(session->s3express_credentials); |
| 125 |
if (session->impl->mock_test.s3express_session_about_to_expire_override) { |
| 126 |
|
| 127 |
return session->impl->mock_test.s3express_session_about_to_expire_override(session, now_seconds); |
| 128 |
} |
| 129 |
uint64_t expire_secs = aws_credentials_get_expiration_timepoint_seconds(session->s3express_credentials); |
| 130 |
uint64_t threshold_secs = 0; |
| 131 |
int overflow = aws_add_u64_checked(now_seconds, s_about_to_expire_threshold_secs, &threshold_secs); |
| 132 |
AWS_ASSERT(!overflow); |
| 133 |
(void)overflow; |
| 134 |
return threshold_secs >= expire_secs; |
| 135 |
} |
| 136 |
|
| 137 |
static struct aws_s3express_session_creator *s_aws_s3express_session_creator_destroy( |
| 138 |
struct aws_s3express_session_creator *session_creator); |
| 139 |
|
| 140 |
static void s_credentials_provider_s3express_impl_lock_synced_data( |
| 141 |
struct aws_s3express_credentials_provider_impl *impl) { |
| 142 |
int err = aws_mutex_lock(&impl->synced_data.lock); |
| 143 |
AWS_ASSERT(!err); |
| 144 |
(void)err; |
| 145 |
} |
| 146 |
|
| 147 |
static void s_credentials_provider_s3express_impl_unlock_synced_data( |
| 148 |
struct aws_s3express_credentials_provider_impl *impl) { |
| 149 |
int err = aws_mutex_unlock(&impl->synced_data.lock); |
| 150 |
AWS_ASSERT(!err); |
| 151 |
(void)err; |
| 152 |
} |
| 153 |
|
| 154 |
static int s_on_incoming_body_fn( |
| 155 |
struct aws_s3_meta_request *meta_request, |
| 156 |
const struct aws_byte_cursor *body, |
| 157 |
uint64_t range_start, |
| 158 |
void *user_data) { |
| 159 |
(void)meta_request; |
| 160 |
(void)range_start; |
| 161 |
|
| 162 |
struct aws_s3express_session_creator *session_creator = user_data; |
| 163 |
return aws_byte_buf_append_dynamic(&session_creator->response_buf, body); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
struct aws_s3express_xml_parser_user_data { |
| 179 |
struct aws_allocator *allocator; |
| 180 |
struct aws_string *access_key_id; |
| 181 |
struct aws_string *secret_access_key; |
| 182 |
struct aws_string *session_token; |
| 183 |
void *log_id; |
| 184 |
uint64_t expire_timestamp_secs; |
| 185 |
}; |
| 186 |
|
| 187 |
static int s_s3express_xml_traversing_credentials(struct aws_xml_node *node, void *user_data) { |
| 188 |
|
| 189 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 190 |
|
| 191 |
struct aws_s3express_xml_parser_user_data *parser_ud = user_data; |
| 192 |
struct aws_byte_cursor credential_data; |
| 193 |
AWS_ZERO_STRUCT(credential_data); |
| 194 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "SessionToken")) { |
| 195 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 196 |
return AWS_OP_ERR; |
| 197 |
} |
| 198 |
parser_ud->session_token = |
| 199 |
aws_string_new_from_array(parser_ud->allocator, credential_data.ptr, credential_data.len); |
| 200 |
} |
| 201 |
|
| 202 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "SecretAccessKey")) { |
| 203 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 204 |
return AWS_OP_ERR; |
| 205 |
} |
| 206 |
parser_ud->secret_access_key = |
| 207 |
aws_string_new_from_array(parser_ud->allocator, credential_data.ptr, credential_data.len); |
| 208 |
} |
| 209 |
|
| 210 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "AccessKeyId")) { |
| 211 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 212 |
return AWS_OP_ERR; |
| 213 |
} |
| 214 |
parser_ud->access_key_id = |
| 215 |
aws_string_new_from_array(parser_ud->allocator, credential_data.ptr, credential_data.len); |
| 216 |
} |
| 217 |
|
| 218 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Expiration")) { |
| 219 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 220 |
return AWS_OP_ERR; |
| 221 |
} |
| 222 |
AWS_LOGF_TRACE( |
| 223 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 224 |
"(id=%p): Read Expiration " PRInSTR "", |
| 225 |
(void *)parser_ud->log_id, |
| 226 |
AWS_BYTE_CURSOR_PRI(credential_data)); |
| 227 |
struct aws_date_time dt; |
| 228 |
if (aws_date_time_init_from_str_cursor(&dt, &credential_data, AWS_DATE_FORMAT_AUTO_DETECT)) { |
| 229 |
AWS_LOGF_ERROR( |
| 230 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 231 |
"(id=%p): Failed to parse Expiration " PRInSTR "", |
| 232 |
(void *)parser_ud->log_id, |
| 233 |
AWS_BYTE_CURSOR_PRI(credential_data)); |
| 234 |
return AWS_OP_ERR; |
| 235 |
} |
| 236 |
parser_ud->expire_timestamp_secs = (uint64_t)aws_date_time_as_epoch_secs(&dt); |
| 237 |
} |
| 238 |
|
| 239 |
return AWS_OP_SUCCESS; |
| 240 |
} |
| 241 |
|
| 242 |
static int s_s3express_xml_traversing_CreateSessionResult_children(struct aws_xml_node *node, void *user_data) { |
| 243 |
|
| 244 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 245 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Credentials")) { |
| 246 |
return aws_xml_node_traverse(node, s_s3express_xml_traversing_credentials, user_data); |
| 247 |
} |
| 248 |
|
| 249 |
return AWS_OP_SUCCESS; |
| 250 |
} |
| 251 |
|
| 252 |
static int s_s3express_xml_traversing_root(struct aws_xml_node *node, void *user_data) { |
| 253 |
|
| 254 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 255 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "CreateSessionResult")) { |
| 256 |
return aws_xml_node_traverse(node, s_s3express_xml_traversing_CreateSessionResult_children, user_data); |
| 257 |
} |
| 258 |
|
| 259 |
return AWS_OP_SUCCESS; |
| 260 |
} |
| 261 |
|
| 262 |
static struct aws_credentials *s_parse_s3express_xml( |
| 263 |
struct aws_allocator *alloc, |
| 264 |
struct aws_byte_cursor xml, |
| 265 |
void *logging_id) { |
| 266 |
|
| 267 |
struct aws_credentials *credentials = NULL; |
| 268 |
|
| 269 |
struct aws_s3express_xml_parser_user_data user_data = { |
| 270 |
.allocator = alloc, |
| 271 |
.log_id = logging_id, |
| 272 |
}; |
| 273 |
struct aws_xml_parser_options options = { |
| 274 |
.doc = xml, |
| 275 |
.on_root_encountered = s_s3express_xml_traversing_root, |
| 276 |
.user_data = &user_data, |
| 277 |
}; |
| 278 |
if (aws_xml_parse(alloc, &options)) { |
| 279 |
AWS_LOGF_ERROR( |
| 280 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 281 |
"(id=%p): credentials parsing failed with error %s", |
| 282 |
logging_id, |
| 283 |
aws_error_debug_str(aws_last_error())); |
| 284 |
goto done; |
| 285 |
} |
| 286 |
if (user_data.access_key_id && user_data.secret_access_key && user_data.session_token && |
| 287 |
user_data.expire_timestamp_secs) { |
| 288 |
|
| 289 |
credentials = aws_credentials_new_from_string( |
| 290 |
alloc, |
| 291 |
user_data.access_key_id, |
| 292 |
user_data.secret_access_key, |
| 293 |
user_data.session_token, |
| 294 |
user_data.expire_timestamp_secs); |
| 295 |
} |
| 296 |
|
| 297 |
done: |
| 298 |
|
| 299 |
aws_string_destroy(user_data.access_key_id); |
| 300 |
aws_string_destroy(user_data.secret_access_key); |
| 301 |
aws_string_destroy(user_data.session_token); |
| 302 |
|
| 303 |
return credentials; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
static void s_on_request_finished( |
| 308 |
struct aws_s3_meta_request *meta_request, |
| 309 |
const struct aws_s3_meta_request_result *meta_request_result, |
| 310 |
void *user_data) { |
| 311 |
(void)meta_request; |
| 312 |
struct aws_s3express_session_creator *session_creator = user_data; |
| 313 |
struct aws_s3express_credentials_provider_impl *impl = session_creator->provider->impl; |
| 314 |
if (impl->mock_test.meta_request_finished_overhead) { |
| 315 |
impl->mock_test.meta_request_finished_overhead(meta_request, meta_request_result, user_data); |
| 316 |
} |
| 317 |
|
| 318 |
struct aws_linked_list pending_callbacks; |
| 319 |
aws_linked_list_init(&pending_callbacks); |
| 320 |
|
| 321 |
struct aws_credentials *credentials = NULL; |
| 322 |
int error_code = meta_request_result->error_code; |
| 323 |
|
| 324 |
AWS_LOGF_DEBUG( |
| 325 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 326 |
"(id=%p): CreateSession call completed with http status: %d and error code %s", |
| 327 |
(void *)session_creator->provider, |
| 328 |
meta_request_result->response_status, |
| 329 |
aws_error_debug_str(error_code)); |
| 330 |
|
| 331 |
if (error_code && meta_request_result->error_response_body && meta_request_result->error_response_body->len > 0) { |
| 332 |
|
| 333 |
error_code = AWS_ERROR_S3EXPRESS_CREATE_SESSION_FAILED; |
| 334 |
AWS_LOGF_ERROR( |
| 335 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 336 |
"(id=%p): CreateSession call failed with http status: %d, and error response body is: %.*s", |
| 337 |
(void *)session_creator->provider, |
| 338 |
meta_request_result->response_status, |
| 339 |
(int)meta_request_result->error_response_body->len, |
| 340 |
meta_request_result->error_response_body->buffer); |
| 341 |
} |
| 342 |
|
| 343 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 344 |
credentials = s_parse_s3express_xml( |
| 345 |
session_creator->allocator, aws_byte_cursor_from_buf(&session_creator->response_buf), session_creator); |
| 346 |
|
| 347 |
if (!credentials) { |
| 348 |
error_code = AWS_AUTH_PROVIDER_PARSER_UNEXPECTED_RESPONSE; |
| 349 |
AWS_LOGF_ERROR( |
| 350 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 351 |
"(id=%p): failed to read credentials from document, treating as an error.", |
| 352 |
(void *)session_creator->provider); |
| 353 |
} |
| 354 |
} |
| 355 |
{ |
| 356 |
s_credentials_provider_s3express_impl_lock_synced_data(impl); |
| 357 |
aws_linked_list_swap_contents(&session_creator->synced_data.query_queue, &pending_callbacks); |
| 358 |
aws_hash_table_remove(&impl->synced_data.session_creator_table, session_creator->hash_key, NULL, NULL); |
| 359 |
struct aws_s3express_session *session = session_creator->synced_data.session; |
| 360 |
if (session) { |
| 361 |
session->creator = NULL; |
| 362 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 363 |
|
| 364 |
aws_credentials_release(session->s3express_credentials); |
| 365 |
session->s3express_credentials = credentials; |
| 366 |
aws_credentials_acquire(credentials); |
| 367 |
} else { |
| 368 |
|
| 369 |
aws_cache_remove(impl->synced_data.cache, session->hash_key); |
| 370 |
} |
| 371 |
} else if (error_code == AWS_ERROR_SUCCESS) { |
| 372 |
|
| 373 |
session = s_aws_s3express_session_new( |
| 374 |
session_creator->provider, |
| 375 |
session_creator->hash_key, |
| 376 |
session_creator->region, |
| 377 |
session_creator->host, |
| 378 |
session_creator->headers, |
| 379 |
credentials); |
| 380 |
aws_cache_put(impl->synced_data.cache, session->hash_key, session); |
| 381 |
} |
| 382 |
|
| 383 |
s_credentials_provider_s3express_impl_unlock_synced_data(impl); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
while (!aws_linked_list_empty(&pending_callbacks)) { |
| 388 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(&pending_callbacks); |
| 389 |
struct aws_query_callback_node *callback_node = AWS_CONTAINER_OF(node, struct aws_query_callback_node, node); |
| 390 |
callback_node->get_cred_callback(credentials, error_code, callback_node->get_cred_user_data); |
| 391 |
aws_mem_release(session_creator->allocator, callback_node); |
| 392 |
} |
| 393 |
aws_credentials_release(credentials); |
| 394 |
s_aws_s3express_session_creator_destroy(session_creator); |
| 395 |
} |
| 396 |
|
| 397 |
static struct aws_http_message *s_create_session_request_new( |
| 398 |
struct aws_allocator *allocator, |
| 399 |
struct aws_byte_cursor host_value, |
| 400 |
struct aws_http_headers *headers, |
| 401 |
const struct aws_uri *endpoint_override) { |
| 402 |
struct aws_http_message *request = aws_http_message_new_request(allocator); |
| 403 |
|
| 404 |
struct aws_byte_cursor host = host_value; |
| 405 |
|
| 406 |
if (endpoint_override != NULL) { |
| 407 |
host = *aws_uri_authority(endpoint_override); |
| 408 |
} |
| 409 |
struct aws_http_header host_header = { |
| 410 |
.name = g_host_header_name, |
| 411 |
.value = host, |
| 412 |
}; |
| 413 |
if (aws_http_message_add_header(request, host_header)) { |
| 414 |
goto error; |
| 415 |
} |
| 416 |
|
| 417 |
struct aws_http_header user_agent_header = { |
| 418 |
.name = g_user_agent_header_name, |
| 419 |
.value = aws_byte_cursor_from_c_str("aws-sdk-crt/s3express-credentials-provider"), |
| 420 |
}; |
| 421 |
if (aws_http_message_add_header(request, user_agent_header)) { |
| 422 |
goto error; |
| 423 |
} |
| 424 |
if (headers != NULL) { |
| 425 |
for (size_t header_index = 0; header_index < g_s3_create_session_allowed_headers_count; ++header_index) { |
| 426 |
struct aws_byte_cursor header_name = g_s3_create_session_allowed_headers[header_index]; |
| 427 |
struct aws_byte_cursor header_value; |
| 428 |
if (aws_http_headers_get(headers, header_name, &header_value) == AWS_OP_SUCCESS && header_value.len > 0) { |
| 429 |
struct aws_http_header header = { |
| 430 |
.name = header_name, |
| 431 |
.value = header_value, |
| 432 |
}; |
| 433 |
if (aws_http_message_add_header(request, header)) { |
| 434 |
goto error; |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if (aws_http_message_set_request_method(request, aws_http_method_get)) { |
| 441 |
goto error; |
| 442 |
} |
| 443 |
|
| 444 |
struct aws_byte_cursor path_and_query = s_create_session_path_query; |
| 445 |
if (endpoint_override != NULL) { |
| 446 |
const struct aws_byte_cursor *override_path_query = aws_uri_path_and_query(endpoint_override); |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
if (override_path_query->len > 1) { |
| 451 |
path_and_query = *override_path_query; |
| 452 |
} |
| 453 |
} |
| 454 |
if (aws_http_message_set_request_path(request, path_and_query)) { |
| 455 |
goto error; |
| 456 |
} |
| 457 |
return request; |
| 458 |
error: |
| 459 |
return aws_http_message_release(request); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
static struct aws_s3express_session_creator *s_aws_s3express_session_creator_destroy( |
| 464 |
struct aws_s3express_session_creator *session_creator) { |
| 465 |
if (session_creator == NULL) { |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
AWS_FATAL_ASSERT(aws_linked_list_empty(&session_creator->synced_data.query_queue)); |
| 469 |
struct aws_s3express_credentials_provider_impl *impl = session_creator->provider->impl; |
| 470 |
aws_s3_meta_request_release(session_creator->synced_data.meta_request); |
| 471 |
aws_ref_count_release(&impl->internal_ref); |
| 472 |
|
| 473 |
aws_string_destroy(session_creator->hash_key); |
| 474 |
aws_string_destroy(session_creator->region); |
| 475 |
aws_string_destroy(session_creator->host); |
| 476 |
aws_http_headers_release(session_creator->headers); |
| 477 |
|
| 478 |
aws_byte_buf_clean_up(&session_creator->response_buf); |
| 479 |
aws_mem_release(session_creator->allocator, session_creator); |
| 480 |
return NULL; |
| 481 |
} |
| 482 |
|
| 483 |
struct aws_string *aws_encode_s3express_hash_key_new( |
| 484 |
struct aws_allocator *allocator, |
| 485 |
const struct aws_credentials *original_credentials, |
| 486 |
struct aws_byte_cursor host_value, |
| 487 |
struct aws_http_headers *headers) { |
| 488 |
|
| 489 |
struct aws_byte_buf combined_buf; |
| 490 |
|
| 491 |
|
| 492 |
struct aws_byte_cursor access_key = aws_credentials_get_access_key_id(original_credentials); |
| 493 |
struct aws_byte_cursor secret_access_key = aws_credentials_get_secret_access_key(original_credentials); |
| 494 |
aws_byte_buf_init(&combined_buf, allocator, access_key.len + secret_access_key.len); |
| 495 |
aws_byte_buf_write_from_whole_cursor(&combined_buf, access_key); |
| 496 |
aws_byte_buf_write_from_whole_cursor(&combined_buf, secret_access_key); |
| 497 |
|
| 498 |
|
| 499 |
if (headers != NULL) { |
| 500 |
struct aws_byte_cursor collon = aws_byte_cursor_from_c_str(":"); |
| 501 |
struct aws_byte_cursor comma = aws_byte_cursor_from_c_str(","); |
| 502 |
for (size_t header_index = 0; header_index < g_s3_create_session_allowed_headers_count; ++header_index) { |
| 503 |
struct aws_byte_cursor header_name = g_s3_create_session_allowed_headers[header_index]; |
| 504 |
struct aws_byte_cursor header_value; |
| 505 |
if (aws_http_headers_get(headers, header_name, &header_value) == AWS_OP_SUCCESS && header_value.len > 0) { |
| 506 |
aws_byte_buf_append_dynamic(&combined_buf, &comma); |
| 507 |
aws_byte_buf_append_dynamic(&combined_buf, &header_name); |
| 508 |
aws_byte_buf_append_dynamic(&combined_buf, &collon); |
| 509 |
aws_byte_buf_append_dynamic(&combined_buf, &header_value); |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
struct aws_byte_cursor combined_cursor = aws_byte_cursor_from_buf(&combined_buf); |
| 516 |
struct aws_byte_buf digest_buf; |
| 517 |
aws_byte_buf_init(&digest_buf, allocator, AWS_SHA256_LEN); |
| 518 |
aws_sha256_compute(allocator, &combined_cursor, &digest_buf, 0); |
| 519 |
|
| 520 |
|
| 521 |
struct aws_byte_buf result_buffer; |
| 522 |
aws_byte_buf_init(&result_buffer, allocator, host_value.len + digest_buf.len); |
| 523 |
aws_byte_buf_write_from_whole_cursor(&result_buffer, host_value); |
| 524 |
aws_byte_buf_write_from_whole_buffer(&result_buffer, digest_buf); |
| 525 |
struct aws_string *result = aws_string_new_from_buf(allocator, &result_buffer); |
| 526 |
|
| 527 |
|
| 528 |
aws_byte_buf_clean_up(&result_buffer); |
| 529 |
aws_byte_buf_clean_up(&combined_buf); |
| 530 |
aws_byte_buf_clean_up(&digest_buf); |
| 531 |
|
| 532 |
return result; |
| 533 |
} |
| 534 |
|
| 535 |
static struct aws_s3express_session_creator *s_session_creator_new( |
| 536 |
struct aws_s3express_credentials_provider *provider, |
| 537 |
const struct aws_credentials *original_credentials, |
| 538 |
const struct aws_credentials_properties_s3express *s3express_properties) { |
| 539 |
|
| 540 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 541 |
struct aws_http_message *request = s_create_session_request_new( |
| 542 |
provider->allocator, |
| 543 |
s3express_properties->host, |
| 544 |
s3express_properties->headers, |
| 545 |
impl->mock_test.endpoint_override); |
| 546 |
if (!request) { |
| 547 |
return NULL; |
| 548 |
} |
| 549 |
|
| 550 |
struct aws_s3express_session_creator *session_creator = |
| 551 |
aws_mem_calloc(provider->allocator, 1, sizeof(struct aws_s3express_session_creator)); |
| 552 |
session_creator->allocator = provider->allocator; |
| 553 |
session_creator->provider = provider; |
| 554 |
session_creator->host = aws_string_new_from_cursor(session_creator->allocator, &s3express_properties->host); |
| 555 |
session_creator->region = aws_string_new_from_cursor(session_creator->allocator, &s3express_properties->region); |
| 556 |
if (s3express_properties->headers != NULL) { |
| 557 |
aws_http_headers_acquire(s3express_properties->headers); |
| 558 |
session_creator->headers = s3express_properties->headers; |
| 559 |
} |
| 560 |
|
| 561 |
struct aws_signing_config_aws s3express_signing_config = { |
| 562 |
.credentials = original_credentials, |
| 563 |
.service = g_s3express_service_name, |
| 564 |
.region = s3express_properties->region, |
| 565 |
}; |
| 566 |
|
| 567 |
aws_byte_buf_init(&session_creator->response_buf, provider->allocator, 512); |
| 568 |
struct aws_s3_meta_request_options options = { |
| 569 |
.message = request, |
| 570 |
.type = AWS_S3_META_REQUEST_TYPE_DEFAULT, |
| 571 |
.body_callback = s_on_incoming_body_fn, |
| 572 |
.finish_callback = s_on_request_finished, |
| 573 |
.signing_config = &s3express_signing_config, |
| 574 |
.user_data = session_creator, |
| 575 |
.operation_name = aws_byte_cursor_from_c_str("CreateSession"), |
| 576 |
}; |
| 577 |
session_creator->synced_data.meta_request = aws_s3_client_make_meta_request(impl->client, &options); |
| 578 |
AWS_FATAL_ASSERT(session_creator->synced_data.meta_request); |
| 579 |
aws_http_message_release(request); |
| 580 |
aws_ref_count_acquire(&impl->internal_ref); |
| 581 |
aws_linked_list_init(&session_creator->synced_data.query_queue); |
| 582 |
|
| 583 |
return session_creator; |
| 584 |
} |
| 585 |
|
| 586 |
static int s_s3express_get_creds( |
| 587 |
struct aws_s3express_credentials_provider *provider, |
| 588 |
const struct aws_credentials *original_credentials, |
| 589 |
const struct aws_credentials_properties_s3express *s3express_properties, |
| 590 |
aws_on_get_credentials_callback_fn callback, |
| 591 |
void *user_data) { |
| 592 |
if (s3express_properties->host.len == 0) { |
| 593 |
AWS_LOGF_ERROR( |
| 594 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 595 |
"(id=%p): The host property is empty to get credentials from S3 Express", |
| 596 |
(void *)provider); |
| 597 |
|
| 598 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 599 |
} |
| 600 |
|
| 601 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 602 |
|
| 603 |
struct aws_hash_element *session_creator_hash_element = NULL; |
| 604 |
int was_created = 0; |
| 605 |
struct aws_credentials *s3express_credentials = NULL; |
| 606 |
struct aws_byte_cursor access_key; |
| 607 |
AWS_ZERO_STRUCT(access_key); |
| 608 |
if (original_credentials) { |
| 609 |
access_key = aws_credentials_get_access_key_id(original_credentials); |
| 610 |
} |
| 611 |
|
| 612 |
uint64_t current_stamp = UINT64_MAX; |
| 613 |
aws_sys_clock_get_ticks(¤t_stamp); |
| 614 |
struct aws_string *hash_key = aws_encode_s3express_hash_key_new( |
| 615 |
provider->allocator, original_credentials, s3express_properties->host, s3express_properties->headers); |
| 616 |
uint64_t now_seconds = aws_timestamp_convert(current_stamp, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); |
| 617 |
|
| 618 |
s_credentials_provider_s3express_impl_lock_synced_data(impl); |
| 619 |
|
| 620 |
AWS_FATAL_ASSERT(!impl->synced_data.destroying); |
| 621 |
|
| 622 |
struct aws_s3express_session *session = NULL; |
| 623 |
int ret_code = aws_cache_find(impl->synced_data.cache, hash_key, (void **)&session); |
| 624 |
AWS_ASSERT(ret_code == AWS_OP_SUCCESS); |
| 625 |
if (session) { |
| 626 |
|
| 627 |
session->inactive = false; |
| 628 |
AWS_ASSERT(session->s3express_credentials != NULL); |
| 629 |
if (s_s3express_session_is_valid(session, now_seconds)) { |
| 630 |
s3express_credentials = session->s3express_credentials; |
| 631 |
|
| 632 |
aws_credentials_acquire(s3express_credentials); |
| 633 |
aws_string_destroy(hash_key); |
| 634 |
goto unlock; |
| 635 |
} else { |
| 636 |
|
| 637 |
aws_cache_remove(impl->synced_data.cache, hash_key); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
ret_code = aws_hash_table_create( |
| 643 |
&impl->synced_data.session_creator_table, hash_key, &session_creator_hash_element, &was_created); |
| 644 |
AWS_ASSERT(ret_code == AWS_OP_SUCCESS); |
| 645 |
(void)ret_code; |
| 646 |
|
| 647 |
|
| 648 |
if (was_created) { |
| 649 |
|
| 650 |
struct aws_s3express_session_creator *new_session_creator = |
| 651 |
s_session_creator_new(provider, original_credentials, s3express_properties); |
| 652 |
|
| 653 |
AWS_FATAL_ASSERT(new_session_creator); |
| 654 |
new_session_creator->hash_key = hash_key; |
| 655 |
session_creator_hash_element->value = new_session_creator; |
| 656 |
} else { |
| 657 |
aws_string_destroy(hash_key); |
| 658 |
} |
| 659 |
|
| 660 |
if (s3express_credentials == NULL) { |
| 661 |
|
| 662 |
struct aws_s3express_session_creator *session_creator = session_creator_hash_element->value; |
| 663 |
struct aws_query_callback_node *callback_node = |
| 664 |
aws_mem_acquire(provider->allocator, sizeof(struct aws_query_callback_node)); |
| 665 |
callback_node->get_cred_callback = callback; |
| 666 |
callback_node->get_cred_user_data = user_data; |
| 667 |
aws_linked_list_push_back(&session_creator->synced_data.query_queue, &callback_node->node); |
| 668 |
} |
| 669 |
unlock: |
| 670 |
s_credentials_provider_s3express_impl_unlock_synced_data(impl); |
| 671 |
if (s3express_credentials) { |
| 672 |
uint64_t expire_secs = aws_credentials_get_expiration_timepoint_seconds(s3express_credentials); |
| 673 |
AWS_LOGF_TRACE( |
| 674 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 675 |
"(id=%p): Found credentials from cache. Timestamp to expire is %" PRIu64 ", while now is %" PRIu64 ".", |
| 676 |
(void *)provider, |
| 677 |
expire_secs, |
| 678 |
now_seconds); |
| 679 |
|
| 680 |
callback(s3express_credentials, AWS_ERROR_SUCCESS, user_data); |
| 681 |
aws_credentials_release(s3express_credentials); |
| 682 |
return AWS_OP_SUCCESS; |
| 683 |
} |
| 684 |
return AWS_OP_SUCCESS; |
| 685 |
} |
| 686 |
|
| 687 |
static void s_finish_provider_destroy(struct aws_s3express_credentials_provider *provider) { |
| 688 |
AWS_LOGF_TRACE( |
| 689 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 690 |
"(id=%p): finishing destroying S3 Express credentials provider", |
| 691 |
(void *)provider); |
| 692 |
|
| 693 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 694 |
aws_hash_table_clean_up(&impl->synced_data.session_creator_table); |
| 695 |
aws_cache_destroy(impl->synced_data.cache); |
| 696 |
aws_credentials_release(impl->default_original_credentials); |
| 697 |
aws_credentials_provider_release(impl->default_original_credentials_provider); |
| 698 |
aws_mutex_clean_up(&impl->synced_data.lock); |
| 699 |
aws_mem_release(provider->allocator, impl->bg_refresh_task); |
| 700 |
|
| 701 |
if (provider && provider->shutdown_complete_callback) { |
| 702 |
provider->shutdown_complete_callback(provider->shutdown_user_data); |
| 703 |
} |
| 704 |
aws_mem_release(provider->allocator, provider); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
static void s_clean_up_background_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 709 |
(void)status; |
| 710 |
struct aws_s3express_credentials_provider *provider = arg; |
| 711 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 712 |
|
| 713 |
|
| 714 |
aws_event_loop_cancel_task(impl->bg_event_loop, impl->bg_refresh_task); |
| 715 |
aws_mem_release(provider->allocator, task); |
| 716 |
|
| 717 |
|
| 718 |
aws_ref_count_release(&impl->internal_ref); |
| 719 |
} |
| 720 |
|
| 721 |
static void s_external_destroy(struct aws_s3express_credentials_provider *provider) { |
| 722 |
AWS_LOGF_TRACE( |
| 723 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 724 |
"(id=%p): external refcount drops to zero, start destroying", |
| 725 |
(void *)provider); |
| 726 |
|
| 727 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 728 |
{ |
| 729 |
s_credentials_provider_s3express_impl_lock_synced_data(impl); |
| 730 |
impl->synced_data.destroying = true; |
| 731 |
aws_cache_clear(impl->synced_data.cache); |
| 732 |
for (struct aws_hash_iter iter = aws_hash_iter_begin(&impl->synced_data.session_creator_table); |
| 733 |
!aws_hash_iter_done(&iter); |
| 734 |
aws_hash_iter_next(&iter)) { |
| 735 |
struct aws_s3express_session_creator *session_creator = |
| 736 |
(struct aws_s3express_session_creator *)iter.element.value; |
| 737 |
|
| 738 |
aws_s3_meta_request_cancel(session_creator->synced_data.meta_request); |
| 739 |
} |
| 740 |
s_credentials_provider_s3express_impl_unlock_synced_data(impl); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
struct aws_task *clean_up_background_task = aws_mem_calloc(provider->allocator, 1, sizeof(struct aws_task)); |
| 745 |
aws_task_init(clean_up_background_task, s_clean_up_background_task, provider, "clean_up_s3express_background"); |
| 746 |
aws_event_loop_schedule_task_now(impl->bg_event_loop, clean_up_background_task); |
| 747 |
} |
| 748 |
|
| 749 |
static struct aws_s3express_credentials_provider_vtable s_aws_s3express_credentials_provider_vtable = { |
| 750 |
.get_credentials = s_s3express_get_creds, |
| 751 |
.destroy = s_external_destroy, |
| 752 |
}; |
| 753 |
|
| 754 |
static void s_schedule_bg_refresh(struct aws_s3express_credentials_provider *provider) { |
| 755 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 756 |
|
| 757 |
AWS_FATAL_ASSERT(impl->bg_event_loop != NULL); |
| 758 |
uint64_t current_stamp = UINT64_MAX; |
| 759 |
|
| 760 |
aws_high_res_clock_get_ticks(¤t_stamp); |
| 761 |
uint64_t interval_secs = impl->mock_test.bg_refresh_secs_override == 0 ? s_background_refresh_interval_secs |
| 762 |
: impl->mock_test.bg_refresh_secs_override; |
| 763 |
|
| 764 |
|
| 765 |
aws_event_loop_schedule_task_future( |
| 766 |
impl->bg_event_loop, |
| 767 |
impl->bg_refresh_task, |
| 768 |
current_stamp + aws_timestamp_convert(interval_secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 769 |
|
| 770 |
return; |
| 771 |
} |
| 772 |
|
| 773 |
static void s_refresh_session_list( |
| 774 |
struct aws_s3express_credentials_provider *provider, |
| 775 |
const struct aws_credentials *current_original_credentials) { |
| 776 |
|
| 777 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 778 |
uint64_t current_stamp = UINT64_MAX; |
| 779 |
aws_sys_clock_get_ticks(¤t_stamp); |
| 780 |
uint64_t now_seconds = aws_timestamp_convert(current_stamp, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); |
| 781 |
AWS_LOGF_TRACE( |
| 782 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p): background refreshing task in process", (void *)provider); |
| 783 |
{ |
| 784 |
s_credentials_provider_s3express_impl_lock_synced_data(impl); |
| 785 |
if (impl->synced_data.destroying) { |
| 786 |
|
| 787 |
s_credentials_provider_s3express_impl_unlock_synced_data(impl); |
| 788 |
return; |
| 789 |
} |
| 790 |
const struct aws_linked_list *session_list = |
| 791 |
aws_linked_hash_table_get_iteration_list(&impl->synced_data.cache->table); |
| 792 |
|
| 793 |
|
| 794 |
struct aws_linked_list_node *node = NULL; |
| 795 |
for (node = aws_linked_list_begin(session_list); node != aws_linked_list_end(session_list);) { |
| 796 |
|
| 797 |
struct aws_linked_hash_table_node *table_node = |
| 798 |
AWS_CONTAINER_OF(node, struct aws_linked_hash_table_node, node); |
| 799 |
node = aws_linked_list_next(node); |
| 800 |
struct aws_s3express_session *session = table_node->value; |
| 801 |
if (s_s3express_session_about_to_expire(session, now_seconds)) { |
| 802 |
if (session->inactive) { |
| 803 |
|
| 804 |
aws_cache_remove(impl->synced_data.cache, session->hash_key); |
| 805 |
} else { |
| 806 |
|
| 807 |
|
| 808 |
struct aws_hash_element *session_creator_hash_element = NULL; |
| 809 |
int was_created = 0; |
| 810 |
struct aws_string *hash_key = aws_string_new_from_string(provider->allocator, session->hash_key); |
| 811 |
int ret_code = aws_hash_table_create( |
| 812 |
&impl->synced_data.session_creator_table, |
| 813 |
hash_key, |
| 814 |
&session_creator_hash_element, |
| 815 |
&was_created); |
| 816 |
AWS_ASSERT(ret_code == AWS_OP_SUCCESS); |
| 817 |
(void)ret_code; |
| 818 |
if (was_created) { |
| 819 |
struct aws_string *current_creds_hash = aws_encode_s3express_hash_key_new( |
| 820 |
provider->allocator, |
| 821 |
current_original_credentials, |
| 822 |
aws_byte_cursor_from_string(session->host), |
| 823 |
session->headers); |
| 824 |
bool creds_match = aws_string_eq(current_creds_hash, hash_key); |
| 825 |
aws_string_destroy(current_creds_hash); |
| 826 |
if (!creds_match) { |
| 827 |
|
| 828 |
if (!s_s3express_session_is_valid(session, now_seconds)) { |
| 829 |
|
| 830 |
aws_cache_remove(impl->synced_data.cache, session->hash_key); |
| 831 |
} |
| 832 |
|
| 833 |
session->inactive = true; |
| 834 |
|
| 835 |
aws_string_destroy(hash_key); |
| 836 |
aws_hash_table_remove_element( |
| 837 |
&impl->synced_data.session_creator_table, session_creator_hash_element); |
| 838 |
goto unlock; |
| 839 |
} |
| 840 |
|
| 841 |
struct aws_credentials_properties_s3express s3express_properties = { |
| 842 |
.host = aws_byte_cursor_from_string(session->host), |
| 843 |
.headers = session->headers, |
| 844 |
}; |
| 845 |
if (session->region) { |
| 846 |
s3express_properties.region = aws_byte_cursor_from_string(session->region); |
| 847 |
} |
| 848 |
|
| 849 |
struct aws_s3express_session_creator *new_session_creator = |
| 850 |
s_session_creator_new(provider, current_original_credentials, &s3express_properties); |
| 851 |
AWS_FATAL_ASSERT(new_session_creator); |
| 852 |
new_session_creator->synced_data.session = session; |
| 853 |
session->creator = new_session_creator; |
| 854 |
new_session_creator->hash_key = hash_key; |
| 855 |
|
| 856 |
session_creator_hash_element->value = new_session_creator; |
| 857 |
} else { |
| 858 |
|
| 859 |
|
| 860 |
aws_string_destroy(hash_key); |
| 861 |
struct aws_s3express_session_creator *session_creator = session_creator_hash_element->value; |
| 862 |
AWS_FATAL_ASSERT(session_creator->synced_data.session == session); |
| 863 |
} |
| 864 |
session->inactive = true; |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
unlock: |
| 869 |
s_credentials_provider_s3express_impl_unlock_synced_data(impl); |
| 870 |
} |
| 871 |
s_schedule_bg_refresh(provider); |
| 872 |
} |
| 873 |
|
| 874 |
static void s_get_original_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) { |
| 875 |
struct aws_s3express_credentials_provider *provider = user_data; |
| 876 |
if (error_code) { |
| 877 |
AWS_LOGF_DEBUG( |
| 878 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 879 |
"id=%p: S3 Express Provider back ground refresh failed: Failed to fetch original credentials with " |
| 880 |
"error %s. Skipping refresh.", |
| 881 |
(void *)provider, |
| 882 |
aws_error_debug_str(aws_last_error())); |
| 883 |
|
| 884 |
s_schedule_bg_refresh(provider); |
| 885 |
return; |
| 886 |
} |
| 887 |
s_refresh_session_list(provider, credentials); |
| 888 |
} |
| 889 |
|
| 890 |
static void s_bg_refresh_task(struct aws_task *task, void *arg, enum aws_task_status status) { |
| 891 |
(void)task; |
| 892 |
if (status != AWS_TASK_STATUS_RUN_READY) { |
| 893 |
return; |
| 894 |
} |
| 895 |
|
| 896 |
struct aws_s3express_credentials_provider *provider = arg; |
| 897 |
struct aws_s3express_credentials_provider_impl *impl = provider->impl; |
| 898 |
if (impl->default_original_credentials) { |
| 899 |
s_refresh_session_list(provider, impl->default_original_credentials); |
| 900 |
} else { |
| 901 |
|
| 902 |
if (aws_credentials_provider_get_credentials( |
| 903 |
impl->default_original_credentials_provider, s_get_original_credentials_callback, provider)) { |
| 904 |
AWS_LOGF_DEBUG( |
| 905 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 906 |
"id=%p: S3 Express Provider back ground refresh failed: Failed to get original credentials from " |
| 907 |
"provider with error %s. Skipping refresh.", |
| 908 |
(void *)provider, |
| 909 |
aws_error_debug_str(aws_last_error())); |
| 910 |
|
| 911 |
s_schedule_bg_refresh(provider); |
| 912 |
return; |
| 913 |
} |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
void aws_s3express_credentials_provider_init_base( |
| 918 |
struct aws_s3express_credentials_provider *provider, |
| 919 |
struct aws_allocator *allocator, |
| 920 |
struct aws_s3express_credentials_provider_vtable *vtable, |
| 921 |
void *impl) { |
| 922 |
|
| 923 |
AWS_PRECONDITION(provider); |
| 924 |
AWS_PRECONDITION(vtable); |
| 925 |
|
| 926 |
provider->allocator = allocator; |
| 927 |
provider->vtable = vtable; |
| 928 |
provider->impl = impl; |
| 929 |
aws_ref_count_init(&provider->ref_count, provider, (aws_simple_completion_callback *)provider->vtable->destroy); |
| 930 |
} |
| 931 |
|
| 932 |
struct aws_s3express_credentials_provider *aws_s3express_credentials_provider_new_default( |
| 933 |
struct aws_allocator *allocator, |
| 934 |
const struct aws_s3express_credentials_provider_default_options *options) { |
| 935 |
|
| 936 |
if (!options->client) { |
| 937 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "a S3 client is necessary for querying S3 Express"); |
| 938 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 939 |
return NULL; |
| 940 |
} |
| 941 |
|
| 942 |
struct aws_s3express_credentials_provider *provider = NULL; |
| 943 |
struct aws_s3express_credentials_provider_impl *impl = NULL; |
| 944 |
|
| 945 |
aws_mem_acquire_many( |
| 946 |
allocator, |
| 947 |
2, |
| 948 |
&provider, |
| 949 |
sizeof(struct aws_s3express_credentials_provider), |
| 950 |
&impl, |
| 951 |
sizeof(struct aws_s3express_credentials_provider_impl)); |
| 952 |
|
| 953 |
AWS_LOGF_DEBUG(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "static: creating S3 Express credentials provider"); |
| 954 |
AWS_ZERO_STRUCT(*provider); |
| 955 |
AWS_ZERO_STRUCT(*impl); |
| 956 |
|
| 957 |
aws_s3express_credentials_provider_init_base( |
| 958 |
provider, allocator, &s_aws_s3express_credentials_provider_vtable, impl); |
| 959 |
|
| 960 |
aws_hash_table_init( |
| 961 |
&impl->synced_data.session_creator_table, |
| 962 |
allocator, |
| 963 |
10, |
| 964 |
aws_hash_string, |
| 965 |
aws_hash_callback_string_eq, |
| 966 |
NULL, |
| 967 |
NULL); |
| 968 |
|
| 969 |
impl->synced_data.cache = aws_cache_new_lru( |
| 970 |
allocator, |
| 971 |
aws_hash_string, |
| 972 |
(aws_hash_callback_eq_fn *)aws_string_eq, |
| 973 |
NULL, |
| 974 |
(aws_hash_callback_destroy_fn *)s_aws_s3express_session_destroy, |
| 975 |
s_default_cache_capacity); |
| 976 |
AWS_ASSERT(impl->synced_data.cache); |
| 977 |
|
| 978 |
|
| 979 |
impl->client = options->client; |
| 980 |
struct aws_signing_config_aws client_cached_config = impl->client->cached_signing_config->config; |
| 981 |
if (client_cached_config.credentials) { |
| 982 |
impl->default_original_credentials = client_cached_config.credentials; |
| 983 |
aws_credentials_acquire(impl->default_original_credentials); |
| 984 |
} else { |
| 985 |
impl->default_original_credentials_provider = |
| 986 |
aws_credentials_provider_acquire(client_cached_config.credentials_provider); |
| 987 |
} |
| 988 |
|
| 989 |
provider->shutdown_complete_callback = options->shutdown_complete_callback; |
| 990 |
provider->shutdown_user_data = options->shutdown_user_data; |
| 991 |
aws_mutex_init(&impl->synced_data.lock); |
| 992 |
aws_ref_count_init(&impl->internal_ref, provider, (aws_simple_completion_callback *)s_finish_provider_destroy); |
| 993 |
|
| 994 |
|
| 995 |
impl->bg_refresh_task = aws_mem_calloc(provider->allocator, 1, sizeof(struct aws_task)); |
| 996 |
aws_task_init(impl->bg_refresh_task, s_bg_refresh_task, provider, "s3express_background_refresh"); |
| 997 |
|
| 998 |
impl->bg_event_loop = aws_event_loop_group_get_next_loop(impl->client->client_bootstrap->event_loop_group); |
| 999 |
impl->mock_test.bg_refresh_secs_override = options->mock_test.bg_refresh_secs_override; |
| 1000 |
s_schedule_bg_refresh(provider); |
| 1001 |
|
| 1002 |
return provider; |
| 1003 |
} |
| 1004 |
|
| 1005 |
struct aws_s3express_credentials_provider *aws_s3express_credentials_provider_release( |
| 1006 |
struct aws_s3express_credentials_provider *provider) { |
| 1007 |
if (provider) { |
| 1008 |
aws_ref_count_release(&provider->ref_count); |
| 1009 |
} |
| 1010 |
return NULL; |
| 1011 |
} |
| 1012 |
|
| 1013 |
int aws_s3express_credentials_provider_get_credentials( |
| 1014 |
struct aws_s3express_credentials_provider *provider, |
| 1015 |
const struct aws_credentials *original_credentials, |
| 1016 |
const struct aws_credentials_properties_s3express *property, |
| 1017 |
aws_on_get_credentials_callback_fn callback, |
| 1018 |
void *user_data) { |
| 1019 |
|
| 1020 |
AWS_PRECONDITION(property); |
| 1021 |
AWS_PRECONDITION(provider); |
| 1022 |
AWS_ASSERT(provider->vtable->get_credentials); |
| 1023 |
|
| 1024 |
return provider->vtable->get_credentials(provider, original_credentials, property, callback, user_data); |
| 1025 |
} |