From 6033b38f5694504306e93bff769e6e2c0c31cbd1 Mon Sep 17 00:00:00 2001 From: Shubham Date: Mon, 9 Nov 2026 15:35:53 +1300 Subject: [PATCH 1/2] Compare the traffic before warning about a filter's spelling (#440 N1, N2) N1: a pair whose filter names differ only in spelling was reported as 'could not be compared' before its traffic was compared, so two rules about different hosts warned although they cannot collide whichever ACL Batfish resolves the names to. The warning now needs traffic that overlaps or cannot be decided. between() shares the loop, so #426's check is fixed too. N2: pin each protocol number to its own protocol, and acl_in,acl_out as a specifier. Changing '17' to tcp, '1' to udp, or dropping ',' from the specifier pattern used to pass every test; all three now fail one. --- analysis/policy_conflicts.py | 18 +++++++++++++----- docs/policy-rules.md | 4 +++- tests/test_policy_contradictions.py | 23 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/analysis/policy_conflicts.py b/analysis/policy_conflicts.py index bfbe595..bc08f3c 100644 --- a/analysis/policy_conflicts.py +++ b/analysis/policy_conflicts.py @@ -342,11 +342,19 @@ def _compare(prohibitions: Sequence[Any], requirements: Sequence[Any], node, filter_name = forbid.get("node", ""), forbid.get("filter", "") where = f"on {node} filter {filter_name}" if node and filter_name else "" if scope is None: - undecided.append( - f"{name_p(forbid)} and {name_r(require)} name filters " - f"{forbid.get('filter')!r} and {require.get('filter')!r} on {node}, " - "which Batfish may resolve to the same ACL; could not be compared" - ) + # The traffic first (#440, N1). Two rules whose traffic is + # provably disjoint cannot collide whichever ACL Batfish + # resolves their filters to, so only a pair that might overlap + # is worth a warning about the spelling of its filter names. + pairs = [overlap(qa, qb)[0] + for qa in forbid.get("queries", []) or [] + for qb in require.get("queries", []) or []] + if any(verdict is not False for verdict in pairs): + undecided.append( + f"{name_p(forbid)} and {name_r(require)} name filters " + f"{forbid.get('filter')!r} and {require.get('filter')!r} on {node}, " + "which Batfish may resolve to the same ACL; could not be compared" + ) continue unknown_here: List[str] = [] found = False diff --git a/docs/policy-rules.md b/docs/policy-rules.md index 9ae8930..db6fa1a 100644 --- a/docs/policy-rules.md +++ b/docs/policy-rules.md @@ -413,7 +413,9 @@ same ACL in different spellings. When two filters match only after trimming and ignoring case, or either is a specifier rather than a plain name (`/`, `@`, `(`, `,`), the pair is reported as **undecidable**, not skipped. Found in review on #387: the first version compared the raw strings and called -`ACL_IN` versus `acl_in` "cannot collide". +`ACL_IN` versus `acl_in` "cannot collide". The traffic is compared first, +though (#440): two rules whose traffic is provably disjoint cannot collide +whichever ACL their filters resolve to, so they get no warning. A rule's `queries` are a union, because `policy_compliance` fails the rule if any one of them fails, so a contradiction is any query of the prohibition diff --git a/tests/test_policy_contradictions.py b/tests/test_policy_contradictions.py index 9a8c491..a757f12 100644 --- a/tests/test_policy_contradictions.py +++ b/tests/test_policy_contradictions.py @@ -252,7 +252,8 @@ def test_an_unrecognised_protocol_is_undecidable_not_disjoint(): assert len(loaded.undecidable) == 1 -@pytest.mark.parametrize("other", ["ACL_IN", "acl_in ", "/acl_in/", "@in(GigabitEthernet0/0)"]) +@pytest.mark.parametrize("other", ["ACL_IN", "acl_in ", "/acl_in/", "@in(GigabitEthernet0/0)", + "acl_in,acl_out"]) def test_a_filter_batfish_may_resolve_to_the_same_acl_is_not_skipped(other): """`ACL_IN` and `acl_in` were "different filter: cannot collide". Batfish resolves them to the same ACL, so the pair is reported, not skipped.""" @@ -262,6 +263,26 @@ def test_a_filter_batfish_may_resolve_to_the_same_acl_is_not_skipped(other): assert "could not be compared" in loaded.undecidable[0] +@pytest.mark.parametrize("other", ["ACL_IN", "/acl_in/", "acl_in,acl_out"]) +def test_disjoint_traffic_needs_no_warning_whatever_the_filter_is_called(other): + """#440, N1. The spelling warning fired before the traffic was compared, + so two rules about different hosts warned although they cannot collide + whichever ACL Batfish resolves the names to.""" + loaded = load_policy(policy( + rule(1, "prohibition", [{"srcIps": "10.10.10.0/24", "dstIps": "10.20.0.5"}]), + rule(2, "requirement", [{"srcIps": "10.10.10.0/24", "dstIps": "218.8.104.58"}], + filter_name=other))) + assert loaded.undecidable == [] + + +@pytest.mark.parametrize("number,name,other", [("6", "tcp", "udp"), ("17", "udp", "tcp"), + ("1", "icmp", "udp")]) +def test_each_protocol_number_means_its_own_protocol(number, name, other): + """#440, N2: changing "17" to tcp, or "1" to udp, passed every test.""" + assert policy_conflicts.overlap({"ipProtocols": [number]}, {"ipProtocols": [name]})[0] is True + assert policy_conflicts.overlap({"ipProtocols": [number]}, {"ipProtocols": [other]})[0] is False + + def test_one_disjoint_parsed_field_beats_an_unparsed_one(): verdict, _ = policy_conflicts.overlap( {"dstIps": "10.20.0.5", "ipProtocols": ["!tcp"]}, {"dstIps": "218.8.104.58"}) From 0888683a7f54668d408440d8ab5f059dbfae051a Mon Sep 17 00:00:00 2001 From: Shubham Date: Mon, 16 Nov 2026 13:38:28 +1300 Subject: [PATCH 2/2] Keep the spelling warning when a rule has no queries (#464 review) With either rule's queries empty there are no pairs, any([]) is False, and the 'could not be compared' warning main gives went away. Nothing proves such a pair disjoint, so it warns again. #468 refuses an empty list at load, but policy_conflicts can still be handed one, and this does not depend on the order the two PRs land in. --- analysis/policy_conflicts.py | 4 +++- tests/test_policy_contradictions.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/analysis/policy_conflicts.py b/analysis/policy_conflicts.py index bc08f3c..a55d582 100644 --- a/analysis/policy_conflicts.py +++ b/analysis/policy_conflicts.py @@ -349,7 +349,9 @@ def _compare(prohibitions: Sequence[Any], requirements: Sequence[Any], pairs = [overlap(qa, qb)[0] for qa in forbid.get("queries", []) or [] for qb in require.get("queries", []) or []] - if any(verdict is not False for verdict in pairs): + # No pairs at all means a rule with no queries: nothing proves the + # two disjoint, so it still warns (#464 review, @SamikaPerera). + if not pairs or any(verdict is not False for verdict in pairs): undecided.append( f"{name_p(forbid)} and {name_r(require)} name filters " f"{forbid.get('filter')!r} and {require.get('filter')!r} on {node}, " diff --git a/tests/test_policy_contradictions.py b/tests/test_policy_contradictions.py index a757f12..1f4a8cd 100644 --- a/tests/test_policy_contradictions.py +++ b/tests/test_policy_contradictions.py @@ -275,6 +275,19 @@ def test_disjoint_traffic_needs_no_warning_whatever_the_filter_is_called(other): assert loaded.undecidable == [] +@pytest.mark.parametrize("empty_side", ["prohibition", "requirement"]) +def test_a_rule_with_no_queries_still_warns_about_a_spelling_difference(empty_side): + """#464 review: with either rule's queries empty there are no pairs, and + any([]) is False, so the spelling warning silently went away. Nothing + proves such a pair disjoint, so it still warns. (#468 refuses an empty + queries list at load; policy_conflicts can still be handed one.)""" + from analysis.policy_conflicts import contradictions + forbid = rule(1, "prohibition", [] if empty_side == "prohibition" else [P]) + require = rule(2, "requirement", [] if empty_side == "requirement" else [R], filter_name="ACL_IN") + _, undecided = contradictions([forbid, require]) + assert len(undecided) == 1 and "may resolve to the same ACL" in undecided[0] + + @pytest.mark.parametrize("number,name,other", [("6", "tcp", "udp"), ("17", "udp", "tcp"), ("1", "icmp", "udp")]) def test_each_protocol_number_means_its_own_protocol(number, name, other):