| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/credentials.h> |
| 7 |
|
| 8 |
#include <aws/auth/private/aws_profile.h> |
| 9 |
#include <aws/auth/private/credentials_utils.h> |
| 10 |
#include <aws/common/clock.h> |
| 11 |
#include <aws/common/date_time.h> |
| 12 |
#include <aws/common/environment.h> |
| 13 |
#include <aws/common/string.h> |
| 14 |
#include <aws/common/uuid.h> |
| 15 |
#include <aws/common/xml_parser.h> |
| 16 |
#include <aws/http/connection.h> |
| 17 |
#include <aws/http/connection_manager.h> |
| 18 |
#include <aws/http/request_response.h> |
| 19 |
#include <aws/http/status_code.h> |
| 20 |
#include <aws/io/file_utils.h> |
| 21 |
#include <aws/io/logging.h> |
| 22 |
#include <aws/io/socket.h> |
| 23 |
#include <aws/io/stream.h> |
| 24 |
#include <aws/io/tls_channel_handler.h> |
| 25 |
#include <aws/io/uri.h> |
| 26 |
#include <inttypes.h> |
| 27 |
|
| 28 |
#if defined(_MSC_VER) |
| 29 |
# pragma warning(disable : 4204) |
| 30 |
# pragma warning(disable : 4232) |
| 31 |
#endif |
| 32 |
|
| 33 |
#define STS_WEB_IDENTITY_RESPONSE_SIZE_INITIAL 2048 |
| 34 |
#define STS_WEB_IDENTITY_RESPONSE_SIZE_LIMIT 10000 |
| 35 |
#define STS_WEB_IDENTITY_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS 2 |
| 36 |
#define STS_WEB_IDENTITY_CREDS_DEFAULT_DURATION_SECONDS 900 |
| 37 |
#define STS_WEB_IDENTITY_MAX_ATTEMPTS 3 |
| 38 |
|
| 39 |
static void s_on_connection_manager_shutdown(void *user_data); |
| 40 |
static int s_stswebid_error_xml_on_Error_child(struct aws_xml_node *, void *); |
| 41 |
static int s_stswebid_200_xml_on_AssumeRoleWithWebIdentityResponse_child(struct aws_xml_node *, void *); |
| 42 |
static int s_stswebid_200_xml_on_AssumeRoleWithWebIdentityResult_child(struct aws_xml_node *, void *); |
| 43 |
static int s_stswebid_200_xml_on_Credentials_child(struct aws_xml_node *, void *); |
| 44 |
static int s_stswebid_xml_on_AssumedRoleUser_child(struct aws_xml_node *, void *); |
| 45 |
|
| 46 |
struct aws_credentials_provider_sts_web_identity_impl { |
| 47 |
struct aws_http_connection_manager *connection_manager; |
| 48 |
const struct aws_auth_http_system_vtable *function_table; |
| 49 |
struct aws_string *role_arn; |
| 50 |
struct aws_string *role_session_name; |
| 51 |
struct aws_string *token_file_path; |
| 52 |
struct aws_string *endpoint; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct sts_web_identity_user_data { |
| 59 |
|
| 60 |
struct aws_allocator *allocator; |
| 61 |
struct aws_credentials_provider *sts_web_identity_provider; |
| 62 |
aws_on_get_credentials_callback_fn *original_callback; |
| 63 |
void *original_user_data; |
| 64 |
|
| 65 |
|
| 66 |
struct aws_http_connection *connection; |
| 67 |
struct aws_http_message *request; |
| 68 |
struct aws_byte_buf response; |
| 69 |
|
| 70 |
struct aws_string *access_key_id; |
| 71 |
struct aws_string *secret_access_key; |
| 72 |
struct aws_string *session_token; |
| 73 |
struct aws_string *account_id; |
| 74 |
uint64_t expiration_timepoint_in_seconds; |
| 75 |
|
| 76 |
struct aws_byte_buf payload_buf; |
| 77 |
|
| 78 |
int status_code; |
| 79 |
int error_code; |
| 80 |
int attempt_count; |
| 81 |
}; |
| 82 |
|
| 83 |
static void s_user_data_reset_request_and_response(struct sts_web_identity_user_data *user_data) { |
| 84 |
aws_byte_buf_reset(&user_data->response, true ); |
| 85 |
aws_byte_buf_reset(&user_data->payload_buf, true ); |
| 86 |
user_data->status_code = 0; |
| 87 |
if (user_data->request) { |
| 88 |
aws_input_stream_destroy(aws_http_message_get_body_stream(user_data->request)); |
| 89 |
} |
| 90 |
aws_http_message_destroy(user_data->request); |
| 91 |
user_data->request = NULL; |
| 92 |
|
| 93 |
aws_string_destroy(user_data->access_key_id); |
| 94 |
user_data->access_key_id = NULL; |
| 95 |
|
| 96 |
aws_string_destroy_secure(user_data->secret_access_key); |
| 97 |
user_data->secret_access_key = NULL; |
| 98 |
|
| 99 |
aws_string_destroy_secure(user_data->session_token); |
| 100 |
user_data->session_token = NULL; |
| 101 |
|
| 102 |
aws_string_destroy(user_data->account_id); |
| 103 |
user_data->account_id = NULL; |
| 104 |
} |
| 105 |
|
| 106 |
static void s_user_data_destroy(struct sts_web_identity_user_data *user_data) { |
| 107 |
if (user_data == NULL) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
struct aws_credentials_provider_sts_web_identity_impl *impl = user_data->sts_web_identity_provider->impl; |
| 112 |
|
| 113 |
if (user_data->connection) { |
| 114 |
impl->function_table->aws_http_connection_manager_release_connection( |
| 115 |
impl->connection_manager, user_data->connection); |
| 116 |
} |
| 117 |
s_user_data_reset_request_and_response(user_data); |
| 118 |
aws_byte_buf_clean_up(&user_data->response); |
| 119 |
|
| 120 |
aws_string_destroy(user_data->access_key_id); |
| 121 |
aws_string_destroy_secure(user_data->secret_access_key); |
| 122 |
aws_string_destroy_secure(user_data->session_token); |
| 123 |
|
| 124 |
aws_byte_buf_clean_up(&user_data->payload_buf); |
| 125 |
|
| 126 |
aws_credentials_provider_release(user_data->sts_web_identity_provider); |
| 127 |
aws_mem_release(user_data->allocator, user_data); |
| 128 |
} |
| 129 |
|
| 130 |
static struct sts_web_identity_user_data *s_user_data_new( |
| 131 |
struct aws_credentials_provider *sts_web_identity_provider, |
| 132 |
aws_on_get_credentials_callback_fn callback, |
| 133 |
void *user_data) { |
| 134 |
|
| 135 |
struct sts_web_identity_user_data *wrapped_user_data = |
| 136 |
aws_mem_calloc(sts_web_identity_provider->allocator, 1, sizeof(struct sts_web_identity_user_data)); |
| 137 |
if (wrapped_user_data == NULL) { |
| 138 |
goto on_error; |
| 139 |
} |
| 140 |
|
| 141 |
wrapped_user_data->allocator = sts_web_identity_provider->allocator; |
| 142 |
wrapped_user_data->sts_web_identity_provider = sts_web_identity_provider; |
| 143 |
aws_credentials_provider_acquire(sts_web_identity_provider); |
| 144 |
wrapped_user_data->original_user_data = user_data; |
| 145 |
wrapped_user_data->original_callback = callback; |
| 146 |
|
| 147 |
if (aws_byte_buf_init( |
| 148 |
&wrapped_user_data->response, |
| 149 |
sts_web_identity_provider->allocator, |
| 150 |
STS_WEB_IDENTITY_RESPONSE_SIZE_INITIAL)) { |
| 151 |
goto on_error; |
| 152 |
} |
| 153 |
|
| 154 |
if (aws_byte_buf_init(&wrapped_user_data->payload_buf, sts_web_identity_provider->allocator, 1024)) { |
| 155 |
goto on_error; |
| 156 |
} |
| 157 |
|
| 158 |
return wrapped_user_data; |
| 159 |
|
| 160 |
on_error: |
| 161 |
|
| 162 |
s_user_data_destroy(wrapped_user_data); |
| 163 |
|
| 164 |
return NULL; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static int s_stswebid_error_xml_on_root(struct aws_xml_node *node, void *user_data) { |
| 201 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 202 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Error")) { |
| 203 |
return aws_xml_node_traverse(node, s_stswebid_error_xml_on_Error_child, user_data); |
| 204 |
} |
| 205 |
|
| 206 |
return AWS_OP_SUCCESS; |
| 207 |
} |
| 208 |
|
| 209 |
static int s_stswebid_error_xml_on_Error_child(struct aws_xml_node *node, void *user_data) { |
| 210 |
bool *get_retryable_error = user_data; |
| 211 |
|
| 212 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 213 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Code")) { |
| 214 |
|
| 215 |
struct aws_byte_cursor data_cursor = {0}; |
| 216 |
if (aws_xml_node_as_body(node, &data_cursor)) { |
| 217 |
return AWS_OP_ERR; |
| 218 |
} |
| 219 |
|
| 220 |
if (aws_byte_cursor_eq_c_str_ignore_case(&data_cursor, "IDPCommunicationError") || |
| 221 |
aws_byte_cursor_eq_c_str_ignore_case(&data_cursor, "InvalidIdentityToken")) { |
| 222 |
*get_retryable_error = true; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return AWS_OP_SUCCESS; |
| 227 |
} |
| 228 |
|
| 229 |
static bool s_parse_retryable_error_from_response(struct aws_allocator *allocator, struct aws_byte_buf *response) { |
| 230 |
|
| 231 |
bool get_retryable_error = false; |
| 232 |
struct aws_xml_parser_options options = { |
| 233 |
.doc = aws_byte_cursor_from_buf(response), |
| 234 |
.on_root_encountered = s_stswebid_error_xml_on_root, |
| 235 |
.user_data = &get_retryable_error, |
| 236 |
}; |
| 237 |
|
| 238 |
if (aws_xml_parse(allocator, &options)) { |
| 239 |
AWS_LOGF_ERROR( |
| 240 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 241 |
"Failed to parse xml error response for sts web identity with error %s", |
| 242 |
aws_error_str(aws_last_error())); |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
return get_retryable_error; |
| 247 |
} |
| 248 |
|
| 249 |
static int s_stswebid_200_xml_on_root(struct aws_xml_node *node, void *user_data) { |
| 250 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 251 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "AssumeRoleWithWebIdentityResponse")) { |
| 252 |
return aws_xml_node_traverse(node, s_stswebid_200_xml_on_AssumeRoleWithWebIdentityResponse_child, user_data); |
| 253 |
} |
| 254 |
return AWS_OP_SUCCESS; |
| 255 |
} |
| 256 |
|
| 257 |
static int s_stswebid_200_xml_on_AssumeRoleWithWebIdentityResponse_child( |
| 258 |
struct aws_xml_node *node, |
| 259 |
|
| 260 |
void *user_data) { |
| 261 |
|
| 262 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 263 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "AssumeRoleWithWebIdentityResult")) { |
| 264 |
return aws_xml_node_traverse(node, s_stswebid_200_xml_on_AssumeRoleWithWebIdentityResult_child, user_data); |
| 265 |
} |
| 266 |
return AWS_OP_SUCCESS; |
| 267 |
} |
| 268 |
|
| 269 |
static int s_stswebid_200_xml_on_AssumeRoleWithWebIdentityResult_child(struct aws_xml_node *node, void *user_data) { |
| 270 |
|
| 271 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 272 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Credentials")) { |
| 273 |
return aws_xml_node_traverse(node, s_stswebid_200_xml_on_Credentials_child, user_data); |
| 274 |
} |
| 275 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "AssumedRoleUser")) { |
| 276 |
return aws_xml_node_traverse(node, s_stswebid_xml_on_AssumedRoleUser_child, user_data); |
| 277 |
} |
| 278 |
return AWS_OP_SUCCESS; |
| 279 |
} |
| 280 |
|
| 281 |
static int s_stswebid_xml_on_AssumedRoleUser_child(struct aws_xml_node *node, void *user_data) { |
| 282 |
struct sts_web_identity_user_data *query_user_data = user_data; |
| 283 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 284 |
struct aws_byte_cursor arn_cursor; |
| 285 |
AWS_ZERO_STRUCT(arn_cursor); |
| 286 |
|
| 287 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Arn")) { |
| 288 |
if (aws_xml_node_as_body(node, &arn_cursor)) { |
| 289 |
return AWS_OP_ERR; |
| 290 |
} |
| 291 |
struct aws_byte_cursor account_id = aws_parse_account_id_from_arn(arn_cursor); |
| 292 |
query_user_data->account_id = aws_string_new_from_cursor(query_user_data->allocator, &account_id); |
| 293 |
} |
| 294 |
|
| 295 |
return AWS_OP_SUCCESS; |
| 296 |
} |
| 297 |
|
| 298 |
static int s_stswebid_200_xml_on_Credentials_child(struct aws_xml_node *node, void *user_data) { |
| 299 |
struct sts_web_identity_user_data *query_user_data = user_data; |
| 300 |
|
| 301 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 302 |
struct aws_byte_cursor credential_data; |
| 303 |
AWS_ZERO_STRUCT(credential_data); |
| 304 |
|
| 305 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "AccessKeyId")) { |
| 306 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 307 |
return AWS_OP_ERR; |
| 308 |
} |
| 309 |
query_user_data->access_key_id = aws_string_new_from_cursor(query_user_data->allocator, &credential_data); |
| 310 |
} |
| 311 |
|
| 312 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "SecretAccessKey")) { |
| 313 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 314 |
return AWS_OP_ERR; |
| 315 |
} |
| 316 |
query_user_data->secret_access_key = aws_string_new_from_cursor(query_user_data->allocator, &credential_data); |
| 317 |
} |
| 318 |
|
| 319 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "SessionToken")) { |
| 320 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 321 |
return AWS_OP_ERR; |
| 322 |
} |
| 323 |
query_user_data->session_token = aws_string_new_from_cursor(query_user_data->allocator, &credential_data); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Expiration")) { |
| 330 |
if (aws_xml_node_as_body(node, &credential_data)) { |
| 331 |
return AWS_OP_ERR; |
| 332 |
} |
| 333 |
if (credential_data.len != 0) { |
| 334 |
struct aws_date_time expiration; |
| 335 |
if (aws_date_time_init_from_str_cursor(&expiration, &credential_data, AWS_DATE_FORMAT_ISO_8601) == |
| 336 |
AWS_OP_SUCCESS) { |
| 337 |
query_user_data->expiration_timepoint_in_seconds = (uint64_t)aws_date_time_as_epoch_secs(&expiration); |
| 338 |
} else { |
| 339 |
AWS_LOGF_ERROR( |
| 340 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 341 |
"Failed to parse time string from sts web identity xml response: %s", |
| 342 |
aws_error_str(aws_last_error())); |
| 343 |
return AWS_OP_ERR; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return AWS_OP_SUCCESS; |
| 349 |
} |
| 350 |
|
| 351 |
static struct aws_credentials *s_parse_credentials_from_response( |
| 352 |
struct sts_web_identity_user_data *query_user_data, |
| 353 |
struct aws_byte_buf *response) { |
| 354 |
|
| 355 |
struct aws_credentials *credentials = NULL; |
| 356 |
|
| 357 |
if (!response || response->len == 0) { |
| 358 |
goto on_finish; |
| 359 |
} |
| 360 |
|
| 361 |
uint64_t now = UINT64_MAX; |
| 362 |
if (aws_sys_clock_get_ticks(&now) != AWS_OP_SUCCESS) { |
| 363 |
AWS_LOGF_ERROR( |
| 364 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 365 |
"Failed to get sys clock for sts web identity credentials provider to parse error information."); |
| 366 |
goto on_finish; |
| 367 |
} |
| 368 |
uint64_t now_seconds = aws_timestamp_convert(now, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL); |
| 369 |
query_user_data->expiration_timepoint_in_seconds = now_seconds + STS_WEB_IDENTITY_CREDS_DEFAULT_DURATION_SECONDS; |
| 370 |
|
| 371 |
struct aws_xml_parser_options options = { |
| 372 |
.doc = aws_byte_cursor_from_buf(response), |
| 373 |
.on_root_encountered = s_stswebid_200_xml_on_root, |
| 374 |
.user_data = query_user_data, |
| 375 |
}; |
| 376 |
if (aws_xml_parse(query_user_data->allocator, &options)) { |
| 377 |
AWS_LOGF_ERROR( |
| 378 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 379 |
"Failed to parse xml response for sts web identity with error: %s", |
| 380 |
aws_error_str(aws_last_error())); |
| 381 |
goto on_finish; |
| 382 |
} |
| 383 |
|
| 384 |
if (!query_user_data->access_key_id || !query_user_data->secret_access_key) { |
| 385 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "STS web identity not found in XML response."); |
| 386 |
goto on_finish; |
| 387 |
} |
| 388 |
struct aws_credentials_options creds_option = { |
| 389 |
.access_key_id_cursor = aws_byte_cursor_from_string(query_user_data->access_key_id), |
| 390 |
.secret_access_key_cursor = aws_byte_cursor_from_string(query_user_data->secret_access_key), |
| 391 |
.session_token_cursor = aws_byte_cursor_from_string(query_user_data->session_token), |
| 392 |
.account_id_cursor = aws_byte_cursor_from_string(query_user_data->account_id), |
| 393 |
.expiration_timepoint_seconds = query_user_data->expiration_timepoint_in_seconds, |
| 394 |
}; |
| 395 |
credentials = aws_credentials_new_with_options(query_user_data->allocator, &creds_option); |
| 396 |
|
| 397 |
if (credentials == NULL) { |
| 398 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to create credentials for sts web identity"); |
| 399 |
goto on_finish; |
| 400 |
} |
| 401 |
|
| 402 |
on_finish: |
| 403 |
|
| 404 |
if (credentials == NULL) { |
| 405 |
|
| 406 |
query_user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_STS_WEB_IDENTITY_SOURCE_FAILURE; |
| 407 |
} |
| 408 |
|
| 409 |
return credentials; |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
static void s_finalize_get_credentials_query(struct sts_web_identity_user_data *user_data) { |
| 416 |
|
| 417 |
struct aws_credentials *credentials = NULL; |
| 418 |
if (user_data->status_code == AWS_HTTP_STATUS_CODE_200_OK) { |
| 419 |
credentials = s_parse_credentials_from_response(user_data, &user_data->response); |
| 420 |
} |
| 421 |
|
| 422 |
if (credentials != NULL) { |
| 423 |
AWS_LOGF_INFO( |
| 424 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 425 |
"(id=%p) STS_WEB_IDENTITY credentials provider successfully queried credentials", |
| 426 |
(void *)user_data->sts_web_identity_provider); |
| 427 |
} else { |
| 428 |
AWS_LOGF_WARN( |
| 429 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 430 |
"(id=%p) STS_WEB_IDENTITY credentials provider failed to query credentials", |
| 431 |
(void *)user_data->sts_web_identity_provider); |
| 432 |
|
| 433 |
if (user_data->error_code == AWS_ERROR_SUCCESS) { |
| 434 |
user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_STS_WEB_IDENTITY_SOURCE_FAILURE; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
user_data->original_callback(credentials, user_data->error_code, user_data->original_user_data); |
| 440 |
|
| 441 |
|
| 442 |
s_user_data_destroy(user_data); |
| 443 |
aws_credentials_release(credentials); |
| 444 |
} |
| 445 |
|
| 446 |
static int s_on_incoming_body_fn( |
| 447 |
struct aws_http_stream *stream, |
| 448 |
const struct aws_byte_cursor *body, |
| 449 |
void *wrapped_user_data) { |
| 450 |
|
| 451 |
(void)stream; |
| 452 |
|
| 453 |
struct sts_web_identity_user_data *user_data = wrapped_user_data; |
| 454 |
struct aws_credentials_provider_sts_web_identity_impl *impl = user_data->sts_web_identity_provider->impl; |
| 455 |
|
| 456 |
AWS_LOGF_TRACE( |
| 457 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 458 |
"(id=%p) STS_WEB_IDENTITY credentials provider received %zu response bytes", |
| 459 |
(void *)user_data->sts_web_identity_provider, |
| 460 |
body->len); |
| 461 |
|
| 462 |
if (body->len + user_data->response.len > STS_WEB_IDENTITY_RESPONSE_SIZE_LIMIT) { |
| 463 |
impl->function_table->aws_http_connection_close(user_data->connection); |
| 464 |
AWS_LOGF_ERROR( |
| 465 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 466 |
"(id=%p) STS_WEB_IDENTITY credentials provider query response exceeded maximum allowed length", |
| 467 |
(void *)user_data->sts_web_identity_provider); |
| 468 |
|
| 469 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 470 |
} |
| 471 |
|
| 472 |
if (aws_byte_buf_append_dynamic(&user_data->response, body)) { |
| 473 |
impl->function_table->aws_http_connection_close(user_data->connection); |
| 474 |
AWS_LOGF_ERROR( |
| 475 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 476 |
"(id=%p) STS_WEB_IDENTITY credentials provider query error appending response: %s", |
| 477 |
(void *)user_data->sts_web_identity_provider, |
| 478 |
aws_error_str(aws_last_error())); |
| 479 |
|
| 480 |
return AWS_OP_ERR; |
| 481 |
} |
| 482 |
|
| 483 |
return AWS_OP_SUCCESS; |
| 484 |
} |
| 485 |
|
| 486 |
static int s_on_incoming_headers_fn( |
| 487 |
struct aws_http_stream *stream, |
| 488 |
enum aws_http_header_block header_block, |
| 489 |
const struct aws_http_header *header_array, |
| 490 |
size_t num_headers, |
| 491 |
void *wrapped_user_data) { |
| 492 |
|
| 493 |
(void)header_array; |
| 494 |
(void)num_headers; |
| 495 |
|
| 496 |
if (header_block != AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 497 |
return AWS_OP_SUCCESS; |
| 498 |
} |
| 499 |
|
| 500 |
struct sts_web_identity_user_data *user_data = wrapped_user_data; |
| 501 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 502 |
if (user_data->status_code == 0) { |
| 503 |
struct aws_credentials_provider_sts_web_identity_impl *impl = user_data->sts_web_identity_provider->impl; |
| 504 |
if (impl->function_table->aws_http_stream_get_incoming_response_status(stream, &user_data->status_code)) { |
| 505 |
AWS_LOGF_ERROR( |
| 506 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 507 |
"(id=%p) STS_WEB_IDENTITY credentials provider failed to get http status code: %s", |
| 508 |
(void *)user_data->sts_web_identity_provider, |
| 509 |
aws_error_str(aws_last_error())); |
| 510 |
|
| 511 |
return AWS_OP_ERR; |
| 512 |
} |
| 513 |
AWS_LOGF_DEBUG( |
| 514 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 515 |
"(id=%p) STS_WEB_IDENTITY credentials provider query received http status code %d", |
| 516 |
(void *)user_data->sts_web_identity_provider, |
| 517 |
user_data->status_code); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
return AWS_OP_SUCCESS; |
| 522 |
} |
| 523 |
|
| 524 |
static void s_query_credentials(struct sts_web_identity_user_data *user_data); |
| 525 |
|
| 526 |
static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *data) { |
| 527 |
struct sts_web_identity_user_data *user_data = data; |
| 528 |
|
| 529 |
struct aws_credentials_provider_sts_web_identity_impl *impl = user_data->sts_web_identity_provider->impl; |
| 530 |
impl->function_table->aws_http_stream_release(stream); |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
if (user_data->status_code != AWS_HTTP_STATUS_CODE_200_OK || error_code != AWS_OP_SUCCESS) { |
| 537 |
if (++user_data->attempt_count < STS_WEB_IDENTITY_MAX_ATTEMPTS && user_data->response.len) { |
| 538 |
if (s_parse_retryable_error_from_response(user_data->allocator, &user_data->response)) { |
| 539 |
s_query_credentials(user_data); |
| 540 |
return; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
s_finalize_get_credentials_query(user_data); |
| 546 |
} |
| 547 |
|
| 548 |
static struct aws_http_header s_content_type_header = { |
| 549 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("content-type"), |
| 550 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("application/x-www-form-urlencoded"), |
| 551 |
}; |
| 552 |
|
| 553 |
static struct aws_http_header s_api_version_header = { |
| 554 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-api-version"), |
| 555 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("2011-06-15"), |
| 556 |
}; |
| 557 |
static struct aws_http_header s_accept_header = { |
| 558 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Accept"), |
| 559 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("*/*"), |
| 560 |
}; |
| 561 |
|
| 562 |
static struct aws_http_header s_user_agent_header = { |
| 563 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("User-Agent"), |
| 564 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("aws-sdk-crt/sts-web-identity-credentials-provider"), |
| 565 |
}; |
| 566 |
|
| 567 |
static struct aws_http_header s_keep_alive_header = { |
| 568 |
.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"), |
| 569 |
.value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("keep-alive"), |
| 570 |
}; |
| 571 |
|
| 572 |
static struct aws_byte_cursor s_content_length = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("content-length"); |
| 573 |
static struct aws_byte_cursor s_path = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/"); |
| 574 |
|
| 575 |
static int s_make_sts_web_identity_http_query( |
| 576 |
struct sts_web_identity_user_data *user_data, |
| 577 |
struct aws_byte_cursor *body_cursor) { |
| 578 |
AWS_FATAL_ASSERT(user_data->connection); |
| 579 |
|
| 580 |
struct aws_http_stream *stream = NULL; |
| 581 |
struct aws_input_stream *input_stream = NULL; |
| 582 |
struct aws_http_message *request = aws_http_message_new_request(user_data->allocator); |
| 583 |
if (request == NULL) { |
| 584 |
return AWS_OP_ERR; |
| 585 |
} |
| 586 |
|
| 587 |
struct aws_credentials_provider_sts_web_identity_impl *impl = user_data->sts_web_identity_provider->impl; |
| 588 |
|
| 589 |
char content_length[21]; |
| 590 |
AWS_ZERO_ARRAY(content_length); |
| 591 |
snprintf(content_length, sizeof(content_length), "%" PRIu64, (uint64_t)body_cursor->len); |
| 592 |
|
| 593 |
struct aws_http_header content_len_header = { |
| 594 |
.name = s_content_length, |
| 595 |
.value = aws_byte_cursor_from_c_str(content_length), |
| 596 |
}; |
| 597 |
|
| 598 |
if (aws_http_message_add_header(request, content_len_header)) { |
| 599 |
goto on_error; |
| 600 |
} |
| 601 |
|
| 602 |
if (aws_http_message_add_header(request, s_content_type_header)) { |
| 603 |
goto on_error; |
| 604 |
} |
| 605 |
|
| 606 |
struct aws_http_header host_header = { |
| 607 |
.name = aws_byte_cursor_from_c_str("Host"), |
| 608 |
.value = aws_byte_cursor_from_string(impl->endpoint), |
| 609 |
}; |
| 610 |
|
| 611 |
if (aws_http_message_add_header(request, host_header)) { |
| 612 |
goto on_error; |
| 613 |
} |
| 614 |
|
| 615 |
if (aws_http_message_add_header(request, s_api_version_header)) { |
| 616 |
goto on_error; |
| 617 |
} |
| 618 |
|
| 619 |
if (aws_http_message_add_header(request, s_accept_header)) { |
| 620 |
goto on_error; |
| 621 |
} |
| 622 |
|
| 623 |
if (aws_http_message_add_header(request, s_user_agent_header)) { |
| 624 |
goto on_error; |
| 625 |
} |
| 626 |
|
| 627 |
if (aws_http_message_add_header(request, s_keep_alive_header)) { |
| 628 |
goto on_error; |
| 629 |
} |
| 630 |
|
| 631 |
input_stream = aws_input_stream_new_from_cursor(user_data->allocator, body_cursor); |
| 632 |
if (!input_stream) { |
| 633 |
goto on_error; |
| 634 |
} |
| 635 |
|
| 636 |
aws_http_message_set_body_stream(request, input_stream); |
| 637 |
|
| 638 |
if (aws_http_message_set_request_path(request, s_path)) { |
| 639 |
goto on_error; |
| 640 |
} |
| 641 |
|
| 642 |
if (aws_http_message_set_request_method(request, aws_http_method_post)) { |
| 643 |
goto on_error; |
| 644 |
} |
| 645 |
|
| 646 |
user_data->request = request; |
| 647 |
|
| 648 |
struct aws_http_make_request_options request_options = { |
| 649 |
.self_size = sizeof(request_options), |
| 650 |
.on_response_headers = s_on_incoming_headers_fn, |
| 651 |
.on_response_header_block_done = NULL, |
| 652 |
.on_response_body = s_on_incoming_body_fn, |
| 653 |
.on_complete = s_on_stream_complete_fn, |
| 654 |
.user_data = user_data, |
| 655 |
.request = request, |
| 656 |
}; |
| 657 |
|
| 658 |
stream = impl->function_table->aws_http_connection_make_request(user_data->connection, &request_options); |
| 659 |
|
| 660 |
if (!stream) { |
| 661 |
goto on_error; |
| 662 |
} |
| 663 |
|
| 664 |
if (impl->function_table->aws_http_stream_activate(stream)) { |
| 665 |
goto on_error; |
| 666 |
} |
| 667 |
|
| 668 |
return AWS_OP_SUCCESS; |
| 669 |
|
| 670 |
on_error: |
| 671 |
impl->function_table->aws_http_stream_release(stream); |
| 672 |
aws_input_stream_destroy(input_stream); |
| 673 |
aws_http_message_destroy(request); |
| 674 |
user_data->request = NULL; |
| 675 |
return AWS_OP_ERR; |
| 676 |
} |
| 677 |
|
| 678 |
static void s_query_credentials(struct sts_web_identity_user_data *user_data) { |
| 679 |
AWS_FATAL_ASSERT(user_data->connection); |
| 680 |
|
| 681 |
struct aws_credentials_provider_sts_web_identity_impl *impl = user_data->sts_web_identity_provider->impl; |
| 682 |
|
| 683 |
|
| 684 |
s_user_data_reset_request_and_response(user_data); |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
struct aws_byte_buf token_buf; |
| 695 |
bool success = false; |
| 696 |
|
| 697 |
AWS_ZERO_STRUCT(token_buf); |
| 698 |
|
| 699 |
struct aws_byte_cursor work_cursor = |
| 700 |
aws_byte_cursor_from_c_str("Action=AssumeRoleWithWebIdentity&Version=2011-06-15&RoleArn="); |
| 701 |
if (aws_byte_buf_append_dynamic(&user_data->payload_buf, &work_cursor)) { |
| 702 |
goto on_finish; |
| 703 |
} |
| 704 |
|
| 705 |
work_cursor = aws_byte_cursor_from_string(impl->role_arn); |
| 706 |
if (aws_byte_buf_append_encoding_uri_param(&user_data->payload_buf, &work_cursor)) { |
| 707 |
goto on_finish; |
| 708 |
} |
| 709 |
|
| 710 |
work_cursor = aws_byte_cursor_from_c_str("&RoleSessionName="); |
| 711 |
if (aws_byte_buf_append_dynamic(&user_data->payload_buf, &work_cursor)) { |
| 712 |
goto on_finish; |
| 713 |
} |
| 714 |
|
| 715 |
work_cursor = aws_byte_cursor_from_string(impl->role_session_name); |
| 716 |
if (aws_byte_buf_append_encoding_uri_param(&user_data->payload_buf, &work_cursor)) { |
| 717 |
goto on_finish; |
| 718 |
} |
| 719 |
|
| 720 |
work_cursor = aws_byte_cursor_from_c_str("&WebIdentityToken="); |
| 721 |
if (aws_byte_buf_append_dynamic(&user_data->payload_buf, &work_cursor)) { |
| 722 |
goto on_finish; |
| 723 |
} |
| 724 |
|
| 725 |
if (aws_byte_buf_init_from_file(&token_buf, user_data->allocator, aws_string_c_str(impl->token_file_path))) { |
| 726 |
goto on_finish; |
| 727 |
} |
| 728 |
work_cursor = aws_byte_cursor_from_buf(&token_buf); |
| 729 |
if (aws_byte_buf_append_encoding_uri_param(&user_data->payload_buf, &work_cursor)) { |
| 730 |
goto on_finish; |
| 731 |
} |
| 732 |
struct aws_byte_cursor body_cursor = aws_byte_cursor_from_buf(&user_data->payload_buf); |
| 733 |
|
| 734 |
if (s_make_sts_web_identity_http_query(user_data, &body_cursor) == AWS_OP_ERR) { |
| 735 |
goto on_finish; |
| 736 |
} |
| 737 |
success = true; |
| 738 |
|
| 739 |
on_finish: |
| 740 |
aws_byte_buf_clean_up(&token_buf); |
| 741 |
if (!success) { |
| 742 |
s_finalize_get_credentials_query(user_data); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
static void s_on_acquire_connection(struct aws_http_connection *connection, int error_code, void *data) { |
| 747 |
struct sts_web_identity_user_data *user_data = data; |
| 748 |
|
| 749 |
if (connection == NULL) { |
| 750 |
AWS_LOGF_WARN( |
| 751 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 752 |
"id=%p: STS_WEB_IDENTITY provider failed to acquire a connection, error code %d(%s)", |
| 753 |
(void *)user_data->sts_web_identity_provider, |
| 754 |
error_code, |
| 755 |
aws_error_str(error_code)); |
| 756 |
|
| 757 |
s_finalize_get_credentials_query(user_data); |
| 758 |
return; |
| 759 |
} |
| 760 |
|
| 761 |
user_data->connection = connection; |
| 762 |
|
| 763 |
s_query_credentials(user_data); |
| 764 |
} |
| 765 |
|
| 766 |
static int s_credentials_provider_sts_web_identity_get_credentials_async( |
| 767 |
struct aws_credentials_provider *provider, |
| 768 |
aws_on_get_credentials_callback_fn callback, |
| 769 |
void *user_data) { |
| 770 |
|
| 771 |
struct aws_credentials_provider_sts_web_identity_impl *impl = provider->impl; |
| 772 |
|
| 773 |
AWS_LOGF_DEBUG( |
| 774 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 775 |
"id=%p: STS_WEB_IDENTITY provider trying to load credentials", |
| 776 |
(void *)provider); |
| 777 |
|
| 778 |
struct sts_web_identity_user_data *wrapped_user_data = s_user_data_new(provider, callback, user_data); |
| 779 |
if (wrapped_user_data == NULL) { |
| 780 |
goto error; |
| 781 |
} |
| 782 |
|
| 783 |
impl->function_table->aws_http_connection_manager_acquire_connection( |
| 784 |
impl->connection_manager, s_on_acquire_connection, wrapped_user_data); |
| 785 |
|
| 786 |
return AWS_OP_SUCCESS; |
| 787 |
|
| 788 |
error: |
| 789 |
s_user_data_destroy(wrapped_user_data); |
| 790 |
return AWS_OP_ERR; |
| 791 |
} |
| 792 |
|
| 793 |
static void s_credentials_provider_sts_web_identity_destroy(struct aws_credentials_provider *provider) { |
| 794 |
struct aws_credentials_provider_sts_web_identity_impl *impl = provider->impl; |
| 795 |
if (impl == NULL) { |
| 796 |
return; |
| 797 |
} |
| 798 |
|
| 799 |
aws_string_destroy(impl->role_arn); |
| 800 |
aws_string_destroy(impl->role_session_name); |
| 801 |
aws_string_destroy(impl->token_file_path); |
| 802 |
aws_string_destroy(impl->endpoint); |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
if (impl->connection_manager) { |
| 809 |
impl->function_table->aws_http_connection_manager_release(impl->connection_manager); |
| 810 |
} else { |
| 811 |
|
| 812 |
|
| 813 |
s_on_connection_manager_shutdown(provider); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
} |
| 818 |
|
| 819 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_sts_web_identity_vtable = { |
| 820 |
.get_credentials = s_credentials_provider_sts_web_identity_get_credentials_async, |
| 821 |
.destroy = s_credentials_provider_sts_web_identity_destroy, |
| 822 |
}; |
| 823 |
|
| 824 |
static void s_on_connection_manager_shutdown(void *user_data) { |
| 825 |
struct aws_credentials_provider *provider = user_data; |
| 826 |
|
| 827 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 828 |
aws_mem_release(provider->allocator, provider); |
| 829 |
} |
| 830 |
|
| 831 |
AWS_STATIC_STRING_FROM_LITERAL(s_region_config, "region"); |
| 832 |
AWS_STATIC_STRING_FROM_LITERAL(s_role_arn_config, "role_arn"); |
| 833 |
AWS_STATIC_STRING_FROM_LITERAL(s_role_arn_env, "AWS_ROLE_ARN"); |
| 834 |
AWS_STATIC_STRING_FROM_LITERAL(s_role_session_name_config, "role_session_name"); |
| 835 |
AWS_STATIC_STRING_FROM_LITERAL(s_role_session_name_env, "AWS_ROLE_SESSION_NAME"); |
| 836 |
AWS_STATIC_STRING_FROM_LITERAL(s_token_file_path_config, "web_identity_token_file"); |
| 837 |
AWS_STATIC_STRING_FROM_LITERAL(s_token_file_path_env, "AWS_WEB_IDENTITY_TOKEN_FILE"); |
| 838 |
|
| 839 |
struct sts_web_identity_parameters { |
| 840 |
struct aws_allocator *allocator; |
| 841 |
|
| 842 |
struct aws_string *endpoint; |
| 843 |
struct aws_byte_buf role_arn; |
| 844 |
struct aws_byte_buf role_session_name; |
| 845 |
struct aws_byte_buf token_file_path; |
| 846 |
}; |
| 847 |
|
| 848 |
struct aws_profile_collection *s_load_profile(struct aws_allocator *allocator) { |
| 849 |
|
| 850 |
struct aws_profile_collection *config_profiles = NULL; |
| 851 |
struct aws_string *config_file_path = NULL; |
| 852 |
|
| 853 |
config_file_path = aws_get_config_file_path(allocator, NULL); |
| 854 |
if (!config_file_path) { |
| 855 |
AWS_LOGF_ERROR( |
| 856 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 857 |
"Failed to resolve config file path during sts web identity provider initialization: %s", |
| 858 |
aws_error_str(aws_last_error())); |
| 859 |
goto on_error; |
| 860 |
} |
| 861 |
|
| 862 |
config_profiles = aws_profile_collection_new_from_file(allocator, config_file_path, AWS_PST_CONFIG); |
| 863 |
if (config_profiles != NULL) { |
| 864 |
AWS_LOGF_DEBUG( |
| 865 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 866 |
"Successfully built config profile collection from file at (%s)", |
| 867 |
aws_string_c_str(config_file_path)); |
| 868 |
} else { |
| 869 |
AWS_LOGF_ERROR( |
| 870 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 871 |
"Failed to build config profile collection from file at (%s) : %s", |
| 872 |
aws_string_c_str(config_file_path), |
| 873 |
aws_error_str(aws_last_error())); |
| 874 |
goto on_error; |
| 875 |
} |
| 876 |
|
| 877 |
aws_string_destroy(config_file_path); |
| 878 |
return config_profiles; |
| 879 |
|
| 880 |
on_error: |
| 881 |
aws_string_destroy(config_file_path); |
| 882 |
aws_profile_collection_destroy(config_profiles); |
| 883 |
return NULL; |
| 884 |
} |
| 885 |
|
| 886 |
AWS_STATIC_STRING_FROM_LITERAL(s_sts_service_name, "sts"); |
| 887 |
AWS_STATIC_STRING_FROM_LITERAL(s_sts_service_env_name, "STS"); |
| 888 |
|
| 889 |
static int s_generate_uuid_to_buf(struct aws_allocator *allocator, struct aws_byte_buf *dst) { |
| 890 |
|
| 891 |
if (!allocator || !dst) { |
| 892 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 893 |
} |
| 894 |
|
| 895 |
struct aws_uuid uuid; |
| 896 |
if (aws_uuid_init(&uuid)) { |
| 897 |
AWS_LOGF_ERROR( |
| 898 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to initiate an uuid struct: %s", aws_error_str(aws_last_error())); |
| 899 |
return aws_last_error(); |
| 900 |
} |
| 901 |
|
| 902 |
char uuid_str[AWS_UUID_STR_LEN] = {0}; |
| 903 |
struct aws_byte_buf uuid_buf = aws_byte_buf_from_array(uuid_str, sizeof(uuid_str)); |
| 904 |
uuid_buf.len = 0; |
| 905 |
if (aws_uuid_to_str(&uuid, &uuid_buf)) { |
| 906 |
AWS_LOGF_ERROR( |
| 907 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to stringify uuid: %s", aws_error_str(aws_last_error())); |
| 908 |
return aws_last_error(); |
| 909 |
} |
| 910 |
if (aws_byte_buf_init_copy(dst, allocator, &uuid_buf)) { |
| 911 |
AWS_LOGF_ERROR( |
| 912 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 913 |
"Failed to generate role session name during sts web identity provider initialization: %s", |
| 914 |
aws_error_str(aws_last_error())); |
| 915 |
return aws_last_error(); |
| 916 |
} |
| 917 |
return AWS_OP_SUCCESS; |
| 918 |
} |
| 919 |
|
| 920 |
static struct aws_string *s_check_or_get_with_env( |
| 921 |
struct aws_allocator *allocator, |
| 922 |
const struct aws_string *env_key, |
| 923 |
struct aws_byte_cursor option) { |
| 924 |
|
| 925 |
AWS_ASSERT(allocator); |
| 926 |
struct aws_string *out = NULL; |
| 927 |
|
| 928 |
if (option.len) { |
| 929 |
out = aws_string_new_from_cursor(allocator, &option); |
| 930 |
} else { |
| 931 |
aws_get_environment_value(allocator, env_key, &out); |
| 932 |
} |
| 933 |
|
| 934 |
return out; |
| 935 |
} |
| 936 |
|
| 937 |
static void s_check_or_get_with_profile_config( |
| 938 |
struct aws_allocator *allocator, |
| 939 |
const struct aws_profile *profile, |
| 940 |
struct aws_string **target, |
| 941 |
const struct aws_string *config_key) { |
| 942 |
|
| 943 |
if (!allocator || !profile || !config_key) { |
| 944 |
return; |
| 945 |
} |
| 946 |
if ((!(*target) || !(*target)->len)) { |
| 947 |
if (*target) { |
| 948 |
aws_string_destroy(*target); |
| 949 |
*target = NULL; |
| 950 |
} |
| 951 |
const struct aws_profile_property *property = aws_profile_get_property(profile, config_key); |
| 952 |
if (property) { |
| 953 |
*target = aws_string_new_from_string(allocator, aws_profile_property_get_value(property)); |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
static void s_parameters_destroy(struct sts_web_identity_parameters *parameters) { |
| 959 |
if (!parameters) { |
| 960 |
return; |
| 961 |
} |
| 962 |
aws_string_destroy(parameters->endpoint); |
| 963 |
aws_byte_buf_clean_up(¶meters->role_arn); |
| 964 |
aws_byte_buf_clean_up(¶meters->role_session_name); |
| 965 |
aws_byte_buf_clean_up(¶meters->token_file_path); |
| 966 |
aws_mem_release(parameters->allocator, parameters); |
| 967 |
} |
| 968 |
|
| 969 |
static struct sts_web_identity_parameters *s_parameters_new( |
| 970 |
struct aws_allocator *allocator, |
| 971 |
const struct aws_credentials_provider_sts_web_identity_options *options) { |
| 972 |
|
| 973 |
struct sts_web_identity_parameters *parameters = |
| 974 |
aws_mem_calloc(allocator, 1, sizeof(struct sts_web_identity_parameters)); |
| 975 |
if (parameters == NULL) { |
| 976 |
return NULL; |
| 977 |
} |
| 978 |
parameters->allocator = allocator; |
| 979 |
|
| 980 |
bool success = false; |
| 981 |
struct aws_string *region = NULL; |
| 982 |
if (options->region.len > 0) { |
| 983 |
region = aws_string_new_from_cursor(allocator, &options->region); |
| 984 |
} else { |
| 985 |
region = aws_credentials_provider_resolve_region_from_env(allocator); |
| 986 |
} |
| 987 |
|
| 988 |
struct aws_string *role_arn = s_check_or_get_with_env(allocator, s_role_arn_env, options->role_arn); |
| 989 |
struct aws_string *role_session_name = |
| 990 |
s_check_or_get_with_env(allocator, s_role_session_name_env, options->role_session_name); |
| 991 |
struct aws_string *token_file_path = |
| 992 |
s_check_or_get_with_env(allocator, s_token_file_path_env, options->token_file_path); |
| 993 |
; |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
struct aws_profile_collection *config_profile = NULL; |
| 1001 |
struct aws_string *profile_name = NULL; |
| 1002 |
const struct aws_profile *profile = NULL; |
| 1003 |
bool get_all_parameters = |
| 1004 |
(region && region->len && role_arn && role_arn->len && token_file_path && token_file_path->len); |
| 1005 |
if (!get_all_parameters) { |
| 1006 |
if (options->config_profile_collection_cached) { |
| 1007 |
|
| 1008 |
config_profile = aws_profile_collection_acquire(options->config_profile_collection_cached); |
| 1009 |
} else { |
| 1010 |
|
| 1011 |
config_profile = s_load_profile(allocator); |
| 1012 |
if (!config_profile) { |
| 1013 |
goto on_finish; |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
profile_name = aws_get_profile_name(allocator, &options->profile_name_override); |
| 1018 |
profile = aws_profile_collection_get_profile(config_profile, profile_name); |
| 1019 |
|
| 1020 |
if (!profile) { |
| 1021 |
AWS_LOGF_ERROR( |
| 1022 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 1023 |
"Failed to resolve either region, role arn or token file path during sts web identity provider " |
| 1024 |
"initialization."); |
| 1025 |
goto on_finish; |
| 1026 |
|
| 1027 |
} else { |
| 1028 |
s_check_or_get_with_profile_config(allocator, profile, ®ion, s_region_config); |
| 1029 |
s_check_or_get_with_profile_config(allocator, profile, &role_arn, s_role_arn_config); |
| 1030 |
s_check_or_get_with_profile_config(allocator, profile, &role_session_name, s_role_session_name_config); |
| 1031 |
s_check_or_get_with_profile_config(allocator, profile, &token_file_path, s_token_file_path_config); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
if (aws_credentials_provider_construct_endpoint( |
| 1037 |
allocator, |
| 1038 |
¶meters->endpoint, |
| 1039 |
region, |
| 1040 |
s_sts_service_name, |
| 1041 |
s_sts_service_env_name, |
| 1042 |
s_sts_service_name, |
| 1043 |
config_profile, |
| 1044 |
profile, |
| 1045 |
NULL, |
| 1046 |
false)) { |
| 1047 |
AWS_LOGF_ERROR( |
| 1048 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to construct sts endpoint with, probably region is missing."); |
| 1049 |
goto on_finish; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
if (!role_arn || !role_arn->len || |
| 1054 |
aws_byte_buf_init_copy_from_cursor(¶meters->role_arn, allocator, aws_byte_cursor_from_string(role_arn))) { |
| 1055 |
AWS_LOGF_ERROR( |
| 1056 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 1057 |
"Failed to resolve role arn during sts web identity provider initialization."); |
| 1058 |
goto on_finish; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
if (!token_file_path || !token_file_path->len || |
| 1063 |
aws_byte_buf_init_copy_from_cursor( |
| 1064 |
¶meters->token_file_path, allocator, aws_byte_cursor_from_string(token_file_path))) { |
| 1065 |
AWS_LOGF_ERROR( |
| 1066 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 1067 |
"Failed to resolve token file path during sts web identity provider initialization."); |
| 1068 |
goto on_finish; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
if (role_session_name && role_session_name->len) { |
| 1073 |
if (aws_byte_buf_init_copy_from_cursor( |
| 1074 |
¶meters->role_session_name, allocator, aws_byte_cursor_from_string(role_session_name))) { |
| 1075 |
goto on_finish; |
| 1076 |
} |
| 1077 |
} else if (s_generate_uuid_to_buf(allocator, ¶meters->role_session_name)) { |
| 1078 |
goto on_finish; |
| 1079 |
} |
| 1080 |
|
| 1081 |
AWS_LOGF_DEBUG( |
| 1082 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 1083 |
"Successfully loaded all required parameters for sts web identity credentials provider."); |
| 1084 |
success = true; |
| 1085 |
|
| 1086 |
on_finish: |
| 1087 |
aws_string_destroy(region); |
| 1088 |
aws_string_destroy(role_arn); |
| 1089 |
aws_string_destroy(role_session_name); |
| 1090 |
aws_string_destroy(token_file_path); |
| 1091 |
aws_string_destroy(profile_name); |
| 1092 |
aws_profile_collection_release(config_profile); |
| 1093 |
if (!success) { |
| 1094 |
s_parameters_destroy(parameters); |
| 1095 |
parameters = NULL; |
| 1096 |
} |
| 1097 |
return parameters; |
| 1098 |
} |
| 1099 |
|
| 1100 |
struct aws_credentials_provider *aws_credentials_provider_new_sts_web_identity( |
| 1101 |
struct aws_allocator *allocator, |
| 1102 |
const struct aws_credentials_provider_sts_web_identity_options *options) { |
| 1103 |
|
| 1104 |
struct sts_web_identity_parameters *parameters = s_parameters_new(allocator, options); |
| 1105 |
if (!parameters) { |
| 1106 |
return NULL; |
| 1107 |
} |
| 1108 |
|
| 1109 |
struct aws_tls_connection_options tls_connection_options; |
| 1110 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 1111 |
|
| 1112 |
struct aws_credentials_provider *provider = NULL; |
| 1113 |
struct aws_credentials_provider_sts_web_identity_impl *impl = NULL; |
| 1114 |
|
| 1115 |
aws_mem_acquire_many( |
| 1116 |
allocator, |
| 1117 |
2, |
| 1118 |
&provider, |
| 1119 |
sizeof(struct aws_credentials_provider), |
| 1120 |
&impl, |
| 1121 |
sizeof(struct aws_credentials_provider_sts_web_identity_impl)); |
| 1122 |
|
| 1123 |
if (!provider) { |
| 1124 |
goto on_error; |
| 1125 |
} |
| 1126 |
|
| 1127 |
AWS_ZERO_STRUCT(*provider); |
| 1128 |
AWS_ZERO_STRUCT(*impl); |
| 1129 |
|
| 1130 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_sts_web_identity_vtable, impl); |
| 1131 |
|
| 1132 |
if (!options->tls_ctx) { |
| 1133 |
AWS_LOGF_ERROR( |
| 1134 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 1135 |
"a TLS context must be provided to the STS web identity credentials provider"); |
| 1136 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 1137 |
return NULL; |
| 1138 |
} |
| 1139 |
|
| 1140 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, options->tls_ctx); |
| 1141 |
struct aws_byte_cursor host = aws_byte_cursor_from_string(parameters->endpoint); |
| 1142 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &host)) { |
| 1143 |
AWS_LOGF_ERROR( |
| 1144 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 1145 |
"(id=%p): failed to create a tls connection options with error %s", |
| 1146 |
(void *)provider, |
| 1147 |
aws_error_str(aws_last_error())); |
| 1148 |
goto on_error; |
| 1149 |
} |
| 1150 |
|
| 1151 |
struct aws_socket_options socket_options; |
| 1152 |
AWS_ZERO_STRUCT(socket_options); |
| 1153 |
socket_options.type = AWS_SOCKET_STREAM; |
| 1154 |
socket_options.domain = AWS_SOCKET_IPV4; |
| 1155 |
socket_options.connect_timeout_ms = (uint32_t)aws_timestamp_convert( |
| 1156 |
STS_WEB_IDENTITY_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 1157 |
|
| 1158 |
struct aws_http_connection_manager_options manager_options; |
| 1159 |
AWS_ZERO_STRUCT(manager_options); |
| 1160 |
manager_options.bootstrap = options->bootstrap; |
| 1161 |
manager_options.initial_window_size = STS_WEB_IDENTITY_RESPONSE_SIZE_LIMIT; |
| 1162 |
manager_options.socket_options = &socket_options; |
| 1163 |
manager_options.host = host; |
| 1164 |
manager_options.port = 443; |
| 1165 |
manager_options.max_connections = 2; |
| 1166 |
manager_options.shutdown_complete_callback = s_on_connection_manager_shutdown; |
| 1167 |
manager_options.shutdown_complete_user_data = provider; |
| 1168 |
manager_options.tls_connection_options = &tls_connection_options; |
| 1169 |
manager_options.proxy_ev_settings = options->proxy_ev_settings; |
| 1170 |
|
| 1171 |
impl->function_table = options->function_table; |
| 1172 |
if (impl->function_table == NULL) { |
| 1173 |
impl->function_table = g_aws_credentials_provider_http_function_table; |
| 1174 |
} |
| 1175 |
|
| 1176 |
impl->connection_manager = impl->function_table->aws_http_connection_manager_new(allocator, &manager_options); |
| 1177 |
if (impl->connection_manager == NULL) { |
| 1178 |
goto on_error; |
| 1179 |
} |
| 1180 |
|
| 1181 |
impl->role_arn = aws_string_new_from_array(allocator, parameters->role_arn.buffer, parameters->role_arn.len); |
| 1182 |
if (impl->role_arn == NULL) { |
| 1183 |
goto on_error; |
| 1184 |
} |
| 1185 |
|
| 1186 |
impl->role_session_name = |
| 1187 |
aws_string_new_from_array(allocator, parameters->role_session_name.buffer, parameters->role_session_name.len); |
| 1188 |
if (impl->role_session_name == NULL) { |
| 1189 |
goto on_error; |
| 1190 |
} |
| 1191 |
|
| 1192 |
impl->token_file_path = |
| 1193 |
aws_string_new_from_array(allocator, parameters->token_file_path.buffer, parameters->token_file_path.len); |
| 1194 |
if (impl->token_file_path == NULL) { |
| 1195 |
goto on_error; |
| 1196 |
} |
| 1197 |
|
| 1198 |
impl->endpoint = aws_string_new_from_string(allocator, parameters->endpoint); |
| 1199 |
if (impl->endpoint == NULL) { |
| 1200 |
goto on_error; |
| 1201 |
} |
| 1202 |
|
| 1203 |
provider->shutdown_options = options->shutdown_options; |
| 1204 |
s_parameters_destroy(parameters); |
| 1205 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 1206 |
return provider; |
| 1207 |
|
| 1208 |
on_error: |
| 1209 |
|
| 1210 |
aws_credentials_provider_destroy(provider); |
| 1211 |
s_parameters_destroy(parameters); |
| 1212 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 1213 |
return NULL; |
| 1214 |
} |