Debian Codegraph @

crt/aws-c-sdkutils/source/endpoints_standard_lib.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/host_utils.h>
7 #include <aws/common/json.h>
8 #include <aws/common/string.h>
9 #include <aws/common/uri.h>
10
11 #include <aws/sdkutils/private/endpoints_regex.h>
12 #include <aws/sdkutils/private/endpoints_types_impl.h>
13 #include <aws/sdkutils/private/endpoints_util.h>
14 #include <aws/sdkutils/resource_name.h>
15
16 static struct aws_byte_cursor s_scheme_http = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("http");
17 static struct aws_byte_cursor s_scheme_https = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("https");
18
19 static int s_resolve_fn_is_set(
20 struct aws_allocator *allocator,
21 struct aws_endpoints_args args,
22 struct aws_endpoints_resolution_scope *scope,
23 struct aws_endpoints_value *out_value) {
24
25 int result = AWS_OP_SUCCESS;
26 struct aws_endpoints_value argv_value = {0};
27 if (args.argc != 1 || aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_ANY, &argv_value)) {
28 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for isSet.");
29 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
30 goto on_done;
31 }
32
33 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
34 out_value->v.boolean = argv_value.type != AWS_ENDPOINTS_VALUE_NONE;
35
36 on_done:
37 aws_endpoints_value_clean_up(&argv_value);
38 return result;
39 }
40
41 static int s_resolve_fn_not(
42 struct aws_allocator *allocator,
43 struct aws_endpoints_args args,
44 struct aws_endpoints_resolution_scope *scope,
45 struct aws_endpoints_value *out_value) {
46
47 int result = AWS_OP_SUCCESS;
48 struct aws_endpoints_value argv_value = {0};
49 if (args.argc != 1 ||
50 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_BOOLEAN, &argv_value)) {
51 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for not.");
52 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
53 goto on_done;
54 }
55
56 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
57 out_value->v.boolean = !argv_value.v.boolean;
58
59 on_done:
60 aws_endpoints_value_clean_up(&argv_value);
61 return result;
62 }
63
64 static int s_resolve_fn_get_attr(
65 struct aws_allocator *allocator,
66 struct aws_endpoints_args args,
67 struct aws_endpoints_resolution_scope *scope,
68 struct aws_endpoints_value *out_value) {
69
70 int result = AWS_OP_SUCCESS;
71 struct aws_endpoints_value argv_value = {0};
72 struct aws_endpoints_value argv_path = {0};
73 if (args.argc != 2 || aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_ANY, &argv_value) ||
74 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_STRING, &argv_path)) {
75 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for get attr.");
76 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
77 goto on_done;
78 }
79
80 struct aws_byte_cursor path_cur = argv_path.v.owning_cursor_string.cur;
81
82 if (argv_value.type == AWS_ENDPOINTS_VALUE_OBJECT) {
83 if (aws_endpoints_path_through_object(allocator, &argv_value, path_cur, out_value)) {
84 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to path through object.");
85 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
86 goto on_done;
87 }
88 } else if (argv_value.type == AWS_ENDPOINTS_VALUE_ARRAY) {
89 if (aws_endpoints_path_through_array(allocator, scope, &argv_value, path_cur, out_value)) {
90 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to path through array.");
91 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
92 goto on_done;
93 }
94 } else {
95 AWS_LOGF_ERROR(
96 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid value type for pathing through. type: %d", argv_value.type);
97 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
98 goto on_done;
99 }
100
101 on_done:
102 aws_endpoints_value_clean_up(&argv_value);
103 aws_endpoints_value_clean_up(&argv_path);
104 return result;
105 }
106
107 static int s_resolve_fn_substring(
108 struct aws_allocator *allocator,
109 struct aws_endpoints_args args,
110 struct aws_endpoints_resolution_scope *scope,
111 struct aws_endpoints_value *out_value) {
112
113 int result = AWS_OP_SUCCESS;
114 struct aws_endpoints_value input_value = {0};
115 struct aws_endpoints_value start_value = {0};
116 struct aws_endpoints_value stop_value = {0};
117 struct aws_endpoints_value reverse_value = {0};
118 if (args.argc != 4 ||
119 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &input_value) ||
120 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_NUMBER, &start_value) ||
121 aws_endpoints_argv_expect(allocator, scope, args, 2, AWS_ENDPOINTS_VALUE_NUMBER, &stop_value) ||
122 aws_endpoints_argv_expect(allocator, scope, args, 3, AWS_ENDPOINTS_VALUE_BOOLEAN, &reverse_value)) {
123 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for substring.");
124 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
125 goto on_done;
126 }
127
128 if (start_value.v.number >= stop_value.v.number ||
129 input_value.v.owning_cursor_string.cur.len < stop_value.v.number) {
130 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
131 goto on_done;
132 }
133
134 for (size_t idx = 0; idx < input_value.v.owning_cursor_string.cur.len; ++idx) {
135 if (input_value.v.owning_cursor_string.cur.ptr[idx] > 127) {
136 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
137 goto on_done;
138 }
139 }
140
141 if (!reverse_value.v.boolean) {
142 size_t start = (size_t)start_value.v.number;
143 size_t end = (size_t)stop_value.v.number;
144 struct aws_byte_cursor substring = {
145 .ptr = input_value.v.owning_cursor_string.cur.ptr + start,
146 .len = end - start,
147 };
148
149 out_value->type = AWS_ENDPOINTS_VALUE_STRING;
150 if (input_value.is_ref) {
151 out_value->v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(substring);
152 } else {
153 out_value->v.owning_cursor_string = aws_endpoints_owning_cursor_from_cursor(allocator, substring);
154 }
155
156 } else {
157 size_t r_start = input_value.v.owning_cursor_string.cur.len - (size_t)stop_value.v.number;
158 size_t r_stop = input_value.v.owning_cursor_string.cur.len - (size_t)start_value.v.number;
159
160 struct aws_byte_cursor substring = {
161 .ptr = input_value.v.owning_cursor_string.cur.ptr + r_start,
162 .len = r_stop - r_start,
163 };
164 out_value->type = AWS_ENDPOINTS_VALUE_STRING;
165 if (input_value.is_ref) {
166 out_value->v.owning_cursor_string = aws_endpoints_non_owning_cursor_create(substring);
167 } else {
168 out_value->v.owning_cursor_string = aws_endpoints_owning_cursor_from_cursor(allocator, substring);
169 }
170 }
171
172 on_done:
173 aws_endpoints_value_clean_up(&input_value);
174 aws_endpoints_value_clean_up(&start_value);
175 aws_endpoints_value_clean_up(&stop_value);
176 aws_endpoints_value_clean_up(&reverse_value);
177 return result;
178 }
179
180 static int s_resolve_fn_string_equals(
181 struct aws_allocator *allocator,
182 struct aws_endpoints_args args,
183 struct aws_endpoints_resolution_scope *scope,
184 struct aws_endpoints_value *out_value) {
185
186 int result = AWS_OP_SUCCESS;
187 struct aws_endpoints_value argv_value_1 = {0};
188 struct aws_endpoints_value argv_value_2 = {0};
189 if (args.argc != 2 ||
190 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_value_1) ||
191 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_STRING, &argv_value_2)) {
192 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve stringEquals.");
193 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
194 goto on_done;
195 }
196
197 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
198 out_value->v.boolean =
199 aws_byte_cursor_eq(&argv_value_1.v.owning_cursor_string.cur, &argv_value_2.v.owning_cursor_string.cur);
200
201 on_done:
202 aws_endpoints_value_clean_up(&argv_value_1);
203 aws_endpoints_value_clean_up(&argv_value_2);
204 return result;
205 }
206
207 static int s_resolve_fn_boolean_equals(
208 struct aws_allocator *allocator,
209 struct aws_endpoints_args args,
210 struct aws_endpoints_resolution_scope *scope,
211 struct aws_endpoints_value *out_value) {
212
213 int result = AWS_OP_SUCCESS;
214 struct aws_endpoints_value argv_value_1 = {0};
215 struct aws_endpoints_value argv_value_2 = {0};
216 if (args.argc != 2 ||
217 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_BOOLEAN, &argv_value_1) ||
218 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_BOOLEAN, &argv_value_2)) {
219 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve booleanEquals.");
220 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
221 goto on_done;
222 }
223
224 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
225 out_value->v.boolean = argv_value_1.v.boolean == argv_value_2.v.boolean;
226
227 on_done:
228 aws_endpoints_value_clean_up(&argv_value_1);
229 aws_endpoints_value_clean_up(&argv_value_2);
230 return result;
231 }
232
233 static int s_resolve_fn_coalesce(
234 struct aws_allocator *allocator,
235 struct aws_endpoints_args args,
236 struct aws_endpoints_resolution_scope *scope,
237 struct aws_endpoints_value *out_value) {
238
239 int result = AWS_OP_SUCCESS;
240 bool is_out_set = false;
241
242 for (size_t i = 0; i < args.argc; ++i) {
243 struct aws_endpoints_value argv_value = {0};
244 if (aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_ANY, &argv_value)) {
245 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve coalesce.");
246 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
247 goto on_done;
248 }
249
250 if (argv_value.type != AWS_ENDPOINTS_VALUE_NONE) {
251 *out_value = argv_value;
252 is_out_set = true;
253 break;
254 } else {
255 aws_endpoints_value_clean_up(&argv_value);
256 }
257 }
258
259 if (!is_out_set) {
260 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
261 }
262
263 on_done:
264 return result;
265 }
266
267 static int s_resolve_fn_split(
268 struct aws_allocator *allocator,
269 struct aws_endpoints_args args,
270 struct aws_endpoints_resolution_scope *scope,
271 struct aws_endpoints_value *out_value) {
272
273 int result = AWS_OP_SUCCESS;
274 struct aws_endpoints_value input_value = {0};
275 struct aws_endpoints_value delim_value = {0};
276 struct aws_endpoints_value limit_value = {0};
277 if (args.argc != 3 ||
278 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &input_value) ||
279 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_STRING, &delim_value) ||
280 aws_endpoints_argv_expect(allocator, scope, args, 2, AWS_ENDPOINTS_VALUE_NUMBER, &limit_value)) {
281 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for split.");
282 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
283 goto on_done;
284 }
285
286 for (size_t idx = 0; idx < input_value.v.owning_cursor_string.cur.len; ++idx) {
287 if (input_value.v.owning_cursor_string.cur.ptr[idx] > 127) {
288 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
289 goto on_done;
290 }
291 }
292
293 aws_array_list_init_dynamic(&out_value->v.array, allocator, 10, sizeof(struct aws_endpoints_value));
294
295 size_t limit = (size_t)limit_value.v.number;
296 struct aws_byte_cursor input = input_value.v.owning_cursor_object.cur;
297 struct aws_byte_cursor delim = delim_value.v.owning_cursor_object.cur;
298 struct aws_byte_cursor substr = {0};
299 bool has_leftover_str = true;
300 for (size_t count = 0; count < limit; ++count) {
301 if (aws_byte_cursor_next_split_on_cursor(&input, delim, &substr)) {
302 struct aws_endpoints_value val = {
303 .is_ref = false,
304 .type = AWS_ENDPOINTS_VALUE_STRING,
305 .v.owning_cursor_object = aws_endpoints_owning_cursor_from_cursor(allocator, substr)};
306
307 aws_array_list_push_back(&out_value->v.array, &val);
308 } else {
309 has_leftover_str = false;
310 break;
311 }
312 }
313
314 if (has_leftover_str) {
315 if (aws_byte_cursor_next_split_on_cursor(&input, delim, &substr)) {
316 aws_byte_cursor_advance(&input, input.ptr - substr.ptr);
317 struct aws_endpoints_value val = {
318 .is_ref = false,
319 .type = AWS_ENDPOINTS_VALUE_STRING,
320 .v.owning_cursor_object = aws_endpoints_owning_cursor_from_cursor(allocator, input)};
321
322 aws_array_list_push_back(&out_value->v.array, &val);
323 }
324 }
325
326 on_done:
327 aws_endpoints_value_clean_up(&input_value);
328 aws_endpoints_value_clean_up(&delim_value);
329 aws_endpoints_value_clean_up(&limit_value);
330 return result;
331 }
332
333 static int s_resolve_fn_ite(
334 struct aws_allocator *allocator,
335 struct aws_endpoints_args args,
336 struct aws_endpoints_resolution_scope *scope,
337 struct aws_endpoints_value *out_value) {
338
339 int result = AWS_OP_SUCCESS;
340 struct aws_endpoints_value cond_value = {0};
341 struct aws_endpoints_value true_value = {0};
342 struct aws_endpoints_value false_value = {0};
343 if (args.argc != 3 ||
344 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_BOOLEAN, &cond_value) ||
345 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_ANY, &true_value) ||
346 aws_endpoints_argv_expect(allocator, scope, args, 2, AWS_ENDPOINTS_VALUE_ANY, &false_value)) {
347 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve ite.");
348 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
349 goto on_done;
350 }
351
352 if (cond_value.type != AWS_ENDPOINTS_VALUE_BOOLEAN) {
353 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected cond type in ite.");
354 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
355 goto on_done;
356 }
357
358 if (cond_value.v.boolean == true) {
359 *out_value = true_value;
360 aws_endpoints_value_clean_up(&false_value);
361 } else {
362 *out_value = false_value;
363 aws_endpoints_value_clean_up(&true_value);
364 }
365
366 on_done:
367 aws_endpoints_value_clean_up(&cond_value);
368 return result;
369 }
370
371 static int s_resolve_fn_uri_encode(
372 struct aws_allocator *allocator,
373 struct aws_endpoints_args args,
374 struct aws_endpoints_resolution_scope *scope,
375 struct aws_endpoints_value *out_value) {
376
377 int result = AWS_OP_SUCCESS;
378 struct aws_byte_buf buf = {0};
379 struct aws_endpoints_value argv_value = {0};
380 if (args.argc != 1 ||
381 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_value)) {
382 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve parameter to uri encode.");
383 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
384 goto on_done;
385 }
386
387 if (aws_byte_buf_init(&buf, allocator, 10)) {
388 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve parameter to uri encode.");
389 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
390 goto on_done;
391 }
392
393 if (aws_byte_buf_append_encoding_uri_param(&buf, &argv_value.v.owning_cursor_string.cur)) {
394 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to uri encode value.");
395 aws_byte_buf_clean_up(&buf);
396 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
397 goto on_done;
398 }
399
400 out_value->type = AWS_ENDPOINTS_VALUE_STRING;
401 out_value->v.owning_cursor_string =
402 aws_endpoints_owning_cursor_from_string(aws_string_new_from_buf(allocator, &buf));
403
404 on_done:
405 aws_endpoints_value_clean_up(&argv_value);
406 aws_byte_buf_clean_up(&buf);
407 return result;
408 }
409
410 static bool s_is_uri_ip(struct aws_byte_cursor host, bool is_uri_encoded) {
411 return aws_host_utils_is_ipv4(host) || aws_host_utils_is_ipv6(host, is_uri_encoded);
412 }
413
414 static int s_resolve_fn_parse_url(
415 struct aws_allocator *allocator,
416 struct aws_endpoints_args args,
417 struct aws_endpoints_resolution_scope *scope,
418 struct aws_endpoints_value *out_value) {
419
420 int result = AWS_OP_SUCCESS;
421 struct aws_uri uri;
422 struct aws_json_value *root = NULL;
423 struct aws_endpoints_value argv_url = {0};
424 if (args.argc != 1 || aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_url)) {
425 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for parse url.");
426 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
427 goto on_done;
428 }
429
430 if (aws_uri_init_parse(&uri, allocator, &argv_url.v.owning_cursor_string.cur)) {
431 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
432 /* reset error from parser, since non-uri strings should successfully resolve to none. */
433 aws_reset_error();
434 goto on_done;
435 }
436
437 if (aws_uri_query_string(&uri)->len > 0) {
438 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
439 goto on_done;
440 }
441
442 const struct aws_byte_cursor *scheme = aws_uri_scheme(&uri);
443 AWS_ASSERT(scheme != NULL);
444
445 root = aws_json_value_new_object(allocator);
446
447 if (scheme->len == 0) {
448 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
449 goto on_done;
450 }
451
452 if (!(aws_byte_cursor_eq(scheme, &s_scheme_http) || aws_byte_cursor_eq(scheme, &s_scheme_https))) {
453 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
454 goto on_done;
455 }
456
457 if (aws_json_value_add_to_object(
458 root, aws_byte_cursor_from_c_str("scheme"), aws_json_value_new_string(allocator, *scheme))) {
459 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add scheme to object.");
460 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
461 goto on_done;
462 }
463
464 const struct aws_byte_cursor *authority = aws_uri_authority(&uri);
465 AWS_ASSERT(authority != NULL);
466
467 if (authority->len == 0) {
468 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
469 goto on_done;
470 }
471
472 if (aws_json_value_add_to_object(
473 root, aws_byte_cursor_from_c_str("authority"), aws_json_value_new_string(allocator, *authority))) {
474 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add authority to object.");
475 goto on_done;
476 }
477
478 const struct aws_byte_cursor *path = aws_uri_path(&uri);
479
480 if (aws_json_value_add_to_object(
481 root, aws_byte_cursor_from_c_str("path"), aws_json_value_new_string(allocator, *path))) {
482 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add path to object.");
483 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
484 goto on_done;
485 }
486
487 struct aws_byte_cursor normalized_path_cur = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("normalizedPath");
488 struct aws_byte_buf normalized_path_buf;
489 if (aws_byte_buf_init_from_normalized_uri_path(allocator, *path, &normalized_path_buf) ||
490 aws_json_value_add_to_object(
491 root,
492 normalized_path_cur,
493 aws_json_value_new_string(allocator, aws_byte_cursor_from_buf(&normalized_path_buf)))) {
494 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to normalize path.");
495 aws_byte_buf_clean_up(&normalized_path_buf);
496 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
497 goto on_done;
498 }
499
500 aws_byte_buf_clean_up(&normalized_path_buf);
501
502 const struct aws_byte_cursor *host_name = aws_uri_host_name(&uri);
503 bool is_ip = s_is_uri_ip(*host_name, true);
504 if (aws_json_value_add_to_object(
505 root, aws_byte_cursor_from_c_str("isIp"), aws_json_value_new_boolean(allocator, is_ip))) {
506 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add isIp to object.");
507 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
508 goto on_done;
509 }
510
511 struct aws_byte_buf buf;
512 if (aws_byte_buf_init(&buf, allocator, 0)) {
513 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed init buffer for parseUrl return.");
514 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
515 goto on_done;
516 }
517
518 if (aws_byte_buf_append_json_string(root, &buf)) {
519 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to create JSON object.");
520 aws_byte_buf_clean_up(&buf);
521 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
522 goto on_done;
523 }
524
525 out_value->type = AWS_ENDPOINTS_VALUE_OBJECT;
526 out_value->v.owning_cursor_object =
527 aws_endpoints_owning_cursor_from_string(aws_string_new_from_buf(allocator, &buf));
528
529 aws_byte_buf_clean_up(&buf);
530
531 on_done:
532 aws_uri_clean_up(&uri);
533 aws_endpoints_value_clean_up(&argv_url);
534 aws_json_value_destroy(root);
535 return result;
536 }
537
538 static int s_resolve_is_valid_host_label(
539 struct aws_allocator *allocator,
540 struct aws_endpoints_args args,
541 struct aws_endpoints_resolution_scope *scope,
542 struct aws_endpoints_value *out_value) {
543
544 struct aws_endpoints_value argv_value = {0};
545 struct aws_endpoints_value argv_allow_subdomains = {0};
546 if (args.argc != 2 ||
547 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_value) ||
548 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_BOOLEAN, &argv_allow_subdomains)) {
549 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve not.");
550 goto on_error;
551 }
552
553 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
554 out_value->v.boolean =
555 aws_is_valid_host_label(argv_value.v.owning_cursor_string.cur, argv_allow_subdomains.v.boolean);
556
557 aws_endpoints_value_clean_up(&argv_value);
558 aws_endpoints_value_clean_up(&argv_allow_subdomains);
559 return AWS_OP_SUCCESS;
560
561 on_error:
562 aws_endpoints_value_clean_up(&argv_value);
563 aws_endpoints_value_clean_up(&argv_allow_subdomains);
564 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
565 }
566
567 static int s_resolve_fn_aws_partition(
568 struct aws_allocator *allocator,
569 struct aws_endpoints_args args,
570 struct aws_endpoints_resolution_scope *scope,
571 struct aws_endpoints_value *out_value) {
572
573 int result = AWS_OP_SUCCESS;
574 struct aws_endpoints_value argv_region = {0};
575
576 if (args.argc != 1 ||
577 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_region)) {
578 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve arguments for partitions.");
579 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
580 goto on_done;
581 }
582
583 struct aws_hash_element *element = NULL;
584 struct aws_byte_cursor key = argv_region.v.owning_cursor_string.cur;
585 if (aws_hash_table_find(&scope->partitions->region_to_partition_info, &key, &element)) {
586 AWS_LOGF_ERROR(
587 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to find partition info. " PRInSTR, AWS_BYTE_CURSOR_PRI(key));
588 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
589 goto on_done;
590 }
591
592 if (element != NULL) {
593 out_value->type = AWS_ENDPOINTS_VALUE_OBJECT;
594 out_value->v.owning_cursor_object =
595 aws_endpoints_owning_cursor_create(allocator, ((struct aws_partition_info *)element->value)->info);
596 goto on_done;
597 }
598
599 struct aws_byte_cursor partition_cur = {0};
600 for (struct aws_hash_iter iter = aws_hash_iter_begin(&scope->partitions->base_partitions);
601 !aws_hash_iter_done(&iter);
602 aws_hash_iter_next(&iter)) {
603
604 struct aws_partition_info *partition = (struct aws_partition_info *)iter.element.value;
605
606 if (partition->region_regex && aws_endpoints_regex_match(partition->region_regex, key) == AWS_OP_SUCCESS) {
607 partition_cur = partition->name;
608 break;
609 }
610 }
611
612 if (partition_cur.len == 0) {
613 partition_cur = aws_byte_cursor_from_c_str("aws");
614 }
615
616 if (aws_hash_table_find(&scope->partitions->base_partitions, &partition_cur, &element) || element == NULL) {
617 AWS_LOGF_ERROR(
618 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to find partition info. " PRInSTR, AWS_BYTE_CURSOR_PRI(key));
619 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
620 goto on_done;
621 }
622
623 out_value->type = AWS_ENDPOINTS_VALUE_OBJECT;
624 out_value->v.owning_cursor_object =
625 aws_endpoints_owning_cursor_create(allocator, ((struct aws_partition_info *)element->value)->info);
626
627 on_done:
628 aws_endpoints_value_clean_up(&argv_region);
629 return result;
630 }
631
632 static int s_resolve_fn_aws_parse_arn(
633 struct aws_allocator *allocator,
634 struct aws_endpoints_args args,
635 struct aws_endpoints_resolution_scope *scope,
636 struct aws_endpoints_value *out_value) {
637
638 int result = AWS_OP_SUCCESS;
639 struct aws_json_value *object = NULL;
640 struct aws_endpoints_value argv_value = {0};
641 if (args.argc != 1 ||
642 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_value)) {
643 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve parseArn.");
644 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
645 goto on_done;
646 }
647
648 struct aws_resource_name arn;
649 if (aws_resource_name_init_from_cur(&arn, &argv_value.v.owning_cursor_string.cur)) {
650 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
651 goto on_done;
652 }
653
654 object = aws_json_value_new_object(allocator);
655 if (object == NULL) {
656 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init object for parseArn.");
657 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
658 goto on_done;
659 }
660
661 if (arn.partition.len == 0 || arn.resource_id.len == 0 || arn.service.len == 0) {
662 out_value->type = AWS_ENDPOINTS_VALUE_NONE;
663 goto on_done;
664 }
665
666 /* Split resource id into components, either on : or / */
667 /* TODO: support multiple delims in existing split helper? */
668 struct aws_json_value *resource_id_node = aws_json_value_new_array(allocator);
669 size_t start = 0;
670 for (size_t i = 0; i < arn.resource_id.len; ++i) {
671 if (arn.resource_id.ptr[i] == '/' || arn.resource_id.ptr[i] == ':') {
672 struct aws_byte_cursor cur = {
673 .ptr = arn.resource_id.ptr + start,
674 .len = i - start,
675 };
676
677 struct aws_json_value *element = aws_json_value_new_string(allocator, cur);
678 if (element == NULL || aws_json_value_add_array_element(resource_id_node, element)) {
679 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add resource id element");
680 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
681 goto on_done;
682 }
683
684 start = i + 1;
685 }
686 }
687
688 if (start <= arn.resource_id.len) {
689 struct aws_byte_cursor cur = {
690 .ptr = arn.resource_id.ptr + start,
691 .len = arn.resource_id.len - start,
692 };
693 struct aws_json_value *element = aws_json_value_new_string(allocator, cur);
694 if (element == NULL || aws_json_value_add_array_element(resource_id_node, element)) {
695 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add resource id element");
696 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
697 goto on_done;
698 }
699 }
700
701 if (aws_json_value_add_to_object(
702 object, aws_byte_cursor_from_c_str("partition"), aws_json_value_new_string(allocator, arn.partition)) ||
703 aws_json_value_add_to_object(
704 object, aws_byte_cursor_from_c_str("service"), aws_json_value_new_string(allocator, arn.service)) ||
705 aws_json_value_add_to_object(
706 object, aws_byte_cursor_from_c_str("region"), aws_json_value_new_string(allocator, arn.region)) ||
707 aws_json_value_add_to_object(
708 object, aws_byte_cursor_from_c_str("accountId"), aws_json_value_new_string(allocator, arn.account_id)) ||
709 aws_json_value_add_to_object(object, aws_byte_cursor_from_c_str("resourceId"), resource_id_node)) {
710 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add elements to object for parseArn.");
711 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
712 goto on_done;
713 }
714
715 out_value->type = AWS_ENDPOINTS_VALUE_OBJECT;
716 out_value->v.owning_cursor_object =
717 aws_endpoints_owning_cursor_from_string(aws_string_new_from_json(allocator, object));
718
719 if (out_value->v.owning_cursor_object.cur.len == 0) {
720 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to create string from json.");
721 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
722 goto on_done;
723 }
724
725 on_done:
726 aws_json_value_destroy(object);
727 aws_endpoints_value_clean_up(&argv_value);
728 return result;
729 }
730
731 static int s_resolve_is_virtual_hostable_s3_bucket(
732 struct aws_allocator *allocator,
733 struct aws_endpoints_args args,
734 struct aws_endpoints_resolution_scope *scope,
735 struct aws_endpoints_value *out_value) {
736
737 int result = AWS_OP_SUCCESS;
738 struct aws_endpoints_value argv_value = {0};
739 struct aws_endpoints_value argv_allow_subdomains = {0};
740 if (args.argc != 2 ||
741 aws_endpoints_argv_expect(allocator, scope, args, 0, AWS_ENDPOINTS_VALUE_STRING, &argv_value) ||
742 aws_endpoints_argv_expect(allocator, scope, args, 1, AWS_ENDPOINTS_VALUE_BOOLEAN, &argv_allow_subdomains)) {
743 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to resolve args for isVirtualHostableS3Bucket.");
744 result = aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
745 goto on_done;
746 }
747
748 struct aws_byte_cursor label_cur = argv_value.v.owning_cursor_string.cur;
749
750 bool has_uppercase_chars = false;
751 for (size_t i = 0; i < label_cur.len; ++i) {
752 if (label_cur.ptr[i] >= 'A' && label_cur.ptr[i] <= 'Z') {
753 has_uppercase_chars = true;
754 break;
755 }
756 }
757
758 out_value->type = AWS_ENDPOINTS_VALUE_BOOLEAN;
759 out_value->v.boolean = (label_cur.len >= 3 && label_cur.len <= 63) && !has_uppercase_chars &&
760 aws_is_valid_host_label(label_cur, argv_allow_subdomains.v.boolean) &&
761 !aws_host_utils_is_ipv4(label_cur);
762
763 on_done:
764 aws_endpoints_value_clean_up(&argv_value);
765 aws_endpoints_value_clean_up(&argv_allow_subdomains);
766 return result;
767 }
768
769 typedef int(standard_lib_function_fn)(
770 struct aws_allocator *allocator,
771 struct aws_endpoints_args args,
772 struct aws_endpoints_resolution_scope *scope,
773 struct aws_endpoints_value *out_value);
774
775 static standard_lib_function_fn *s_resolve_fn_vt[AWS_ENDPOINTS_FN_LAST] = {
776 [AWS_ENDPOINTS_FN_IS_SET] = s_resolve_fn_is_set,
777 [AWS_ENDPOINTS_FN_NOT] = s_resolve_fn_not,
778 [AWS_ENDPOINTS_FN_GET_ATTR] = s_resolve_fn_get_attr,
779 [AWS_ENDPOINTS_FN_SUBSTRING] = s_resolve_fn_substring,
780 [AWS_ENDPOINTS_FN_STRING_EQUALS] = s_resolve_fn_string_equals,
781 [AWS_ENDPOINTS_FN_BOOLEAN_EQUALS] = s_resolve_fn_boolean_equals,
782 [AWS_ENDPOINTS_FN_COALESCE] = s_resolve_fn_coalesce,
783 [AWS_ENDPOINTS_FN_SPLIT] = s_resolve_fn_split,
784 [AWS_ENDPOINTS_FN_ITE] = s_resolve_fn_ite,
785 [AWS_ENDPOINTS_FN_URI_ENCODE] = s_resolve_fn_uri_encode,
786 [AWS_ENDPOINTS_FN_PARSE_URL] = s_resolve_fn_parse_url,
787 [AWS_ENDPOINTS_FN_IS_VALID_HOST_LABEL] = s_resolve_is_valid_host_label,
788 [AWS_ENDPOINTS_FN_AWS_PARTITION] = s_resolve_fn_aws_partition,
789 [AWS_ENDPOINTS_FN_AWS_PARSE_ARN] = s_resolve_fn_aws_parse_arn,
790 [AWS_ENDPOINTS_FN_AWS_IS_VIRTUAL_HOSTABLE_S3_BUCKET] = s_resolve_is_virtual_hostable_s3_bucket,
791 };
792
793 int aws_endpoints_dispatch_standard_lib_fn_resolve(
794 enum aws_endpoints_fn_type type,
795 struct aws_allocator *allocator,
796 struct aws_endpoints_args args,
797 struct aws_endpoints_resolution_scope *scope,
798 struct aws_endpoints_value *out_value) {
799 return s_resolve_fn_vt[type](allocator, args, scope, out_value);
800 }