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
51 changes: 50 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
web: ${{ steps.filter.outputs.web }}
python: ${{ steps.filter.outputs.python }}
shell: ${{ steps.filter.outputs.shell }}
kubernetes: ${{ steps.filter.outputs.kubernetes }}
steps:
- uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
id: filter
Expand All @@ -40,6 +41,19 @@ jobs:
- '**/*.py'
- 'scripts/benchmark/**'
- '.github/**'
kubernetes:
- 'sandbox/**'
- 'deploy/**'
- 'cmd/artex-runner/**'
- 'cmd/artex-exec-wrap/**'
- 'cmd/artex-egress-proxy/**'
- 'Dockerfile.runner'
- 'Dockerfile.worker'
- 'Dockerfile.egress-proxy'
- 'scripts/k3d-e2e.sh'
- 'go.mod'
- 'go.sum'
- '.github/workflows/ci.yml'
# Added files too: a new top-level directory must appear in the bot summary.
shell:
- '**/*.sh'
Expand Down Expand Up @@ -188,7 +202,7 @@ jobs:
# shellcheck is preinstalled on the ubuntu runners. -S warning: the scripts
# currently have only info-level notes (intentional A && B || C chains in
# update.sh, an unquoted glob in backup.sh's prune), so fail on warnings.
- run: shellcheck -S warning install.sh update.sh start.sh scripts/backup.sh scripts/verify.sh reset-password.sh scripts/extract-changelog-section.sh scripts/test-extract-changelog-section.sh .github/scripts/bot.sh scripts/test-bot-context.sh scripts/test-start-sh.sh
- run: shellcheck -S warning install.sh update.sh start.sh scripts/backup.sh scripts/verify.sh reset-password.sh scripts/extract-changelog-section.sh scripts/test-extract-changelog-section.sh .github/scripts/bot.sh scripts/test-bot-context.sh scripts/test-start-sh.sh scripts/k3d-e2e.sh
- run: bash scripts/test-extract-changelog-section.sh
# start.sh is the Docker ENTRYPOINT and runs under dash there, as /bin/sh does here.
- run: bash scripts/test-start-sh.sh
Expand All @@ -197,6 +211,40 @@ jobs:
# scripts/benchmark/ exist.
- run: bash scripts/test-bot-context.sh

# Live test of the Kubernetes sandbox backend and the Helm chart on k3d: the runner,
# worker and egress-proxy images are built from this checkout, the chart is installed
# runner-only (the panel image is not needed), and TestKubeSandboxE2E checks pod
# hardening, NetworkPolicy isolation, the egress proxy, kill, recovery and cleanup.
kubernetes:
needs: changes
if: ${{ !cancelled() && (github.event_name != 'pull_request' || needs.changes.result != 'success' || needs.changes.outputs.kubernetes == 'true') }}
runs-on: ubuntu-latest
timeout-minutes: 30
env:
K3D_VERSION: v5.9.0
# Pinned here, not taken from the release's own checksums file (see the
# Dockerfile tools stage for why).
K3D_SHA256: 06d8f25bc3a971c4eb29e0ff08429b180402db0f4dec838c9eac427e296800a0
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
- uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0
with:
go-version-file: go.mod
- name: install k3d
run: |
curl -fsSL -o /tmp/k3d "https://github.com/k3d-io/k3d/releases/download/${K3D_VERSION}/k3d-linux-amd64"
echo "${K3D_SHA256} /tmp/k3d" | sha256sum -c -
sudo install -m 0755 /tmp/k3d /usr/local/bin/k3d
k3d version
helm version --short
- name: helm lint
run: helm lint --strict deploy/helm/artex
- name: k3d sandbox e2e
env:
RUNNER_ONLY: "1"
ARTEX_E2E_INTERNET: "1"
run: bash scripts/k3d-e2e.sh

web:
needs: changes
if: ${{ !cancelled() && (github.event_name != 'pull_request' || needs.changes.result != 'success' || needs.changes.outputs.web == 'true') }}
Expand Down Expand Up @@ -231,6 +279,7 @@ jobs:
- run: npm run test:chat-mentions
- run: npm run test:format
- run: npm run test:http-href
- run: npm run test:sandbox-topology
- run: npm run build:static

python:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project are documented in this file. The format is b

## [Unreleased]

### Added

- **Kubernetes sandbox backend for `artex-runner`.** With `ARTEX_RUNNER_BACKEND=kubernetes` each task runs in a worker pod behind its own egress-proxy pod, created through the Kubernetes API with no container runtime socket. The worker runs as uid/gid 10001 with `runAsNonRoot`, a read-only root filesystem, all capabilities dropped, `seccomp: RuntimeDefault`, no service-account token and no service links, and mounts only its task directory. Per-task NetworkPolicies are created before the pods and deleted after them: the worker can reach only its own proxy, and the proxy only cluster DNS, public addresses and the task's scope CIDRs. The runner waits until the policies are in effect before the first command, runs commands through `pods/exec`, and re-adopts task pods after a restart.
- **Helm chart for Kubernetes.** `deploy/helm/artex` deploys the panel, PostgreSQL and `artex-runner` with the Kubernetes sandbox backend, a namespace-scoped Role, NetworkPolicies and a values schema. A chart-level default-deny covers every runner-managed pod, so a missing per-task policy fails closed. The `volume-permissions` init container fixes the `Bash can create files in /app/data` self-test failure on world-writable volumes (#620). `scripts/k3d-e2e.sh` and the new `kubernetes` CI job test the chart live on k3d.
- **Sandbox page and Kubernetes sandbox scaling.** The runner's new `GET /v1/tasks` and the panel's `GET /api/sandbox/overview` report each task's pods, scope, network policies, health and recent egress, and **System > Sandbox** shows them as a graph or a list for the Docker and Kubernetes backends. On Kubernetes the runner scales a task to zero after `sandbox.scaling.idleTimeout` (`ARTEX_KUBE_IDLE_TIMEOUT`, default 15 minutes) without a command and starts its pods again on the next one. `sandbox.scaling.maxActive` (`ARTEX_KUBE_MAX_ACTIVE`, default 10) caps the tasks with pods: the least recently used idle task is scaled to zero, or the command waits for `sandbox.scaling.queueTimeout` and fails with HTTP 429 `sandbox at capacity`, which does not pause the task. A task pod still unschedulable at `sandbox.podReadyTimeout` also fails with 429 and is kept for the next command. Scale events, totals and cold-start times survive a runner restart (`ARTEX_KUBE_STATE_DIR`, `runner-state` on the data volume). `sandbox.nodeSelector`, `tolerations`, `priorityClassName` and `podAnnotations` support autoscaled node pools; the chart README documents cluster-autoscaler settings, tested with `scripts/capd-autoscaler-e2e.sh`.

### Security

- **Company egress is promoted only from operator-approved company scope.** A sandboxed worker could register assets with `insert_assets` (an attacker IP or domain attributed to the company through its domain or ICP rule), and the agent could add rules with `add_company_scope`; the next operator selection or re-approval of the company copied those hosts into `task_scope_egress`, persistently widening the approved sandbox egress. The approval snapshot now copies only `company_scope` domain and IP/CIDR rules with the new `approved_by_operator` flag (migration `0025_company_scope_approved.sql`), which only the operator company API sets; `add_company_scope` and the ScopeSentry sync store rules with the flag off. Existing rules are backfilled as approved. Behaviour change: a domain or IP discovered only in assets is no longer reachable from the sandbox until it is added explicitly to the company scope (operator) or to the task scope. Egress rows already frozen by earlier approvals are not purged by this change, and re-approving the company does not remove them (the snapshot only adds rows); removing the company from the task scope drops its rows, and approving it again rebuilds them from approved rules only.
Expand Down
4 changes: 4 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ not the console afterwards.
- Prebuilt GHCR images: the image is published from the next tagged release. Until then,
`docker-compose.ghcr.yml` and `docker pull ghcr.io/baarai/artex` return `denied`; use the checkout
build above.
- Kubernetes: the Helm chart in [`deploy/helm/artex`](deploy/helm/artex/README.md) runs the panel,
PostgreSQL and the sandbox runner with its Kubernetes backend: every agent command runs in a
per-task pod, with no Docker socket. It needs a CNI that enforces NetworkPolicy.
`scripts/k3d-e2e.sh` installs and tests it on a local k3d cluster.

## Troubleshooting

Expand Down
78 changes: 59 additions & 19 deletions cmd/artex-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"log"
"net/http"
"os"
Expand All @@ -22,33 +23,61 @@ func envOr(k, def string) string {
return def
}

// buildBackend selects the container backend from ARTEX_RUNNER_BACKEND
// ("docker", the default, or "kubernetes").
func buildBackend() (sandbox.Backend, error) {
wsRoot := envOr(sandbox.EnvWorkspaceRoot, sandbox.DefaultWorkspaceRoot)
auditRoot := envOr(sandbox.EnvAuditRoot, sandbox.DefaultAuditRoot)
workerImage := envOr(sandbox.EnvWorkerImage, sandbox.DefaultWorkerImage)
proxyImage := envOr(sandbox.EnvProxyImage, sandbox.DefaultProxyImage)

switch kind := strings.ToLower(envOr(sandbox.EnvRunnerBackend, sandbox.BackendDocker)); kind {
case sandbox.BackendDocker:
sock := envOr("DOCKER_SOCK", "/var/run/docker.sock")
if dh := strings.TrimSpace(os.Getenv("DOCKER_HOST")); strings.HasPrefix(dh, "unix://") {
sock = strings.TrimPrefix(dh, "unix://")
}
log.Printf("[runner] backend=docker socket=%s", sock)
return sandbox.NewDockerBackend(sandbox.DockerConfig{
SocketPath: sock,
WorkerImage: workerImage,
ProxyImage: proxyImage,
WorkspaceRoot: wsRoot,
HostWorkspaceRoot: envOr("ARTEX_HOST_WORKSPACE_ROOT", wsRoot),
AuditRoot: auditRoot,
HostAuditRoot: envOr("ARTEX_HOST_AUDIT_ROOT", auditRoot),
TaskNetPrefix: envOr(sandbox.EnvTaskNetPrefix, sandbox.DefaultTaskNetPrefix),
EgressNet: envOr(sandbox.EnvEgressNet, sandbox.DefaultEgressNet),
}), nil
case sandbox.BackendKubernetes:
cfg, err := sandbox.KubeConfigFromEnv()
if err != nil {
return nil, err
}
cfg.WorkerImage, cfg.ProxyImage = workerImage, proxyImage
cfg.WorkspaceRoot, cfg.AuditRoot = wsRoot, auditRoot
b, err := sandbox.NewKubernetesBackend(cfg)
if err != nil {
return nil, err
}
log.Printf("[runner] backend=kubernetes namespace=%s claim=%s idle_timeout=%s max_active=%d state_dir=%q", b.Namespace(), cfg.DataClaim, cfg.IdleTimeout, cfg.MaxActive, cfg.StateDir)
return b, nil
default:
return nil, fmt.Errorf("invalid %s=%q (want %q or %q)", sandbox.EnvRunnerBackend, kind, sandbox.BackendDocker, sandbox.BackendKubernetes)
}
}

func main() {
token := strings.TrimSpace(os.Getenv(sandbox.EnvRunnerToken))
if token == "" {
log.Fatalf("[runner] %s is required (refusing to start unauthenticated)", sandbox.EnvRunnerToken)
}
addr := envOr("ARTEX_RUNNER_ADDR", sandbox.DefaultRunnerAddr)
sock := envOr("DOCKER_SOCK", "/var/run/docker.sock")
if dh := strings.TrimSpace(os.Getenv("DOCKER_HOST")); strings.HasPrefix(dh, "unix://") {
sock = strings.TrimPrefix(dh, "unix://")
backend, err := buildBackend()
if err != nil {
log.Fatalf("[runner] %v", err)
}

wsRoot := envOr(sandbox.EnvWorkspaceRoot, sandbox.DefaultWorkspaceRoot)
hostWSRoot := envOr("ARTEX_HOST_WORKSPACE_ROOT", wsRoot)
auditRoot := envOr(sandbox.EnvAuditRoot, sandbox.DefaultAuditRoot)
hostAuditRoot := envOr("ARTEX_HOST_AUDIT_ROOT", auditRoot)
backend := sandbox.NewDockerBackend(sandbox.DockerConfig{
SocketPath: sock,
WorkerImage: envOr(sandbox.EnvWorkerImage, sandbox.DefaultWorkerImage),
ProxyImage: envOr(sandbox.EnvProxyImage, sandbox.DefaultProxyImage),
WorkspaceRoot: wsRoot,
HostWorkspaceRoot: hostWSRoot,
AuditRoot: auditRoot,
HostAuditRoot: hostAuditRoot,
TaskNetPrefix: envOr(sandbox.EnvTaskNetPrefix, sandbox.DefaultTaskNetPrefix),
EgressNet: envOr(sandbox.EnvEgressNet, sandbox.DefaultEgressNet),
})

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

Expand All @@ -58,6 +87,11 @@ func main() {
log.Printf("[runner] recovered %d managed task container(s) by label", n)
}

// Backends that scale idle tasks to zero run their reaper for the process lifetime.
if sc, ok := backend.(interface{ RunScaler(context.Context) }); ok {
go sc.RunScaler(ctx)
}

rs, err := sandbox.NewRunnerServer(token, version, backend)
if err != nil {
log.Fatalf("[runner] init failed: %v", err)
Expand All @@ -79,4 +113,10 @@ func main() {
sctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(sctx)
// Backends that keep scaling history write it out before exiting.
if fs, ok := backend.(interface{ FlushState() error }); ok {
if err := fs.FlushState(); err != nil {
log.Printf("[runner] save scaling state: %v", err)
}
}
}
14 changes: 14 additions & 0 deletions deploy/helm/artex/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v2
name: artex
description: ARTEX autonomous pentest platform with the per-task Kubernetes sandbox (no Docker socket).
type: application
version: 0.1.0
appVersion: "0.7.1"
kubeVersion: ">=1.25.0-0"
home: https://github.com/BaaRai/ARTEX
sources:
- https://github.com/BaaRai/ARTEX
keywords:
- pentest
- security
- sandbox
Loading
Loading