mode_hook
mode_hook
¶
Hook mode: OCI hooks + per-container netns.
Uses OCI hooks to apply per-container nftables rules inside each
container's network namespace. No root required — only podman and nft.
The stdlib-only hook_entrypoint.py applies the pre-generated ruleset
at container creation; this module handles DNS pre-resolution, live
allow/deny, and shield lifecycle (up/down/state).
HookMode(*, config, runner, audit, dns, profiles, ruleset)
¶
Hook-mode shield backend (Strategy, implements ShieldModeBackend).
Manages the full lifecycle of OCI-hook-based container firewalling: pre-start DNS resolution and hook installation, live allow/deny, bypass (down/up), and ruleset preview.
Create a hook mode backend with all collaborators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ShieldConfig
|
Shield configuration (provides state_dir). |
required |
runner
|
CommandRunner
|
Command runner for subprocess calls. |
required |
audit
|
AuditLogger
|
Audit logger for event logging. |
required |
dns
|
DnsResolver
|
DNS resolver for domain resolution and caching. |
required |
profiles
|
ProfileLoader
|
Profile loader for allowlist profiles. |
required |
ruleset
|
RulesetBuilder
|
Ruleset builder for nft generation and verification. |
required |
Source code in src/terok_shield/core/mode_hook.py
pre_start(container, profiles)
¶
Prepare for container start in hook mode.
Installs hooks, composes profiles, resolves DNS, writes allowlist, detects DNS tier, sets annotations, and returns the podman CLI arguments needed for shield protection.
Raises:
| Type | Description |
|---|---|
ShieldNeedsSetup
|
When global hooks are not installed
(see |
Source code in src/terok_shield/core/mode_hook.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
allow_domain(domain)
¶
Add a domain to the dnsmasq config and signal reload.
Delegates to dnsmasq.add_domain(), which persists the domain to
live.domains (not profile.domains) and removes any matching
entry from denied.domains. When dnsmasq is running, a SIGHUP is
sent so the change takes effect immediately without a container restart.
These entries are runtime additions: they survive dnsmasq reloads but
are separate from the pre-start profile.domains list.
The IP-level allow (nft set update) is handled separately by
allow_ip() — this method is the domain-tracking counterpart
that ensures future IP rotations are also captured.
No-op when the container is not using the dnsmasq DNS tier (the
static IP-level allow already happened via allow_ip()).
Source code in src/terok_shield/core/mode_hook.py
deny_domain(domain)
¶
Remove a domain from the dnsmasq config and signal reload.
Counterpart of allow_domain(). Removes the domain so dnsmasq
stops auto-populating nft sets for it on future DNS queries.
No-op when the container is not using the dnsmasq DNS tier.
Source code in src/terok_shield/core/mode_hook.py
allow_ip(container, ip)
¶
Live-allow an IP for a running container via nsenter.
Source code in src/terok_shield/core/mode_hook.py
deny_ip(container, ip)
¶
Live-deny an IP for a running container via nsenter.
Removes from the nft allow set (best-effort) and from live.allowed. Adds to the nft deny set. Persists to deny.list when the IP would otherwise be re-allowed (profile IP or interactive mode).
Source code in src/terok_shield/core/mode_hook.py
shield_down(container, *, allow_all=False)
¶
Switch a running container to bypass mode.
Source code in src/terok_shield/core/mode_hook.py
shield_up(container)
¶
Restore normal deny-all mode for a running container.
Source code in src/terok_shield/core/mode_hook.py
shield_state(container)
¶
Query the live nft ruleset to determine the container's shield state.
Source code in src/terok_shield/core/mode_hook.py
list_rules(container)
¶
List current nft rules for a running container.
Source code in src/terok_shield/core/mode_hook.py
preview(*, down=False, allow_all=False)
¶
Generate the ruleset that would be applied to a container.