Debian Codegraph @

crt/aws-c-sdkutils/source/endpoints_rule_engine.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/common/byte_buf.h>
7 #include <aws/common/json.h>
8 #include <aws/common/macros.h>
9 #include <aws/common/string.h>
10 #include <aws/sdkutils/partitions.h>
11 #include <aws/sdkutils/private/endpoints_types_impl.h>
12 #include <aws/sdkutils/private/endpoints_util.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15
16 /* TODO: checking for unknown enum values is annoying and is brittle. compile
17 time assert on enum size or members would make it a lot simpler. */
18
19 /*
20 * How rule resolution works.
21 * Note: read comments in endpoint_types_impl.h first to understand type system.
22 *
23 * Initial scope is created from parameters defined in request context and
24 * default values defined in ruleset (s_init_top_level_scope). Validation that
25 * all required parameters have values is done at this point as well.
26 *
27 * Rules are then resolved sequentially against scope.
28 * First list of conditions associated with the rule is resolved
29 * (s_resolve_conditions). Final result of conditions resolution is an AND of
30 * truthiness of resolved values (as defined in is_value_truthy) for each
31 * condition. If resolution is true then rule is selected.
32 * - For endpoint and error rules that means terminal state is reached and rule
33 * data is returned
34 * - For tree rule, the engine starts resolving rules associated with tree rule.
35 * Note: tree rules are terminal and once engine jumps into tree rule
36 * resolution there is no way to jump back out.
37 *
38 * Conditions can add values to scope. Those values are valid for the duration of
39 * rule resolution. Note: for tree rules, any values added in tree conditions are
40 * valid for all rules within the tree.
41 * Scope can be though of as a 'leveled' structure. Top level or 0 level
42 * represents all values from context and defaults. Levels 1 and up represent
43 * values added by rules. Ex. if we start at level 0, all values added by rule
44 * can be though of as level 1.
45 * Since tree rule cannot be exited from, engine is simplified by making all
46 * values in scope top level whenever tree is jumped into. So in practice engine
47 * goes back between top level and first level as resolving rules. If that
48 * changes in future, scope can add explicit level number and cleanup only values
49 * at that level when going to next rule.
50 *
51 * Overall flow is as follows:
52 * - Start with any values provided in context as scope
53 * - Add any default values provided in ruleset and validate all required
54 * params are specified.
55 * - Iterate through rules and resolve each rule:
56 * -- resolve conditions with side effects
57 * -- if conditions are truthy return rule result
58 * -- if conditions are truthy and rule is tree, jump down a level and
59 * restart resolution with tree rules
60 * -- if conditions are falsy, rollback level and go to next rule
61 * - if no rules match, resolution fails with exhausted error.
62 */
63
64 static void s_scope_value_destroy_cb(void *data) {
65 struct aws_endpoints_scope_value *value = data;
66 aws_endpoints_scope_value_destroy(value);
67 }
68
69 static int s_deep_copy_context_to_scope(
70 struct aws_allocator *allocator,
71 const struct aws_endpoints_request_context *context,
72 struct aws_endpoints_resolution_scope *scope) {
73
74 struct aws_endpoints_scope_value *new_value = NULL;
75
76 for (struct aws_hash_iter iter = aws_hash_iter_begin(&context->values); !aws_hash_iter_done(&iter);
77 aws_hash_iter_next(&iter)) {
78
79 struct aws_endpoints_scope_value *context_value = (struct aws_endpoints_scope_value *)iter.element.value;
80
81 /* Own the name: context_value may be freed after resolve returns */
82 new_value = aws_endpoints_scope_value_new(allocator, context_value->name.cur, true);
83 if (aws_endpoints_deep_copy_parameter_value(allocator, &context_value->value, &new_value->value)) {
84 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to deep copy value.");
85 goto on_error;
86 }
87
88 if (aws_hash_table_put(&scope->values, &new_value->name.cur, new_value, NULL)) {
89 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add deep copy to scope.");
90 goto on_error;
91 }
92 }
93
94 return AWS_OP_SUCCESS;
95
96 on_error:
97 aws_endpoints_scope_value_destroy(new_value);
98 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED);
99 }
100
101 static int s_init_top_level_scope(
102 struct aws_allocator *allocator,
103 const struct aws_endpoints_request_context *context,
104 const struct aws_endpoints_ruleset *ruleset,
105 const struct aws_partitions_config *partitions,
106 struct aws_endpoints_resolution_state *state) {
107 AWS_PRECONDITION(allocator);
108 AWS_PRECONDITION(context);
109 AWS_PRECONDITION(ruleset);
110 AWS_PRECONDITION(state);
111
112 state->rule_idx = 0;
113 state->rules = &ruleset->rules;
114 state->scope.partitions = partitions;
115 state->scope.expr_index = ruleset->exprs;
116
117 if (aws_hash_table_init(
118 &state->scope.values,
119 allocator,
120 0,
121 aws_hash_byte_cursor_ptr,
122 aws_endpoints_byte_cursor_eq,
123 NULL,
124 s_scope_value_destroy_cb)) {
125 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init request context values.");
126 goto on_error;
127 }
128
129 if (s_deep_copy_context_to_scope(allocator, context, &state->scope)) {
130 goto on_error;
131 }
132
133 if (aws_array_list_init_dynamic(&state->added_keys, allocator, 10, sizeof(struct aws_byte_cursor))) {
134 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init added keys.");
135 goto on_error;
136 }
137
138 /* Add defaults to the top level scope. */
139 for (struct aws_hash_iter iter = aws_hash_iter_begin(&ruleset->parameters); !aws_hash_iter_done(&iter);
140 aws_hash_iter_next(&iter)) {
141 const struct aws_byte_cursor key = *(const struct aws_byte_cursor *)iter.element.key;
142 struct aws_endpoints_parameter *value = (struct aws_endpoints_parameter *)iter.element.value;
143
144 /* Skip non-required values, since they cannot have default values. */
145 if (!value->is_required) {
146 continue;
147 }
148
149 struct aws_hash_element *existing = NULL;
150 if (aws_hash_table_find(&state->scope.values, &key, &existing)) {
151 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init request context values.");
152 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED);
153 }
154
155 if (existing == NULL) {
156 if (!value->has_default_value) {
157 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "No value or default for required parameter.");
158 goto on_error;
159 }
160
161 /* Non-owning: key points into ruleset which outlives the scope */
162 struct aws_endpoints_scope_value *val = aws_endpoints_scope_value_new(allocator, key, false);
163 AWS_ASSERT(val);
164
165 switch (value->type) {
166 case AWS_ENDPOINTS_PARAMETER_STRING:
167 case AWS_ENDPOINTS_PARAMETER_BOOLEAN:
168 case AWS_ENDPOINTS_PARAMETER_STRING_ARRAY:
169 val->value = value->default_value;
170 val->value.is_ref = true;
171 break;
172 default:
173 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected parameter type.");
174 aws_endpoints_scope_value_destroy(val);
175 goto on_error;
176 }
177
178 if (aws_hash_table_put(&state->scope.values, &val->name.cur, val, NULL)) {
179 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add value to top level scope.");
180 aws_endpoints_scope_value_destroy(val);
181 goto on_error;
182 }
183 }
184 }
185
186 return AWS_OP_SUCCESS;
187
188 on_error:
189 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED);
190 }
191
192 static void s_state_clean_up(struct aws_endpoints_resolution_state *state) {
193 AWS_PRECONDITION(state);
194
195 aws_hash_table_clean_up(&state->scope.values);
196 aws_array_list_clean_up(&state->added_keys);
197 }
198
199 /*
200 ******************************
201 * Expr/String resolve
202 ******************************
203 */
204
205 static int s_resolve_one_condition(
206 struct aws_allocator *allocator,
207 struct aws_endpoints_condition *condition,
208 struct aws_endpoints_resolution_state *state,
209 bool *out_is_truthy) {
210
211 struct aws_endpoints_scope_value *scope_value = NULL;
212
213 struct aws_endpoints_value val;
214 if (aws_endpoints_resolve_expr(allocator, condition->expr_ref, &state->scope, &val)) {
215 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve expr.");
216 goto on_error;
217 }
218
219 *out_is_truthy = aws_endpoints_is_value_truthy(&val);
220
221 /* Note: assigning value is skipped if condition is falsy, since nothing can
222 use it and that avoids adding value and then removing it from scope right away. */
223 if (*out_is_truthy && condition->assign.len > 0) {
224 /* If condition assigns a value, push it to scope and let scope
225 handle value memory. */
226 /* Non-owning: condition->assign points into ruleset which outlives the scope */
227 scope_value = aws_endpoints_scope_value_new(allocator, condition->assign, false);
228 scope_value->value = val;
229
230 if (aws_array_list_push_back(&state->added_keys, &scope_value->name.cur)) {
231 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to update key at given scope.");
232 goto on_error;
233 }
234
235 int was_created = 1;
236 if (aws_hash_table_put(&state->scope.values, &scope_value->name.cur, scope_value, &was_created)) {
237 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to set assigned variable.");
238 goto on_error;
239 }
240
241 /* Shadowing existing values is prohibited. */
242 if (!was_created) {
243 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Assigned variable shadows existing one.");
244 goto on_error;
245 }
246 } else {
247 /* Otherwise clean up temp value */
248 aws_endpoints_value_clean_up(&val);
249 }
250
251 return AWS_OP_SUCCESS;
252
253 on_error:
254 aws_endpoints_scope_value_destroy(scope_value);
255 /* Only cleanup value if mem ownership was not transferred to scope value. */
256 if (scope_value == NULL) {
257 aws_endpoints_value_clean_up(&val);
258 }
259
260 *out_is_truthy = false;
261 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
262 }
263
264 static int s_resolve_conditions(
265 struct aws_allocator *allocator,
266 const struct aws_array_list *conditions,
267 struct aws_endpoints_resolution_state *state,
268 bool *out_is_truthy) {
269
270 /* Note: spec defines empty conditions list as truthy. */
271 *out_is_truthy = true;
272
273 for (size_t idx = 0; idx < aws_array_list_length(conditions); ++idx) {
274 struct aws_endpoints_condition *condition = NULL;
275 if (aws_array_list_get_at_ptr(conditions, (void **)&condition, idx)) {
276 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to retrieve condition.");
277 goto on_error;
278 }
279
280 if (s_resolve_one_condition(allocator, condition, state, out_is_truthy)) {
281 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve condition.");
282 goto on_error;
283 }
284
285 /* truthiness of all conditions is an AND of truthiness for each condition,
286 hence first false one short circuits resolution */
287 if (!*out_is_truthy) {
288 break;
289 }
290 }
291
292 return AWS_OP_SUCCESS;
293
294 on_error:
295 *out_is_truthy = false;
296 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
297 }
298
299 int aws_endpoints_path_through_array(
300 struct aws_allocator *allocator,
301 struct aws_endpoints_resolution_scope *scope,
302 struct aws_endpoints_value *value,
303 struct aws_byte_cursor path_cur,
304 struct aws_endpoints_value *out_value) {
305 (void)allocator;
306 (void)scope;
307
308 AWS_PRECONDITION(value->type == AWS_ENDPOINTS_VALUE_ARRAY);
309
310 int64_t index;
311 struct aws_byte_cursor split = {0};
312 if ((!aws_byte_cursor_next_split(&path_cur, '[', &split) || split.len > 0) ||
313 !aws_byte_cursor_next_split(&path_cur, ']', &split) || aws_byte_cursor_utf8_parse_i64(split, &index)) {
314 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Could not parse index from template string.");
315 goto on_error;
316 }
317
318 if (index >= (int64_t)aws_array_list_length(&value->v.array)) {
319 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
320 return AWS_OP_SUCCESS;
321 }
322
323 if (index < 0) {
324 index = aws_array_list_length(&value->v.array) - index;
325 }
326
327 if (index < 0) {
328 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected negative index.");
329 goto on_error;
330 }
331
332 struct aws_endpoints_value *val = NULL;
333 if (aws_array_list_get_at_ptr(&value->v.array, (void **)&val, (size_t)index)) {
334 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to index into resolved value");
335 goto on_error;
336 }
337
338 *out_value = *val;
339 out_value->is_ref = true;
340
341 return AWS_OP_SUCCESS;
342
343 on_error:
344 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
345 }
346
347 int aws_endpoints_path_through_object(
348 struct aws_allocator *allocator,
349 struct aws_endpoints_value *value,
350 struct aws_byte_cursor path_cur,
351 struct aws_endpoints_value *out_value) {
352
353 AWS_ZERO_STRUCT(*out_value);
354 struct aws_json_value *root_node = NULL;
355
356 struct aws_byte_cursor value_cur = value->type != AWS_ENDPOINTS_VALUE_STRING ? value->v.owning_cursor_string.cur
357 : value->v.owning_cursor_object.cur;
358
359 root_node = aws_json_value_new_from_string(allocator, value_cur);
360 const struct aws_json_value *result;
361 if (aws_path_through_json(allocator, root_node, path_cur, &result)) {
362 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to path through json.");
363 goto on_error;
364 }
365
366 if (result == NULL) {
367 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
368 } else if (aws_json_value_is_string(result)) {
369 struct aws_byte_cursor final;
370 if (aws_json_value_get_string(result, &final)) {
371 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Could not parse string from node.");
372 goto on_error;
373 }
374
375 out_value->type = AWS_ENDPOINTS_VALUE_STRING;
376 out_value->v.owning_cursor_string = aws_endpoints_owning_cursor_from_cursor(allocator, final);
377 } else if (aws_json_value_is_array(result) || aws_json_value_is_object(result)) {
378 struct aws_byte_buf json_blob;
379 aws_byte_buf_init(&json_blob, allocator, 0);
380
381 if (aws_byte_buf_append_json_string(result, &json_blob)) {
382 aws_byte_buf_clean_up(&json_blob);
383 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to extract properties.");
384 goto on_error;
385 }
386
387 aws_byte_buf_clean_up(&json_blob);
388 out_value->type = AWS_ENDPOINTS_VALUE_OBJECT;
389 out_value->v.owning_cursor_object =
390 aws_endpoints_owning_cursor_from_string(aws_string_new_from_buf(allocator, &json_blob));
391 } else if (aws_json_value_is_boolean(result)) {
392 if (aws_json_value_get_boolean(result, &out_value->v.boolean)) {
393 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Could not parse boolean from node.");
394 goto on_error;
395 }
396
397 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
398 } else if (aws_json_value_is_number(result)) {
399 if (aws_json_value_get_number(result, &out_value->v.number)) {
400 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Could not parse number from node.");
401 goto on_error;
402 }
403
404 out_value->type = AWS_ENDPOINTS_VALUE_NUMBER;
405 }
406
407 aws_json_value_destroy(root_node);
408 return AWS_OP_SUCCESS;
409
410 on_error:
411 aws_json_value_destroy(root_node);
412 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
413 }
414
415 /*
416 ******************************
417 * Request Context
418 ******************************
419 */
420
421 static void s_endpoints_request_context_destroy(void *data) {
422 if (data == NULL) {
423 return;
424 }
425
426 struct aws_endpoints_request_context *context = data;
427 aws_hash_table_clean_up(&context->values);
428
429 aws_mem_release(context->allocator, context);
430 }
431
432 struct aws_endpoints_request_context *aws_endpoints_request_context_new(struct aws_allocator *allocator) {
433 AWS_PRECONDITION(allocator);
434
435 struct aws_endpoints_request_context *context =
436 aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_request_context));
437
438 context->allocator = allocator;
439 aws_ref_count_init(&context->ref_count, context, s_endpoints_request_context_destroy);
440
441 if (aws_hash_table_init(
442 &context->values,
443 allocator,
444 0,
445 aws_hash_byte_cursor_ptr,
446 aws_endpoints_byte_cursor_eq,
447 NULL,
448 s_scope_value_destroy_cb)) {
449 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init request context values.");
450 goto on_error;
451 }
452
453 return context;
454
455 on_error:
456 s_endpoints_request_context_destroy(context);
457 return NULL;
458 }
459
460 struct aws_endpoints_request_context *aws_endpoints_request_context_acquire(
461 struct aws_endpoints_request_context *request_context) {
462 AWS_PRECONDITION(request_context);
463 if (request_context) {
464 aws_ref_count_acquire(&request_context->ref_count);
465 }
466 return request_context;
467 }
468
469 struct aws_endpoints_request_context *aws_endpoints_request_context_release(
470 struct aws_endpoints_request_context *request_context) {
471 if (request_context) {
472 aws_ref_count_release(&request_context->ref_count);
473 }
474 return NULL;
475 }
476
477 int aws_endpoints_request_context_add_string(
478 struct aws_allocator *allocator,
479 struct aws_endpoints_request_context *context,
480 struct aws_byte_cursor name,
481 struct aws_byte_cursor value) {
482 AWS_PRECONDITION(allocator);
483
484 /* Own the name: caller-provided cursor may not outlive this call */
485 struct aws_endpoints_scope_value *val = aws_endpoints_scope_value_new(allocator, name, true);
486 val->value.type = AWS_ENDPOINTS_VALUE_STRING;
487 val->value.v.owning_cursor_string = aws_endpoints_owning_cursor_from_cursor(allocator, value);
488
489 if (aws_hash_table_put(&context->values, &val->name.cur, val, NULL)) {
490 aws_endpoints_scope_value_destroy(val);
491 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED);
492 };
493
494 return AWS_OP_SUCCESS;
495 }
496
497 int aws_endpoints_request_context_add_boolean(
498 struct aws_allocator *allocator,
499 struct aws_endpoints_request_context *context,
500 struct aws_byte_cursor name,
501 bool value) {
502 AWS_PRECONDITION(allocator);
503
504 /* Own the name: caller-provided cursor may not outlive this call */
505 struct aws_endpoints_scope_value *val = aws_endpoints_scope_value_new(allocator, name, true);
506 val->value.type = AWS_ENDPOINTS_VALUE_BOOLEAN;
507 val->value.v.boolean = value;
508
509 if (aws_hash_table_put(&context->values, &val->name.cur, val, NULL)) {
510 aws_endpoints_scope_value_destroy(val);
511 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED);
512 };
513
514 return AWS_OP_SUCCESS;
515 }
516
517 int aws_endpoints_request_context_add_string_array(
518 struct aws_allocator *allocator,
519 struct aws_endpoints_request_context *context,
520 struct aws_byte_cursor name,
521 const struct aws_byte_cursor *values,
522 size_t len) {
523
524 /* Own the name: caller-provided cursor may not outlive this call */
525 struct aws_endpoints_scope_value *val = aws_endpoints_scope_value_new(allocator, name, true);
526 val->value.type = AWS_ENDPOINTS_VALUE_ARRAY;
527 aws_array_list_init_dynamic(&val->value.v.array, allocator, len, sizeof(struct aws_endpoints_value));
528
529 for (size_t i = 0; i < len; ++i) {
530 struct aws_endpoints_value elem = {
531 .is_ref = false,
532 .type = AWS_ENDPOINTS_VALUE_STRING,
533 .v.owning_cursor_object = aws_endpoints_owning_cursor_from_cursor(allocator, values[i])};
534
535 aws_array_list_set_at(&val->value.v.array, &elem, i);
536 }
537
538 if (aws_hash_table_put(&context->values, &val->name.cur, val, NULL)) {
539 aws_endpoints_scope_value_destroy(val);
540 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_INIT_FAILED);
541 };
542
543 return AWS_OP_SUCCESS;
544 }
545
546 /*
547 ******************************
548 * Rule engine.
549 ******************************
550 */
551
552 struct aws_endpoints_resolved_endpoint *aws_endpoints_resolved_endpoint_acquire(
553 struct aws_endpoints_resolved_endpoint *resolved_endpoint) {
554 AWS_PRECONDITION(resolved_endpoint);
555 if (resolved_endpoint) {
556 aws_ref_count_acquire(&resolved_endpoint->ref_count);
557 }
558 return resolved_endpoint;
559 }
560
561 struct aws_endpoints_resolved_endpoint *aws_endpoints_resolved_endpoint_release(
562 struct aws_endpoints_resolved_endpoint *resolved_endpoint) {
563 if (resolved_endpoint) {
564 aws_ref_count_release(&resolved_endpoint->ref_count);
565 }
566 return NULL;
567 }
568
569 enum aws_endpoints_resolved_endpoint_type aws_endpoints_resolved_endpoint_get_type(
570 const struct aws_endpoints_resolved_endpoint *resolved_endpoint) {
571 AWS_PRECONDITION(resolved_endpoint);
572 return resolved_endpoint->type;
573 }
574
575 int aws_endpoints_resolved_endpoint_get_url(
576 const struct aws_endpoints_resolved_endpoint *resolved_endpoint,
577 struct aws_byte_cursor *out_url) {
578 AWS_PRECONDITION(resolved_endpoint);
579 AWS_PRECONDITION(out_url);
580 if (resolved_endpoint->type != AWS_ENDPOINTS_RESOLVED_ENDPOINT) {
581 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
582 }
583 *out_url = aws_byte_cursor_from_buf(&resolved_endpoint->r.endpoint.url);
584 return AWS_OP_SUCCESS;
585 }
586
587 int aws_endpoints_resolved_endpoint_get_properties(
588 const struct aws_endpoints_resolved_endpoint *resolved_endpoint,
589 struct aws_byte_cursor *out_properties) {
590 AWS_PRECONDITION(resolved_endpoint);
591 AWS_PRECONDITION(out_properties);
592 if (resolved_endpoint->type != AWS_ENDPOINTS_RESOLVED_ENDPOINT) {
593 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
594 }
595 *out_properties = aws_byte_cursor_from_buf(&resolved_endpoint->r.endpoint.properties);
596 return AWS_OP_SUCCESS;
597 }
598
599 int aws_endpoints_resolved_endpoint_get_headers(
600 const struct aws_endpoints_resolved_endpoint *resolved_endpoint,
601 const struct aws_hash_table **out_headers) {
602 AWS_PRECONDITION(resolved_endpoint);
603 AWS_PRECONDITION(out_headers);
604 if (resolved_endpoint->type != AWS_ENDPOINTS_RESOLVED_ENDPOINT) {
605 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
606 }
607 *out_headers = &resolved_endpoint->r.endpoint.headers;
608 return AWS_OP_SUCCESS;
609 }
610
611 int aws_endpoints_resolved_endpoint_get_error(
612 const struct aws_endpoints_resolved_endpoint *resolved_endpoint,
613 struct aws_byte_cursor *out_error) {
614 AWS_PRECONDITION(resolved_endpoint);
615 AWS_PRECONDITION(out_error);
616 if (resolved_endpoint->type != AWS_ENDPOINTS_RESOLVED_ERROR) {
617 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
618 }
619 *out_error = aws_byte_cursor_from_buf(&resolved_endpoint->r.error);
620 return AWS_OP_SUCCESS;
621 }
622
623 struct aws_endpoints_rule_engine {
624 struct aws_allocator *allocator;
625 struct aws_ref_count ref_count;
626
627 struct aws_endpoints_ruleset *ruleset;
628 struct aws_partitions_config *partitions_config;
629 };
630
631 static void s_endpoints_rule_engine_destroy(void *data) {
632 if (data == NULL) {
633 return;
634 }
635
636 struct aws_endpoints_rule_engine *engine = data;
637 aws_endpoints_ruleset_release(engine->ruleset);
638 aws_partitions_config_release(engine->partitions_config);
639
640 aws_mem_release(engine->allocator, engine);
641 }
642
643 struct aws_endpoints_rule_engine *aws_endpoints_rule_engine_new(
644 struct aws_allocator *allocator,
645 struct aws_endpoints_ruleset *ruleset,
646 struct aws_partitions_config *partitions_config) {
647 AWS_PRECONDITION(allocator);
648 AWS_PRECONDITION(ruleset);
649
650 struct aws_endpoints_rule_engine *engine = aws_mem_calloc(allocator, 1, sizeof(struct aws_endpoints_rule_engine));
651 engine->allocator = allocator;
652 engine->ruleset = ruleset;
653 engine->partitions_config = partitions_config;
654
655 aws_endpoints_ruleset_acquire(ruleset);
656 aws_partitions_config_acquire(partitions_config);
657 aws_ref_count_init(&engine->ref_count, engine, s_endpoints_rule_engine_destroy);
658
659 return engine;
660 }
661
662 struct aws_endpoints_rule_engine *aws_endpoints_rule_engine_acquire(struct aws_endpoints_rule_engine *rule_engine) {
663 AWS_PRECONDITION(rule_engine);
664 if (rule_engine) {
665 aws_ref_count_acquire(&rule_engine->ref_count);
666 }
667 return rule_engine;
668 }
669
670 struct aws_endpoints_rule_engine *aws_endpoints_rule_engine_release(struct aws_endpoints_rule_engine *rule_engine) {
671 if (rule_engine) {
672 aws_ref_count_release(&rule_engine->ref_count);
673 }
674 return NULL;
675 }
676
677 int s_revert_scope(struct aws_endpoints_resolution_state *state) {
678
679 for (size_t idx = 0; idx < aws_array_list_length(&state->added_keys); ++idx) {
680 struct aws_byte_cursor *cur = NULL;
681 if (aws_array_list_get_at_ptr(&state->added_keys, (void **)&cur, idx)) {
682 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to retrieve value.");
683 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
684 }
685
686 aws_hash_table_remove(&state->scope.values, cur, NULL, NULL);
687 }
688
689 aws_array_list_clear(&state->added_keys);
690
691 return AWS_OP_SUCCESS;
692 }
693
694 int aws_endpoints_rule_engine_resolve(
695 struct aws_endpoints_rule_engine *engine,
696 const struct aws_endpoints_request_context *context,
697 struct aws_endpoints_resolved_endpoint **out_resolved_endpoint) {
698
699 if (aws_array_list_length(&engine->ruleset->rules) == 0) {
700 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_EMPTY_RULESET);
701 }
702
703 int result = AWS_OP_SUCCESS;
704 struct aws_endpoints_resolution_state state;
705 if (s_init_top_level_scope(engine->allocator, context, engine->ruleset, engine->partitions_config, &state)) {
706 result = AWS_OP_ERR;
707 goto on_done;
708 }
709
710 while (state.rule_idx < aws_array_list_length(state.rules)) {
711 struct aws_endpoints_rule *rule = NULL;
712 if (aws_array_list_get_at_ptr(state.rules, (void **)&rule, state.rule_idx)) {
713 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to get rule.");
714 result = AWS_OP_ERR;
715 goto on_done;
716 }
717
718 bool is_truthy = false;
719 if (s_resolve_conditions(engine->allocator, &rule->conditions, &state, &is_truthy)) {
720 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve conditions.");
721 result = AWS_OP_ERR;
722 goto on_done;
723 }
724
725 if (!is_truthy) {
726 s_revert_scope(&state);
727 ++state.rule_idx;
728 continue;
729 }
730
731 switch (rule->type) {
732 case AWS_ENDPOINTS_RULE_ENDPOINT: {
733 struct aws_endpoints_resolved_endpoint *endpoint =
734 aws_endpoints_resolved_endpoint_new(engine->allocator);
735 endpoint->type = AWS_ENDPOINTS_RESOLVED_ENDPOINT;
736
737 struct aws_endpoints_value val;
738 if (aws_endpoints_resolve_expr(
739 engine->allocator, rule->rule_data.endpoint.url_expr_ref, &state.scope, &val) ||
740 val.type != AWS_ENDPOINTS_VALUE_STRING ||
741 aws_byte_buf_init_copy_from_cursor(
742 &endpoint->r.endpoint.url, engine->allocator, val.v.owning_cursor_string.cur)) {
743 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve templated url.");
744 result = AWS_OP_ERR;
745 goto on_done;
746 }
747
748 aws_endpoints_value_clean_up(&val);
749
750 struct resolve_template_callback_data data = {.allocator = engine->allocator, .scope = &state.scope};
751
752 if (rule->rule_data.endpoint.properties.len > 0 &&
753 aws_byte_buf_init_from_resolved_templated_string(
754 engine->allocator,
755 &endpoint->r.endpoint.properties,
756 aws_byte_cursor_from_buf(&rule->rule_data.endpoint.properties),
757 aws_endpoints_resolve_template,
758 &data,
759 true)) {
760 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve templated properties.");
761 result = AWS_OP_ERR;
762 goto on_done;
763 }
764
765 if (aws_endpoints_resolve_headers(
766 engine->allocator,
767 &state.scope,
768 &rule->rule_data.endpoint.headers,
769 &endpoint->r.endpoint.headers)) {
770 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve templated headers.");
771 result = AWS_OP_ERR;
772 goto on_done;
773 }
774
775 *out_resolved_endpoint = endpoint;
776 goto on_done;
777 }
778 case AWS_ENDPOINTS_RULE_ERROR: {
779 struct aws_endpoints_resolved_endpoint *error = aws_endpoints_resolved_endpoint_new(engine->allocator);
780 error->type = AWS_ENDPOINTS_RESOLVED_ERROR;
781
782 struct aws_endpoints_value val;
783 if (aws_endpoints_resolve_expr(
784 engine->allocator, rule->rule_data.error.error_expr_ref, &state.scope, &val) ||
785 val.type != AWS_ENDPOINTS_VALUE_STRING ||
786 aws_byte_buf_init_copy_from_cursor(
787 &error->r.error, engine->allocator, val.v.owning_cursor_string.cur)) {
788 aws_endpoints_value_clean_up(&val);
789 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve templated url.");
790 result = AWS_OP_ERR;
791 goto on_done;
792 }
793
794 aws_endpoints_value_clean_up(&val);
795 *out_resolved_endpoint = error;
796 goto on_done;
797 }
798 case AWS_ENDPOINTS_RULE_TREE: {
799 /* jumping down a level */
800 aws_array_list_clear(&state.added_keys);
801 state.rule_idx = 0;
802 state.rules = &rule->rule_data.tree.rules;
803 continue;
804 }
805 default: {
806 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected rule type.");
807 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
808 goto on_done;
809 }
810 }
811 }
812
813 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "All rules have been exhausted.");
814 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RULESET_EXHAUSTED);
815
816 on_done:
817 s_state_clean_up(&state);
818 return result;
819 }