| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/testing/aws_test_harness.h> |
| 7 |
|
| 8 |
#include <aws/auth/private/sso_token_providers.h> |
| 9 |
#include <aws/auth/private/sso_token_utils.h> |
| 10 |
#include <aws/common/clock.h> |
| 11 |
#include <aws/common/environment.h> |
| 12 |
#include <aws/common/file.h> |
| 13 |
#include <aws/io/channel_bootstrap.h> |
| 14 |
#include <aws/io/event_loop.h> |
| 15 |
#include <aws/io/tls_channel_handler.h> |
| 16 |
#include <aws/sdkutils/aws_profile.h> |
| 17 |
|
| 18 |
#include <credentials_provider_utils.h> |
| 19 |
|
| 20 |
#include "shared_credentials_test_definitions.h" |
| 21 |
|
| 22 |
struct sso_session_profile_example { |
| 23 |
const char *name; |
| 24 |
struct aws_byte_cursor text; |
| 25 |
}; |
| 26 |
|
| 27 |
static int s_sso_token_provider_profile_invalid_profile_test(struct aws_allocator *allocator, void *ctx) { |
| 28 |
(void)ctx; |
| 29 |
aws_auth_library_init(allocator); |
| 30 |
const struct sso_session_profile_example invalid_profile_examples[] = { |
| 31 |
{ |
| 32 |
.name = "No config", |
| 33 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 34 |
"[profile default]\naws_access_key_id=fake_access_key\naws_secret_access_key=fake_secret_key\n"), |
| 35 |
}, |
| 36 |
{ |
| 37 |
.name = "No sso_start_url", |
| 38 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[profile " |
| 39 |
"default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 40 |
"access_key=fake_secret_key\nsso_region=us-east-1\n"), |
| 41 |
}, |
| 42 |
{ |
| 43 |
.name = "only sso_session", |
| 44 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[profile " |
| 45 |
"default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 46 |
"access_key=fake_secret_key\nsso_session=dev\n[sso-session " |
| 47 |
"dev]\nsso_start_url=url\nsso_region=us-east-1"), |
| 48 |
}, |
| 49 |
}; |
| 50 |
|
| 51 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 52 |
struct aws_token_provider_sso_profile_options options = { |
| 53 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 54 |
}; |
| 55 |
|
| 56 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_profile_examples); ++i) { |
| 57 |
printf("invalid example [%zu]: %s\n", i, invalid_profile_examples[i].name); |
| 58 |
struct aws_string *config_contents = aws_string_new_from_cursor(allocator, &invalid_profile_examples[i].text); |
| 59 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents)); |
| 60 |
ASSERT_NULL(aws_token_provider_new_sso_profile(allocator, &options)); |
| 61 |
aws_string_destroy(config_contents); |
| 62 |
} |
| 63 |
|
| 64 |
aws_string_destroy(config_file_str); |
| 65 |
aws_auth_library_clean_up(); |
| 66 |
return AWS_OP_SUCCESS; |
| 67 |
} |
| 68 |
|
| 69 |
AWS_TEST_CASE(sso_token_provider_profile_invalid_profile_test, s_sso_token_provider_profile_invalid_profile_test); |
| 70 |
|
| 71 |
static int s_sso_token_provider_profile_valid_profile_test(struct aws_allocator *allocator, void *ctx) { |
| 72 |
(void)ctx; |
| 73 |
aws_auth_library_init(allocator); |
| 74 |
static struct sso_session_profile_example s_valid_profile_examples[] = { |
| 75 |
{ |
| 76 |
.name = "profile", |
| 77 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 78 |
"access_key=fake_secret_key\nsso_region=us-east-" |
| 79 |
"1\nsso_start_url=url"), |
| 80 |
}, |
| 81 |
{ |
| 82 |
.name = "with sso_session", |
| 83 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 84 |
"[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 85 |
"access_key=fake_secret_key\nsso_region=us-east-1\nsso_start_url=url\nsso_" |
| 86 |
"session=dev\n[sso-session dev]\nsso_region=us-east-" |
| 87 |
"1\nsso_start_url=url2"), |
| 88 |
}, |
| 89 |
}; |
| 90 |
|
| 91 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 92 |
struct aws_token_provider_sso_profile_options options = { |
| 93 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 94 |
}; |
| 95 |
|
| 96 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_profile_examples); ++i) { |
| 97 |
printf("valid example [%zu]: %s\n", i, s_valid_profile_examples[i].name); |
| 98 |
struct aws_string *config_contents = aws_string_new_from_cursor(allocator, &s_valid_profile_examples[i].text); |
| 99 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents)); |
| 100 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_profile(allocator, &options); |
| 101 |
ASSERT_NOT_NULL(provider); |
| 102 |
aws_credentials_provider_release(provider); |
| 103 |
aws_string_destroy(config_contents); |
| 104 |
} |
| 105 |
|
| 106 |
aws_string_destroy(config_file_str); |
| 107 |
aws_auth_library_clean_up(); |
| 108 |
return AWS_OP_SUCCESS; |
| 109 |
} |
| 110 |
|
| 111 |
AWS_TEST_CASE(sso_token_provider_profile_valid_profile_test, s_sso_token_provider_profile_valid_profile_test); |
| 112 |
|
| 113 |
static struct aws_mock_token_provider_sso_tester { |
| 114 |
struct aws_tls_ctx *tls_ctx; |
| 115 |
struct aws_event_loop_group *el_group; |
| 116 |
struct aws_host_resolver *resolver; |
| 117 |
struct aws_client_bootstrap *bootstrap; |
| 118 |
|
| 119 |
struct aws_mutex lock; |
| 120 |
struct aws_condition_variable signal; |
| 121 |
struct aws_credentials *credentials; |
| 122 |
bool has_received_credentials_callback; |
| 123 |
bool has_received_shutdown_callback; |
| 124 |
int error_code; |
| 125 |
|
| 126 |
} s_tester; |
| 127 |
|
| 128 |
static int s_aws_mock_token_provider_sso_tester_init(struct aws_allocator *allocator) { |
| 129 |
aws_auth_library_init(allocator); |
| 130 |
|
| 131 |
AWS_ZERO_STRUCT(s_tester); |
| 132 |
|
| 133 |
struct aws_tls_ctx_options tls_ctx_options; |
| 134 |
aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator); |
| 135 |
s_tester.tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options); |
| 136 |
ASSERT_NOT_NULL(s_tester.tls_ctx); |
| 137 |
|
| 138 |
s_tester.el_group = aws_event_loop_group_new_default(allocator, 0, NULL); |
| 139 |
|
| 140 |
struct aws_host_resolver_default_options resolver_options = { |
| 141 |
.el_group = s_tester.el_group, |
| 142 |
.max_entries = 8, |
| 143 |
}; |
| 144 |
s_tester.resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 145 |
|
| 146 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 147 |
.event_loop_group = s_tester.el_group, |
| 148 |
.host_resolver = s_tester.resolver, |
| 149 |
}; |
| 150 |
s_tester.bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 151 |
|
| 152 |
|
| 153 |
aws_tls_ctx_options_clean_up(&tls_ctx_options); |
| 154 |
|
| 155 |
return AWS_OP_SUCCESS; |
| 156 |
} |
| 157 |
|
| 158 |
void s_aws_mock_token_provider_sso_tester_cleanup(void) { |
| 159 |
aws_tls_ctx_release(s_tester.tls_ctx); |
| 160 |
aws_client_bootstrap_release(s_tester.bootstrap); |
| 161 |
aws_host_resolver_release(s_tester.resolver); |
| 162 |
aws_event_loop_group_release(s_tester.el_group); |
| 163 |
|
| 164 |
aws_condition_variable_clean_up(&s_tester.signal); |
| 165 |
aws_mutex_clean_up(&s_tester.lock); |
| 166 |
aws_credentials_release(s_tester.credentials); |
| 167 |
aws_auth_library_clean_up(); |
| 168 |
} |
| 169 |
|
| 170 |
static int s_sso_token_provider_sso_session_invalid_config_test(struct aws_allocator *allocator, void *ctx) { |
| 171 |
(void)ctx; |
| 172 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 173 |
const struct sso_session_profile_example invalid_config_examples[] = { |
| 174 |
{ |
| 175 |
.name = "no sso-session", |
| 176 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 177 |
"access_key=fake_secret_key\nsso_region=us-east-" |
| 178 |
"1\nsso_start_url=url"), |
| 179 |
}, |
| 180 |
{ |
| 181 |
.name = "sso_session with without sso_region", |
| 182 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[profile " |
| 183 |
"default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 184 |
"access_key=fake_secret_key\nsso_session=dev\n[sso-session " |
| 185 |
"dev]\nsso_start_url=url"), |
| 186 |
}, |
| 187 |
{ |
| 188 |
.name = "sso_session with without sso_start_url", |
| 189 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[profile " |
| 190 |
"default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 191 |
"access_key=fake_secret_key\nsso_session=dev\n[sso-session " |
| 192 |
"dev]\nsso_region=us-east-1"), |
| 193 |
}, |
| 194 |
{ |
| 195 |
.name = "sso_session with different profile region", |
| 196 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 197 |
"[profile " |
| 198 |
"default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 199 |
"access_key=fake_secret_key\nsso_session=dev\nsso_region=us-west-" |
| 200 |
"1\nsso_start_url=url\n[sso-session dev]\nsso_region=us-east-1\nsso_start_url=url"), |
| 201 |
}, |
| 202 |
{ |
| 203 |
.name = "sso_session with different profile start url", |
| 204 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 205 |
"[profile " |
| 206 |
"default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 207 |
"access_key=fake_secret_key\nsso_session=dev\nsso_region=us-east-" |
| 208 |
"1\nsso_start_url=url\n[sso-session dev]\nsso_region=us-east-1\nsso_start_url=url2"), |
| 209 |
}, |
| 210 |
{ |
| 211 |
.name = "different sso_session name", |
| 212 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 213 |
"[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 214 |
"access_key=fake_secret_key\nsso_region=us-east-1\nsso_start_url=url\nsso_" |
| 215 |
"session=dev\n[sso-session dev2]\nsso_region=us-east-" |
| 216 |
"1\nsso_start_url=url"), |
| 217 |
}, |
| 218 |
}; |
| 219 |
|
| 220 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 221 |
struct aws_token_provider_sso_session_options options = { |
| 222 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 223 |
.tls_ctx = s_tester.tls_ctx, |
| 224 |
.bootstrap = s_tester.bootstrap, |
| 225 |
}; |
| 226 |
|
| 227 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(invalid_config_examples); ++i) { |
| 228 |
printf("invalid example [%zu]: %s\n", i, invalid_config_examples[i].name); |
| 229 |
struct aws_string *config_contents = aws_string_new_from_cursor(allocator, &invalid_config_examples[i].text); |
| 230 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents)); |
| 231 |
ASSERT_NULL(aws_token_provider_new_sso_session(allocator, &options)); |
| 232 |
aws_string_destroy(config_contents); |
| 233 |
} |
| 234 |
|
| 235 |
aws_string_destroy(config_file_str); |
| 236 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 237 |
return AWS_OP_SUCCESS; |
| 238 |
} |
| 239 |
|
| 240 |
AWS_TEST_CASE(sso_token_provider_sso_session_invalid_config_test, s_sso_token_provider_sso_session_invalid_config_test); |
| 241 |
|
| 242 |
static int s_sso_token_provider_sso_session_valid_config_test(struct aws_allocator *allocator, void *ctx) { |
| 243 |
(void)ctx; |
| 244 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 245 |
|
| 246 |
static struct sso_session_profile_example s_valid_profile_examples[] = { |
| 247 |
{ |
| 248 |
.name = "sso-session", |
| 249 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 250 |
"access_key=fake_secret_key\nsso_" |
| 251 |
"session=dev\n[sso-session dev]\nsso_region=us-east-" |
| 252 |
"1\nsso_start_url=url"), |
| 253 |
}, |
| 254 |
{ |
| 255 |
.name = "with profile sso_region", |
| 256 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 257 |
"access_key=fake_secret_key\nsso_region=us-east-1\nsso_" |
| 258 |
"session=dev\n[sso-session dev]\nsso_region=us-east-" |
| 259 |
"1\nsso_start_url=url"), |
| 260 |
}, |
| 261 |
{ |
| 262 |
.name = "with profile sso_start_url", |
| 263 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 264 |
"access_key=fake_secret_key\nsso_start_url=url\nsso_" |
| 265 |
"session=dev\n[sso-session dev]\nsso_region=us-east-" |
| 266 |
"1\nsso_start_url=url"), |
| 267 |
}, |
| 268 |
{ |
| 269 |
.name = "with profile sso_region and sso_start_url", |
| 270 |
.text = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL( |
| 271 |
"[default]\naws_access_key_id=fake_access_key\naws_secret_" |
| 272 |
"access_key=fake_secret_key\nsso_region=us-east-1\nsso_start_url=url\nsso_" |
| 273 |
"session=dev\n[sso-session dev]\nsso_region=us-east-" |
| 274 |
"1\nsso_start_url=url"), |
| 275 |
}, |
| 276 |
}; |
| 277 |
|
| 278 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 279 |
struct aws_token_provider_sso_session_options options = { |
| 280 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 281 |
.tls_ctx = s_tester.tls_ctx, |
| 282 |
.bootstrap = s_tester.bootstrap, |
| 283 |
}; |
| 284 |
|
| 285 |
for (size_t i = 0; i < AWS_ARRAY_SIZE(s_valid_profile_examples); ++i) { |
| 286 |
printf("valid example [%zu]: %s\n", i, s_valid_profile_examples[i].name); |
| 287 |
struct aws_string *config_contents = aws_string_new_from_cursor(allocator, &s_valid_profile_examples[i].text); |
| 288 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, config_contents)); |
| 289 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_session(allocator, &options); |
| 290 |
ASSERT_NOT_NULL(provider); |
| 291 |
aws_credentials_provider_release(provider); |
| 292 |
aws_string_destroy(config_contents); |
| 293 |
} |
| 294 |
|
| 295 |
aws_string_destroy(config_file_str); |
| 296 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 297 |
return AWS_OP_SUCCESS; |
| 298 |
} |
| 299 |
|
| 300 |
AWS_TEST_CASE(sso_token_provider_sso_session_valid_config_test, s_sso_token_provider_sso_session_valid_config_test); |
| 301 |
|
| 302 |
|
| 303 |
AWS_STATIC_STRING_FROM_LITERAL(s_sso_profile_start_url, "https://d-123.awsapps.com/start"); |
| 304 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 305 |
s_sso_profile_config_contents, |
| 306 |
"[default]\n" |
| 307 |
"sso_start_url = https://d-123.awsapps.com/start\n"); |
| 308 |
|
| 309 |
AWS_STATIC_STRING_FROM_LITERAL(s_sso_session_name, "session"); |
| 310 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 311 |
s_sso_session_config_contents, |
| 312 |
"[default]\n" |
| 313 |
"sso_session = session\n" |
| 314 |
"[sso-session session]\n" |
| 315 |
"sso_start_url = https://d-123.awsapps.com/start\n" |
| 316 |
"sso_region = us-west-2\n"); |
| 317 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 318 |
s_sso_token, |
| 319 |
"{\"accessToken\": \"ValidAccessToken\",\"expiresAt\": \"2015-03-12T05:35:19Z\"}"); |
| 320 |
AWS_STATIC_STRING_FROM_LITERAL(s_invalid_config, "invalid config"); |
| 321 |
|
| 322 |
AWS_STATIC_STRING_FROM_LITERAL(s_good_token, "ValidAccessToken"); |
| 323 |
static uint64_t s_token_expiration_s = 1426138519; |
| 324 |
static int s_sso_token_provider_sso_session_basic_success(struct aws_allocator *allocator, void *ctx) { |
| 325 |
(void)ctx; |
| 326 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 327 |
|
| 328 |
|
| 329 |
struct aws_string *tmp_home; |
| 330 |
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home)); |
| 331 |
|
| 332 |
|
| 333 |
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name); |
| 334 |
ASSERT_NOT_NULL(token_path); |
| 335 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path)); |
| 336 |
ASSERT_SUCCESS(aws_create_profile_file(token_path, s_sso_token)); |
| 337 |
|
| 338 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 339 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, s_sso_session_config_contents)); |
| 340 |
mock_aws_set_system_time(0); |
| 341 |
struct aws_token_provider_sso_session_options options = { |
| 342 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 343 |
.tls_ctx = s_tester.tls_ctx, |
| 344 |
.bootstrap = s_tester.bootstrap, |
| 345 |
.system_clock_fn = mock_aws_get_system_time, |
| 346 |
}; |
| 347 |
|
| 348 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_session(allocator, &options); |
| 349 |
ASSERT_NOT_NULL(provider); |
| 350 |
|
| 351 |
struct aws_get_credentials_test_callback_result callback_results; |
| 352 |
aws_get_credentials_test_callback_result_init(&callback_results, 1); |
| 353 |
ASSERT_SUCCESS( |
| 354 |
aws_credentials_provider_get_credentials(provider, aws_test_get_credentials_async_callback, &callback_results)); |
| 355 |
aws_wait_on_credentials_callback(&callback_results); |
| 356 |
|
| 357 |
ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_token(callback_results.credentials), s_good_token); |
| 358 |
ASSERT_INT_EQUALS( |
| 359 |
aws_credentials_get_expiration_timepoint_seconds(callback_results.credentials), s_token_expiration_s); |
| 360 |
|
| 361 |
aws_get_credentials_test_callback_result_clean_up(&callback_results); |
| 362 |
aws_credentials_provider_release(provider); |
| 363 |
|
| 364 |
aws_directory_delete(tmp_home, true); |
| 365 |
aws_string_destroy(tmp_home); |
| 366 |
aws_string_destroy(token_path); |
| 367 |
aws_string_destroy(config_file_str); |
| 368 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 369 |
return AWS_OP_SUCCESS; |
| 370 |
} |
| 371 |
|
| 372 |
AWS_TEST_CASE(sso_token_provider_sso_session_basic_success, s_sso_token_provider_sso_session_basic_success); |
| 373 |
|
| 374 |
static int s_sso_token_provider_sso_session_config_file_cached(struct aws_allocator *allocator, void *ctx) { |
| 375 |
(void)ctx; |
| 376 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 377 |
|
| 378 |
|
| 379 |
struct aws_string *tmp_home; |
| 380 |
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home)); |
| 381 |
|
| 382 |
|
| 383 |
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name); |
| 384 |
ASSERT_NOT_NULL(token_path); |
| 385 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path)); |
| 386 |
ASSERT_SUCCESS(aws_create_profile_file(token_path, s_sso_token)); |
| 387 |
|
| 388 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 389 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, s_invalid_config)); |
| 390 |
|
| 391 |
struct aws_byte_buf profile_buffer = aws_byte_buf_from_c_str(aws_string_c_str(s_sso_session_config_contents)); |
| 392 |
struct aws_profile_collection *config_collection = |
| 393 |
aws_profile_collection_new_from_buffer(allocator, &profile_buffer, AWS_PST_CONFIG); |
| 394 |
|
| 395 |
mock_aws_set_system_time(0); |
| 396 |
struct aws_token_provider_sso_session_options options = { |
| 397 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 398 |
.config_file_cached = config_collection, |
| 399 |
.tls_ctx = s_tester.tls_ctx, |
| 400 |
.bootstrap = s_tester.bootstrap, |
| 401 |
.system_clock_fn = mock_aws_get_system_time, |
| 402 |
}; |
| 403 |
|
| 404 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_session(allocator, &options); |
| 405 |
ASSERT_NOT_NULL(provider); |
| 406 |
|
| 407 |
struct aws_get_credentials_test_callback_result callback_results; |
| 408 |
aws_get_credentials_test_callback_result_init(&callback_results, 1); |
| 409 |
ASSERT_SUCCESS( |
| 410 |
aws_credentials_provider_get_credentials(provider, aws_test_get_credentials_async_callback, &callback_results)); |
| 411 |
aws_wait_on_credentials_callback(&callback_results); |
| 412 |
|
| 413 |
ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_token(callback_results.credentials), s_good_token); |
| 414 |
ASSERT_INT_EQUALS( |
| 415 |
aws_credentials_get_expiration_timepoint_seconds(callback_results.credentials), s_token_expiration_s); |
| 416 |
|
| 417 |
aws_get_credentials_test_callback_result_clean_up(&callback_results); |
| 418 |
aws_credentials_provider_release(provider); |
| 419 |
|
| 420 |
aws_directory_delete(tmp_home, true); |
| 421 |
aws_string_destroy(tmp_home); |
| 422 |
aws_string_destroy(token_path); |
| 423 |
aws_string_destroy(config_file_str); |
| 424 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 425 |
aws_profile_collection_release(config_collection); |
| 426 |
return AWS_OP_SUCCESS; |
| 427 |
} |
| 428 |
|
| 429 |
AWS_TEST_CASE(sso_token_provider_sso_session_config_file_cached, s_sso_token_provider_sso_session_config_file_cached); |
| 430 |
|
| 431 |
static int s_sso_token_provider_sso_session_expired_token(struct aws_allocator *allocator, void *ctx) { |
| 432 |
(void)ctx; |
| 433 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 434 |
|
| 435 |
|
| 436 |
struct aws_string *tmp_home; |
| 437 |
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home)); |
| 438 |
|
| 439 |
|
| 440 |
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_session_name); |
| 441 |
ASSERT_NOT_NULL(token_path); |
| 442 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path)); |
| 443 |
ASSERT_SUCCESS(aws_create_profile_file(token_path, s_sso_token)); |
| 444 |
|
| 445 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 446 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, s_sso_session_config_contents)); |
| 447 |
uint64_t nano_expiration = |
| 448 |
aws_timestamp_convert(s_token_expiration_s + 1, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 449 |
mock_aws_set_system_time(nano_expiration); |
| 450 |
struct aws_token_provider_sso_session_options options = { |
| 451 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 452 |
.tls_ctx = s_tester.tls_ctx, |
| 453 |
.bootstrap = s_tester.bootstrap, |
| 454 |
.system_clock_fn = mock_aws_get_system_time, |
| 455 |
}; |
| 456 |
|
| 457 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_session(allocator, &options); |
| 458 |
ASSERT_NOT_NULL(provider); |
| 459 |
|
| 460 |
struct aws_get_credentials_test_callback_result callback_results; |
| 461 |
aws_get_credentials_test_callback_result_init(&callback_results, 1); |
| 462 |
ASSERT_ERROR( |
| 463 |
AWS_AUTH_SSO_TOKEN_EXPIRED, |
| 464 |
aws_credentials_provider_get_credentials(provider, aws_test_get_credentials_async_callback, &callback_results)); |
| 465 |
aws_credentials_provider_release(provider); |
| 466 |
|
| 467 |
aws_directory_delete(tmp_home, true); |
| 468 |
aws_string_destroy(tmp_home); |
| 469 |
aws_string_destroy(token_path); |
| 470 |
aws_string_destroy(config_file_str); |
| 471 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 472 |
return AWS_OP_SUCCESS; |
| 473 |
} |
| 474 |
|
| 475 |
AWS_TEST_CASE(sso_token_provider_sso_session_expired_token, s_sso_token_provider_sso_session_expired_token); |
| 476 |
|
| 477 |
static int s_sso_token_provider_profile_basic_success(struct aws_allocator *allocator, void *ctx) { |
| 478 |
(void)ctx; |
| 479 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 480 |
|
| 481 |
|
| 482 |
struct aws_string *tmp_home; |
| 483 |
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home)); |
| 484 |
|
| 485 |
|
| 486 |
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_profile_start_url); |
| 487 |
ASSERT_NOT_NULL(token_path); |
| 488 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path)); |
| 489 |
ASSERT_SUCCESS(aws_create_profile_file(token_path, s_sso_token)); |
| 490 |
|
| 491 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 492 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, s_sso_profile_config_contents)); |
| 493 |
|
| 494 |
mock_aws_set_system_time(0); |
| 495 |
struct aws_token_provider_sso_profile_options options = { |
| 496 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 497 |
.system_clock_fn = mock_aws_get_system_time, |
| 498 |
}; |
| 499 |
|
| 500 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_profile(allocator, &options); |
| 501 |
ASSERT_NOT_NULL(provider); |
| 502 |
|
| 503 |
struct aws_get_credentials_test_callback_result callback_results; |
| 504 |
aws_get_credentials_test_callback_result_init(&callback_results, 1); |
| 505 |
ASSERT_SUCCESS( |
| 506 |
aws_credentials_provider_get_credentials(provider, aws_test_get_credentials_async_callback, &callback_results)); |
| 507 |
aws_wait_on_credentials_callback(&callback_results); |
| 508 |
|
| 509 |
ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_token(callback_results.credentials), s_good_token); |
| 510 |
ASSERT_INT_EQUALS( |
| 511 |
aws_credentials_get_expiration_timepoint_seconds(callback_results.credentials), s_token_expiration_s); |
| 512 |
|
| 513 |
aws_get_credentials_test_callback_result_clean_up(&callback_results); |
| 514 |
aws_credentials_provider_release(provider); |
| 515 |
|
| 516 |
aws_directory_delete(tmp_home, true); |
| 517 |
aws_string_destroy(tmp_home); |
| 518 |
aws_string_destroy(token_path); |
| 519 |
aws_string_destroy(config_file_str); |
| 520 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 521 |
return AWS_OP_SUCCESS; |
| 522 |
} |
| 523 |
|
| 524 |
AWS_TEST_CASE(sso_token_provider_profile_basic_success, s_sso_token_provider_profile_basic_success); |
| 525 |
static int s_sso_token_provider_profile_cached_config_file(struct aws_allocator *allocator, void *ctx) { |
| 526 |
(void)ctx; |
| 527 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 528 |
|
| 529 |
|
| 530 |
struct aws_string *tmp_home; |
| 531 |
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home)); |
| 532 |
|
| 533 |
|
| 534 |
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_profile_start_url); |
| 535 |
ASSERT_NOT_NULL(token_path); |
| 536 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path)); |
| 537 |
ASSERT_SUCCESS(aws_create_profile_file(token_path, s_sso_token)); |
| 538 |
|
| 539 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 540 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, s_invalid_config)); |
| 541 |
|
| 542 |
struct aws_byte_buf profile_buffer = aws_byte_buf_from_c_str(aws_string_c_str(s_sso_profile_config_contents)); |
| 543 |
|
| 544 |
struct aws_profile_collection *config_collection = |
| 545 |
aws_profile_collection_new_from_buffer(allocator, &profile_buffer, AWS_PST_CONFIG); |
| 546 |
|
| 547 |
mock_aws_set_system_time(0); |
| 548 |
struct aws_token_provider_sso_profile_options options = { |
| 549 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 550 |
.config_file_cached = config_collection, |
| 551 |
.system_clock_fn = mock_aws_get_system_time, |
| 552 |
}; |
| 553 |
|
| 554 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_profile(allocator, &options); |
| 555 |
ASSERT_NOT_NULL(provider); |
| 556 |
|
| 557 |
struct aws_get_credentials_test_callback_result callback_results; |
| 558 |
aws_get_credentials_test_callback_result_init(&callback_results, 1); |
| 559 |
ASSERT_SUCCESS( |
| 560 |
aws_credentials_provider_get_credentials(provider, aws_test_get_credentials_async_callback, &callback_results)); |
| 561 |
aws_wait_on_credentials_callback(&callback_results); |
| 562 |
|
| 563 |
ASSERT_CURSOR_VALUE_STRING_EQUALS(aws_credentials_get_token(callback_results.credentials), s_good_token); |
| 564 |
ASSERT_INT_EQUALS( |
| 565 |
aws_credentials_get_expiration_timepoint_seconds(callback_results.credentials), s_token_expiration_s); |
| 566 |
|
| 567 |
aws_get_credentials_test_callback_result_clean_up(&callback_results); |
| 568 |
aws_credentials_provider_release(provider); |
| 569 |
|
| 570 |
aws_directory_delete(tmp_home, true); |
| 571 |
aws_string_destroy(tmp_home); |
| 572 |
aws_string_destroy(token_path); |
| 573 |
aws_string_destroy(config_file_str); |
| 574 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 575 |
aws_profile_collection_release(config_collection); |
| 576 |
return AWS_OP_SUCCESS; |
| 577 |
} |
| 578 |
|
| 579 |
AWS_TEST_CASE(sso_token_provider_profile_cached_config_file, s_sso_token_provider_profile_cached_config_file); |
| 580 |
|
| 581 |
static int s_sso_token_provider_profile_expired_token(struct aws_allocator *allocator, void *ctx) { |
| 582 |
(void)ctx; |
| 583 |
s_aws_mock_token_provider_sso_tester_init(allocator); |
| 584 |
|
| 585 |
|
| 586 |
struct aws_string *tmp_home; |
| 587 |
ASSERT_SUCCESS(aws_create_random_home_directory(allocator, &tmp_home)); |
| 588 |
|
| 589 |
|
| 590 |
struct aws_string *token_path = aws_construct_sso_token_path(allocator, s_sso_profile_start_url); |
| 591 |
ASSERT_NOT_NULL(token_path); |
| 592 |
ASSERT_SUCCESS(aws_create_directory_components(allocator, token_path)); |
| 593 |
ASSERT_SUCCESS(aws_create_profile_file(token_path, s_sso_token)); |
| 594 |
|
| 595 |
struct aws_string *config_file_str = aws_create_process_unique_file_name(allocator); |
| 596 |
ASSERT_SUCCESS(aws_create_profile_file(config_file_str, s_sso_profile_config_contents)); |
| 597 |
|
| 598 |
uint64_t nano_expiration = |
| 599 |
aws_timestamp_convert(s_token_expiration_s + 100, AWS_TIMESTAMP_SECS, AWS_TIMESTAMP_NANOS, NULL); |
| 600 |
mock_aws_set_system_time(nano_expiration); |
| 601 |
struct aws_token_provider_sso_profile_options options = { |
| 602 |
.config_file_name_override = aws_byte_cursor_from_string(config_file_str), |
| 603 |
.system_clock_fn = mock_aws_get_system_time, |
| 604 |
}; |
| 605 |
|
| 606 |
struct aws_credentials_provider *provider = aws_token_provider_new_sso_profile(allocator, &options); |
| 607 |
ASSERT_NOT_NULL(provider); |
| 608 |
|
| 609 |
struct aws_get_credentials_test_callback_result callback_results; |
| 610 |
aws_get_credentials_test_callback_result_init(&callback_results, 1); |
| 611 |
ASSERT_ERROR( |
| 612 |
AWS_AUTH_SSO_TOKEN_EXPIRED, |
| 613 |
aws_credentials_provider_get_credentials(provider, aws_test_get_credentials_async_callback, &callback_results)); |
| 614 |
|
| 615 |
aws_credentials_provider_release(provider); |
| 616 |
|
| 617 |
aws_directory_delete(tmp_home, true); |
| 618 |
aws_string_destroy(tmp_home); |
| 619 |
aws_string_destroy(token_path); |
| 620 |
aws_string_destroy(config_file_str); |
| 621 |
s_aws_mock_token_provider_sso_tester_cleanup(); |
| 622 |
return AWS_OP_SUCCESS; |
| 623 |
} |
| 624 |
|
| 625 |
AWS_TEST_CASE(sso_token_provider_profile_expired_token, s_sso_token_provider_profile_expired_token); |