Usage and reports#
Two things that need no BDD work of their own: finding everything that uses a name, and rendering results the way the CLI does.
The full script is below; it reads estate.yaml from the current directory.
usage_reports.py
"""Find what uses a name, then render findings and usage as JSON, YAML and tables."""
from pathlib import Path
import click
from rich.console import Console
from fireman import BDDManager, Pipeline, file_source, report
from fireman.model import Anomaly, NameUsage, PolicyFindings, Usage
inventory = Pipeline().load(file_source(Path("estate.yaml")))
# Usage needs no BDD: it only follows names through groups and rules.
usage = Usage(inventory)
for scope in (None, "edge"):
found: NameUsage = usage.of(scope, "web-servers")
click.echo(f"web-servers seen from {scope or 'global'}: defined in {found.scope or 'global'}, kind {found.kind}")
for group in found.groups:
click.echo(f" group {group.name} ({group.scope or 'global'})" + (f" via {group.via}" if group.via else ""))
for rule in found.rules:
where = f"{rule.firewall}/{rule.policy}/{rule.name or rule.order_id}"
click.echo(f" rule {where}" + (f" via {rule.via}" if rule.via else ""))
for scope, name in ((None, "ntp"), ("core", "lan")):
try:
usage.of(scope, name)
except KeyError as exc:
click.echo(f"KeyError: {exc.args[0]}")
findings = Pipeline().classify(inventory, make_bdd=BDDManager).policies
# Reports are plain models, so they can be filtered or rebuilt before rendering.
shadowed = tuple(
PolicyFindings(
firewall=entry.firewall,
policy=entry.policy,
findings=tuple(f for f in entry.findings if f.anomaly is Anomaly.SHADOWED),
)
for entry in findings
if any(f.anomaly is Anomaly.SHADOWED for f in entry.findings)
)
click.echo("== JSON, shadowed rules only")
click.echo(report.to_json(shadowed))
click.echo("== YAML, uses of dmz")
click.echo(report.to_yaml(usage.of(None, "dmz")), nl=False)
console = Console()
console.print(report.to_table(findings))
console.print(report.to_usage_table(usage.of("edge", "web-servers")))
estate.yaml
# A small estate: one firewall, `edge`, between the internet, a DMZ and the LAN.
# The docs use it throughout. `inbound` is seeded with anomalies; `outbound` is
# clean.
addresses:
- name: lan
values: [10.1.0.0/16]
- name: dmz
values: [192.168.10.0/24]
- name: web-servers
values: [192.168.10.10, 192.168.10.11, 192.168.10.12]
fqdns:
- name: update-server
fqdn: updates.example.com
addresses: [203.0.113.10]
resolved_at: 2026-11-20T08:00:00Z
- name: partner-api
fqdn: api.partner.example
addresses: [203.0.113.50]
resolved_at: 2026-11-20T08:00:00Z
services:
- name: web
entries:
- {protocol: 6, ports: [80, 80]}
- {protocol: 6, ports: [443, 443]}
- name: ssh
entries:
- {protocol: 6, ports: [22, 22]}
- name: dns
entries:
- {protocol: 17, ports: [53, 53]}
- {protocol: 6, ports: [53, 53]}
groups:
# Every DMZ host except the web servers. A global group sees the global
# `web-servers`, even inside `edge`, which has its own.
- name: dmz-hosts
kind: address
include: [dmz]
exclude: [web-servers]
firewalls:
- name: edge
addresses:
# edge publishes only two of the three web servers.
- name: web-servers
values: [192.168.10.10, 192.168.10.11]
policies:
- name: inbound
rules:
- order_id: 10
name: block-abusers
action: deny
sources: [198.51.100.0/24]
destinations: [web-servers]
services: [web]
- order_id: 20
name: allow-web
action: accept
sources: [any]
destinations: [web-servers]
services: [web]
- order_id: 30
name: allow-partner-https
action: accept
sources: [203.0.113.0/24]
destinations: [web-servers]
services:
- {protocol: 6, ports: [443, 443]}
- order_id: 40
name: block-scanners
action: deny
sources: [192.0.2.0/24]
destinations: [web-servers]
services: [web]
- order_id: 50
name: allow-partner-api
action: accept
sources: [partner-api]
destinations: [web-servers]
services: [web]
- order_id: 60
name: allow-admin-ssh
action: accept
sources: [lan]
destinations: [dmz-hosts]
services: [ssh]
- name: outbound
rules:
- order_id: 10
name: allow-dns
action: accept
sources: [lan]
destinations: [any]
services: [dns]
- order_id: 20
name: allow-updates
action: accept
sources: [dmz-hosts]
destinations: [update-server]
services: [web]
- order_id: 30
name: allow-web-out
action: accept
sources: [lan]
destinations: [lan, dmz]
destination_negate: true
services: [web]
- order_id: 40
name: allow-ping
action: accept
sources: [lan]
destinations: [any]
services:
- {protocol: 1, icmp_type: 8}
Who uses a name#
Usage(inventory) indexes a validated inventory once; nothing is compiled,
so it is quick on any estate. usage.of(scope, name) follows the name through
groups (includes and excludes, at any depth) to every rule that ends up using
it, and returns a NameUsage model:
- Scope
The scope that defines the name: the firewall if it has its own object of that name, else
Nonefor global. Lookup works as in rules (see Concepts):of(None, name)looks globally,of("edge", name)looks fromedge.- Kind
addressorservice. FQDNs and address groups are addresses.- Groups
Each group using the name, as
GroupUse(scope, name, via).- Rules
Each rule using the name, as
RuleUse(firewall, policy, order_id, name, via).
via is the nearest group the use goes through, or None for a direct use.
Literals and service entries in rules are not names, so they never show up.
An unknown firewall or name raises KeyError.
inventory = Pipeline().load(file_source(Path("estate.yaml")))
# Usage needs no BDD: it only follows names through groups and rules.
usage = Usage(inventory)
for scope in (None, "edge"):
found: NameUsage = usage.of(scope, "web-servers")
click.echo(f"web-servers seen from {scope or 'global'}: defined in {found.scope or 'global'}, kind {found.kind}")
for group in found.groups:
click.echo(f" group {group.name} ({group.scope or 'global'})" + (f" via {group.via}" if group.via else ""))
for rule in found.rules:
where = f"{rule.firewall}/{rule.policy}/{rule.name or rule.order_id}"
click.echo(f" rule {where}" + (f" via {rule.via}" if rule.via else ""))
for scope, name in ((None, "ntp"), ("core", "lan")):
try:
usage.of(scope, name)
except KeyError as exc:
click.echo(f"KeyError: {exc.args[0]}")
edge has its own web-servers, so its rules use that object and not the
global one. The global one is still used, through the global group
dmz-hosts.
Rendering#
fireman.report renders what the commands print. Every result is a pydantic
model, so the JSON and YAML carry all of it; the tables are
rich Table objects to print with a Console,
or to put in your own layout. report.label(rule_id) is the one-line name the
tables use for a rule: order_id 20, or web-in (order_id 20) when it has a name.
to_json(report),to_yaml(report)Any report model: a tuple of
PolicyFindings, aNameUsage, or a tuple ofPlacedRequestorCoveredRequest. Fields keep model order.to_table(findings)One row per finding.
to_usage_table(usage)Groups first, then rules.
to_place_table(placed),to_covered_table(covered)
Reports are plain data, so you can filter or rebuild them before rendering. The script keeps only the shadowed rules for the JSON:
findings = Pipeline().classify(inventory, make_bdd=BDDManager).policies
# Reports are plain models, so they can be filtered or rebuilt before rendering.
shadowed = tuple(
PolicyFindings(
firewall=entry.firewall,
policy=entry.policy,
findings=tuple(f for f in entry.findings if f.anomaly is Anomaly.SHADOWED),
)
for entry in findings
if any(f.anomaly is Anomaly.SHADOWED for f in entry.findings)
)
click.echo("== JSON, shadowed rules only")
click.echo(report.to_json(shadowed))
click.echo("== YAML, uses of dmz")
click.echo(report.to_yaml(usage.of(None, "dmz")), nl=False)
console = Console()
console.print(report.to_table(findings))
console.print(report.to_usage_table(usage.of("edge", "web-servers")))
The width of a table follows the terminal, as with the CLI; pass
Console(width=...) for a fixed width.
Output#
web-servers seen from global: defined in global, kind address
group dmz-hosts (global)
rule edge/inbound/allow-admin-ssh via dmz-hosts
rule edge/outbound/allow-updates via dmz-hosts
web-servers seen from edge: defined in edge, kind address
rule edge/inbound/block-abusers
rule edge/inbound/allow-web
rule edge/inbound/allow-partner-https
rule edge/inbound/block-scanners
rule edge/inbound/allow-partner-api
KeyError: unknown name 'ntp'
KeyError: unknown firewall 'core'
== JSON, shadowed rules only
[
{
"firewall": "edge",
"policy": "inbound",
"findings": [
{
"rule": {
"order_id": 40,
"name": "block-scanners"
},
"anomaly": "shadowed",
"related": [
{
"order_id": 20,
"name": "allow-web"
}
]
}
],
"cached": false
}
]
== YAML, uses of dmz
scope: null
name: dmz
kind: address
groups:
- scope: null
name: dmz-hosts
via: null
rules:
- firewall: edge
policy: inbound
order_id: 60
name: allow-admin-ssh
via: dmz-hosts
- firewall: edge
policy: outbound
order_id: 20
name: allow-updates
via: dmz-hosts
- firewall: edge
policy: outbound
order_id: 30
name: allow-web-out
via: null
Findings
┏━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Firewall ┃ Policy ┃ Rule ┃ Anomaly ┃ Related ┃
┡━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ edge │ inbound │ allow-web (order_id 20) │ generalization │ block-abusers (order_id 10) │
│ edge │ inbound │ allow-web (order_id 20) │ correlated │ block-abusers (order_id 10) │
│ edge │ inbound │ allow-partner-https (order_id 30) │ redundant │ allow-web (order_id 20) │
│ edge │ inbound │ block-scanners (order_id 40) │ shadowed │ allow-web (order_id 20) │
│ edge │ inbound │ allow-partner-api (order_id 50) │ redundant │ allow-web (order_id 20) │
└──────────┴─────────┴───────────────────────────────────┴────────────────┴─────────────────────────────┘
Uses of web-servers (address, edge)
┏━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━┓
┃ Kind ┃ Where ┃ Name ┃ Via ┃
┡━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━┩
│ rule │ edge/inbound │ block-abusers (order_id 10) │ │
│ rule │ edge/inbound │ allow-web (order_id 20) │ │
│ rule │ edge/inbound │ allow-partner-https (order_id 30) │ │
│ rule │ edge/inbound │ block-scanners (order_id 40) │ │
│ rule │ edge/inbound │ allow-partner-api (order_id 50) │ │
└──────┴──────────────┴───────────────────────────────────┴─────┘
In the YAML, scope: null means global, and allow-web-out uses dmz
directly: it is in its negated destinations.