| 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_sso_session_impl { |
| 23 |
struct aws_string *sso_token_file_path; |
| 24 |
aws_io_clock_fn *system_clock_fn; |
| 25 |
}; |
| 26 |
|
| 27 |
static int s_token_provider_sso_session_get_token( |
| 28 |
struct aws_credentials_provider *provider, |
| 29 |
aws_on_get_credentials_callback_fn callback, |
| 30 |
void *user_data) { |
| 31 |
|
| 32 |
struct aws_token_provider_sso_session_impl *impl = provider->impl; |
| 33 |
struct aws_sso_token *sso_token = NULL; |
| 34 |
struct aws_credentials *credentials = NULL; |
| 35 |
int result = AWS_OP_ERR; |
| 36 |
|
| 37 |
sso_token = aws_sso_token_new_from_file(provider->allocator, impl->sso_token_file_path); |
| 38 |
if (!sso_token) { |
| 39 |
AWS_LOGF_ERROR( |
| 40 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p) failed to get sso token from file.", (void *)provider); |
| 41 |
goto done; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
uint64_t now_ns = UINT64_MAX; |
| 46 |
if (impl->system_clock_fn(&now_ns) != AWS_OP_SUCCESS) { |
| 47 |
goto done; |
| 48 |
} |
| 49 |
|
| 50 |
if (aws_date_time_as_nanos(&sso_token->expiration) <= now_ns) { |
| 51 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p) cached sso token is expired.", (void *)provider); |
| 52 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_EXPIRED); |
| 53 |
goto done; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
credentials = aws_credentials_new_token( |
| 59 |
provider->allocator, |
| 60 |
aws_byte_cursor_from_string(sso_token->access_token), |
| 61 |
(uint64_t)aws_date_time_as_epoch_secs(&sso_token->expiration)); |
| 62 |
if (!credentials) { |
| 63 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "(id=%p) Unable to construct credentials.", (void *)provider); |
| 64 |
goto done; |
| 65 |
} |
| 66 |
callback(credentials, AWS_OP_SUCCESS, user_data); |
| 67 |
result = AWS_OP_SUCCESS; |
| 68 |
|
| 69 |
done: |
| 70 |
aws_sso_token_destroy(sso_token); |
| 71 |
aws_credentials_release(credentials); |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
static void s_token_provider_sso_session_destroy(struct aws_credentials_provider *provider) { |
| 76 |
struct aws_token_provider_sso_session_impl *impl = provider->impl; |
| 77 |
if (impl == NULL) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
aws_string_destroy(impl->sso_token_file_path); |
| 82 |
|
| 83 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 84 |
aws_mem_release(provider->allocator, provider); |
| 85 |
} |
| 86 |
|
| 87 |
static struct aws_credentials_provider_vtable s_aws_token_provider_sso_session_vtable = { |
| 88 |
.get_credentials = s_token_provider_sso_session_get_token, |
| 89 |
.destroy = s_token_provider_sso_session_destroy, |
| 90 |
}; |
| 91 |
|
| 92 |
AWS_STRING_FROM_LITERAL(s_sso_session_name, "sso_session"); |
| 93 |
AWS_STRING_FROM_LITERAL(s_sso_region_name, "sso_region"); |
| 94 |
AWS_STRING_FROM_LITERAL(s_sso_start_url_name, "sso_start_url"); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static struct aws_string *s_verify_config_and_construct_sso_token_path( |
| 108 |
struct aws_allocator *allocator, |
| 109 |
const struct aws_token_provider_sso_session_options *options) { |
| 110 |
struct aws_profile_collection *config_collection = NULL; |
| 111 |
struct aws_string *profile_name = NULL; |
| 112 |
struct aws_string *sso_token_path = NULL; |
| 113 |
|
| 114 |
profile_name = aws_get_profile_name(allocator, &options->profile_name_override); |
| 115 |
if (!profile_name) { |
| 116 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso-session: token provider failed to resolve profile name"); |
| 117 |
goto cleanup; |
| 118 |
} |
| 119 |
if (options->config_file_cached) { |
| 120 |
|
| 121 |
config_collection = aws_profile_collection_acquire(options->config_file_cached); |
| 122 |
} else { |
| 123 |
|
| 124 |
config_collection = aws_load_profile_collection_from_config_file(allocator, options->config_file_name_override); |
| 125 |
} |
| 126 |
if (!config_collection) { |
| 127 |
goto cleanup; |
| 128 |
} |
| 129 |
|
| 130 |
const struct aws_profile *profile = aws_profile_collection_get_profile(config_collection, profile_name); |
| 131 |
|
| 132 |
if (!profile) { |
| 133 |
AWS_LOGF_ERROR( |
| 134 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 135 |
"sso-session: token provider could not load" |
| 136 |
" a profile at %s.", |
| 137 |
aws_string_c_str(profile_name)); |
| 138 |
goto cleanup; |
| 139 |
} |
| 140 |
|
| 141 |
const struct aws_profile_property *sso_session_property = aws_profile_get_property(profile, s_sso_session_name); |
| 142 |
if (!sso_session_property) { |
| 143 |
AWS_LOGF_ERROR( |
| 144 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 145 |
"token-provider-sso-session: token provider could not find an sso-session at profile %s", |
| 146 |
aws_string_c_str(profile_name)); |
| 147 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 148 |
goto cleanup; |
| 149 |
} |
| 150 |
const struct aws_string *sso_session_name = aws_profile_property_get_value(sso_session_property); |
| 151 |
|
| 152 |
|
| 153 |
const struct aws_profile *session_profile = |
| 154 |
aws_profile_collection_get_section(config_collection, AWS_PROFILE_SECTION_TYPE_SSO_SESSION, sso_session_name); |
| 155 |
if (!session_profile) { |
| 156 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "token-provider-sso-session: failed to find an sso-session"); |
| 157 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 158 |
goto cleanup; |
| 159 |
} |
| 160 |
|
| 161 |
const struct aws_profile_property *sso_region_property = |
| 162 |
aws_profile_get_property(session_profile, s_sso_region_name); |
| 163 |
const struct aws_profile_property *sso_start_url_property = |
| 164 |
aws_profile_get_property(session_profile, s_sso_start_url_name); |
| 165 |
|
| 166 |
if (!sso_region_property) { |
| 167 |
AWS_LOGF_ERROR( |
| 168 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "token-provider-sso-session: failed to find sso_region in sso-session"); |
| 169 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 170 |
goto cleanup; |
| 171 |
} |
| 172 |
|
| 173 |
if (!sso_start_url_property) { |
| 174 |
AWS_LOGF_ERROR( |
| 175 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 176 |
"token-provider-sso-session: failed to find sso_start_url in sso-session"); |
| 177 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 178 |
goto cleanup; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
const struct aws_string *sso_region = aws_profile_property_get_value(sso_region_property); |
| 183 |
const struct aws_string *sso_start_url = aws_profile_property_get_value(sso_start_url_property); |
| 184 |
|
| 185 |
const struct aws_profile_property *profile_sso_region_property = |
| 186 |
aws_profile_get_property(profile, s_sso_region_name); |
| 187 |
const struct aws_profile_property *profile_sso_start_url_property = |
| 188 |
aws_profile_get_property(profile, s_sso_start_url_name); |
| 189 |
|
| 190 |
if (profile_sso_region_property && |
| 191 |
!aws_string_eq(sso_region, aws_profile_property_get_value(profile_sso_region_property))) { |
| 192 |
AWS_LOGF_ERROR( |
| 193 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 194 |
"token-provider-sso-session: profile & sso-session have different value for sso_region"); |
| 195 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 196 |
goto cleanup; |
| 197 |
} |
| 198 |
|
| 199 |
if (profile_sso_start_url_property && |
| 200 |
!aws_string_eq(sso_start_url, aws_profile_property_get_value(profile_sso_start_url_property))) { |
| 201 |
AWS_LOGF_ERROR( |
| 202 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 203 |
"token-provider-sso-session: profile & sso-session have different value for sso_start_url"); |
| 204 |
aws_raise_error(AWS_AUTH_SSO_TOKEN_PROVIDER_SOURCE_FAILURE); |
| 205 |
goto cleanup; |
| 206 |
} |
| 207 |
|
| 208 |
sso_token_path = aws_construct_sso_token_path(allocator, sso_session_name); |
| 209 |
|
| 210 |
cleanup: |
| 211 |
aws_string_destroy(profile_name); |
| 212 |
aws_profile_collection_release(config_collection); |
| 213 |
return sso_token_path; |
| 214 |
} |
| 215 |
|
| 216 |
struct aws_credentials_provider *aws_token_provider_new_sso_session( |
| 217 |
struct aws_allocator *allocator, |
| 218 |
const struct aws_token_provider_sso_session_options *options) { |
| 219 |
|
| 220 |
|
| 221 |
AWS_ASSERT(options->bootstrap); |
| 222 |
AWS_ASSERT(options->tls_ctx); |
| 223 |
|
| 224 |
struct aws_string *token_path = s_verify_config_and_construct_sso_token_path(allocator, options); |
| 225 |
if (!token_path) { |
| 226 |
return NULL; |
| 227 |
} |
| 228 |
struct aws_credentials_provider *provider = NULL; |
| 229 |
struct aws_token_provider_sso_session_impl *impl = NULL; |
| 230 |
|
| 231 |
aws_mem_acquire_many( |
| 232 |
allocator, |
| 233 |
2, |
| 234 |
&provider, |
| 235 |
sizeof(struct aws_credentials_provider), |
| 236 |
&impl, |
| 237 |
sizeof(struct aws_token_provider_sso_session_impl)); |
| 238 |
AWS_ZERO_STRUCT(*provider); |
| 239 |
AWS_ZERO_STRUCT(*impl); |
| 240 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_token_provider_sso_session_vtable, impl); |
| 241 |
impl->sso_token_file_path = aws_string_new_from_string(allocator, token_path); |
| 242 |
provider->shutdown_options = options->shutdown_options; |
| 243 |
if (options->system_clock_fn) { |
| 244 |
impl->system_clock_fn = options->system_clock_fn; |
| 245 |
} else { |
| 246 |
impl->system_clock_fn = aws_sys_clock_get_ticks; |
| 247 |
} |
| 248 |
|
| 249 |
aws_string_destroy(token_path); |
| 250 |
return provider; |
| 251 |
} |