Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve rpki validation #62

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kartograf/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self, args):
self.args.reproduce += '/'
self.data_dir = self.args.reproduce
else:
self.data_dir = str(cwd / "data" / self.epoch_dir)
self.data_dir = str(Path("data") / self.epoch_dir)

if Path(self.data_dir).exists() and not self.reproduce:
print("Not so fast, a folder with that epoch already exists.")
Expand Down
40 changes: 26 additions & 14 deletions kartograf/rpki/fetch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import subprocess
import sys

from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing in one commit and then re-adding in the next is a bit annoying to review. You could squash both commits and that would be a bit easier.

import json
from threading import Lock
from pathlib import Path
import requests
Expand Down Expand Up @@ -97,7 +98,7 @@ def validate_rpki_db(context):
with open(context.debug_log, 'a') as logs:
logs.write("\n\n=== RPKI Validation ===\n")

def process_file(file):
def process_file(batch):
result = subprocess.run(["rpki-client",
"-j",
"-n",
Expand All @@ -106,26 +107,37 @@ def process_file(file):
"-P",
context.epoch,
] + tal_options +
["-f", file], # -f has to be last
["-f"] + batch, # -f has to be last
capture_output=True,
check=False)

if result.stderr and context.debug_log:
stderr_output = result.stderr.decode()
with debug_file_lock:
with open(context.debug_log, 'a') as logs:
logs.write(f'\nfile: {file}\n')
logs.write(stderr_output)
return result.stdout

with ThreadPoolExecutor() as executor:
results = list(tqdm(executor.map(process_file, files), total=len(files)))

json_results = [result.decode() for result in results if result]
total = len(files)
batch_size = 250
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should test more batch size values. 250 worked out better than 100 and 500, so I stuck with it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already a great win, it can be optimized further later. Let's first make sure it's robust and get it in asap :)

batches = []
for i in range(0, total, batch_size):
batch = [str(f) for f in files[i:i + batch_size]]
batches.append(batch)

with open(result_path, "w") as res_file:
res_file.write("[")
res_file.write(",".join(json_results))
res_file.write("]")

print(f"{len(json_results)} RKPI ROAs validated and saved to {result_path}, file hash: {calculate_sha256(result_path)}")
total_batches = len(batches)
results = []
with ThreadPoolExecutor() as executor:
futures = [executor.submit(process_file, batch) for batch in batches]
for future in tqdm(as_completed(futures), total=total_batches):
result = future.result()
if result:
normalized = result.replace(b"\n}\n{\n\t", b"\n},\n{\n").decode('utf-8').strip()
results.append(normalized)
results_json = json.loads("[" + ",".join(results) + "]")
s = sorted(results_json, key=lambda result: result["hash_id"])

with open(result_path, 'w') as f:
json.dump(s, f)

print(f"RKPI ROAs validated and saved to {result_path}, file hash: {calculate_sha256(result_path)}")
2 changes: 1 addition & 1 deletion kartograf/rpki/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def parse_rpki(context):
if not prefix or is_bogon_pfx(prefix) or is_bogon_asn(asn):
if context.debug_log:
with open(context.debug_log, 'a') as logs:
logs.write(f"RPKI: parser encountered an invalid IP network: {prefix}")
logs.write(f"RPKI: parser encountered an invalid IP network: {prefix} \n")
continue

if context.max_encode and is_out_of_encoding_range(asn, context.max_encode):
Expand Down