Debian Codegraph @

crt/aws-c-auth/source/credentials_provider_cached.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/mutex.h>
11 #include <aws/common/time.h>
12
13 #include <inttypes.h>
14
15 /*
16
17 ToDo: credentials expiration environment overrides
18
19 AWS_STATIC_STRING_FROM_LITERAL(s_credential_expiration_env_var, "AWS_CREDENTIAL_EXPIRATION");
20
21 */
22
23 #define REFRESH_CREDENTIALS_EARLY_DURATION_SECONDS 60 * 5 /* 5 minutes */
24
25 struct aws_credentials_provider_cached {
26 struct aws_credentials_provider *source;
27 struct aws_credentials *cached_credentials;
28 struct aws_mutex lock;
29 uint64_t refresh_interval_in_ns;
30 uint64_t next_refresh_time;
31 aws_io_clock_fn *high_res_clock_fn;
32 aws_io_clock_fn *system_clock_fn;
33 struct aws_linked_list pending_queries;
34 };
35
36 static void s_aws_credentials_query_list_notify_and_clean_up(
37 struct aws_linked_list *query_list,
38 struct aws_allocator *allocator,
39 struct aws_credentials *credentials,
40 int error_code) {
41
42 while (!aws_linked_list_empty(query_list)) {
43 struct aws_linked_list_node *node = aws_linked_list_pop_front(query_list);
44 struct aws_credentials_query *query = AWS_CONTAINER_OF(node, struct aws_credentials_query, node);
45 query->callback(credentials, error_code, query->user_data);
46 aws_credentials_query_clean_up(query);
47 aws_mem_release(allocator, query);
48 }
49 }
50
51 static void s_swap_cached_credentials(
52 struct aws_credentials_provider *provider,
53 struct aws_credentials *new_credentials) {
54 struct aws_credentials_provider_cached *cached_provider = provider->impl;
55
56 aws_credentials_release(cached_provider->cached_credentials);
57 cached_provider->cached_credentials = new_credentials;
58 if (cached_provider->cached_credentials != NULL) {
59 aws_credentials_acquire(cached_provider->cached_credentials);
60
61 AWS_LOGF_DEBUG(
62 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
63 "(id=%p) Cached credentials provider successfully sourced credentials on refresh",
64 (void *)provider);
65 } else {
66 AWS_LOGF_DEBUG(
67 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
68 "(id=%p) Cached credentials provider was unable to source credentials on refresh",
69 (void *)provider);
70 }
71 }
72
73 static void s_cached_credentials_provider_get_credentials_async_callback(
74 struct aws_credentials *credentials,
75 int error_code,
76 void *user_data) {
77
78 struct aws_credentials_provider *provider = user_data;
79 struct aws_credentials_provider_cached *impl = provider->impl;
80
81 aws_mutex_lock(&impl->lock);
82
83 /*
84 * Move pending queries so that we can do notifications outside the lock
85 */
86 struct aws_linked_list pending_queries;
87 aws_linked_list_init(&pending_queries);
88
89 aws_linked_list_swap_contents(&pending_queries, &impl->pending_queries);
90
91 uint64_t next_refresh_time_in_ns = UINT64_MAX;
92
93 uint64_t high_res_now = 0;
94 if (!impl->high_res_clock_fn(&high_res_now)) {
95 if (impl->refresh_interval_in_ns > 0) {
96 next_refresh_time_in_ns = high_res_now + impl->refresh_interval_in_ns;
97 }
98
99 uint64_t credentials_expiration_timepoint_seconds = UINT64_MAX;
100 if (credentials != NULL) {
101 credentials_expiration_timepoint_seconds = aws_credentials_get_expiration_timepoint_seconds(credentials);
102 }
103
104 /*
105 * If the sourced credentials have an explicit expiration time, we should always use that time
106 * rather than the much cruder, mechanical refresh setting on the caching wrapper.
107 */
108 if (credentials_expiration_timepoint_seconds < UINT64_MAX) {
109 uint64_t system_now = 0;
110 if (!impl->system_clock_fn(&system_now)) {
111
112 uint64_t system_now_seconds =
113 aws_timestamp_convert(system_now, AWS_TIMESTAMP_NANOS, AWS_TIMESTAMP_SECS, NULL);
114 if (credentials_expiration_timepoint_seconds >=
115 system_now_seconds + REFRESH_CREDENTIALS_EARLY_DURATION_SECONDS) {
116 next_refresh_time_in_ns = high_res_now;
117 next_refresh_time_in_ns += aws_timestamp_convert(
118 credentials_expiration_timepoint_seconds - system_now_seconds -
119 REFRESH_CREDENTIALS_EARLY_DURATION_SECONDS,
120 AWS_TIMESTAMP_SECS,
121 AWS_TIMESTAMP_NANOS,
122 NULL);
123 } else {
124 next_refresh_time_in_ns = high_res_now;
125 }
126 }
127 }
128 }
129
130 impl->next_refresh_time = next_refresh_time_in_ns;
131
132 AWS_LOGF_DEBUG(
133 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
134 "(id=%p) Cached credentials provider next refresh time set to %" PRIu64,
135 (void *)provider,
136 impl->next_refresh_time);
137
138 s_swap_cached_credentials(provider, credentials);
139
140 aws_mutex_unlock(&impl->lock);
141
142 AWS_LOGF_DEBUG(
143 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
144 "(id=%p) Cached credentials provider notifying pending queries of new credentials",
145 (void *)provider);
146
147 s_aws_credentials_query_list_notify_and_clean_up(&pending_queries, provider->allocator, credentials, error_code);
148 }
149
150 static int s_cached_credentials_provider_get_credentials_async(
151 struct aws_credentials_provider *provider,
152 aws_on_get_credentials_callback_fn callback,
153 void *user_data) {
154
155 struct aws_credentials_provider_cached *impl = provider->impl;
156
157 uint64_t current_time = 0;
158 impl->high_res_clock_fn(&current_time);
159
160 bool should_submit_query = false;
161 bool perform_callback = false;
162 struct aws_credentials *credentials = NULL;
163
164 aws_mutex_lock(&impl->lock);
165
166 if (impl->cached_credentials != NULL && current_time < impl->next_refresh_time) {
167 perform_callback = true;
168 credentials = impl->cached_credentials;
169 aws_credentials_acquire(credentials);
170 } else {
171 struct aws_credentials_query *query =
172 aws_mem_acquire(provider->allocator, sizeof(struct aws_credentials_query));
173 if (query != NULL) {
174 aws_credentials_query_init(query, provider, callback, user_data);
175 should_submit_query = aws_linked_list_empty(&impl->pending_queries);
176 aws_linked_list_push_back(&impl->pending_queries, &query->node);
177 } else {
178 perform_callback = true;
179 }
180 }
181
182 aws_mutex_unlock(&impl->lock);
183
184 if (should_submit_query) {
185 AWS_LOGF_INFO(
186 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
187 "(id=%p) Cached credentials provider has expired credentials. Requerying.",
188 (void *)provider);
189
190 aws_credentials_provider_get_credentials(
191 impl->source, s_cached_credentials_provider_get_credentials_async_callback, provider);
192
193 } else if (!perform_callback) {
194 AWS_LOGF_DEBUG(
195 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
196 "(id=%p) Cached credentials provider has expired credentials. Waiting on existing query.",
197 (void *)provider);
198 }
199
200 if (perform_callback) {
201 if (credentials != NULL) {
202 AWS_LOGF_DEBUG(
203 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
204 "(id=%p) Cached credentials provider successfully sourced from cache",
205 (void *)provider);
206 } else {
207 AWS_LOGF_DEBUG(
208 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
209 "(id=%p) Cached credentials provider failed to source credentials while skipping requery",
210 (void *)provider);
211 }
212 callback(credentials, (credentials != NULL) ? AWS_ERROR_SUCCESS : aws_last_error(), user_data);
213 aws_credentials_release(credentials);
214 }
215
216 return AWS_OP_SUCCESS;
217 }
218
219 static void s_cached_credentials_provider_destroy(struct aws_credentials_provider *provider) {
220 struct aws_credentials_provider_cached *impl = provider->impl;
221 if (impl == NULL) {
222 return;
223 }
224
225 aws_credentials_provider_release(impl->source);
226
227 /* Invoke our own shutdown callback */
228 aws_credentials_provider_invoke_shutdown_callback(provider);
229
230 if (impl->cached_credentials != NULL) {
231 aws_credentials_release(impl->cached_credentials);
232 }
233
234 aws_mutex_clean_up(&impl->lock);
235
236 aws_mem_release(provider->allocator, provider);
237 }
238
239 static struct aws_credentials_provider_vtable s_aws_credentials_provider_cached_vtable = {
240 .get_credentials = s_cached_credentials_provider_get_credentials_async,
241 .destroy = s_cached_credentials_provider_destroy,
242 };
243
244 struct aws_credentials_provider *aws_credentials_provider_new_cached(
245 struct aws_allocator *allocator,
246 const struct aws_credentials_provider_cached_options *options) {
247
248 AWS_ASSERT(options->source != NULL);
249
250 struct aws_credentials_provider *provider = NULL;
251 struct aws_credentials_provider_cached *impl = NULL;
252
253 aws_mem_acquire_many(
254 allocator,
255 2,
256 &provider,
257 sizeof(struct aws_credentials_provider),
258 &impl,
259 sizeof(struct aws_credentials_provider_cached));
260
261 if (!provider) {
262 return NULL;
263 }
264
265 AWS_ZERO_STRUCT(*provider);
266 AWS_ZERO_STRUCT(*impl);
267
268 aws_credentials_provider_init_base(provider, allocator, &s_aws_credentials_provider_cached_vtable, impl);
269
270 if (aws_mutex_init(&impl->lock)) {
271 goto on_error;
272 }
273
274 aws_linked_list_init(&impl->pending_queries);
275
276 impl->source = options->source;
277 aws_credentials_provider_acquire(impl->source);
278
279 if (options->refresh_time_in_milliseconds > 0) {
280 impl->refresh_interval_in_ns = aws_timestamp_convert(
281 options->refresh_time_in_milliseconds, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
282 } else {
283 /*
284 * TODO: query AWS_CREDENTIAL_EXPIRATION for a refresh override
285 *
286 * This must be an ISO 8601 time interval which we don't have a parser for yet (one could be cobbled
287 * together from the existing timestamp parser). Does not seem important enough to get bogged down in atm.
288 * Punting for now.
289 */
290 impl->refresh_interval_in_ns = 0;
291 }
292
293 if (options->high_res_clock_fn != NULL) {
294 impl->high_res_clock_fn = options->high_res_clock_fn;
295 } else {
296 impl->high_res_clock_fn = &aws_high_res_clock_get_ticks;
297 }
298
299 if (options->system_clock_fn != NULL) {
300 impl->system_clock_fn = options->system_clock_fn;
301 } else {
302 impl->system_clock_fn = &aws_sys_clock_get_ticks;
303 }
304
305 provider->shutdown_options = options->shutdown_options;
306
307 return provider;
308
309 on_error:
310
311 aws_credentials_provider_destroy(provider);
312
313 return NULL;
314 }