policy
policy
¶
The unified +/- policy line format — one grammar for every tier.
A policy file is one entry per line::
+pypi.org # allow a domain
+*.pythonhosted.org # allow every subdomain
+192.168.1.50:8080 # allow one host:port
+localhost:8000 # reach a service on the host's localhost
-telemetry.vendor.com # deny
+api.anthropic.com %reason=harness-test %expires=2026-06-09
The leading + (allow) or - (deny) is mandatory. %key=value
markers carry optional metadata (reason, expires, from,
first/last, hits); # starts a free-text comment that the
parser ignores entirely. Blank and comment-only lines are skipped.
%expires is the one marker the shield enforces, on the t10 override tier:
a date (2026-06-09) keeps the entry valid through that day, until the
host's local midnight that ends it; a full ISO-8601 timestamp
(2026-06-09T12:00Z) until that instant
(expiry_deadline).
The reserved target localhost is a host-service grant: +localhost:PORT
opens the host machine's own localhost:PORT to the container (the loader
routes it to the backend host-loopback address, accepted above the deny tiers).
It requires an explicit port and the + action.
This module is the single parser for shipped, generated, and authored policy alike. Stdlib-only, so it is cheap to audit and safe to import from anywhere.
Action = Literal['+', '-']
module-attribute
¶
A policy verdict prefix: "+" (allow) or "-" (deny).
LOCALHOST = 'localhost'
module-attribute
¶
Reserved target: +localhost:PORT grants the container access to the host's localhost.
EXPIRES_MARKER = 'expires'
module-attribute
¶
The %expires= marker key: when a t10 override stops being valid.
REASON_MARKER = 'reason'
module-attribute
¶
The %reason= marker key: why a t10 override was granted, in the operator's words.
Recorded, never enforced — a break-glass carries its reason with it, so the
tier file says why each hole in the deny exists. Free text rides through
marker_value.
__all__ = ['EXPIRES_MARKER', 'LOCALHOST', 'REASON_MARKER', 'Action', 'PolicyEntry', 'domain_targets', 'entry_deadline', 'expiry_deadline', 'ip_targets', 'is_ip', 'localhost_ports', 'marker_value', 'parse_policy', 'render_policy', 'render_target', 'validate_target']
module-attribute
¶
PolicyEntry(action, target, port=None, meta=dict())
dataclass
¶
One parsed policy line.
Attributes:
| Name | Type | Description |
|---|---|---|
action |
Action
|
|
target |
str
|
a domain (optionally |
port |
int | None
|
the optional |
meta |
dict[str, str]
|
the |
parse_policy(text)
¶
Parse policy text into entries, failing fast on a malformed line.
Raises:
| Type | Description |
|---|---|
ValueError
|
on a missing |
Source code in src/terok_shield/policy.py
render_target(entry)
¶
Render an entry's target[:port] — the line minus its action and markers.
An IPv6 literal carrying a port is bracketed, else addr:port is
indistinguishable from a longer IPv6 address on re-parse.
Source code in src/terok_shield/policy.py
render_policy(entries)
¶
Render entries back to canonical text (round-trips parse_policy).
Source code in src/terok_shield/policy.py
localhost_ports(entries)
¶
Ports from the +localhost:PORT host-service grants, fed to the builder's loopback_ports.
Only admitting (+) entries grant a port — a programmatically built
-localhost entry (which parse_policy would reject) is never a grant.
Source code in src/terok_shield/policy.py
expiry_deadline(value)
¶
The instant an %expires value runs out, as a timezone-aware datetime.
A date — the form an operator writes, and the one an orchestrator passes
— keeps the entry valid through that day: it runs out at the host's
local midnight that ends the day, so expires set to today still holds
until tonight. A full ISO-8601 timestamp runs out at that instant; one
without an offset is read as host local time, like the date.
Raises:
| Type | Description |
|---|---|
ValueError
|
When value is neither an ISO date nor an ISO timestamp. |
Source code in src/terok_shield/policy.py
marker_value(text)
¶
text as one %key=value marker value — free text kept to a single token.
A marker ends at the first whitespace and a # starts a comment, so a
reason written the way an operator types it ("vault outage, direct
debug") would break the line it rides on. Percent-encoding keeps it
whole: whitespace, # and % itself become %XX, and
urllib.parse.unquote reads the text back unchanged.
Source code in src/terok_shield/policy.py
entry_deadline(entry)
¶
When entry stops being valid, or None when it carries no %expires marker.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the marker's value is not a date or a timestamp. |
Source code in src/terok_shield/policy.py
is_ip(target)
¶
True when target is an IP literal or CIDR (a domain or localhost is False).
ip_targets(entries)
¶
The IP/CIDR targets among entries (domains and localhost excluded), order-preserving.
domain_targets(entries)
¶
The domain targets among entries (IPs and localhost excluded), order-preserving.
validate_target(target)
¶
Accept a domain (optionally *.-prefixed), an IP literal, or a CIDR.
The target grammar every policy line obeys, on its own: no action prefix,
no :port, no %markers. Public so a caller that keeps host lists
of its own (an orchestrator's named egress sets, a promotion target)
validates them by the same rule the shield will apply, instead of a copy
of the pattern that drifts. A caller that wants a narrower grammar — no
wildcards, no CIDRs — layers its own check on top.
Raises:
| Type | Description |
|---|---|
ValueError
|
When target is neither a domain, an IP literal, nor a CIDR. |