Skip to content

feat(deploy): scope-restricted panel egress for sandbox off mode - #792

Open
thecorporatetractor wants to merge 5 commits into
BaaRai:mainfrom
thecorporatetractor:feat/k8s-off-mode-egress
Open

thecorporatetractor wants to merge 5 commits into
BaaRai:mainfrom
thecorporatetractor:feat/k8s-off-mode-egress

Conversation

@thecorporatetractor

Copy link
Copy Markdown
Contributor

Problem

With sandbox.mode=off there is no runner and no task pod: agent commands run inside the panel pod. On Kubernetes the panel's egress is then open to the internet, other namespaces, the nodes and the Kubernetes API. The operator-approved scope is not enforced at the network level (see #421 for the general issue).

Change

  • artex-runner gains ARTEX_RUNNER_BACKEND=panel-egress: a policy-only controller. It accepts the panel's scope over the token-authenticated PUT /v1/panel-egress (GET reports the state) and keeps one NetworkPolicy that selects the panel pod: approved CIDRs as they are, resolved addresses of the scope hosts as /32. Resolved private, metadata and IPv6 addresses are dropped unless an approved CIDR covers them, as in the per-task proxy policy. Hosts are re-resolved every refresh; an address stays allowed for retention after it was last seen (DNS rotation). Before the first push, and while the controller is down, the last policy stays (fail closed). Exec, kill and delete are refused.
  • Panel: with ARTEX_SANDBOX=off and ARTEX_PANEL_EGRESS_URL set, it builds the union of the approved scope of every live task with the same code as the per-task proxy, adds known hostnames under the approved domains and the LLM endpoints, and pushes it before each planner/worker turn when it changed and every 30 s. It holds no Kubernetes credentials. GET /api/health reports sandbox_egress. ARTEX_RUNNER_TOKEN is scrubbed and sealed like the other startup secrets. Nothing changes without the variable.
  • Chart: sandbox.offModeEgress.enabled=true by default with sandbox.mode=off. A static NetworkPolicy allows the panel only cluster DNS, the database and the controller. The controller runs from the runner image, non-root, no capabilities, read-only root filesystem, with a Role limited to creating NetworkPolicies and reading/updating the one scope policy. extraHosts, extraCIDRs, postgresCIDRs, refresh, retention are configurable. Rendering fails instead of running open when NetworkPolicy is disabled or an external database has no CIDRs. enabled=false keeps the old open egress and is documented as unsafe.
  • scripts/k3d-e2e.sh gains OFF_MODE=1. Limits are in the chart README: union of all live tasks, IP-based, DNS still resolvable, no IPv6, no NET_RAW (in-scope nmap -sT works).
  • Docker deployments and sandbox.mode=required are unchanged.

Depends on

Depends on #783 (Helm chart), which depends on #780. The diff includes their commits until they merge. Review the last 3 commits.

Tests

  • go build ./..., go vet ./..., gofmt clean, cross-builds for darwin/arm64, darwin/amd64, windows/amd64: OK.
  • helm lint --strict with default values and with sandbox.mode=off: 0 failed. Chart render tests for off mode (helmchart_offmode_test.go: policies, RBAC, wiring, schema rejections, lint of both modes): pass.
  • go test -race -count=1 ./sandbox ./cmd/...: OK. ./server under -race with PostgreSQL hit the default 10 min go test timeout on this loaded machine (no failure, the timeout fired in an unrelated tamper-hooks test); the same package passes without -race.
  • go test -p 1 ./... against a throwaway PostgreSQL 16 container: 25 packages OK, 0 failures.
  • After the rebase onto the updated chart PR (which brought fix(db): return approved_by_operator and expanded in the task scope list #775 and security: promote company scope egress only when approved by the operator #779, both changing scope code this PR reads): go build, go vet, and go test -p 1 ./sandbox ./server ./db ./cmd/... with PostgreSQL: all pass.
  • shellcheck -S warning scripts/k3d-e2e.sh: clean.
  • Live, OFF_MODE=1 scripts/k3d-e2e.sh on a new k3d cluster with images built from this branch: helm test Succeeded, isolation self-test passed, controller rolled out, TestPanelEgressOffModeE2E passed in 240 s, 5/5 subtests: baseline (other namespaces, API server, nodes, metadata and out-of-scope hosts unreachable as the agent uid), in-scope hosts reachable including nmap -sT, revocation removes access, a stopped controller fails closed, task deletion removes its scope.

Notes

  • This PR and feat(sandbox): Sandbox page, scale-to-zero and capacity cap for the Kubernetes backend #791 (Sandbox page) both touch cmd/artex-runner/main.go, server/server.go, deploy/helm/artex/templates/_helpers.tpl, scripts/k3d-e2e.sh and CHANGELOG.md. Merged together they give textual conflicts in those files; all are "keep both sides" (both if blocks in main.go, both struct fields and calls in server.go, both helpers in _helpers.tpl, both set_args additions in the script, both CHANGELOG entries; server.go needs gofmt after). The test clock in paneleggress_test.go is named egressClock so it does not clash with the scaling tests' fakeClock. Checked with a throwaway merge: build, vet, gofmt, chart lint in both modes, and go test ./sandbox pass.
  • The scope is the union of all live tasks, not per task: while two tasks run, each can reach the other's scope.
  • The policy is IP-based. A shared IP (CDN) of an in-scope host also allows other hosts on that IP.

Part of #781.

ARTEX_RUNNER_BACKEND=kubernetes runs each task in a worker pod behind its own
egress-proxy pod, created through the Kubernetes API with no container runtime
socket. Per-task NetworkPolicies are applied before the pods and removed after
them; the runner waits until they are in effect before the first command, runs
commands through pods/exec under env -i and artex-exec-wrap, and re-adopts task
pods after a restart. Unit tests run against a fake API server; a live test
(TestKubeSandboxE2E) drives a deployed runner.
deploy/helm/artex deploys the panel, PostgreSQL and artex-runner with a
namespace-scoped Role, default-deny NetworkPolicies and a values schema. A
volume-permissions init container restores root ownership of /app/data and
/app/keys, fixing the isolation self-test failure on world-writable volumes
(BaaRai#620). scripts/k3d-e2e.sh and the kubernetes CI job test it live on k3d.
ARTEX_RUNNER_BACKEND=panel-egress turns artex-runner into a policy-only
controller for ARTEX_SANDBOX=off on Kubernetes. It accepts the panel's scope
union over the token-authenticated PUT /v1/panel-egress (GET reports the
state) and keeps one NetworkPolicy selecting the panel pod: approved CIDRs as
they are, and the addresses the scope hosts resolve to as /32 ipBlocks.
Resolved addresses in DefaultProxyDeniedCIDRs (or the configured list) and
IPv6 answers are dropped unless an approved CIDR covers them, mirroring the
per-task proxy policy. Hosts are re-resolved every refresh; an address stays
allowed for the retention window after a host last resolved to it, a failed
lookup keeps the last known addresses until they expire, and a host that
leaves the scope loses its addresses on that push. Before the first push and
while it is down, the last policy stays in place (fail closed). Exec, kill and
delete are refused. The API connection setup of the Kubernetes backend is
shared through newKubeConn.
With ARTEX_SANDBOX=off and ARTEX_PANEL_EGRESS_URL set (the Helm chart sets it),
the panel builds the union of the operator-approved scope of every live task
with taskSandboxRunInfo, adds the known asset hostnames under the approved
domains and the LLM endpoints from the environment and the saved profiles, and
pushes it to the controller before each planner/worker turn when it changed
and every 30 s. A failed push is retried by the periodic sync and, after 15 s,
by the next turn. The state is logged on change and reported to signed-in
sessions in GET /api/health as sandbox_egress. Nothing changes without the
variable: Docker deployments and required mode behave as before.
ARTEX_RUNNER_TOKEN, which now also authorizes scope pushes, is scrubbed and
sealed like the other startup secrets.
With sandbox.mode=off the chart now restricts the panel's egress by default
(sandbox.offModeEgress.enabled=true): a static NetworkPolicy allows only
cluster DNS, the database (bundled, or postgresCIDRs) and the panel egress
controller, which runs from the runner image as non-root with no capabilities
and a Role limited to creating NetworkPolicies and reading/updating the one
scope policy. extraHosts/extraCIDRs, refresh and retention are configurable.
Rendering fails instead of running open when NetworkPolicy is disabled or an
external database has no CIDRs; values from an older release count as enabled.
enabled=false keeps the previous unrestricted behaviour, documented as unsafe.

Chart render tests cover the policies, RBAC, wiring and schema rejections, and
lint both modes. TestPanelEgressOffModeE2E checks live, as the agent uid inside
the panel, that in-scope hosts (including nmap -sT) are reachable and other
namespaces, the API server, nodes, metadata and out-of-scope hosts are not,
that revocation and task deletion remove access, and that a stopped controller
fails closed; scripts/k3d-e2e.sh runs it with OFF_MODE=1.

scripts/k3d-e2e.sh reads the panel log before grepping it for the isolation
self-test: with pipefail, grep -q exiting early failed the check on the longer
off-mode log.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant