| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/credentials.h> |
| 7 |
|
| 8 |
#include <aws/auth/private/aws_profile.h> |
| 9 |
#include <aws/auth/private/credentials_utils.h> |
| 10 |
#include <aws/auth/private/sso_token_providers.h> |
| 11 |
#include <aws/auth/private/sso_token_utils.h> |
| 12 |
#include <aws/common/clock.h> |
| 13 |
|
| 14 |
#ifdef _MSC_VER |
| 15 |
|
| 16 |
# pragma warning(disable : 4204) |
| 17 |
#endif |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
struct aws_token_provider_profile_impl { |
| 23 |
struct aws_string *sso_token_file_path; |
| 24 |
|
| 25 |
aws_io_clock_fn *system_clock_fn; |
| 26 |
}; |
| 27 |
|
| 28 |
static int s_token_provider_profile_get_token( |
| 29 |
struct aws_credentials_provider *provider, |
| 30 |
aws_on_get_credentials_callback_fn callback, |
| 31 |
void *user_data) { |
| 32 |
struct aws_token_provider_profile_impl *impl = provider->impl; |
| 33 |
|
| 34 |
struct aws_sso_token *sso_token = NULL; |
| 35 |
struct aws_credentials *credentials = NULL; |
| 36 |
int result = AWS_OP_ERR; |
| 37 |
sso_token = aws_sso_token_new_from_file(provider->allocator, impl->sso_token_file_path); |
| 38 |
if (!sso_token) { |
| 39 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p) failed to get sso token from file", (void *)provider); |
| 40 |
goto done; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
uint64_t now_ns = UINT64_MAX; |
| 45 |
if (impl->system_clock_fn(&now_ns) != AWS_OP_SUCCESS) { |
| 46 |
goto done; |
| 47 |
} |
| 48 |
|
| 49 |
if (aws_date_time_as_nanos(&sso_token->expiration) <= now_ns) { |
| 50 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p) cached sso token is expired.", (void *)provider); |
| 51 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_EXPIRED); |
| 52 |
goto done; |
| 53 |
} |
| 54 |
|
| 55 |
credentials = aws_credentials_new_token( |
| 56 |
provider->allocator, |
| 57 |
aws_byte_cursor_from_string(sso_token->access_token), |
| 58 |
(uint64_t)aws_date_time_as_epoch_secs(&sso_token->expiration)); |
| 59 |
if (!credentials) { |
| 60 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p) Unable to construct credentials.", (void *)provider); |
| 61 |
goto done; |
| 62 |
} |
| 63 |
|
| 64 |
callback(credentials, AWS_OP_SUCCESS, user_data); |
| 65 |
result = AWS_OP_SUCCESS; |
| 66 |
|
| 67 |
done: |
| 68 |
aws_sso_token_destroy(sso_token); |
| 69 |
aws_credentials_release(credentials); |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
static void s_token_provider_profile_destroy(struct aws_credentials_provider *provider) { |
| 74 |
struct aws_token_provider_profile_impl *impl = provider->impl; |
| 75 |
if (impl == NULL) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
aws_string_destroy(impl->sso_token_file_path); |
| 80 |
|
| 81 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 82 |
aws_mem_release(provider->allocator, provider); |
| 83 |
} |
| 84 |
|
| 85 |
static struct aws_credentials_provider_vtable s_aws_token_provider_profile_vtable = { |
| 86 |
.get_credentials = s_token_provider_profile_get_token, |
| 87 |
.destroy = s_token_provider_profile_destroy, |
| 88 |
}; |
| 89 |
|
| 90 |
AWS_STRING_FROM_LITERAL(s_profile_sso_start_url_name, "sso_start_url"); |
| 91 |
|
| 92 |
static struct aws_string *s_construct_profile_sso_token_path( |
| 93 |
struct aws_allocator *allocator, |
| 94 |
const struct aws_token_provider_sso_profile_options *options) { |
| 95 |
|
| 96 |
struct aws_profile_collection *config_collection = NULL; |
| 97 |
struct aws_string *profile_name = NULL; |
| 98 |
struct aws_string *sso_token_path = NULL; |
| 99 |
|
| 100 |
profile_name = aws_get_profile_name(allocator, &options->profile_name_override); |
| 101 |
if (!profile_name) { |
| 102 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "token-provider-sso-profile: failed to resolve profile name"); |
| 103 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 104 |
goto cleanup; |
| 105 |
} |
| 106 |
if (options->config_file_cached) { |
| 107 |
|
| 108 |
config_collection = aws_profile_collection_acquire(options->config_file_cached); |
| 109 |
} else { |
| 110 |
|
| 111 |
config_collection = aws_load_profile_collection_from_config_file(allocator, options->config_file_name_override); |
| 112 |
} |
| 113 |
|
| 114 |
if (!config_collection) { |
| 115 |
AWS_LOGF_ERROR( |
| 116 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 117 |
"token-provider-sso-profile: could not load or parse" |
| 118 |
" a config file."); |
| 119 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 120 |
goto cleanup; |
| 121 |
} |
| 122 |
|
| 123 |
const struct aws_profile *profile = aws_profile_collection_get_profile(config_collection, profile_name); |
| 124 |
|
| 125 |
if (!profile) { |
| 126 |
AWS_LOGF_ERROR( |
| 127 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 128 |
"token-provider-sso-profile: could not load" |
| 129 |
" a profile at %s.", |
| 130 |
aws_string_c_str(profile_name)); |
| 131 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 132 |
goto cleanup; |
| 133 |
} |
| 134 |
|
| 135 |
const struct aws_profile_property *sso_start_url_property = |
| 136 |
aws_profile_get_property(profile, s_profile_sso_start_url_name); |
| 137 |
|
| 138 |
if (!sso_start_url_property) { |
| 139 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "token-provider-sso-profile: failed to find sso_start_url"); |
| 140 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 141 |
goto cleanup; |
| 142 |
} |
| 143 |
|
| 144 |
sso_token_path = aws_construct_sso_token_path(allocator, aws_profile_property_get_value(sso_start_url_property)); |
| 145 |
if (!sso_token_path) { |
| 146 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "token-provider-sso-profile: failed to construct token path"); |
| 147 |
goto cleanup; |
| 148 |
} |
| 149 |
|
| 150 |
cleanup: |
| 151 |
aws_string_destroy(profile_name); |
| 152 |
aws_profile_collection_release(config_collection); |
| 153 |
return sso_token_path; |
| 154 |
} |
| 155 |
|
| 156 |
struct aws_credentials_provider *aws_token_provider_new_sso_profile( |
| 157 |
struct aws_allocator *allocator, |
| 158 |
const struct aws_token_provider_sso_profile_options *options) { |
| 159 |
struct aws_string *token_path = s_construct_profile_sso_token_path(allocator, options); |
| 160 |
if (!token_path) { |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
struct aws_credentials_provider *provider = NULL; |
| 164 |
struct aws_token_provider_profile_impl *impl = NULL; |
| 165 |
|
| 166 |
aws_mem_acquire_many( |
| 167 |
allocator, |
| 168 |
2, |
| 169 |
&provider, |
| 170 |
sizeof(struct aws_credentials_provider), |
| 171 |
&impl, |
| 172 |
sizeof(struct aws_token_provider_profile_impl)); |
| 173 |
AWS_ZERO_STRUCT(*provider); |
| 174 |
AWS_ZERO_STRUCT(*impl); |
| 175 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_token_provider_profile_vtable, impl); |
| 176 |
impl->sso_token_file_path = aws_string_new_from_string(allocator, token_path); |
| 177 |
provider->shutdown_options = options->shutdown_options; |
| 178 |
if (options->system_clock_fn) { |
| 179 |
impl->system_clock_fn = options->system_clock_fn; |
| 180 |
} else { |
| 181 |
impl->system_clock_fn = aws_sys_clock_get_ticks; |
| 182 |
} |
| 183 |
|
| 184 |
aws_string_destroy(token_path); |
| 185 |
return provider; |
| 186 |
} |