0
0
Fork 0

Rewrite how registry objects get parsed for better compatibility

This commit is contained in:
Jan Philipp Timme 2019-06-02 19:45:28 +02:00
parent 535d9420a3
commit 725874d163
1 changed files with 37 additions and 14 deletions

View File

@ -52,27 +52,42 @@ def parse_route_objects(folder_path):
for root, dirs, files in os.walk(folder_path): for root, dirs, files in os.walk(folder_path):
for filename in files: for filename in files:
current_file = str(root) + str(filename) 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) route_objects.append(route_object)
return route_objects return route_objects
def read_route_object_file(file_path): def read_schema_object_file(file_path):
with open(file_path) as f: with open(file_path) as f:
data = f.read() data = f.read()
route_object = {} schema_object = {}
current_key = None
current_value = None
for line in data.split('\n'): for line in data.split('\n'):
if line.strip() == '': continue # Ignore completely empty lines
items = line.split(':', 1) if line == '': continue
key = items[0].strip() # If a new key/value pair comes up (meaning there's a : within the first 20 chars)
value = items[1].strip() if ':' in line[0:20]:
if key in route_object.keys(): # Write last processed key/value-pair into schema_object if needed
if isinstance(route_object[key], list): if current_key != None:
route_object[key].append(value) if current_key in schema_object.keys():
else: if isinstance(schema_object[current_key], list):
route_object[key] = [route_object[key], value] 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: else:
route_object[key] = value # This denotes a linebreak in a multiline value
return route_object 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): def create_roa_entries(route_objects, filter_rules, mode, f):
for route_object in route_objects: 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 not permitted: ' + str(route_network) + "\n")
f.write('# route object was denied by filter rule: ' + str(matching_filter_rule) + "\n") f.write('# route object was denied by filter rule: ' + str(matching_filter_rule) + "\n")
continue continue
# Check if min-length fits filter requirements
# Figure out max-length (filter rule wins over route object) # Figure out max-length (filter rule wins over route object)
if 'max-length' in route_object.keys(): if 'max-length' in route_object.keys():
allowed_max_len = min(int(matching_filter_rule['max_length']), int(route_object['max-length'])) 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__': if __name__ == '__main__':
# Get registry path over commandline argument # Get registry path over commandline argument
registry_path = sys.argv[1] 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 # Read filter rules
ipv4_filter_rules = parse_ipv4_filter_rules(registry_path) ipv4_filter_rules = parse_ipv4_filter_rules(registry_path)
ipv6_filter_rules = parse_ipv6_filter_rules(registry_path) ipv6_filter_rules = parse_ipv6_filter_rules(registry_path)