1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
# This script pulls latest 'partitions.json' and 's3-endpoint-rule-set.json' from Git.
# You will need a secret in secrets manager which has the 'ruleset-url' and 'ruleset-token'.
# It uses the latest files to generate 'source/s3_endpoint_resolver/aws_s3_endpoint_rule_set.c' and
# 'source/s3_endpoint_resolver/aws_s3_endpoint_resolver_partition.c'
import argparse
import json
import boto3
import requests
def escape_char(c):
escape_dict = {
'\\': '\\\\',
'\'': '\\\'',
'\0': '\\0',
'\a': '\\a',
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
'\v': '\\v'
}
return escape_dict.get(c, c)
def get_header():
return """\
/**
* Copyright Amazon.com, Inc. or its affiliates.
* All Rights Reserved. SPDX-License-Identifier: Apache-2.0.
*/
#include "aws/s3/private/s3_endpoint_resolver.h"
#include <aws/s3/s3_endpoint_resolver.h>
/**
* This file is generated using scripts/update_s3_endpoint_resolver_artifacts.py.
* Do not modify directly. */
/* clang-format off */
"""
def generate_c_file_from_json(json_content, c_file_name, c_struct_name):
num_chars_per_line = 20
try:
# Compact the json
compact_json_str = json.dumps(json_content, separators=(',', ':'))
compact_c = []
for i in range(0, len(compact_json_str), num_chars_per_line):
compact_c.append(
', '.join("'{}'".format(escape_char(char)) for char in compact_json_str[i:i + num_chars_per_line]))
# Write json to a C file
with open(c_file_name, 'w') as f:
f.write(get_header())
f.write(f"static const char s_generated_array[] = {{\n\t")
f.write(",\n\t".join(compact_c))
f.write("};\n\n")
f.write(f"const struct aws_byte_cursor {c_struct_name} = {{\n\t")
f.write(f".len = {len(compact_json_str)},\n\t")
f.write(f".ptr = (uint8_t *) s_generated_array\n}};\n")
print(f"{c_file_name} has been created successfully.")
except Exception as e:
print(f"An error occurred: {e}")
def get_secret_from_secrets_manager(secret_name, region_name):
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except Exception as e:
raise e
return json.loads(get_secret_value_response['SecretString'])
def download_from_git(url, token=None):
headers = {'Accept': 'application/vnd.github+json'}
if token is not None:
headers['Authorization'] = f"Bearer {token}"
http_response = requests.get(url, headers=headers)
if http_response.status_code != 200:
raise Exception(f"HTTP Status code is {http_response.status_code}")
return json.loads(http_response.content.decode())
if __name__ == '__main__':
argument_parser = argparse.ArgumentParser(description="Endpoint Ruleset Updater")
argument_parser.add_argument("--ruleset", metavar="<Path to ruleset>",
required=False, help="Path to endpoint ruleset json file")
argument_parser.add_argument("--partitions", metavar="<Path to partitions>",
required=False, help="Path to partitions json file")
parsed_args = argument_parser.parse_args()
git_secret = get_secret_from_secrets_manager("s3/endpoint/resolver/artifacts/git", "us-east-1")
if (parsed_args.ruleset):
with open(parsed_args.ruleset) as f:
rule_set = json.load(f)
else:
rule_set = download_from_git(git_secret['ruleset-url'], git_secret['ruleset-token'])
if (parsed_args.partitions):
with open(parsed_args.partitions) as f:
partition = json.load(f)
else:
partition = download_from_git('https://raw.githubusercontent.com/aws/aws-sdk-cpp/main/tools/code-generation/partitions/partitions.json')
generate_c_file_from_json(
rule_set,
'source/s3_endpoint_resolver/aws_s3_endpoint_rule_set.c',
'aws_s3_endpoint_rule_set')
generate_c_file_from_json(
partition,
'source/s3_endpoint_resolver/aws_s3_endpoint_resolver_partition.c',
'aws_s3_endpoint_resolver_partitions')
|