From 725874d1630c62afe225838b8cbbf6552225ca9d Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Sun, 2 Jun 2019 19:45:28 +0200 Subject: [PATCH] Rewrite how registry objects get parsed for better compatibility --- generate.py | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/generate.py b/generate.py index 8b4900e..20eac54 100755 --- a/generate.py +++ b/generate.py @@ -52,27 +52,42 @@ def parse_route_objects(folder_path): for root, dirs, files in os.walk(folder_path): for filename in files: current_file = str(root) + str(filename) - route_object = read_route_object_file(current_file) + route_object = read_schema_object_file(current_file) route_objects.append(route_object) return route_objects -def read_route_object_file(file_path): +def read_schema_object_file(file_path): with open(file_path) as f: data = f.read() - route_object = {} + schema_object = {} + current_key = None + current_value = None for line in data.split('\n'): - if line.strip() == '': continue - items = line.split(':', 1) - key = items[0].strip() - value = items[1].strip() - if key in route_object.keys(): - if isinstance(route_object[key], list): - route_object[key].append(value) - else: - route_object[key] = [route_object[key], value] + # Ignore completely empty lines + if line == '': continue + # If a new key/value pair comes up (meaning there's a : within the first 20 chars) + if ':' in line[0:20]: + # Write last processed key/value-pair into schema_object if needed + if current_key != None: + if current_key in schema_object.keys(): + if isinstance(schema_object[current_key], list): + schema_object[current_key].append(current_value) + else: + schema_object[current_key] = [schema_object[current_key], current_value] + else: + schema_object[current_key] = current_value + # Then begin parsing the next one + items = line.split(':', 1) + current_key = items[0].strip() + current_value = items[1].strip() else: - route_object[key] = value - return route_object + # This denotes a linebreak in a multiline value + if line[0] == '+': + current_value += '\n' + elif line.startswith(' ' * 20): + # This is an additional line in a multiline value + current_value += line[20:] + '\n' + return schema_object def create_roa_entries(route_objects, filter_rules, mode, f): for route_object in route_objects: @@ -101,6 +116,9 @@ def create_roa_entries(route_objects, filter_rules, mode, f): f.write('# route object not permitted: ' + str(route_network) + "\n") f.write('# route object was denied by filter rule: ' + str(matching_filter_rule) + "\n") continue + # Check if min-length fits filter requirements + + # Figure out max-length (filter rule wins over route object) if 'max-length' in route_object.keys(): allowed_max_len = min(int(matching_filter_rule['max_length']), int(route_object['max-length'])) @@ -119,6 +137,11 @@ def create_roa_entries(route_objects, filter_rules, mode, f): if __name__ == '__main__': # Get registry path over commandline argument registry_path = sys.argv[1] + + # Uncomment this block to test the parser against that evil file in the registry. + #print(read_schema_object_file(registry_path + '/data/schema/SCHEMA-SCHEMA')) + #exit + # Read filter rules ipv4_filter_rules = parse_ipv4_filter_rules(registry_path) ipv6_filter_rules = parse_ipv6_filter_rules(registry_path)