Skip to content

Commit

Permalink
Merge pull request #3 from wobcom/peer-filters
Browse files Browse the repository at this point in the history
enable filter lists for small non-downstream peers
  • Loading branch information
johannwagner authored Dec 14, 2022
2 parents 02bd341 + 37c3824 commit ba6d72e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
16 changes: 9 additions & 7 deletions wanda/as_filter/as_filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from functools import cached_property

from wanda.autonomous_system.autonomous_system import AutonomousSystem
from wanda.irrd_client import IRRDClient
Expand All @@ -9,12 +10,13 @@

class ASFilter:

def __init__(self, irrd_client: IRRDClient, autos: AutonomousSystem, enable_extended_filters=False):
def __init__(self, irrd_client: IRRDClient, autos: AutonomousSystem, is_customer=True):
self.irrd_client = irrd_client
self.autos = autos
self.enable_extended_filters = enable_extended_filters
self.is_customer = is_customer

def get_prefix_lists(self):
@cached_property
def prefix_lists(self):

v4_set = set()
v6_set = set()
Expand All @@ -27,14 +29,14 @@ def get_prefix_lists(self):
v4_set.update(result_entries_v4_cleaned)
v6_set.update(result_entries_v6_cleaned)

if len(v4_set) == 0 and len(v6_set) == 0:
if len(v4_set) == 0 and len(v6_set) == 0 and self.is_customer:
l.error(f"{self.autos} has no v4 filter lists.")
raise Exception(
f"{self.autos} has no v6 filter lists. Since AS is our customer, we forbid this for security reasons.")

return v4_set, v6_set

def get_filter_lists(self):
def get_filter_lists(self, enable_extended_filters=False):

irr_names = self.autos.get_irr_names()
file_content = self.irrd_client.generate_input_aspath_access_list(self.autos.asn, irr_names[0])
Expand All @@ -43,8 +45,8 @@ def get_filter_lists(self):
l.warning(f"{self.autos} could not generate as-path access-lists, this breaks configuration syntax..")
return ""

if self.enable_extended_filters:
v4_set, v6_set = self.get_prefix_lists()
if enable_extended_filters:
v4_set, v6_set = self.prefix_lists

v4_tmpl = ';\n '.join(sorted(v4_set))
v6_tmpl = ';\n '.join(sorted(v6_set))
Expand Down
9 changes: 5 additions & 4 deletions wanda/filter_list_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
def process_filter_lists_for_as(arg):
[irrd_client, autonomous_system, customer_as, filter_lists] = arg
asn = autonomous_system.asn
is_customer = asn in customer_as

extended_filtering = asn in customer_as

ass = ASFilter(irrd_client, autonomous_system, enable_extended_filters=extended_filtering)
as_filter_list = ass.get_filter_lists()
ass = ASFilter(irrd_client, autonomous_system, is_customer=is_customer)
v4_set, v6_set = ass.prefix_lists
extended_filtering = is_customer or len(v4_set) + len(v6_set) < 5000
as_filter_list = ass.get_filter_lists(enable_extended_filters=extended_filtering)

filter_lists[asn] = as_filter_list

Expand Down
12 changes: 7 additions & 5 deletions wanda/tests/test_as_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ def get_asfilter(**kwargs):
class TestASFilter:

@pytest.mark.parametrize(
"asfilter,enable_extended_filters",
"enable_extended_filters",
[
(get_asfilter(enable_extended_filters=True), True),
(get_asfilter(enable_extended_filters=False), False)
(True),
(False)
]
)
def test_prefix_lists(self, mocker, asfilter, enable_extended_filters):
def test_prefix_lists(self, mocker, enable_extended_filters):
asfilter = get_asfilter()

mocker.patch(
'wanda.irrd_client.IRRDClient.generate_prefix_lists',
return_value=(WOBCOM_PREFIX_LIST_MOCK_V4, WOBCOM_PREFIX_LIST_MOCK_V6)
Expand All @@ -72,7 +74,7 @@ def test_prefix_lists(self, mocker, asfilter, enable_extended_filters):
return_value=AS_PATH_MOCK
)

file_content = asfilter.get_filter_lists()
file_content = asfilter.get_filter_lists(enable_extended_filters)

assert len(file_content) > 0

Expand Down

0 comments on commit ba6d72e

Please sign in to comment.