Skip to content

Commit

Permalink
fixed bug when user supplied AS in front of their ASN
Browse files Browse the repository at this point in the history
  • Loading branch information
ssw committed Jan 6, 2023
1 parent 4b959ef commit efbe889
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
32 changes: 29 additions & 3 deletions docker_container_version/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion docker_container_version/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</style>
</head>
<body>
<p>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</p>
<p style="text-align: center">This app queries the stat.ripe.net to determine if a RPKI-ROA created with the following information<br>would likely agree (i.e., not evaluate as invalid) for routes currently seen in the Internet</p>
<form method="post">
<label for="ip_prefix">IP Prefix:</label>
<input type="text" name="ip_prefix" placeholder="10.1.0.0/16" required>
Expand Down
6 changes: 5 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit efbe889

Please sign in to comment.