| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/private/credentials_utils.h> |
| 7 |
|
| 8 |
#include <aws/common/clock.h> |
| 9 |
#include <aws/common/date_time.h> |
| 10 |
#include <aws/common/environment.h> |
| 11 |
#include <aws/common/json.h> |
| 12 |
#include <aws/common/string.h> |
| 13 |
#include <aws/common/uuid.h> |
| 14 |
#include <aws/http/connection.h> |
| 15 |
#include <aws/http/request_response.h> |
| 16 |
#include <aws/http/status_code.h> |
| 17 |
#include <aws/sdkutils/aws_profile.h> |
| 18 |
|
| 19 |
#if defined(_MSC_VER) |
| 20 |
# pragma warning(disable : 4232) |
| 21 |
#endif |
| 22 |
|
| 23 |
static struct aws_auth_http_system_vtable s_default_function_table = { |
| 24 |
.aws_http_connection_manager_new = aws_http_connection_manager_new, |
| 25 |
.aws_http_connection_manager_release = aws_http_connection_manager_release, |
| 26 |
.aws_http_connection_manager_acquire_connection = aws_http_connection_manager_acquire_connection, |
| 27 |
.aws_http_connection_manager_release_connection = aws_http_connection_manager_release_connection, |
| 28 |
.aws_http_connection_make_request = aws_http_connection_make_request, |
| 29 |
.aws_http_stream_activate = aws_http_stream_activate, |
| 30 |
.aws_http_stream_get_connection = aws_http_stream_get_connection, |
| 31 |
.aws_http_stream_get_incoming_response_status = aws_http_stream_get_incoming_response_status, |
| 32 |
.aws_http_stream_release = aws_http_stream_release, |
| 33 |
.aws_http_connection_close = aws_http_connection_close, |
| 34 |
.aws_high_res_clock_get_ticks = aws_high_res_clock_get_ticks, |
| 35 |
}; |
| 36 |
|
| 37 |
const struct aws_auth_http_system_vtable *g_aws_credentials_provider_http_function_table = &s_default_function_table; |
| 38 |
|
| 39 |
void aws_credentials_query_init( |
| 40 |
struct aws_credentials_query *query, |
| 41 |
struct aws_credentials_provider *provider, |
| 42 |
aws_on_get_credentials_callback_fn *callback, |
| 43 |
void *user_data) { |
| 44 |
AWS_ZERO_STRUCT(*query); |
| 45 |
|
| 46 |
query->provider = provider; |
| 47 |
query->user_data = user_data; |
| 48 |
query->callback = callback; |
| 49 |
|
| 50 |
aws_credentials_provider_acquire(provider); |
| 51 |
} |
| 52 |
|
| 53 |
void aws_credentials_query_clean_up(struct aws_credentials_query *query) { |
| 54 |
if (query != NULL) { |
| 55 |
aws_credentials_provider_release(query->provider); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void aws_credentials_provider_init_base( |
| 60 |
struct aws_credentials_provider *provider, |
| 61 |
struct aws_allocator *allocator, |
| 62 |
struct aws_credentials_provider_vtable *vtable, |
| 63 |
void *impl) { |
| 64 |
|
| 65 |
provider->allocator = allocator; |
| 66 |
provider->vtable = vtable; |
| 67 |
provider->impl = impl; |
| 68 |
|
| 69 |
aws_atomic_init_int(&provider->ref_count, 1); |
| 70 |
} |
| 71 |
|
| 72 |
void aws_credentials_provider_invoke_shutdown_callback(struct aws_credentials_provider *provider) { |
| 73 |
if (provider && provider->shutdown_options.shutdown_callback) { |
| 74 |
provider->shutdown_options.shutdown_callback(provider->shutdown_options.shutdown_user_data); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static bool s_parse_expiration_value_from_json_object( |
| 79 |
struct aws_json_value *value, |
| 80 |
const struct aws_parse_credentials_from_json_doc_options *options, |
| 81 |
uint64_t *expiration_timepoint_in_seconds) { |
| 82 |
|
| 83 |
if (value == NULL) { |
| 84 |
AWS_LOGF_INFO(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "No credentials Expiration field in Json document."); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
struct aws_byte_cursor expiration_cursor = { |
| 89 |
.ptr = NULL, |
| 90 |
.len = 0, |
| 91 |
}; |
| 92 |
|
| 93 |
switch (options->expiration_format) { |
| 94 |
case AWS_PCEF_STRING_ISO_8601_DATE: { |
| 95 |
|
| 96 |
if (aws_json_value_get_string(value, &expiration_cursor)) { |
| 97 |
AWS_LOGF_INFO( |
| 98 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 99 |
"Unable to extract credentials Expiration field from Json document."); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
if (expiration_cursor.len == 0) { |
| 104 |
AWS_LOGF_INFO( |
| 105 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Parsed a credentials json document with empty expiration."); |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
struct aws_date_time expiration; |
| 110 |
if (aws_date_time_init_from_str_cursor(&expiration, &expiration_cursor, AWS_DATE_FORMAT_ISO_8601)) { |
| 111 |
AWS_LOGF_INFO( |
| 112 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 113 |
"credentials Expiration in Json document is not a valid ISO_8601 date string."); |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
*expiration_timepoint_in_seconds = (uint64_t)aws_date_time_as_epoch_secs(&expiration); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
case AWS_PCEF_NUMBER_UNIX_EPOCH: { |
| 122 |
double expiration_value = 0; |
| 123 |
if (aws_json_value_get_number(value, &expiration_value)) { |
| 124 |
AWS_LOGF_INFO( |
| 125 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 126 |
"Unable to extract credentials Expiration field from Json document."); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
*expiration_timepoint_in_seconds = (uint64_t)expiration_value; |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
case AWS_PCEF_NUMBER_UNIX_EPOCH_MS: { |
| 135 |
double expiration_value_ms = 0; |
| 136 |
if (aws_json_value_get_number(value, &expiration_value_ms)) { |
| 137 |
AWS_LOGF_INFO( |
| 138 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 139 |
"Unable to extract credentials Expiration field from Json document."); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
*expiration_timepoint_in_seconds = |
| 144 |
aws_timestamp_convert((uint64_t)expiration_value_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_SECS, NULL); |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
default: |
| 149 |
return false; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
struct aws_credentials *aws_parse_credentials_from_aws_json_object( |
| 154 |
struct aws_allocator *allocator, |
| 155 |
struct aws_json_value *document_root, |
| 156 |
const struct aws_parse_credentials_from_json_doc_options *options) { |
| 157 |
|
| 158 |
AWS_FATAL_ASSERT(allocator); |
| 159 |
AWS_FATAL_ASSERT(document_root); |
| 160 |
AWS_FATAL_ASSERT(options); |
| 161 |
AWS_FATAL_ASSERT(options->access_key_id_name); |
| 162 |
AWS_FATAL_ASSERT(options->secret_access_key_name); |
| 163 |
|
| 164 |
if (options->token_required) { |
| 165 |
AWS_FATAL_ASSERT(options->token_name); |
| 166 |
} |
| 167 |
|
| 168 |
if (options->expiration_required) { |
| 169 |
AWS_FATAL_ASSERT(options->expiration_name); |
| 170 |
} |
| 171 |
|
| 172 |
struct aws_credentials *credentials = NULL; |
| 173 |
struct aws_json_value *access_key_id = NULL; |
| 174 |
struct aws_json_value *secret_access_key = NULL; |
| 175 |
struct aws_json_value *token = NULL; |
| 176 |
struct aws_json_value *account_id = NULL; |
| 177 |
struct aws_json_value *creds_expiration = NULL; |
| 178 |
|
| 179 |
bool parse_error = true; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
struct aws_byte_cursor access_key_id_cursor; |
| 185 |
access_key_id = |
| 186 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str((char *)options->access_key_id_name)); |
| 187 |
if (!aws_json_value_is_string(access_key_id) || |
| 188 |
aws_json_value_get_string(access_key_id, &access_key_id_cursor) == AWS_OP_ERR) { |
| 189 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse AccessKeyId from Json document."); |
| 190 |
goto done; |
| 191 |
} |
| 192 |
|
| 193 |
struct aws_byte_cursor secret_access_key_cursor; |
| 194 |
secret_access_key = aws_json_value_get_from_object( |
| 195 |
document_root, aws_byte_cursor_from_c_str((char *)options->secret_access_key_name)); |
| 196 |
if (!aws_json_value_is_string(secret_access_key) || |
| 197 |
aws_json_value_get_string(secret_access_key, &secret_access_key_cursor) == AWS_OP_ERR) { |
| 198 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse SecretAccessKey from Json document."); |
| 199 |
goto done; |
| 200 |
} |
| 201 |
|
| 202 |
struct aws_byte_cursor token_cursor; |
| 203 |
AWS_ZERO_STRUCT(token_cursor); |
| 204 |
if (options->token_name) { |
| 205 |
token = aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str((char *)options->token_name)); |
| 206 |
if (!aws_json_value_is_string(token) || aws_json_value_get_string(token, &token_cursor) == AWS_OP_ERR || |
| 207 |
token_cursor.len == 0) { |
| 208 |
if (options->token_required) { |
| 209 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse Token from Json document."); |
| 210 |
goto done; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
struct aws_byte_cursor account_id_cursor; |
| 216 |
AWS_ZERO_STRUCT(account_id_cursor); |
| 217 |
if (options->account_id_name) { |
| 218 |
account_id = |
| 219 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str((char *)options->account_id_name)); |
| 220 |
if (!aws_json_value_is_string(account_id) || |
| 221 |
aws_json_value_get_string(account_id, &account_id_cursor) == AWS_OP_ERR) { |
| 222 |
AWS_LOGF_DEBUG(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse account_id from Json document."); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
uint64_t expiration_timepoint_in_seconds = UINT64_MAX; |
| 228 |
if (options->expiration_name) { |
| 229 |
creds_expiration = |
| 230 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str((char *)options->expiration_name)); |
| 231 |
|
| 232 |
if (!s_parse_expiration_value_from_json_object(creds_expiration, options, &expiration_timepoint_in_seconds)) { |
| 233 |
if (options->expiration_required) { |
| 234 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse Expiration from Json document."); |
| 235 |
goto done; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if (access_key_id_cursor.len == 0 || secret_access_key_cursor.len == 0) { |
| 244 |
AWS_LOGF_ERROR( |
| 245 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 246 |
"Parsed an unexpected credentials json document, either access key, secret key is empty."); |
| 247 |
goto done; |
| 248 |
} |
| 249 |
struct aws_credentials_options creds_option = { |
| 250 |
.access_key_id_cursor = access_key_id_cursor, |
| 251 |
.secret_access_key_cursor = secret_access_key_cursor, |
| 252 |
.session_token_cursor = token_cursor, |
| 253 |
.account_id_cursor = account_id_cursor, |
| 254 |
.expiration_timepoint_seconds = expiration_timepoint_in_seconds, |
| 255 |
}; |
| 256 |
credentials = aws_credentials_new_with_options(allocator, &creds_option); |
| 257 |
|
| 258 |
if (credentials == NULL) { |
| 259 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to allocate memory for credentials."); |
| 260 |
parse_error = false; |
| 261 |
goto done; |
| 262 |
} |
| 263 |
|
| 264 |
done: |
| 265 |
|
| 266 |
if (parse_error) { |
| 267 |
aws_raise_error(AWS_AUTH_PROVIDER_PARSER_UNEXPECTED_RESPONSE); |
| 268 |
} |
| 269 |
|
| 270 |
return credentials; |
| 271 |
} |
| 272 |
|
| 273 |
struct aws_credentials *aws_parse_credentials_from_json_document( |
| 274 |
struct aws_allocator *allocator, |
| 275 |
struct aws_byte_cursor document, |
| 276 |
const struct aws_parse_credentials_from_json_doc_options *options) { |
| 277 |
struct aws_credentials *credentials = NULL; |
| 278 |
|
| 279 |
struct aws_json_value *document_root = aws_json_value_new_from_string(allocator, document); |
| 280 |
if (document_root == NULL) { |
| 281 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Failed to parse document as Json document."); |
| 282 |
return NULL; |
| 283 |
} |
| 284 |
|
| 285 |
struct aws_json_value *top_level_object = NULL; |
| 286 |
if (options->top_level_object_name) { |
| 287 |
top_level_object = |
| 288 |
aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str(options->top_level_object_name)); |
| 289 |
if (!top_level_object) { |
| 290 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "failed to parse top level object in json document."); |
| 291 |
goto done; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
credentials = aws_parse_credentials_from_aws_json_object( |
| 296 |
allocator, top_level_object ? top_level_object : document_root, options); |
| 297 |
done: |
| 298 |
aws_json_value_destroy(document_root); |
| 299 |
return credentials; |
| 300 |
} |
| 301 |
|
| 302 |
enum aws_retry_error_type aws_credentials_provider_compute_retry_error_type(int response_code, int error_code) { |
| 303 |
|
| 304 |
enum aws_retry_error_type error_type = response_code >= 400 && response_code < 500 |
| 305 |
? AWS_RETRY_ERROR_TYPE_CLIENT_ERROR |
| 306 |
: AWS_RETRY_ERROR_TYPE_SERVER_ERROR; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if (aws_http_error_code_is_retryable(error_code)) { |
| 311 |
error_type = AWS_RETRY_ERROR_TYPE_TRANSIENT; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
if (response_code == AWS_HTTP_STATUS_CODE_429_TOO_MANY_REQUESTS) { |
| 316 |
|
| 317 |
error_type = AWS_RETRY_ERROR_TYPE_THROTTLING; |
| 318 |
} |
| 319 |
|
| 320 |
return error_type; |
| 321 |
} |
| 322 |
|
| 323 |
struct aws_profile_collection *aws_load_profile_collection_from_config_file( |
| 324 |
struct aws_allocator *allocator, |
| 325 |
struct aws_byte_cursor config_file_name_override) { |
| 326 |
|
| 327 |
struct aws_profile_collection *config_profiles = NULL; |
| 328 |
struct aws_string *config_file_path = NULL; |
| 329 |
|
| 330 |
config_file_path = aws_get_config_file_path(allocator, &config_file_name_override); |
| 331 |
if (!config_file_path) { |
| 332 |
AWS_LOGF_ERROR( |
| 333 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 334 |
"Failed to resolve config file path: %s", |
| 335 |
aws_error_str(aws_last_error())); |
| 336 |
return NULL; |
| 337 |
} |
| 338 |
|
| 339 |
config_profiles = aws_profile_collection_new_from_file(allocator, config_file_path, AWS_PST_CONFIG); |
| 340 |
if (config_profiles != NULL) { |
| 341 |
AWS_LOGF_DEBUG( |
| 342 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 343 |
"Successfully built config profile collection from file at (%s)", |
| 344 |
aws_string_c_str(config_file_path)); |
| 345 |
} else { |
| 346 |
AWS_LOGF_ERROR( |
| 347 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 348 |
"Failed to build config profile collection from file at (%s) : %s", |
| 349 |
aws_string_c_str(config_file_path), |
| 350 |
aws_error_str(aws_last_error())); |
| 351 |
} |
| 352 |
|
| 353 |
aws_string_destroy(config_file_path); |
| 354 |
return config_profiles; |
| 355 |
} |
| 356 |
|
| 357 |
static struct aws_byte_cursor s_dot_cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("."); |
| 358 |
|
| 359 |
|
| 360 |
static struct aws_byte_cursor s_aws_dns_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazonaws.com"); |
| 361 |
|
| 362 |
|
| 363 |
static struct aws_byte_cursor s_cn_region_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("cn-"); |
| 364 |
static struct aws_byte_cursor s_aws_cn_dns_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazonaws.com.cn"); |
| 365 |
|
| 366 |
|
| 367 |
static struct aws_byte_cursor s_iso_region_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-iso-"); |
| 368 |
static struct aws_byte_cursor s_aws_iso_dns_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("c2s.ic.gov"); |
| 369 |
|
| 370 |
|
| 371 |
static struct aws_byte_cursor s_isob_region_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-isob-"); |
| 372 |
static struct aws_byte_cursor s_aws_isob_dns_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("sc2s.sgov.gov"); |
| 373 |
|
| 374 |
|
| 375 |
static struct aws_byte_cursor s_isoe_region_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("eu-isoe-"); |
| 376 |
static struct aws_byte_cursor s_aws_isoe_dns_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("cloud.adc-e.uk"); |
| 377 |
|
| 378 |
|
| 379 |
static struct aws_byte_cursor s_isof_region_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("us-isof-"); |
| 380 |
static struct aws_byte_cursor s_aws_isof_dns_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("csp.hci.ic.gov"); |
| 381 |
|
| 382 |
AWS_STATIC_STRING_FROM_LITERAL(s_endpoint_url_env, "AWS_ENDPOINT_URL"); |
| 383 |
AWS_STATIC_STRING_FROM_LITERAL(s_endpoint_url_property, "endpoint_url"); |
| 384 |
AWS_STATIC_STRING_FROM_LITERAL(s_services_property, "services"); |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
struct aws_string *s_get_override_endpoint( |
| 399 |
struct aws_allocator *allocator, |
| 400 |
const struct aws_string *override_service_name_env, |
| 401 |
const struct aws_string *override_service_name_property, |
| 402 |
const struct aws_profile_collection *profile_collection, |
| 403 |
const struct aws_profile *profile) { |
| 404 |
|
| 405 |
struct aws_byte_cursor url_env_cursor = aws_byte_cursor_from_string(s_endpoint_url_env); |
| 406 |
struct aws_byte_cursor underscore_cursor = aws_byte_cursor_from_c_str("_"); |
| 407 |
struct aws_byte_cursor service_name_env_cursor = aws_byte_cursor_from_string(override_service_name_env); |
| 408 |
|
| 409 |
struct aws_string *out_endpoint = NULL; |
| 410 |
struct aws_string *service_endpoint_str = NULL; |
| 411 |
|
| 412 |
struct aws_byte_buf service_endpoint_buf; |
| 413 |
AWS_ZERO_STRUCT(service_endpoint_buf); |
| 414 |
|
| 415 |
aws_byte_buf_init(&service_endpoint_buf, allocator, 10); |
| 416 |
if (aws_byte_buf_append_dynamic(&service_endpoint_buf, &url_env_cursor) || |
| 417 |
aws_byte_buf_append_dynamic(&service_endpoint_buf, &underscore_cursor) || |
| 418 |
aws_byte_buf_append_dynamic(&service_endpoint_buf, &service_name_env_cursor)) { |
| 419 |
goto on_finish; |
| 420 |
} |
| 421 |
service_endpoint_str = aws_string_new_from_buf(allocator, &service_endpoint_buf); |
| 422 |
out_endpoint = aws_get_env_nonempty(allocator, aws_string_c_str(service_endpoint_str)); |
| 423 |
if (out_endpoint) { |
| 424 |
goto on_finish; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
out_endpoint = aws_get_env_nonempty(allocator, aws_string_c_str(s_endpoint_url_env)); |
| 429 |
if (out_endpoint) { |
| 430 |
goto on_finish; |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
if (profile_collection == NULL || profile == NULL) { |
| 435 |
goto on_finish; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
const struct aws_profile_property *property = aws_profile_get_property(profile, s_services_property); |
| 440 |
if (property) { |
| 441 |
const struct aws_profile *services = aws_profile_collection_get_section( |
| 442 |
profile_collection, AWS_PROFILE_SECTION_TYPE_SERVICES, aws_profile_property_get_value(property)); |
| 443 |
if (services) { |
| 444 |
property = aws_profile_get_property(services, override_service_name_property); |
| 445 |
if (property) { |
| 446 |
out_endpoint = aws_string_new_from_string( |
| 447 |
allocator, aws_profile_property_get_sub_property(property, s_endpoint_url_property)); |
| 448 |
goto on_finish; |
| 449 |
} |
| 450 |
} |
| 451 |
goto on_finish; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
property = aws_profile_get_property(profile, s_endpoint_url_property); |
| 456 |
if (property) { |
| 457 |
out_endpoint = aws_string_new_from_string(allocator, aws_profile_property_get_value(property)); |
| 458 |
|
| 459 |
goto on_finish; |
| 460 |
} |
| 461 |
|
| 462 |
on_finish: |
| 463 |
aws_byte_buf_clean_up(&service_endpoint_buf); |
| 464 |
aws_string_destroy(service_endpoint_str); |
| 465 |
return out_endpoint; |
| 466 |
} |
| 467 |
|
| 468 |
int aws_credentials_provider_construct_endpoint( |
| 469 |
struct aws_allocator *allocator, |
| 470 |
struct aws_string **out_endpoint, |
| 471 |
const struct aws_string *region, |
| 472 |
const struct aws_string *service_name_host, |
| 473 |
const struct aws_string *service_name_env, |
| 474 |
const struct aws_string *service_name_property, |
| 475 |
const struct aws_profile_collection *profile_collection, |
| 476 |
const struct aws_profile *profile, |
| 477 |
const struct aws_byte_cursor *suffix_override, |
| 478 |
bool swap_region_and_service_name) { |
| 479 |
|
| 480 |
*out_endpoint = |
| 481 |
s_get_override_endpoint(allocator, service_name_env, service_name_property, profile_collection, profile); |
| 482 |
if (*out_endpoint) { |
| 483 |
return AWS_OP_SUCCESS; |
| 484 |
} |
| 485 |
|
| 486 |
AWS_PRECONDITION(allocator); |
| 487 |
AWS_PRECONDITION(out_endpoint); |
| 488 |
if (!region || !service_name_host) { |
| 489 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 490 |
} |
| 491 |
int result = AWS_OP_ERR; |
| 492 |
|
| 493 |
struct aws_byte_buf endpoint; |
| 494 |
AWS_ZERO_STRUCT(endpoint); |
| 495 |
aws_byte_buf_init(&endpoint, allocator, 10); |
| 496 |
struct aws_byte_cursor service_cursor = aws_byte_cursor_from_string(service_name_host); |
| 497 |
struct aws_byte_cursor region_cursor = aws_byte_cursor_from_string(region); |
| 498 |
|
| 499 |
if (swap_region_and_service_name) { |
| 500 |
if (aws_byte_buf_append_dynamic(&endpoint, ®ion_cursor) || |
| 501 |
aws_byte_buf_append_dynamic(&endpoint, &s_dot_cursor) || |
| 502 |
aws_byte_buf_append_dynamic(&endpoint, &service_cursor) || |
| 503 |
aws_byte_buf_append_dynamic(&endpoint, &s_dot_cursor)) { |
| 504 |
goto on_error; |
| 505 |
} |
| 506 |
} else { |
| 507 |
if (aws_byte_buf_append_dynamic(&endpoint, &service_cursor) || |
| 508 |
aws_byte_buf_append_dynamic(&endpoint, &s_dot_cursor) || |
| 509 |
aws_byte_buf_append_dynamic(&endpoint, ®ion_cursor) || |
| 510 |
aws_byte_buf_append_dynamic(&endpoint, &s_dot_cursor)) { |
| 511 |
goto on_error; |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
const struct aws_byte_cursor region_cur = aws_byte_cursor_from_string(region); |
| 516 |
|
| 517 |
if (suffix_override) { |
| 518 |
if (aws_byte_buf_append_dynamic(&endpoint, suffix_override)) { |
| 519 |
goto on_error; |
| 520 |
} |
| 521 |
} else if (aws_byte_cursor_starts_with(®ion_cur, &s_cn_region_prefix)) { |
| 522 |
if (aws_byte_buf_append_dynamic(&endpoint, &s_aws_cn_dns_suffix)) { |
| 523 |
goto on_error; |
| 524 |
} |
| 525 |
} else if (aws_byte_cursor_starts_with(®ion_cur, &s_iso_region_prefix)) { |
| 526 |
if (aws_byte_buf_append_dynamic(&endpoint, &s_aws_iso_dns_suffix)) { |
| 527 |
goto on_error; |
| 528 |
} |
| 529 |
} else if (aws_byte_cursor_starts_with(®ion_cur, &s_isob_region_prefix)) { |
| 530 |
if (aws_byte_buf_append_dynamic(&endpoint, &s_aws_isob_dns_suffix)) { |
| 531 |
goto on_error; |
| 532 |
} |
| 533 |
} else if (aws_byte_cursor_starts_with(®ion_cur, &s_isoe_region_prefix)) { |
| 534 |
if (aws_byte_buf_append_dynamic(&endpoint, &s_aws_isoe_dns_suffix)) { |
| 535 |
goto on_error; |
| 536 |
} |
| 537 |
} else if (aws_byte_cursor_starts_with(®ion_cur, &s_isof_region_prefix)) { |
| 538 |
if (aws_byte_buf_append_dynamic(&endpoint, &s_aws_isof_dns_suffix)) { |
| 539 |
goto on_error; |
| 540 |
} |
| 541 |
} else { |
| 542 |
if (aws_byte_buf_append_dynamic(&endpoint, &s_aws_dns_suffix)) { |
| 543 |
goto on_error; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
*out_endpoint = aws_string_new_from_buf(allocator, &endpoint); |
| 548 |
result = AWS_OP_SUCCESS; |
| 549 |
|
| 550 |
on_error: |
| 551 |
aws_byte_buf_clean_up(&endpoint); |
| 552 |
if (result != AWS_OP_SUCCESS) { |
| 553 |
*out_endpoint = NULL; |
| 554 |
} |
| 555 |
return result; |
| 556 |
} |
| 557 |
|
| 558 |
AWS_STATIC_STRING_FROM_LITERAL(s_region_env, "AWS_REGION"); |
| 559 |
AWS_STATIC_STRING_FROM_LITERAL(s_default_region_env, "AWS_DEFAULT_REGION"); |
| 560 |
|
| 561 |
struct aws_string *aws_credentials_provider_resolve_region_from_env(struct aws_allocator *allocator) { |
| 562 |
|
| 563 |
struct aws_string *region = aws_get_env_nonempty(allocator, aws_string_c_str(s_region_env)); |
| 564 |
if (region != NULL) { |
| 565 |
return region; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
return aws_get_env_nonempty(allocator, aws_string_c_str(s_default_region_env)); |
| 570 |
} |
| 571 |
|
| 572 |
struct aws_byte_cursor aws_parse_account_id_from_arn(struct aws_byte_cursor arn) { |
| 573 |
struct aws_byte_cursor account_id; |
| 574 |
AWS_ZERO_STRUCT(account_id); |
| 575 |
|
| 576 |
|
| 577 |
for (int i = 0; i < 5; i++) { |
| 578 |
if (!aws_byte_cursor_next_split(&arn, ':', &account_id)) { |
| 579 |
AWS_LOGF_ERROR( |
| 580 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 581 |
"Failed to parse account_id string from STS xml response: %s", |
| 582 |
aws_error_str(aws_last_error())); |
| 583 |
struct aws_byte_cursor empty; |
| 584 |
AWS_ZERO_STRUCT(empty); |
| 585 |
return empty; |
| 586 |
} |
| 587 |
} |
| 588 |
return account_id; |
| 589 |
} |