state
state
¶
Per-container state bundle layout contract.
Every shielded container gets an isolated state directory. This module
is the single source of truth for where files live within it — all
paths are derived from a single state_dir root through
StateBundle.
Bundle layout::
{state_dir}/
├── policy/ # v15 tiered +/- policy (one file per tier set)
│ ├── 10-override # → t10_override
│ ├── 20-security-deny # → t20_security_deny
│ ├── 30-provider-allow # → t30_provider_allow
│ ├── 40-project-allow # → t40_project_allow
│ └── live # runtime overlay (folded into its tiers)
├── resolved.ips # derived: resolved allow IPs (t40 set seed)
├── override_resolved.ips # derived: t10 override names → their resolved IPs
├── deny_resolved.ips # derived: t20 security-tier names → their resolved IPs (deny seed)
├── live_deny_resolved.ips # derived: policy/live denied names → their resolved IPs (deny seed)
├── ruleset.nft # pre-generated nft ruleset (gateways baked in)
├── override.expiry # derived: expiring t10 addresses + deadlines (the hook times them)
├── override.lock # serializes live t10 grants and revokes (flock; empty)
├── upstream.dns # upstream DNS address
├── dns.tier # active DNS tier
├── dnsmasq.command # symbolic/operator launch choice
├── dnsmasq.bin # live executable identity for cleanup
├── network.mode # rootless network mode (pasta/slirp4netns)
├── network.gateways # slirp4netns: the IPv4 and IPv6 gateways the container launched with
├── loopback.ports # per-container host-loopback TCP ports (newline-separated)
├── audit.enabled # creation-time audit flag; the NFLOG reader honours it
├── dnsmasq.conf # generated dnsmasq configuration
├── dnsmasq.pid # dnsmasq PID (in container netns)
├── dnsmasq.log # dnsmasq query log (for shield watch)
├── resolv.conf # bind-mounted over /etc/resolv.conf on every tier
├── container.id # podman container ID (short, 12-char hex)
└── audit.jsonl # per-container audit log
SHORT_ID_LEN = 12
module-attribute
¶
Length of the short container id — the prefix of podman's full id every surface keys on.
BUNDLE_VERSION = 20
module-attribute
¶
Integer version of the state bundle layout.
Bumped whenever the file layout changes in a backwards-incompatible way. The OCI hook hard-fails if the annotation version does not match — deliberately no compatibility window and no migration: containers prepared by a different generation fail fast at restart with a message naming the remedy (re-create the task; a running container keeps running untouched, and the task workspace rides its mounts). The hook and package must agree on this protocol.
Current shape (v20): network.gateways records the slirp4netns gateways
the container launched with — the host's own loopback, as the container
reaches it. Every rebuild, replayed ruleset and override check reads them
from there, never from containers.conf, whose cidr= may have changed
since. pasta needs no record: its host-loopback address is a constant.
v19: audit.enabled records the creation-time audit flag,
which the NFLOG reader honours — no audit lines when it is off. The reader
records the DOWN posture as a harvest source: the DOWN rulesets accept the
allow tiers (t10/t30/t40) logged ALLOWED ahead of the DOWN catch-all,
so a DOWN line means accepted only because the shield is down. The
runtime overlay's denied names resolve into live_deny_resolved.ips, apart
from the security tier's deny_resolved.ips; both deny caches keep one line
per name, as override_resolved.ips does, so a name that answers nothing at
a refresh keeps the addresses it had. The t10 override sets carry
the timeout flag, and an override's %expires becomes its elements'
kernel timeout: ruleset.nft seeds only the overrides that never expire,
override.expiry lists the rest with their deadlines for the hook to time
at apply, and override_resolved.ips keeps one line per override name so
each address takes the expiry of the name it came from. A live override's
tier line carries its %reason beside its %expires. Under slirp4netns
ruleset.nft refuses the gateway — the host's own loopback — in t00, above
t10, past the ports its preamble opens, and no t10 seed holds a host-loopback
or hard-deny address.
v18: symbolic dnsmasq launch choice is separate from live process identity. Global hooks are setup-owned, not per-container files.
v17: dnsmasq.bin records the dnsmasq binary the OCI hook launches.
v16: v15 plus two derived seed caches —
override_resolved.ips (t10 break-glass) and deny_resolved.ips
(t20 security-deny), so both tiers are repopulated by address on every
shield down/up rebuild; v15 replaced the six v14 split allow/deny
files with the tiered policy/ bundle of unified +/- files plus
the derived resolved.ips cache.) Earlier shapes are recoverable via
git log -L /^BUNDLE_VERSION/:src/terok_shield/state.py.
POLICY_DIR = 'policy'
module-attribute
¶
TIER_FILES = {'override': '10-override', 'security_deny': '20-security-deny', 'provider_allow': '30-provider-allow', 'project_allow': '40-project-allow'}
module-attribute
¶
LIVE_FILE = 'live'
module-attribute
¶
STABLE_LOCAL_NAMES = frozenset({LOCALHOST, *CONTAINER_HOST_ALIASES})
module-attribute
¶
Names that never rotate: the host's own localhost and the aliases the shield pins to it.
*.localhost names (RFC 6761) count too — they never leave the loopback.
STATE_DIR_MODE = 448
module-attribute
¶
Permission mode for state_dir and its subdirectories.
Owner-only. The OCI hook in _oci_state.py rejects state_dir if
st_mode & 0o022 (group- or world-writable), because a loose mode
would let any local peer drop a ruleset.nft for the hook to apply
with CAP_NET_ADMIN. mkdir(mode=…) is masked by umask, so
the writer side has to chmod after creation to guarantee the bit
pattern the validator demands.
EffectivePolicy(override, security_deny, provider_allow, project_allow, live)
dataclass
¶
Per-tier policy entries read from the bundle, in authority order.
live is the runtime overlay (shield allow/deny); the engine
folds its + entries into the project-allow set and its - entries
into the security-deny set.
override
instance-attribute
¶
security_deny
instance-attribute
¶
provider_allow
instance-attribute
¶
project_allow
instance-attribute
¶
live
instance-attribute
¶
all_entries()
¶
Every entry across tiers, top-to-bottom in authority order.
localhost_ports()
¶
allow_domains()
¶
deny_domains()
¶
dnsmasq_domains()
¶
Effective dnsmasq nftset list: admitted domains minus denied.
deny_ips()
¶
security_deny_targets()
¶
The security tier's own domains + literal IPs (localhost excluded).
The t20 set must hold addresses to survive a shield down
rebuild and to catch direct-IP access that never touches the DNS
plane, so the tier's domains are statically resolved, name by name,
into deny_resolved
(mirroring the t10 override treatment); its literal IPs reach the set
as they stand. The runtime overlay's denies
resolve into a cache of their own
(live_deny_targets):
tier authority has to tell a vault host's address from one the
operator denied, and one shared cache could not.
Source code in src/terok_shield/state.py
live_deny_targets()
¶
The domains the runtime overlay denies — resolved into the overlay's own deny cache.
Only names: a literal - IP in policy/live reaches the t20 set
as it stands. Their addresses land in
live_deny_resolved,
apart from the security tier's, so a later live allow can lift the
operator's own deny without ever lifting the security tier.
Source code in src/terok_shield/state.py
effective_ips()
¶
Admitted literal IPs minus denied (the non-resolved part of the set seed).
allow_targets()
¶
Admitted domains + literal IPs to resolve (localhost excluded) — the resolver input.
active_override(now=None)
¶
The t10 + entries still valid at now (epoch seconds; default: the current time).
An entry's %expires marker ends it — a date at the end of that day,
host local time; an entry without one never expires. Every consumer
of the override tier reads it through here, so an override that ran
out is no longer resolved or seeded, and is left out of every dnsmasq
config written after it ran out — even before the next authoring
rewrites the tier. A dnsmasq already running keeps the config it
loaded, though: the kernel drops the t10 element on time, while the
name stays punched through the sinkhole until the next shield
up/down reconciles the config, or a restart rewrites it.
Source code in src/terok_shield/state.py
with_override(entry)
¶
This policy with entry in its t10 tier, in place of any earlier entry for its target.
An earlier entry is for its target however it spells it — the same
network, or the same name in any case — so a grant never leaves its
host twice in the tier under two deadlines. The tier as a live grant
leaves it, held in memory: the grant resolves and seeds the t10 set
from it, and only once the kernel has taken that does the tier record
the entry
(upsert_override,
which replaces an entry the same way). Every other tier is this
policy's own.
Source code in src/terok_shield/state.py
override_targets(now=None)
¶
Break-glass override domains + literal IPs to resolve (localhost excluded).
The t10 override is a separate above-deny nft set — not part of the
ordinary allow tiers
(allow_targets),
so it is resolved and seeded independently. Only the overrides still
valid at now count (active_override).
Source code in src/terok_shield/state.py
wildcard_domains()
¶
Admitted *. entries; only a tier that resolves live can enforce them.
override_domains(now=None)
¶
Break-glass override domains — the DNS-plane punch-through set.
A t10 override host is usually also denied by t20 (that is the point of an override), so the dnsmasq sinkhole generator must treat these names as allowed — otherwise the override host would NXDOMAIN and the statically seeded t10 set would never see a connection. Only the overrides still valid at now punch through.
Source code in src/terok_shield/state.py
rotation_risk(tier, now=None)
¶
The names tier pins to resolved addresses, by kind; None when there are none.
Allowlisted names count on every tier but dnsmasq-live; denied
names on the tiers without dnsmasq, where nothing sinkholes them —
less any name a still-valid override opens, which the override line
speaks for; override names on every tier.
Source code in src/terok_shield/state.py
OverrideSeed(address, deadline=None)
dataclass
¶
One element of the t10 override set: an address, and when its override runs out.
Attributes:
| Name | Type | Description |
|---|---|---|
address |
str
|
An IP or CIDR, in nft's canonical text. |
deadline |
float | None
|
Epoch seconds at which the override ends — the kernel drops
the element then — or |
RotationRisk(tier, allow=(), deny=(), override=())
dataclass
¶
Policy names the container's DNS tier pins to the addresses they resolved to.
A name the shield resolves itself — once at launch, again at most hourly
on a posture change — is enforced by those addresses only, and the
provider behind it can move to another at any time. What that costs
depends on the name's tier and on the posture, so the value keeps the
three kinds apart and words them per posture
(lines).
Computed on read from the bundle — the recorded DNS tier and the
authored policy (recorded_rotation_risk)
— so it never disagrees with what the container runs. IP and CIDR
entries cannot rotate and stable local names
(STABLE_LOCAL_NAMES) never
do, so neither is ever listed.
Attributes:
| Name | Type | Description |
|---|---|---|
tier |
DnsTier
|
The DNS tier the container launched with. |
allow |
tuple[str, ...]
|
Allowlisted names (t30, t40, runtime |
deny |
tuple[str, ...]
|
Denied names (t20, runtime |
override |
tuple[str, ...]
|
Break-glass names (t10), resolved once on every tier — the live tier included, since dnsmasq never populates the override set. An address they rotate to is not reached. |
tier
instance-attribute
¶
allow = ()
class-attribute
instance-attribute
¶
deny = ()
class-attribute
instance-attribute
¶
override = ()
class-attribute
instance-attribute
¶
lines(posture=None)
¶
The warning lines that matter in posture, worded for it.
posture None means a launch or restart: the container boots UP
and may be lowered afterwards, so every line counts and the deny line
names both ways out. In UP every line counts; in DOWN only the deny
line — accept-by-default makes a pinned allow moot, while the security
tier is still meant to hold; DISENGAGED enforces nothing, so nothing
is worth a word.
Source code in src/terok_shield/state.py
SecurityTierRefusal(target, *, entry, address='')
¶
Bases: RuntimeError
A grant the t20 security tier refuses: only a break-glass override reaches the target.
The security tier (policy/20-security-deny) holds what the
orchestrator denies direct egress to — the vault-relayed provider hosts,
above all. A live allow may lift the operator's own runtime deny, never
this tier: the one way past it is the t10 override, authored with a
reason and an expiry and sitting above the deny. Raised before anything
changes, so a refused allow leaves the ruleset and the bundle as they were.
Attributes:
| Name | Type | Description |
|---|---|---|
target |
The domain or IP the caller asked to allow. |
|
entry |
The security-tier name, address or range that refuses it. |
|
address |
The address the target shares with the tier, when the refusal is by address; empty when the name itself is refused. |
Record what was refused and by which entry; the message names a shared address.
Source code in src/terok_shield/state.py
SecurityTier(names=(), addresses=())
dataclass
¶
The t20 security tier as one matcher — by name, by suffix, by address.
Read from a bundle by
StateBundle.read_security_tier.
The runtime overlay's - entries are left out on purpose: those are the
operator's own denies, which a live allow may lift. Nothing a live allow
does may lift this tier, so it is what a grant is checked against.
Attributes:
| Name | Type | Description |
|---|---|---|
names |
tuple[str, ...]
|
The tier's domains as authored. A name refuses itself and
every name under it — the reach of its dnsmasq sinkhole — and a
|
addresses |
tuple[str, ...]
|
The tier's literal IPs and CIDRs plus the addresses its names resolved to. Overlap is enough to refuse: a range target that would take in one security address is as refused as the address itself. |
names = ()
class-attribute
instance-attribute
¶
addresses = ()
class-attribute
instance-attribute
¶
refusal(target, addresses=())
¶
Why the tier refuses target, or any of the addresses it resolved to; else None.
The name goes first, so a security host is refused as itself even when it resolved to nothing just now. The addresses follow — an IP or CIDR target's own, then the resolved ones — so a name that merely shares an address with the tier, a CDN front say, is refused naming that address: allowing it would hand the agent the vault host's door too.
Source code in src/terok_shield/state.py
name_refusing(name)
¶
The tier's name that refuses name — that very name, or a domain above it.
Source code in src/terok_shield/state.py
address_refusing(address)
¶
The tier's address or range that address overlaps (canonical text), else None.
The narrowest overlapping entry wins: an exact security address says more than the range around it.
Source code in src/terok_shield/state.py
StateBundle(state_dir)
dataclass
¶
File-layout contract for a single shielded container's state_dir.
Frozen so the per-task instance is safe to pass through hook
callbacks without anyone smuggling a mutated state_dir into a
later stage. Every property is a pure derivation off state_dir;
the IO methods (read_effective,
read_effective_ips,
read_denied_ips,
ensure_dirs) bundle
the small handful of read-and-compose / setup helpers that previously
floated as free functions taking state_dir repeatedly.
state_dir
instance-attribute
¶
ruleset
property
¶
Path to the pre-generated nft ruleset file.
upstream_dns
property
¶
Path to the persisted upstream DNS address.
dns_tier
property
¶
Path to the persisted DNS tier value.
network_mode
property
¶
Path to the persisted rootless network mode (pasta/slirp4netns).
Detected once at pre_start and read back by HookMode.refresh,
which derives the ruleset's gateway addresses from it — a restart
rebuilds the bundle without paying for a podman info probe, and
cannot pick a mode the running container was not launched with.
network_gateways
property
¶
Path to the gateways a slirp4netns container launched with: "<IPv4> <IPv6>".
Recorded once at pre_start, from the containers.conf of that
moment. A running container keeps the gateway it started with, so
every later reader takes it from here — see
read_gateways.
loopback_ports
property
¶
Path to the per-container host-loopback TCP ports list.
Written by HookMode.pre_start from the caller-supplied
ShieldConfig.loopback_ports (the per-container triple of
gate / token-broker / ssh-signer ports the supervisor binds).
Read back by shield_up / shield_down when they rebuild
the nft ruleset — so a fresh Shield constructed without
the override still emits the correct
tcp dport <p> ip daddr 10.0.2.2 accept rules.
policy_dir
property
¶
Directory holding the per-tier +/- policy files.
policy_live
property
¶
Path to the runtime overlay (shield allow/deny append here).
resolved_cache
property
¶
Derived per-container cache of resolved allow IPs (the t40 set seed).
Separate from the authored policy/ tiers so resolution can be
reused across task starts and invalidated independently — keyed on
policy_mtime.
override_resolved
property
¶
Derived per-container cache of the t10 override names' addresses (part of the t10 seed).
The t10 override sits above the security-deny tier and is a separate
nft set, so it is resolved and cached apart from the allow tiers.
Statically resolved at pre_start — break-glass entries are rare and
specific, and dnsmasq interception would populate t40 (below the deny),
defeating the override. Keyed by name, one line each
(read_name_resolution):
every override carries its own expiry, and an address can only be
timed to the expiry of the override that resolved to it.
override_expiry
property
¶
Derived: the expiring t10 addresses and their deadlines, for the OCI hook to time.
Written beside ruleset.nft, which keeps only the overrides that
never expire. The hook turns each deadline into the seconds left when
it applies the ruleset, so a start that replays an old ruleset never
re-grants an override past its expiry.
override_lock
property
¶
The lock file override_change holds.
Empty; only its flock matters. Created on first use.
deny_resolved
property
¶
Derived per-container cache of the security tier's resolved IPs (part of the t20 seed).
Denied domains must reach the packet filter as addresses: the deny
set is what survives a shield down (the down posture keeps enforcing it),
and an address-level deny also catches direct-IP access that never
consults the DNS plane. Statically resolved at pre_start on every
DNS tier, cached apart from the allow-side resolved.ips so the
two invalidate independently — and apart from
live_deny_resolved,
so these addresses stay attributable to the security tier.
Keyed by name, one line each
(read_name_resolution): a
refresh in which one name answers nothing keeps that name's addresses
while the others take their new ones, so a single timed-out lookup
never drops a vault host out of the t20 set.
live_deny_resolved
property
¶
Derived cache of the addresses the runtime overlay's denied names resolve to.
The other half of the t20 seed. Kept apart from
deny_resolved so
each address keeps its provenance: the security tier's are what tier
authority checks a live allow against, these stem from the
operator's own runtime denies, which a live allow may lift. Keyed by
name like its sibling, with the same rule: a name that answers nothing
at a refresh keeps the addresses it had.
dnsmasq_conf
property
¶
Path to the generated dnsmasq configuration file.
dnsmasq_pid
property
¶
Path to the dnsmasq PID file (PID is in the container netns).
dnsmasq_command
property
¶
Path to the symbolic or explicitly configured dnsmasq launch choice.
dnsmasq_bin
property
¶
Path to the live dnsmasq executable identity, never a launch choice.
dnsmasq_log
property
¶
Path to the dnsmasq query log (consumed by shield watch).
resolv_conf
property
¶
Path to the resolv.conf bind-mounted over /etc/resolv.conf on every tier.
container_id
property
¶
Path to the persisted podman container ID file.
reader_pid
property
¶
Path where the bridge hook tracks the live NFLOG reader PID.
audit
property
¶
Path to the per-container audit log.
audit_flag
property
¶
Path to the creation-time audit flag (true / false).
Written once, at pre_start, from the config the container was
created with — the same value its audit_enabled annotation
carries. The NFLOG reader runs inside the container's netns and
reads no config, so this file is how it learns the operator's
choice; a harvest reads it back to tell "audit was off" apart from
"nothing to harvest".
meta_path
property
¶
Persisted-meta-path pointer file under state_dir.
Mirrors the resource-side META_PATH_FILE_NAME constant — one
filename on both sides of the hook boundary so package code that
reads it (Shield.up()/down()) and resource code that
writes it (the bridge createRuntime hook) can never drift
on path convention.
read_dns_tier()
¶
The tier pre_start recorded for this container, or None when there is none.
None means the container was never shielded, or the file is not a
tier name; a retired name reads as the tier it named.
Source code in src/terok_shield/state.py
read_rotation_risk()
¶
The RotationRisk of this bundle, computed on read.
None when the container was never shielded, when its policy
names nothing a rotation can hurt, and when the bundle cannot be read
— a warning must never be what breaks a launch or a status poll.
Source code in src/terok_shield/state.py
read_network_mode()
¶
The rootless network mode pre_start recorded, or "" when there is none.
A read that must never fail a caller: an absent or unreadable file
reads as no mode, the way read_dns_tier
treats its own.
Source code in src/terok_shield/state.py
record_gateways(gateway_v4, gateway_v6)
¶
Record the gateways the container launches with; pasta has none to record.
Source code in src/terok_shield/state.py
read_gateways()
¶
The (IPv4, IPv6) gateways the container launched with — ("", "") but under slirp4netns.
Only slirp4netns has gateways, so only a slirp4netns bundle reads the record. The ruleset opens the host-service ports at them and refuses the rest of the host's loopback there, above t10.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
A slirp4netns bundle without the record — prepared
before the record existed. Guessing the gateway from
|
Source code in src/terok_shield/state.py
host_loopback()
¶
The addresses that reach the host's own loopback from this container.
What host.containers.internal names under the recorded network
mode: pasta's constant map, or the gateways a slirp4netns container
launched with (host_loopback_addresses).
Source code in src/terok_shield/state.py
read_loopback_ports()
¶
Read persisted loopback ports; empty tuple when the file is absent.
Source code in src/terok_shield/state.py
read_effective_loopback_ports()
¶
Every host-loopback port the ruleset preamble opens, from both sources.
The supervisor's ports persisted in loopback.ports come first, then
the +localhost:PORT grants authored across the policy tiers —
deduplicated, order preserved. The ruleset builder reads this, not
the file alone, so a profile's host-service grant reaches the preamble.
Source code in src/terok_shield/state.py
tier_path(tier)
¶
Path to one tier's policy file (tier is a TIER_FILES key).
override_change()
¶
Serialize live grants, revokes, denies, and table rebuilds across processes.
A grant and a revoke each read the t10 tier, swap the running t10
set from it and record the change. Interleaved, one would swap from
a tier the other just changed — a revoked address back into the
kernel, which the tier no longer records, or one grant's element out
of it. So each holds this lock from its read of the tier through its
swap and record. A rebuild holds the same lock from its policy
snapshot through publication, and a live deny through its kernel
update and overlay record, so neither can overwrite the other with
an older policy. Names resolve before taking it, so a revoke never
waits on a resolver. The lock is an exclusive flock
on override_lock,
released when the block ends, however it ends.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
When there is no state directory to hold the lock: no bundle, so no t10 tier to change. |
Source code in src/terok_shield/state.py
policy_mtime()
¶
Newest mtime among the policy files (0.0 when none exist yet).
Feeds the resolver's content-aware freshness check: a resolved cache older than this means the authored allowlist changed since we resolved.
Source code in src/terok_shield/state.py
read_tier(path)
¶
write_tier(tier, content)
¶
Write a tier file only when content differs, and atomically.
Skipping no-op writes preserves the file's mtime, which the resolver's content-aware freshness keys on — so an unchanged allowlist stays a cache hit across task starts instead of forcing a re-resolution.
The new text goes to a temp file renamed over the tier, so a write that fails — a full disk — leaves the tier as it was. A live grant the tier cannot record can then say what the next rebuild seeds in its place: the earlier record, whole, never a torn line.
Source code in src/terok_shield/state.py
read_effective()
¶
Read and compose every tier into an EffectivePolicy.
Source code in src/terok_shield/state.py
overlay_set(action, target)
¶
Upsert {action}{target} into the runtime overlay (policy/live).
The target is validated through the parser (a malformed domain/IP
raises). Any prior entry for target is dropped first, so a later
shield allow flips an earlier deny (and vice-versa) rather
than stacking.
Source code in src/terok_shield/state.py
upsert_override(target, expires, *, reason)
¶
Record +target %expires=… %reason=… in the t10 tier, replacing any earlier entry for target.
The live break-glass path's record
(override_entry builds the
line): the tier is what every rebuild re-seeds the override set from,
so the grant survives a shield down/up. A grant writes it
once the kernel holds what it seeded, so the tier never records a
grant the kernel did not take. The tier as it stands is read afresh
for the write, and only target's entries change in it — every one
override_entries
finds for it, however spelled, gives way; a write that fails leaves it
as it was (write_tier).
The tier stays the orchestrator's to author, though — the next
refresh rewrites it from the orchestrator's own policy.
Source code in src/terok_shield/state.py
override_entries(target)
¶
Every t10 entry recorded for target, in force or not.
target matches the way the shield compares targets: an IP or CIDR by
the network it names, a name case-insensitively. The entries may come
from a live grant
(upsert_override)
or from the orchestrator's authored shield.override. The tier
does not tell the two apart, and a revoke ends either.
Source code in src/terok_shield/state.py
remove_override(target)
¶
Drop every t10 entry for target from the tier; return the entries dropped.
The revoke path's record, and the counterpart of
upsert_override.
Every rebuild re-seeds the override set from this tier, so once the
entry is gone no shield down/up re-opens it. The tier is
still the orchestrator's to author: the next refresh rewrites it
from the orchestrator's own policy, and an override authored there
comes back. Matching follows
override_entries.
When nothing matches, the file is not written.
Source code in src/terok_shield/state.py
read_container_id()
¶
The short container id the OCI hook recorded, or None when there is none.
The hook writes it at createRuntime, cut from the OCI state's full
id. None means the container has not started under the hook yet,
or the file holds something that is not a podman id.
Source code in src/terok_shield/state.py
read_audit_enabled()
¶
Whether audit was on when this container was created.
Only an explicit false says off. An absent or unreadable flag
reads as on: the shield's default, and how a bundle that never
recorded the flag behaved.
Source code in src/terok_shield/state.py
read_denied_ips()
¶
The tier-20 security-deny set seed: literal denied IPs + resolved denied domains.
Unions the current literal - IPs (security-deny tier + runtime
overlay) with both statically resolved caches —
deny_resolved and
live_deny_resolved.
This is what shield down/up repopulate the deny set from — a
denied domain must keep denying by address across every rebuild, or
the down posture would silently un-deny it.
Source code in src/terok_shield/state.py
read_live_denied_ips()
¶
The operator's own runtime denies, by address — the part of t20 a live allow may lift.
The literal - IPs and CIDRs of policy/live plus the addresses
its denied names resolved to. The security tier's addresses are not
among them, and nothing a live allow does takes them out of the set.
Source code in src/terok_shield/state.py
read_security_tier(extra_addresses=())
¶
The t20 security tier this bundle records, as a SecurityTier matcher.
Names and literal addresses come from policy/20-security-deny; the
resolved addresses from
deny_resolved, plus
extra_addresses — a fresher resolution of the same names, for a
caller on a tier where a vault host's address can rotate past the
launch-time cache. The runtime overlay stays out: its denies are the
operator's to lift.
Source code in src/terok_shield/state.py
read_effective_ips()
¶
The tier-40 project-allow set seed: resolved allow IPs minus denied.
Unions the derived resolved_cache
(literal allow IPs plus resolved allow-domains, refreshed at pre_start)
with the policy tiers' current literal allow IPs — so a runtime
shield allow of a raw IP survives a shield up rebuild even
before the next resolution — then subtracts the denied IPs.
Source code in src/terok_shield/state.py
read_override_ips(now=None)
¶
The tier-10 override set seed's addresses at now — see read_override_seeds.
Source code in src/terok_shield/state.py
read_override_seeds(now=None, *, policy=None, resolution=None)
¶
The tier-10 override set seed at now: every address a still-valid override may open.
Literal + override IPs come from the tier itself, the addresses of
override names from the name-keyed
override_resolved
cache. Each address carries the deadline of the override that named
it — the latest one when several did, however each spelled it, none
at all when any of them never expires. Overrides that already ran
out are left out
(active_override),
and so are addresses another seed covers, which an interval set would
refuse. Denies are not subtracted — the whole point of an override
is to sit above the security-deny tier.
What no override may open is left out too, whichever path refreshed
the cache: the host's own loopback under the recorded network mode,
and the hard-deny floor
(read_override_refusals
says which). A grant checks its own target's fresh answer, yet it
re-resolves the other names too, and so do a refresh and a rebuild
over a stale cache — a name rebound onto the slirp4netns gateway must
not ride into t10 on any of them.
policy stands in for the policy the bundle records, resolution
for its override cache: a live grant seeds the t10 tier as it will
leave it (with_override),
from the answers it resolved, before the tier or the cache records
anything. The network mode is the bundle's either way.
Source code in src/terok_shield/state.py
read_override_refusals(now=None)
¶
One line for each address a still-valid override names that no override may open.
The other half of
read_override_seeds:
the host's own loopback and the hard-deny floor sit above t10, so the
seed leaves them out, and these lines say so. A name rebound onto one
of them is worth an operator's attention — that is what a DNS
rebinding of a break-glass name looks like.
Source code in src/terok_shield/state.py
ensure_dirs()
¶
Create the state directory and its required subdirectories.
Both directories are forced to
STATE_DIR_MODE
(0o700) on every call — the OCI hook rejects anything
looser, and a prior run under a permissive umask (Fedora's
default 0o002 is a common offender) would otherwise leave
the bundle stranded.
Source code in src/terok_shield/state.py
read_name_resolution(cache)
¶
The addresses each name resolved to, from a name-keyed resolution cache.
One line per name: the name, then its addresses. None when the cache
is absent or written in the flat one-address-per-line form, which keeps
no names — a caller treats that as nothing resolved, never as addresses
it could pin to any name.
Source code in src/terok_shield/state.py
render_name_resolution(mapping)
¶
The cache text for mapping — the inverse of read_name_resolution.
A name that resolved to nothing is left out: it has no line to give.
Source code in src/terok_shield/state.py
write_name_resolution(cache, mapping)
¶
Write mapping to the name-keyed resolution cache, whole or not at all.
For answers resolved before they may be kept: a live override grant resolves its names, swaps the running t10 set, and only once the kernel holds the grant and the tier records it does the cache take the addresses it was seeded from. Written after the tier, the cache post-dates it, so the next rebuild trusts it for an hour like any fresh cache. A name with no address gets no line.
Source code in src/terok_shield/state.py
override_entry(target, expires, *, reason)
¶
The t10 entry a live break-glass grant records: +target %expires=… %reason=….
Built through the policy grammar, as every tier line is read, so a grant
fails on a malformed target or expiry before anything changes. The
reason rides along as one marker token
(marker_value keeps free text
whole), so the tier says why each hole in the deny exists, not only until
when.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the line is no policy line the grammar accepts. |
Source code in src/terok_shield/state.py
recorded_dns_tier(state_dir)
¶
The DNS tier a shielded container launched with, read from state_dir.
Thin public wrapper over
StateBundle.read_dns_tier
so callers that only want the tier need not know the bundle layout.
Source code in src/terok_shield/state.py
recorded_container_id(state_dir)
¶
The short id of the container shielded from state_dir, as its OCI hook recorded it.
The 12-character short id is the one key every per-container surface
shares: the NFLOG reader stamps it on each event, and the clearance hub
keys its socket and its mute by it. A caller that holds only the state
dir — a task's orchestrator — names the container exactly as those
surfaces do, instead of by a podman name they have never seen. Thin
public wrapper over
StateBundle.read_container_id,
like recorded_dns_tier;
None until the container's first start.
Source code in src/terok_shield/state.py
recorded_rotation_risk(state_dir)
¶
What the DNS tier of the container shielded from state_dir pins to resolved addresses.
A file read — the recorded tier and the authored policy — computed on
read, so an orchestrator shows the same warning the shield logs without
a Shield or a container to ask: at launch, in a task pane, after a
posture change. Thin public wrapper over
StateBundle.read_rotation_risk,
like recorded_dns_tier; None
when nothing is at risk or nothing was recorded.