Python API#
Everything the fireman command does is a library call you can make yourself:
load an estate, compile it, classify every policy, ask where a rule should go,
and render the answers. The pages in this part are walkthroughs of complete
scripts. Each one runs as is next to the sample files, and its output is shown
as it really prints.
Install#
Add Fireman to your project:
uv add fireman # or: pip install fireman
The scripts read the sample estate, estate.yaml, and some also read
requests.yaml and branch.json, a second file that adds a firewall branch.
Save them in the folder you run the scripts from:
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}
requests.yaml
# Requests against edge/inbound, for `fireman place` and `fireman covered`.
- name: partner-https
action: accept
sources: [203.0.113.0/24]
destinations: [web-servers]
services:
- {protocol: 6, ports: [443, 443]}
- name: admin-ssh
action: accept
sources: [lan]
destinations: [dmz-hosts]
services: [ssh]
- name: monitoring-ping
action: accept
sources: [10.1.20.0/24]
destinations: [dmz]
services:
- {protocol: 1, icmp_type: 8}
- name: block-scanners
action: deny
sources: [192.0.2.0/24]
destinations: [web-servers]
services: [web]
branch.json
{
"firewalls": [
{
"name": "branch",
"addresses": [
{"name": "branch-lan", "values": ["10.2.0.0/16"]}
],
"policies": [
{
"name": "outbound",
"rules": [
{
"order_id": 10,
"name": "allow-dns",
"action": "accept",
"sources": ["branch-lan"],
"destinations": ["any"],
"services": ["dns"]
},
{
"order_id": 20,
"name": "allow-web",
"action": "accept",
"sources": ["branch-lan"],
"destinations": ["any"],
"services": ["web"]
},
{
"order_id": 30,
"name": "block-test-net",
"action": "deny",
"sources": ["branch-lan"],
"destinations": ["192.0.2.0/24"],
"services": ["web"]
}
]
}
]
}
]
}
How the pieces fit#
Input comes in through sources, the pipeline validates it into an inventory, and the core compiles each policy into exact traffic sets. Everything after that reads the compiled policies.
flowchart LR
F["YAML or JSON files,<br>your own feeds"] --> L["fireman.load<br>Source, Resource"]
L --> P["Pipeline.load"]
P --> I["Inventory<br>validated estate"]
I --> C["compile_inventory"]
B["BDDManager"] --> C
C --> CI["CompiledInventory<br>one CompiledPolicy per policy"]
CI --> Q["classify, place, coverage<br>Finding, Placement, Coverage"]
Q --> R["fireman.report<br>JSON, YAML, rich tables"]
I --> U["Usage<br>who uses a name"]
U --> R
I --> PC["Pipeline.classify"]
PC <--> S["Store<br>findings cache"]
PC --> R
load never imports the core, and the core never renders anything: each layer
only knows the one below it. That is why you can feed your own data in (see
Extending) or take the findings out as plain pydantic models.
The main types#
classDiagram
direction TB
class Pipeline {
name: str
load(sources) Inventory
run(sources, bdd, implicit_deny) CompiledInventory
classify(inventory, make_bdd, implicit_deny, store, reuse) Classification
}
class Source {
resources: Resource[]
}
class Resource {
name: addresses, fqdns, services, groups or rules
firewall: str | None
policy: str | None
extract() Extracted
add_map(fn) Resource
}
class Inventory {
objects: Objects
firewalls: Firewall[]
}
class CompiledInventory {
bdd: BDDManager
catalogs: scope to Catalog
policies: (firewall, policy) to CompiledPolicy
rules: (firewall, policy) to Rule[]
request(firewall, sources, destinations, services) TrafficSet
}
class CompiledPolicy {
bdd: BDDManager
universe: TrafficSet | None
implicit_deny: bool
states: State[]
classify() Finding[]
place(rule) Placement
coverage(rule) Coverage
is_covered(rule) bool
}
class State {
accept: TrafficSet
deny: TrafficSet
effective: TrafficSet
residual: TrafficSet
rule: TrafficRule
id: RuleId
}
class TrafficRule {
action: accept | deny
traffic: TrafficSet
id: RuleId | None
}
class TrafficSet {
bdd: BDDManager
node: BDDFunction
is_empty: bool
rows: FormattedTraffic[]
union, intersection, difference()
project(axis) TrafficSet
widen(other) TrafficSet
}
class BDDManager {
nodes: int
cache: int
threads: int
gc() int
}
class Store {
root: Path
digest(inventory, firewall, implicit_deny) str
findings(digest) PolicyFindings[]
save(entries)
clear(name)
}
Source o-- Resource
Pipeline ..> Source : reads
Pipeline ..> Inventory : validates
Pipeline ..> CompiledInventory : run
Pipeline ..> Store : classify
CompiledInventory o-- CompiledPolicy
CompiledPolicy *-- State
State --> TrafficRule
TrafficRule --> TrafficSet
TrafficSet --> BDDManager
A TrafficSet belongs to the BDDManager it was built in, and sets from two
managers never mix (combining them raises ValueError). A CompiledInventory
builds every set in the one manager you give it.
What to import#
Import the data models from fireman.model and everything else from the
package root:
from fireman import BDDManager, Pipeline # and the rest below
from fireman import report # rendering
from fireman.model import Finding, Inventory, Placement, Rule # the data models
- Root
fireman Pipeline,BDDManager,compile_inventory,CompiledInventory,CompiledPolicy,State,TrafficRule,Store; the load API (yaml_file,json_file,file_source,csv_file,Source,source,Resource,resource,LoadError); the builderstraffic,tcp,udp,icmpand the constantsANY,TCP_ANY,UDP_ANY,ICMP_ANY; andFormattedTrafficfromfireman.bdd.fireman.modelPlain data: the input models (
Inventory,Firewall,Policy,Rule, the objects and groups,PlaceRequest), the results (Finding,Anomaly,RuleId,Placement,Insert,Widen,Decision,Coverage,PolicyFindings,Classification,PlacedRequest,CoveredRequest),Problem,UsageandNameUsage, andTrafficSet. The input and result models are frozen pydantic models, so they compare by value, hash, and dump to JSON.UsageandTrafficSetare frozen pydantic dataclasses;TrafficSetholds a BDD, so it does not dump.fireman.reportto_json,to_yaml, and the rich tablesto_table,to_usage_table,to_place_tableandto_covered_table.fireman.bddFormattedTraffic, the row type ofTrafficSet.rows,Decision.trafficand the options’left. You only need it for type annotations.
Pages#
- Loading an estate
Read files and sources into a validated
Inventorymodel, and handleLoadError.- Compiling and classifying
Size a
BDDManager, compile, classify, and map findings back to rules.- Classifying with the cache
Classify a whole estate one firewall at a time, with the findings cache.
- Placing and checking requests
Ask whether traffic is allowed and where a new rule would go.
- Traffic sets by hand
Build and combine traffic sets by hand, and walk a policy with no inventory.
- Usage and reports
Find what uses a name, and render results as JSON, YAML or tables.