diff --git a/analysis/policy_conflicts.py b/analysis/policy_conflicts.py index bfbe595..a55d582 100644 --- a/analysis/policy_conflicts.py +++ b/analysis/policy_conflicts.py @@ -342,11 +342,21 @@ 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 []] + # 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}, " + "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..1f4a8cd 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,39 @@ 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("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): + """#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"})