| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "aws/common/byte_buf.h" |
| 7 |
#include <aws/auth/credentials.h> |
| 8 |
|
| 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/host_utils.h> |
| 14 |
#include <aws/common/string.h> |
| 15 |
#include <aws/http/connection.h> |
| 16 |
#include <aws/http/connection_manager.h> |
| 17 |
#include <aws/http/request_response.h> |
| 18 |
#include <aws/http/status_code.h> |
| 19 |
#include <aws/io/channel_bootstrap.h> |
| 20 |
#include <aws/io/host_resolver.h> |
| 21 |
#include <aws/io/logging.h> |
| 22 |
#include <aws/io/socket.h> |
| 23 |
#include <aws/io/tls_channel_handler.h> |
| 24 |
#include <aws/io/uri.h> |
| 25 |
|
| 26 |
#if defined(_MSC_VER) |
| 27 |
# pragma warning(disable : 4204) |
| 28 |
# pragma warning(disable : 4232) |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
#define ECS_RESPONSE_SIZE_INITIAL 2048 |
| 33 |
#define ECS_RESPONSE_SIZE_LIMIT 10000 |
| 34 |
#define ECS_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS 2 |
| 35 |
#define ECS_MAX_ATTEMPTS 3 |
| 36 |
#define ECS_RETRY_TIMEOUT_MS 100 |
| 37 |
|
| 38 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_token_file, "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE"); |
| 39 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_token, "AWS_CONTAINER_AUTHORIZATION_TOKEN"); |
| 40 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_relative_uri, "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"); |
| 41 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_creds_env_full_uri, "AWS_CONTAINER_CREDENTIALS_FULL_URI"); |
| 42 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_host, "169.254.170.2"); |
| 43 |
|
| 44 |
static void s_on_connection_manager_shutdown(void *user_data); |
| 45 |
|
| 46 |
struct aws_credentials_provider_ecs_impl { |
| 47 |
struct aws_http_connection_manager *connection_manager; |
| 48 |
const struct aws_auth_http_system_vtable *function_table; |
| 49 |
struct aws_string *host; |
| 50 |
struct aws_string *path_and_query; |
| 51 |
struct aws_string *auth_token_file_path; |
| 52 |
struct aws_string *auth_token; |
| 53 |
struct aws_client_bootstrap *bootstrap; |
| 54 |
struct aws_retry_strategy *retry_strategy; |
| 55 |
bool is_https; |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
struct aws_credentials_provider_ecs_user_data { |
| 62 |
|
| 63 |
struct aws_allocator *allocator; |
| 64 |
struct aws_credentials_provider *ecs_provider; |
| 65 |
aws_on_get_credentials_callback_fn *original_callback; |
| 66 |
void *original_user_data; |
| 67 |
struct aws_byte_buf auth_token; |
| 68 |
struct aws_retry_token *retry_token; |
| 69 |
|
| 70 |
|
| 71 |
struct aws_http_connection *connection; |
| 72 |
struct aws_http_message *request; |
| 73 |
struct aws_byte_buf current_result; |
| 74 |
int status_code; |
| 75 |
int error_code; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
static void s_ecs_user_data_reset_request_specific_data(struct aws_credentials_provider_ecs_user_data *user_data) { |
| 80 |
if (user_data->request) { |
| 81 |
aws_http_message_release(user_data->request); |
| 82 |
user_data->request = NULL; |
| 83 |
} |
| 84 |
if (user_data->connection) { |
| 85 |
struct aws_credentials_provider_ecs_impl *impl = user_data->ecs_provider->impl; |
| 86 |
int result = impl->function_table->aws_http_connection_manager_release_connection( |
| 87 |
impl->connection_manager, user_data->connection); |
| 88 |
(void)result; |
| 89 |
AWS_ASSERT(result == AWS_OP_SUCCESS); |
| 90 |
user_data->connection = NULL; |
| 91 |
} |
| 92 |
aws_byte_buf_reset(&user_data->current_result, false); |
| 93 |
user_data->status_code = 0; |
| 94 |
user_data->error_code = 0; |
| 95 |
} |
| 96 |
|
| 97 |
static void s_aws_credentials_provider_ecs_user_data_destroy(struct aws_credentials_provider_ecs_user_data *user_data) { |
| 98 |
if (user_data == NULL) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
struct aws_credentials_provider_ecs_impl *impl = user_data->ecs_provider->impl; |
| 103 |
|
| 104 |
if (user_data->connection) { |
| 105 |
impl->function_table->aws_http_connection_manager_release_connection( |
| 106 |
impl->connection_manager, user_data->connection); |
| 107 |
} |
| 108 |
|
| 109 |
aws_byte_buf_clean_up(&user_data->auth_token); |
| 110 |
aws_byte_buf_clean_up(&user_data->current_result); |
| 111 |
aws_retry_token_release(user_data->retry_token); |
| 112 |
|
| 113 |
if (user_data->request) { |
| 114 |
aws_http_message_destroy(user_data->request); |
| 115 |
} |
| 116 |
aws_credentials_provider_release(user_data->ecs_provider); |
| 117 |
aws_mem_release(user_data->allocator, user_data); |
| 118 |
} |
| 119 |
|
| 120 |
static struct aws_credentials_provider_ecs_user_data *s_aws_credentials_provider_ecs_user_data_new( |
| 121 |
struct aws_credentials_provider *ecs_provider, |
| 122 |
aws_on_get_credentials_callback_fn callback, |
| 123 |
void *user_data) { |
| 124 |
|
| 125 |
struct aws_credentials_provider_ecs_user_data *wrapped_user_data = |
| 126 |
aws_mem_calloc(ecs_provider->allocator, 1, sizeof(struct aws_credentials_provider_ecs_user_data)); |
| 127 |
|
| 128 |
wrapped_user_data->allocator = ecs_provider->allocator; |
| 129 |
wrapped_user_data->ecs_provider = ecs_provider; |
| 130 |
aws_credentials_provider_acquire(ecs_provider); |
| 131 |
wrapped_user_data->original_user_data = user_data; |
| 132 |
wrapped_user_data->original_callback = callback; |
| 133 |
|
| 134 |
if (aws_byte_buf_init(&wrapped_user_data->current_result, ecs_provider->allocator, ECS_RESPONSE_SIZE_INITIAL)) { |
| 135 |
goto on_error; |
| 136 |
} |
| 137 |
|
| 138 |
struct aws_credentials_provider_ecs_impl *impl = ecs_provider->impl; |
| 139 |
if (impl->auth_token_file_path != NULL && impl->auth_token_file_path->len > 0) { |
| 140 |
if (aws_byte_buf_init_from_file( |
| 141 |
&wrapped_user_data->auth_token, |
| 142 |
ecs_provider->allocator, |
| 143 |
aws_string_c_str(impl->auth_token_file_path))) { |
| 144 |
AWS_LOGF_ERROR( |
| 145 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 146 |
"(id=%p) ECS credentials provider failed to read token from the path: %s with error: %d", |
| 147 |
(void *)ecs_provider, |
| 148 |
aws_string_c_str(impl->auth_token_file_path), |
| 149 |
aws_last_error()); |
| 150 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_ECS_INVALID_TOKEN_FILE_PATH); |
| 151 |
goto on_error; |
| 152 |
} |
| 153 |
} else if (impl->auth_token != NULL && impl->auth_token->len > 0) { |
| 154 |
if (aws_byte_buf_init_copy_from_cursor( |
| 155 |
&wrapped_user_data->auth_token, |
| 156 |
ecs_provider->allocator, |
| 157 |
aws_byte_cursor_from_string(impl->auth_token))) { |
| 158 |
goto on_error; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return wrapped_user_data; |
| 163 |
on_error: |
| 164 |
s_aws_credentials_provider_ecs_user_data_destroy(wrapped_user_data); |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static void s_ecs_finalize_get_credentials_query(struct aws_credentials_provider_ecs_user_data *ecs_user_data) { |
| 185 |
|
| 186 |
struct aws_credentials *credentials = NULL; |
| 187 |
struct aws_parse_credentials_from_json_doc_options parse_options = { |
| 188 |
.access_key_id_name = "AccessKeyId", |
| 189 |
.secret_access_key_name = "SecretAccessKey", |
| 190 |
.token_name = "Token", |
| 191 |
.account_id_name = "AccountId", |
| 192 |
.expiration_name = "Expiration", |
| 193 |
.token_required = false, |
| 194 |
.expiration_required = true, |
| 195 |
}; |
| 196 |
if (aws_byte_buf_append_null_terminator(&ecs_user_data->current_result) == AWS_OP_SUCCESS) { |
| 197 |
credentials = aws_parse_credentials_from_json_document( |
| 198 |
ecs_user_data->allocator, aws_byte_cursor_from_buf(&ecs_user_data->current_result), &parse_options); |
| 199 |
} else { |
| 200 |
AWS_LOGF_ERROR( |
| 201 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 202 |
"(id=%p) ECS credentials provider failed to add null terminating char to resulting buffer.", |
| 203 |
(void *)ecs_user_data->ecs_provider); |
| 204 |
} |
| 205 |
|
| 206 |
if (credentials != NULL) { |
| 207 |
AWS_LOGF_INFO( |
| 208 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 209 |
"(id=%p) ECS credentials provider successfully queried instance role credentials", |
| 210 |
(void *)ecs_user_data->ecs_provider); |
| 211 |
|
| 212 |
int result = aws_retry_token_record_success(ecs_user_data->retry_token); |
| 213 |
(void)result; |
| 214 |
AWS_ASSERT(result == AWS_ERROR_SUCCESS); |
| 215 |
} else { |
| 216 |
AWS_LOGF_WARN( |
| 217 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 218 |
"(id=%p) ECS credentials provider failed to query instance role credentials with error %d(%s)", |
| 219 |
(void *)ecs_user_data->ecs_provider, |
| 220 |
ecs_user_data->error_code, |
| 221 |
aws_error_str(ecs_user_data->error_code)); |
| 222 |
enum aws_retry_error_type error_type = |
| 223 |
aws_credentials_provider_compute_retry_error_type(ecs_user_data->status_code, ecs_user_data->error_code); |
| 224 |
|
| 225 |
|
| 226 |
if (error_type != AWS_RETRY_ERROR_TYPE_CLIENT_ERROR && ecs_user_data->retry_token != NULL) { |
| 227 |
if (aws_retry_strategy_schedule_retry( |
| 228 |
ecs_user_data->retry_token, error_type, s_on_retry_ready, ecs_user_data) == AWS_OP_SUCCESS) { |
| 229 |
AWS_LOGF_INFO( |
| 230 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 231 |
"(id=%p): successfully scheduled a retry", |
| 232 |
(void *)ecs_user_data->ecs_provider); |
| 233 |
return; |
| 234 |
} else |
| 235 |
AWS_LOGF_ERROR( |
| 236 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 237 |
"(id=%p): failed to schedule retry: %s", |
| 238 |
(void *)ecs_user_data->ecs_provider, |
| 239 |
aws_error_str(aws_last_error())); |
| 240 |
ecs_user_data->error_code = aws_last_error(); |
| 241 |
} |
| 242 |
|
| 243 |
if (ecs_user_data->error_code == AWS_ERROR_SUCCESS) { |
| 244 |
ecs_user_data->error_code = aws_last_error(); |
| 245 |
if (ecs_user_data->error_code == AWS_ERROR_SUCCESS) { |
| 246 |
ecs_user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_ECS_SOURCE_FAILURE; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
ecs_user_data->original_callback(credentials, ecs_user_data->error_code, ecs_user_data->original_user_data); |
| 253 |
|
| 254 |
|
| 255 |
s_aws_credentials_provider_ecs_user_data_destroy(ecs_user_data); |
| 256 |
aws_credentials_release(credentials); |
| 257 |
} |
| 258 |
|
| 259 |
static int s_ecs_on_incoming_body_fn( |
| 260 |
struct aws_http_stream *stream, |
| 261 |
const struct aws_byte_cursor *data, |
| 262 |
void *user_data) { |
| 263 |
|
| 264 |
(void)stream; |
| 265 |
|
| 266 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 267 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 268 |
|
| 269 |
AWS_LOGF_TRACE( |
| 270 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 271 |
"(id=%p) ECS credentials provider received %zu response bytes", |
| 272 |
(void *)ecs_user_data->ecs_provider, |
| 273 |
data->len); |
| 274 |
|
| 275 |
if (data->len + ecs_user_data->current_result.len > ECS_RESPONSE_SIZE_LIMIT) { |
| 276 |
impl->function_table->aws_http_connection_close(ecs_user_data->connection); |
| 277 |
AWS_LOGF_ERROR( |
| 278 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 279 |
"(id=%p) ECS credentials provider query response exceeded maximum allowed length", |
| 280 |
(void *)ecs_user_data->ecs_provider); |
| 281 |
|
| 282 |
return aws_raise_error(AWS_ERROR_SHORT_BUFFER); |
| 283 |
} |
| 284 |
|
| 285 |
if (aws_byte_buf_append_dynamic(&ecs_user_data->current_result, data)) { |
| 286 |
impl->function_table->aws_http_connection_close(ecs_user_data->connection); |
| 287 |
AWS_LOGF_ERROR( |
| 288 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 289 |
"(id=%p) ECS credentials provider query error appending response", |
| 290 |
(void *)ecs_user_data->ecs_provider); |
| 291 |
|
| 292 |
return AWS_OP_ERR; |
| 293 |
} |
| 294 |
|
| 295 |
return AWS_OP_SUCCESS; |
| 296 |
} |
| 297 |
|
| 298 |
static int s_ecs_on_incoming_headers_fn( |
| 299 |
struct aws_http_stream *stream, |
| 300 |
enum aws_http_header_block header_block, |
| 301 |
const struct aws_http_header *header_array, |
| 302 |
size_t num_headers, |
| 303 |
void *user_data) { |
| 304 |
|
| 305 |
(void)header_array; |
| 306 |
(void)num_headers; |
| 307 |
|
| 308 |
if (header_block != AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 309 |
return AWS_OP_SUCCESS; |
| 310 |
} |
| 311 |
|
| 312 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 313 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 314 |
if (ecs_user_data->status_code == 0) { |
| 315 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 316 |
if (impl->function_table->aws_http_stream_get_incoming_response_status( |
| 317 |
stream, &ecs_user_data->status_code)) { |
| 318 |
|
| 319 |
AWS_LOGF_ERROR( |
| 320 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 321 |
"(id=%p) ECS credentials provider failed to get http status code", |
| 322 |
(void *)ecs_user_data->ecs_provider); |
| 323 |
|
| 324 |
return AWS_OP_ERR; |
| 325 |
} |
| 326 |
AWS_LOGF_DEBUG( |
| 327 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 328 |
"(id=%p) ECS credentials provider query received http status code %d", |
| 329 |
(void *)ecs_user_data->ecs_provider, |
| 330 |
ecs_user_data->status_code); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return AWS_OP_SUCCESS; |
| 335 |
} |
| 336 |
|
| 337 |
static void s_ecs_query_task_role_credentials(struct aws_credentials_provider_ecs_user_data *ecs_user_data); |
| 338 |
|
| 339 |
static void s_ecs_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 340 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 341 |
|
| 342 |
aws_http_message_destroy(ecs_user_data->request); |
| 343 |
ecs_user_data->request = NULL; |
| 344 |
|
| 345 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 346 |
impl->function_table->aws_http_stream_release(stream); |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (ecs_user_data->status_code != AWS_HTTP_STATUS_CODE_200_OK || error_code != AWS_OP_SUCCESS) { |
| 353 |
ecs_user_data->current_result.len = 0; |
| 354 |
|
| 355 |
if (error_code != AWS_OP_SUCCESS) { |
| 356 |
ecs_user_data->error_code = error_code; |
| 357 |
} else { |
| 358 |
ecs_user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_HTTP_STATUS_FAILURE; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 363 |
} |
| 364 |
|
| 365 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_accept_header, "Accept"); |
| 366 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_accept_header_value, "application/json"); |
| 367 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_user_agent_header, "User-Agent"); |
| 368 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_user_agent_header_value, "aws-sdk-crt/ecs-credentials-provider"); |
| 369 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_authorization_header, "Authorization"); |
| 370 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_accept_encoding_header, "Accept-Encoding"); |
| 371 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_accept_encoding_header_value, "identity"); |
| 372 |
AWS_STATIC_STRING_FROM_LITERAL(s_ecs_host_header, "Host"); |
| 373 |
|
| 374 |
static int s_make_ecs_http_query( |
| 375 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data, |
| 376 |
struct aws_byte_cursor *uri) { |
| 377 |
AWS_FATAL_ASSERT(ecs_user_data->connection); |
| 378 |
|
| 379 |
struct aws_http_stream *stream = NULL; |
| 380 |
struct aws_http_message *request = aws_http_message_new_request(ecs_user_data->allocator); |
| 381 |
if (request == NULL) { |
| 382 |
return AWS_OP_ERR; |
| 383 |
} |
| 384 |
|
| 385 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 386 |
|
| 387 |
struct aws_http_header host_header = { |
| 388 |
.name = aws_byte_cursor_from_string(s_ecs_host_header), |
| 389 |
.value = aws_byte_cursor_from_string(impl->host), |
| 390 |
}; |
| 391 |
if (aws_http_message_add_header(request, host_header)) { |
| 392 |
goto on_error; |
| 393 |
} |
| 394 |
|
| 395 |
if (ecs_user_data->auth_token.len) { |
| 396 |
struct aws_http_header auth_header = { |
| 397 |
.name = aws_byte_cursor_from_string(s_ecs_authorization_header), |
| 398 |
.value = aws_byte_cursor_from_buf(&ecs_user_data->auth_token), |
| 399 |
}; |
| 400 |
if (aws_http_message_add_header(request, auth_header)) { |
| 401 |
goto on_error; |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
struct aws_http_header accept_header = { |
| 406 |
.name = aws_byte_cursor_from_string(s_ecs_accept_header), |
| 407 |
.value = aws_byte_cursor_from_string(s_ecs_accept_header_value), |
| 408 |
}; |
| 409 |
if (aws_http_message_add_header(request, accept_header)) { |
| 410 |
goto on_error; |
| 411 |
} |
| 412 |
|
| 413 |
struct aws_http_header accept_encoding_header = { |
| 414 |
.name = aws_byte_cursor_from_string(s_ecs_accept_encoding_header), |
| 415 |
.value = aws_byte_cursor_from_string(s_ecs_accept_encoding_header_value), |
| 416 |
}; |
| 417 |
if (aws_http_message_add_header(request, accept_encoding_header)) { |
| 418 |
goto on_error; |
| 419 |
} |
| 420 |
|
| 421 |
struct aws_http_header user_agent_header = { |
| 422 |
.name = aws_byte_cursor_from_string(s_ecs_user_agent_header), |
| 423 |
.value = aws_byte_cursor_from_string(s_ecs_user_agent_header_value), |
| 424 |
}; |
| 425 |
if (aws_http_message_add_header(request, user_agent_header)) { |
| 426 |
goto on_error; |
| 427 |
} |
| 428 |
|
| 429 |
if (aws_http_message_set_request_path(request, *uri)) { |
| 430 |
goto on_error; |
| 431 |
} |
| 432 |
|
| 433 |
if (aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET"))) { |
| 434 |
goto on_error; |
| 435 |
} |
| 436 |
|
| 437 |
ecs_user_data->request = request; |
| 438 |
|
| 439 |
struct aws_http_make_request_options request_options = { |
| 440 |
.self_size = sizeof(request_options), |
| 441 |
.on_response_headers = s_ecs_on_incoming_headers_fn, |
| 442 |
.on_response_header_block_done = NULL, |
| 443 |
.on_response_body = s_ecs_on_incoming_body_fn, |
| 444 |
.on_complete = s_ecs_on_stream_complete_fn, |
| 445 |
.user_data = ecs_user_data, |
| 446 |
.request = request, |
| 447 |
}; |
| 448 |
|
| 449 |
|
| 450 |
struct aws_credentials_provider *provider = ecs_user_data->ecs_provider; |
| 451 |
aws_credentials_provider_acquire(provider); |
| 452 |
stream = impl->function_table->aws_http_connection_make_request(ecs_user_data->connection, &request_options); |
| 453 |
|
| 454 |
if (!stream) { |
| 455 |
goto on_error; |
| 456 |
} |
| 457 |
|
| 458 |
if (impl->function_table->aws_http_stream_activate(stream)) { |
| 459 |
goto on_error; |
| 460 |
} |
| 461 |
aws_credentials_provider_release(provider); |
| 462 |
|
| 463 |
return AWS_OP_SUCCESS; |
| 464 |
|
| 465 |
on_error: |
| 466 |
impl->function_table->aws_http_stream_release(stream); |
| 467 |
aws_http_message_destroy(request); |
| 468 |
ecs_user_data->request = NULL; |
| 469 |
return AWS_OP_ERR; |
| 470 |
} |
| 471 |
|
| 472 |
static void s_ecs_query_task_role_credentials(struct aws_credentials_provider_ecs_user_data *ecs_user_data) { |
| 473 |
AWS_FATAL_ASSERT(ecs_user_data->connection); |
| 474 |
|
| 475 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 476 |
|
| 477 |
struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_string(impl->path_and_query); |
| 478 |
if (s_make_ecs_http_query(ecs_user_data, &uri_cursor) == AWS_OP_ERR) { |
| 479 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
static void s_ecs_on_acquire_connection(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 484 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 485 |
|
| 486 |
if (connection == NULL) { |
| 487 |
AWS_LOGF_WARN( |
| 488 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 489 |
"id=%p: ECS provider failed to acquire a connection, error code %d(%s)", |
| 490 |
(void *)ecs_user_data->ecs_provider, |
| 491 |
error_code, |
| 492 |
aws_error_str(error_code)); |
| 493 |
|
| 494 |
ecs_user_data->error_code = error_code; |
| 495 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
ecs_user_data->connection = connection; |
| 500 |
|
| 501 |
s_ecs_query_task_role_credentials(ecs_user_data); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) { |
| 506 |
(void)token; |
| 507 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 508 |
|
| 509 |
if (error_code) { |
| 510 |
AWS_LOGF_WARN( |
| 511 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 512 |
"id=%p: ECS provider failed to acquire a connection, error code %d(%s)", |
| 513 |
(void *)ecs_user_data->ecs_provider, |
| 514 |
error_code, |
| 515 |
aws_error_str(error_code)); |
| 516 |
|
| 517 |
ecs_user_data->error_code = error_code; |
| 518 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
s_ecs_user_data_reset_request_specific_data(ecs_user_data); |
| 524 |
|
| 525 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 526 |
impl->function_table->aws_http_connection_manager_acquire_connection( |
| 527 |
impl->connection_manager, s_ecs_on_acquire_connection, ecs_user_data); |
| 528 |
} |
| 529 |
|
| 530 |
static void s_on_retry_token_acquired( |
| 531 |
struct aws_retry_strategy *strategy, |
| 532 |
int error_code, |
| 533 |
struct aws_retry_token *token, |
| 534 |
void *user_data) { |
| 535 |
(void)strategy; |
| 536 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 537 |
|
| 538 |
if (error_code) { |
| 539 |
AWS_LOGF_WARN( |
| 540 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 541 |
"id=%p: ECS provider failed to acquire a connection, error code %d(%s)", |
| 542 |
(void *)ecs_user_data->ecs_provider, |
| 543 |
error_code, |
| 544 |
aws_error_str(error_code)); |
| 545 |
|
| 546 |
ecs_user_data->error_code = error_code; |
| 547 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 548 |
return; |
| 549 |
} |
| 550 |
|
| 551 |
ecs_user_data->retry_token = token; |
| 552 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 553 |
impl->function_table->aws_http_connection_manager_acquire_connection( |
| 554 |
impl->connection_manager, s_ecs_on_acquire_connection, ecs_user_data); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
static bool s_is_valid_remote_host_ip(const struct aws_host_address *host_address) { |
| 564 |
bool result = false; |
| 565 |
struct aws_byte_cursor address = aws_byte_cursor_from_string(host_address->address); |
| 566 |
if (host_address->record_type == AWS_ADDRESS_RECORD_TYPE_A) { |
| 567 |
const struct aws_byte_cursor ipv4_loopback_address_prefix = aws_byte_cursor_from_c_str("127."); |
| 568 |
const struct aws_byte_cursor ecs_container_host_address = aws_byte_cursor_from_c_str("169.254.170.2"); |
| 569 |
const struct aws_byte_cursor eks_container_host_address = aws_byte_cursor_from_c_str("169.254.170.23"); |
| 570 |
|
| 571 |
result |= aws_byte_cursor_starts_with(&address, &ipv4_loopback_address_prefix); |
| 572 |
result |= aws_byte_cursor_eq(&address, &ecs_container_host_address); |
| 573 |
result |= aws_byte_cursor_eq(&address, &eks_container_host_address); |
| 574 |
|
| 575 |
} else if (host_address->record_type == AWS_ADDRESS_RECORD_TYPE_AAAA) { |
| 576 |
|
| 577 |
const struct aws_byte_cursor ipv6_loopback_address = aws_byte_cursor_from_c_str("::1"); |
| 578 |
const struct aws_byte_cursor ipv6_loopback_address_verbose = aws_byte_cursor_from_c_str("0:0:0:0:0:0:0:1"); |
| 579 |
const struct aws_byte_cursor eks_container_host_ipv6_address = aws_byte_cursor_from_c_str("fd00:ec2::23"); |
| 580 |
const struct aws_byte_cursor eks_container_host_ipv6_address_verbose = |
| 581 |
aws_byte_cursor_from_c_str("fd00:ec2:0:0:0:0:0:23"); |
| 582 |
|
| 583 |
result |= aws_byte_cursor_eq(&address, &ipv6_loopback_address); |
| 584 |
result |= aws_byte_cursor_eq(&address, &ipv6_loopback_address_verbose); |
| 585 |
result |= aws_byte_cursor_eq(&address, &eks_container_host_ipv6_address); |
| 586 |
result |= aws_byte_cursor_eq(&address, &eks_container_host_ipv6_address_verbose); |
| 587 |
} |
| 588 |
|
| 589 |
return result; |
| 590 |
} |
| 591 |
|
| 592 |
static void s_on_host_resolved( |
| 593 |
struct aws_host_resolver *resolver, |
| 594 |
const struct aws_string *host_name, |
| 595 |
int error_code, |
| 596 |
const struct aws_array_list *host_addresses, |
| 597 |
void *user_data) { |
| 598 |
(void)resolver; |
| 599 |
(void)host_name; |
| 600 |
|
| 601 |
struct aws_credentials_provider_ecs_user_data *ecs_user_data = user_data; |
| 602 |
if (error_code) { |
| 603 |
AWS_LOGF_WARN( |
| 604 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 605 |
"id=%p: ECS provider failed to resolve host, error code %d(%s)", |
| 606 |
(void *)ecs_user_data->ecs_provider, |
| 607 |
error_code, |
| 608 |
aws_error_str(error_code)); |
| 609 |
ecs_user_data->error_code = error_code; |
| 610 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 611 |
return; |
| 612 |
} |
| 613 |
size_t host_addresses_len = aws_array_list_length(host_addresses); |
| 614 |
if (!host_addresses_len) { |
| 615 |
goto on_error; |
| 616 |
} |
| 617 |
for (size_t i = 0; i < host_addresses_len; ++i) { |
| 618 |
struct aws_host_address *host_address_ptr = NULL; |
| 619 |
aws_array_list_get_at_ptr(host_addresses, (void **)&host_address_ptr, i); |
| 620 |
if (!s_is_valid_remote_host_ip(host_address_ptr)) { |
| 621 |
goto on_error; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
struct aws_credentials_provider_ecs_impl *impl = ecs_user_data->ecs_provider->impl; |
| 626 |
|
| 627 |
if (aws_retry_strategy_acquire_retry_token( |
| 628 |
impl->retry_strategy, NULL, s_on_retry_token_acquired, ecs_user_data, ECS_RETRY_TIMEOUT_MS)) { |
| 629 |
AWS_LOGF_ERROR( |
| 630 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 631 |
"(id=%p): failed to acquire retry token: %s", |
| 632 |
(void *)ecs_user_data->ecs_provider, |
| 633 |
aws_error_debug_str(aws_last_error())); |
| 634 |
goto on_error; |
| 635 |
} |
| 636 |
|
| 637 |
return; |
| 638 |
on_error: |
| 639 |
AWS_LOGF_ERROR( |
| 640 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 641 |
"id=%p: ECS provider failed to resolve address to an allowed ip address with error %d(%s)", |
| 642 |
(void *)ecs_user_data->ecs_provider, |
| 643 |
AWS_AUTH_CREDENTIALS_PROVIDER_ECS_INVALID_HOST, |
| 644 |
aws_error_str(AWS_AUTH_CREDENTIALS_PROVIDER_ECS_INVALID_HOST)); |
| 645 |
ecs_user_data->error_code = AWS_AUTH_CREDENTIALS_PROVIDER_ECS_INVALID_HOST; |
| 646 |
s_ecs_finalize_get_credentials_query(ecs_user_data); |
| 647 |
} |
| 648 |
|
| 649 |
static int s_credentials_provider_ecs_get_credentials_async( |
| 650 |
struct aws_credentials_provider *provider, |
| 651 |
aws_on_get_credentials_callback_fn callback, |
| 652 |
void *user_data) { |
| 653 |
|
| 654 |
AWS_LOGF_DEBUG( |
| 655 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "id=%p: ECS provider trying to load credentials", (void *)provider); |
| 656 |
|
| 657 |
struct aws_credentials_provider_ecs_impl *impl = provider->impl; |
| 658 |
|
| 659 |
struct aws_credentials_provider_ecs_user_data *wrapped_user_data = |
| 660 |
s_aws_credentials_provider_ecs_user_data_new(provider, callback, user_data); |
| 661 |
if (wrapped_user_data == NULL) { |
| 662 |
goto error; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
if (impl->is_https || aws_string_eq(impl->host, s_ecs_host)) { |
| 668 |
if (aws_retry_strategy_acquire_retry_token( |
| 669 |
impl->retry_strategy, NULL, s_on_retry_token_acquired, wrapped_user_data, ECS_RETRY_TIMEOUT_MS)) { |
| 670 |
AWS_LOGF_ERROR( |
| 671 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 672 |
"(id=%p): failed to acquire retry token: %s", |
| 673 |
(void *)wrapped_user_data->ecs_provider, |
| 674 |
aws_error_debug_str(aws_last_error())); |
| 675 |
goto error; |
| 676 |
} |
| 677 |
} else if (aws_host_resolver_resolve_host( |
| 678 |
impl->bootstrap->host_resolver, |
| 679 |
impl->host, |
| 680 |
s_on_host_resolved, |
| 681 |
&impl->bootstrap->host_resolver_config, |
| 682 |
wrapped_user_data)) { |
| 683 |
goto error; |
| 684 |
} |
| 685 |
return AWS_OP_SUCCESS; |
| 686 |
|
| 687 |
error: |
| 688 |
|
| 689 |
s_aws_credentials_provider_ecs_user_data_destroy(wrapped_user_data); |
| 690 |
|
| 691 |
return AWS_OP_ERR; |
| 692 |
} |
| 693 |
|
| 694 |
static void s_credentials_provider_ecs_destroy(struct aws_credentials_provider *provider) { |
| 695 |
struct aws_credentials_provider_ecs_impl *impl = provider->impl; |
| 696 |
if (impl == NULL) { |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
aws_string_destroy(impl->path_and_query); |
| 701 |
aws_string_destroy(impl->auth_token); |
| 702 |
aws_string_destroy(impl->auth_token_file_path); |
| 703 |
aws_string_destroy(impl->host); |
| 704 |
aws_retry_strategy_release(impl->retry_strategy); |
| 705 |
aws_client_bootstrap_release(impl->bootstrap); |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
if (impl->connection_manager) { |
| 712 |
impl->function_table->aws_http_connection_manager_release(impl->connection_manager); |
| 713 |
} else { |
| 714 |
|
| 715 |
|
| 716 |
s_on_connection_manager_shutdown(provider); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
} |
| 721 |
|
| 722 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_ecs_vtable = { |
| 723 |
.get_credentials = s_credentials_provider_ecs_get_credentials_async, |
| 724 |
.destroy = s_credentials_provider_ecs_destroy, |
| 725 |
}; |
| 726 |
|
| 727 |
static void s_on_connection_manager_shutdown(void *user_data) { |
| 728 |
struct aws_credentials_provider *provider = user_data; |
| 729 |
|
| 730 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 731 |
aws_mem_release(provider->allocator, provider); |
| 732 |
} |
| 733 |
|
| 734 |
struct aws_credentials_provider *aws_credentials_provider_new_ecs( |
| 735 |
struct aws_allocator *allocator, |
| 736 |
const struct aws_credentials_provider_ecs_options *options) { |
| 737 |
|
| 738 |
if (!options->bootstrap) { |
| 739 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "ECS provider: bootstrap must be specified"); |
| 740 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 741 |
return NULL; |
| 742 |
} |
| 743 |
|
| 744 |
struct aws_credentials_provider *provider = NULL; |
| 745 |
struct aws_credentials_provider_ecs_impl *impl = NULL; |
| 746 |
|
| 747 |
aws_mem_acquire_many( |
| 748 |
allocator, |
| 749 |
2, |
| 750 |
&provider, |
| 751 |
sizeof(struct aws_credentials_provider), |
| 752 |
&impl, |
| 753 |
sizeof(struct aws_credentials_provider_ecs_impl)); |
| 754 |
|
| 755 |
if (!provider) { |
| 756 |
return NULL; |
| 757 |
} |
| 758 |
|
| 759 |
AWS_ZERO_STRUCT(*provider); |
| 760 |
AWS_ZERO_STRUCT(*impl); |
| 761 |
|
| 762 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_ecs_vtable, impl); |
| 763 |
impl->bootstrap = aws_client_bootstrap_acquire(options->bootstrap); |
| 764 |
struct aws_tls_connection_options tls_connection_options; |
| 765 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 766 |
if (options->tls_ctx) { |
| 767 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, options->tls_ctx); |
| 768 |
struct aws_byte_cursor host = options->host; |
| 769 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &host)) { |
| 770 |
AWS_LOGF_ERROR( |
| 771 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 772 |
"(id=%p): failed to create a tls connection options with error %s", |
| 773 |
(void *)provider, |
| 774 |
aws_error_debug_str(aws_last_error())); |
| 775 |
goto on_error; |
| 776 |
} |
| 777 |
impl->is_https = true; |
| 778 |
} |
| 779 |
|
| 780 |
struct aws_socket_options socket_options; |
| 781 |
AWS_ZERO_STRUCT(socket_options); |
| 782 |
socket_options.type = AWS_SOCKET_STREAM; |
| 783 |
socket_options.domain = AWS_SOCKET_IPV4; |
| 784 |
socket_options.connect_timeout_ms = (uint32_t)aws_timestamp_convert( |
| 785 |
ECS_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 786 |
|
| 787 |
struct aws_http_connection_manager_options manager_options; |
| 788 |
AWS_ZERO_STRUCT(manager_options); |
| 789 |
manager_options.bootstrap = options->bootstrap; |
| 790 |
manager_options.initial_window_size = ECS_RESPONSE_SIZE_LIMIT; |
| 791 |
manager_options.socket_options = &socket_options; |
| 792 |
manager_options.host = options->host; |
| 793 |
if (options->port == 0) { |
| 794 |
manager_options.port = options->tls_ctx ? 443 : 80; |
| 795 |
} else { |
| 796 |
manager_options.port = options->port; |
| 797 |
} |
| 798 |
manager_options.max_connections = 2; |
| 799 |
manager_options.shutdown_complete_callback = s_on_connection_manager_shutdown; |
| 800 |
manager_options.shutdown_complete_user_data = provider; |
| 801 |
manager_options.tls_connection_options = options->tls_ctx ? &tls_connection_options : NULL; |
| 802 |
manager_options.proxy_ev_settings = options->proxy_ev_settings; |
| 803 |
|
| 804 |
impl->function_table = options->function_table; |
| 805 |
if (impl->function_table == NULL) { |
| 806 |
impl->function_table = g_aws_credentials_provider_http_function_table; |
| 807 |
} |
| 808 |
|
| 809 |
impl->connection_manager = impl->function_table->aws_http_connection_manager_new(allocator, &manager_options); |
| 810 |
if (impl->connection_manager == NULL) { |
| 811 |
goto on_error; |
| 812 |
} |
| 813 |
if (options->auth_token.len != 0) { |
| 814 |
impl->auth_token = aws_string_new_from_cursor(allocator, &options->auth_token); |
| 815 |
if (impl->auth_token == NULL) { |
| 816 |
goto on_error; |
| 817 |
} |
| 818 |
} |
| 819 |
if (options->auth_token_file_path.len != 0) { |
| 820 |
impl->auth_token_file_path = aws_string_new_from_cursor(allocator, &options->auth_token_file_path); |
| 821 |
if (impl->auth_token_file_path == NULL) { |
| 822 |
goto on_error; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
impl->path_and_query = aws_string_new_from_cursor(allocator, &options->path_and_query); |
| 827 |
if (impl->path_and_query == NULL) { |
| 828 |
goto on_error; |
| 829 |
} |
| 830 |
|
| 831 |
impl->host = aws_string_new_from_cursor(allocator, &options->host); |
| 832 |
if (impl->host == NULL) { |
| 833 |
goto on_error; |
| 834 |
} |
| 835 |
|
| 836 |
struct aws_standard_retry_options retry_options = { |
| 837 |
.backoff_retry_options = |
| 838 |
{ |
| 839 |
.el_group = options->bootstrap->event_loop_group, |
| 840 |
.max_retries = ECS_MAX_ATTEMPTS, |
| 841 |
}, |
| 842 |
}; |
| 843 |
impl->retry_strategy = aws_retry_strategy_new_standard(allocator, &retry_options); |
| 844 |
|
| 845 |
provider->shutdown_options = options->shutdown_options; |
| 846 |
|
| 847 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 848 |
|
| 849 |
return provider; |
| 850 |
|
| 851 |
on_error: |
| 852 |
|
| 853 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 854 |
aws_credentials_provider_destroy(provider); |
| 855 |
|
| 856 |
return NULL; |
| 857 |
} |
| 858 |
|
| 859 |
struct aws_credentials_provider *aws_credentials_provider_new_ecs_from_environment( |
| 860 |
struct aws_allocator *allocator, |
| 861 |
const struct aws_credentials_provider_ecs_environment_options *options) { |
| 862 |
|
| 863 |
if (!options->tls_ctx) { |
| 864 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "ECS provider: tls_ctx must be specified"); |
| 865 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 866 |
return NULL; |
| 867 |
} |
| 868 |
|
| 869 |
struct aws_credentials_provider_ecs_options explicit_options = { |
| 870 |
.shutdown_options = options->shutdown_options, |
| 871 |
.bootstrap = options->bootstrap, |
| 872 |
.function_table = options->function_table, |
| 873 |
.proxy_ev_settings = options->proxy_ev_settings, |
| 874 |
}; |
| 875 |
|
| 876 |
struct aws_string *ecs_env_token_file_path = NULL; |
| 877 |
struct aws_string *ecs_env_token = NULL; |
| 878 |
struct aws_string *relative_uri_str = NULL; |
| 879 |
struct aws_string *full_uri_str = NULL; |
| 880 |
struct aws_uri full_uri; |
| 881 |
AWS_ZERO_STRUCT(full_uri); |
| 882 |
struct aws_credentials_provider *provider = NULL; |
| 883 |
|
| 884 |
|
| 885 |
aws_get_environment_value(allocator, s_ecs_creds_env_token_file, &ecs_env_token_file_path); |
| 886 |
aws_get_environment_value(allocator, s_ecs_creds_env_token, &ecs_env_token); |
| 887 |
aws_get_environment_value(allocator, s_ecs_creds_env_relative_uri, &relative_uri_str); |
| 888 |
aws_get_environment_value(allocator, s_ecs_creds_env_full_uri, &full_uri_str); |
| 889 |
|
| 890 |
if (ecs_env_token_file_path != NULL && ecs_env_token_file_path->len > 0) { |
| 891 |
explicit_options.auth_token_file_path = aws_byte_cursor_from_string(ecs_env_token_file_path); |
| 892 |
} |
| 893 |
if (ecs_env_token != NULL && ecs_env_token->len > 0) { |
| 894 |
explicit_options.auth_token = aws_byte_cursor_from_string(ecs_env_token); |
| 895 |
} |
| 896 |
|
| 897 |
if (relative_uri_str != NULL && relative_uri_str->len != 0) { |
| 898 |
|
| 899 |
|
| 900 |
AWS_LOGF_INFO( |
| 901 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 902 |
"ECS provider: using relative uri %s", |
| 903 |
aws_string_c_str(relative_uri_str)); |
| 904 |
|
| 905 |
explicit_options.path_and_query = aws_byte_cursor_from_string(relative_uri_str); |
| 906 |
explicit_options.host = aws_byte_cursor_from_string(s_ecs_host); |
| 907 |
explicit_options.port = 80; |
| 908 |
|
| 909 |
provider = aws_credentials_provider_new_ecs(allocator, &explicit_options); |
| 910 |
|
| 911 |
} else if (full_uri_str != NULL && full_uri_str->len != 0) { |
| 912 |
|
| 913 |
|
| 914 |
AWS_LOGF_INFO( |
| 915 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "ECS provider: using full uri %s", aws_string_c_str(full_uri_str)); |
| 916 |
struct aws_byte_cursor full_uri_cursor = aws_byte_cursor_from_string(full_uri_str); |
| 917 |
if (aws_uri_init_parse(&full_uri, allocator, &full_uri_cursor)) { |
| 918 |
AWS_LOGF_ERROR( |
| 919 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 920 |
"ECS provider: Failed because %s environment variable is invalid uri %s.", |
| 921 |
aws_string_c_str(s_ecs_creds_env_full_uri), |
| 922 |
aws_string_c_str(full_uri_str)); |
| 923 |
goto cleanup; |
| 924 |
} |
| 925 |
|
| 926 |
explicit_options.host = *aws_uri_host_name(&full_uri); |
| 927 |
explicit_options.path_and_query = *aws_uri_path_and_query(&full_uri); |
| 928 |
if (explicit_options.path_and_query.len == 0) { |
| 929 |
explicit_options.path_and_query = aws_byte_cursor_from_c_str("/"); |
| 930 |
} |
| 931 |
|
| 932 |
if (aws_byte_cursor_eq_c_str_ignore_case(aws_uri_scheme(&full_uri), "https")) { |
| 933 |
explicit_options.tls_ctx = options->tls_ctx; |
| 934 |
} |
| 935 |
|
| 936 |
explicit_options.port = aws_uri_port(&full_uri); |
| 937 |
if (explicit_options.port == 0) { |
| 938 |
explicit_options.port = explicit_options.tls_ctx ? 443 : 80; |
| 939 |
} |
| 940 |
|
| 941 |
provider = aws_credentials_provider_new_ecs(allocator, &explicit_options); |
| 942 |
|
| 943 |
} else { |
| 944 |
|
| 945 |
AWS_LOGF_INFO( |
| 946 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 947 |
"ECS provider: Unable to initialize from environment because AWS_CONTAINER_CREDENTIALS_FULL_URI and " |
| 948 |
"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI are not set."); |
| 949 |
aws_raise_error(AWS_AUTH_CREDENTIALS_PROVIDER_INVALID_ENVIRONMENT); |
| 950 |
goto cleanup; |
| 951 |
} |
| 952 |
|
| 953 |
cleanup: |
| 954 |
aws_string_destroy(relative_uri_str); |
| 955 |
aws_string_destroy(full_uri_str); |
| 956 |
aws_string_destroy(ecs_env_token_file_path); |
| 957 |
aws_string_destroy(ecs_env_token); |
| 958 |
|
| 959 |
aws_uri_clean_up(&full_uri); |
| 960 |
return provider; |
| 961 |
} |