carlos / platform Public

activator: CARLOS_SOCKET and CARLOS_DB for rootfs commands; the web contract pinned

rootfsCmdVars composes a carlos run's environment on a rootfs row:
cmdVars over the generation's file environment, then rootfsVars with
the row's socket and database, so a tenant's file cannot redirect
either. Tests pin the web launch contract (the generation's own socket),
the run rule, an unchanged binary launch, and INV-5's composer row.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 6871e8596894abbe66445a75a46a0fdac5d62221 parent 691ec92
3 files changed, +258 −3
  • internal/activator/rootfs_invariants_test.go +47 −3
  • internal/activator/rootfsenv.go +22 −0
  • internal/activator/rootfsenv_test.go +189 −0

CI failed — run details

diff --git a/internal/activator/rootfs_invariants_test.go b/internal/activator/rootfs_invariants_test.go
index f278f89d0..a2eba103e 100644
--- a/internal/activator/rootfs_invariants_test.go
+++ b/internal/activator/rootfs_invariants_test.go
@@ -14,7 +14,8 @@ package activator
// - INV-4: TestRootfsRefusalsAreNotBuildFailures. Every rootfs refusal is
// an ownership end, never the build's failure.
// - INV-5: TestRootfsLaunchFilesCarryOnlyComposedEnv. A rootfs launch
-// file carries the composed environment and nothing of the agent's.
+// file carries the composed environment and nothing of the agent's, and
+// each rootfs composer's variables hold nothing of the agent's either.
//
// The tables only grow. A later task adds its handle, its process or its
// sentinel as a row; it never removes a row, an entry of a refused list or
@@ -838,7 +839,7 @@ func TestRootfsRefusalsAreNotBuildFailures(t *testing.T) {
// rootfsLaunchRows: process -> a start through the production backend that
// returns the launch file it wrote and the variables the start composed.
-// Task 6 and Task 12, R1 and W1 add their processes (run, release, worker).
+// Task 12, R1 and W1 add their processes (run, release, worker).
var rootfsLaunchRows = map[string]func(t *testing.T) (instancectl.Launch, map[string]string){
"web": func(t *testing.T) (instancectl.Launch, map[string]string) {
b, ctl := newRootfsBackend(t)
@@ -865,6 +866,21 @@ var rootfsLaunchRows = map[string]func(t *testing.T) (instancectl.Launch, map[st
},
}
+// rootfsComposerRows: process -> its composer's variables, production-wired:
+// a rootfs generation started with envFile as the tenant's file, and the
+// composer run over the file environment that generation launched with, as
+// the production caller runs it. A launch file built from them goes through
+// rootfsLaunchEnv. Task 6 adds run (rootfsCmdVars); W1 adds worker.
+var rootfsComposerRows = map[string]func(t *testing.T, envFile string) map[string]string{
+ "run": func(t *testing.T, envFile string) map[string]string {
+ b, ctl := newRootfsBackend(t)
+ rt := rootfsRoute(b)
+ up, _ := startRootfsGen7(t, b, ctl, rt, envFile)
+ p := b.pathsFor(rt)
+ return b.rootfsCmdVars(rt, up.LaunchEnv(), p.dbPath, "r1", filepath.Join(p.dataDir, CmdRunDirName, "r1"))
+ },
+}
+
// rootfsAgentOnlyEnv is the agent's own environment for the INV-5 test:
// none of it may reach a rootfs launch file, a CARLOS_ name included
// (childEnv drops those, so only a copy of the agent's environment would
@@ -892,6 +908,30 @@ func TestRootfsLaunchFilesCarryOnlyComposedEnv(t *testing.T) {
}
})
}
+ for process, row := range rootfsComposerRows {
+ t.Run("composer/"+process, func(t *testing.T) {
+ vars := row(t, "SHOP_MODE=live\n")
+ if vars["SHOP_MODE"] != "live" {
+ t.Fatalf("the instance's file did not reach the composed variables: %v", vars)
+ }
+ for _, kv := range rootfsLaunchEnv(vars) {
+ if k, _, _ := strings.Cut(kv, "="); rootfsAgentOnlyEnv[k] != "" {
+ t.Errorf("the agent's %s reached the composed variables", kv)
+ }
+ }
+ // The tenant's own PATH is its choice, and exactly its value
+ // is what the process gets.
+ var paths []string
+ for _, kv := range rootfsLaunchEnv(row(t, "SHOP_MODE=live\nPATH=/app/bin\n")) {
+ if k, _, _ := strings.Cut(kv, "="); k == "PATH" {
+ paths = append(paths, kv)
+ }
+ }
+ if !slices.Equal(paths, []string{"PATH=/app/bin"}) {
+ t.Errorf("PATH entries %q, want exactly the tenant's PATH=/app/bin", paths)
+ }
+ })
+ }
}
// ---- the tables only grow ----------------------------------------------
@@ -917,9 +957,10 @@ var (
"rootfsRefusals": {"errBuildStateUnwritten", "errNoRootfsControl", "errRootfsBinaryLaunch", "errRootfsRefused",
"refuseRootfs (a gate's refusal)", "root's 403 (ErrStagedImage)", "root's pre-start refusal (ErrStartRefused, marked)"},
"rootfsLaunchRows": {"web"},
+ "rootfsComposerRows": {"run"},
"rootfsAgentOnlyEnv": {"AGENT_ONLY", "CARLOS_AGENT_ONLY", "HOME", "PATH"},
// The structural guard's targets on main at Task 1.
- "scanned": {"prepare.go:ExecBackend.writeRootfsLaunchFor", "rootfs.go"},
+ "scanned": {"prepare.go:ExecBackend.writeRootfsLaunchFor", "rootfs.go", "rootfsenv.go"},
}
verbGuardFixturesFloor = map[string]verbGuardFixture{
"rootfs_clean.go": {},
@@ -1008,6 +1049,9 @@ func TestRootfsInvariantTablesOnlyGrow(t *testing.T) {
for name := range rootfsLaunchRows {
have["rootfsLaunchRows"] = append(have["rootfsLaunchRows"], name)
}
+ for name := range rootfsComposerRows {
+ have["rootfsComposerRows"] = append(have["rootfsComposerRows"], name)
+ }
for name := range rootfsAgentOnlyEnv {
have["rootfsAgentOnlyEnv"] = append(have["rootfsAgentOnlyEnv"], name)
}
diff --git a/internal/activator/rootfsenv.go b/internal/activator/rootfsenv.go
new file mode 100644
index 000000000..73b25cccd
--- /dev/null
+++ b/internal/activator/rootfsenv.go
@@ -0,0 +1,22 @@
+// rootfsenv.go is the environment contract of a rootfs row's commands
+// (rootfs apps Plan 3, D1). The web process's half is spawnRootfs's:
+// rootfsVars over agentOwnedVars, with the generation's own socket. A
+// rootfs process has no argv of the agent's, so its socket and database
+// reach it as environment (rootfsVars), set after the file environment so
+// a tenant's file cannot point either elsewhere. A launch file built from
+// these variables goes through rootfsLaunchEnv, never childEnv, which
+// starts from the agent's own environment (INV-5). Binary rows never come
+// here: their argv carries --socket and --db. The environment is never
+// logged.
+package activator
+
+import "github.com/carlosframework/platform/internal/registry"
+
+// rootfsCmdVars is a carlos run's environment on a rootfs row: cmdVars over
+// fileEnv, the file environment the run's generation launched with (no
+// control or purge channel; CARLOS_HOST, CARLOS_REGION, CARLOS_DB and the
+// run's id and directory set), then rootfsVars with the row's canonical
+// socket and dbPath.
+func (b *ExecBackend) rootfsCmdVars(rt registry.Route, fileEnv map[string]string, dbPath, runID, runDir string) map[string]string {
+ return rootfsVars(b.cmdVars(rt, fileEnv, dbPath, runID, runDir), rt.Socket, dbPath)
+}
diff --git a/internal/activator/rootfsenv_test.go b/internal/activator/rootfsenv_test.go
new file mode 100644
index 000000000..501fd3434
--- /dev/null
+++ b/internal/activator/rootfsenv_test.go
@@ -0,0 +1,189 @@
+package activator
+
+import (
+ "context"
+ "path/filepath"
+ "slices"
+ "sort"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/instancectl"
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// The tenant's file in these tests tries to point both agent-owned rootfs
+// variables elsewhere, and to hand a command the control channel.
+const rootfsContractEnvFile = "SHOP_MODE=live\nCARLOS_SOCKET=/tmp/x\nCARLOS_DB=/x\nCARLOS_CONTROL_SOCKET=/tmp/c\nCARLOS_PURGE=header\n"
+
+// startRootfsGen7 starts generation 7 of the rootfs test row on its own
+// socket through StartGenExe, with envFile as the tenant's file, and
+// answers the handle and the generation's socket.
+func startRootfsGen7(t *testing.T, b *ExecBackend, ctl *fakeControl, rt registry.Route, envFile string) (*unitProc, string) {
+ t.Helper()
+ b.GenContainment = true
+ writeInstanceEnv(t, b, rt, envFile)
+ id := stagedRootfsIdentity(t, b, "v2")
+ sock := nestedGenSocketIn(b.pathsFor(rt).runDir, 7)
+ ctl.onStart = serveHealthy(t, sock)
+ p, g, err := b.StartGenExe(context.Background(), rt, 7, true, id)
+ if err != nil {
+ t.Fatalf("StartGenExe: %v", err)
+ }
+ t.Cleanup(func() { p.Stop(context.Background()) }) //nolint:errcheck
+ if g.Socket != sock {
+ t.Fatalf("generation socket %q, want %q", g.Socket, sock)
+ }
+ return p.(*unitProc), sock
+}
+
+func sortedKeys(m map[string]string) []string {
+ keys := make([]string, 0, len(m))
+ for k := range m {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+ return keys
+}
+
+// Plan 3 D1, the web half: a rootfs generation's launch file hands the
+// process the generation's own socket (never rt.Socket, never the
+// tenant's), the instance's database, the row's host and the generation's
+// control socket, over the tenant's own variables. Nothing else, and
+// nothing of the agent's environment.
+func TestRootfsWebLaunchCarriesTheContract(t *testing.T) {
+ t.Setenv("PATH", "/agent/bin")
+ t.Setenv("AGENT_ONLY", "box")
+ b, ctl := newRootfsBackend(t)
+ b.Region = "eu-west-2"
+ rt := rootfsRoute(b)
+ _, sock := startRootfsGen7(t, b, ctl, rt, rootfsContractEnvFile)
+ l := readLaunchNamed(t, b, rt, instancectl.GenLaunchFileName(7))
+ if l.Schema != instancectl.LaunchSchemaRootfs || len(l.Argv) != 0 {
+ t.Errorf("launch schema %d argv %q, want schema %d and no argv", l.Schema, l.Argv, instancectl.LaunchSchemaRootfs)
+ }
+ env := envMap(t, l.Env)
+ want := map[string]string{
+ "CARLOS_SOCKET": sock, // the generation's own, not rt.Socket and not the .env's
+ "CARLOS_DB": provision.NestedDBPath(b.DataDir, rt.Host),
+ "CARLOS_HOST": rt.Host,
+ "CARLOS_CONTROL_SOCKET": sock + ".control",
+ "CARLOS_REGION": "eu-west-2",
+ "SHOP_MODE": "live",
+ }
+ if sock == rt.Socket {
+ t.Fatalf("the generation socket is the canonical one: %q", sock)
+ }
+ for k, v := range want {
+ if env[k] != v {
+ t.Errorf("%s = %q, want %q", k, env[k], v)
+ }
+ }
+ if len(env["CARLOS_ADMIN_TOKEN"]) < 20 {
+ t.Errorf("CARLOS_ADMIN_TOKEN missing or short: %q", env["CARLOS_ADMIN_TOKEN"])
+ }
+ if len(env["CARLOS_INSTANCE_KEY"]) < 20 {
+ t.Errorf("CARLOS_INSTANCE_KEY missing or short: %q", env["CARLOS_INSTANCE_KEY"])
+ }
+ // The whole list, so a variable that starts reaching the web process
+ // is a visible change here. CARLOS_PURGE is the agent's (no cache on
+ // this backend: removed, the tenant's value with it).
+ wantKeys := []string{"CARLOS_ADMIN_TOKEN", "CARLOS_CONTROL_SOCKET", "CARLOS_DB", "CARLOS_HOST", "CARLOS_INSTANCE_KEY", "CARLOS_REGION", "CARLOS_SOCKET", "SHOP_MODE"}
+ if got := sortedKeys(env); !slices.Equal(got, wantKeys) {
+ t.Errorf("launch variables %v, want %v", got, wantKeys)
+ }
+ if !slices.IsSorted(l.Env) {
+ t.Errorf("env not one sorted entry per name: %v", l.Env)
+ }
+}
+
+// Plan 3 D1, the run half: a carlos run on a rootfs row gets the row's
+// canonical socket and the instance's database as environment, set after
+// the file environment its generation launched with, so neither can be
+// pointed elsewhere; no control channel, no purge channel; the run's id
+// and directory.
+func TestRootfsCmdVarsFollowTheRunRule(t *testing.T) {
+ t.Setenv("PATH", "/agent/bin")
+ b, ctl := newRootfsBackend(t)
+ b.Region = "eu-west-2"
+ rt := rootfsRoute(b)
+ up, _ := startRootfsGen7(t, b, ctl, rt, rootfsContractEnvFile)
+ genEnv := up.LaunchEnv()
+ if genEnv["CARLOS_SOCKET"] != "/tmp/x" || genEnv["CARLOS_DB"] != "/x" {
+ t.Fatalf("the generation's file environment lost the tenant's values before the composer ran: %v", genEnv)
+ }
+ p := b.pathsFor(rt)
+ runDir := filepath.Join(p.dataDir, CmdRunDirName, "r1")
+ vars := b.rootfsCmdVars(rt, genEnv, p.dbPath, "r1", runDir)
+ want := map[string]string{
+ "CARLOS_SOCKET": rt.Socket,
+ "CARLOS_DB": provision.NestedDBPath(b.DataDir, rt.Host),
+ "CARLOS_HOST": rt.Host,
+ "CARLOS_REGION": "eu-west-2",
+ "CARLOS_RUN_ID": "r1",
+ "CARLOS_RUN_DIR": runDir,
+ "SHOP_MODE": "live",
+ }
+ for k, v := range want {
+ if vars[k] != v {
+ t.Errorf("%s = %q, want %q", k, vars[k], v)
+ }
+ }
+ for _, k := range []string{"CARLOS_CONTROL_SOCKET", "CARLOS_PURGE"} {
+ if v, ok := vars[k]; ok {
+ t.Errorf("a run carries %s=%q", k, v)
+ }
+ }
+ // The serving generation's token and key, never ones of its own, and
+ // nothing else beyond the above.
+ for _, k := range []string{"CARLOS_ADMIN_TOKEN", "CARLOS_INSTANCE_KEY"} {
+ if vars[k] == "" || vars[k] != genEnv[k] {
+ t.Errorf("%s %q, want the generation's %q", k, vars[k], genEnv[k])
+ }
+ }
+ wantKeys := []string{"CARLOS_ADMIN_TOKEN", "CARLOS_DB", "CARLOS_HOST", "CARLOS_INSTANCE_KEY", "CARLOS_REGION", "CARLOS_RUN_DIR", "CARLOS_RUN_ID", "CARLOS_SOCKET", "SHOP_MODE"}
+ if got := sortedKeys(vars); !slices.Equal(got, wantKeys) {
+ t.Errorf("run variables %v, want %v", got, wantKeys)
+ }
+ // The caller's map is never written.
+ if genEnv["CARLOS_SOCKET"] != "/tmp/x" || genEnv["CARLOS_RUN_ID"] != "" {
+ t.Errorf("rootfsCmdVars wrote the generation's environment: %v", genEnv)
+ }
+ // What a launch file would carry: these and nothing of the agent's.
+ env := rootfsLaunchEnv(vars)
+ if m := envMap(t, env); len(m) != len(vars) || m["PATH"] != "" {
+ t.Errorf("launch env %v", env)
+ }
+}
+
+// Binary rows take no composer of this file: the socket and database stay
+// on the argv, and the agent sets no CARLOS_SOCKET or CARLOS_DB.
+func TestBinaryLaunchUnchanged(t *testing.T) {
+ t.Parallel()
+ b, ctl := newRootfsBackend(t)
+ rt := nestedRoute(b, "blog.example.com")
+ stageLive(t, b, rt.Host, "v1")
+ writeInstanceEnv(t, b, rt, "SHOP_MODE=live\n")
+ ctl.onStart = serveHealthy(t, rt.Socket)
+ p, _, err := b.Start(context.Background(), rt)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer p.Stop(context.Background()) //nolint:errcheck
+ l := readLaunchFile(t, b, rt, instancectl.RoleRun)
+ if want := []string{b.liveLink(rt.Host), "--socket", rt.Socket, "--db", rt.DBPath}; !slices.Equal(l.Argv, want) {
+ t.Errorf("binary argv %q, want %q", l.Argv, want)
+ }
+ if l.Schema != instancectl.LaunchSchema || l.Runtime != "" {
+ t.Errorf("binary launch schema %d runtime %q", l.Schema, l.Runtime)
+ }
+ env := envMap(t, l.Env)
+ for _, k := range rootfsOwnedNames {
+ if v, ok := env[k]; ok {
+ t.Errorf("a binary launch carries %s=%q", k, v)
+ }
+ }
+ if env["SHOP_MODE"] != "live" {
+ t.Errorf("the tenant's variable is missing from the binary launch: %v", env)
+ }
+}