Skip to content

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

bogolang
FR

← Examples

audit_rules

Checks five house rules against saved configurations and prints the checks that failed.

Checks five house rules (SSH only on the vty lines, encrypted passwords, a log server, an NTP server, a description on every port in use) against the saved configurations of three devices. It prints a table of the checks that failed, with the test that was false. No device is contacted.

bogo run audit_rules.bogo
bogo test audit_rules.bogo

To use it on your own network:

  • List your devices in devices.csv (name, ip, platform, role).
  • Put a saved configuration of each one in configs, named <name>.cfg, such as your nightly backups.
  • Write your own rules. Each line under rule is one test that must be true.
  • Without configs="configs", audit() reads each device itself over SSH: it really connects, with the login stored by bogo login.
audit_rules.bogo
edition 2027
"""A few house rules, checked against saved configurations. No device is contacted."""


class AuditRules:
    """Each rule is a list of tests, one per line. A device fails a rule at the first
    test that is false. The configurations are read from the folder configs."""

    @staticmethod
    def ssh_only(vty: Section) -> bool:
        """True when this vty block lets in SSH and nothing else."""
        return vty.find("transport input") == ["transport input ssh"]

    @staticmethod
    def undescribed(c: Config) -> list[str]:
        """The interfaces that are not shut down and have no description."""
        return [str(i.name) for i in c.interfaces if not i.shutdown and i.description == ""]

    rule "vty takes ssh only" for d:
        config.section("line vty") != []
        all(AuditRules.ssh_only(v) for v in config.section("line vty"))

    rule "passwords are encrypted" for d:
        config.has("service password-encryption")

    rule "logs go to a server" for d:
        config.has("logging host")

    rule "clock from ntp" for d:
        config.has("ntp server")

    rule "ports in use are described" for d where d.role == "switch":
        AuditRules.undescribed(config) == []

    @staticmethod
    def main(args: list[str]) -> None:
        devices: list[Device] = inventory("devices.csv")
        result: AuditResult = audit(devices, configs="configs")
        if result.passed:
            print(f"All {len(result.rows)} checks passed.")
            return
        table(result.failed, ["device", "rule", "detail"])
        print()
        print(f"{len(result.failed)} of {len(result.rows)} checks failed.")
        for d in devices:
            c: Config = load_config(f"configs/{d.name}.cfg", platform=d.platform)
            if AuditRules.undescribed(c):
                print(f"{d.name}: no description on {', '.join(AuditRules.undescribed(c))}")


class TestAuditRules:
    def config(self, text: str) -> Config:
        return Config(Device(name="t", ip=192.0.2.1, platform="iosxe"), text, "")

    def test_telnet_on_a_vty_is_caught(self) -> None:
        c: Config = self.config(
            "line vty 0 4\n transport input ssh\nline vty 5 15\n transport input ssh telnet\n"
        )
        answers: list[bool] = [AuditRules.ssh_only(v) for v in c.section("line vty")]
        assert answers == [True, False]

    def test_a_vty_with_no_transport_line_is_not_ssh_only(self) -> None:
        c: Config = self.config("line vty 0 4\n exec-timeout 5 0\n")
        assert not AuditRules.ssh_only(c.section("line vty")[0])

    def test_shut_ports_need_no_description(self) -> None:
        c: Config = self.config(
            "interface Gi1/0/1\n description up\n!\ninterface Gi1/0/2\n shutdown\n!\n"
            "interface Gi1/0/3\n switchport access vlan 10\n!\n"
        )
        assert AuditRules.undescribed(c) == ["Gi1/0/3"]