Extending#
Fireman reads its own YAML and JSON out of the box. Anything else, such as a CSV export, a vendor’s XML or a CMDB API, comes in through a small piece of Python you write: a source. The idea comes from dlt: you yield raw items as they are, and describe how to reshape them. Fireman checks the result as strictly as it checks its own files.
A source plugs into the command line with fireman load --source MODULE.FUNC and
into the library with Pipeline().load(...).
The model#
- Source
A group of resources that belong together, usually one export or one system. Make one with the
@sourcedecorator.- Resource
A stream of raw items for one section (
addresses,fqdns,services,groupsorrules) in one scope: global, one firewall, or one policy of a firewall. Make one with the@resourcedecorator orcsv_file.- Schema
An optional pydantic model of your raw data, such as the columns of a CSV row. It runs first and reports bad rows with your own field names.
- Converter
A function added with
add_mapthat turns one item into the shape of Fireman’s input format (see Input format).add_mapreturns a new resource, so converters daisy-chain (.add_map(parse).add_map(rename)) and run in the order you add them.
Items then go through Fireman’s own models, exactly as if they came from a YAML file. Once every item is valid, the resources are merged by scope and the whole estate is cross-checked: names exist, kinds match, groups don’t loop.
your data → schema → converters → Fireman's models → merge by scope → Inventory cross-checks
Every bad item is reported, not just the first. A problem names the scope, the section, the item’s index and the field:
dc/inbound/rules[2]: port 'postgres' is not a number
If anything is wrong you get a LoadError holding every Problem, and nothing
is loaded.
classDiagram
direction LR
class Pipeline {
name: str
load(*sources) Inventory
}
class Source {
resources: Resource[]
}
class Resource {
name: section name
fn: Callable returning Iterable
schema: BaseModel subclass or None
maps: Callable[]
firewall: str or None
policy: str or None
scope: str
add_map(fn) Resource
extract() Extracted
}
class Extracted {
<<NamedTuple>>
items: index and item[]
problems: Problem[]
}
class Problem {
<<fireman.model>>
resource: str
index: int or None
loc: tuple
message: str
scope: str or None
}
class LoadError {
<<Exception>>
problems: Problem[]
}
Pipeline ..> Source : reads
Source o-- Resource
Resource ..> Extracted : extract()
Extracted o-- Problem
LoadError o-- Problem
Pipeline ..> LoadError : raises
Extracted.items keeps each surviving item with its original index, so a
problem found later, by Fireman’s models, still points at the right item.
What happens on load#
Pipeline.load extracts every resource, validates each item, and only then
builds and cross-checks the estate:
sequenceDiagram
actor You
participant P as Pipeline
participant R as Resource
participant Y as your schema and converters
participant M as Fireman's models
participant I as Inventory
You->>P: load(my_source(), yaml_file(...))
loop every resource, in source order
P->>R: extract()
loop every item fn() yields
R->>Y: schema, then each converter
Y-->>R: converted item, or a Problem
end
R-->>P: Extracted(items, problems)
P->>M: validate each item (AddressObject, Rule, ...)
M-->>P: model, or a Problem
end
alt any item problem
P-->>You: raise LoadError(problems)
else every item valid
P->>P: merge by scope
P->>I: Inventory.model_validate(estate)
alt cross-check fails
I-->>P: ValidationError
P-->>You: raise LoadError(inventory problems)
else
I-->>P: Inventory
P-->>You: Inventory
end
end
The cross-checks only run once every item is valid on its own. A rule that was skipped would otherwise show up as a confusing “unknown name”.
In this part#
- Resources
Writing resources: scopes, schemas, converters, which errors become problems, and
csv_file.- Sources
Grouping resources into a source, mixing it with YAML files, merge order, and using it from the command line and from Python.
- Example: CSV exports
A complete data-centre firewall read from four CSV exports.
- Example: an XML export
A complete site firewall read from an XML export with
ElementTree.