carlos / platform Public

console: CARLOS_DEPLOYMENT_SINGLE_TENANT, the operator's one-customer declaration (rootfs spec §5.7)

The console reads the setting exactly as the box does (Plan 4 D6,
first-slice amendment 1.2): "1" on, "" and "0" off, no trimming. Any
other value, " 1 " and "1 " included, stops the console at boot, so it
never allows a create that every box would refuse.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev b715ec72e91451903d99b32064d48a60cf76e8ce parent 1054558
3 files changed, +215 −0
  • cmd/console/main.go +37 −0
  • cmd/console/singletenant_test.go +167 −0
  • internal/console/server.go +11 −0
diff --git a/cmd/console/main.go b/cmd/console/main.go
index ef9e71d10..614159ac3 100644
--- a/cmd/console/main.go
+++ b/cmd/console/main.go
@@ -567,6 +567,12 @@ func serve() {
// D13): applied after the literal so a test can prove it reaches the
// config (applyMovesSwitch).
applyMovesSwitch(&cfg, os.Getenv)
+ // The single-tenant declaration (CARLOS_DEPLOYMENT_SINGLE_TENANT, rootfs
+ // spec §5.7). A value the boxes would read differently stops the console
+ // here rather than let it allow creates every box refuses.
+ if err := applySingleTenant(&cfg, os.Getenv); err != nil {
+ log.Fatal(err)
+ }
if cfg.HostClaim {
cfg.WakeRetryReset = console.NewProductionWakeRetryReset()
}
@@ -1770,6 +1776,37 @@ func applyMovesSwitch(cfg *console.Config, getenv func(string) string) {
cfg.MovesEnabled = moves.Enabled(getenv)
}
+// singleTenantEnv is the operator's one-customer declaration (rootfs apps
+// spec §5.7). Every box reads the same variable from its host.env
+// (internal/host singleTenant).
+const singleTenantEnv = "CARLOS_DEPLOYMENT_SINGLE_TENANT"
+
+// parseSingleTenant reads the setting exactly as the box does (Plan 4 D6):
+// "1" is on, "" and "0" are off. There is no trimming. The box reads any
+// other value, " 1 " included, as off, so the console refuses it rather
+// than run with a reading some box disagrees with.
+func parseSingleTenant(v string) (bool, error) {
+ switch v {
+ case "1":
+ return true, nil
+ case "", "0":
+ return false, nil
+ }
+ return false, fmt.Errorf("console: %s %q: want 0 or 1", singleTenantEnv, v)
+}
+
+// applySingleTenant sets console.Config.DeploymentSingleTenant from
+// singleTenantEnv. On an error the field is left as it was and serve()
+// stops.
+func applySingleTenant(cfg *console.Config, getenv func(string) string) error {
+ on, err := parseSingleTenant(getenv(singleTenantEnv))
+ if err != nil {
+ return err
+ }
+ cfg.DeploymentSingleTenant = on
+ return nil
+}
+
// armMovesPaging arms alerting's sinks from CARLOS_ALERT_WEBHOOK and
// CARLOS_ALERT_SNS_TOPIC_ARN for claim recovery and its optional move pass.
// Claim loss pages run even when moves are disabled.
diff --git a/cmd/console/singletenant_test.go b/cmd/console/singletenant_test.go
new file mode 100644
index 000000000..f69f2a4ad
--- /dev/null
+++ b/cmd/console/singletenant_test.go
@@ -0,0 +1,167 @@
+package main
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/console"
+)
+
+// The console reads CARLOS_DEPLOYMENT_SINGLE_TENANT exactly as the box does
+// (internal/host singleTenant, D6): "1" is on, "" and "0" are off, with no
+// trimming. Anything else stops the console at boot, so every value it runs
+// with is one every box reads the same way.
+func TestParseSingleTenant(t *testing.T) {
+ for v, want := range map[string]bool{"": false, "0": false, "1": true} {
+ got, err := parseSingleTenant(v)
+ if err != nil || got != want {
+ t.Errorf("parseSingleTenant(%q) = %v, %v; want %v", v, got, err, want)
+ }
+ }
+ // The box reads only 1 (D6). A console that took "true" as on would
+ // allow creates every box refuses. The box reads " 1 " and "1 " as off,
+ // so a console that trimmed them would split from it the same way.
+ for _, v := range []string{"true", "yes", "on", "2", "false", " 1 ", "1 ", " 1", "0 ", " ", "\t1", "1\n"} {
+ if _, err := parseSingleTenant(v); err == nil {
+ t.Errorf("parseSingleTenant(%q) accepted", v)
+ }
+ }
+}
+
+func TestApplySingleTenantReadsOnlyItsVariable(t *testing.T) {
+ if singleTenantEnv != "CARLOS_DEPLOYMENT_SINGLE_TENANT" {
+ t.Fatalf("singleTenantEnv = %q; the box reads CARLOS_DEPLOYMENT_SINGLE_TENANT", singleTenantEnv)
+ }
+ var cfg console.Config
+ err := applySingleTenant(&cfg, func(k string) string {
+ if k != singleTenantEnv {
+ t.Fatalf("read %q", k)
+ }
+ return "1"
+ })
+ if err != nil || !cfg.DeploymentSingleTenant {
+ t.Fatalf("DeploymentSingleTenant = %v, %v", cfg.DeploymentSingleTenant, err)
+ }
+
+ // Off and unset leave the field false; a bad value errors and leaves
+ // it false, so nothing downstream could read a half-applied setting.
+ for _, v := range []string{"", "0", " 1 ", "true"} {
+ cfg := console.Config{}
+ err := applySingleTenant(&cfg, func(string) string { return v })
+ if cfg.DeploymentSingleTenant {
+ t.Errorf("%q: DeploymentSingleTenant = true", v)
+ }
+ if bad := v != "" && v != "0"; bad != (err != nil) {
+ t.Errorf("%q: err = %v", v, err)
+ }
+ }
+}
+
+// serve() is not callable from a test, so this parses main.go (the shape of
+// TestServeAppliesTheMovesSwitchFromTheEnvironment) and checks: serve()
+// calls applySingleTenant(&X, os.Getenv) exactly once, as the init of an
+// if whose err != nil branch hands that error to log.Fatal; the same X is
+// the config handed to console.NewServer; and the call comes first.
+func TestServeAppliesTheSingleTenantSetting(t *testing.T) {
+ f, err := parser.ParseFile(token.NewFileSet(), "main.go", nil, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var serve *ast.FuncDecl
+ for _, d := range f.Decls {
+ if fn, ok := d.(*ast.FuncDecl); ok && fn.Name.Name == "serve" {
+ serve = fn
+ }
+ }
+ if serve == nil {
+ t.Fatal("no serve() in main.go")
+ }
+ var applies, news []*ast.CallExpr
+ var guarded *ast.CallExpr
+ ast.Inspect(serve.Body, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.IfStmt:
+ if call := singleTenantGuard(n); call != nil {
+ guarded = call
+ }
+ case *ast.CallExpr:
+ switch fun := n.Fun.(type) {
+ case *ast.Ident:
+ if fun.Name == "applySingleTenant" {
+ applies = append(applies, n)
+ }
+ case *ast.SelectorExpr:
+ if isIdent(fun.X, "console") && fun.Sel.Name == "NewServer" {
+ news = append(news, n)
+ }
+ }
+ }
+ return true
+ })
+ if len(applies) != 1 || len(news) != 1 {
+ t.Fatalf("serve() has %d applySingleTenant calls and %d console.NewServer calls, want one of each", len(applies), len(news))
+ }
+ apply, ns := applies[0], news[0]
+ if guarded != apply {
+ t.Fatal("applySingleTenant is not called as `if err := applySingleTenant(...); err != nil { log.Fatal(err) }`; a bad value would not stop the console")
+ }
+ if len(apply.Args) != 2 || len(ns.Args) < 1 {
+ t.Fatal("applySingleTenant wants (&cfg, os.Getenv) and console.NewServer wants (cfg, store)")
+ }
+ if sel, ok := apply.Args[1].(*ast.SelectorExpr); !ok || !isIdent(sel.X, "os") || sel.Sel.Name != "Getenv" {
+ t.Fatal("applySingleTenant is not given os.Getenv; the setting would not read the real environment")
+ }
+ addr, ok := apply.Args[0].(*ast.UnaryExpr)
+ if !ok || addr.Op != token.AND {
+ t.Fatal("applySingleTenant's first argument is not &X")
+ }
+ x, ok := addr.X.(*ast.Ident)
+ if !ok {
+ t.Fatal("applySingleTenant's first argument is not the address of a plain variable")
+ }
+ if !isIdent(ns.Args[0], x.Name) {
+ t.Fatalf("console.NewServer is not handed %s, the config applySingleTenant changed", x.Name)
+ }
+ if apply.Pos() >= ns.Pos() {
+ t.Fatal("applySingleTenant runs after console.NewServer; the server would be built with the setting off")
+ }
+}
+
+// singleTenantGuard returns the applySingleTenant call when s is
+// `if E := applySingleTenant(...); E != nil { ... log.Fatal(E) ... }`.
+func singleTenantGuard(s *ast.IfStmt) *ast.CallExpr {
+ init, ok := s.Init.(*ast.AssignStmt)
+ if !ok || len(init.Lhs) != 1 || len(init.Rhs) != 1 {
+ return nil
+ }
+ errVar, ok := init.Lhs[0].(*ast.Ident)
+ if !ok {
+ return nil
+ }
+ call, ok := init.Rhs[0].(*ast.CallExpr)
+ if !ok || !isIdent(call.Fun, "applySingleTenant") {
+ return nil
+ }
+ cond, ok := s.Cond.(*ast.BinaryExpr)
+ if !ok || cond.Op != token.NEQ || !isIdent(cond.X, errVar.Name) || !isIdent(cond.Y, "nil") {
+ return nil
+ }
+ fatal := false
+ ast.Inspect(s.Body, func(n ast.Node) bool {
+ c, ok := n.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ if sel, ok := c.Fun.(*ast.SelectorExpr); ok && isIdent(sel.X, "log") && sel.Sel.Name == "Fatal" &&
+ len(c.Args) == 1 && isIdent(c.Args[0], errVar.Name) {
+ fatal = true
+ }
+ return true
+ })
+ if !fatal {
+ return nil
+ }
+ return call
+}
diff --git a/internal/console/server.go b/internal/console/server.go
index 34f966e5e..b71035cdc 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -659,6 +659,17 @@ type Config struct {
// zone that one person owns, and the reserved rules refused ordinary
// names there. See deniedDomains.
ReservePlatformDomains bool
+ // DeploymentSingleTenant (CARLOS_DEPLOYMENT_SINGLE_TENANT) is the
+ // operator's declaration that every box of this deployment belongs to
+ // one customer (rootfs apps spec §5.7). With it set, a rootfs app may
+ // run on any box, not only on a fleet box of its own account. Each box
+ // reads the same variable from its host.env and decides for itself;
+ // the console's reading only words a clear refusal, so the operator
+ // sets it on the console and on every box together. Operator-only: no
+ // door reads or writes it except through rootfsPlacementRefusal and
+ // rootfsOnOwnBoxes. cmd/console parses it exactly as the box does
+ // ("", "0" or "1", no trimming) and stops at boot on anything else.
+ DeploymentSingleTenant bool
// MovesEnabled is the Phase 1 moves switch (moves.Enabled, from
// CARLOS_MOVES_PHASE1). Off, the console behaves exactly as it did
// before Phase 1: the legacy move door answers and nothing new is