Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ venv
node_modules
docs/
scripts/
# The UI image carries the host helpers it pins (app/layer_scripts.py).
!scripts/cashpilot-gvisor
*.md
!requirements.txt
.DS_Store
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ COPY --from=builder /build/.venv ./.venv

COPY app/ ./app/
COPY services/ ./services/
# The host helpers the Fleet page offers as one pinned command (app/layer_scripts.py).
COPY scripts/cashpilot-gvisor ./scripts/
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

Expand Down
4 changes: 4 additions & 0 deletions app/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ def _validate_network_shape(docker: dict[str, Any], name: str) -> list[str]:
runtime = docker.get("runtime")
if runtime is not None and (not isinstance(runtime, str) or not runtime.strip()):
errors.append(f"{name}: docker.runtime must be a runtime name")
reason = docker.get("runtime_reason")
if reason is not None and (runtime is None or not isinstance(reason, str) or not reason.strip()):
errors.append(f"{name}: docker.runtime_reason must be a sentence, next to docker.runtime")
caps = {str(c).upper().removeprefix("CAP_") for c in docker.get("cap_add") or []}
if held := sorted(caps & NETNS_CAPABILITIES):
# The threat class this rule closes: a container with these in the host's
Expand Down Expand Up @@ -1039,6 +1042,7 @@ def vps_allowed(requirements: dict[str, Any] | None) -> bool | None:
"docker.env[key].label",
"docker.env[key].description",
"docker.setup",
"docker.runtime_reason",
"docker.critical_volumes[].holds",
"docker.health_signals[].means",
"docker.health_probe.checks[].means",
Expand Down
73 changes: 73 additions & 0 deletions app/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
"api.runs_several_copies_box_service": "{name} runs several copies on a box, and a service with a data path runs one per box. Scale it to one copy on every box first, then add the data path.",
"api.runs_through_another_proxy_worker": "{name} runs through another proxy on this worker. Remove it, then deploy it again through this one.",
"api.runs_through_proxy_running_s": "{name} runs through a proxy on {box}. Running it from {box}'s own IP{value} shows that address to {name} and to everyone its traffic reaches. Confirm with your password.",
"api.runtime_choice_cannot": "This earner is set to run under gVisor on {worker}, and it cannot: {why} Set it back to Standard on the Fleet page, or fix that first. Nothing was deployed.",
"api.runtime_choice_routed": "This earner is set to run under gVisor on {worker}, and a copy routed through a proxy runs under Standard only. Set it back to Standard on the Fleet page to route it. Nothing was deployed.",
"api.runtime_choice_worker_too_old": "This earner is set to run under {runtime} on {worker}, and the worker is too old to take a runtime choice. Update the worker, or set the earner back to Standard on the Fleet page. Nothing was deployed.",
"api.saving_failed": "Saving {name} failed.",
"api.send_proxy_json_object": "Send the proxy as a JSON object",
"api.service_has_no_docker_image": "Service ''{slug}'' has no Docker image",
Expand Down Expand Up @@ -1929,6 +1932,76 @@
"lang.auto": "Automatic (browser language)",
"lang.label": "Language",
"lang.not_saved": "Language not saved: {error}",
"layers.badge.tip": "The layers this copy runs behind: the LAN fence, and Standard (runc) or gVisor. The Fleet page changes them.",
"layers.check.button": "Check gVisor",
"layers.check.green": "Green.",
"layers.check.not_installed": "Not installed.",
"layers.check.red": "Red.",
"layers.check.running": "Checking…",
"layers.check.tip": "Start a short probe container under gVisor on the earners' network. It must see gVisor's kernel and resolve a name.",
"layers.cmd.dev_build": "This CashPilot UI is a development build, so it cannot pin the command to a release. Run a released image to get it.",
"layers.cmd.not_shipped": "This CashPilot UI image does not carry {name}, so it cannot check the download.",
"layers.cmd.unknown": "{name} is not a CashPilot helper.",
"layers.copied": "Copied",
"layers.copy": "Copy",
"layers.copy_fallback": "This page is not on https, so the clipboard is blocked. The command is selected: press Ctrl+C or Cmd+C.",
"layers.default.docker": "Docker's default",
"layers.default.label": "Runtime for earners you deploy here from now on",
"layers.default.note": "Earners already here keep their runtime.",
"layers.default.saved": "Saved. It applies to earners you deploy here from now on.",
"layers.default.saving": "Saving…",
"layers.docs": "What each layer guards",
"layers.earner.confirm": "Run {service} under {runtime}?\n\nThis redeploys {service} now. It stops and starts again, usually in under a minute. Its data volume is kept.",
"layers.earner.fence_unknown": "fence unknown",
"layers.earner.fenced": "fenced",
"layers.earner.locked": "Locked",
"layers.earner.not_fenced": "not fenced",
"layers.earner.pending": "{runtime} on its next deploy",
"layers.earner.redeployed": "{service} was redeployed under {runtime}.",
"layers.earner.retry": "Run under {runtime} again",
"layers.earner.saved": "{service} runs under {runtime} from its next deploy.",
"layers.earner.saving": "Saving the runtime of {service}…",
"layers.earner.to_gvisor": "Run under gVisor",
"layers.earner.to_runc": "Run under Standard",
"layers.earner.vm_failed": "Secure VM fence failed",
"layers.earner.vm_ok": "in the Secure VM",
"layers.earner.vm_unknown": "Secure VM fence unknown",
"layers.fence": "LAN fence",
"layers.gvisor.install": "Install gVisor on this host",
"layers.gvisor.install_intro": "gVisor puts its own kernel between each earner and this host's kernel. It costs about a fifth of the throughput and six to nine times the CPU. Run this command on the host. It asks before it changes anything, and running containers keep running.",
"layers.gvisor.installed": "installed",
"layers.gvisor.missing": "not installed",
"layers.gvisor.undo": "Remove gVisor from this host",
"layers.gvisor.undo_note": "Switch every earner on this worker back to Standard first. The command refuses while one still runs under gVisor.",
"layers.gvisor.unsupported": "not possible here",
"layers.off": "off",
"layers.on": "on",
"layers.proxy": "Socket proxy",
"layers.proxy.off_tip": "This worker holds the raw Docker socket, which is root on the host. The shipped compose files put a socket proxy in front of it.",
"layers.runtime.gvisor": "gVisor",
"layers.runtime.runc": "Standard (runc)",
"layers.title": "Security layers",
"layers.unknown": "unknown",
"layers.vm": "Secure VM",
"layers.vm.fence_failed": "fence failed",
"layers.vm.fence_ok": "fence ok",
"layers.vm.fence_stale": "fence not checked lately",
"layers.vm.fence_unknown": "fence unknown",
"layers.vm.inside": "This worker runs inside a Secure VM on {host}.",
"layers.vm.kvm": "KVM available",
"layers.vm.no_kvm": "no KVM",
"layers.vm.tip": "A VM layer for earners is planned. It needs KVM on the host.",
"layers.why.catalog": "{service} must run under {runtime}: its catalog entry says so.",
"layers.why.catalog_reason": "{service} must run under {runtime}: {reason}",
"layers.why.install_first": "Install gVisor on this host first.",
"layers.why.missing": "gVisor is not installed on this host.",
"layers.why.net_raw_missing": "{service} needs raw sockets, and this worker has no gVisor runtime registered with --net-raw. The install command registers one.",
"layers.why.no_docker": "The worker could not ask Docker which runtimes it has.",
"layers.why.not_linux": "gVisor runs on Linux hosts only.",
"layers.why.old_worker": "This worker is older than security layers. Update it to choose a runtime here.",
"layers.why.pin_default": "CASHPILOT_CONTAINER_RUNTIME on this worker is {runtime}. The variable wins over this page.",
"layers.why.pin_override": "CASHPILOT_CONTAINER_RUNTIME_OVERRIDES on this worker sets {slug}={runtime}. The variable wins over this page.",
"layers.why.unraid": "Unraid cannot run gVisor: it boots from a RAM filesystem that gVisor cannot leave. Run the earners in a VM instead.",
"logs.failed": "Error: {error}",
"logs.load_failed": "Error loading logs: {error}",
"logs.loading": "Loading logs...",
Expand Down
68 changes: 68 additions & 0 deletions app/layer_scripts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""The one copyable command that installs, checks or removes a security layer on a host.

A layer such as gVisor needs root on the host, which the worker does not have
and must not get: the socket proxy refuses privileged containers by design. So
the UI shows a command for the owner to run. The command downloads the helper
from this repository at the tag of the release the UI runs, checks it against
the sha256 of the UI image's own copy, and only then runs it with sudo. A file
that changed on the way, or a tag that does not hold it, stops the command
before anything runs as root.

The helper's own arguments are shell-quoted. A secret never goes in them: a
helper that needs one asks for it, so it stays out of shell history and ps.
"""

from __future__ import annotations

import functools
import hashlib
import re
import shlex
from pathlib import Path

from app import i18n, version

REPO_RAW = "https://raw.githubusercontent.com/GeiserX/CashPilot"
SCRIPTS_DIR = Path(__file__).resolve().parent.parent / "scripts"

#: Each helper the UI offers, and the interpreter it runs under.
SCRIPTS: dict[str, str] = {"cashpilot-gvisor": "bash"}

_RELEASE = re.compile(r"\d+\.\d+\.\d+")


class Unavailable(Exception):
"""No pinned command can be shown; the message says why."""


@functools.cache
def sha256(name: str) -> str:
return hashlib.sha256((SCRIPTS_DIR / name).read_bytes()).hexdigest()


def problem(name: str) -> str | None:
"""Why no pinned command can be shown for ``name``, or None when one can."""
if name not in SCRIPTS:
return i18n.t("layers.cmd.unknown", name=name)
if not _RELEASE.fullmatch(version.current().removeprefix("v")):
return i18n.t("layers.cmd.dev_build")
try:
sha256(name)
except OSError:
return i18n.t("layers.cmd.not_shipped", name=name)
return None


def command(name: str, action: str, args: list[str] | tuple[str, ...] = ()) -> str:
"""The command line for ``name action args``, pinned to this release's tag and its sha256."""
if (why := problem(name)) is not None:
raise Unavailable(why)
release = version.current().removeprefix("v")
digest = sha256(name)
url = f"{REPO_RAW}/v{release}/scripts/{name}"
run = " ".join(shlex.quote(part) for part in (action, *args))
return (
f'd=$(mktemp -d) && curl -fsSL -o "$d/{name}" {url} '
f'&& echo "{digest} $d/{name}" | sha256sum -c - '
f'&& sudo {SCRIPTS[name]} "$d/{name}" {run}'
)
Loading
Loading