| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/aws_imds_client.h> |
| 7 |
#include <aws/auth/private/credentials_utils.h> |
| 8 |
#include <aws/common/clock.h> |
| 9 |
#include <aws/common/condition_variable.h> |
| 10 |
#include <aws/common/environment.h> |
| 11 |
#include <aws/common/mutex.h> |
| 12 |
#include <aws/common/string.h> |
| 13 |
#include <aws/common/uri.h> |
| 14 |
#include <aws/http/connection.h> |
| 15 |
#include <aws/http/request_response.h> |
| 16 |
#include <aws/http/status_code.h> |
| 17 |
#include <aws/io/channel_bootstrap.h> |
| 18 |
#include <aws/io/socket.h> |
| 19 |
#include <aws/io/tls_channel_handler.h> |
| 20 |
#include <ctype.h> |
| 21 |
|
| 22 |
#include <aws/common/json.h> |
| 23 |
|
| 24 |
#if defined(_MSC_VER) |
| 25 |
# pragma warning(disable : 4204) |
| 26 |
# pragma warning(disable : 4232) |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
#define IMDS_RESPONSE_SIZE_INITIAL 2048 |
| 31 |
#define IMDS_RESPONSE_TOKEN_SIZE_INITIAL 64 |
| 32 |
#define IMDS_RESPONSE_SIZE_LIMIT 65535 |
| 33 |
#define IMDS_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS 2 |
| 34 |
#define IMDS_DEFAULT_RETRIES 1 |
| 35 |
|
| 36 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_host, "169.254.169.254"); |
| 37 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_host_ipv6, "fd00:ec2::254"); |
| 38 |
AWS_STATIC_STRING_FROM_LITERAL(s_ec2_metadata_service_endpoint_env, "AWS_EC2_METADATA_SERVICE_ENDPOINT"); |
| 39 |
AWS_STATIC_STRING_FROM_LITERAL(s_ec2_metadata_service_endpoint_mode_env, "AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE"); |
| 40 |
static struct aws_byte_cursor s_ec2_metadata_service_endpoint_mode_ipv4 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("IPv4"); |
| 41 |
static struct aws_byte_cursor s_ec2_metadata_service_endpoint_mode_ipv6 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("IPv6"); |
| 42 |
|
| 43 |
enum imds_token_state { |
| 44 |
AWS_IMDS_TS_INVALID, |
| 45 |
AWS_IMDS_TS_VALID, |
| 46 |
AWS_IMDS_TS_UPDATE_IN_PROGRESS, |
| 47 |
}; |
| 48 |
|
| 49 |
enum imds_token_copy_result { |
| 50 |
|
| 51 |
AWS_IMDS_TCR_SUCCESS, |
| 52 |
|
| 53 |
AWS_IMDS_TCR_WAITING_IN_QUEUE, |
| 54 |
|
| 55 |
AWS_IMDS_TCR_UNEXPECTED_ERROR, |
| 56 |
}; |
| 57 |
|
| 58 |
struct imds_token_query { |
| 59 |
struct aws_linked_list_node node; |
| 60 |
void *user_data; |
| 61 |
}; |
| 62 |
|
| 63 |
struct aws_imds_client { |
| 64 |
struct aws_allocator *allocator; |
| 65 |
struct aws_http_connection_manager *connection_manager; |
| 66 |
struct aws_retry_strategy *retry_strategy; |
| 67 |
const struct aws_auth_http_system_vtable *function_table; |
| 68 |
struct aws_imds_client_shutdown_options shutdown_options; |
| 69 |
|
| 70 |
|
| 71 |
bool token_required; |
| 72 |
struct aws_byte_buf cached_token; |
| 73 |
uint64_t cached_token_expiration_timestamp; |
| 74 |
enum imds_token_state token_state; |
| 75 |
struct aws_linked_list pending_queries; |
| 76 |
struct aws_mutex token_lock; |
| 77 |
struct aws_condition_variable token_signal; |
| 78 |
bool ec2_metadata_v1_disabled; |
| 79 |
|
| 80 |
|
| 81 |
struct aws_string *endpoint_host; |
| 82 |
uint32_t endpoint_port; |
| 83 |
bool endpoint_uses_tls; |
| 84 |
|
| 85 |
struct aws_atomic_var ref_count; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static void s_resolve_imds_endpoint( |
| 92 |
struct aws_imds_client *client, |
| 93 |
const struct aws_imds_client_options *options, |
| 94 |
enum aws_socket_domain *out_socket_domain) { |
| 95 |
|
| 96 |
struct aws_allocator *allocator = client->allocator; |
| 97 |
enum aws_imds_endpoint_mode endpoint_mode = options->imds_endpoint_mode; |
| 98 |
struct aws_uri env_endpoint; |
| 99 |
AWS_ZERO_STRUCT(env_endpoint); |
| 100 |
bool env_endpoint_initialized = false; |
| 101 |
|
| 102 |
|
| 103 |
if (options->imds_endpoint.ptr == NULL || options->imds_endpoint.len == 0) { |
| 104 |
struct aws_string *endpoint_env = |
| 105 |
aws_get_env_nonempty(allocator, aws_string_c_str(s_ec2_metadata_service_endpoint_env)); |
| 106 |
if (endpoint_env) { |
| 107 |
|
| 108 |
struct aws_byte_cursor endpoint_cursor = aws_byte_cursor_from_string(endpoint_env); |
| 109 |
if (aws_uri_init_parse(&env_endpoint, allocator, &endpoint_cursor) == AWS_OP_SUCCESS) { |
| 110 |
env_endpoint_initialized = true; |
| 111 |
} |
| 112 |
aws_string_destroy(endpoint_env); |
| 113 |
} |
| 114 |
} else { |
| 115 |
|
| 116 |
if (aws_uri_init_parse(&env_endpoint, allocator, &options->imds_endpoint) == AWS_OP_SUCCESS) { |
| 117 |
env_endpoint_initialized = true; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
if (endpoint_mode == AWS_IMDS_ENDPOINT_MODE_DEFAULT) { |
| 123 |
struct aws_string *mode_env = |
| 124 |
aws_get_env_nonempty(allocator, aws_string_c_str(s_ec2_metadata_service_endpoint_mode_env)); |
| 125 |
if (mode_env) { |
| 126 |
struct aws_byte_cursor mode_cursor = aws_byte_cursor_from_string(mode_env); |
| 127 |
if (aws_byte_cursor_eq_ignore_case(&mode_cursor, &s_ec2_metadata_service_endpoint_mode_ipv6)) { |
| 128 |
endpoint_mode = AWS_IMDS_ENDPOINT_MODE_IPV6; |
| 129 |
} else if (aws_byte_cursor_eq_ignore_case(&mode_cursor, &s_ec2_metadata_service_endpoint_mode_ipv4)) { |
| 130 |
endpoint_mode = AWS_IMDS_ENDPOINT_MODE_IPV4; |
| 131 |
} |
| 132 |
|
| 133 |
aws_string_destroy(mode_env); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
*out_socket_domain = (endpoint_mode == AWS_IMDS_ENDPOINT_MODE_IPV6) ? AWS_SOCKET_IPV6 : AWS_SOCKET_IPV4; |
| 139 |
|
| 140 |
|
| 141 |
struct aws_byte_cursor host_cursor_from_source; |
| 142 |
if (env_endpoint_initialized) { |
| 143 |
|
| 144 |
host_cursor_from_source = *aws_uri_host_name(&env_endpoint); |
| 145 |
client->endpoint_port = aws_uri_port(&env_endpoint); |
| 146 |
|
| 147 |
|
| 148 |
struct aws_byte_cursor scheme_cursor = *aws_uri_scheme(&env_endpoint); |
| 149 |
if (aws_byte_cursor_eq_c_str_ignore_case(&scheme_cursor, "https")) { |
| 150 |
client->endpoint_uses_tls = true; |
| 151 |
if (client->endpoint_port == 0) { |
| 152 |
client->endpoint_port = 443; |
| 153 |
} |
| 154 |
} else { |
| 155 |
client->endpoint_uses_tls = false; |
| 156 |
if (client->endpoint_port == 0) { |
| 157 |
client->endpoint_port = 80; |
| 158 |
} |
| 159 |
} |
| 160 |
client->endpoint_host = aws_string_new_from_cursor(allocator, &host_cursor_from_source); |
| 161 |
} else { |
| 162 |
|
| 163 |
if (endpoint_mode == AWS_IMDS_ENDPOINT_MODE_IPV6) { |
| 164 |
client->endpoint_host = aws_string_clone_or_reuse(allocator, s_imds_host_ipv6); |
| 165 |
} else { |
| 166 |
client->endpoint_host = aws_string_clone_or_reuse(allocator, s_imds_host); |
| 167 |
} |
| 168 |
client->endpoint_port = 80; |
| 169 |
client->endpoint_uses_tls = false; |
| 170 |
} |
| 171 |
|
| 172 |
if (env_endpoint_initialized) { |
| 173 |
aws_uri_clean_up(&env_endpoint); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void s_aws_imds_client_destroy(struct aws_imds_client *client) { |
| 178 |
if (!client) { |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
aws_retry_strategy_release(client->retry_strategy); |
| 186 |
aws_condition_variable_clean_up(&client->token_signal); |
| 187 |
aws_mutex_clean_up(&client->token_lock); |
| 188 |
aws_byte_buf_clean_up(&client->cached_token); |
| 189 |
|
| 190 |
|
| 191 |
aws_string_destroy(client->endpoint_host); |
| 192 |
|
| 193 |
client->function_table->aws_http_connection_manager_release(client->connection_manager); |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
static void s_on_connection_manager_shutdown(void *user_data) { |
| 198 |
struct aws_imds_client *client = user_data; |
| 199 |
if (!client) { |
| 200 |
return; |
| 201 |
} |
| 202 |
if (client->shutdown_options.shutdown_callback) { |
| 203 |
client->shutdown_options.shutdown_callback(client->shutdown_options.shutdown_user_data); |
| 204 |
} |
| 205 |
|
| 206 |
aws_mem_release(client->allocator, client); |
| 207 |
} |
| 208 |
|
| 209 |
void aws_imds_client_release(struct aws_imds_client *client) { |
| 210 |
if (!client) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
size_t old_value = aws_atomic_fetch_sub(&client->ref_count, 1); |
| 215 |
if (old_value == 1) { |
| 216 |
s_aws_imds_client_destroy(client); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void aws_imds_client_acquire(struct aws_imds_client *client) { |
| 221 |
aws_atomic_fetch_add(&client->ref_count, 1); |
| 222 |
} |
| 223 |
|
| 224 |
struct aws_imds_client *aws_imds_client_new( |
| 225 |
struct aws_allocator *allocator, |
| 226 |
const struct aws_imds_client_options *options) { |
| 227 |
|
| 228 |
if (!options->bootstrap) { |
| 229 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Client bootstrap is required for querying IMDS"); |
| 230 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
|
| 234 |
struct aws_imds_client *client = aws_mem_calloc(allocator, 1, sizeof(struct aws_imds_client)); |
| 235 |
if (!client) { |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
|
| 239 |
struct aws_tls_connection_options tls_connection_options; |
| 240 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 241 |
if (aws_mutex_init(&client->token_lock)) { |
| 242 |
goto on_error; |
| 243 |
} |
| 244 |
|
| 245 |
if (aws_condition_variable_init(&client->token_signal)) { |
| 246 |
goto on_error; |
| 247 |
} |
| 248 |
|
| 249 |
if (aws_byte_buf_init(&client->cached_token, allocator, IMDS_RESPONSE_TOKEN_SIZE_INITIAL)) { |
| 250 |
goto on_error; |
| 251 |
} |
| 252 |
|
| 253 |
aws_linked_list_init(&client->pending_queries); |
| 254 |
|
| 255 |
aws_atomic_store_int(&client->ref_count, 1); |
| 256 |
client->allocator = allocator; |
| 257 |
client->function_table = |
| 258 |
options->function_table ? options->function_table : g_aws_credentials_provider_http_function_table; |
| 259 |
client->token_required = options->imds_version == IMDS_PROTOCOL_V1 ? false : true; |
| 260 |
client->ec2_metadata_v1_disabled = options->ec2_metadata_v1_disabled; |
| 261 |
client->shutdown_options = options->shutdown_options; |
| 262 |
|
| 263 |
|
| 264 |
enum aws_socket_domain socket_domain; |
| 265 |
|
| 266 |
s_resolve_imds_endpoint(client, options, &socket_domain); |
| 267 |
|
| 268 |
struct aws_socket_options socket_options; |
| 269 |
AWS_ZERO_STRUCT(socket_options); |
| 270 |
socket_options.type = AWS_SOCKET_STREAM; |
| 271 |
socket_options.domain = socket_domain; |
| 272 |
socket_options.connect_timeout_ms = (uint32_t)aws_timestamp_convert( |
| 273 |
IMDS_CONNECT_TIMEOUT_DEFAULT_IN_SECONDS, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_MILLIS, NULL); |
| 274 |
|
| 275 |
struct aws_http_connection_manager_options manager_options; |
| 276 |
AWS_ZERO_STRUCT(manager_options); |
| 277 |
manager_options.bootstrap = options->bootstrap; |
| 278 |
manager_options.initial_window_size = IMDS_RESPONSE_SIZE_LIMIT; |
| 279 |
manager_options.socket_options = &socket_options; |
| 280 |
manager_options.tls_connection_options = NULL; |
| 281 |
manager_options.host = aws_byte_cursor_from_string(client->endpoint_host); |
| 282 |
manager_options.port = client->endpoint_port; |
| 283 |
manager_options.max_connections = 10; |
| 284 |
manager_options.shutdown_complete_callback = s_on_connection_manager_shutdown; |
| 285 |
manager_options.shutdown_complete_user_data = client; |
| 286 |
manager_options.proxy_ev_settings = options->proxy_ev_settings; |
| 287 |
|
| 288 |
|
| 289 |
if (client->endpoint_uses_tls) { |
| 290 |
#ifdef BYO_CRYPTO |
| 291 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 292 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "TLS IMDS endpoint is not supported."); |
| 293 |
goto on_error; |
| 294 |
#else |
| 295 |
struct aws_tls_ctx_options tls_ctx_options; |
| 296 |
aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator); |
| 297 |
|
| 298 |
struct aws_tls_ctx *tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options); |
| 299 |
aws_tls_ctx_options_clean_up(&tls_ctx_options); |
| 300 |
|
| 301 |
if (!tls_ctx) { |
| 302 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to create TLS context for HTTPS endpoint"); |
| 303 |
goto on_error; |
| 304 |
} |
| 305 |
|
| 306 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, tls_ctx); |
| 307 |
aws_tls_ctx_release(tls_ctx); |
| 308 |
|
| 309 |
|
| 310 |
struct aws_byte_cursor server_name = aws_byte_cursor_from_string(client->endpoint_host); |
| 311 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &server_name)) { |
| 312 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to set SNI hostname for TLS connection"); |
| 313 |
goto on_error; |
| 314 |
} |
| 315 |
#endif |
| 316 |
} |
| 317 |
|
| 318 |
client->connection_manager = client->function_table->aws_http_connection_manager_new(allocator, &manager_options); |
| 319 |
if (!client->connection_manager) { |
| 320 |
goto on_error; |
| 321 |
} |
| 322 |
|
| 323 |
if (options->retry_strategy) { |
| 324 |
client->retry_strategy = options->retry_strategy; |
| 325 |
aws_retry_strategy_acquire(client->retry_strategy); |
| 326 |
} else { |
| 327 |
struct aws_exponential_backoff_retry_options retry_options = { |
| 328 |
.el_group = options->bootstrap->event_loop_group, |
| 329 |
.max_retries = IMDS_DEFAULT_RETRIES, |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
client->retry_strategy = aws_retry_strategy_new_exponential_backoff(allocator, &retry_options); |
| 334 |
} |
| 335 |
if (!client->retry_strategy) { |
| 336 |
goto on_error; |
| 337 |
} |
| 338 |
|
| 339 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 340 |
return client; |
| 341 |
|
| 342 |
on_error: |
| 343 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 344 |
s_aws_imds_client_destroy(client); |
| 345 |
return NULL; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
struct imds_user_data { |
| 352 |
|
| 353 |
struct aws_allocator *allocator; |
| 354 |
struct aws_imds_client *client; |
| 355 |
aws_imds_client_on_get_resource_callback_fn *original_callback; |
| 356 |
void *original_user_data; |
| 357 |
|
| 358 |
|
| 359 |
struct aws_http_connection *connection; |
| 360 |
struct aws_http_message *request; |
| 361 |
struct aws_byte_buf current_result; |
| 362 |
struct aws_byte_buf imds_token; |
| 363 |
struct aws_string *resource_path; |
| 364 |
struct aws_retry_token *retry_token; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
bool imds_token_required; |
| 370 |
|
| 371 |
bool is_fallback_request; |
| 372 |
bool is_imds_token_request; |
| 373 |
bool ec2_metadata_v1_disabled; |
| 374 |
int status_code; |
| 375 |
int error_code; |
| 376 |
|
| 377 |
struct aws_atomic_var ref_count; |
| 378 |
}; |
| 379 |
|
| 380 |
static void s_user_data_destroy(struct imds_user_data *user_data) { |
| 381 |
if (user_data == NULL) { |
| 382 |
return; |
| 383 |
} |
| 384 |
struct aws_imds_client *client = user_data->client; |
| 385 |
|
| 386 |
if (user_data->connection) { |
| 387 |
client->function_table->aws_http_connection_manager_release_connection( |
| 388 |
client->connection_manager, user_data->connection); |
| 389 |
} |
| 390 |
|
| 391 |
aws_byte_buf_clean_up(&user_data->current_result); |
| 392 |
aws_byte_buf_clean_up(&user_data->imds_token); |
| 393 |
aws_string_destroy(user_data->resource_path); |
| 394 |
|
| 395 |
if (user_data->request) { |
| 396 |
aws_http_message_destroy(user_data->request); |
| 397 |
} |
| 398 |
aws_retry_token_release(user_data->retry_token); |
| 399 |
aws_imds_client_release(client); |
| 400 |
aws_mem_release(user_data->allocator, user_data); |
| 401 |
} |
| 402 |
|
| 403 |
static struct imds_user_data *s_user_data_new( |
| 404 |
struct aws_imds_client *client, |
| 405 |
struct aws_byte_cursor resource_path, |
| 406 |
aws_imds_client_on_get_resource_callback_fn *callback, |
| 407 |
void *user_data) { |
| 408 |
|
| 409 |
struct imds_user_data *wrapped_user_data = aws_mem_calloc(client->allocator, 1, sizeof(struct imds_user_data)); |
| 410 |
if (!wrapped_user_data) { |
| 411 |
goto on_error; |
| 412 |
} |
| 413 |
|
| 414 |
wrapped_user_data->allocator = client->allocator; |
| 415 |
wrapped_user_data->client = client; |
| 416 |
aws_imds_client_acquire(client); |
| 417 |
wrapped_user_data->original_user_data = user_data; |
| 418 |
wrapped_user_data->original_callback = callback; |
| 419 |
|
| 420 |
if (aws_byte_buf_init(&wrapped_user_data->current_result, client->allocator, IMDS_RESPONSE_SIZE_INITIAL)) { |
| 421 |
goto on_error; |
| 422 |
} |
| 423 |
|
| 424 |
if (aws_byte_buf_init(&wrapped_user_data->imds_token, client->allocator, IMDS_RESPONSE_TOKEN_SIZE_INITIAL)) { |
| 425 |
goto on_error; |
| 426 |
} |
| 427 |
|
| 428 |
wrapped_user_data->resource_path = |
| 429 |
aws_string_new_from_array(client->allocator, resource_path.ptr, resource_path.len); |
| 430 |
if (!wrapped_user_data->resource_path) { |
| 431 |
goto on_error; |
| 432 |
} |
| 433 |
|
| 434 |
wrapped_user_data->imds_token_required = client->token_required; |
| 435 |
wrapped_user_data->ec2_metadata_v1_disabled = client->ec2_metadata_v1_disabled; |
| 436 |
aws_atomic_store_int(&wrapped_user_data->ref_count, 1); |
| 437 |
return wrapped_user_data; |
| 438 |
|
| 439 |
on_error: |
| 440 |
s_user_data_destroy(wrapped_user_data); |
| 441 |
return NULL; |
| 442 |
} |
| 443 |
|
| 444 |
static void s_user_data_acquire(struct imds_user_data *user_data) { |
| 445 |
if (user_data == NULL) { |
| 446 |
return; |
| 447 |
} |
| 448 |
aws_atomic_fetch_add(&user_data->ref_count, 1); |
| 449 |
} |
| 450 |
|
| 451 |
static void s_user_data_release(struct imds_user_data *user_data) { |
| 452 |
if (!user_data) { |
| 453 |
return; |
| 454 |
} |
| 455 |
size_t old_value = aws_atomic_fetch_sub(&user_data->ref_count, 1); |
| 456 |
if (old_value == 1) { |
| 457 |
s_user_data_destroy(user_data); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
static void s_reset_scratch_user_data(struct imds_user_data *user_data) { |
| 462 |
user_data->current_result.len = 0; |
| 463 |
user_data->status_code = 0; |
| 464 |
|
| 465 |
if (user_data->request) { |
| 466 |
aws_http_message_destroy(user_data->request); |
| 467 |
user_data->request = NULL; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
static enum imds_token_copy_result s_copy_token_safely(struct imds_user_data *user_data); |
| 472 |
static void s_update_token_safely( |
| 473 |
struct aws_imds_client *client, |
| 474 |
struct aws_byte_buf *token, |
| 475 |
bool token_required, |
| 476 |
uint64_t expire_timestamp); |
| 477 |
static void s_query_complete(struct imds_user_data *user_data); |
| 478 |
static void s_on_acquire_connection(struct aws_http_connection *connection, int error_code, void *user_data); |
| 479 |
static void s_on_retry_token_acquired(struct aws_retry_strategy *, int, struct aws_retry_token *, void *); |
| 480 |
|
| 481 |
static int s_on_incoming_body_fn(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) { |
| 482 |
(void)stream; |
| 483 |
(void)data; |
| 484 |
|
| 485 |
struct imds_user_data *imds_user_data = user_data; |
| 486 |
struct aws_imds_client *client = imds_user_data->client; |
| 487 |
|
| 488 |
if (data->len + imds_user_data->current_result.len > IMDS_RESPONSE_SIZE_LIMIT) { |
| 489 |
client->function_table->aws_http_connection_close(imds_user_data->connection); |
| 490 |
AWS_LOGF_ERROR( |
| 491 |
AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client query response exceeded maximum allowed length", (void *)client); |
| 492 |
|
| 493 |
return aws_raise_error(AWS_AUTH_IMDS_CLIENT_SOURCE_FAILURE); |
| 494 |
} |
| 495 |
|
| 496 |
if (aws_byte_buf_append_dynamic(&imds_user_data->current_result, data)) { |
| 497 |
client->function_table->aws_http_connection_close(imds_user_data->connection); |
| 498 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client query error appending response", (void *)client); |
| 499 |
|
| 500 |
return AWS_OP_ERR; |
| 501 |
} |
| 502 |
|
| 503 |
return AWS_OP_SUCCESS; |
| 504 |
} |
| 505 |
|
| 506 |
static int s_on_incoming_headers_fn( |
| 507 |
struct aws_http_stream *stream, |
| 508 |
enum aws_http_header_block header_block, |
| 509 |
const struct aws_http_header *header_array, |
| 510 |
size_t num_headers, |
| 511 |
void *user_data) { |
| 512 |
|
| 513 |
(void)header_array; |
| 514 |
(void)num_headers; |
| 515 |
|
| 516 |
if (header_block != AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 517 |
return AWS_OP_SUCCESS; |
| 518 |
} |
| 519 |
|
| 520 |
struct imds_user_data *imds_user_data = user_data; |
| 521 |
struct aws_imds_client *client = imds_user_data->client; |
| 522 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 523 |
if (imds_user_data->status_code == 0) { |
| 524 |
if (client->function_table->aws_http_stream_get_incoming_response_status( |
| 525 |
stream, &imds_user_data->status_code)) { |
| 526 |
AWS_LOGF_ERROR( |
| 527 |
AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client failed to get http status code", (void *)client); |
| 528 |
return AWS_OP_ERR; |
| 529 |
} |
| 530 |
AWS_LOGF_DEBUG( |
| 531 |
AWS_LS_IMDS_CLIENT, |
| 532 |
"(id=%p) IMDS client query received http status code %d for requester %p.", |
| 533 |
(void *)client, |
| 534 |
imds_user_data->status_code, |
| 535 |
user_data); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
return AWS_OP_SUCCESS; |
| 540 |
} |
| 541 |
|
| 542 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_host_header, "Host"); |
| 543 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_accept_header, "Accept"); |
| 544 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_accept_header_value, "*/*"); |
| 545 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_user_agent_header, "User-Agent"); |
| 546 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_user_agent_header_value, "aws-sdk-crt/aws-imds-client"); |
| 547 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_h1_0_keep_alive_header, "Connection"); |
| 548 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_h1_0_keep_alive_header_value, "keep-alive"); |
| 549 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_token_resource_path, "/latest/api/token"); |
| 550 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_token_ttl_header, "x-aws-ec2-metadata-token-ttl-seconds"); |
| 551 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_token_header, "x-aws-ec2-metadata-token"); |
| 552 |
AWS_STATIC_STRING_FROM_LITERAL(s_imds_token_ttl_default_value, "21600"); |
| 553 |
|
| 554 |
static const uint64_t s_imds_token_ttl_secs = 21595; |
| 555 |
|
| 556 |
static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data); |
| 557 |
|
| 558 |
static int s_make_imds_http_query( |
| 559 |
struct imds_user_data *user_data, |
| 560 |
const struct aws_byte_cursor *verb, |
| 561 |
const struct aws_byte_cursor *uri, |
| 562 |
const struct aws_http_header *headers, |
| 563 |
size_t header_count) { |
| 564 |
|
| 565 |
AWS_FATAL_ASSERT(user_data->connection); |
| 566 |
struct aws_imds_client *client = user_data->client; |
| 567 |
struct aws_http_stream *stream = NULL; |
| 568 |
struct aws_http_message *request = aws_http_message_new_request(user_data->allocator); |
| 569 |
|
| 570 |
if (request == NULL) { |
| 571 |
return AWS_OP_ERR; |
| 572 |
} |
| 573 |
|
| 574 |
if (headers && aws_http_message_add_header_array(request, headers, header_count)) { |
| 575 |
goto on_error; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
struct aws_http_header host_header = { |
| 580 |
.name = aws_byte_cursor_from_string(s_imds_host_header), |
| 581 |
.value = aws_byte_cursor_from_string(client->endpoint_host), |
| 582 |
}; |
| 583 |
if (aws_http_message_add_header(request, host_header)) { |
| 584 |
goto on_error; |
| 585 |
} |
| 586 |
|
| 587 |
struct aws_http_header accept_header = { |
| 588 |
.name = aws_byte_cursor_from_string(s_imds_accept_header), |
| 589 |
.value = aws_byte_cursor_from_string(s_imds_accept_header_value), |
| 590 |
}; |
| 591 |
if (aws_http_message_add_header(request, accept_header)) { |
| 592 |
goto on_error; |
| 593 |
} |
| 594 |
|
| 595 |
struct aws_http_header user_agent_header = { |
| 596 |
.name = aws_byte_cursor_from_string(s_imds_user_agent_header), |
| 597 |
.value = aws_byte_cursor_from_string(s_imds_user_agent_header_value), |
| 598 |
}; |
| 599 |
if (aws_http_message_add_header(request, user_agent_header)) { |
| 600 |
goto on_error; |
| 601 |
} |
| 602 |
|
| 603 |
struct aws_http_header keep_alive_header = { |
| 604 |
.name = aws_byte_cursor_from_string(s_imds_h1_0_keep_alive_header), |
| 605 |
.value = aws_byte_cursor_from_string(s_imds_h1_0_keep_alive_header_value), |
| 606 |
}; |
| 607 |
if (aws_http_message_add_header(request, keep_alive_header)) { |
| 608 |
goto on_error; |
| 609 |
} |
| 610 |
|
| 611 |
if (aws_http_message_set_request_method(request, *verb)) { |
| 612 |
goto on_error; |
| 613 |
} |
| 614 |
|
| 615 |
if (aws_http_message_set_request_path(request, *uri)) { |
| 616 |
goto on_error; |
| 617 |
} |
| 618 |
|
| 619 |
user_data->request = request; |
| 620 |
|
| 621 |
struct aws_http_make_request_options request_options = { |
| 622 |
.self_size = sizeof(request_options), |
| 623 |
.on_response_headers = s_on_incoming_headers_fn, |
| 624 |
.on_response_header_block_done = NULL, |
| 625 |
.on_response_body = s_on_incoming_body_fn, |
| 626 |
.on_complete = s_on_stream_complete_fn, |
| 627 |
.response_first_byte_timeout_ms = 1000, |
| 628 |
.user_data = user_data, |
| 629 |
.request = request, |
| 630 |
}; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
s_user_data_acquire(user_data); |
| 635 |
stream = client->function_table->aws_http_connection_make_request(user_data->connection, &request_options); |
| 636 |
if (!stream || client->function_table->aws_http_stream_activate(stream)) { |
| 637 |
goto on_error; |
| 638 |
} |
| 639 |
s_user_data_release(user_data); |
| 640 |
|
| 641 |
return AWS_OP_SUCCESS; |
| 642 |
|
| 643 |
on_error: |
| 644 |
user_data->client->function_table->aws_http_stream_release(stream); |
| 645 |
aws_http_message_destroy(request); |
| 646 |
user_data->request = NULL; |
| 647 |
s_user_data_release(user_data); |
| 648 |
return AWS_OP_ERR; |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
static void s_client_on_token_response(struct imds_user_data *user_data) { |
| 655 |
|
| 656 |
if (user_data->status_code == AWS_HTTP_STATUS_CODE_400_BAD_REQUEST) { |
| 657 |
s_update_token_safely(user_data->client, NULL, true, 0 ); |
| 658 |
return; |
| 659 |
} |
| 660 |
|
| 661 |
if (user_data->status_code == AWS_HTTP_STATUS_CODE_200_OK && user_data->current_result.len != 0) { |
| 662 |
AWS_LOGF_DEBUG(AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client has fetched the token", (void *)user_data->client); |
| 663 |
|
| 664 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(&(user_data->current_result)); |
| 665 |
aws_byte_cursor_trim_pred(&cursor, aws_char_is_space); |
| 666 |
aws_byte_buf_reset(&user_data->imds_token, true ); |
| 667 |
if (aws_byte_buf_append_and_update(&user_data->imds_token, &cursor)) { |
| 668 |
s_update_token_safely(user_data->client, NULL , true , 0 ); |
| 669 |
return; |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
uint64_t current = 0; |
| 674 |
user_data->client->function_table->aws_high_res_clock_get_ticks(¤t); |
| 675 |
uint64_t expire_timestamp = aws_add_u64_saturating( |
| 676 |
current, aws_timestamp_convert(s_imds_token_ttl_secs, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL)); |
| 677 |
|
| 678 |
AWS_ASSERT(cursor.len != 0); |
| 679 |
s_update_token_safely(user_data->client, &user_data->imds_token, true , expire_timestamp); |
| 680 |
} else if (user_data->ec2_metadata_v1_disabled) { |
| 681 |
AWS_LOGF_DEBUG( |
| 682 |
AWS_LS_IMDS_CLIENT, |
| 683 |
"(id=%p) IMDS client failed to fetch token for requester %p, and fall back to v1 is disabled." |
| 684 |
"Received response status code: %d", |
| 685 |
(void *)user_data->client, |
| 686 |
(void *)user_data, |
| 687 |
user_data->status_code); |
| 688 |
s_update_token_safely(user_data->client, NULL , true , 0 ); |
| 689 |
} else { |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
AWS_LOGF_DEBUG( |
| 695 |
AWS_LS_IMDS_CLIENT, |
| 696 |
"(id=%p) IMDS client failed to fetch token for requester %p, fall back to v1 for the same " |
| 697 |
"requester. Received response status code: %d", |
| 698 |
(void *)user_data->client, |
| 699 |
(void *)user_data, |
| 700 |
user_data->status_code); |
| 701 |
s_update_token_safely(user_data->client, NULL , false , 0 ); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
static int s_client_start_query_token(struct aws_imds_client *client) { |
| 706 |
struct imds_user_data *user_data = s_user_data_new(client, aws_byte_cursor_from_c_str(""), NULL, (void *)client); |
| 707 |
if (!user_data) { |
| 708 |
AWS_LOGF_ERROR( |
| 709 |
AWS_LS_IMDS_CLIENT, |
| 710 |
"(id=%p) IMDS client failed to query token with error: %s.", |
| 711 |
(void *)client, |
| 712 |
aws_error_str(aws_last_error())); |
| 713 |
return AWS_OP_ERR; |
| 714 |
} |
| 715 |
user_data->is_imds_token_request = true; |
| 716 |
if (aws_retry_strategy_acquire_retry_token( |
| 717 |
client->retry_strategy, NULL, s_on_retry_token_acquired, user_data, 100)) { |
| 718 |
s_user_data_release(user_data); |
| 719 |
return AWS_OP_ERR; |
| 720 |
} |
| 721 |
|
| 722 |
return AWS_OP_SUCCESS; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
static void s_client_do_query_token(struct imds_user_data *user_data) { |
| 727 |
|
| 728 |
struct aws_byte_cursor uri = aws_byte_cursor_from_string(s_imds_token_resource_path); |
| 729 |
|
| 730 |
|
| 731 |
struct aws_http_header token_ttl_header = { |
| 732 |
.name = aws_byte_cursor_from_string(s_imds_token_ttl_header), |
| 733 |
.value = aws_byte_cursor_from_string(s_imds_token_ttl_default_value), |
| 734 |
}; |
| 735 |
|
| 736 |
struct aws_http_header headers[] = { |
| 737 |
token_ttl_header, |
| 738 |
}; |
| 739 |
|
| 740 |
struct aws_byte_cursor verb = aws_byte_cursor_from_c_str("PUT"); |
| 741 |
|
| 742 |
if (s_make_imds_http_query(user_data, &verb, &uri, headers, AWS_ARRAY_SIZE(headers))) { |
| 743 |
user_data->error_code = aws_last_error(); |
| 744 |
if (user_data->error_code == AWS_ERROR_SUCCESS) { |
| 745 |
user_data->error_code = AWS_ERROR_UNKNOWN; |
| 746 |
} |
| 747 |
s_query_complete(user_data); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
static void s_do_query_resource(struct imds_user_data *user_data) { |
| 755 |
|
| 756 |
struct aws_http_header token_header = { |
| 757 |
.name = aws_byte_cursor_from_string(s_imds_token_header), |
| 758 |
.value = aws_byte_cursor_from_buf(&user_data->imds_token), |
| 759 |
}; |
| 760 |
|
| 761 |
struct aws_http_header headers[] = { |
| 762 |
token_header, |
| 763 |
}; |
| 764 |
|
| 765 |
size_t headers_count = 0; |
| 766 |
struct aws_http_header *headers_array_ptr = NULL; |
| 767 |
|
| 768 |
if (user_data->imds_token_required) { |
| 769 |
headers_count = 1; |
| 770 |
headers_array_ptr = headers; |
| 771 |
} |
| 772 |
|
| 773 |
struct aws_byte_cursor verb = aws_byte_cursor_from_c_str("GET"); |
| 774 |
|
| 775 |
struct aws_byte_cursor path_cursor = aws_byte_cursor_from_string(user_data->resource_path); |
| 776 |
if (s_make_imds_http_query(user_data, &verb, &path_cursor, headers_array_ptr, headers_count)) { |
| 777 |
user_data->error_code = aws_last_error(); |
| 778 |
if (user_data->error_code == AWS_ERROR_SUCCESS) { |
| 779 |
user_data->error_code = AWS_ERROR_UNKNOWN; |
| 780 |
} |
| 781 |
s_query_complete(user_data); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
int s_get_resource_async_with_imds_token(struct imds_user_data *user_data); |
| 786 |
|
| 787 |
static void s_query_complete(struct imds_user_data *user_data) { |
| 788 |
if (user_data->is_imds_token_request) { |
| 789 |
s_client_on_token_response(user_data); |
| 790 |
s_user_data_release(user_data); |
| 791 |
return; |
| 792 |
} |
| 793 |
|
| 794 |
if (user_data->status_code == AWS_HTTP_STATUS_CODE_401_UNAUTHORIZED) { |
| 795 |
struct aws_imds_client *client = user_data->client; |
| 796 |
aws_mutex_lock(&client->token_lock); |
| 797 |
if (aws_byte_buf_eq(&user_data->imds_token, &client->cached_token)) { |
| 798 |
|
| 799 |
client->token_state = AWS_IMDS_TS_INVALID; |
| 800 |
AWS_LOGF_DEBUG( |
| 801 |
AWS_LS_IMDS_CLIENT, |
| 802 |
"(id=%p) IMDS client's cached token is invalidated by requester %p.", |
| 803 |
(void *)client, |
| 804 |
(void *)user_data); |
| 805 |
} |
| 806 |
|
| 807 |
client->token_required = true; |
| 808 |
aws_mutex_unlock(&client->token_lock); |
| 809 |
|
| 810 |
if (!user_data->imds_token_required && !user_data->is_fallback_request) { |
| 811 |
AWS_LOGF_DEBUG( |
| 812 |
AWS_LS_IMDS_CLIENT, |
| 813 |
"(id=%p) IMDS client failed to fetch resource via V1, try to use V2. requester %p.", |
| 814 |
(void *)user_data->client, |
| 815 |
(void *)user_data); |
| 816 |
|
| 817 |
s_reset_scratch_user_data(user_data); |
| 818 |
user_data->is_fallback_request = true; |
| 819 |
aws_retry_token_release(user_data->retry_token); |
| 820 |
|
| 821 |
if (s_get_resource_async_with_imds_token(user_data)) { |
| 822 |
s_user_data_release(user_data); |
| 823 |
} |
| 824 |
return; |
| 825 |
} else { |
| 826 |
|
| 827 |
AWS_LOGF_ERROR( |
| 828 |
AWS_LS_IMDS_CLIENT, |
| 829 |
"(id=%p) IMDS client failed to fetch resource. Server response 401 UNAUTHORIZED. requester %p.", |
| 830 |
(void *)user_data->client, |
| 831 |
(void *)user_data); |
| 832 |
user_data->error_code = AWS_AUTH_IMDS_CLIENT_SOURCE_FAILURE; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
user_data->original_callback( |
| 839 |
user_data->error_code ? NULL : &user_data->current_result, |
| 840 |
user_data->error_code, |
| 841 |
user_data->original_user_data); |
| 842 |
|
| 843 |
s_user_data_release(user_data); |
| 844 |
} |
| 845 |
|
| 846 |
static void s_on_acquire_connection(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 847 |
struct imds_user_data *imds_user_data = user_data; |
| 848 |
imds_user_data->connection = connection; |
| 849 |
|
| 850 |
if (!connection) { |
| 851 |
AWS_LOGF_WARN( |
| 852 |
AWS_LS_IMDS_CLIENT, |
| 853 |
"id=%p: IMDS Client failed to acquire a connection, error code %d(%s)", |
| 854 |
(void *)imds_user_data->client, |
| 855 |
error_code, |
| 856 |
aws_error_str(error_code)); |
| 857 |
imds_user_data->error_code = error_code; |
| 858 |
s_query_complete(imds_user_data); |
| 859 |
return; |
| 860 |
} |
| 861 |
|
| 862 |
if (imds_user_data->is_imds_token_request) { |
| 863 |
s_client_do_query_token(imds_user_data); |
| 864 |
} else { |
| 865 |
s_do_query_resource(imds_user_data); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
static void s_on_retry_ready(struct aws_retry_token *token, int error_code, void *user_data) { |
| 870 |
(void)token; |
| 871 |
|
| 872 |
struct imds_user_data *imds_user_data = user_data; |
| 873 |
struct aws_imds_client *client = imds_user_data->client; |
| 874 |
|
| 875 |
if (!error_code) { |
| 876 |
client->function_table->aws_http_connection_manager_acquire_connection( |
| 877 |
client->connection_manager, s_on_acquire_connection, user_data); |
| 878 |
} else { |
| 879 |
AWS_LOGF_WARN( |
| 880 |
AWS_LS_IMDS_CLIENT, |
| 881 |
"id=%p: IMDS Client failed to retry the request with error code %d(%s)", |
| 882 |
(void *)client, |
| 883 |
error_code, |
| 884 |
aws_error_str(error_code)); |
| 885 |
imds_user_data->error_code = error_code; |
| 886 |
s_query_complete(imds_user_data); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 891 |
struct imds_user_data *imds_user_data = user_data; |
| 892 |
struct aws_imds_client *client = imds_user_data->client; |
| 893 |
|
| 894 |
aws_http_message_destroy(imds_user_data->request); |
| 895 |
imds_user_data->request = NULL; |
| 896 |
imds_user_data->connection = NULL; |
| 897 |
|
| 898 |
struct aws_http_connection *connection = client->function_table->aws_http_stream_get_connection(stream); |
| 899 |
client->function_table->aws_http_stream_release(stream); |
| 900 |
client->function_table->aws_http_connection_manager_release_connection(client->connection_manager, connection); |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
if (error_code) { |
| 905 |
AWS_LOGF_WARN( |
| 906 |
AWS_LS_IMDS_CLIENT, |
| 907 |
"id=%p: Stream completed with error code %d(%s)", |
| 908 |
(void *)client, |
| 909 |
error_code, |
| 910 |
aws_error_str(error_code)); |
| 911 |
|
| 912 |
if (!aws_retry_strategy_schedule_retry( |
| 913 |
imds_user_data->retry_token, AWS_RETRY_ERROR_TYPE_TRANSIENT, s_on_retry_ready, user_data)) { |
| 914 |
AWS_LOGF_DEBUG( |
| 915 |
AWS_LS_IMDS_CLIENT, |
| 916 |
"id=%p: Stream completed, retrying the last request on a new connection.", |
| 917 |
(void *)client); |
| 918 |
return; |
| 919 |
} else { |
| 920 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "id=%p: Stream completed, retries have been exhausted.", (void *)client); |
| 921 |
imds_user_data->error_code = error_code; |
| 922 |
} |
| 923 |
} else if (aws_retry_token_record_success(imds_user_data->retry_token)) { |
| 924 |
AWS_LOGF_ERROR( |
| 925 |
AWS_LS_IMDS_CLIENT, |
| 926 |
"id=%p: Error while recording successful retry: %s", |
| 927 |
(void *)client, |
| 928 |
aws_error_str(aws_last_error())); |
| 929 |
} |
| 930 |
|
| 931 |
s_query_complete(imds_user_data); |
| 932 |
} |
| 933 |
|
| 934 |
static void s_on_retry_token_acquired( |
| 935 |
struct aws_retry_strategy *strategy, |
| 936 |
int error_code, |
| 937 |
struct aws_retry_token *token, |
| 938 |
void *user_data) { |
| 939 |
(void)strategy; |
| 940 |
|
| 941 |
struct imds_user_data *imds_user_data = user_data; |
| 942 |
struct aws_imds_client *client = imds_user_data->client; |
| 943 |
|
| 944 |
if (!error_code) { |
| 945 |
AWS_LOGF_DEBUG(AWS_LS_IMDS_CLIENT, "id=%p: IMDS Client successfully acquired retry token.", (void *)client); |
| 946 |
imds_user_data->retry_token = token; |
| 947 |
client->function_table->aws_http_connection_manager_acquire_connection( |
| 948 |
client->connection_manager, s_on_acquire_connection, imds_user_data); |
| 949 |
} else { |
| 950 |
AWS_LOGF_WARN( |
| 951 |
AWS_LS_IMDS_CLIENT, |
| 952 |
"id=%p: IMDS Client failed to acquire retry token, error code %d(%s)", |
| 953 |
(void *)client, |
| 954 |
error_code, |
| 955 |
aws_error_str(error_code)); |
| 956 |
imds_user_data->error_code = error_code; |
| 957 |
s_query_complete(imds_user_data); |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
static void s_complete_pending_queries( |
| 962 |
struct aws_imds_client *client, |
| 963 |
struct aws_linked_list *queries, |
| 964 |
bool token_required, |
| 965 |
struct aws_byte_buf *token) { |
| 966 |
|
| 967 |
|
| 968 |
while (!aws_linked_list_empty(queries)) { |
| 969 |
struct aws_linked_list_node *node = aws_linked_list_pop_back(queries); |
| 970 |
struct imds_token_query *query = AWS_CONTAINER_OF(node, struct imds_token_query, node); |
| 971 |
struct imds_user_data *requester = query->user_data; |
| 972 |
aws_mem_release(client->allocator, query); |
| 973 |
|
| 974 |
bool should_continue = true; |
| 975 |
if (requester->imds_token_required && !token_required) { |
| 976 |
if (requester->is_fallback_request) { |
| 977 |
AWS_LOGF_ERROR( |
| 978 |
AWS_LS_IMDS_CLIENT, |
| 979 |
"(id=%p) IMDS client failed to fetch resource without token, and also failed to fetch token. " |
| 980 |
"requester %p.", |
| 981 |
(void *)requester->client, |
| 982 |
(void *)requester); |
| 983 |
requester->error_code = AWS_AUTH_IMDS_CLIENT_SOURCE_FAILURE; |
| 984 |
should_continue = false; |
| 985 |
} else { |
| 986 |
AWS_LOGF_DEBUG( |
| 987 |
AWS_LS_IMDS_CLIENT, |
| 988 |
"(id=%p) IMDS client failed to fetch token, fallback to v1. requester %p.", |
| 989 |
(void *)requester->client, |
| 990 |
(void *)requester); |
| 991 |
requester->is_fallback_request = true; |
| 992 |
} |
| 993 |
} |
| 994 |
requester->imds_token_required = token_required; |
| 995 |
if (token) { |
| 996 |
aws_byte_buf_reset(&requester->imds_token, true); |
| 997 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(token); |
| 998 |
if (aws_byte_buf_append_dynamic(&requester->imds_token, &cursor)) { |
| 999 |
AWS_LOGF_ERROR( |
| 1000 |
AWS_LS_IMDS_CLIENT, |
| 1001 |
"(id=%p) IMDS client failed to copy IMDS token for requester %p.", |
| 1002 |
(void *)client, |
| 1003 |
(void *)requester); |
| 1004 |
should_continue = false; |
| 1005 |
} |
| 1006 |
} else if (token_required) { |
| 1007 |
requester->error_code = AWS_AUTH_IMDS_CLIENT_SOURCE_FAILURE; |
| 1008 |
should_continue = false; |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (should_continue && aws_retry_strategy_acquire_retry_token( |
| 1012 |
client->retry_strategy, NULL, s_on_retry_token_acquired, requester, 100)) { |
| 1013 |
AWS_LOGF_ERROR( |
| 1014 |
AWS_LS_IMDS_CLIENT, |
| 1015 |
"(id=%p) IMDS client failed to allocate retry token for requester %p to send resource request.", |
| 1016 |
(void *)client, |
| 1017 |
(void *)requester); |
| 1018 |
should_continue = false; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if (!should_continue) { |
| 1022 |
if (requester->error_code == AWS_ERROR_SUCCESS) { |
| 1023 |
requester->error_code = aws_last_error() == AWS_ERROR_SUCCESS ? AWS_ERROR_UNKNOWN : aws_last_error(); |
| 1024 |
} |
| 1025 |
s_query_complete(requester); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
} |
| 1029 |
|
| 1030 |
static enum imds_token_copy_result s_copy_token_safely(struct imds_user_data *user_data) { |
| 1031 |
struct aws_imds_client *client = user_data->client; |
| 1032 |
enum imds_token_copy_result ret = AWS_IMDS_TCR_UNEXPECTED_ERROR; |
| 1033 |
|
| 1034 |
struct aws_linked_list pending_queries; |
| 1035 |
aws_linked_list_init(&pending_queries); |
| 1036 |
uint64_t current = 0; |
| 1037 |
user_data->client->function_table->aws_high_res_clock_get_ticks(¤t); |
| 1038 |
|
| 1039 |
aws_mutex_lock(&client->token_lock); |
| 1040 |
if (client->token_state == AWS_IMDS_TS_VALID) { |
| 1041 |
if (current > client->cached_token_expiration_timestamp) { |
| 1042 |
|
| 1043 |
client->token_state = AWS_IMDS_TS_INVALID; |
| 1044 |
AWS_LOGF_DEBUG( |
| 1045 |
AWS_LS_IMDS_CLIENT, |
| 1046 |
"(id=%p) IMDS client's cached token expired. Fetching new token for requester %p.", |
| 1047 |
(void *)client, |
| 1048 |
(void *)user_data); |
| 1049 |
} else { |
| 1050 |
aws_byte_buf_reset(&user_data->imds_token, true); |
| 1051 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(&client->cached_token); |
| 1052 |
if (aws_byte_buf_append_dynamic(&user_data->imds_token, &cursor)) { |
| 1053 |
ret = AWS_IMDS_TCR_UNEXPECTED_ERROR; |
| 1054 |
} else { |
| 1055 |
ret = AWS_IMDS_TCR_SUCCESS; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
if (client->token_state != AWS_IMDS_TS_VALID) { |
| 1061 |
ret = AWS_IMDS_TCR_WAITING_IN_QUEUE; |
| 1062 |
struct imds_token_query *query = aws_mem_calloc(client->allocator, 1, sizeof(struct imds_token_query)); |
| 1063 |
query->user_data = user_data; |
| 1064 |
aws_linked_list_push_back(&client->pending_queries, &query->node); |
| 1065 |
|
| 1066 |
if (client->token_state == AWS_IMDS_TS_INVALID) { |
| 1067 |
if (s_client_start_query_token(client)) { |
| 1068 |
ret = AWS_IMDS_TCR_UNEXPECTED_ERROR; |
| 1069 |
aws_linked_list_swap_contents(&pending_queries, &client->pending_queries); |
| 1070 |
} else { |
| 1071 |
client->token_state = AWS_IMDS_TS_UPDATE_IN_PROGRESS; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
aws_mutex_unlock(&client->token_lock); |
| 1076 |
|
| 1077 |
s_complete_pending_queries(client, &pending_queries, true, NULL); |
| 1078 |
|
| 1079 |
switch (ret) { |
| 1080 |
case AWS_IMDS_TCR_SUCCESS: |
| 1081 |
AWS_LOGF_DEBUG( |
| 1082 |
AWS_LS_IMDS_CLIENT, |
| 1083 |
"(id=%p) IMDS client copied token to requester %p successfully.", |
| 1084 |
(void *)client, |
| 1085 |
(void *)user_data); |
| 1086 |
break; |
| 1087 |
|
| 1088 |
case AWS_IMDS_TCR_WAITING_IN_QUEUE: |
| 1089 |
AWS_LOGF_DEBUG( |
| 1090 |
AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client's token is invalid and is now updating.", (void *)client); |
| 1091 |
break; |
| 1092 |
|
| 1093 |
case AWS_IMDS_TCR_UNEXPECTED_ERROR: |
| 1094 |
AWS_LOGF_DEBUG( |
| 1095 |
AWS_LS_IMDS_CLIENT, |
| 1096 |
"(id=%p) IMDS client encountered unexpected error when processing token query for requester %p, error: " |
| 1097 |
"%s.", |
| 1098 |
(void *)client, |
| 1099 |
(void *)user_data, |
| 1100 |
aws_error_str(aws_last_error())); |
| 1101 |
break; |
| 1102 |
} |
| 1103 |
return ret; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
static void s_update_token_safely( |
| 1111 |
struct aws_imds_client *client, |
| 1112 |
struct aws_byte_buf *token, |
| 1113 |
bool token_required, |
| 1114 |
uint64_t expire_timestamp) { |
| 1115 |
AWS_FATAL_ASSERT(client); |
| 1116 |
bool updated = false; |
| 1117 |
|
| 1118 |
struct aws_linked_list pending_queries; |
| 1119 |
aws_linked_list_init(&pending_queries); |
| 1120 |
|
| 1121 |
aws_mutex_lock(&client->token_lock); |
| 1122 |
client->token_required = token_required; |
| 1123 |
if (token) { |
| 1124 |
aws_byte_buf_reset(&client->cached_token, true); |
| 1125 |
struct aws_byte_cursor cursor = aws_byte_cursor_from_buf(token); |
| 1126 |
if (aws_byte_buf_append_dynamic(&client->cached_token, &cursor) == AWS_OP_SUCCESS) { |
| 1127 |
client->token_state = AWS_IMDS_TS_VALID; |
| 1128 |
client->cached_token_expiration_timestamp = expire_timestamp; |
| 1129 |
updated = true; |
| 1130 |
} |
| 1131 |
} else { |
| 1132 |
client->token_state = AWS_IMDS_TS_INVALID; |
| 1133 |
} |
| 1134 |
aws_linked_list_swap_contents(&pending_queries, &client->pending_queries); |
| 1135 |
aws_mutex_unlock(&client->token_lock); |
| 1136 |
|
| 1137 |
s_complete_pending_queries(client, &pending_queries, token_required, token); |
| 1138 |
|
| 1139 |
if (updated) { |
| 1140 |
AWS_LOGF_DEBUG( |
| 1141 |
AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client updated the cached token successfully.", (void *)client); |
| 1142 |
} else { |
| 1143 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "(id=%p) IMDS client failed to update the token from IMDS.", (void *)client); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
int s_get_resource_async_with_imds_token(struct imds_user_data *user_data) { |
| 1148 |
enum imds_token_copy_result res = s_copy_token_safely(user_data); |
| 1149 |
if (res == AWS_IMDS_TCR_UNEXPECTED_ERROR) { |
| 1150 |
return AWS_OP_ERR; |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (res == AWS_IMDS_TCR_WAITING_IN_QUEUE) { |
| 1154 |
return AWS_OP_SUCCESS; |
| 1155 |
} |
| 1156 |
|
| 1157 |
if (aws_retry_strategy_acquire_retry_token( |
| 1158 |
user_data->client->retry_strategy, NULL, s_on_retry_token_acquired, user_data, 100)) { |
| 1159 |
return AWS_OP_ERR; |
| 1160 |
} |
| 1161 |
return AWS_OP_SUCCESS; |
| 1162 |
} |
| 1163 |
|
| 1164 |
int aws_imds_client_get_resource_async( |
| 1165 |
struct aws_imds_client *client, |
| 1166 |
struct aws_byte_cursor resource_path, |
| 1167 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1168 |
void *user_data) { |
| 1169 |
|
| 1170 |
struct imds_user_data *wrapped_user_data = s_user_data_new(client, resource_path, callback, user_data); |
| 1171 |
if (wrapped_user_data == NULL) { |
| 1172 |
goto error; |
| 1173 |
} |
| 1174 |
|
| 1175 |
if (!wrapped_user_data->imds_token_required) { |
| 1176 |
if (aws_retry_strategy_acquire_retry_token( |
| 1177 |
client->retry_strategy, NULL, s_on_retry_token_acquired, wrapped_user_data, 100)) { |
| 1178 |
goto error; |
| 1179 |
} |
| 1180 |
} else if (s_get_resource_async_with_imds_token(wrapped_user_data)) { |
| 1181 |
goto error; |
| 1182 |
} |
| 1183 |
return AWS_OP_SUCCESS; |
| 1184 |
|
| 1185 |
error: |
| 1186 |
s_user_data_release(wrapped_user_data); |
| 1187 |
|
| 1188 |
return AWS_OP_ERR; |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
static struct aws_byte_cursor s_instance_identity_document = |
| 1203 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("instance-identity/document"); |
| 1204 |
static struct aws_byte_cursor s_instance_identity_signature = |
| 1205 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("instance-identity/signature"); |
| 1206 |
static struct aws_byte_cursor s_ec2_metadata_root = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/latest/meta-data"); |
| 1207 |
static struct aws_byte_cursor s_ec2_credentials_root = |
| 1208 |
AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/latest/meta-data/iam/security-credentials/"); |
| 1209 |
static struct aws_byte_cursor s_ec2_userdata_root = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/latest/user-data/"); |
| 1210 |
static struct aws_byte_cursor s_ec2_dynamicdata_root = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/latest/dynamic/"); |
| 1211 |
|
| 1212 |
struct imds_get_array_user_data { |
| 1213 |
struct aws_allocator *allocator; |
| 1214 |
aws_imds_client_on_get_array_callback_fn *callback; |
| 1215 |
void *user_data; |
| 1216 |
}; |
| 1217 |
|
| 1218 |
struct imds_get_credentials_user_data { |
| 1219 |
struct aws_allocator *allocator; |
| 1220 |
aws_imds_client_on_get_credentials_callback_fn *callback; |
| 1221 |
void *user_data; |
| 1222 |
}; |
| 1223 |
|
| 1224 |
struct imds_get_iam_user_data { |
| 1225 |
struct aws_allocator *allocator; |
| 1226 |
aws_imds_client_on_get_iam_profile_callback_fn *callback; |
| 1227 |
void *user_data; |
| 1228 |
}; |
| 1229 |
|
| 1230 |
struct imds_get_instance_user_data { |
| 1231 |
struct aws_allocator *allocator; |
| 1232 |
aws_imds_client_on_get_instance_info_callback_fn *callback; |
| 1233 |
void *user_data; |
| 1234 |
}; |
| 1235 |
|
| 1236 |
static void s_process_array_resource(const struct aws_byte_buf *resource, int error_code, void *user_data) { |
| 1237 |
struct imds_get_array_user_data *wrapped_user_data = user_data; |
| 1238 |
struct aws_array_list resource_array; |
| 1239 |
AWS_ZERO_STRUCT(resource_array); |
| 1240 |
|
| 1241 |
if (resource && !error_code) { |
| 1242 |
struct aws_byte_cursor resource_cursor = aws_byte_cursor_from_buf(resource); |
| 1243 |
if (aws_array_list_init_dynamic( |
| 1244 |
&resource_array, wrapped_user_data->allocator, 10, sizeof(struct aws_byte_cursor))) { |
| 1245 |
goto on_finish; |
| 1246 |
} |
| 1247 |
aws_byte_cursor_split_on_char(&resource_cursor, '\n', &resource_array); |
| 1248 |
} |
| 1249 |
|
| 1250 |
on_finish: |
| 1251 |
wrapped_user_data->callback(&resource_array, error_code, wrapped_user_data->user_data); |
| 1252 |
aws_array_list_clean_up_secure(&resource_array); |
| 1253 |
aws_mem_release(wrapped_user_data->allocator, wrapped_user_data); |
| 1254 |
} |
| 1255 |
|
| 1256 |
static void s_process_credentials_resource(const struct aws_byte_buf *resource, int error_code, void *user_data) { |
| 1257 |
struct imds_get_credentials_user_data *wrapped_user_data = user_data; |
| 1258 |
struct aws_credentials *credentials = NULL; |
| 1259 |
|
| 1260 |
struct aws_byte_buf json_data; |
| 1261 |
AWS_ZERO_STRUCT(json_data); |
| 1262 |
|
| 1263 |
if (!resource || error_code) { |
| 1264 |
goto on_finish; |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (aws_byte_buf_init_copy(&json_data, wrapped_user_data->allocator, resource)) { |
| 1268 |
goto on_finish; |
| 1269 |
} |
| 1270 |
|
| 1271 |
if (aws_byte_buf_append_null_terminator(&json_data)) { |
| 1272 |
goto on_finish; |
| 1273 |
} |
| 1274 |
|
| 1275 |
struct aws_parse_credentials_from_json_doc_options parse_options = { |
| 1276 |
.access_key_id_name = "AccessKeyId", |
| 1277 |
.secret_access_key_name = "SecretAccessKey", |
| 1278 |
.token_name = "Token", |
| 1279 |
.expiration_name = "Expiration", |
| 1280 |
.token_required = true, |
| 1281 |
.expiration_required = true, |
| 1282 |
}; |
| 1283 |
|
| 1284 |
credentials = aws_parse_credentials_from_json_document( |
| 1285 |
wrapped_user_data->allocator, aws_byte_cursor_from_buf(&json_data), &parse_options); |
| 1286 |
|
| 1287 |
on_finish: |
| 1288 |
wrapped_user_data->callback(credentials, error_code, wrapped_user_data->user_data); |
| 1289 |
aws_credentials_release(credentials); |
| 1290 |
aws_byte_buf_clean_up_secure(&json_data); |
| 1291 |
aws_mem_release(wrapped_user_data->allocator, wrapped_user_data); |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
static int s_parse_iam_profile(struct aws_json_value *document_root, struct aws_imds_iam_profile *dest) { |
| 1302 |
|
| 1303 |
bool success = false; |
| 1304 |
|
| 1305 |
struct aws_byte_cursor last_updated_cursor; |
| 1306 |
struct aws_json_value *last_updated = |
| 1307 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("LastUpdated")); |
| 1308 |
if (last_updated == NULL) { |
| 1309 |
last_updated = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("lastupdated")); |
| 1310 |
} |
| 1311 |
if (!aws_json_value_is_string(last_updated) || |
| 1312 |
(aws_json_value_get_string(last_updated, &last_updated_cursor) == AWS_OP_ERR)) { |
| 1313 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse LastUpdated from Json document for iam profile."); |
| 1314 |
goto done; |
| 1315 |
} |
| 1316 |
|
| 1317 |
struct aws_byte_cursor profile_arn_cursor; |
| 1318 |
struct aws_json_value *profile_arn = |
| 1319 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("InstanceProfileArn")); |
| 1320 |
if (profile_arn == NULL) { |
| 1321 |
profile_arn = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("instanceprofilearn")); |
| 1322 |
} |
| 1323 |
if (!aws_json_value_is_string(profile_arn) || |
| 1324 |
(aws_json_value_get_string(profile_arn, &profile_arn_cursor) == AWS_OP_ERR)) { |
| 1325 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse InstanceProfileArn from Json document for iam profile."); |
| 1326 |
goto done; |
| 1327 |
} |
| 1328 |
|
| 1329 |
struct aws_byte_cursor profile_id_cursor; |
| 1330 |
struct aws_json_value *profile_id = |
| 1331 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("InstanceProfileId")); |
| 1332 |
if (profile_id == NULL) { |
| 1333 |
profile_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("instanceprofileid")); |
| 1334 |
} |
| 1335 |
if (!aws_json_value_is_string(profile_id) || |
| 1336 |
(aws_json_value_get_string(profile_id, &profile_id_cursor) == AWS_OP_ERR)) { |
| 1337 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse InstanceProfileId from Json document for iam profile."); |
| 1338 |
goto done; |
| 1339 |
} |
| 1340 |
|
| 1341 |
if (last_updated_cursor.len == 0 || profile_arn_cursor.len == 0 || profile_id_cursor.len == 0) { |
| 1342 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Parsed an unexpected Json document fro iam profile."); |
| 1343 |
goto done; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if (aws_date_time_init_from_str_cursor(&dest->last_updated, &last_updated_cursor, AWS_DATE_FORMAT_ISO_8601)) { |
| 1347 |
AWS_LOGF_ERROR( |
| 1348 |
AWS_LS_IMDS_CLIENT, "LastUpdate in iam profile Json document is not a valid ISO_8601 date string."); |
| 1349 |
goto done; |
| 1350 |
} |
| 1351 |
|
| 1352 |
dest->instance_profile_arn = profile_arn_cursor; |
| 1353 |
dest->instance_profile_id = profile_id_cursor; |
| 1354 |
|
| 1355 |
success = true; |
| 1356 |
|
| 1357 |
done: |
| 1358 |
return success ? AWS_OP_ERR : AWS_OP_SUCCESS; |
| 1359 |
} |
| 1360 |
|
| 1361 |
static void s_process_iam_profile(const struct aws_byte_buf *resource, int error_code, void *user_data) { |
| 1362 |
struct imds_get_iam_user_data *wrapped_user_data = user_data; |
| 1363 |
struct aws_json_value *document_root = NULL; |
| 1364 |
struct aws_imds_iam_profile iam; |
| 1365 |
AWS_ZERO_STRUCT(iam); |
| 1366 |
|
| 1367 |
struct aws_byte_buf json_data; |
| 1368 |
AWS_ZERO_STRUCT(json_data); |
| 1369 |
|
| 1370 |
if (!resource || error_code) { |
| 1371 |
goto on_finish; |
| 1372 |
} |
| 1373 |
|
| 1374 |
if (aws_byte_buf_init_copy(&json_data, wrapped_user_data->allocator, resource)) { |
| 1375 |
goto on_finish; |
| 1376 |
} |
| 1377 |
|
| 1378 |
if (aws_byte_buf_append_null_terminator(&json_data)) { |
| 1379 |
goto on_finish; |
| 1380 |
} |
| 1381 |
|
| 1382 |
struct aws_byte_cursor json_data_cursor = aws_byte_cursor_from_buf(&json_data); |
| 1383 |
document_root = aws_json_value_new_from_string(aws_default_allocator(), json_data_cursor); |
| 1384 |
if (document_root == NULL) { |
| 1385 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse document as Json document for iam profile."); |
| 1386 |
goto on_finish; |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (s_parse_iam_profile(document_root, &iam)) { |
| 1390 |
goto on_finish; |
| 1391 |
} |
| 1392 |
|
| 1393 |
on_finish: |
| 1394 |
wrapped_user_data->callback(&iam, error_code, wrapped_user_data->user_data); |
| 1395 |
aws_byte_buf_clean_up_secure(&json_data); |
| 1396 |
aws_mem_release(wrapped_user_data->allocator, wrapped_user_data); |
| 1397 |
if (document_root != NULL) { |
| 1398 |
aws_json_value_destroy(document_root); |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
static int s_parse_instance_info(struct aws_json_value *document_root, struct aws_imds_instance_info *dest) { |
| 1422 |
|
| 1423 |
bool success = false; |
| 1424 |
|
| 1425 |
struct aws_byte_cursor account_id_cursor; |
| 1426 |
struct aws_json_value *account_id = |
| 1427 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("accountId")); |
| 1428 |
if (account_id == NULL) { |
| 1429 |
account_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("accountid")); |
| 1430 |
if (account_id == NULL) { |
| 1431 |
account_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("AccountId")); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
if (!aws_json_value_is_string(account_id) || |
| 1435 |
(aws_json_value_get_string(account_id, &account_id_cursor) == AWS_OP_ERR)) { |
| 1436 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse accountId from Json document for ec2 instance info."); |
| 1437 |
goto done; |
| 1438 |
} |
| 1439 |
dest->account_id = account_id_cursor; |
| 1440 |
|
| 1441 |
struct aws_byte_cursor architecture_cursor; |
| 1442 |
struct aws_json_value *architecture = |
| 1443 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("architecture")); |
| 1444 |
if (architecture == NULL) { |
| 1445 |
architecture = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("Architecture")); |
| 1446 |
} |
| 1447 |
if (!aws_json_value_is_string(architecture) || |
| 1448 |
(aws_json_value_get_string(architecture, &architecture_cursor) == AWS_OP_ERR)) { |
| 1449 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse architecture from Json document for ec2 instance info."); |
| 1450 |
goto done; |
| 1451 |
} |
| 1452 |
dest->architecture = architecture_cursor; |
| 1453 |
|
| 1454 |
struct aws_byte_cursor availability_zone_cursor; |
| 1455 |
struct aws_json_value *availability_zone = |
| 1456 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("availabilityZone")); |
| 1457 |
if (availability_zone == NULL) { |
| 1458 |
availability_zone = |
| 1459 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("availabilityzone")); |
| 1460 |
if (availability_zone == NULL) { |
| 1461 |
availability_zone = |
| 1462 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("AvailabilityZone")); |
| 1463 |
} |
| 1464 |
} |
| 1465 |
if (!aws_json_value_is_string(availability_zone) || |
| 1466 |
(aws_json_value_get_string(availability_zone, &availability_zone_cursor) == AWS_OP_ERR)) { |
| 1467 |
AWS_LOGF_ERROR( |
| 1468 |
AWS_LS_IMDS_CLIENT, "Failed to parse availabilityZone from Json document for ec2 instance info."); |
| 1469 |
goto done; |
| 1470 |
} |
| 1471 |
dest->availability_zone = availability_zone_cursor; |
| 1472 |
|
| 1473 |
struct aws_byte_cursor billing_products_cursor; |
| 1474 |
struct aws_json_value *billing_products = |
| 1475 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("billingProducts")); |
| 1476 |
if (billing_products == NULL) { |
| 1477 |
billing_products = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("billingproducts")); |
| 1478 |
if (billing_products == NULL) { |
| 1479 |
billing_products = |
| 1480 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("BillingProducts")); |
| 1481 |
} |
| 1482 |
} |
| 1483 |
if (aws_json_value_is_array(billing_products)) { |
| 1484 |
struct aws_json_value *element; |
| 1485 |
for (size_t i = 0; i < aws_json_get_array_size(billing_products); i++) { |
| 1486 |
element = aws_json_get_array_element(billing_products, i); |
| 1487 |
if (aws_json_value_is_string(element) && |
| 1488 |
aws_json_value_get_string(element, &billing_products_cursor) != AWS_OP_ERR) { |
| 1489 |
struct aws_byte_cursor item = billing_products_cursor; |
| 1490 |
aws_array_list_push_back(&dest->billing_products, (const void *)&item); |
| 1491 |
} |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
struct aws_byte_cursor marketplace_product_codes_cursor; |
| 1496 |
struct aws_json_value *marketplace_product_codes = |
| 1497 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("marketplaceProductCodes")); |
| 1498 |
if (marketplace_product_codes == NULL) { |
| 1499 |
marketplace_product_codes = |
| 1500 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("marketplaceproductcodes")); |
| 1501 |
if (marketplace_product_codes == NULL) { |
| 1502 |
marketplace_product_codes = |
| 1503 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("MarketplaceProductCodes")); |
| 1504 |
} |
| 1505 |
} |
| 1506 |
if (aws_json_value_is_array(marketplace_product_codes)) { |
| 1507 |
struct aws_json_value *element; |
| 1508 |
for (size_t i = 0; i < aws_json_get_array_size(marketplace_product_codes); i++) { |
| 1509 |
element = aws_json_get_array_element(marketplace_product_codes, i); |
| 1510 |
if (aws_json_value_is_string(element) && |
| 1511 |
aws_json_value_get_string(element, &marketplace_product_codes_cursor) != AWS_OP_ERR) { |
| 1512 |
struct aws_byte_cursor item = marketplace_product_codes_cursor; |
| 1513 |
aws_array_list_push_back(&dest->billing_products, (const void *)&item); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
} |
| 1517 |
|
| 1518 |
struct aws_byte_cursor image_id_cursor; |
| 1519 |
struct aws_json_value *image_id = |
| 1520 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("imageId")); |
| 1521 |
if (image_id == NULL) { |
| 1522 |
image_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("imageid")); |
| 1523 |
if (image_id == NULL) { |
| 1524 |
image_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("ImageId")); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
if (aws_json_value_is_string(image_id) && (aws_json_value_get_string(image_id, &image_id_cursor) != AWS_OP_ERR)) { |
| 1528 |
dest->image_id = image_id_cursor; |
| 1529 |
} |
| 1530 |
|
| 1531 |
struct aws_byte_cursor instance_id_cursor; |
| 1532 |
struct aws_json_value *instance_id = |
| 1533 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("instanceId")); |
| 1534 |
if (instance_id == NULL) { |
| 1535 |
instance_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("instanceid")); |
| 1536 |
if (instance_id == NULL) { |
| 1537 |
instance_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("InstanceId")); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
if (!aws_json_value_is_string(instance_id) || |
| 1541 |
(aws_json_value_get_string(instance_id, &instance_id_cursor) == AWS_OP_ERR)) { |
| 1542 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse instanceId from Json document for ec2 instance info."); |
| 1543 |
goto done; |
| 1544 |
} |
| 1545 |
dest->instance_id = instance_id_cursor; |
| 1546 |
|
| 1547 |
struct aws_byte_cursor instance_type_cursor; |
| 1548 |
struct aws_json_value *instance_type = |
| 1549 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("instanceType")); |
| 1550 |
if (instance_type == NULL) { |
| 1551 |
instance_type = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("instancetype")); |
| 1552 |
if (instance_type == NULL) { |
| 1553 |
instance_type = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("InstanceType")); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
if (!aws_json_value_is_string(instance_type) || |
| 1557 |
(aws_json_value_get_string(instance_type, &instance_type_cursor) == AWS_OP_ERR)) { |
| 1558 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse instanceType from Json document for ec2 instance info."); |
| 1559 |
goto done; |
| 1560 |
} |
| 1561 |
dest->instance_type = instance_type_cursor; |
| 1562 |
|
| 1563 |
struct aws_byte_cursor kernel_id_cursor; |
| 1564 |
struct aws_json_value *kernel_id = |
| 1565 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("kernelId")); |
| 1566 |
if (kernel_id == NULL) { |
| 1567 |
kernel_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("kernelid")); |
| 1568 |
if (kernel_id == NULL) { |
| 1569 |
kernel_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("KernelId")); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
if (aws_json_value_is_string(kernel_id) && |
| 1573 |
(aws_json_value_get_string(kernel_id, &kernel_id_cursor) != AWS_OP_ERR)) { |
| 1574 |
dest->kernel_id = kernel_id_cursor; |
| 1575 |
} |
| 1576 |
|
| 1577 |
struct aws_byte_cursor private_ip_cursor; |
| 1578 |
struct aws_json_value *private_ip = |
| 1579 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("privateIp")); |
| 1580 |
if (private_ip == NULL) { |
| 1581 |
private_ip = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("privateip")); |
| 1582 |
if (private_ip == NULL) { |
| 1583 |
private_ip = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("PrivateIp")); |
| 1584 |
} |
| 1585 |
} |
| 1586 |
if (aws_json_value_is_string(private_ip) && |
| 1587 |
(aws_json_value_get_string(private_ip, &private_ip_cursor) != AWS_OP_ERR)) { |
| 1588 |
dest->private_ip = private_ip_cursor; |
| 1589 |
} |
| 1590 |
|
| 1591 |
struct aws_byte_cursor ramdisk_id_cursor; |
| 1592 |
struct aws_json_value *ramdisk_id = |
| 1593 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("ramdiskId")); |
| 1594 |
if (ramdisk_id == NULL) { |
| 1595 |
ramdisk_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("ramdiskid")); |
| 1596 |
if (ramdisk_id == NULL) { |
| 1597 |
ramdisk_id = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("RamdiskId")); |
| 1598 |
} |
| 1599 |
} |
| 1600 |
if (aws_json_value_is_string(ramdisk_id) && |
| 1601 |
(aws_json_value_get_string(ramdisk_id, &ramdisk_id_cursor) != AWS_OP_ERR)) { |
| 1602 |
dest->ramdisk_id = ramdisk_id_cursor; |
| 1603 |
} |
| 1604 |
|
| 1605 |
struct aws_byte_cursor region_cursor; |
| 1606 |
struct aws_json_value *region = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("region")); |
| 1607 |
if (region == NULL) { |
| 1608 |
region = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("Region")); |
| 1609 |
} |
| 1610 |
if (!aws_json_value_is_string(region) || (aws_json_value_get_string(region, ®ion_cursor) == AWS_OP_ERR)) { |
| 1611 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse region from Json document for ec2 instance info."); |
| 1612 |
goto done; |
| 1613 |
} |
| 1614 |
dest->region = region_cursor; |
| 1615 |
|
| 1616 |
struct aws_byte_cursor version_cursor; |
| 1617 |
struct aws_json_value *version = |
| 1618 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("version")); |
| 1619 |
if (version == NULL) { |
| 1620 |
version = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("Version")); |
| 1621 |
} |
| 1622 |
if (!aws_json_value_is_string(version) || (aws_json_value_get_string(version, &version_cursor) == AWS_OP_ERR)) { |
| 1623 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse version from Json document for ec2 instance info."); |
| 1624 |
goto done; |
| 1625 |
} |
| 1626 |
dest->version = version_cursor; |
| 1627 |
|
| 1628 |
struct aws_byte_cursor pending_time_cursor; |
| 1629 |
struct aws_json_value *pending_time = |
| 1630 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("pendingTime")); |
| 1631 |
if (pending_time == NULL) { |
| 1632 |
pending_time = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("pendingtime")); |
| 1633 |
if (pending_time == NULL) { |
| 1634 |
pending_time = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("PendingTime")); |
| 1635 |
} |
| 1636 |
} |
| 1637 |
if (!aws_json_value_is_string(pending_time) || |
| 1638 |
(aws_json_value_get_string(pending_time, &pending_time_cursor) == AWS_OP_ERR)) { |
| 1639 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse pendingTime from Json document for ec2 instance info."); |
| 1640 |
goto done; |
| 1641 |
} |
| 1642 |
|
| 1643 |
if (aws_date_time_init_from_str_cursor(&dest->pending_time, &pending_time_cursor, AWS_DATE_FORMAT_ISO_8601)) { |
| 1644 |
AWS_LOGF_ERROR( |
| 1645 |
AWS_LS_IMDS_CLIENT, "pendingTime in instance info Json document is not a valid ISO_8601 date string."); |
| 1646 |
goto done; |
| 1647 |
} |
| 1648 |
|
| 1649 |
success = true; |
| 1650 |
|
| 1651 |
done: |
| 1652 |
return success ? AWS_OP_ERR : AWS_OP_SUCCESS; |
| 1653 |
} |
| 1654 |
|
| 1655 |
static void s_process_instance_info(const struct aws_byte_buf *resource, int error_code, void *user_data) { |
| 1656 |
struct imds_get_instance_user_data *wrapped_user_data = user_data; |
| 1657 |
struct aws_imds_instance_info instance_info; |
| 1658 |
AWS_ZERO_STRUCT(instance_info); |
| 1659 |
struct aws_byte_buf json_data; |
| 1660 |
AWS_ZERO_STRUCT(json_data); |
| 1661 |
|
| 1662 |
struct aws_json_value *document_root = NULL; |
| 1663 |
|
| 1664 |
if (aws_array_list_init_dynamic( |
| 1665 |
&instance_info.billing_products, wrapped_user_data->allocator, 10, sizeof(struct aws_byte_cursor))) { |
| 1666 |
goto on_finish; |
| 1667 |
} |
| 1668 |
|
| 1669 |
if (aws_array_list_init_dynamic( |
| 1670 |
&instance_info.marketplace_product_codes, |
| 1671 |
wrapped_user_data->allocator, |
| 1672 |
10, |
| 1673 |
sizeof(struct aws_byte_cursor))) { |
| 1674 |
goto on_finish; |
| 1675 |
} |
| 1676 |
|
| 1677 |
if (!resource || error_code) { |
| 1678 |
goto on_finish; |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (aws_byte_buf_init_copy(&json_data, wrapped_user_data->allocator, resource)) { |
| 1682 |
goto on_finish; |
| 1683 |
} |
| 1684 |
|
| 1685 |
if (aws_byte_buf_append_null_terminator(&json_data)) { |
| 1686 |
goto on_finish; |
| 1687 |
} |
| 1688 |
|
| 1689 |
struct aws_byte_cursor json_data_cursor = aws_byte_cursor_from_buf(&json_data); |
| 1690 |
document_root = aws_json_value_new_from_string(aws_default_allocator(), json_data_cursor); |
| 1691 |
if (document_root == NULL) { |
| 1692 |
AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse document as Json document for ec2 instance info."); |
| 1693 |
goto on_finish; |
| 1694 |
} |
| 1695 |
|
| 1696 |
if (s_parse_instance_info(document_root, &instance_info)) { |
| 1697 |
goto on_finish; |
| 1698 |
} |
| 1699 |
|
| 1700 |
on_finish: |
| 1701 |
wrapped_user_data->callback(&instance_info, error_code, wrapped_user_data->user_data); |
| 1702 |
aws_array_list_clean_up_secure(&instance_info.billing_products); |
| 1703 |
aws_array_list_clean_up_secure(&instance_info.marketplace_product_codes); |
| 1704 |
aws_byte_buf_clean_up_secure(&json_data); |
| 1705 |
aws_mem_release(wrapped_user_data->allocator, wrapped_user_data); |
| 1706 |
if (document_root != NULL) { |
| 1707 |
aws_json_value_destroy(document_root); |
| 1708 |
} |
| 1709 |
} |
| 1710 |
|
| 1711 |
static int s_aws_imds_get_resource( |
| 1712 |
struct aws_imds_client *client, |
| 1713 |
struct aws_byte_cursor path, |
| 1714 |
struct aws_byte_cursor name, |
| 1715 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1716 |
void *user_data) { |
| 1717 |
|
| 1718 |
struct aws_byte_buf resource; |
| 1719 |
if (aws_byte_buf_init_copy_from_cursor(&resource, client->allocator, path)) { |
| 1720 |
return AWS_OP_ERR; |
| 1721 |
} |
| 1722 |
if (aws_byte_buf_append_dynamic(&resource, &name)) { |
| 1723 |
goto error; |
| 1724 |
} |
| 1725 |
if (aws_imds_client_get_resource_async(client, aws_byte_cursor_from_buf(&resource), callback, user_data)) { |
| 1726 |
goto error; |
| 1727 |
} |
| 1728 |
aws_byte_buf_clean_up(&resource); |
| 1729 |
return AWS_OP_SUCCESS; |
| 1730 |
|
| 1731 |
error: |
| 1732 |
aws_byte_buf_clean_up(&resource); |
| 1733 |
return AWS_OP_ERR; |
| 1734 |
} |
| 1735 |
|
| 1736 |
int s_aws_imds_get_converted_resource( |
| 1737 |
struct aws_imds_client *client, |
| 1738 |
struct aws_byte_cursor path, |
| 1739 |
struct aws_byte_cursor name, |
| 1740 |
aws_imds_client_on_get_resource_callback_fn conversion_fn, |
| 1741 |
void *user_data) { |
| 1742 |
return s_aws_imds_get_resource(client, path, name, conversion_fn, user_data); |
| 1743 |
} |
| 1744 |
|
| 1745 |
int aws_imds_client_get_ami_id( |
| 1746 |
struct aws_imds_client *client, |
| 1747 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1748 |
void *user_data) { |
| 1749 |
return s_aws_imds_get_resource( |
| 1750 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/ami-id"), callback, user_data); |
| 1751 |
} |
| 1752 |
|
| 1753 |
int aws_imds_client_get_ami_launch_index( |
| 1754 |
struct aws_imds_client *client, |
| 1755 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1756 |
void *user_data) { |
| 1757 |
return s_aws_imds_get_resource( |
| 1758 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/ami-launch-index"), callback, user_data); |
| 1759 |
} |
| 1760 |
|
| 1761 |
int aws_imds_client_get_ami_manifest_path( |
| 1762 |
struct aws_imds_client *client, |
| 1763 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1764 |
void *user_data) { |
| 1765 |
return s_aws_imds_get_resource( |
| 1766 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/ami-manifest-path"), callback, user_data); |
| 1767 |
} |
| 1768 |
|
| 1769 |
int aws_imds_client_get_ancestor_ami_ids( |
| 1770 |
struct aws_imds_client *client, |
| 1771 |
aws_imds_client_on_get_array_callback_fn callback, |
| 1772 |
void *user_data) { |
| 1773 |
|
| 1774 |
struct imds_get_array_user_data *wrapped_user_data = |
| 1775 |
aws_mem_calloc(client->allocator, 1, sizeof(struct imds_get_array_user_data)); |
| 1776 |
if (!wrapped_user_data) { |
| 1777 |
return AWS_OP_ERR; |
| 1778 |
} |
| 1779 |
|
| 1780 |
wrapped_user_data->allocator = client->allocator; |
| 1781 |
wrapped_user_data->callback = callback; |
| 1782 |
wrapped_user_data->user_data = user_data; |
| 1783 |
|
| 1784 |
return s_aws_imds_get_converted_resource( |
| 1785 |
client, |
| 1786 |
s_ec2_metadata_root, |
| 1787 |
aws_byte_cursor_from_c_str("/ancestor-ami-ids"), |
| 1788 |
s_process_array_resource, |
| 1789 |
wrapped_user_data); |
| 1790 |
} |
| 1791 |
|
| 1792 |
int aws_imds_client_get_instance_action( |
| 1793 |
struct aws_imds_client *client, |
| 1794 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1795 |
void *user_data) { |
| 1796 |
return s_aws_imds_get_resource( |
| 1797 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/instance-action"), callback, user_data); |
| 1798 |
} |
| 1799 |
|
| 1800 |
int aws_imds_client_get_instance_id( |
| 1801 |
struct aws_imds_client *client, |
| 1802 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1803 |
void *user_data) { |
| 1804 |
return s_aws_imds_get_resource( |
| 1805 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/instance-id"), callback, user_data); |
| 1806 |
} |
| 1807 |
|
| 1808 |
int aws_imds_client_get_instance_type( |
| 1809 |
struct aws_imds_client *client, |
| 1810 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1811 |
void *user_data) { |
| 1812 |
return s_aws_imds_get_resource( |
| 1813 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/instance-type"), callback, user_data); |
| 1814 |
} |
| 1815 |
|
| 1816 |
int aws_imds_client_get_mac_address( |
| 1817 |
struct aws_imds_client *client, |
| 1818 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1819 |
void *user_data) { |
| 1820 |
return s_aws_imds_get_resource( |
| 1821 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/mac"), callback, user_data); |
| 1822 |
} |
| 1823 |
|
| 1824 |
int aws_imds_client_get_private_ip_address( |
| 1825 |
struct aws_imds_client *client, |
| 1826 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1827 |
void *user_data) { |
| 1828 |
return s_aws_imds_get_resource( |
| 1829 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/local-ipv4"), callback, user_data); |
| 1830 |
} |
| 1831 |
|
| 1832 |
int aws_imds_client_get_availability_zone( |
| 1833 |
struct aws_imds_client *client, |
| 1834 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1835 |
void *user_data) { |
| 1836 |
return s_aws_imds_get_resource( |
| 1837 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/placement/availability-zone"), callback, user_data); |
| 1838 |
} |
| 1839 |
|
| 1840 |
int aws_imds_client_get_product_codes( |
| 1841 |
struct aws_imds_client *client, |
| 1842 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1843 |
void *user_data) { |
| 1844 |
return s_aws_imds_get_resource( |
| 1845 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/product-codes"), callback, user_data); |
| 1846 |
} |
| 1847 |
|
| 1848 |
int aws_imds_client_get_public_key( |
| 1849 |
struct aws_imds_client *client, |
| 1850 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1851 |
void *user_data) { |
| 1852 |
return s_aws_imds_get_resource( |
| 1853 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/public-keys/0/openssh-key"), callback, user_data); |
| 1854 |
} |
| 1855 |
|
| 1856 |
int aws_imds_client_get_ramdisk_id( |
| 1857 |
struct aws_imds_client *client, |
| 1858 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1859 |
void *user_data) { |
| 1860 |
return s_aws_imds_get_resource( |
| 1861 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/ramdisk-id"), callback, user_data); |
| 1862 |
} |
| 1863 |
|
| 1864 |
int aws_imds_client_get_reservation_id( |
| 1865 |
struct aws_imds_client *client, |
| 1866 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1867 |
void *user_data) { |
| 1868 |
return s_aws_imds_get_resource( |
| 1869 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/reservation-id"), callback, user_data); |
| 1870 |
} |
| 1871 |
|
| 1872 |
int aws_imds_client_get_security_groups( |
| 1873 |
struct aws_imds_client *client, |
| 1874 |
aws_imds_client_on_get_array_callback_fn callback, |
| 1875 |
void *user_data) { |
| 1876 |
|
| 1877 |
struct imds_get_array_user_data *wrapped_user_data = |
| 1878 |
aws_mem_calloc(client->allocator, 1, sizeof(struct imds_get_array_user_data)); |
| 1879 |
if (!wrapped_user_data) { |
| 1880 |
return AWS_OP_ERR; |
| 1881 |
} |
| 1882 |
|
| 1883 |
wrapped_user_data->allocator = client->allocator; |
| 1884 |
wrapped_user_data->callback = callback; |
| 1885 |
wrapped_user_data->user_data = user_data; |
| 1886 |
|
| 1887 |
return s_aws_imds_get_converted_resource( |
| 1888 |
client, |
| 1889 |
s_ec2_metadata_root, |
| 1890 |
aws_byte_cursor_from_c_str("/security-groups"), |
| 1891 |
s_process_array_resource, |
| 1892 |
wrapped_user_data); |
| 1893 |
} |
| 1894 |
|
| 1895 |
int aws_imds_client_get_block_device_mapping( |
| 1896 |
struct aws_imds_client *client, |
| 1897 |
aws_imds_client_on_get_array_callback_fn callback, |
| 1898 |
void *user_data) { |
| 1899 |
|
| 1900 |
struct imds_get_array_user_data *wrapped_user_data = |
| 1901 |
aws_mem_calloc(client->allocator, 1, sizeof(struct imds_get_array_user_data)); |
| 1902 |
|
| 1903 |
if (!wrapped_user_data) { |
| 1904 |
return AWS_OP_ERR; |
| 1905 |
} |
| 1906 |
|
| 1907 |
wrapped_user_data->allocator = client->allocator; |
| 1908 |
wrapped_user_data->callback = callback; |
| 1909 |
wrapped_user_data->user_data = user_data; |
| 1910 |
return s_aws_imds_get_converted_resource( |
| 1911 |
client, |
| 1912 |
s_ec2_metadata_root, |
| 1913 |
aws_byte_cursor_from_c_str("/block-device-mapping"), |
| 1914 |
s_process_array_resource, |
| 1915 |
wrapped_user_data); |
| 1916 |
} |
| 1917 |
|
| 1918 |
int aws_imds_client_get_attached_iam_role( |
| 1919 |
struct aws_imds_client *client, |
| 1920 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1921 |
void *user_data) { |
| 1922 |
return s_aws_imds_get_resource( |
| 1923 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/iam/security-credentials/"), callback, user_data); |
| 1924 |
} |
| 1925 |
|
| 1926 |
int aws_imds_client_get_credentials( |
| 1927 |
struct aws_imds_client *client, |
| 1928 |
struct aws_byte_cursor iam_role_name, |
| 1929 |
aws_imds_client_on_get_credentials_callback_fn callback, |
| 1930 |
void *user_data) { |
| 1931 |
|
| 1932 |
struct imds_get_credentials_user_data *wrapped_user_data = |
| 1933 |
aws_mem_calloc(client->allocator, 1, sizeof(struct imds_get_credentials_user_data)); |
| 1934 |
if (!wrapped_user_data) { |
| 1935 |
return AWS_OP_ERR; |
| 1936 |
} |
| 1937 |
|
| 1938 |
wrapped_user_data->allocator = client->allocator; |
| 1939 |
wrapped_user_data->callback = callback; |
| 1940 |
wrapped_user_data->user_data = user_data; |
| 1941 |
|
| 1942 |
return s_aws_imds_get_converted_resource( |
| 1943 |
client, s_ec2_credentials_root, iam_role_name, s_process_credentials_resource, wrapped_user_data); |
| 1944 |
} |
| 1945 |
|
| 1946 |
int aws_imds_client_get_iam_profile( |
| 1947 |
struct aws_imds_client *client, |
| 1948 |
aws_imds_client_on_get_iam_profile_callback_fn callback, |
| 1949 |
void *user_data) { |
| 1950 |
|
| 1951 |
struct imds_get_iam_user_data *wrapped_user_data = |
| 1952 |
aws_mem_calloc(client->allocator, 1, sizeof(struct imds_get_iam_user_data)); |
| 1953 |
if (!wrapped_user_data) { |
| 1954 |
return AWS_OP_ERR; |
| 1955 |
} |
| 1956 |
|
| 1957 |
wrapped_user_data->allocator = client->allocator; |
| 1958 |
wrapped_user_data->callback = callback; |
| 1959 |
wrapped_user_data->user_data = user_data; |
| 1960 |
|
| 1961 |
return s_aws_imds_get_converted_resource( |
| 1962 |
client, s_ec2_metadata_root, aws_byte_cursor_from_c_str("/iam/info"), s_process_iam_profile, wrapped_user_data); |
| 1963 |
} |
| 1964 |
|
| 1965 |
int aws_imds_client_get_user_data( |
| 1966 |
struct aws_imds_client *client, |
| 1967 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1968 |
void *user_data) { |
| 1969 |
return s_aws_imds_get_resource(client, s_ec2_userdata_root, aws_byte_cursor_from_c_str(""), callback, user_data); |
| 1970 |
} |
| 1971 |
|
| 1972 |
int aws_imds_client_get_instance_signature( |
| 1973 |
struct aws_imds_client *client, |
| 1974 |
aws_imds_client_on_get_resource_callback_fn callback, |
| 1975 |
void *user_data) { |
| 1976 |
return s_aws_imds_get_resource(client, s_ec2_dynamicdata_root, s_instance_identity_signature, callback, user_data); |
| 1977 |
} |
| 1978 |
|
| 1979 |
int aws_imds_client_get_instance_info( |
| 1980 |
struct aws_imds_client *client, |
| 1981 |
aws_imds_client_on_get_instance_info_callback_fn callback, |
| 1982 |
void *user_data) { |
| 1983 |
|
| 1984 |
struct imds_get_instance_user_data *wrapped_user_data = |
| 1985 |
aws_mem_calloc(client->allocator, 1, sizeof(struct imds_get_instance_user_data)); |
| 1986 |
if (!wrapped_user_data) { |
| 1987 |
return AWS_OP_ERR; |
| 1988 |
} |
| 1989 |
|
| 1990 |
wrapped_user_data->allocator = client->allocator; |
| 1991 |
wrapped_user_data->callback = callback; |
| 1992 |
wrapped_user_data->user_data = user_data; |
| 1993 |
|
| 1994 |
return s_aws_imds_get_converted_resource( |
| 1995 |
client, s_ec2_dynamicdata_root, s_instance_identity_document, s_process_instance_info, wrapped_user_data); |
| 1996 |
} |