| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
#include <aws/auth/credentials.h> |
| 9 |
#include <aws/auth/private/credentials_utils.h> |
| 10 |
#include <aws/common/condition_variable.h> |
| 11 |
#include <aws/common/environment.h> |
| 12 |
#include <aws/common/json.h> |
| 13 |
#include <aws/common/string.h> |
| 14 |
#include <aws/http/request_response.h> |
| 15 |
#include <aws/io/channel_bootstrap.h> |
| 16 |
#include <aws/io/event_loop.h> |
| 17 |
#include <aws/io/socket.h> |
| 18 |
#include <aws/io/tls_channel_handler.h> |
| 19 |
|
| 20 |
#include "aws/io/stream.h" |
| 21 |
|
| 22 |
struct aws_mock_web_credential_provider_tester { |
| 23 |
struct aws_allocator *allocator; |
| 24 |
struct aws_byte_buf request_uri; |
| 25 |
|
| 26 |
struct aws_array_list response_data_callbacks; |
| 27 |
bool is_connection_acquire_successful; |
| 28 |
bool is_request_successful; |
| 29 |
|
| 30 |
struct aws_mutex lock; |
| 31 |
struct aws_condition_variable signal; |
| 32 |
|
| 33 |
struct aws_credentials *credentials; |
| 34 |
bool has_received_credentials_callback; |
| 35 |
bool has_received_shutdown_callback; |
| 36 |
int error_code; |
| 37 |
|
| 38 |
struct aws_tls_ctx *ctx; |
| 39 |
struct aws_tls_connection_options tls_connection_options; |
| 40 |
|
| 41 |
struct aws_event_loop_group *el_group; |
| 42 |
struct aws_host_resolver *resolver; |
| 43 |
struct aws_client_bootstrap *bootstrap; |
| 44 |
|
| 45 |
struct aws_http_connection_manager *mock_manager; |
| 46 |
struct aws_http_connection *mock_connection; |
| 47 |
struct aws_http_stream *mock_stream; |
| 48 |
|
| 49 |
size_t current_request_attempt_number; |
| 50 |
struct aws_http_make_request_options request_callback_options; |
| 51 |
|
| 52 |
struct aws_byte_buf request_body; |
| 53 |
|
| 54 |
void (*manager_destructor_fn)(void *); |
| 55 |
void *manager_destructor_user_data; |
| 56 |
|
| 57 |
struct proxy_env_var_settings *proxy_config; |
| 58 |
}; |
| 59 |
|
| 60 |
static struct aws_mock_web_credential_provider_tester s_tester; |
| 61 |
|
| 62 |
static struct aws_http_connection_manager *s_aws_http_connection_manager_new_mock( |
| 63 |
struct aws_allocator *allocator, |
| 64 |
const struct aws_http_connection_manager_options *options) { |
| 65 |
|
| 66 |
(void)allocator; |
| 67 |
(void)options; |
| 68 |
|
| 69 |
if (s_tester.proxy_config != NULL) { |
| 70 |
AWS_FATAL_ASSERT(options->proxy_ev_settings->env_var_type == s_tester.proxy_config->env_var_type); |
| 71 |
AWS_FATAL_ASSERT(options->proxy_ev_settings->connection_type == s_tester.proxy_config->connection_type); |
| 72 |
AWS_FATAL_ASSERT(options->proxy_ev_settings->tls_options == s_tester.proxy_config->tls_options); |
| 73 |
} |
| 74 |
|
| 75 |
s_tester.manager_destructor_fn = options->shutdown_complete_callback; |
| 76 |
s_tester.manager_destructor_user_data = options->shutdown_complete_user_data; |
| 77 |
|
| 78 |
return s_tester.mock_manager; |
| 79 |
} |
| 80 |
|
| 81 |
static void s_aws_http_connection_manager_release_mock(struct aws_http_connection_manager *manager) { |
| 82 |
(void)manager; |
| 83 |
|
| 84 |
s_tester.manager_destructor_fn(s_tester.manager_destructor_user_data); |
| 85 |
} |
| 86 |
|
| 87 |
static void s_aws_http_connection_manager_acquire_connection_mock( |
| 88 |
struct aws_http_connection_manager *manager, |
| 89 |
aws_http_connection_manager_on_connection_setup_fn *callback, |
| 90 |
void *user_data) { |
| 91 |
|
| 92 |
(void)manager; |
| 93 |
(void)callback; |
| 94 |
(void)user_data; |
| 95 |
|
| 96 |
if (s_tester.is_connection_acquire_successful) { |
| 97 |
callback(s_tester.mock_connection, AWS_ERROR_SUCCESS, user_data); |
| 98 |
} else { |
| 99 |
aws_raise_error(AWS_ERROR_HTTP_UNKNOWN); |
| 100 |
callback(NULL, AWS_ERROR_HTTP_UNKNOWN, user_data); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static int s_aws_http_connection_manager_release_connection_mock( |
| 105 |
struct aws_http_connection_manager *manager, |
| 106 |
struct aws_http_connection *connection) { |
| 107 |
|
| 108 |
(void)manager; |
| 109 |
(void)connection; |
| 110 |
|
| 111 |
return AWS_OP_SUCCESS; |
| 112 |
} |
| 113 |
|
| 114 |
static void s_invoke_mock_request_callbacks( |
| 115 |
const struct aws_http_make_request_options *options, |
| 116 |
struct aws_array_list *data_callbacks, |
| 117 |
bool is_request_successful) { |
| 118 |
|
| 119 |
size_t data_callback_count = aws_array_list_length(data_callbacks); |
| 120 |
|
| 121 |
struct aws_http_header headers[1]; |
| 122 |
AWS_ZERO_ARRAY(headers); |
| 123 |
|
| 124 |
headers[0].name = aws_byte_cursor_from_c_str("some-header"); |
| 125 |
headers[0].value = aws_byte_cursor_from_c_str("value"); |
| 126 |
|
| 127 |
if (options->on_response_headers) { |
| 128 |
options->on_response_headers(s_tester.mock_stream, AWS_HTTP_HEADER_BLOCK_MAIN, headers, 1, options->user_data); |
| 129 |
} |
| 130 |
|
| 131 |
if (options->on_response_header_block_done) { |
| 132 |
options->on_response_header_block_done(s_tester.mock_stream, data_callback_count > 0, options->user_data); |
| 133 |
} |
| 134 |
|
| 135 |
size_t response_count = aws_array_list_length(&s_tester.response_data_callbacks); |
| 136 |
if (response_count > 0) { |
| 137 |
size_t response_body_index = aws_min_size(s_tester.current_request_attempt_number, response_count - 1); |
| 138 |
struct aws_byte_cursor data_callback_cursor; |
| 139 |
aws_array_list_get_at(data_callbacks, &data_callback_cursor, response_body_index); |
| 140 |
options->on_response_body(s_tester.mock_stream, &data_callback_cursor, options->user_data); |
| 141 |
} |
| 142 |
|
| 143 |
++s_tester.current_request_attempt_number; |
| 144 |
|
| 145 |
options->on_complete( |
| 146 |
s_tester.mock_stream, is_request_successful ? AWS_ERROR_SUCCESS : AWS_ERROR_HTTP_UNKNOWN, options->user_data); |
| 147 |
} |
| 148 |
|
| 149 |
static struct aws_http_stream *s_aws_http_connection_make_request_mock( |
| 150 |
struct aws_http_connection *client_connection, |
| 151 |
const struct aws_http_make_request_options *options) { |
| 152 |
|
| 153 |
(void)client_connection; |
| 154 |
(void)options; |
| 155 |
|
| 156 |
struct aws_byte_cursor path; |
| 157 |
AWS_ZERO_STRUCT(path); |
| 158 |
aws_http_message_get_request_path(options->request, &path); |
| 159 |
|
| 160 |
aws_byte_buf_append_dynamic(&s_tester.request_uri, &path); |
| 161 |
s_tester.request_callback_options = *options; |
| 162 |
|
| 163 |
aws_byte_buf_reset(&s_tester.request_body, false); |
| 164 |
|
| 165 |
struct aws_input_stream *body_stream = aws_http_message_get_body_stream(options->request); |
| 166 |
if (body_stream != NULL) { |
| 167 |
aws_input_stream_seek(body_stream, 0, AWS_SSB_BEGIN); |
| 168 |
|
| 169 |
int64_t body_length = 0; |
| 170 |
aws_input_stream_get_length(body_stream, &body_length); |
| 171 |
|
| 172 |
AWS_LOGF_DEBUG(AWS_LS_AUTH_GENERAL, "Request body length: %d", (int)body_length); |
| 173 |
|
| 174 |
aws_byte_buf_reserve(&s_tester.request_body, (size_t)body_length); |
| 175 |
|
| 176 |
aws_input_stream_read(body_stream, &s_tester.request_body); |
| 177 |
} |
| 178 |
|
| 179 |
return s_tester.mock_stream; |
| 180 |
} |
| 181 |
|
| 182 |
static int s_aws_http_stream_activate_mock(struct aws_http_stream *stream) { |
| 183 |
(void)stream; |
| 184 |
|
| 185 |
s_invoke_mock_request_callbacks( |
| 186 |
&s_tester.request_callback_options, &s_tester.response_data_callbacks, s_tester.is_request_successful); |
| 187 |
return AWS_OP_SUCCESS; |
| 188 |
} |
| 189 |
|
| 190 |
static int s_aws_http_stream_get_incoming_response_status_mock( |
| 191 |
const struct aws_http_stream *stream, |
| 192 |
int *out_status_code) { |
| 193 |
(void)stream; |
| 194 |
|
| 195 |
if (s_tester.is_request_successful) { |
| 196 |
*out_status_code = 200; |
| 197 |
} else { |
| 198 |
*out_status_code = 400; |
| 199 |
} |
| 200 |
|
| 201 |
return AWS_OP_SUCCESS; |
| 202 |
} |
| 203 |
|
| 204 |
static void s_aws_http_stream_release_mock(struct aws_http_stream *stream) { |
| 205 |
(void)stream; |
| 206 |
} |
| 207 |
|
| 208 |
static void s_aws_http_connection_close_mock(struct aws_http_connection *connection) { |
| 209 |
(void)connection; |
| 210 |
} |
| 211 |
|
| 212 |
static struct aws_auth_http_system_vtable s_mock_function_table = { |
| 213 |
.aws_http_connection_manager_new = s_aws_http_connection_manager_new_mock, |
| 214 |
.aws_http_connection_manager_release = s_aws_http_connection_manager_release_mock, |
| 215 |
.aws_http_connection_manager_acquire_connection = s_aws_http_connection_manager_acquire_connection_mock, |
| 216 |
.aws_http_connection_manager_release_connection = s_aws_http_connection_manager_release_connection_mock, |
| 217 |
.aws_http_connection_make_request = s_aws_http_connection_make_request_mock, |
| 218 |
.aws_http_stream_activate = s_aws_http_stream_activate_mock, |
| 219 |
.aws_http_stream_get_incoming_response_status = s_aws_http_stream_get_incoming_response_status_mock, |
| 220 |
.aws_http_stream_release = s_aws_http_stream_release_mock, |
| 221 |
.aws_http_connection_close = s_aws_http_connection_close_mock}; |
| 222 |
|
| 223 |
static int s_aws_cognito_tester_init(struct aws_allocator *allocator) { |
| 224 |
aws_auth_library_init(allocator); |
| 225 |
|
| 226 |
s_tester.allocator = allocator; |
| 227 |
|
| 228 |
if (aws_array_list_init_dynamic(&s_tester.response_data_callbacks, allocator, 10, sizeof(struct aws_byte_cursor))) { |
| 229 |
return AWS_OP_ERR; |
| 230 |
} |
| 231 |
|
| 232 |
if (aws_byte_buf_init(&s_tester.request_uri, allocator, 100)) { |
| 233 |
return AWS_OP_ERR; |
| 234 |
} |
| 235 |
|
| 236 |
if (aws_mutex_init(&s_tester.lock)) { |
| 237 |
return AWS_OP_ERR; |
| 238 |
} |
| 239 |
|
| 240 |
if (aws_condition_variable_init(&s_tester.signal)) { |
| 241 |
return AWS_OP_ERR; |
| 242 |
} |
| 243 |
|
| 244 |
s_tester.el_group = aws_event_loop_group_new_default(allocator, 0, NULL); |
| 245 |
|
| 246 |
struct aws_host_resolver_default_options resolver_options = { |
| 247 |
.el_group = s_tester.el_group, |
| 248 |
.max_entries = 8, |
| 249 |
}; |
| 250 |
s_tester.resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 251 |
|
| 252 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 253 |
.event_loop_group = s_tester.el_group, |
| 254 |
.host_resolver = s_tester.resolver, |
| 255 |
}; |
| 256 |
s_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 257 |
|
| 258 |
AWS_ZERO_STRUCT(s_tester.tls_connection_options); |
| 259 |
struct aws_tls_ctx_options tls_options; |
| 260 |
aws_tls_ctx_options_init_default_client(&tls_options, allocator); |
| 261 |
s_tester.ctx = aws_tls_client_ctx_new(allocator, &tls_options); |
| 262 |
ASSERT_NOT_NULL(s_tester.ctx); |
| 263 |
aws_tls_ctx_options_clean_up(&tls_options); |
| 264 |
aws_tls_connection_options_init_from_ctx(&s_tester.tls_connection_options, s_tester.ctx); |
| 265 |
|
| 266 |
|
| 267 |
s_tester.is_connection_acquire_successful = true; |
| 268 |
s_tester.is_request_successful = true; |
| 269 |
|
| 270 |
|
| 271 |
s_tester.mock_manager = (void *)(&s_tester.mock_manager); |
| 272 |
s_tester.mock_connection = (void *)(&s_tester.mock_connection); |
| 273 |
s_tester.mock_stream = (void *)(&s_tester.mock_stream); |
| 274 |
|
| 275 |
aws_byte_buf_init(&s_tester.request_body, allocator, 100); |
| 276 |
|
| 277 |
return AWS_OP_SUCCESS; |
| 278 |
} |
| 279 |
|
| 280 |
static void s_aws_cognito_tester_cleanup(void) { |
| 281 |
aws_array_list_clean_up(&s_tester.response_data_callbacks); |
| 282 |
aws_byte_buf_clean_up(&s_tester.request_uri); |
| 283 |
aws_condition_variable_clean_up(&s_tester.signal); |
| 284 |
aws_mutex_clean_up(&s_tester.lock); |
| 285 |
aws_credentials_release(s_tester.credentials); |
| 286 |
aws_byte_buf_clean_up(&s_tester.request_body); |
| 287 |
|
| 288 |
aws_client_bootstrap_release(s_tester.bootstrap); |
| 289 |
aws_host_resolver_release(s_tester.resolver); |
| 290 |
aws_event_loop_group_release(s_tester.el_group); |
| 291 |
|
| 292 |
aws_tls_ctx_release(s_tester.ctx); |
| 293 |
s_tester.ctx = NULL; |
| 294 |
|
| 295 |
aws_tls_connection_options_clean_up(&s_tester.tls_connection_options); |
| 296 |
aws_auth_library_clean_up(); |
| 297 |
} |
| 298 |
|
| 299 |
static bool s_has_tester_received_credentials_callback(void *user_data) { |
| 300 |
(void)user_data; |
| 301 |
|
| 302 |
return s_tester.has_received_credentials_callback; |
| 303 |
} |
| 304 |
|
| 305 |
static void s_aws_wait_for_credentials_result(void) { |
| 306 |
aws_mutex_lock(&s_tester.lock); |
| 307 |
aws_condition_variable_wait_pred( |
| 308 |
&s_tester.signal, &s_tester.lock, s_has_tester_received_credentials_callback, NULL); |
| 309 |
aws_mutex_unlock(&s_tester.lock); |
| 310 |
} |
| 311 |
|
| 312 |
static void s_get_credentials_callback(struct aws_credentials *credentials, int error_code, void *user_data) { |
| 313 |
(void)user_data; |
| 314 |
|
| 315 |
aws_mutex_lock(&s_tester.lock); |
| 316 |
s_tester.has_received_credentials_callback = true; |
| 317 |
s_tester.error_code = error_code; |
| 318 |
s_tester.credentials = credentials; |
| 319 |
aws_credentials_acquire(credentials); |
| 320 |
aws_condition_variable_notify_one(&s_tester.signal); |
| 321 |
aws_mutex_unlock(&s_tester.lock); |
| 322 |
} |
| 323 |
|
| 324 |
static int s_credentials_provider_cognito_new_destroy(struct aws_allocator *allocator, void *ctx) { |
| 325 |
(void)ctx; |
| 326 |
|
| 327 |
s_aws_cognito_tester_init(allocator); |
| 328 |
|
| 329 |
struct aws_credentials_provider_cognito_options options = { |
| 330 |
.bootstrap = s_tester.bootstrap, |
| 331 |
.function_table = &s_mock_function_table, |
| 332 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 333 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 334 |
.tls_ctx = s_tester.ctx, |
| 335 |
}; |
| 336 |
|
| 337 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 338 |
ASSERT_NOT_NULL(provider); |
| 339 |
aws_credentials_provider_release(provider); |
| 340 |
|
| 341 |
s_aws_cognito_tester_cleanup(); |
| 342 |
|
| 343 |
return AWS_OP_SUCCESS; |
| 344 |
} |
| 345 |
|
| 346 |
AWS_TEST_CASE(credentials_provider_cognito_new_destroy, s_credentials_provider_cognito_new_destroy); |
| 347 |
|
| 348 |
static int s_credentials_provider_cognito_failure_connect_fn(struct aws_allocator *allocator, void *ctx) { |
| 349 |
(void)ctx; |
| 350 |
|
| 351 |
s_aws_cognito_tester_init(allocator); |
| 352 |
s_tester.is_connection_acquire_successful = false; |
| 353 |
|
| 354 |
struct aws_credentials_provider_cognito_options options = { |
| 355 |
.bootstrap = s_tester.bootstrap, |
| 356 |
.function_table = &s_mock_function_table, |
| 357 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 358 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 359 |
.tls_ctx = s_tester.ctx, |
| 360 |
}; |
| 361 |
|
| 362 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 363 |
ASSERT_NOT_NULL(provider); |
| 364 |
|
| 365 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 366 |
|
| 367 |
s_aws_wait_for_credentials_result(); |
| 368 |
|
| 369 |
aws_mutex_lock(&s_tester.lock); |
| 370 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 371 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_HTTP_UNKNOWN); |
| 372 |
ASSERT_TRUE(s_tester.credentials == NULL); |
| 373 |
aws_mutex_unlock(&s_tester.lock); |
| 374 |
|
| 375 |
aws_credentials_provider_release(provider); |
| 376 |
|
| 377 |
s_aws_cognito_tester_cleanup(); |
| 378 |
|
| 379 |
return AWS_OP_SUCCESS; |
| 380 |
} |
| 381 |
|
| 382 |
AWS_TEST_CASE(credentials_provider_cognito_failure_connect, s_credentials_provider_cognito_failure_connect_fn); |
| 383 |
|
| 384 |
static int s_credentials_provider_cognito_failure_request_fn(struct aws_allocator *allocator, void *ctx) { |
| 385 |
(void)ctx; |
| 386 |
|
| 387 |
s_aws_cognito_tester_init(allocator); |
| 388 |
s_tester.is_request_successful = false; |
| 389 |
|
| 390 |
struct aws_credentials_provider_cognito_options options = { |
| 391 |
.bootstrap = s_tester.bootstrap, |
| 392 |
.function_table = &s_mock_function_table, |
| 393 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 394 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 395 |
.tls_ctx = s_tester.ctx, |
| 396 |
}; |
| 397 |
|
| 398 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 399 |
ASSERT_NOT_NULL(provider); |
| 400 |
|
| 401 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 402 |
|
| 403 |
s_aws_wait_for_credentials_result(); |
| 404 |
|
| 405 |
aws_mutex_lock(&s_tester.lock); |
| 406 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 407 |
ASSERT_TRUE(s_tester.error_code == AWS_AUTH_CREDENTIALS_PROVIDER_HTTP_STATUS_FAILURE); |
| 408 |
ASSERT_TRUE(s_tester.credentials == NULL); |
| 409 |
aws_mutex_unlock(&s_tester.lock); |
| 410 |
|
| 411 |
aws_credentials_provider_release(provider); |
| 412 |
|
| 413 |
s_aws_cognito_tester_cleanup(); |
| 414 |
|
| 415 |
return AWS_OP_SUCCESS; |
| 416 |
} |
| 417 |
|
| 418 |
AWS_TEST_CASE(credentials_provider_cognito_failure_request, s_credentials_provider_cognito_failure_request_fn); |
| 419 |
|
| 420 |
AWS_STATIC_STRING_FROM_LITERAL(s_bad_document_response, "{\"NotTheExpectedDocumentFormat\":\"Error\"}"); |
| 421 |
|
| 422 |
static int s_credentials_provider_cognito_failure_bad_document_fn(struct aws_allocator *allocator, void *ctx) { |
| 423 |
(void)ctx; |
| 424 |
|
| 425 |
s_aws_cognito_tester_init(allocator); |
| 426 |
|
| 427 |
struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_bad_document_response); |
| 428 |
aws_array_list_push_back(&s_tester.response_data_callbacks, &bad_document_cursor); |
| 429 |
|
| 430 |
struct aws_credentials_provider_cognito_options options = { |
| 431 |
.bootstrap = s_tester.bootstrap, |
| 432 |
.function_table = &s_mock_function_table, |
| 433 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 434 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 435 |
.tls_ctx = s_tester.ctx, |
| 436 |
}; |
| 437 |
|
| 438 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 439 |
ASSERT_NOT_NULL(provider); |
| 440 |
|
| 441 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 442 |
|
| 443 |
s_aws_wait_for_credentials_result(); |
| 444 |
|
| 445 |
aws_mutex_lock(&s_tester.lock); |
| 446 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 447 |
ASSERT_TRUE(s_tester.error_code != AWS_ERROR_SUCCESS); |
| 448 |
ASSERT_TRUE(s_tester.credentials == NULL); |
| 449 |
aws_mutex_unlock(&s_tester.lock); |
| 450 |
|
| 451 |
aws_credentials_provider_release(provider); |
| 452 |
|
| 453 |
s_aws_cognito_tester_cleanup(); |
| 454 |
|
| 455 |
return AWS_OP_SUCCESS; |
| 456 |
} |
| 457 |
|
| 458 |
AWS_TEST_CASE( |
| 459 |
credentials_provider_cognito_failure_bad_document, |
| 460 |
s_credentials_provider_cognito_failure_bad_document_fn); |
| 461 |
|
| 462 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 463 |
s_good_document_response, |
| 464 |
"{\"Credentials\":{\"AccessKeyId\":\"SomeAccessKeyIdValue\",\"SecretKey\":\"SomeSecretKeyValue\",\"SessionToken\":" |
| 465 |
"\"SomeSessionTokenValue\",\"Expiration\":1663003154}}"); |
| 466 |
|
| 467 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_access_key_id, "SomeAccessKeyIdValue"); |
| 468 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_secret_access_key, "SomeSecretKeyValue"); |
| 469 |
AWS_STATIC_STRING_FROM_LITERAL(s_expected_session_token, "SomeSessionTokenValue"); |
| 470 |
|
| 471 |
static int s_verify_credentials(struct aws_credentials *credentials) { |
| 472 |
struct aws_byte_cursor access_key_id = aws_credentials_get_access_key_id(credentials); |
| 473 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 474 |
s_expected_access_key_id->bytes, s_expected_access_key_id->len, access_key_id.ptr, access_key_id.len); |
| 475 |
|
| 476 |
struct aws_byte_cursor secret_access_key = aws_credentials_get_secret_access_key(credentials); |
| 477 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 478 |
s_expected_secret_access_key->bytes, |
| 479 |
s_expected_secret_access_key->len, |
| 480 |
secret_access_key.ptr, |
| 481 |
secret_access_key.len); |
| 482 |
|
| 483 |
struct aws_byte_cursor session_token = aws_credentials_get_session_token(credentials); |
| 484 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 485 |
s_expected_session_token->bytes, s_expected_session_token->len, session_token.ptr, session_token.len); |
| 486 |
|
| 487 |
uint64_t expiration = aws_credentials_get_expiration_timepoint_seconds(credentials); |
| 488 |
ASSERT_TRUE(expiration > 0); |
| 489 |
|
| 490 |
return AWS_OP_SUCCESS; |
| 491 |
} |
| 492 |
|
| 493 |
static int s_credentials_provider_cognito_success_fn(struct aws_allocator *allocator, void *ctx) { |
| 494 |
(void)ctx; |
| 495 |
|
| 496 |
s_aws_cognito_tester_init(allocator); |
| 497 |
|
| 498 |
struct aws_byte_cursor good_document_cursor = aws_byte_cursor_from_string(s_good_document_response); |
| 499 |
aws_array_list_push_back(&s_tester.response_data_callbacks, &good_document_cursor); |
| 500 |
|
| 501 |
struct aws_credentials_provider_cognito_options options = { |
| 502 |
.bootstrap = s_tester.bootstrap, |
| 503 |
.function_table = &s_mock_function_table, |
| 504 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 505 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 506 |
.tls_ctx = s_tester.ctx, |
| 507 |
}; |
| 508 |
|
| 509 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 510 |
ASSERT_NOT_NULL(provider); |
| 511 |
|
| 512 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 513 |
|
| 514 |
s_aws_wait_for_credentials_result(); |
| 515 |
|
| 516 |
aws_mutex_lock(&s_tester.lock); |
| 517 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 518 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_SUCCESS); |
| 519 |
ASSERT_TRUE(s_tester.credentials != NULL); |
| 520 |
ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials)); |
| 521 |
aws_mutex_unlock(&s_tester.lock); |
| 522 |
|
| 523 |
aws_credentials_provider_release(provider); |
| 524 |
|
| 525 |
s_aws_cognito_tester_cleanup(); |
| 526 |
|
| 527 |
return AWS_OP_SUCCESS; |
| 528 |
} |
| 529 |
|
| 530 |
AWS_TEST_CASE(credentials_provider_cognito_success, s_credentials_provider_cognito_success_fn); |
| 531 |
|
| 532 |
static int s_credentials_provider_cognito_success_after_retry_fn(struct aws_allocator *allocator, void *ctx) { |
| 533 |
(void)ctx; |
| 534 |
|
| 535 |
s_aws_cognito_tester_init(allocator); |
| 536 |
|
| 537 |
|
| 538 |
struct aws_byte_cursor bad_document_cursor = aws_byte_cursor_from_string(s_bad_document_response); |
| 539 |
aws_array_list_push_back(&s_tester.response_data_callbacks, &bad_document_cursor); |
| 540 |
struct aws_byte_cursor good_document_cursor = aws_byte_cursor_from_string(s_good_document_response); |
| 541 |
aws_array_list_push_back(&s_tester.response_data_callbacks, &good_document_cursor); |
| 542 |
|
| 543 |
struct aws_credentials_provider_cognito_options options = { |
| 544 |
.bootstrap = s_tester.bootstrap, |
| 545 |
.function_table = &s_mock_function_table, |
| 546 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 547 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 548 |
.tls_ctx = s_tester.ctx, |
| 549 |
}; |
| 550 |
|
| 551 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 552 |
ASSERT_NOT_NULL(provider); |
| 553 |
|
| 554 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 555 |
|
| 556 |
s_aws_wait_for_credentials_result(); |
| 557 |
|
| 558 |
aws_mutex_lock(&s_tester.lock); |
| 559 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 560 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_SUCCESS); |
| 561 |
ASSERT_TRUE(s_tester.credentials != NULL); |
| 562 |
ASSERT_SUCCESS(s_verify_credentials(s_tester.credentials)); |
| 563 |
aws_mutex_unlock(&s_tester.lock); |
| 564 |
|
| 565 |
aws_credentials_provider_release(provider); |
| 566 |
|
| 567 |
s_aws_cognito_tester_cleanup(); |
| 568 |
|
| 569 |
return AWS_OP_SUCCESS; |
| 570 |
} |
| 571 |
|
| 572 |
AWS_TEST_CASE(credentials_provider_cognito_success_after_retry, s_credentials_provider_cognito_success_after_retry_fn); |
| 573 |
|
| 574 |
AWS_STATIC_STRING_FROM_LITERAL(s_cognito_identity_environment_variable, "AWS_TESTING_COGNITO_IDENTITY"); |
| 575 |
|
| 576 |
static int s_credentials_provider_cognito_success_unauthenticated_fn(struct aws_allocator *allocator, void *ctx) { |
| 577 |
(void)ctx; |
| 578 |
|
| 579 |
s_aws_cognito_tester_init(allocator); |
| 580 |
|
| 581 |
struct aws_string *identity = NULL; |
| 582 |
ASSERT_SUCCESS(aws_get_environment_value(allocator, s_cognito_identity_environment_variable, &identity)); |
| 583 |
ASSERT_NOT_NULL(identity); |
| 584 |
|
| 585 |
struct aws_credentials_provider_cognito_options options = { |
| 586 |
.bootstrap = s_tester.bootstrap, |
| 587 |
.endpoint = aws_byte_cursor_from_c_str("cognito-identity.us-east-1.amazonaws.com"), |
| 588 |
.identity = aws_byte_cursor_from_string(identity), |
| 589 |
.tls_ctx = s_tester.ctx, |
| 590 |
}; |
| 591 |
|
| 592 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 593 |
ASSERT_NOT_NULL(provider); |
| 594 |
|
| 595 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 596 |
|
| 597 |
s_aws_wait_for_credentials_result(); |
| 598 |
|
| 599 |
aws_mutex_lock(&s_tester.lock); |
| 600 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 601 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_SUCCESS); |
| 602 |
ASSERT_TRUE(s_tester.credentials != NULL); |
| 603 |
aws_mutex_unlock(&s_tester.lock); |
| 604 |
|
| 605 |
aws_credentials_provider_release(provider); |
| 606 |
|
| 607 |
s_aws_cognito_tester_cleanup(); |
| 608 |
|
| 609 |
aws_string_destroy(identity); |
| 610 |
|
| 611 |
return AWS_OP_SUCCESS; |
| 612 |
} |
| 613 |
|
| 614 |
AWS_TEST_CASE( |
| 615 |
credentials_provider_cognito_success_unauthenticated, |
| 616 |
s_credentials_provider_cognito_success_unauthenticated_fn); |
| 617 |
|
| 618 |
static int s_get_token_pairs_sync_failure( |
| 619 |
void *get_token_pairs_user_data, |
| 620 |
aws_credentials_provider_cognito_get_token_pairs_completion_fn *completion_callback, |
| 621 |
void *completion_user_data) { |
| 622 |
|
| 623 |
(void)get_token_pairs_user_data; |
| 624 |
(void)completion_callback; |
| 625 |
(void)completion_user_data; |
| 626 |
|
| 627 |
return aws_raise_error(AWS_ERROR_UNKNOWN); |
| 628 |
} |
| 629 |
|
| 630 |
static int s_credentials_provider_cognito_failure_dynamic_token_pairs_fn(struct aws_allocator *allocator, void *ctx) { |
| 631 |
(void)ctx; |
| 632 |
|
| 633 |
s_aws_cognito_tester_init(allocator); |
| 634 |
|
| 635 |
struct aws_credentials_provider_cognito_options options = { |
| 636 |
.bootstrap = s_tester.bootstrap, |
| 637 |
.function_table = &s_mock_function_table, |
| 638 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 639 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 640 |
.tls_ctx = s_tester.ctx, |
| 641 |
.get_token_pairs = s_get_token_pairs_sync_failure, |
| 642 |
}; |
| 643 |
|
| 644 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 645 |
ASSERT_NOT_NULL(provider); |
| 646 |
|
| 647 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 648 |
|
| 649 |
s_aws_wait_for_credentials_result(); |
| 650 |
|
| 651 |
aws_mutex_lock(&s_tester.lock); |
| 652 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 653 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_UNKNOWN); |
| 654 |
ASSERT_TRUE(s_tester.credentials == NULL); |
| 655 |
aws_mutex_unlock(&s_tester.lock); |
| 656 |
|
| 657 |
aws_credentials_provider_release(provider); |
| 658 |
|
| 659 |
s_aws_cognito_tester_cleanup(); |
| 660 |
|
| 661 |
return AWS_OP_SUCCESS; |
| 662 |
} |
| 663 |
|
| 664 |
AWS_TEST_CASE( |
| 665 |
credentials_provider_cognito_failure_dynamic_token_pairs, |
| 666 |
s_credentials_provider_cognito_failure_dynamic_token_pairs_fn); |
| 667 |
|
| 668 |
static int s_get_token_pairs_completion_failure( |
| 669 |
void *get_token_pairs_user_data, |
| 670 |
aws_credentials_provider_cognito_get_token_pairs_completion_fn *completion_callback, |
| 671 |
void *completion_user_data) { |
| 672 |
|
| 673 |
(void)get_token_pairs_user_data; |
| 674 |
|
| 675 |
completion_callback(NULL, 0, AWS_ERROR_UNKNOWN, completion_user_data); |
| 676 |
|
| 677 |
return AWS_OP_SUCCESS; |
| 678 |
} |
| 679 |
|
| 680 |
static int s_credentials_provider_cognito_failure_dynamic_token_pairs_completion_fn( |
| 681 |
struct aws_allocator *allocator, |
| 682 |
void *ctx) { |
| 683 |
(void)ctx; |
| 684 |
|
| 685 |
s_aws_cognito_tester_init(allocator); |
| 686 |
|
| 687 |
struct aws_credentials_provider_cognito_options options = { |
| 688 |
.bootstrap = s_tester.bootstrap, |
| 689 |
.function_table = &s_mock_function_table, |
| 690 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 691 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 692 |
.tls_ctx = s_tester.ctx, |
| 693 |
.get_token_pairs = s_get_token_pairs_completion_failure, |
| 694 |
}; |
| 695 |
|
| 696 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 697 |
ASSERT_NOT_NULL(provider); |
| 698 |
|
| 699 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 700 |
|
| 701 |
s_aws_wait_for_credentials_result(); |
| 702 |
|
| 703 |
aws_mutex_lock(&s_tester.lock); |
| 704 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 705 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_UNKNOWN); |
| 706 |
ASSERT_TRUE(s_tester.credentials == NULL); |
| 707 |
aws_mutex_unlock(&s_tester.lock); |
| 708 |
|
| 709 |
aws_credentials_provider_release(provider); |
| 710 |
|
| 711 |
s_aws_cognito_tester_cleanup(); |
| 712 |
|
| 713 |
return AWS_OP_SUCCESS; |
| 714 |
} |
| 715 |
|
| 716 |
AWS_TEST_CASE( |
| 717 |
credentials_provider_cognito_failure_dynamic_token_pairs_completion, |
| 718 |
s_credentials_provider_cognito_failure_dynamic_token_pairs_completion_fn); |
| 719 |
|
| 720 |
static int s_get_token_pairs_success( |
| 721 |
void *get_token_pairs_user_data, |
| 722 |
aws_credentials_provider_cognito_get_token_pairs_completion_fn *completion_callback, |
| 723 |
void *completion_user_data) { |
| 724 |
|
| 725 |
(void)get_token_pairs_user_data; |
| 726 |
|
| 727 |
struct aws_cognito_identity_provider_token_pair pairs[] = { |
| 728 |
{ |
| 729 |
.identity_provider_name = aws_byte_cursor_from_c_str("dynamic-name"), |
| 730 |
.identity_provider_token = aws_byte_cursor_from_c_str("dynamic-value"), |
| 731 |
}, |
| 732 |
}; |
| 733 |
|
| 734 |
completion_callback(pairs, AWS_ARRAY_SIZE(pairs), AWS_ERROR_SUCCESS, completion_user_data); |
| 735 |
|
| 736 |
return AWS_OP_SUCCESS; |
| 737 |
} |
| 738 |
|
| 739 |
static int s_credentials_provider_cognito_success_dynamic_token_pairs_fn(struct aws_allocator *allocator, void *ctx) { |
| 740 |
(void)ctx; |
| 741 |
|
| 742 |
s_aws_cognito_tester_init(allocator); |
| 743 |
|
| 744 |
struct aws_byte_cursor good_document_cursor = aws_byte_cursor_from_string(s_good_document_response); |
| 745 |
aws_array_list_push_back(&s_tester.response_data_callbacks, &good_document_cursor); |
| 746 |
|
| 747 |
struct aws_cognito_identity_provider_token_pair static_pairs[] = { |
| 748 |
{ |
| 749 |
.identity_provider_name = aws_byte_cursor_from_c_str("static-name"), |
| 750 |
.identity_provider_token = aws_byte_cursor_from_c_str("static-value"), |
| 751 |
}, |
| 752 |
}; |
| 753 |
|
| 754 |
struct aws_credentials_provider_cognito_options options = { |
| 755 |
.bootstrap = s_tester.bootstrap, |
| 756 |
.function_table = &s_mock_function_table, |
| 757 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 758 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 759 |
.tls_ctx = s_tester.ctx, |
| 760 |
.logins = static_pairs, |
| 761 |
.login_count = AWS_ARRAY_SIZE(static_pairs), |
| 762 |
.get_token_pairs = s_get_token_pairs_success, |
| 763 |
}; |
| 764 |
|
| 765 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 766 |
ASSERT_NOT_NULL(provider); |
| 767 |
|
| 768 |
ASSERT_SUCCESS(aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL)); |
| 769 |
|
| 770 |
s_aws_wait_for_credentials_result(); |
| 771 |
|
| 772 |
aws_mutex_lock(&s_tester.lock); |
| 773 |
ASSERT_TRUE(s_tester.has_received_credentials_callback == true); |
| 774 |
ASSERT_TRUE(s_tester.error_code == AWS_ERROR_SUCCESS); |
| 775 |
ASSERT_TRUE(s_tester.credentials != NULL); |
| 776 |
|
| 777 |
|
| 778 |
struct aws_json_value *json_body = |
| 779 |
aws_json_value_new_from_string(allocator, aws_byte_cursor_from_buf(&s_tester.request_body)); |
| 780 |
ASSERT_NOT_NULL(json_body); |
| 781 |
|
| 782 |
struct aws_json_value *logins = aws_json_value_get_from_object(json_body, aws_byte_cursor_from_c_str("Logins")); |
| 783 |
ASSERT_NOT_NULL(logins); |
| 784 |
|
| 785 |
|
| 786 |
struct aws_json_value *static_login = |
| 787 |
aws_json_value_get_from_object(logins, aws_byte_cursor_from_c_str("static-name")); |
| 788 |
ASSERT_NOT_NULL(static_login); |
| 789 |
ASSERT_TRUE(aws_json_value_is_string(static_login)); |
| 790 |
|
| 791 |
struct aws_byte_cursor static_value_cursor; |
| 792 |
ASSERT_SUCCESS(aws_json_value_get_string(static_login, &static_value_cursor)); |
| 793 |
|
| 794 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 795 |
static_pairs[0].identity_provider_token.ptr, |
| 796 |
static_pairs[0].identity_provider_token.len, |
| 797 |
static_value_cursor.ptr, |
| 798 |
static_value_cursor.len); |
| 799 |
|
| 800 |
|
| 801 |
struct aws_json_value *dynamic_login = |
| 802 |
aws_json_value_get_from_object(logins, aws_byte_cursor_from_c_str("dynamic-name")); |
| 803 |
ASSERT_NOT_NULL(dynamic_login); |
| 804 |
ASSERT_TRUE(aws_json_value_is_string(dynamic_login)); |
| 805 |
|
| 806 |
struct aws_byte_cursor dynamic_value_cursor; |
| 807 |
ASSERT_SUCCESS(aws_json_value_get_string(dynamic_login, &dynamic_value_cursor)); |
| 808 |
|
| 809 |
struct aws_byte_cursor expected_dynamic_value = aws_byte_cursor_from_c_str("dynamic-value"); |
| 810 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 811 |
expected_dynamic_value.ptr, expected_dynamic_value.len, dynamic_value_cursor.ptr, dynamic_value_cursor.len); |
| 812 |
|
| 813 |
aws_json_value_destroy(json_body); |
| 814 |
|
| 815 |
aws_mutex_unlock(&s_tester.lock); |
| 816 |
|
| 817 |
aws_credentials_provider_release(provider); |
| 818 |
|
| 819 |
s_aws_cognito_tester_cleanup(); |
| 820 |
|
| 821 |
return AWS_OP_SUCCESS; |
| 822 |
} |
| 823 |
|
| 824 |
AWS_TEST_CASE( |
| 825 |
credentials_provider_cognito_success_dynamic_token_pairs, |
| 826 |
s_credentials_provider_cognito_success_dynamic_token_pairs_fn); |
| 827 |
|
| 828 |
static int s_credentials_provider_cognito_proxy_routing_enabled_test(struct aws_allocator *allocator, void *ctx) { |
| 829 |
(void)ctx; |
| 830 |
|
| 831 |
s_aws_cognito_tester_init(allocator); |
| 832 |
|
| 833 |
struct proxy_env_var_settings proxy_config = { |
| 834 |
.env_var_type = AWS_HPEV_ENABLE, |
| 835 |
}; |
| 836 |
|
| 837 |
s_tester.proxy_config = &proxy_config; |
| 838 |
|
| 839 |
struct aws_credentials_provider_cognito_options options = { |
| 840 |
.bootstrap = s_tester.bootstrap, |
| 841 |
.function_table = &s_mock_function_table, |
| 842 |
.endpoint = aws_byte_cursor_from_c_str("somewhere.amazonaws.com"), |
| 843 |
.identity = aws_byte_cursor_from_c_str("someone"), |
| 844 |
.tls_ctx = s_tester.ctx, |
| 845 |
.proxy_ev_settings = &proxy_config, |
| 846 |
}; |
| 847 |
|
| 848 |
struct aws_credentials_provider *provider = aws_credentials_provider_new_cognito(allocator, &options); |
| 849 |
|
| 850 |
aws_credentials_provider_get_credentials(provider, s_get_credentials_callback, NULL); |
| 851 |
|
| 852 |
s_aws_wait_for_credentials_result(); |
| 853 |
|
| 854 |
ASSERT_TRUE(s_tester.credentials == NULL); |
| 855 |
|
| 856 |
aws_credentials_provider_release(provider); |
| 857 |
|
| 858 |
s_aws_cognito_tester_cleanup(); |
| 859 |
|
| 860 |
return 0; |
| 861 |
} |
| 862 |
AWS_TEST_CASE( |
| 863 |
credentials_provider_cognito_proxy_routing_enabled_test, |
| 864 |
s_credentials_provider_cognito_proxy_routing_enabled_test); |