run
run
¶
Subprocess execution boundary for all external commands.
Every shell-out in terok-shield flows through the :class:CommandRunner
protocol. Production code uses :class:SubprocessRunner; tests inject
fakes. This keeps external dependencies auditable and mockable in one
place.
CommandRunner
¶
Bases: Protocol
Protocol for executing external commands.
Decouples all subprocess calls behind a testable interface.
SubprocessRunner()
¶
Default CommandRunner implementation using subprocess.run.
Resolves the nft binary path at construction time and raises
NftNotFoundError immediately if nft is not installed.
Resolve the nft binary path, raising NftNotFoundError if missing.
Source code in src/terok_shield/core/run.py
run(cmd, *, check=True, stdin=None, timeout=None)
¶
Run a command, return stdout. Raise ExecError on failure when check=True.
Source code in src/terok_shield/core/run.py
has(name)
¶
Return True if an executable is on PATH (cached per name).
nft(*args, stdin=None, check=True)
¶
Run nft command directly (hook mode, inside container netns).
Source code in src/terok_shield/core/run.py
nft_via_nsenter(container, *args, pid=None, stdin=None, check=True)
¶
Run nft inside a running container's network namespace.
Source code in src/terok_shield/core/run.py
podman_inspect(container, fmt)
¶
dig_all(domain, *, timeout=10)
¶
Resolve domain to both IPv4 and IPv6 addresses in a single query.
Runs dig +short domain A domain AAAA and validates each line
with ipaddress. Returns empty list on lookup failure or
timeout.
Raises:
| Type | Description |
|---|---|
DigNotFoundError
|
If |
Source code in src/terok_shield/core/run.py
getent_hosts(domain)
¶
Resolve domain via getent hosts (fallback when dig is missing).
Returns validated IP addresses from NSS resolution. Typically
returns fewer results than dig (often a single address).
Source code in src/terok_shield/core/run.py
ExecError(cmd, rc, stderr)
¶
Bases: Exception
Raised when a subprocess fails.
Store command details and format the error message.
Source code in src/terok_shield/core/run.py
NftNotFoundError
¶
Bases: RuntimeError
Raised when the nft binary is not found on the host.
DigNotFoundError
¶
Bases: RuntimeError
Raised when the dig binary is not found on the host.
DNS resolution requires dig (from bind-utils / dnsutils).
ShieldNeedsSetup
¶
Bases: RuntimeError
Raised when global OCI hooks are not installed.
Per-container --hooks-dir does not persist across container
restarts, so global hooks are required. The message includes
system-specific setup hints.
find_nft()
¶
Locate the nft binary, checking PATH then common sbin directories.
sbin directories are checked explicitly because rootless users often lack them in PATH. Returns empty string if not found.