From efbe889ae925c780e2446bcea7ffd2f887db93f9 Mon Sep 17 00:00:00 2001 From: "ssw@internet2.edu" Date: Fri, 6 Jan 2023 17:05:26 -0500 Subject: [PATCH] fixed bug when user supplied AS in front of their ASN --- docker_container_version/main.py | 32 +++++++++++++++++-- docker_container_version/templates/index.html | 2 +- main.py | 6 +++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/docker_container_version/main.py b/docker_container_version/main.py index 8d06484..d8f189f 100644 --- a/docker_container_version/main.py +++ b/docker_container_version/main.py @@ -5,8 +5,8 @@ import json import os +# point to templates directory relative to home directory template_dir = template_dir = os.path.join(os.path.dirname(__file__), 'templates') - app = Flask(__name__, template_folder=template_dir) def get_asn_from_as(asn): # Remove the "AS" from the beginning of the ASN @@ -26,7 +26,12 @@ def get_more_specifics(data): origins.append(prefix_origin['origin']) return [prefixes, origins] def get_prefix_roa_status(prefix, origin): - """ given a prefix, determine if it is covered by an existing ROA""" + """ + :param prefix: prefix to check + :param origin: origin ASN to check + :return: ROV status of the prefix based on RIPEstat ROA data + """ + url = f"https://stat.ripe.net/data/rpki-validation/data.json?resource={origin}&prefix={prefix}" response = requests.get(url) data = json.loads(response.text) @@ -38,7 +43,10 @@ def get_prefix_roa_status(prefix, origin): return None return(validation_status) def get_prefix_info(prefix): - """ given a prefix, return the more specific prefixes and their origins as seen by RIPEstat""" + """ + :param prefix: covering prefix to check for more specific prefixes + :return: list of more specific prefixes and their origins + """ url = "https://stat.ripe.net/data/routing-status/data.json?resource=" + prefix try: response = requests.get(url) @@ -92,6 +100,11 @@ def is_valid_asn(asn): return False def is_valid_prefix_maxlength(ip_prefix, prefix_maxlength): + """ check to see prefix_maxlength is a valid value for the prefix + :param ip_prefix: prefix to check + :param prefix_maxlength: maximum length of the prefix + :return: True if prefix_maxlength is valid, False otherwise + """ try: # Try to parse the prefix maxlength as an integer prefix_maxlength = int(prefix_maxlength) @@ -106,6 +119,15 @@ def is_valid_prefix_maxlength(ip_prefix, prefix_maxlength): def check_list_of_prefixes_against_ROA(origin, prefixes, origins, roa_prefix, roa_maxlen, roa_asn): + """ given a list of prefixes and their origins, check if the ROA covers them + :param origin: origin ASN + :param prefixes: list of prefixes + :param origins: list of origins + :param roa_prefix: prefix from the ROA + :param roa_maxlen: maximum length of the prefix from the ROA + :param roa_asn: ASN from the ROA + :return: list output lines to be displayed on the web page + """ messages = [] existing_roa_status = get_prefix_roa_status(roa_prefix, origin) @@ -120,6 +142,10 @@ def check_list_of_prefixes_against_ROA(origin, prefixes, origins, roa_prefix, ro @app.route('/', methods=['GET', 'POST']) def index(): + """ main function to handle the web page + :return: web page + """ + if request.method == 'POST': # Get the user input roa_ip_prefix = request.form['ip_prefix'] diff --git a/docker_container_version/templates/index.html b/docker_container_version/templates/index.html index 305a88d..1d00251 100644 --- a/docker_container_version/templates/index.html +++ b/docker_container_version/templates/index.html @@ -42,7 +42,7 @@ -

This app queries the stat.ripe.net to determine if a RPKI-ROA created with the following information would likely agree (i.e., not evaluate as invalid) for routes currently seen in the Internet

+

This app queries the stat.ripe.net to determine if a RPKI-ROA created with the following information
would likely agree (i.e., not evaluate as invalid) for routes currently seen in the Internet

diff --git a/main.py b/main.py index d8f189f..81476ec 100644 --- a/main.py +++ b/main.py @@ -128,7 +128,6 @@ def check_list_of_prefixes_against_ROA(origin, prefixes, origins, roa_prefix, ro :param roa_asn: ASN from the ROA :return: list output lines to be displayed on the web page """ - messages = [] existing_roa_status = get_prefix_roa_status(roa_prefix, origin) messages.append([roa_prefix, return_rov_status(roa_prefix, roa_maxlen, roa_asn, roa_prefix, origin), origin, @@ -156,6 +155,10 @@ def index(): origin_asn = request.form['origin_asn'] origin_asn = origin_asn.strip() + + origin_asn = get_asn_from_as(origin_asn) + + # Validate the origin ASN if not is_valid_asn(origin_asn): return f"\"{origin_asn}\" is an Invalid origin ASN" @@ -168,6 +171,7 @@ def index(): roa_ip_prefix = request.form['ip_prefix'] roa_prefix_maxlength = int(request.form['prefix_maxlength']) roa_origin_asn = request.form['origin_asn'] + roa_origin_asn = get_asn_from_as(roa_origin_asn) prefix_info = get_prefix_info(roa_ip_prefix) if prefix_info is None: return "Prefix not found or problems with RIPEstat API"