Debian Codegraph @

crt/aws-c-sdkutils/source/endpoints_util.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/json.h>
7 #include <aws/common/logging.h>
8 #include <aws/common/string.h>
9 #include <aws/sdkutils/private/endpoints_util.h>
10 #include <aws/sdkutils/sdkutils.h>
11
12 #include <inttypes.h>
13
14 /* arbitrary max length of a region. curent longest region name is 16 chars */
15 #define AWS_REGION_LEN 50
16
17 bool aws_is_valid_host_label(struct aws_byte_cursor label, bool allow_subdomains) {
18 bool next_must_be_alnum = true;
19 size_t subdomain_count = 0;
20
21 for (size_t i = 0; i < label.len; ++i) {
22 if (label.ptr[i] == '.') {
23 if (!allow_subdomains || subdomain_count == 0) {
24 return false;
25 }
26
27 if (!aws_isalnum(label.ptr[i - 1])) {
28 return false;
29 }
30
31 next_must_be_alnum = true;
32 subdomain_count = 0;
33 continue;
34 }
35
36 if (next_must_be_alnum && !aws_isalnum(label.ptr[i])) {
37 return false;
38 } else if (label.ptr[i] != '-' && !aws_isalnum(label.ptr[i])) {
39 return false;
40 }
41
42 next_must_be_alnum = false;
43 ++subdomain_count;
44
45 if (subdomain_count > 63) {
46 return false;
47 }
48 }
49
50 return aws_isalnum(label.ptr[label.len - 1]);
51 }
52
53 struct aws_byte_cursor s_path_slash = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/");
54
55 int aws_byte_buf_init_from_normalized_uri_path(
56 struct aws_allocator *allocator,
57 struct aws_byte_cursor path,
58 struct aws_byte_buf *out_normalized_path) {
59 /* Normalized path is just regular path that ensures that path starts and ends with slash */
60
61 if (aws_byte_buf_init(out_normalized_path, allocator, path.len + 2)) {
62 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed init buffer for parseUrl return.");
63 goto on_error;
64 }
65
66 if (path.len == 0) {
67 if (aws_byte_buf_append(out_normalized_path, &s_path_slash)) {
68 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add path to object.");
69 goto on_error;
70 }
71 return AWS_OP_SUCCESS;
72 }
73
74 if (path.ptr[0] != '/') {
75 if (aws_byte_buf_append_dynamic(out_normalized_path, &s_path_slash)) {
76 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to append slash to normalized path.");
77 goto on_error;
78 }
79 }
80
81 if (aws_byte_buf_append_dynamic(out_normalized_path, &path)) {
82 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to append path to normalized path.");
83 goto on_error;
84 }
85
86 if (out_normalized_path->buffer[out_normalized_path->len - 1] != '/') {
87 if (aws_byte_buf_append_dynamic(out_normalized_path, &s_path_slash)) {
88 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to append slash to normalized path.");
89 goto on_error;
90 }
91 }
92
93 return AWS_OP_SUCCESS;
94
95 on_error:
96 aws_byte_buf_clean_up(out_normalized_path);
97 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
98 }
99
100 struct aws_string *aws_string_new_from_json(struct aws_allocator *allocator, const struct aws_json_value *value) {
101 struct aws_byte_buf json_blob;
102 if (aws_byte_buf_init(&json_blob, allocator, 0)) {
103 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init buffer for json conversion.");
104 goto on_error;
105 }
106
107 if (aws_byte_buf_append_json_string(value, &json_blob)) {
108 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to convert json to string.");
109 goto on_error;
110 }
111
112 struct aws_string *ret = aws_string_new_from_buf(allocator, &json_blob);
113 aws_byte_buf_clean_up(&json_blob);
114 return ret;
115
116 on_error:
117 aws_byte_buf_clean_up(&json_blob);
118 aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
119 return NULL;
120 }
121
122 bool aws_endpoints_byte_cursor_eq(const void *a, const void *b) {
123 const struct aws_byte_cursor *a_cur = a;
124 const struct aws_byte_cursor *b_cur = b;
125 return aws_byte_cursor_eq(a_cur, b_cur);
126 }
127
128 void aws_array_list_deep_clean_up(struct aws_array_list *array, aws_array_callback_clean_up_fn on_clean_up_element) {
129 for (size_t idx = 0; idx < aws_array_list_length(array); ++idx) {
130 void *element = NULL;
131
132 aws_array_list_get_at_ptr(array, &element, idx);
133 AWS_ASSERT(element);
134 on_clean_up_element(element);
135 }
136
137 aws_array_list_clean_up(array);
138 }
139
140 /* TODO: this can be moved into common */
141 static bool s_split_on_first_delim(
142 struct aws_byte_cursor input,
143 char split_on,
144 struct aws_byte_cursor *out_split,
145 struct aws_byte_cursor *out_rest) {
146 AWS_PRECONDITION(aws_byte_cursor_is_valid(&input));
147
148 uint8_t *delim = memchr(input.ptr, split_on, input.len);
149 if (delim != NULL) {
150 out_split->ptr = input.ptr;
151 out_split->len = delim - input.ptr;
152
153 out_rest->ptr = delim;
154 out_rest->len = input.len - (delim - input.ptr);
155 return true;
156 }
157
158 *out_split = input;
159 out_rest->ptr = NULL;
160 out_rest->len = 0;
161 return false;
162 }
163
164 static int s_buf_append_and_update_quote_count(
165 struct aws_byte_buf *buf,
166 struct aws_byte_cursor to_append,
167 size_t *quote_count,
168 bool is_json) {
169
170 /* Dont count quotes if its not json. escaped quotes will be replaced with
171 regular quotes when ruleset json is parsed, which will lead to incorrect
172 results for when templates should be resolved in regular strings.
173 Note: in json blobs escaped quotes are preserved and bellow approach works. */
174 if (is_json) {
175 for (size_t idx = 0; idx < to_append.len; ++idx) {
176 if (to_append.ptr[idx] == '"' && !(idx > 0 && to_append.ptr[idx - 1] == '\\')) {
177 ++*quote_count;
178 }
179 }
180 }
181 return aws_byte_buf_append_dynamic(buf, &to_append);
182 }
183
184 static struct aws_byte_cursor escaped_closing_curly = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("}}");
185 static struct aws_byte_cursor escaped_opening_curly = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("{{");
186
187 /*
188 * Small helper to deal with escapes correctly in strings that occur before
189 * template opening curly. General flow for resolving is to look for opening and
190 * then closing curly. This function correctly appends any escaped closing
191 * curlies and errors out if closing is not escaped (i.e. its unmatched).
192 */
193 int s_append_template_prefix_to_buffer(
194 struct aws_byte_buf *out_buf,
195 struct aws_byte_cursor prefix,
196 size_t *quote_count,
197 bool is_json) {
198
199 struct aws_byte_cursor split = {0};
200 struct aws_byte_cursor rest = {0};
201
202 while (s_split_on_first_delim(prefix, '}', &split, &rest)) {
203 if (s_buf_append_and_update_quote_count(out_buf, split, quote_count, is_json)) {
204 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
205 goto on_error;
206 }
207
208 if (*quote_count % 2 == 0) {
209 if (aws_byte_buf_append_byte_dynamic(out_buf, '}')) {
210 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
211 goto on_error;
212 }
213 aws_byte_cursor_advance(&rest, 1);
214 prefix = rest;
215 continue;
216 }
217
218 if (aws_byte_cursor_starts_with(&rest, &escaped_closing_curly)) {
219 if (aws_byte_buf_append_byte_dynamic(out_buf, '}')) {
220 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
221 goto on_error;
222 }
223 aws_byte_cursor_advance(&rest, 2);
224 } else {
225 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Unmatched or unescaped closing curly.");
226 goto on_error;
227 }
228
229 prefix = rest;
230 }
231
232 if (s_buf_append_and_update_quote_count(out_buf, split, quote_count, is_json)) {
233 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
234 goto on_error;
235 }
236
237 return AWS_OP_SUCCESS;
238
239 on_error:
240 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
241 }
242
243 int aws_byte_buf_init_from_resolved_templated_string(
244 struct aws_allocator *allocator,
245 struct aws_byte_buf *out_buf,
246 struct aws_byte_cursor string,
247 aws_endpoints_template_resolve_fn resolve_callback,
248 void *user_data,
249 bool is_json) {
250 AWS_PRECONDITION(allocator);
251
252 struct aws_owning_cursor resolved_template;
253 AWS_ZERO_STRUCT(resolved_template);
254
255 if (aws_byte_buf_init(out_buf, allocator, string.len)) {
256 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
257 }
258
259 size_t quote_count = is_json ? 0 : 1;
260 struct aws_byte_cursor split = {0};
261 struct aws_byte_cursor rest = {0};
262 while (s_split_on_first_delim(string, '{', &split, &rest)) {
263 if (s_append_template_prefix_to_buffer(out_buf, split, &quote_count, is_json)) {
264 AWS_LOGF_ERROR(
265 AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to buffer while evaluating templated sting.");
266 goto on_error;
267 }
268
269 if (quote_count % 2 == 0) {
270 if (aws_byte_buf_append_byte_dynamic(out_buf, '{')) {
271 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
272 goto on_error;
273 }
274 aws_byte_cursor_advance(&rest, 1);
275 string = rest;
276 continue;
277 }
278
279 if (aws_byte_cursor_starts_with(&rest, &escaped_opening_curly)) {
280 if (aws_byte_buf_append_byte_dynamic(out_buf, '{')) {
281 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
282 goto on_error;
283 }
284 aws_byte_cursor_advance(&rest, 2);
285 string = rest;
286 continue;
287 }
288
289 aws_byte_cursor_advance(&rest, 1);
290
291 struct aws_byte_cursor after_closing = {0};
292 if (!s_split_on_first_delim(rest, '}', &split, &after_closing)) {
293 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Unmatched closing curly.");
294 goto on_error;
295 }
296 aws_byte_cursor_advance(&after_closing, 1);
297 string = after_closing;
298
299 if (resolve_callback(split, user_data, &resolved_template)) {
300 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to resolve template.");
301 goto on_error;
302 }
303
304 if (s_buf_append_and_update_quote_count(out_buf, resolved_template.cur, &quote_count, is_json)) {
305 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append resolved value.");
306 goto on_error;
307 }
308
309 aws_owning_cursor_clean_up(&resolved_template);
310 }
311
312 if (s_buf_append_and_update_quote_count(out_buf, split, &quote_count, is_json)) {
313 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
314 goto on_error;
315 }
316
317 return AWS_OP_SUCCESS;
318
319 on_error:
320 aws_byte_buf_clean_up(out_buf);
321 aws_owning_cursor_clean_up(&resolved_template);
322 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
323 }
324
325 int aws_path_through_json(
326 struct aws_allocator *allocator,
327 const struct aws_json_value *root,
328 struct aws_byte_cursor path,
329 const struct aws_json_value **out_value) {
330
331 struct aws_array_list path_segments;
332 if (aws_array_list_init_dynamic(&path_segments, allocator, 10, sizeof(struct aws_byte_cursor)) ||
333 aws_byte_cursor_split_on_char(&path, '.', &path_segments)) {
334 goto on_error;
335 }
336
337 *out_value = root;
338 for (size_t idx = 0; idx < aws_array_list_length(&path_segments); ++idx) {
339 struct aws_byte_cursor path_el_cur;
340 if (aws_array_list_get_at(&path_segments, &path_el_cur, idx)) {
341 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to get path element");
342 goto on_error;
343 }
344
345 struct aws_byte_cursor element_cur = {0};
346 aws_byte_cursor_next_split(&path_el_cur, '[', &element_cur);
347
348 struct aws_byte_cursor index_cur = {0};
349 bool has_index = aws_byte_cursor_next_split(&path_el_cur, '[', &index_cur) &&
350 aws_byte_cursor_next_split(&path_el_cur, ']', &index_cur);
351
352 if (element_cur.len > 0) {
353 *out_value = aws_json_value_get_from_object(*out_value, element_cur);
354 if (NULL == *out_value) {
355 AWS_LOGF_ERROR(
356 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid path. " PRInSTR ".", AWS_BYTE_CURSOR_PRI(element_cur));
357 goto on_error;
358 }
359 }
360
361 if (has_index) {
362 int64_t index;
363
364 if (aws_byte_cursor_utf8_parse_i64(index_cur, &index)) {
365 AWS_LOGF_ERROR(
366 AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE,
367 "Failed to parse index: " PRInSTR,
368 AWS_BYTE_CURSOR_PRI(index_cur));
369 goto on_error;
370 }
371
372 if (index < 0) {
373 index = aws_json_get_array_size(*out_value) - index;
374 }
375
376 if (index < 0) {
377 AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Unexpected negaive index");
378 goto on_error;
379 }
380
381 *out_value = aws_json_get_array_element(*out_value, (size_t)index);
382 if (NULL == *out_value) {
383 aws_reset_error();
384 goto on_success;
385 }
386 }
387 }
388
389 on_success:
390 aws_array_list_clean_up(&path_segments);
391 return AWS_OP_SUCCESS;
392
393 on_error:
394 aws_array_list_clean_up(&path_segments);
395 *out_value = NULL;
396 return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
397 }
398
399 struct aws_owning_cursor aws_endpoints_owning_cursor_create(
400 struct aws_allocator *allocator,
401 const struct aws_string *str) {
402 struct aws_string *clone = aws_string_clone_or_reuse(allocator, str);
403 struct aws_owning_cursor ret = {.string = clone, .cur = aws_byte_cursor_from_string(clone)};
404 return ret;
405 }
406
407 struct aws_owning_cursor aws_endpoints_owning_cursor_from_string(struct aws_string *str) {
408 struct aws_owning_cursor ret = {.string = str, .cur = aws_byte_cursor_from_string(str)};
409 return ret;
410 }
411
412 struct aws_owning_cursor aws_endpoints_owning_cursor_from_cursor(
413 struct aws_allocator *allocator,
414 const struct aws_byte_cursor cur) {
415 struct aws_string *clone = aws_string_new_from_cursor(allocator, &cur);
416 struct aws_owning_cursor ret = {.string = clone, .cur = aws_byte_cursor_from_string(clone)};
417 return ret;
418 }
419
420 struct aws_owning_cursor aws_endpoints_non_owning_cursor_create(struct aws_byte_cursor cur) {
421 struct aws_owning_cursor ret = {.string = NULL, .cur = cur};
422 return ret;
423 }
424
425 void aws_owning_cursor_clean_up(struct aws_owning_cursor *cursor) {
426 aws_string_destroy(cursor->string);
427 cursor->string = NULL;
428 cursor->cur.ptr = NULL;
429 cursor->cur.len = 0;
430 }