| index 36af9db4c..16d5b61d6 100644 |
| --- a/internal/host/dbreads_rootrunner_linux_test.go |
| +++ b/internal/host/dbreads_rootrunner_linux_test.go |
| @@ -126,7 +126,10 @@ func newDBReadRunner(t *testing.T, tmpSize string) *dbreadRunner { |
| sqliteFixture(t, "/data/registry.db") |
| sqliteFixture(t, "/etc/carlos/witness.db") |
| sqliteFixture(t, "/data/instances/other.example.com/other.db") |
| - if err := os.WriteFile("/etc/carlos/host.env", []byte("CARLOS_REGION=uk\n"), 0o640); err != nil { |
| + // host.env arms db reads (dbReadsSettings), so the adopt pass's |
| + // ensureSystemUnits arms the socket: it stays dark otherwise. |
| + r.rootPub, r.rootPriv, _ = ed25519.GenerateKey(rand.Reader) |
| + if err := os.WriteFile("/etc/carlos/host.env", []byte(dbreadRunnerHostEnv(r.rootPub)), 0o640); err != nil { |
| t.Fatal(err) |
| } |
| if err := os.WriteFile("/opt/carlos/carlos", self, 0o755); err != nil { |
| @@ -159,7 +162,6 @@ func newDBReadRunner(t *testing.T, tmpSize string) *dbreadRunner { |
| t.Fatal(err) |
| } |
| t.Cleanup(func() { os.RemoveAll(r.dir) }) |
| - r.rootPub, r.rootPriv, _ = ed25519.GenerateKey(rand.Reader) |
| replica := filepath.Join(r.dir, "replica", "instances", dbreadRunnerHost) |
| dbdoortest.Replicate(t, replica, 3) |
| yes := true |
| @@ -173,6 +175,12 @@ func newDBReadRunner(t *testing.T, tmpSize string) *dbreadRunner { |
| return r |
| } |
| |
| +// dbreadRunnerHostEnv is a host.env that arms db reads, plus extra lines. |
| +func dbreadRunnerHostEnv(pub ed25519.PublicKey, extra ...string) string { |
| + lines := append([]string{"CARLOS_REGION=uk", "CARLOS_HIBERNATE_BUCKET=data-uk", "CARLOS_RELEASE_PUBKEY=" + base64.StdEncoding.EncodeToString(pub)}, extra...) |
| + return strings.Join(lines, "\n") + "\n" |
| +} |
| + |
| func (r *dbreadRunner) store() release.Store { |
| return release.NewDirStore(filepath.Join(r.dir, "store")) |
| } |
| @@ -656,3 +664,153 @@ func TestRootDBReadPayload(t *testing.T) { |
| } |
| say() |
| } |
| + |
| +// dbreadTenantUnit is the real tenant template, carlos-instance@.service, |
| +// installed under its own name with only what a runner cannot provide taken |
| +// out: the edge and mount dependencies, the per-instance slice, the stop |
| +// hook and the log socket. Its sandbox (User=carlos, the hidden /run/carlos, |
| +// /data, /opt/carlos and /etc/carlos, the address families) is the |
| +// production one, line for line. ExecStart is this binary as a tenant that |
| +// tries to reach the db-read worker. |
| +const dbreadTenantUnit = "carlos-dbread-tenant@.service" |
| + |
| +func installDBReadTenantUnit(t *testing.T, instance string) { |
| + t.Helper() |
| + raw, err := unitsFS.ReadFile("units/carlos-instance@.service") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var out []string |
| + for _, line := range strings.Split(string(raw), "\n") { |
| + switch { |
| + case strings.HasPrefix(line, "BindsTo="), strings.HasPrefix(line, "Requires="), strings.HasPrefix(line, "After="), |
| + strings.HasPrefix(line, "ExecStopPost="), strings.HasPrefix(line, "Slice="): |
| + continue |
| + case strings.HasPrefix(line, "ExecStart="): |
| + line = "ExecStart=/opt/carlos/carlos dbread-tenant-witness" |
| + case line == "Type=simple": |
| + line = "Type=oneshot" |
| + case strings.HasPrefix(line, "StandardOutput=file:"): |
| + line = "StandardOutput=journal" |
| + } |
| + out = append(out, line) |
| + } |
| + unit := strings.Join(out, "\n") |
| + if !strings.Contains(unit, "TemporaryFileSystem=/run/carlos:ro") || !strings.Contains(unit, "User=carlos") { |
| + t.Fatal("the tenant template no longer hides /run/carlos as carlos; the witness would prove nothing") |
| + } |
| + path := filepath.Join("/etc/systemd/system", dbreadTenantUnit) |
| + if err := os.WriteFile(path, []byte(unit), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { os.Remove(path); runSystemctl("daemon-reload") }) |
| + mustSystemctl(t, "daemon-reload") |
| + uid, gid := carlosIDs(t) |
| + for _, d := range []string{"/data/instances/" + instance, "/run/carlos/instances/" + instance} { |
| + if err := os.MkdirAll(d, 0o750); err != nil { |
| + t.Fatal(err) |
| + } |
| + os.Chown(d, uid, gid) |
| + } |
| + for _, d := range []string{"/run/carlos-control/exe/" + instance + "/app", "/run/carlos-control/exe/" + instance + "/live"} { |
| + if err := os.MkdirAll(d, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + t.Cleanup(func() { |
| + os.RemoveAll("/data/instances/" + instance) |
| + os.RemoveAll("/run/carlos/instances/" + instance) |
| + os.RemoveAll("/run/carlos-control/exe/" + instance) |
| + }) |
| +} |
| + |
| +func carlosIDs(t *testing.T) (int, int) { |
| + t.Helper() |
| + u, err := user.Lookup("carlos") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + uid, _ := strconv.Atoi(u.Uid) |
| + gid, _ := strconv.Atoi(u.Gid) |
| + return uid, gid |
| +} |
| + |
| +// tenantReach runs the installed tenant once and returns its report, after |
| +// checking that no connection reached the socket and no worker started. |
| +func tenantReach(t *testing.T, instance string) map[string]string { |
| + t.Helper() |
| + accepted := func() string { |
| + out, _ := runSystemctl("show", "-p", "NAccepted", "--value", dbReadSocketUnit) |
| + return strings.TrimSpace(out) |
| + } |
| + before := accepted() |
| + report := filepath.Join("/data/instances", instance, "dbread-witness.json") |
| + os.Remove(report) |
| + if out, err := runSystemctl("start", strings.Replace(dbreadTenantUnit, "@", "@"+instance, 1)); err != nil { |
| + jout, _ := exec.Command("journalctl", "-u", strings.Replace(dbreadTenantUnit, "@", "@"+instance, 1), "-n", "30", "--no-pager").CombinedOutput() |
| + t.Fatalf("the tenant unit did not run: %v: %s\n%s", err, out, jout) |
| + } |
| + raw, err := os.ReadFile(report) |
| + if err != nil { |
| + t.Fatalf("the tenant wrote no report: %v", err) |
| + } |
| + var rep map[string]string |
| + if err := json.Unmarshal(raw, &rep); err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Logf("tenant report: %v", rep) |
| + if rep["uid"] == "" || rep["uid"] == "0" { |
| + t.Fatalf("the witness did not run as the tenant user: %v", rep) |
| + } |
| + for _, k := range []string{"connect " + dbReadSocketPath, "connect /run/carlos-dbread/worker.sock"} { |
| + if v, ok := rep[k]; !ok || v == "" { |
| + t.Errorf("a tenant could %s: %v", k, rep) |
| + } |
| + } |
| + if after := accepted(); after != before { |
| + t.Errorf("the socket accepted a tenant connection: NAccepted %s -> %s", before, after) |
| + } |
| + if n := unitsActive(t); n != 0 { |
| + t.Errorf("%d db-read workers running after the tenant's attempt", n) |
| + } |
| + return rep |
| +} |
| + |
| +// Astra round 1, finding 1, on real units: a real tenant unit (the |
| +// production carlos-instance@ sandbox, running as carlos) can neither |
| +// connect to nor activate a db-read worker, with db reads armed on the box |
| +// and with them off; and with them off nothing listens, for the agent |
| +// either. Mutations: put the socket back at /run/carlos-dbread (the tenant |
| +// connects, NAccepted moves); drop the gate in ensureSystemUnits (the |
| +// socket listens with CARLOS_DB_READS=0). |
| +func TestRootDBReadTenantCannotReachTheWorker(t *testing.T) { |
| + r := newDBReadRunner(t, "40g") |
| + const instance = "tenant.example.com" |
| + installDBReadTenantUnit(t, instance) |
| + |
| + // Armed: the socket listens, the agent's probe passes, the tenant |
| + // reaches nothing. |
| + if out, _ := systemctlProbe("is-active", dbReadSocketUnit); strings.TrimSpace(out) != "active" { |
| + t.Fatalf("armed box: carlos-dbread.socket is %q", out) |
| + } |
| + if rep, _ := r.payload("probe", true, ""); rep["capable"] != true { |
| + t.Fatalf("armed box: the agent's probe failed: %v", rep) |
| + } |
| + tenantReach(t, instance) |
| + |
| + for _, off := range [][]string{{"CARLOS_DB_READS=0"}, {"CARLOS_HIBERNATE_BUCKET="}} { |
| + if err := os.WriteFile("/etc/carlos/host.env", []byte(dbreadRunnerHostEnv(r.rootPub, off...)), 0o640); err != nil { |
| + t.Fatal(err) |
| + } |
| + ensureSystemUnits("/etc", filepath.Join(t.TempDir(), "lock"), runSystemctl, systemctlProbe, t.Logf) |
| + active, _ := systemctlProbe("is-active", dbReadSocketUnit) |
| + enabled, _ := systemctlProbe("is-enabled", dbReadSocketUnit) |
| + if strings.TrimSpace(active) == "active" || strings.TrimSpace(enabled) == "enabled" { |
| + t.Fatalf("%v: carlos-dbread.socket is %s and %s", off, strings.TrimSpace(active), strings.TrimSpace(enabled)) |
| + } |
| + if rep, _ := r.payload("probe", true, ""); rep["capable"] == true { |
| + t.Fatalf("%v: something still answers on the worker socket: %v", off, rep) |
| + } |
| + tenantReach(t, instance) |
| + } |
| +} |