| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/credentials.h> |
| 7 |
|
| 8 |
#include <aws/auth/private/credentials_utils.h> |
| 9 |
#include <aws/common/clock.h> |
| 10 |
#include <aws/common/json.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
#include <aws/http/connection.h> |
| 13 |
#include <aws/http/connection_manager.h> |
| 14 |
#include <aws/http/request_response.h> |
| 15 |
#include <aws/http/status_code.h> |
| 16 |
#include <aws/io/channel_bootstrap.h> |
| 17 |
#include <aws/io/retry_strategy.h> |
| 18 |
#include <aws/io/socket.h> |
| 19 |
#include <aws/io/stream.h> |
| 20 |
#include <aws/io/tls_channel_handler.h> |
| 21 |
|
| 22 |
#include <inttypes.h> |
| 23 |
|
| 24 |
#define COGNITO_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS 5 |
| 25 |
#define COGNITO_MAX_RETRIES 3 |
| 26 |
#define HTTP_REQUEST_BODY_INITIAL_SIZE 1024 |
| 27 |
#define HTTP_RESPONSE_BODY_INITIAL_SIZE 4096 |
| 28 |
|
| 29 |
static void s_on_connection_manager_shutdown(void *user_data); |
| 30 |
static void s_on_connection_setup_fn(struct aws_http_connection *connection, int error_code, void *user_data); |
| 31 |
|
| 32 |
struct aws_cognito_login { |
| 33 |
struct aws_byte_cursor identity_provider_name; |
| 34 |
struct aws_byte_cursor identity_provider_token; |
| 35 |
struct aws_byte_buf login_buffer; |
| 36 |
}; |
| 37 |
|
| 38 |
static int s_aws_cognito_login_init( |
| 39 |
struct aws_cognito_login *login, |
| 40 |
struct aws_allocator *allocator, |
| 41 |
struct aws_byte_cursor identity_provider_name, |
| 42 |
struct aws_byte_cursor identity_provider_token) { |
| 43 |
AWS_ZERO_STRUCT(*login); |
| 44 |
|
| 45 |
login->identity_provider_name = identity_provider_name; |
| 46 |
login->identity_provider_token = identity_provider_token; |
| 47 |
|
| 48 |
return aws_byte_buf_init_cache_and_update_cursors( |
| 49 |
&login->login_buffer, allocator, &login->identity_provider_name, &login->identity_provider_token, NULL); |
| 50 |
} |
| 51 |
|
| 52 |
static void s_aws_cognito_login_clean_up(struct aws_cognito_login *login) { |
| 53 |
aws_byte_buf_clean_up(&login->login_buffer); |
| 54 |
|
| 55 |
AWS_ZERO_STRUCT(*login); |
| 56 |
} |
| 57 |
|
| 58 |
struct aws_credentials_provider_cognito_impl { |
| 59 |
struct aws_http_connection_manager *connection_manager; |
| 60 |
struct aws_retry_strategy *retry_strategy; |
| 61 |
const struct aws_auth_http_system_vtable *function_table; |
| 62 |
|
| 63 |
struct aws_string *endpoint; |
| 64 |
|
| 65 |
struct aws_string *identity; |
| 66 |
|
| 67 |
struct aws_array_list static_logins; |
| 68 |
|
| 69 |
struct aws_string *custom_role_arn; |
| 70 |
|
| 71 |
aws_credentials_provider_cognito_get_token_pairs_async_fn *get_token_pairs; |
| 72 |
void *get_token_pairs_user_data; |
| 73 |
}; |
| 74 |
|
| 75 |
struct cognito_user_data { |
| 76 |
struct aws_allocator *allocator; |
| 77 |
|
| 78 |
struct aws_credentials_provider *provider; |
| 79 |
|
| 80 |
aws_on_get_credentials_callback_fn *original_callback; |
| 81 |
void *original_user_data; |
| 82 |
|
| 83 |
struct aws_array_list dynamic_logins; |
| 84 |
|
| 85 |
struct aws_http_connection *connection; |
| 86 |
struct aws_http_message *get_credentials_request; |
| 87 |
struct aws_byte_buf request_body_buffer; |
| 88 |
struct aws_input_stream *request_body_stream; |
| 89 |
|
| 90 |
struct aws_retry_token *retry_token; |
| 91 |
struct aws_credentials *credentials; |
| 92 |
struct aws_byte_buf response_body; |
| 93 |
}; |
| 94 |
|
| 95 |
static void s_user_data_reset(struct cognito_user_data *user_data) { |
| 96 |
aws_byte_buf_clean_up(&user_data->request_body_buffer); |
| 97 |
|
| 98 |
user_data->request_body_stream = aws_input_stream_release(user_data->request_body_stream); |
| 99 |
user_data->get_credentials_request = aws_http_message_release(user_data->get_credentials_request); |
| 100 |
|
| 101 |
struct aws_credentials_provider_cognito_impl *impl = user_data->provider->impl; |
| 102 |
if (user_data->connection != NULL) { |
| 103 |
impl->function_table->aws_http_connection_manager_release_connection( |
| 104 |
impl->connection_manager, user_data->connection); |
| 105 |
user_data->connection = NULL; |
| 106 |
} |
| 107 |
|
| 108 |
aws_byte_buf_reset(&user_data->response_body, false); |
| 109 |
} |
| 110 |
|
| 111 |
static void s_user_data_destroy(struct cognito_user_data *user_data) { |
| 112 |
if (user_data == NULL) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
s_user_data_reset(user_data); |
| 117 |
|
| 118 |
aws_byte_buf_clean_up(&user_data->response_body); |
| 119 |
aws_retry_token_release(user_data->retry_token); |
| 120 |
aws_credentials_provider_release(user_data->provider); |
| 121 |
aws_credentials_release(user_data->credentials); |
| 122 |
|
| 123 |
size_t dynamic_logins_count = aws_array_list_length(&user_data->dynamic_logins); |
| 124 |
for (size_t i = 0; i < dynamic_logins_count; ++i) { |
| 125 |
struct aws_cognito_login login; |
| 126 |
AWS_ZERO_STRUCT(login); |
| 127 |
|
| 128 |
aws_array_list_get_at(&user_data->dynamic_logins, &login, i); |
| 129 |
|
| 130 |
s_aws_cognito_login_clean_up(&login); |
| 131 |
} |
| 132 |
aws_array_list_clean_up(&user_data->dynamic_logins); |
| 133 |
|
| 134 |
aws_mem_release(user_data->allocator, user_data); |
| 135 |
} |
| 136 |
|
| 137 |
static struct cognito_user_data *s_user_data_new( |
| 138 |
struct aws_credentials_provider *provider, |
| 139 |
aws_on_get_credentials_callback_fn callback, |
| 140 |
void *user_data) { |
| 141 |
|
| 142 |
struct aws_allocator *allocator = provider->allocator; |
| 143 |
struct cognito_user_data *cognito_user_data = aws_mem_calloc(allocator, 1, sizeof(struct cognito_user_data)); |
| 144 |
cognito_user_data->allocator = allocator; |
| 145 |
|
| 146 |
aws_byte_buf_init(&cognito_user_data->response_body, cognito_user_data->allocator, HTTP_RESPONSE_BODY_INITIAL_SIZE); |
| 147 |
|
| 148 |
cognito_user_data->provider = aws_credentials_provider_acquire(provider); |
| 149 |
cognito_user_data->original_callback = callback; |
| 150 |
cognito_user_data->original_user_data = user_data; |
| 151 |
|
| 152 |
aws_array_list_init_dynamic(&cognito_user_data->dynamic_logins, allocator, 0, sizeof(struct aws_cognito_login)); |
| 153 |
|
| 154 |
return cognito_user_data; |
| 155 |
} |
| 156 |
|
| 157 |
static void s_finalize_credentials_query(struct cognito_user_data *user_data, int error_code) { |
| 158 |
AWS_FATAL_ASSERT(user_data != NULL); |
| 159 |
|
| 160 |
if (user_data->credentials == NULL && error_code == AWS_ERROR_SUCCESS) { |
| 161 |
error_code = AWS_AUTH_CREDENTIALS_PROVIDER_COGNITO_SOURCE_FAILURE; |
| 162 |
} |
| 163 |
|
| 164 |
(user_data->original_callback)(user_data->credentials, error_code, user_data->original_user_data); |
| 165 |
|
| 166 |
s_user_data_destroy(user_data); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
AWS_STATIC_STRING_FROM_LITERAL(s_credentials_key, "Credentials"); |
| 171 |
AWS_STATIC_STRING_FROM_LITERAL(s_access_key_id_name, "AccessKeyId"); |
| 172 |
AWS_STATIC_STRING_FROM_LITERAL(s_secret_access_key_name, "SecretKey"); |
| 173 |
AWS_STATIC_STRING_FROM_LITERAL(s_session_token_name, "SessionToken"); |
| 174 |
AWS_STATIC_STRING_FROM_LITERAL(s_expiration_name, "Expiration"); |
| 175 |
|
| 176 |
static int s_parse_credentials_from_response(struct cognito_user_data *user_data) { |
| 177 |
|
| 178 |
int result = AWS_OP_ERR; |
| 179 |
|
| 180 |
struct aws_json_value *response_document = |
| 181 |
aws_json_value_new_from_string(user_data->allocator, aws_byte_cursor_from_buf(&user_data->response_body)); |
| 182 |
if (response_document == NULL) { |
| 183 |
goto done; |
| 184 |
} |
| 185 |
|
| 186 |
struct aws_json_value *credentials_entry = |
| 187 |
aws_json_value_get_from_object(response_document, aws_byte_cursor_from_string(s_credentials_key)); |
| 188 |
if (credentials_entry == NULL) { |
| 189 |
goto done; |
| 190 |
} |
| 191 |
|
| 192 |
struct aws_parse_credentials_from_json_doc_options credentials_parse_options = { |
| 193 |
.access_key_id_name = aws_string_c_str(s_access_key_id_name), |
| 194 |
.secret_access_key_name = aws_string_c_str(s_secret_access_key_name), |
| 195 |
.token_name = aws_string_c_str(s_session_token_name), |
| 196 |
.expiration_name = aws_string_c_str(s_expiration_name), |
| 197 |
.expiration_format = AWS_PCEF_NUMBER_UNIX_EPOCH, |
| 198 |
.token_required = true, |
| 199 |
.expiration_required = true, |
| 200 |
}; |
| 201 |
|
| 202 |
user_data->credentials = |
| 203 |
aws_parse_credentials_from_aws_json_object(user_data->allocator, credentials_entry, &credentials_parse_options); |
| 204 |
if (user_data->credentials == NULL) { |
| 205 |
goto done; |
| 206 |
} |
| 207 |
|
| 208 |
result = AWS_OP_SUCCESS; |
| 209 |
|
| 210 |
done: |
| 211 |
|
| 212 |
aws_json_value_destroy(response_document); |
| 213 |
|
| 214 |
if (result != AWS_OP_SUCCESS) { |
| 215 |
aws_raise_error(AWS_AUTH_PROVIDER_PARSER_UNEXPECTED_RESPONSE); |
| 216 |
} |
| 217 |
|
| 218 |
return result; |
| 219 |
} |
| 220 |
|
| 221 |
static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) { |
| 222 |
(void)token; |
| 223 |
struct cognito_user_data *provider_user_data = user_data; |
| 224 |
|
| 225 |
if (error_code != AWS_ERROR_SUCCESS) { |
| 226 |
AWS_LOGF_ERROR( |
| 227 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 228 |
"(id=%p): Cognito credentials provider retry task failed: %s", |
| 229 |
(void *)provider_user_data->provider, |
| 230 |
aws_error_str(error_code)); |
| 231 |
s_finalize_credentials_query(user_data, error_code); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
s_user_data_reset(provider_user_data); |
| 236 |
|
| 237 |
struct aws_credentials_provider_cognito_impl *impl = provider_user_data->provider->impl; |
| 238 |
|
| 239 |
impl->function_table->aws_http_connection_manager_acquire_connection( |
| 240 |
impl->connection_manager, s_on_connection_setup_fn, provider_user_data); |
| 241 |
} |
| 242 |
|
| 243 |
static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 244 |
|
| 245 |
struct cognito_user_data *provider_user_data = user_data; |
| 246 |
struct aws_credentials_provider_cognito_impl *impl = provider_user_data->provider->impl; |
| 247 |
|
| 248 |
int http_response_code = 0; |
| 249 |
impl->function_table->aws_http_stream_get_incoming_response_status(stream, &http_response_code); |
| 250 |
|
| 251 |
if (http_response_code != 200) { |
| 252 |
error_code = AWS_AUTH_CREDENTIALS_PROVIDER_HTTP_STATUS_FAILURE; |
| 253 |
} |
| 254 |
|
| 255 |
impl->function_table->aws_http_stream_release(stream); |
| 256 |
|
| 257 |
AWS_LOGF_DEBUG( |
| 258 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 259 |
"(id=%p): GetCredentialsForIdentity call completed with http status %d", |
| 260 |
(void *)provider_user_data->provider, |
| 261 |
http_response_code); |
| 262 |
|
| 263 |
if (http_response_code == AWS_HTTP_STATUS_CODE_200_OK) { |
| 264 |
aws_retry_token_record_success(provider_user_data->retry_token); |
| 265 |
|
| 266 |
if (s_parse_credentials_from_response(provider_user_data) == AWS_OP_SUCCESS) { |
| 267 |
s_finalize_credentials_query(user_data, AWS_ERROR_SUCCESS); |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
AWS_LOGF_ERROR( |
| 272 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 273 |
"(id=%p): Cognito credentials provider failed to parse GetCredentialsForIdentity response", |
| 274 |
(void *)provider_user_data->provider); |
| 275 |
|
| 276 |
error_code = AWS_AUTH_PROVIDER_PARSER_UNEXPECTED_RESPONSE; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
impl->function_table->aws_http_connection_close(provider_user_data->connection); |
| 283 |
|
| 284 |
enum aws_retry_error_type error_type = |
| 285 |
aws_credentials_provider_compute_retry_error_type(http_response_code, error_code); |
| 286 |
bool can_retry = http_response_code == 0 || error_type != AWS_RETRY_ERROR_TYPE_CLIENT_ERROR; |
| 287 |
if (!can_retry) { |
| 288 |
s_finalize_credentials_query(user_data, error_code); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
if (aws_retry_strategy_schedule_retry( |
| 293 |
provider_user_data->retry_token, error_type, s_on_retry_ready, provider_user_data)) { |
| 294 |
error_code = aws_last_error(); |
| 295 |
AWS_LOGF_ERROR( |
| 296 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 297 |
"(id=%p): Cognito credentials provider failed to schedule retry: %s", |
| 298 |
(void *)provider_user_data->provider, |
| 299 |
aws_error_str(error_code)); |
| 300 |
s_finalize_credentials_query(user_data, error_code); |
| 301 |
return; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
static int s_on_incoming_body_fn(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) { |
| 306 |
(void)stream; |
| 307 |
|
| 308 |
struct cognito_user_data *provider_user_data = user_data; |
| 309 |
return aws_byte_buf_append_dynamic(&provider_user_data->response_body, data); |
| 310 |
} |
| 311 |
|
| 312 |
static int s_add_login_tokens_to_json( |
| 313 |
struct aws_json_value *json_value, |
| 314 |
struct aws_array_list *logins, |
| 315 |
struct aws_allocator *allocator) { |
| 316 |
size_t login_count = aws_array_list_length(logins); |
| 317 |
for (size_t i = 0; i < login_count; ++i) { |
| 318 |
struct aws_cognito_login login; |
| 319 |
if (aws_array_list_get_at(logins, &login, i)) { |
| 320 |
return AWS_OP_ERR; |
| 321 |
} |
| 322 |
|
| 323 |
struct aws_json_value *login_value_string = aws_json_value_new_string(allocator, login.identity_provider_token); |
| 324 |
if (login_value_string == NULL) { |
| 325 |
return AWS_OP_ERR; |
| 326 |
} |
| 327 |
|
| 328 |
if (aws_json_value_add_to_object(json_value, login.identity_provider_name, login_value_string)) { |
| 329 |
aws_json_value_destroy(login_value_string); |
| 330 |
return AWS_OP_ERR; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return AWS_OP_SUCCESS; |
| 335 |
} |
| 336 |
|
| 337 |
AWS_STATIC_STRING_FROM_LITERAL(s_identity_id_key, "IdentityId"); |
| 338 |
AWS_STATIC_STRING_FROM_LITERAL(s_custom_role_arn_key, "CustomRoleArn"); |
| 339 |
AWS_STATIC_STRING_FROM_LITERAL(s_logins_key, "Logins"); |
| 340 |
|
| 341 |
int s_create_get_credentials_for_identity_body_buffer( |
| 342 |
struct aws_byte_buf *buffer, |
| 343 |
struct cognito_user_data *provider_user_data) { |
| 344 |
struct aws_allocator *allocator = provider_user_data->allocator; |
| 345 |
struct aws_credentials_provider_cognito_impl *impl = provider_user_data->provider->impl; |
| 346 |
|
| 347 |
int result = AWS_OP_ERR; |
| 348 |
|
| 349 |
struct aws_json_value *json_body = aws_json_value_new_object(allocator); |
| 350 |
if (json_body == NULL) { |
| 351 |
return AWS_OP_ERR; |
| 352 |
} |
| 353 |
|
| 354 |
struct aws_json_value *identity_string = |
| 355 |
aws_json_value_new_string(allocator, aws_byte_cursor_from_string(impl->identity)); |
| 356 |
if (identity_string == NULL) { |
| 357 |
goto done; |
| 358 |
} |
| 359 |
|
| 360 |
if (aws_json_value_add_to_object(json_body, aws_byte_cursor_from_string(s_identity_id_key), identity_string)) { |
| 361 |
aws_json_value_destroy(identity_string); |
| 362 |
goto done; |
| 363 |
} |
| 364 |
|
| 365 |
if (impl->custom_role_arn != NULL) { |
| 366 |
struct aws_json_value *custom_role_arn_string = |
| 367 |
aws_json_value_new_string(allocator, aws_byte_cursor_from_string(impl->custom_role_arn)); |
| 368 |
if (custom_role_arn_string == NULL) { |
| 369 |
goto done; |
| 370 |
} |
| 371 |
|
| 372 |
if (aws_json_value_add_to_object( |
| 373 |
json_body, aws_byte_cursor_from_string(s_custom_role_arn_key), custom_role_arn_string)) { |
| 374 |
aws_json_value_destroy(custom_role_arn_string); |
| 375 |
goto done; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
size_t static_login_count = aws_array_list_length(&impl->static_logins); |
| 380 |
size_t dynamic_login_count = aws_array_list_length(&provider_user_data->dynamic_logins); |
| 381 |
if (static_login_count + dynamic_login_count > 0) { |
| 382 |
struct aws_json_value *logins = aws_json_value_new_object(allocator); |
| 383 |
if (logins == NULL) { |
| 384 |
goto done; |
| 385 |
} |
| 386 |
|
| 387 |
if (aws_json_value_add_to_object(json_body, aws_byte_cursor_from_string(s_logins_key), logins)) { |
| 388 |
aws_json_value_destroy(logins); |
| 389 |
goto done; |
| 390 |
} |
| 391 |
|
| 392 |
if (s_add_login_tokens_to_json(logins, &impl->static_logins, allocator)) { |
| 393 |
goto done; |
| 394 |
} |
| 395 |
|
| 396 |
if (s_add_login_tokens_to_json(logins, &provider_user_data->dynamic_logins, allocator)) { |
| 397 |
goto done; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
if (aws_byte_buf_append_json_string(json_body, buffer)) { |
| 402 |
goto done; |
| 403 |
} |
| 404 |
|
| 405 |
result = AWS_OP_SUCCESS; |
| 406 |
|
| 407 |
done: |
| 408 |
|
| 409 |
aws_json_value_destroy(json_body); |
| 410 |
|
| 411 |
return result; |
| 412 |
} |
| 413 |
|
| 414 |
static struct aws_http_header s_content_type_header = { |
| 415 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("content-type"), |
| 416 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("application/x-amz-json-1.1"), |
| 417 |
}; |
| 418 |
|
| 419 |
AWS_STATIC_STRING_FROM_LITERAL(s_get_credentials_for_identity_path, "/"); |
| 420 |
|
| 421 |
static struct aws_http_header s_x_amz_target_header = { |
| 422 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("X-Amz-Target"), |
| 423 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("AWSCognitoIdentityService.GetCredentialsForIdentity"), |
| 424 |
}; |
| 425 |
|
| 426 |
static int s_create_get_credentials_for_identity_request(struct cognito_user_data *provider_user_data) { |
| 427 |
struct aws_credentials_provider_cognito_impl *impl = provider_user_data->provider->impl; |
| 428 |
|
| 429 |
struct aws_byte_buf body_buffer; |
| 430 |
AWS_ZERO_STRUCT(body_buffer); |
| 431 |
|
| 432 |
struct aws_input_stream *body_stream = NULL; |
| 433 |
struct aws_http_message *request = aws_http_message_new_request(provider_user_data->allocator); |
| 434 |
if (request == NULL) { |
| 435 |
return AWS_OP_ERR; |
| 436 |
} |
| 437 |
|
| 438 |
if (aws_http_message_set_request_method(request, aws_http_method_post)) { |
| 439 |
goto on_error; |
| 440 |
} |
| 441 |
|
| 442 |
if (aws_http_message_set_request_path(request, aws_byte_cursor_from_string(s_get_credentials_for_identity_path))) { |
| 443 |
goto on_error; |
| 444 |
} |
| 445 |
|
| 446 |
struct aws_http_header host_header = { |
| 447 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("host"), |
| 448 |
.value = aws_byte_cursor_from_string(impl->endpoint), |
| 449 |
}; |
| 450 |
|
| 451 |
if (aws_http_message_add_header(request, host_header)) { |
| 452 |
goto on_error; |
| 453 |
} |
| 454 |
|
| 455 |
if (aws_http_message_add_header(request, s_content_type_header)) { |
| 456 |
goto on_error; |
| 457 |
} |
| 458 |
|
| 459 |
if (aws_http_message_add_header(request, s_x_amz_target_header)) { |
| 460 |
goto on_error; |
| 461 |
} |
| 462 |
|
| 463 |
if (aws_byte_buf_init(&body_buffer, provider_user_data->allocator, HTTP_REQUEST_BODY_INITIAL_SIZE)) { |
| 464 |
goto on_error; |
| 465 |
} |
| 466 |
|
| 467 |
if (s_create_get_credentials_for_identity_body_buffer(&body_buffer, provider_user_data)) { |
| 468 |
goto on_error; |
| 469 |
} |
| 470 |
|
| 471 |
char content_length[21]; |
| 472 |
AWS_ZERO_ARRAY(content_length); |
| 473 |
snprintf(content_length, sizeof(content_length), "%" PRIu64, (uint64_t)body_buffer.len); |
| 474 |
|
| 475 |
struct aws_http_header content_length_header = { |
| 476 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"), |
| 477 |
.value = aws_byte_cursor_from_c_str(content_length), |
| 478 |
}; |
| 479 |
|
| 480 |
if (aws_http_message_add_header(request, content_length_header)) { |
| 481 |
goto on_error; |
| 482 |
} |
| 483 |
|
| 484 |
struct aws_byte_cursor payload_cur = aws_byte_cursor_from_buf(&body_buffer); |
| 485 |
body_stream = aws_input_stream_new_from_cursor(provider_user_data->allocator, &payload_cur); |
| 486 |
if (body_stream == NULL) { |
| 487 |
goto on_error; |
| 488 |
} |
| 489 |
|
| 490 |
aws_http_message_set_body_stream(request, body_stream); |
| 491 |
|
| 492 |
provider_user_data->get_credentials_request = request; |
| 493 |
provider_user_data->request_body_buffer = body_buffer; |
| 494 |
provider_user_data->request_body_stream = body_stream; |
| 495 |
|
| 496 |
return AWS_OP_SUCCESS; |
| 497 |
|
| 498 |
on_error: |
| 499 |
|
| 500 |
aws_byte_buf_clean_up(&body_buffer); |
| 501 |
aws_input_stream_release(body_stream); |
| 502 |
aws_http_message_release(request); |
| 503 |
|
| 504 |
return AWS_OP_ERR; |
| 505 |
} |
| 506 |
|
| 507 |
static void s_on_connection_setup_fn(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 508 |
struct cognito_user_data *wrapped_user_data = user_data; |
| 509 |
struct aws_http_stream *stream = NULL; |
| 510 |
struct aws_credentials_provider_cognito_impl *impl = wrapped_user_data->provider->impl; |
| 511 |
|
| 512 |
if (connection == NULL) { |
| 513 |
AWS_LOGF_ERROR( |
| 514 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 515 |
"(id=%p): Cognito credentials provider failed to acquire http connection: %s", |
| 516 |
(void *)wrapped_user_data->provider, |
| 517 |
aws_error_debug_str(error_code)); |
| 518 |
goto on_error; |
| 519 |
} |
| 520 |
|
| 521 |
wrapped_user_data->connection = connection; |
| 522 |
if (s_create_get_credentials_for_identity_request(wrapped_user_data)) { |
| 523 |
error_code = aws_last_error(); |
| 524 |
AWS_LOGF_ERROR( |
| 525 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 526 |
"(id=%p): Cognito credentials provider failed to create http request: %s", |
| 527 |
(void *)wrapped_user_data->provider, |
| 528 |
aws_error_debug_str(error_code)); |
| 529 |
goto on_error; |
| 530 |
} |
| 531 |
|
| 532 |
struct aws_http_make_request_options options = { |
| 533 |
.user_data = user_data, |
| 534 |
.request = wrapped_user_data->get_credentials_request, |
| 535 |
.self_size = sizeof(struct aws_http_make_request_options), |
| 536 |
.on_response_headers = NULL, |
| 537 |
.on_response_header_block_done = NULL, |
| 538 |
.on_response_body = s_on_incoming_body_fn, |
| 539 |
.on_complete = s_on_stream_complete_fn, |
| 540 |
}; |
| 541 |
|
| 542 |
stream = impl->function_table->aws_http_connection_make_request(connection, &options); |
| 543 |
if (!stream) { |
| 544 |
error_code = aws_last_error(); |
| 545 |
AWS_LOGF_ERROR( |
| 546 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 547 |
"(id=%p): Cognito credentials provider failed to create http stream: %s", |
| 548 |
(void *)wrapped_user_data->provider, |
| 549 |
aws_error_debug_str(error_code)); |
| 550 |
goto on_error; |
| 551 |
} |
| 552 |
|
| 553 |
if (impl->function_table->aws_http_stream_activate(stream)) { |
| 554 |
error_code = aws_last_error(); |
| 555 |
AWS_LOGF_ERROR( |
| 556 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 557 |
"(id=%p): Cognito credentials provider failed to activate http stream: %s", |
| 558 |
(void *)wrapped_user_data->provider, |
| 559 |
aws_error_debug_str(error_code)); |
| 560 |
goto on_error; |
| 561 |
} |
| 562 |
|
| 563 |
return; |
| 564 |
|
| 565 |
on_error: |
| 566 |
|
| 567 |
impl->function_table->aws_http_stream_release(stream); |
| 568 |
s_finalize_credentials_query(wrapped_user_data, error_code); |
| 569 |
} |
| 570 |
|
| 571 |
static void s_on_get_token_pairs_completion( |
| 572 |
struct aws_cognito_identity_provider_token_pair *logins, |
| 573 |
size_t login_count, |
| 574 |
int error_code, |
| 575 |
void *completion_user_data) { |
| 576 |
|
| 577 |
struct cognito_user_data *wrapped_user_data = completion_user_data; |
| 578 |
struct aws_credentials_provider_cognito_impl *impl = wrapped_user_data->provider->impl; |
| 579 |
|
| 580 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 581 |
for (size_t i = 0; i < login_count; i++) { |
| 582 |
struct aws_cognito_identity_provider_token_pair *login_pair = &logins[i]; |
| 583 |
|
| 584 |
struct aws_cognito_login login; |
| 585 |
AWS_ZERO_STRUCT(login); |
| 586 |
|
| 587 |
s_aws_cognito_login_init( |
| 588 |
&login, |
| 589 |
wrapped_user_data->allocator, |
| 590 |
login_pair->identity_provider_name, |
| 591 |
login_pair->identity_provider_token); |
| 592 |
aws_array_list_push_back(&wrapped_user_data->dynamic_logins, &login); |
| 593 |
} |
| 594 |
|
| 595 |
impl->function_table->aws_http_connection_manager_acquire_connection( |
| 596 |
impl->connection_manager, s_on_connection_setup_fn, wrapped_user_data); |
| 597 |
} else { |
| 598 |
AWS_LOGF_ERROR( |
| 599 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 600 |
"(id=%p): Cognito credentials provider failed to dynamically append token pairs on get credentials " |
| 601 |
"invocation: %s", |
| 602 |
(void *)wrapped_user_data->provider, |
| 603 |
aws_error_debug_str(error_code)); |
| 604 |
s_finalize_credentials_query(wrapped_user_data, error_code); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
static void s_on_retry_token_acquired( |
| 609 |
struct aws_retry_strategy *strategy, |
| 610 |
int error_code, |
| 611 |
struct aws_retry_token *token, |
| 612 |
void *user_data) { |
| 613 |
(void)strategy; |
| 614 |
struct cognito_user_data *wrapped_user_data = user_data; |
| 615 |
|
| 616 |
if (token == NULL) { |
| 617 |
AWS_LOGF_ERROR( |
| 618 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 619 |
"(id=%p): Cognito credentials provider failed to acquire retry token: %s", |
| 620 |
(void *)wrapped_user_data->provider, |
| 621 |
aws_error_debug_str(error_code)); |
| 622 |
s_finalize_credentials_query(wrapped_user_data, error_code); |
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
wrapped_user_data->retry_token = token; |
| 627 |
|
| 628 |
struct aws_credentials_provider_cognito_impl *impl = wrapped_user_data->provider->impl; |
| 629 |
|
| 630 |
if (impl->get_token_pairs != NULL) { |
| 631 |
if ((*impl->get_token_pairs)( |
| 632 |
impl->get_token_pairs_user_data, s_on_get_token_pairs_completion, wrapped_user_data)) { |
| 633 |
s_finalize_credentials_query(wrapped_user_data, aws_last_error()); |
| 634 |
} |
| 635 |
} else { |
| 636 |
s_on_get_token_pairs_completion(NULL, 0, AWS_ERROR_SUCCESS, wrapped_user_data); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
static int s_credentials_provider_cognito_get_credentials_async( |
| 641 |
struct aws_credentials_provider *provider, |
| 642 |
aws_on_get_credentials_callback_fn callback, |
| 643 |
void *user_data) { |
| 644 |
|
| 645 |
struct aws_credentials_provider_cognito_impl *impl = provider->impl; |
| 646 |
|
| 647 |
struct cognito_user_data *wrapped_user_data = s_user_data_new(provider, callback, user_data); |
| 648 |
if (wrapped_user_data == NULL) { |
| 649 |
goto on_error; |
| 650 |
} |
| 651 |
|
| 652 |
if (aws_retry_strategy_acquire_retry_token( |
| 653 |
impl->retry_strategy, NULL, s_on_retry_token_acquired, wrapped_user_data, 100)) { |
| 654 |
AWS_LOGF_ERROR( |
| 655 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 656 |
"(id=%p): Cognito credentials provider failed to acquire retry token with error %s", |
| 657 |
(void *)provider, |
| 658 |
aws_error_debug_str(aws_last_error())); |
| 659 |
goto on_error; |
| 660 |
} |
| 661 |
|
| 662 |
return AWS_OP_SUCCESS; |
| 663 |
|
| 664 |
on_error: |
| 665 |
|
| 666 |
s_user_data_destroy(wrapped_user_data); |
| 667 |
|
| 668 |
return AWS_OP_ERR; |
| 669 |
} |
| 670 |
|
| 671 |
static void s_credentials_provider_cognito_destroy(struct aws_credentials_provider *provider) { |
| 672 |
struct aws_credentials_provider_cognito_impl *impl = provider->impl; |
| 673 |
if (impl == NULL) { |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
if (impl->connection_manager) { |
| 681 |
impl->function_table->aws_http_connection_manager_release(impl->connection_manager); |
| 682 |
} else { |
| 683 |
|
| 684 |
|
| 685 |
s_on_connection_manager_shutdown(provider); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
} |
| 690 |
|
| 691 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_cognito_vtable = { |
| 692 |
.get_credentials = s_credentials_provider_cognito_get_credentials_async, |
| 693 |
.destroy = s_credentials_provider_cognito_destroy, |
| 694 |
}; |
| 695 |
|
| 696 |
static void s_on_connection_manager_shutdown(void *user_data) { |
| 697 |
struct aws_credentials_provider *provider = user_data; |
| 698 |
|
| 699 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 700 |
|
| 701 |
struct aws_credentials_provider_cognito_impl *impl = provider->impl; |
| 702 |
|
| 703 |
aws_retry_strategy_release(impl->retry_strategy); |
| 704 |
|
| 705 |
aws_string_destroy(impl->endpoint); |
| 706 |
aws_string_destroy(impl->identity); |
| 707 |
aws_string_destroy(impl->custom_role_arn); |
| 708 |
|
| 709 |
for (size_t i = 0; i < aws_array_list_length(&impl->static_logins); ++i) { |
| 710 |
struct aws_cognito_login login; |
| 711 |
if (aws_array_list_get_at(&impl->static_logins, &login, i)) { |
| 712 |
continue; |
| 713 |
} |
| 714 |
|
| 715 |
s_aws_cognito_login_clean_up(&login); |
| 716 |
} |
| 717 |
|
| 718 |
aws_array_list_clean_up(&impl->static_logins); |
| 719 |
|
| 720 |
aws_mem_release(provider->allocator, provider); |
| 721 |
} |
| 722 |
|
| 723 |
static int s_validate_options(const struct aws_credentials_provider_cognito_options *options) { |
| 724 |
if (options == NULL) { |
| 725 |
return AWS_OP_ERR; |
| 726 |
} |
| 727 |
|
| 728 |
if (options->tls_ctx == NULL) { |
| 729 |
AWS_LOGF_ERROR( |
| 730 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 731 |
"(static) Cognito credentials provider options must include a TLS context"); |
| 732 |
return AWS_OP_ERR; |
| 733 |
} |
| 734 |
|
| 735 |
if (options->bootstrap == NULL) { |
| 736 |
AWS_LOGF_ERROR( |
| 737 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 738 |
"(static) Cognito credentials provider options must include a client bootstrap"); |
| 739 |
return AWS_OP_ERR; |
| 740 |
} |
| 741 |
|
| 742 |
if (options->endpoint.len == 0) { |
| 743 |
AWS_LOGF_ERROR( |
| 744 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 745 |
"(static) Cognito credentials provider options must have a non-empty endpoint"); |
| 746 |
return AWS_OP_ERR; |
| 747 |
} |
| 748 |
|
| 749 |
if (options->identity.len == 0) { |
| 750 |
AWS_LOGF_ERROR( |
| 751 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 752 |
"(static) Cognito credentials provider options must have a non-empty identity"); |
| 753 |
return AWS_OP_ERR; |
| 754 |
} |
| 755 |
|
| 756 |
return AWS_OP_SUCCESS; |
| 757 |
} |
| 758 |
|
| 759 |
struct aws_credentials_provider *aws_credentials_provider_new_cognito( |
| 760 |
struct aws_allocator *allocator, |
| 761 |
const struct aws_credentials_provider_cognito_options *options) { |
| 762 |
|
| 763 |
struct aws_credentials_provider *provider = NULL; |
| 764 |
struct aws_credentials_provider_cognito_impl *impl = NULL; |
| 765 |
|
| 766 |
if (s_validate_options(options)) { |
| 767 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 768 |
return NULL; |
| 769 |
} |
| 770 |
|
| 771 |
aws_mem_acquire_many( |
| 772 |
allocator, |
| 773 |
2, |
| 774 |
&provider, |
| 775 |
sizeof(struct aws_credentials_provider), |
| 776 |
&impl, |
| 777 |
sizeof(struct aws_credentials_provider_cognito_impl)); |
| 778 |
|
| 779 |
if (!provider) { |
| 780 |
return NULL; |
| 781 |
} |
| 782 |
|
| 783 |
AWS_ZERO_STRUCT(*provider); |
| 784 |
AWS_ZERO_STRUCT(*impl); |
| 785 |
|
| 786 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_cognito_vtable, impl); |
| 787 |
|
| 788 |
struct aws_tls_connection_options tls_connection_options; |
| 789 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 790 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, options->tls_ctx); |
| 791 |
struct aws_byte_cursor host = options->endpoint; |
| 792 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &host)) { |
| 793 |
AWS_LOGF_ERROR( |
| 794 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 795 |
"(id=%p): Cognito credentials provider failed to create tls connection options with error %s", |
| 796 |
(void *)provider, |
| 797 |
aws_error_debug_str(aws_last_error())); |
| 798 |
goto on_error; |
| 799 |
} |
| 800 |
|
| 801 |
struct aws_socket_options socket_options; |
| 802 |
AWS_ZERO_STRUCT(socket_options); |
| 803 |
socket_options.type = AWS_SOCKET_STREAM; |
| 804 |
socket_options.domain = AWS_SOCKET_IPV4; |
| 805 |
socket_options.connect_timeout_ms = (uint32_t)aws_timestamp_convert( |
| 806 |
COGNITO_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 807 |
|
| 808 |
struct aws_http_connection_manager_options manager_options; |
| 809 |
AWS_ZERO_STRUCT(manager_options); |
| 810 |
manager_options.bootstrap = options->bootstrap; |
| 811 |
manager_options.initial_window_size = SIZE_MAX; |
| 812 |
manager_options.socket_options = &socket_options; |
| 813 |
manager_options.host = options->endpoint; |
| 814 |
manager_options.port = 443; |
| 815 |
manager_options.max_connections = 2; |
| 816 |
manager_options.shutdown_complete_callback = s_on_connection_manager_shutdown; |
| 817 |
manager_options.shutdown_complete_user_data = provider; |
| 818 |
manager_options.tls_connection_options = &tls_connection_options; |
| 819 |
manager_options.proxy_options = options->http_proxy_options; |
| 820 |
manager_options.proxy_ev_settings = options->proxy_ev_settings; |
| 821 |
|
| 822 |
impl->function_table = options->function_table; |
| 823 |
if (impl->function_table == NULL) { |
| 824 |
impl->function_table = g_aws_credentials_provider_http_function_table; |
| 825 |
} |
| 826 |
|
| 827 |
impl->connection_manager = impl->function_table->aws_http_connection_manager_new(allocator, &manager_options); |
| 828 |
if (impl->connection_manager == NULL) { |
| 829 |
AWS_LOGF_ERROR( |
| 830 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 831 |
"(id=%p): Cognito credentials provider failed to create http connection manager with error %s", |
| 832 |
(void *)provider, |
| 833 |
aws_error_debug_str(aws_last_error())); |
| 834 |
goto on_error; |
| 835 |
} |
| 836 |
|
| 837 |
impl->endpoint = aws_string_new_from_cursor(allocator, &options->endpoint); |
| 838 |
impl->identity = aws_string_new_from_cursor(allocator, &options->identity); |
| 839 |
|
| 840 |
if (options->custom_role_arn != NULL) { |
| 841 |
impl->custom_role_arn = aws_string_new_from_cursor(allocator, options->custom_role_arn); |
| 842 |
} |
| 843 |
|
| 844 |
aws_array_list_init_dynamic( |
| 845 |
&impl->static_logins, allocator, options->login_count, sizeof(struct aws_cognito_login)); |
| 846 |
|
| 847 |
for (size_t i = 0; i < options->login_count; ++i) { |
| 848 |
struct aws_cognito_identity_provider_token_pair *login_token_pair = &options->logins[i]; |
| 849 |
|
| 850 |
struct aws_cognito_login login; |
| 851 |
if (s_aws_cognito_login_init( |
| 852 |
&login, |
| 853 |
allocator, |
| 854 |
login_token_pair->identity_provider_name, |
| 855 |
login_token_pair->identity_provider_token)) { |
| 856 |
AWS_LOGF_ERROR( |
| 857 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 858 |
"(id=%p): Cognito credentials provider failed to initialize login entry with error %s", |
| 859 |
(void *)provider, |
| 860 |
aws_error_debug_str(aws_last_error())); |
| 861 |
goto on_error; |
| 862 |
} |
| 863 |
|
| 864 |
aws_array_list_push_back(&impl->static_logins, &login); |
| 865 |
} |
| 866 |
|
| 867 |
struct aws_standard_retry_options retry_options = { |
| 868 |
.backoff_retry_options = |
| 869 |
{ |
| 870 |
.el_group = options->bootstrap->event_loop_group, |
| 871 |
.max_retries = COGNITO_MAX_RETRIES, |
| 872 |
}, |
| 873 |
}; |
| 874 |
|
| 875 |
impl->retry_strategy = aws_retry_strategy_new_standard(allocator, &retry_options); |
| 876 |
if (!impl->retry_strategy) { |
| 877 |
AWS_LOGF_ERROR( |
| 878 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 879 |
"(id=%p): Cognito credentials provider failed to create a retry strategy with error %s", |
| 880 |
(void *)provider, |
| 881 |
aws_error_debug_str(aws_last_error())); |
| 882 |
goto on_error; |
| 883 |
} |
| 884 |
|
| 885 |
provider->shutdown_options = options->shutdown_options; |
| 886 |
|
| 887 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 888 |
|
| 889 |
impl->get_token_pairs = options->get_token_pairs; |
| 890 |
impl->get_token_pairs_user_data = options->get_token_pairs_user_data; |
| 891 |
|
| 892 |
return provider; |
| 893 |
|
| 894 |
on_error: |
| 895 |
|
| 896 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 897 |
aws_credentials_provider_destroy(provider); |
| 898 |
|
| 899 |
return NULL; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
#define DEFAULT_CREDENTIAL_PROVIDER_REFRESH_MS (15 * 60 * 1000) |
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
struct aws_credentials_provider *aws_credentials_provider_new_cognito_caching( |
| 910 |
struct aws_allocator *allocator, |
| 911 |
const struct aws_credentials_provider_cognito_options *options) { |
| 912 |
|
| 913 |
struct aws_credentials_provider *cognito_provider = NULL; |
| 914 |
struct aws_credentials_provider *caching_provider = NULL; |
| 915 |
|
| 916 |
cognito_provider = aws_credentials_provider_new_cognito(allocator, options); |
| 917 |
if (cognito_provider == NULL) { |
| 918 |
goto on_error; |
| 919 |
} |
| 920 |
|
| 921 |
struct aws_credentials_provider_cached_options cached_options = { |
| 922 |
.source = cognito_provider, |
| 923 |
.refresh_time_in_milliseconds = DEFAULT_CREDENTIAL_PROVIDER_REFRESH_MS, |
| 924 |
}; |
| 925 |
|
| 926 |
caching_provider = aws_credentials_provider_new_cached(allocator, &cached_options); |
| 927 |
if (caching_provider == NULL) { |
| 928 |
goto on_error; |
| 929 |
} |
| 930 |
|
| 931 |
aws_credentials_provider_release(cognito_provider); |
| 932 |
|
| 933 |
return caching_provider; |
| 934 |
|
| 935 |
on_error: |
| 936 |
|
| 937 |
aws_credentials_provider_release(caching_provider); |
| 938 |
aws_credentials_provider_release(cognito_provider); |
| 939 |
|
| 940 |
return NULL; |
| 941 |
} |