feat(runner): add the own code runner contract, client and nsjail service

This commit is contained in:
shira committed 2026-10-11 08:23:42 +03:00
1 parent 5e1db37746
commit cdf6bcbb91
109 files changed
+24084 -28

No files matched your search

+35 -2
View File
@@ -21,6 +21,12 @@ DEMO_FILE := $(ROOT)/deploy/docker/docker-compose.demo.yml
DEMO_RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.demo.release.yml
RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.release.yml
ML_RELEASE_FILE := $(ROOT)/deploy/docker/docker-compose.ml.release.yml
# DEVELOPMENT ONLY: the privileged local code runner. It is never rendered
# together with a release overlay or started by a release target.
RUNNER_DEV_FILE := $(ROOT)/deploy/docker/docker-compose.runner.dev.yml
RUNNER_DEV_PROJECT ?= runner-dev
# Local random service token of the dev runner; runner/.gitignore keeps it out of Git.
RUNNER_DEV_TOKEN_FILE ?= $(ROOT)/runner/.dev/api-token
RELEASE_SCRIPT := $(ROOT)/deploy/scripts/release_manifest.py
RELEASE := PYTHONDONTWRITEBYTECODE=1 python3 "$(RELEASE_SCRIPT)"
# Pull policy of release-build for base and service images: missing or always.
@@ -58,7 +64,7 @@ DEMO_SCRIPT_ENV = DOCKER="$(DOCKER)" DEMO_MODE="$(DEMO_MODE)" DEMO_PROJECT="$(DE
DEMO_RELEASE_MANIFEST="$(DEMO_RELEASE_MANIFEST)"
PLAYWRIGHT_VERSION := $(shell sed -n 's/^ *"@playwright\/test": "\([^"]*\)",\{0,1\}$$/\1/p' "$(ROOT)/frontend/package.json")
.PHONY: help config build up down logs monitoring-config monitoring-up monitoring-down monitoring-logs ml-config ml-build ml-up ml-down ml-logs check check-workflows check-release check-perimeter check-e2e-mail check-demo perimeter-test smoke test-e2e test-e2e-registration release-build release-smoke release-e2e release-manifest release-manifest-verify release-manifest-check release-up release-down release-clean demo-seed demo-reset demo-down
.PHONY: help config build up down logs monitoring-config monitoring-up monitoring-down monitoring-logs ml-config ml-build ml-up ml-down ml-logs runner-dev-config runner-dev-build runner-dev-up runner-dev-down runner-dev-logs check-runner-dev check check-workflows check-release check-perimeter check-e2e-mail check-demo perimeter-test smoke test-e2e test-e2e-registration release-build release-smoke release-e2e release-manifest release-manifest-verify release-manifest-check release-up release-down release-clean demo-seed demo-reset demo-down
help: ## Show deployment commands
@awk 'BEGIN {FS = ":.*## "} /^[a-zA-Z0-9_-]+:.*## / {printf "%-24s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
@@ -76,6 +82,7 @@ config: ## Render all Compose files with safe example values
APP_PROJECT=lincodex-demo-config APP_NETWORK=lincodex-demo-config_default DEMO_ACCOUNT_PASSWORD=render-only-demo-account-password IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-demo-config -f "$(CORE_FILE)" -f "$(DEMO_FILE)" --profile demo-seed config --quiet
APP_PROJECT=lincodex-demo-config APP_NETWORK=lincodex-demo-config_default LINCODEX_RELEASE_TAG=000000000000 DEMO_ACCOUNT_PASSWORD=render-only-demo-account-password IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-demo-config -f "$(CORE_FILE)" -f "$(RELEASE_FILE)" -f "$(DEMO_FILE)" -f "$(DEMO_RELEASE_FILE)" --profile demo-seed config --quiet
E2E_COMPANY_FIXTURE_TOKEN=render-only-company-fixture-token-00000000000000000000000000000000 APP_PROJECT=lincodex-e2e-mail-config APP_NETWORK=lincodex-e2e-mail-config_default PLAYWRIGHT_VERSION="$(PLAYWRIGHT_VERSION)" E2E_UID=1000 E2E_GID=1000 E2E_SPEC_DIR="$(ROOT)/frontend/e2e" E2E_ARTIFACTS_DIR="$(ROOT)/frontend/test-results/e2e" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p lincodex-e2e-mail-config -f "$(CORE_FILE)" -f "$(E2E_FILE)" -f "$(E2E_MAIL_FILE)" --profile e2e config --quiet
RUNNER_DEV_TOKEN_FILE=/dev/null $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" config --quiet
build: ## Build local frontend and backend images
APP_PROJECT="$(APP_PROJECT)" IDENTITY_KEY_SECRET="$(CHECK_IDENTITY_KEY_SECRET)" $(DOCKER) compose --env-file "$(CHECK_ENV_FILE)" -p "$(APP_PROJECT)" -f "$(CORE_FILE)" build
@@ -125,6 +132,29 @@ ml-logs: ## Follow ML service logs
@test -f "$(ENV_FILE)" || { printf 'Missing %s.\n' "$(ENV_FILE)" >&2; exit 2; }
APP_PROJECT="$(APP_PROJECT)" APP_NETWORK="$(APP_NETWORK)" $(DOCKER) compose --env-file "$(ENV_FILE)" -p "$(ML_PROJECT)" -f "$(ML_FILE)" logs -f
# DEVELOPMENT ONLY: the code runner in a privileged local container. See the
# header of docker-compose.runner.dev.yml; production runs the runner only on
# its dedicated VM.
runner-dev-config: ## Render the dev-only runner Compose file
RUNNER_DEV_TOKEN_FILE=/dev/null $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" config --quiet
runner-dev-build: ## Build the runner image for the dev-only Compose project
RUNNER_DEV_TOKEN_FILE=/dev/null $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" build
runner-dev-up: ## Start the dev-only privileged runner on 127.0.0.1 with a local random token
@if [ ! -s "$(RUNNER_DEV_TOKEN_FILE)" ]; then \
mkdir -p "$$(dirname "$(RUNNER_DEV_TOKEN_FILE)")"; \
(umask 077 && python3 -c 'import secrets; print(secrets.token_urlsafe(48))' > "$(RUNNER_DEV_TOKEN_FILE)"); \
printf 'Created the dev runner token file %s\n' "$(RUNNER_DEV_TOKEN_FILE)"; \
fi
RUNNER_DEV_TOKEN_FILE="$(RUNNER_DEV_TOKEN_FILE)" $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" up -d --build
runner-dev-down: ## Stop the dev-only runner
RUNNER_DEV_TOKEN_FILE="$(RUNNER_DEV_TOKEN_FILE)" $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" down
runner-dev-logs: ## Follow dev-only runner logs
RUNNER_DEV_TOKEN_FILE="$(RUNNER_DEV_TOKEN_FILE)" $(DOCKER) compose -p "$(RUNNER_DEV_PROJECT)" -f "$(RUNNER_DEV_FILE)" logs -f
# Edge proxy (TLS and tunnel templates under deploy/edge). edge.env is local;
# the checked-in example is used when it does not exist.
.PHONY: edge-render edge-config
@@ -138,7 +168,7 @@ edge-render: ## Render the edge nginx config from deploy/edge/edge.env into depl
edge-config: ## Render the edge templates and check the nginx config with nginx -t in a container
@DOCKER="$(DOCKER)" EDGE_NGINX_IMAGE="$(EDGE_NGINX_IMAGE)" bash "$(EDGE_DIR)/scripts/check.sh" "$(EDGE_ENV)"
check: config check-workflows check-release check-perimeter check-e2e-mail check-demo ## Validate Compose files, CI workflow aggregates, release tooling, and network boundaries without starting services
check: config check-workflows check-release check-perimeter check-e2e-mail check-demo check-runner-dev ## Validate Compose files, CI workflow aggregates, release tooling, and network boundaries without starting services
check-workflows: ## Check that each workflow aggregate job requires every other job
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/scripts" -p 'test_workflow_aggregate.py'
@@ -152,6 +182,9 @@ check-perimeter: ## Check published ports, the app Nginx paths, limits, and the
check-e2e-mail: ## Check that the mail fixture stays inside the explicit local E2E network
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_e2e_mail.py'
check-runner-dev: ## Check that the dev-only runner switches stay out of production files
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_runner_dev.py'
check-demo: ## Check the guards of the demo targets and the demo overlay without starting services
PYTHONDONTWRITEBYTECODE=1 python3 -m unittest discover -s "$(ROOT)/deploy/scripts" -p 'test_demo.py'
@@ -0,0 +1,59 @@
# DEVELOPMENT ONLY: the code runner on a developer machine (Docker Desktop).
#
# This file is never part of a release or a production deployment. It gives
# the container `privileged: true`, because nsjail needs namespaces and a
# writable cgroup v2 tree inside the Docker Desktop VM, and it serves plain
# HTTP on loopback. A privileged container on the core application host is
# forbidden; production runs the runner image only inside its dedicated VM
# with TLS and without these switches (see docs/en/DEPLOY.md).
#
# Use it through make -C deploy runner-dev-up, which creates the local token
# file named by RUNNER_DEV_TOKEN_FILE.
services:
runner:
build:
context: ../../runner
dockerfile: Dockerfile
image: lincodex-runner-dev:local
# DEV ONLY: see the header.
privileged: true
cgroup: private
# Reaps any orphan as a second line of defense; the runner itself lets
# nsjail reap every jailed process.
init: true
read_only: true
environment:
RUNNER_LISTEN_ADDRESS: ":8080"
# DEV ONLY: production requires RUNNER_TLS_CERT_FILE and RUNNER_TLS_KEY_FILE.
RUNNER_DEV_ALLOW_PLAINTEXT_HTTP: "true"
RUNNER_API_TOKEN_FILE: /run/secrets/runner_api_token
RUNNER_MAX_CONCURRENT_RUNS: "1"
secrets:
- runner_api_token
tmpfs:
- /var/lib/lincodex-runner/work:size=64m,mode=0711
ports:
- "127.0.0.1:${RUNNER_DEV_HTTP_PORT:-8070}:8080"
healthcheck:
test: ["CMD", "/opt/lincodex-runner/lincodex-runner", "healthcheck"]
interval: 10s
timeout: 3s
retries: 5
start_period: 5s
restart: unless-stopped
logging:
driver: json-file
options:
max-size: 10m
max-file: "3"
# The planned runner VM has 2 vCPU and 3 GB.
deploy:
resources:
limits:
cpus: "2.0"
memory: 3G
pids: 512
secrets:
runner_api_token:
file: ${RUNNER_DEV_TOKEN_FILE:?Set RUNNER_DEV_TOKEN_FILE or use make -C deploy runner-dev-up}
+97
View File
@@ -0,0 +1,97 @@
"""Check that the dev-only code runner switches stay out of production files.
The local runner Compose file gives the container `privileged: true` and
serves plain HTTP, because nsjail needs namespaces and a writable cgroup tree
inside Docker Desktop. Production runs the runner only in its dedicated VM.
These tests read files; the render test also needs the docker CLI.
"""
from __future__ import annotations
import json
import os
import re
import shutil
import subprocess
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
DOCKER_DIR = ROOT / "deploy" / "docker"
DEV_FILE = DOCKER_DIR / "docker-compose.runner.dev.yml"
DEPLOY_MAKEFILE = ROOT / "deploy" / "Makefile"
RUNNER_DOCKERFILE = ROOT / "runner" / "Dockerfile"
RELEASE_SCRIPT = ROOT / "deploy" / "scripts" / "release_manifest.py"
DOCKER = os.environ.get("DOCKER", "docker")
DEV_SWITCHES = ("privileged", "RUNNER_DEV_ALLOW_PLAINTEXT_HTTP", "docker-compose.runner.dev.yml")
# Make targets that may use the dev runner file.
DEV_TARGETS = {"config", "runner-dev-config", "runner-dev-build", "runner-dev-up", "runner-dev-down", "runner-dev-logs"}
TARGET = re.compile(r"^([A-Za-z0-9_-]+):")
class RunnerDevFilesTest(unittest.TestCase):
def test_the_dev_file_is_named_and_marked_as_development_only(self) -> None:
self.assertIn(".dev.", DEV_FILE.name)
header = DEV_FILE.read_text(encoding="utf-8").splitlines()[0]
self.assertTrue(header.startswith("# DEVELOPMENT ONLY"), header)
def test_production_compose_files_have_no_dev_switch(self) -> None:
for path in sorted(DOCKER_DIR.glob("docker-compose*.yml")):
if path == DEV_FILE:
continue
text = path.read_text(encoding="utf-8")
for switch in DEV_SWITCHES:
with self.subTest(file=path.name, switch=switch):
self.assertNotIn(switch, text)
def test_the_production_image_has_no_dev_switch(self) -> None:
text = RUNNER_DOCKERFILE.read_text(encoding="utf-8")
self.assertNotIn("RUNNER_DEV_", text)
self.assertNotIn("--privileged", text)
def test_release_tooling_never_uses_the_dev_file(self) -> None:
self.assertNotIn(DEV_FILE.name, RELEASE_SCRIPT.read_text(encoding="utf-8"))
def test_only_dev_targets_use_the_dev_file(self) -> None:
target = None
users = set()
for line in DEPLOY_MAKEFILE.read_text(encoding="utf-8").splitlines():
match = TARGET.match(line)
if match and not line.startswith(("\t", " ")):
target = match.group(1)
if "$(RUNNER_DEV_FILE)" in line and line.startswith("\t"):
users.add(target)
self.assertTrue(users)
self.assertLessEqual(users, DEV_TARGETS)
class RunnerDevRenderTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
if shutil.which(DOCKER) is None:
raise unittest.SkipTest("docker CLI is not available")
result = subprocess.run(
[DOCKER, "compose", "-p", "runner-dev-check", "-f", str(DEV_FILE), "config", "--format", "json"],
env={**os.environ, "RUNNER_DEV_TOKEN_FILE": "/dev/null"},
capture_output=True,
text=True,
check=False,
)
if result.returncode != 0:
raise AssertionError(f"docker compose config failed: {result.stderr}")
cls.runner = json.loads(result.stdout)["services"]["runner"]
def test_the_dev_runner_publishes_only_loopback(self) -> None:
ports = self.runner.get("ports") or []
self.assertTrue(ports)
for port in ports:
self.assertEqual(port["host_ip"], "127.0.0.1")
def test_the_dev_runner_reads_the_token_from_a_secret_file(self) -> None:
environment = self.runner["environment"]
self.assertEqual(environment["RUNNER_API_TOKEN_FILE"], "/run/secrets/runner_api_token")
self.assertNotIn("RUNNER_API_TOKEN", environment)
if __name__ == "__main__":
unittest.main()