Skip to content

Version 0.1.0 is a first version, for trial. See what is not proven →

bogolang
FR

← Examples

acl_test

Asks an access list whether flows would pass, with the line that decided; it never guesses.

Reads the saved configuration edge-rt1.cfg and asks its access list OUTSIDE-IN whether some flows would pass. Each answer comes with the line that decided it. The test never guesses: two flows meet a line it cannot judge for sure (established, and an ICMP kind), and the table says so and names that line. No device is contacted.

bogo run acl_test.bogo
bogo test acl_test.bogo

To use it on your own network:

  • Put a saved configuration of your router in the folder and its name in config_file.
  • Put the name or number of the access list in list_name.
  • Write your own flows in flows. A port of 0 means the flow has none, as for ICMP.
  • IOS and IOS-XE lists are read, standard and extended, IPv4 only.
acl_test.bogo
edition 2027
"""Would the access list on the internet uplink let these flows in? Reads a saved configuration."""


class AclTest:
    """Ask the list OUTSIDE-IN about a few flows and print each answer with the line
    that decided it. When the test cannot be sure, it says so and names the line."""

    config_file: str = "edge-rt1.cfg"
    list_name: str = "OUTSIDE-IN"
    # name, source, destination, protocol, destination port (0 when there is none)
    flows: list[tuple[str, IP, IP, str, int]] = [
        ("web", 203.0.113.50, 198.51.100.10, "tcp", 443),
        ("dns", 203.0.113.50, 198.51.100.20, "udp", 53),
        ("sip from a blocked range", 192.0.2.10, 198.51.100.30, "tcp", 5060),
        ("sip", 203.0.113.60, 198.51.100.30, "tcp", 5061),
        ("voice media", 203.0.113.60, 198.51.100.30, "udp", 16000),
        ("ntp", 203.0.113.60, 198.51.100.99, "udp", 123),
        ("ssh to the web server", 203.0.113.60, 198.51.100.10, "tcp", 22),
        ("ping the web server", 203.0.113.60, 198.51.100.10, "icmp", 0),
    ]

    @staticmethod
    def ask(rules: Acl, src: IP, dst: IP, protocol: str, port: int) -> tuple[str, str]:
        """The answer in one word, and the line that decided or the reason there is no answer."""
        try:
            v: Verdict = rules.permits(src, dst, port=port, protocol=protocol)
        except AclError as e:
            return "CANNOT SAY", str(e)
        return ("permitted" if v else "denied"), (
            f"line {v.number}: {v.line}" if v.number else v.line
        )

    @staticmethod
    def main(args: list[str]) -> None:
        cfg: Config = load_config(AclTest.config_file, platform="iosxe")
        rules: Acl = acl(cfg, AclTest.list_name)
        print(f"{rules.name} on {cfg.hostname}: {len(rules.lines)} lines")
        rows: list[dict[str, str]] = []
        for name, src, dst, protocol, port in AclTest.flows:
            answer, why = AclTest.ask(rules, src, dst, protocol, port)
            service: str = f"{protocol}/{port}" if port else protocol
            rows.append(
                {
                    "flow": name,
                    "from": str(src),
                    "to": str(dst),
                    "service": service,
                    "answer": answer,
                    "why": why,
                }
            )
        table(rows)


class TestAclTest:
    text: str = (
        "hostname t\n!\nip access-list extended T\n"
        " 10 permit tcp any host 192.0.2.10 eq 443\n"
        " 20 permit tcp any any established\n!\nend\n"
    )

    def rules(self) -> Acl:
        cfg: Config = Config(Device(name="t", ip=192.0.2.1, platform="iosxe"), self.text, "")
        return acl(cfg, "T")

    def test_a_permit_names_its_line(self) -> None:
        answer, why = AclTest.ask(self.rules(), 203.0.113.5, 192.0.2.10, "tcp", 443)
        assert answer == "permitted"
        assert why == "line 1: permit tcp any host 192.0.2.10 eq 443"

    def test_a_line_it_cannot_judge_is_never_guessed(self) -> None:
        answer, why = AclTest.ask(self.rules(), 203.0.113.5, 192.0.2.10, "tcp", 22)
        assert answer == "CANNOT SAY"
        assert "established" in why

    def test_other_traffic_meets_the_implicit_deny(self) -> None:
        answer, why = AclTest.ask(self.rules(), 203.0.113.5, 192.0.2.10, "udp", 53)
        assert (answer, why) == ("denied", "implicit deny")