Debian Codegraph @

crt/aws-c-auth/source/credentials_provider_default_chain.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/auth/credentials.h>
7
8 #include <aws/auth/private/credentials_utils.h>
9 #include <aws/common/clock.h>
10 #include <aws/common/environment.h>
11 #include <aws/common/logging.h>
12 #include <aws/common/string.h>
13 #include <aws/io/tls_channel_handler.h>
14 #include <aws/io/uri.h>
15
16 #define DEFAULT_CREDENTIAL_PROVIDER_REFRESH_MS (15 * 60 * 1000)
17
18 #if defined(_MSC_VER)
19 # pragma warning(disable : 4204)
20 /*
21 * For designated initialization: .providers = providers,
22 * of aws_credentials_provider_chain_options in function
23 * aws_credentials_provider_new_chain_default
24 */
25 # pragma warning(disable : 4221)
26 #endif /* _MSC_VER */
27
28 AWS_STATIC_STRING_FROM_LITERAL(s_ec2_creds_env_disable, "AWS_EC2_METADATA_DISABLED");
29
30 /**
31 * ECS and IMDS credentials providers are mutually exclusive,
32 * ECS has higher priority
33 */
34 static struct aws_credentials_provider *s_aws_credentials_provider_new_ecs_or_imds(
35 struct aws_allocator *allocator,
36 const struct aws_credentials_provider_shutdown_options *shutdown_options,
37 struct aws_client_bootstrap *bootstrap,
38 struct aws_tls_ctx *tls_ctx,
39 const struct proxy_env_var_settings *proxy_ev_settings) {
40
41 /* Try to create the ECS provider. This will fail if its environment variables aren't set */
42 struct aws_credentials_provider_ecs_environment_options ecs_options = {
43 .shutdown_options = *shutdown_options,
44 .bootstrap = bootstrap,
45 .tls_ctx = tls_ctx,
46 .proxy_ev_settings = proxy_ev_settings,
47 };
48 struct aws_credentials_provider *ecs_provider =
49 aws_credentials_provider_new_ecs_from_environment(allocator, &ecs_options);
50 if (ecs_provider != NULL) {
51 AWS_LOGF_INFO(
52 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
53 "default chain: ECS credentials provider will be used to retrieve credentials");
54 return ecs_provider;
55 }
56
57 /* Can we do IMDS? */
58 struct aws_string *ec2_imds_disable = NULL;
59 aws_get_environment_value(allocator, s_ec2_creds_env_disable, &ec2_imds_disable);
60 bool imds_enabled = ec2_imds_disable == NULL || aws_string_eq_c_str_ignore_case(ec2_imds_disable, "false");
61 aws_string_destroy(ec2_imds_disable);
62 if (imds_enabled) {
63 AWS_LOGF_INFO(
64 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
65 "default chain: IMDS credentials provider will be used to retrieve credentials");
66 struct aws_credentials_provider_imds_options imds_options = {
67 .shutdown_options = *shutdown_options,
68 .bootstrap = bootstrap,
69 .proxy_ev_settings = proxy_ev_settings,
70 };
71 return aws_credentials_provider_new_imds(allocator, &imds_options);
72 }
73
74 AWS_LOGF_INFO(
75 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "default chain: neither ECS nor IMDS will be used to retrieve credentials");
76 return NULL;
77 }
78
79 struct default_chain_callback_data {
80 struct aws_allocator *allocator;
81 struct aws_credentials_provider *default_chain_provider;
82 aws_on_get_credentials_callback_fn *original_callback;
83 void *original_user_data;
84 };
85
86 static struct default_chain_callback_data *s_create_callback_data(
87 struct aws_credentials_provider *provider,
88 aws_on_get_credentials_callback_fn *callback,
89 void *user_data) {
90 struct default_chain_callback_data *callback_data =
91 aws_mem_calloc(provider->allocator, 1, sizeof(struct default_chain_callback_data));
92 if (callback_data == NULL) {
93 return NULL;
94 }
95 callback_data->allocator = provider->allocator;
96 callback_data->default_chain_provider = provider;
97 callback_data->original_callback = callback;
98 callback_data->original_user_data = user_data;
99
100 aws_credentials_provider_acquire(provider);
101
102 return callback_data;
103 }
104
105 static void s_destroy_callback_data(struct default_chain_callback_data *callback_data) {
106 aws_credentials_provider_release(callback_data->default_chain_provider);
107 aws_mem_release(callback_data->allocator, callback_data);
108 }
109
110 struct aws_credentials_provider_default_chain_impl {
111 struct aws_atomic_var shutdowns_remaining;
112 struct aws_credentials_provider *cached_provider;
113 };
114
115 static void s_aws_provider_default_chain_callback(
116 struct aws_credentials *credentials,
117 int error_code,
118 void *user_data) {
119 struct default_chain_callback_data *callback_data = user_data;
120 struct aws_credentials_provider *provider = callback_data->default_chain_provider;
121
122 if (credentials != NULL) {
123 AWS_LOGF_INFO(
124 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
125 "(id=%p) Default chain credentials provider successfully sourced credentials",
126 (void *)provider);
127 } else {
128 AWS_LOGF_ERROR(
129 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
130 "(id=%p) Default chain credentials provider failed to source credentials with error %d(%s)",
131 (void *)provider,
132 error_code,
133 aws_error_debug_str(error_code));
134 }
135
136 callback_data->original_callback(credentials, error_code, callback_data->original_user_data);
137 s_destroy_callback_data(callback_data);
138 }
139
140 static int s_credentials_provider_default_chain_get_credentials_async(
141 struct aws_credentials_provider *provider,
142 aws_on_get_credentials_callback_fn callback,
143 void *user_data) {
144
145 struct aws_credentials_provider_default_chain_impl *impl = provider->impl;
146
147 AWS_LOGF_DEBUG(
148 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
149 "(id=%p) Credentials provider chain get credentials dispatch",
150 (void *)provider);
151
152 struct default_chain_callback_data *callback_data = s_create_callback_data(provider, callback, user_data);
153 if (callback_data == NULL) {
154 return AWS_OP_ERR;
155 }
156
157 int result = aws_credentials_provider_get_credentials(
158 impl->cached_provider, s_aws_provider_default_chain_callback, callback_data);
159 if (result != AWS_OP_SUCCESS) {
160 s_destroy_callback_data(callback_data);
161 }
162
163 return result;
164 }
165
166 static void s_on_sub_provider_shutdown_completed(void *user_data) {
167 struct aws_credentials_provider *provider = user_data;
168 struct aws_credentials_provider_default_chain_impl *impl = provider->impl;
169
170 size_t remaining = aws_atomic_fetch_sub(&impl->shutdowns_remaining, 1);
171 if (remaining != 1) {
172 return;
173 }
174
175 /* Invoke our own shutdown callback */
176 aws_credentials_provider_invoke_shutdown_callback(provider);
177
178 aws_mem_release(provider->allocator, provider);
179 }
180
181 static void s_credentials_provider_default_chain_destroy(struct aws_credentials_provider *provider) {
182 struct aws_credentials_provider_default_chain_impl *impl = provider->impl;
183 if (impl == NULL) {
184 return;
185 }
186
187 aws_credentials_provider_release(impl->cached_provider);
188
189 s_on_sub_provider_shutdown_completed(provider);
190 }
191
192 static struct aws_credentials_provider_vtable s_aws_credentials_provider_default_chain_vtable = {
193 .get_credentials = s_credentials_provider_default_chain_get_credentials_async,
194 .destroy = s_credentials_provider_default_chain_destroy,
195 };
196
197 /*
198 * Default provider chain implementation
199 */
200 struct aws_credentials_provider *aws_credentials_provider_new_chain_default(
201 struct aws_allocator *allocator,
202 const struct aws_credentials_provider_chain_default_options *options) {
203
204 struct aws_credentials_provider *provider = NULL;
205 struct aws_credentials_provider_default_chain_impl *impl = NULL;
206
207 aws_mem_acquire_many(
208 allocator,
209 2,
210 &provider,
211 sizeof(struct aws_credentials_provider),
212 &impl,
213 sizeof(struct aws_credentials_provider_default_chain_impl));
214
215 if (!provider) {
216 return NULL;
217 }
218
219 AWS_ZERO_STRUCT(*provider);
220 AWS_ZERO_STRUCT(*impl);
221
222 aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_default_chain_vtable, impl);
223 provider->shutdown_options = options->shutdown_options;
224
225 /* 1 shutdown call from the provider's destroy itself */
226 aws_atomic_init_int(&impl->shutdowns_remaining, 1);
227
228 struct aws_credentials_provider_shutdown_options sub_provider_shutdown_options;
229 AWS_ZERO_STRUCT(sub_provider_shutdown_options);
230 sub_provider_shutdown_options.shutdown_callback = s_on_sub_provider_shutdown_completed;
231 sub_provider_shutdown_options.shutdown_user_data = provider;
232
233 struct aws_tls_ctx *tls_ctx = NULL;
234 struct aws_credentials_provider *environment_provider = NULL;
235 struct aws_credentials_provider *profile_provider = NULL;
236 struct aws_credentials_provider *sts_provider = NULL;
237 struct aws_credentials_provider *ecs_or_imds_provider = NULL;
238 struct aws_credentials_provider *chain_provider = NULL;
239 struct aws_credentials_provider *cached_provider = NULL;
240
241 if (options->tls_ctx) {
242 tls_ctx = aws_tls_ctx_acquire(options->tls_ctx);
243 } else {
244 #ifdef BYO_CRYPTO
245 aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
246 AWS_LOGF_ERROR(AWS_LS_AUTH_CREDENTIALS_PROVIDER, "TLS context must be provided to credentials provider.");
247 goto on_error;
248 #else
249 AWS_LOGF_INFO(
250 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
251 "(id=%p): TLS context not provided, initializing a new one for credentials provider.",
252 (void *)provider);
253 struct aws_tls_ctx_options tls_options;
254 aws_tls_ctx_options_init_default_client(&tls_options, allocator);
255 tls_ctx = aws_tls_client_ctx_new(allocator, &tls_options);
256 aws_tls_ctx_options_clean_up(&tls_options);
257 if (!tls_ctx) {
258 AWS_LOGF_ERROR(
259 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
260 "(id=%p): failed to create a TLS context with error %s",
261 (void *)provider,
262 aws_error_debug_str(aws_last_error()));
263 goto on_error;
264 }
265 #endif /* BYO_CRYPTO */
266 }
267
268 enum { providers_size = 4 };
269 struct aws_credentials_provider *providers[providers_size];
270 AWS_ZERO_ARRAY(providers);
271 size_t index = 0;
272
273 /* Providers that touch fast local resources... */
274 if (!options->skip_environment_credentials_provider) {
275 struct aws_credentials_provider_environment_options environment_options;
276 AWS_ZERO_STRUCT(environment_options);
277 environment_provider = aws_credentials_provider_new_environment(allocator, &environment_options);
278 if (environment_provider == NULL) {
279 goto on_error;
280 }
281 providers[index++] = environment_provider;
282 }
283
284 /* Providers that will make a network call only if the relevant configuration is present... */
285
286 struct aws_credentials_provider_profile_options profile_options;
287 AWS_ZERO_STRUCT(profile_options);
288 profile_options.bootstrap = options->bootstrap;
289 profile_options.tls_ctx = tls_ctx;
290 profile_options.shutdown_options = sub_provider_shutdown_options;
291 profile_options.profile_collection_cached = options->profile_collection_cached;
292 profile_options.profile_name_override = options->profile_name_override;
293 profile_provider = aws_credentials_provider_new_profile(allocator, &profile_options);
294 if (profile_provider != NULL) {
295 providers[index++] = profile_provider;
296 /* 1 shutdown call from the profile provider's shutdown */
297 aws_atomic_fetch_add(&impl->shutdowns_remaining, 1);
298 }
299
300 struct aws_credentials_provider_sts_web_identity_options sts_options;
301 AWS_ZERO_STRUCT(sts_options);
302 sts_options.bootstrap = options->bootstrap;
303 sts_options.tls_ctx = tls_ctx;
304 sts_options.shutdown_options = sub_provider_shutdown_options;
305 sts_options.config_profile_collection_cached = options->profile_collection_cached;
306 sts_options.profile_name_override = options->profile_name_override;
307 sts_options.proxy_ev_settings = options->proxy_ev_settings;
308 sts_provider = aws_credentials_provider_new_sts_web_identity(allocator, &sts_options);
309 if (sts_provider != NULL) {
310 providers[index++] = sts_provider;
311 /* 1 shutdown call from the web identity provider's shutdown */
312 aws_atomic_fetch_add(&impl->shutdowns_remaining, 1);
313 }
314
315 /* Providers that will always make a network call unless explicitly disabled... */
316
317 ecs_or_imds_provider = s_aws_credentials_provider_new_ecs_or_imds(
318 allocator, &sub_provider_shutdown_options, options->bootstrap, tls_ctx, options->proxy_ev_settings);
319 if (ecs_or_imds_provider != NULL) {
320 providers[index++] = ecs_or_imds_provider;
321 /* 1 shutdown call from the imds or ecs provider's shutdown */
322 aws_atomic_fetch_add(&impl->shutdowns_remaining, 1);
323 }
324
325 AWS_FATAL_ASSERT(index <= providers_size);
326
327 struct aws_credentials_provider_chain_options chain_options = {
328 .provider_count = index,
329 .providers = providers,
330 };
331
332 chain_provider = aws_credentials_provider_new_chain(allocator, &chain_options);
333 if (chain_provider == NULL) {
334 goto on_error;
335 }
336
337 /*
338 * Transfer ownership
339 */
340 aws_credentials_provider_release(environment_provider);
341 aws_credentials_provider_release(profile_provider);
342 aws_credentials_provider_release(sts_provider);
343 aws_credentials_provider_release(ecs_or_imds_provider);
344
345 struct aws_credentials_provider_cached_options cached_options = {
346 .source = chain_provider,
347 .refresh_time_in_milliseconds = DEFAULT_CREDENTIAL_PROVIDER_REFRESH_MS,
348 };
349
350 cached_provider = aws_credentials_provider_new_cached(allocator, &cached_options);
351 if (cached_provider == NULL) {
352 goto on_error;
353 }
354
355 /*
356 * Transfer ownership
357 */
358 aws_credentials_provider_release(chain_provider);
359
360 impl->cached_provider = cached_provider;
361
362 /* Subproviders have their own reference to the tls_ctx now */
363 aws_tls_ctx_release(tls_ctx);
364
365 return provider;
366
367 on_error:
368
369 /*
370 * Have to be a bit more careful than normal with this clean up pattern since the chain/cache will
371 * recursively destroy the other providers via ref release.
372 *
373 * Technically, the cached_provider can never be non-null here, but let's handle it anyways
374 * in case someone does something weird in the future.
375 */
376 if (cached_provider) {
377 aws_credentials_provider_release(cached_provider);
378 } else if (chain_provider) {
379 aws_credentials_provider_release(chain_provider);
380 } else {
381 aws_credentials_provider_release(ecs_or_imds_provider);
382 aws_credentials_provider_release(profile_provider);
383 aws_credentials_provider_release(sts_provider);
384 aws_credentials_provider_release(environment_provider);
385 }
386
387 aws_tls_ctx_release(tls_ctx);
388
389 aws_mem_release(allocator, provider);
390
391 return NULL;
392 }