Skip to content

_setup_manual

_setup_manual

Interactive per-component hardening setup — SELinux policy, AppArmor addendum.

One [Y/s/n] flow shared by every frontend: say what state the host is in, where the change lands, and exactly which sudo bash command would make it — then run that command, or print the rules it applies. terok setup selinux / terok-executor setup selinux / terok-sandbox setup selinux (and the apparmor twins) all route through handle_setup_component, and the aggregate setup's hints name the same verbs via setup_invocation.

The split matters for sudo: everything that needs the operator's context — the venv path of the bundled installer, the resolved sandbox-live root — is resolved and rendered before privilege escalation, and the shown command is the argv that runs, built from it. Installing is therefore always interactive (sudo asks for the password mid-flow), so there is deliberately no --yes: an unattended run copies the printed command and executes it directly instead.

What the s answer prints is the installer's own input — the rendered AppArmor block, the policy source file — so an operator reviews the change itself, and can diff it against the host afterwards.

SETUP_COMPONENTS = ('selinux', 'apparmor') module-attribute

__all__ = ['SETUP_COMPONENTS', 'handle_setup_component'] module-attribute

handle_setup_component(component, *, show_only=False, with_wayland=False, with_wayland_direct=False, cfg=None)

Run the interactive installer for one hardening component.

The whole setup <component> contract lives here, so every frontend is one routing line: a missing or unknown component name is answered from here too, spelled with the caller's own invocation.

Parameters:

Name Type Description Default
with_wayland bool

Include the separate proxy policy with the SELinux component. Does not remove that policy when omitted.

False
with_wayland_direct bool

Independently include the direct-access container policy. Does not enable the proxy policy or change existing tasks.

False
Source code in src/terok_sandbox/_setup_manual.py
def handle_setup_component(
    component: str | None,
    *,
    show_only: bool = False,
    with_wayland: bool = False,
    with_wayland_direct: bool = False,
    cfg: SandboxConfig | None = None,
) -> int:
    """Run the interactive installer for one hardening component.

    The whole ``setup <component>`` contract lives here, so every
    frontend is one routing line: a missing or unknown component name is
    answered from here too, spelled with the caller's own invocation.

    Args:
        with_wayland: Include the separate proxy policy with the SELinux
            component. Does not remove that policy when omitted.
        with_wayland_direct: Independently include the direct-access container
            policy. Does not enable the proxy policy or change existing tasks.
    """
    from .config import SandboxConfig
    from .operator_cli import setup_invocation

    for flag, selected in (
        ("--with-wayland", with_wayland),
        ("--with-wayland-direct", with_wayland_direct),
    ):
        if selected and component != "selinux":
            raise SystemExit(f"{flag} requires '{setup_invocation()} selinux'")
    with nullcontext() if show_only else setup_lock():
        if component == "selinux":
            comp = _selinux_component(cfg or SandboxConfig())
            if with_wayland:
                comp = _with_wayland_policy(comp)
            if with_wayland_direct:
                comp = _with_wayland_policy(comp, direct=True)
        elif component == "apparmor":
            comp = _apparmor_component()
        else:
            named = (
                f"unknown setup component {component!r}"
                if component
                else "--show needs a component"
            )
            raise SystemExit(f"{named}: {setup_invocation()} <{'|'.join(SETUP_COMPONENTS)}>")
        if not show_only:
            from .setup import check_setup

            require_no_downgrade(check_setup(cfg))
        return _run_component(comp, show_only=show_only)