| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/auth/aws_imds_client.h> |
| 7 |
#include <aws/auth/credentials.h> |
| 8 |
#include <aws/auth/private/credentials_utils.h> |
| 9 |
#include <aws/common/string.h> |
| 10 |
|
| 11 |
#if defined(_MSC_VER) |
| 12 |
# pragma warning(disable : 4204) |
| 13 |
#endif |
| 14 |
|
| 15 |
struct aws_credentials_provider_imds_impl { |
| 16 |
struct aws_imds_client *client; |
| 17 |
}; |
| 18 |
|
| 19 |
static int s_credentials_provider_imds_get_credentials_async( |
| 20 |
struct aws_credentials_provider *provider, |
| 21 |
aws_on_get_credentials_callback_fn callback, |
| 22 |
void *user_data); |
| 23 |
|
| 24 |
static void s_on_imds_client_shutdown(void *user_data); |
| 25 |
|
| 26 |
static void s_credentials_provider_imds_destroy(struct aws_credentials_provider *provider) { |
| 27 |
struct aws_credentials_provider_imds_impl *impl = provider->impl; |
| 28 |
if (impl == NULL) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
if (impl->client) { |
| 33 |
|
| 34 |
aws_imds_client_release(impl->client); |
| 35 |
} else { |
| 36 |
|
| 37 |
|
| 38 |
s_on_imds_client_shutdown(provider); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
static void s_on_imds_client_shutdown(void *user_data) { |
| 43 |
struct aws_credentials_provider *provider = user_data; |
| 44 |
aws_credentials_provider_invoke_shutdown_callback(provider); |
| 45 |
aws_mem_release(provider->allocator, provider); |
| 46 |
} |
| 47 |
|
| 48 |
static struct aws_credentials_provider_vtable s_aws_credentials_provider_imds_vtable = { |
| 49 |
.get_credentials = s_credentials_provider_imds_get_credentials_async, |
| 50 |
.destroy = s_credentials_provider_imds_destroy, |
| 51 |
}; |
| 52 |
|
| 53 |
struct aws_credentials_provider *aws_credentials_provider_new_imds( |
| 54 |
struct aws_allocator *allocator, |
| 55 |
const struct aws_credentials_provider_imds_options *options) { |
| 56 |
|
| 57 |
if (!options->bootstrap) { |
| 58 |
AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "Client bootstrap is required for querying IMDS"); |
| 59 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 60 |
return NULL; |
| 61 |
} |
| 62 |
|
| 63 |
struct aws_credentials_provider *provider = NULL; |
| 64 |
struct aws_credentials_provider_imds_impl *impl = NULL; |
| 65 |
|
| 66 |
aws_mem_acquire_many( |
| 67 |
allocator, |
| 68 |
2, |
| 69 |
&provider, |
| 70 |
sizeof(struct aws_credentials_provider), |
| 71 |
&impl, |
| 72 |
sizeof(struct aws_credentials_provider_imds_impl)); |
| 73 |
|
| 74 |
if (!provider) { |
| 75 |
return NULL; |
| 76 |
} |
| 77 |
|
| 78 |
AWS_ZERO_STRUCT(*provider); |
| 79 |
AWS_ZERO_STRUCT(*impl); |
| 80 |
|
| 81 |
aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_imds_vtable, impl); |
| 82 |
|
| 83 |
struct aws_imds_client_options client_options = { |
| 84 |
.bootstrap = options->bootstrap, |
| 85 |
.function_table = options->function_table, |
| 86 |
.imds_version = options->imds_version, |
| 87 |
.ec2_metadata_v1_disabled = options->ec2_metadata_v1_disabled, |
| 88 |
.shutdown_options = |
| 89 |
{ |
| 90 |
.shutdown_callback = s_on_imds_client_shutdown, |
| 91 |
.shutdown_user_data = provider, |
| 92 |
}, |
| 93 |
.proxy_ev_settings = options->proxy_ev_settings, |
| 94 |
.imds_endpoint = options->imds_endpoint, |
| 95 |
.imds_endpoint_mode = options->imds_endpoint_mode, |
| 96 |
}; |
| 97 |
|
| 98 |
impl->client = aws_imds_client_new(allocator, &client_options); |
| 99 |
if (!impl->client) { |
| 100 |
goto on_error; |
| 101 |
} |
| 102 |
|
| 103 |
provider->shutdown_options = options->shutdown_options; |
| 104 |
return provider; |
| 105 |
|
| 106 |
on_error: |
| 107 |
aws_credentials_provider_destroy(provider); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
struct imds_provider_user_data { |
| 115 |
|
| 116 |
struct aws_allocator *allocator; |
| 117 |
struct aws_credentials_provider *imds_provider; |
| 118 |
aws_on_get_credentials_callback_fn *original_callback; |
| 119 |
struct aws_byte_buf role; |
| 120 |
void *original_user_data; |
| 121 |
}; |
| 122 |
|
| 123 |
static void s_imds_provider_user_data_destroy(struct imds_provider_user_data *user_data) { |
| 124 |
if (user_data == NULL) { |
| 125 |
return; |
| 126 |
} |
| 127 |
aws_byte_buf_clean_up(&user_data->role); |
| 128 |
aws_credentials_provider_release(user_data->imds_provider); |
| 129 |
aws_mem_release(user_data->allocator, user_data); |
| 130 |
} |
| 131 |
|
| 132 |
static struct imds_provider_user_data *s_imds_provider_user_data_new( |
| 133 |
struct aws_credentials_provider *imds_provider, |
| 134 |
aws_on_get_credentials_callback_fn callback, |
| 135 |
void *user_data) { |
| 136 |
|
| 137 |
struct imds_provider_user_data *wrapped_user_data = |
| 138 |
aws_mem_calloc(imds_provider->allocator, 1, sizeof(struct imds_provider_user_data)); |
| 139 |
if (wrapped_user_data == NULL) { |
| 140 |
goto on_error; |
| 141 |
} |
| 142 |
if (aws_byte_buf_init(&wrapped_user_data->role, imds_provider->allocator, 100)) { |
| 143 |
goto on_error; |
| 144 |
} |
| 145 |
wrapped_user_data->allocator = imds_provider->allocator; |
| 146 |
wrapped_user_data->imds_provider = imds_provider; |
| 147 |
aws_credentials_provider_acquire(imds_provider); |
| 148 |
wrapped_user_data->original_user_data = user_data; |
| 149 |
wrapped_user_data->original_callback = callback; |
| 150 |
|
| 151 |
return wrapped_user_data; |
| 152 |
|
| 153 |
on_error: |
| 154 |
s_imds_provider_user_data_destroy(wrapped_user_data); |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
static void s_on_get_credentials(const struct aws_credentials *credentials, int error_code, void *user_data) { |
| 159 |
(void)error_code; |
| 160 |
struct imds_provider_user_data *wrapped_user_data = user_data; |
| 161 |
if (error_code == AWS_OP_SUCCESS) { |
| 162 |
AWS_LOGF_INFO( |
| 163 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 164 |
"id=%p: IMDS provider successfully retrieved credentials", |
| 165 |
(void *)wrapped_user_data->imds_provider); |
| 166 |
} else { |
| 167 |
AWS_LOGF_INFO( |
| 168 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 169 |
"id=%p: IMDS provider failed to retrieve credentials: %s", |
| 170 |
(void *)wrapped_user_data->imds_provider, |
| 171 |
aws_error_str(error_code)); |
| 172 |
} |
| 173 |
wrapped_user_data->original_callback( |
| 174 |
(struct aws_credentials *)credentials, error_code, wrapped_user_data->original_user_data); |
| 175 |
s_imds_provider_user_data_destroy(wrapped_user_data); |
| 176 |
} |
| 177 |
|
| 178 |
static void s_on_get_role(const struct aws_byte_buf *role, int error_code, void *user_data) { |
| 179 |
struct imds_provider_user_data *wrapped_user_data = user_data; |
| 180 |
if (!role || error_code || role->len == 0) { |
| 181 |
goto on_error; |
| 182 |
} |
| 183 |
|
| 184 |
struct aws_byte_cursor role_cursor = aws_byte_cursor_from_buf(role); |
| 185 |
if (aws_byte_buf_append_dynamic(&wrapped_user_data->role, &role_cursor)) { |
| 186 |
goto on_error; |
| 187 |
} |
| 188 |
|
| 189 |
struct aws_credentials_provider_imds_impl *impl = wrapped_user_data->imds_provider->impl; |
| 190 |
if (aws_imds_client_get_credentials( |
| 191 |
impl->client, aws_byte_cursor_from_buf(&wrapped_user_data->role), s_on_get_credentials, user_data)) { |
| 192 |
goto on_error; |
| 193 |
} |
| 194 |
|
| 195 |
return; |
| 196 |
|
| 197 |
on_error: |
| 198 |
AWS_LOGF_INFO( |
| 199 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 200 |
"id=%p: IMDS provider failed to retrieve role: %s", |
| 201 |
(void *)wrapped_user_data->imds_provider, |
| 202 |
aws_error_str(error_code)); |
| 203 |
wrapped_user_data->original_callback( |
| 204 |
NULL, AWS_AUTH_CREDENTIALS_PROVIDER_IMDS_SOURCE_FAILURE, wrapped_user_data->original_user_data); |
| 205 |
s_imds_provider_user_data_destroy(wrapped_user_data); |
| 206 |
} |
| 207 |
|
| 208 |
static int s_credentials_provider_imds_get_credentials_async( |
| 209 |
struct aws_credentials_provider *provider, |
| 210 |
aws_on_get_credentials_callback_fn callback, |
| 211 |
void *user_data) { |
| 212 |
|
| 213 |
AWS_LOGF_DEBUG( |
| 214 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, "id=%p: IMDS provider trying to load credentials", (void *)provider); |
| 215 |
|
| 216 |
struct aws_credentials_provider_imds_impl *impl = provider->impl; |
| 217 |
|
| 218 |
struct imds_provider_user_data *wrapped_user_data = s_imds_provider_user_data_new(provider, callback, user_data); |
| 219 |
if (wrapped_user_data == NULL) { |
| 220 |
goto error; |
| 221 |
} |
| 222 |
|
| 223 |
if (aws_imds_client_get_attached_iam_role(impl->client, s_on_get_role, wrapped_user_data)) { |
| 224 |
goto error; |
| 225 |
} |
| 226 |
|
| 227 |
return AWS_OP_SUCCESS; |
| 228 |
|
| 229 |
error: |
| 230 |
AWS_LOGF_ERROR( |
| 231 |
AWS_LS_AUTH_CREDENTIALS_PROVIDER, |
| 232 |
"id=%p: IMDS provider failed to request credentials: %s", |
| 233 |
(void *)provider, |
| 234 |
aws_error_str(aws_last_error())); |
| 235 |
s_imds_provider_user_data_destroy(wrapped_user_data); |
| 236 |
return AWS_OP_ERR; |
| 237 |
} |