diff --git a/wanda/as_filter/as_filter.py b/wanda/as_filter/as_filter.py index 658d998..09a9f68 100644 --- a/wanda/as_filter/as_filter.py +++ b/wanda/as_filter/as_filter.py @@ -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 @@ -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() @@ -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]) @@ -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)) diff --git a/wanda/filter_list_generation.py b/wanda/filter_list_generation.py index 811f518..99a2eba 100644 --- a/wanda/filter_list_generation.py +++ b/wanda/filter_list_generation.py @@ -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 diff --git a/wanda/tests/test_as_filter.py b/wanda/tests/test_as_filter.py index cef1302..41c0756 100644 --- a/wanda/tests/test_as_filter.py +++ b/wanda/tests/test_as_filter.py @@ -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) @@ -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