| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/array_list.h> |
| 7 |
#include <aws/common/byte_buf.h> |
| 8 |
#include <aws/common/hash_table.h> |
| 9 |
#include <aws/common/json.h> |
| 10 |
#include <aws/common/ref_count.h> |
| 11 |
#include <aws/common/string.h> |
| 12 |
#include <aws/sdkutils/partitions.h> |
| 13 |
#include <aws/sdkutils/private/endpoints_regex.h> |
| 14 |
#include <aws/sdkutils/private/endpoints_types_impl.h> |
| 15 |
#include <aws/sdkutils/private/endpoints_util.h> |
| 16 |
|
| 17 |
static struct aws_byte_cursor s_supported_version = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("1.0"); |
| 18 |
|
| 19 |
struct aws_byte_cursor aws_partitions_get_supported_version(void) { |
| 20 |
return s_supported_version; |
| 21 |
} |
| 22 |
|
| 23 |
static void s_partitions_config_destroy(void *data) { |
| 24 |
if (data == NULL) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
struct aws_partitions_config *partitions = data; |
| 29 |
|
| 30 |
aws_json_value_destroy(partitions->json_root); |
| 31 |
aws_string_destroy(partitions->version); |
| 32 |
aws_hash_table_clean_up(&partitions->base_partitions); |
| 33 |
aws_hash_table_clean_up(&partitions->region_to_partition_info); |
| 34 |
aws_mem_release(partitions->allocator, partitions); |
| 35 |
} |
| 36 |
|
| 37 |
struct region_merge_wrapper { |
| 38 |
struct aws_json_value *outputs_node; |
| 39 |
struct aws_json_value *merge_node; |
| 40 |
}; |
| 41 |
|
| 42 |
static int s_on_region_merge( |
| 43 |
const struct aws_byte_cursor *key, |
| 44 |
const struct aws_json_value *value, |
| 45 |
bool *out_should_continue, |
| 46 |
void *user_data) { |
| 47 |
(void)out_should_continue; |
| 48 |
|
| 49 |
struct region_merge_wrapper *merge = user_data; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
if (aws_byte_cursor_eq_c_str(key, "description")) { |
| 63 |
return AWS_OP_SUCCESS; |
| 64 |
} |
| 65 |
|
| 66 |
if (merge->merge_node == NULL) { |
| 67 |
merge->merge_node = aws_json_value_duplicate(merge->outputs_node); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
if (aws_json_value_has_key(merge->merge_node, *key) && aws_json_value_remove_from_object(merge->merge_node, *key)) { |
| 75 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to remove previous partition value."); |
| 76 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 77 |
} |
| 78 |
|
| 79 |
if (aws_json_value_add_to_object(merge->merge_node, *key, aws_json_value_duplicate(value))) { |
| 80 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to overwrite partition data."); |
| 81 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 82 |
} |
| 83 |
|
| 84 |
return AWS_OP_SUCCESS; |
| 85 |
} |
| 86 |
|
| 87 |
struct partition_parse_wrapper { |
| 88 |
struct aws_partitions_config *partitions; |
| 89 |
struct aws_json_value *outputs_node; |
| 90 |
struct aws_string *outputs_str; |
| 91 |
}; |
| 92 |
|
| 93 |
static int s_on_region_element( |
| 94 |
const struct aws_byte_cursor *key, |
| 95 |
const struct aws_json_value *value, |
| 96 |
bool *out_should_continue, |
| 97 |
void *user_data) { |
| 98 |
(void)out_should_continue; |
| 99 |
|
| 100 |
struct aws_partition_info *partition_info = NULL; |
| 101 |
struct partition_parse_wrapper *wrapper = user_data; |
| 102 |
|
| 103 |
struct region_merge_wrapper merge = { |
| 104 |
.outputs_node = wrapper->outputs_node, |
| 105 |
.merge_node = NULL, |
| 106 |
}; |
| 107 |
|
| 108 |
if (aws_json_const_iterate_object(value, s_on_region_merge, &merge)) { |
| 109 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to parse partitions."); |
| 110 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 111 |
} |
| 112 |
|
| 113 |
if (merge.merge_node != NULL) { |
| 114 |
partition_info = aws_partition_info_new(wrapper->partitions->allocator, *key); |
| 115 |
partition_info->info = aws_string_new_from_json(wrapper->partitions->allocator, merge.merge_node); |
| 116 |
aws_json_value_destroy(merge.merge_node); |
| 117 |
} else { |
| 118 |
partition_info = aws_partition_info_new(wrapper->partitions->allocator, *key); |
| 119 |
partition_info->info = wrapper->outputs_str; |
| 120 |
partition_info->is_copy = true; |
| 121 |
} |
| 122 |
|
| 123 |
if (aws_hash_table_put( |
| 124 |
&wrapper->partitions->region_to_partition_info, &partition_info->name, partition_info, NULL)) { |
| 125 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to add partition info."); |
| 126 |
goto on_error; |
| 127 |
} |
| 128 |
|
| 129 |
return AWS_OP_SUCCESS; |
| 130 |
|
| 131 |
on_error: |
| 132 |
aws_partition_info_destroy(partition_info); |
| 133 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 134 |
} |
| 135 |
|
| 136 |
static int s_on_partition_element( |
| 137 |
size_t idx, |
| 138 |
const struct aws_json_value *partition_node, |
| 139 |
bool *out_should_continue, |
| 140 |
void *user_data) { |
| 141 |
(void)out_should_continue; |
| 142 |
(void)idx; |
| 143 |
|
| 144 |
struct aws_partitions_config *partitions = user_data; |
| 145 |
|
| 146 |
struct aws_byte_cursor id_cur; |
| 147 |
struct aws_json_value *id_node = aws_json_value_get_from_object(partition_node, aws_byte_cursor_from_c_str("id")); |
| 148 |
if (id_node == NULL || aws_json_value_get_string(id_node, &id_cur)) { |
| 149 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to extract id of partition."); |
| 150 |
goto on_error; |
| 151 |
} |
| 152 |
|
| 153 |
struct aws_json_value *outputs_node = |
| 154 |
aws_json_value_get_from_object(partition_node, aws_byte_cursor_from_c_str("outputs")); |
| 155 |
if (outputs_node == NULL) { |
| 156 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to extract outputs of partition."); |
| 157 |
goto on_error; |
| 158 |
} |
| 159 |
|
| 160 |
struct aws_json_value *regex_node = |
| 161 |
aws_json_value_get_from_object(partition_node, aws_byte_cursor_from_c_str("regionRegex")); |
| 162 |
|
| 163 |
struct aws_partition_info *partition_info = aws_partition_info_new(partitions->allocator, id_cur); |
| 164 |
partition_info->info = aws_string_new_from_json(partitions->allocator, outputs_node); |
| 165 |
|
| 166 |
if (regex_node != NULL) { |
| 167 |
struct aws_byte_cursor regex_cur = {0}; |
| 168 |
if (aws_json_value_get_string(regex_node, ®ex_cur)) { |
| 169 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to parse region regex."); |
| 170 |
goto on_partitions_new_error; |
| 171 |
} |
| 172 |
|
| 173 |
partition_info->region_regex = aws_endpoints_regex_new(partitions->allocator, regex_cur); |
| 174 |
if (partition_info->region_regex == NULL) { |
| 175 |
goto on_partitions_new_error; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (partition_info->info == NULL) { |
| 180 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to parse partition info."); |
| 181 |
goto on_partitions_new_error; |
| 182 |
} |
| 183 |
|
| 184 |
if (aws_hash_table_put(&partitions->base_partitions, &partition_info->name, partition_info, NULL)) { |
| 185 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to add partition info."); |
| 186 |
goto on_partitions_new_error; |
| 187 |
} |
| 188 |
|
| 189 |
struct partition_parse_wrapper wrapper = { |
| 190 |
.outputs_node = outputs_node, |
| 191 |
.outputs_str = partition_info->info, |
| 192 |
.partitions = partitions, |
| 193 |
}; |
| 194 |
|
| 195 |
struct aws_json_value *regions_node = |
| 196 |
aws_json_value_get_from_object(partition_node, aws_byte_cursor_from_c_str("regions")); |
| 197 |
if (regions_node != NULL && aws_json_const_iterate_object(regions_node, s_on_region_element, &wrapper)) { |
| 198 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to parse regions."); |
| 199 |
goto on_error; |
| 200 |
} |
| 201 |
|
| 202 |
return AWS_OP_SUCCESS; |
| 203 |
|
| 204 |
on_partitions_new_error: |
| 205 |
aws_partition_info_destroy(partition_info); |
| 206 |
on_error: |
| 207 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 208 |
} |
| 209 |
|
| 210 |
static int s_init_partitions_config_from_json( |
| 211 |
struct aws_allocator *allocator, |
| 212 |
struct aws_partitions_config *partitions, |
| 213 |
struct aws_byte_cursor partitions_cur) { |
| 214 |
|
| 215 |
struct aws_json_value *root = aws_json_value_new_from_string(allocator, partitions_cur); |
| 216 |
|
| 217 |
if (root == NULL) { |
| 218 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_PARSING, "Failed to parse provided string as json."); |
| 219 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 220 |
} |
| 221 |
|
| 222 |
partitions->json_root = root; |
| 223 |
|
| 224 |
struct aws_byte_cursor version_cur; |
| 225 |
struct aws_json_value *version_node = aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("version")); |
| 226 |
if (version_node == NULL || aws_json_value_get_string(version_node, &version_cur)) { |
| 227 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to extract version."); |
| 228 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_UNSUPPORTED); |
| 229 |
} |
| 230 |
|
| 231 |
#ifdef ENDPOINTS_VERSION_CHECK |
| 232 |
if (!aws_byte_cursor_eq_c_str(&version_cur, &s_supported_version)) { |
| 233 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Unsupported partitions version."); |
| 234 |
aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_UNSUPPORTED); |
| 235 |
goto on_error; |
| 236 |
} |
| 237 |
#endif |
| 238 |
|
| 239 |
struct aws_json_value *partitions_node = |
| 240 |
aws_json_value_get_from_object(root, aws_byte_cursor_from_c_str("partitions")); |
| 241 |
if (partitions_node == NULL || aws_json_const_iterate_array(partitions_node, s_on_partition_element, partitions)) { |
| 242 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to parse partitions."); |
| 243 |
return aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 244 |
} |
| 245 |
|
| 246 |
return AWS_OP_SUCCESS; |
| 247 |
} |
| 248 |
|
| 249 |
static void s_callback_partition_info_destroy(void *data) { |
| 250 |
struct aws_partition_info *info = data; |
| 251 |
aws_partition_info_destroy(info); |
| 252 |
} |
| 253 |
|
| 254 |
struct aws_partitions_config *aws_partitions_config_new_from_string( |
| 255 |
struct aws_allocator *allocator, |
| 256 |
struct aws_byte_cursor json) { |
| 257 |
|
| 258 |
AWS_PRECONDITION(allocator); |
| 259 |
AWS_PRECONDITION(aws_byte_cursor_is_valid(&json)); |
| 260 |
|
| 261 |
struct aws_partitions_config *partitions = aws_mem_calloc(allocator, 1, sizeof(struct aws_partitions_config)); |
| 262 |
partitions->allocator = allocator; |
| 263 |
|
| 264 |
if (aws_hash_table_init( |
| 265 |
&partitions->base_partitions, |
| 266 |
allocator, |
| 267 |
10, |
| 268 |
aws_hash_byte_cursor_ptr, |
| 269 |
aws_endpoints_byte_cursor_eq, |
| 270 |
NULL, |
| 271 |
s_callback_partition_info_destroy)) { |
| 272 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to init partition info map."); |
| 273 |
aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 274 |
goto on_error; |
| 275 |
} |
| 276 |
|
| 277 |
if (aws_hash_table_init( |
| 278 |
&partitions->region_to_partition_info, |
| 279 |
allocator, |
| 280 |
20, |
| 281 |
aws_hash_byte_cursor_ptr, |
| 282 |
aws_endpoints_byte_cursor_eq, |
| 283 |
NULL, |
| 284 |
s_callback_partition_info_destroy)) { |
| 285 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to init partition info map."); |
| 286 |
aws_raise_error(AWS_ERROR_SDKUTILS_PARTITIONS_PARSE_FAILED); |
| 287 |
goto on_error; |
| 288 |
} |
| 289 |
|
| 290 |
if (s_init_partitions_config_from_json(allocator, partitions, json)) { |
| 291 |
AWS_LOGF_ERROR(AWS_LS_SDKUTILS_PARTITIONS_PARSING, "Failed to init partition info from json."); |
| 292 |
goto on_error; |
| 293 |
} |
| 294 |
|
| 295 |
aws_ref_count_init(&partitions->ref_count, partitions, s_partitions_config_destroy); |
| 296 |
|
| 297 |
return partitions; |
| 298 |
|
| 299 |
on_error: |
| 300 |
s_partitions_config_destroy(partitions); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
|
| 304 |
struct aws_partitions_config *aws_partitions_config_acquire(struct aws_partitions_config *partitions) { |
| 305 |
AWS_PRECONDITION(partitions); |
| 306 |
if (partitions) { |
| 307 |
aws_ref_count_acquire(&partitions->ref_count); |
| 308 |
} |
| 309 |
return partitions; |
| 310 |
} |
| 311 |
|
| 312 |
struct aws_partitions_config *aws_partitions_config_release(struct aws_partitions_config *partitions) { |
| 313 |
if (partitions) { |
| 314 |
aws_ref_count_release(&partitions->ref_count); |
| 315 |
} |
| 316 |
return NULL; |
| 317 |
} |