| 1 | //go:build linux && rootrunner |
| 2 | |
| 3 | package host |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/carlosframework/platform/internal/hostbox" |
| 15 | "github.com/carlosframework/platform/internal/hostclaim" |
| 16 | "github.com/carlosframework/platform/internal/hostguard" |
| 17 | "github.com/carlosframework/platform/internal/provision" |
| 18 | "github.com/carlosframework/platform/internal/registry" |
| 19 | "github.com/carlosframework/platform/internal/release" |
| 20 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 21 | "github.com/carlosframework/platform/internal/rootstate" |
| 22 | "golang.org/x/sys/unix" |
| 23 | ) |
| 24 | |
| 25 | func TestPlan14GenerationRequiredArtifactsRootRunner(t *testing.T) { |
| 26 | plan14RequiredArtifactsRootRunner(t, false) |
| 27 | } |
| 28 | |
| 29 | func TestPlan14BootLocalRecoveryRootRunner(t *testing.T) { |
| 30 | plan14RequiredArtifactsRootRunner(t, true) |
| 31 | } |
| 32 | |
| 33 | func plan14RequiredArtifactsRootRunner(t *testing.T, local bool) { |
| 34 | const childKey = "PLAN14_E30_REQUIRED_CHILD" |
| 35 | dir := os.Getenv(childKey) |
| 36 | if dir == "" { |
| 37 | generationRunnerRegistryUser(t) |
| 38 | var err error |
| 39 | dir, err = os.MkdirTemp("/run", "plan14-generation-rows-") |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | t.Cleanup(func() { |
| 44 | if err := os.RemoveAll(dir); err != nil { |
| 45 | t.Error(err) |
| 46 | } |
| 47 | }) |
| 48 | if err := os.Chmod(dir, 0755); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | binary, err := os.Executable() |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | cmd := exec.CommandContext(t.Context(), "unshare", "--mount", "--pid", "--fork", "--mount-proc", binary, "-test.run=^"+t.Name()+"$", "-test.v", "-test.timeout=2m") |
| 56 | cmd.Dir = dir |
| 57 | cmd.Env = append(os.Environ(), childKey+"="+dir) |
| 58 | if out, err := cmd.CombinedOutput(); err != nil { |
| 59 | t.Fatalf("isolated required-artifact census: %v\n%s", err, out) |
| 60 | } else { |
| 61 | t.Logf("%s", out) |
| 62 | } |
| 63 | return |
| 64 | } |
| 65 | if os.Geteuid() != 0 { |
| 66 | t.Fatal("designated runner must execute as root") |
| 67 | } |
| 68 | if err := unix.Mount("", "/", "", unix.MS_REC|unix.MS_PRIVATE, ""); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | private := filepath.Join(dir, "varlib") |
| 72 | if err := os.Mkdir(private, 0755); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := unix.Mount(private, "/var/lib", "", unix.MS_BIND, ""); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | t.Cleanup(func() { |
| 79 | if err := unix.Unmount("/var/lib", 0); err != nil { |
| 80 | t.Error(err) |
| 81 | } |
| 82 | }) |
| 83 | t.Setenv("CARLOS_HOST_CLAIM", "true") |
| 84 | t.Setenv("CARLOS_OPT_DIR", dir) |
| 85 | t.Setenv("CARLOS_DATA_DIR", filepath.Join(dir, "data")) |
| 86 | t.Setenv("CARLOS_DEPLOYMENT_BUCKET", "") |
| 87 | t.Setenv("CARLOS_DEPLOYMENT_DIR", filepath.Join(dir, "deployment")) |
| 88 | for _, path := range []string{filepath.Join(dir, "versions"), dataDir(), os.Getenv("CARLOS_DEPLOYMENT_DIR")} { |
| 89 | if err := os.Mkdir(path, 0755); err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | } |
| 93 | uid, gid := generationRunnerRegistryUser(t) |
| 94 | if err := os.Chown(dataDir(), uid, gid); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | reg, err := openRegistryChild(filepath.Join(dataDir(), "registry.db"), uid, gid) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | // Join the fixture writer before the production kernel scan. |
| 102 | if err := reg.Close(); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | for _, path := range []string{rootparticipation.Namespace, "/var/lib/carlos-claims/authority/fences", release.RootGenerationsDir} { |
| 106 | state, err := rootstate.Open(path, true, false) |
| 107 | if err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | state.Close() |
| 111 | } |
| 112 | write := func(path, name string, v any) { |
| 113 | t.Helper() |
| 114 | state, err := rootstate.Open(path, false, false) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | defer state.Close() |
| 119 | raw, err := json.Marshal(v) |
| 120 | if err == nil { |
| 121 | err = state.Write(name, raw) |
| 122 | } |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | } |
| 127 | write(rootparticipation.Namespace, "census.json", rootparticipation.Census{Schema: 1, Box: "runner", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true}) |
| 128 | if info, err := os.Stat("/var/lib/carlos-claims"); err != nil || info.Mode().Perm() != 0700 { |
| 129 | t.Fatalf("private journal directory: %v %v", info, err) |
| 130 | } |
| 131 | generationRunnerEmptyRegistry(t, dir, uid, gid) |
| 132 | generationRunnerCreation(t, dir) |
| 133 | rec := provision.Record{Schema: 1, Host: "retained.example", Account: "bab", App: "shop", Channel: "stable", CreatedAt: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)} |
| 134 | store, err := openDeployStore(t.Context()) |
| 135 | if err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | plan14PutRecord(t, store, rec) |
| 139 | inc, err := hostclaim.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| 140 | if err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | in := hostguard.RootInstallation{Artifacts: hostguard.InstallationArtifacts{Schema: 1, StateDirectory: true, Timers: "backup"}, Host: rec.Host, Incarnation: inc, Stamp: 5, Account: rec.Account, App: rec.App, Record: provision.RecordKey(rec.Account, rec.App, rec.Host), Box: "runner", Enrollment: "en", Migration: 1, InventoryRevision: 1, Empty: true} |
| 144 | write("/var/lib/carlos-claims/authority/fences", in.Host+".json", map[string]any{ |
| 145 | "Schema": 1, "Installation": in, "Retired": []hostguard.Captured{{Incarnation: inc, Stamp: 2}}, |
| 146 | "State": map[string]any{"v": 1, "host": in.Host, "incarnation": inc, "active_stamp": 5, "retired_stamps": []uint64{2}}, |
| 147 | }) |
| 148 | path := provision.InstanceDir(dataDir(), in.Host) |
| 149 | if err := os.MkdirAll(path, 0700); err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | var st unix.Stat_t |
| 153 | if err := unix.Stat(path, &st); err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | rel, err := filepath.Rel(dataDir(), path) |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | payload := hostbox.StopInventory{Complete: true, Host: in.Host, Incarnation: inc, InstallationStamp: 2, Box: in.Box, Enrollment: in.Enrollment, DirectoryRevision: "1", Entries: []hostbox.StopInventoryEntry{ |
| 161 | {Kind: "route", ArtifactID: "route", ManagedRoot: "registry", RelativePath: in.Host, RowIdentity: "row", Ownership: "owner", Materialization: "materialized"}, |
| 162 | {Identity: inventoryIdentityFixture(t, dataDir(), rel), Kind: "path", ArtifactID: "state", ManagedRoot: "data", RelativePath: rel, Ownership: "owner", Materialization: "materialized", Device: uint64(st.Dev), Inode: st.Ino, Type: unix.S_IFDIR}, |
| 163 | }} |
| 164 | journal, err := hostbox.OpenLaunchJournal("/var/lib/carlos-claims/journals.db") |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | t.Cleanup(func() { journal.Close() }) |
| 169 | service, err := hostbox.NewJournalService(journal) |
| 170 | if err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | current := payload |
| 174 | current.InstallationStamp, current.Entries = in.Stamp, nil |
| 175 | if err := service.Inventory.WriteStopInventory(t.Context(), current); err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | put := func(p hostbox.StopInventory) { |
| 179 | t.Helper() |
| 180 | if err := service.Inventory.WriteStopInventory(t.Context(), p); err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | } |
| 184 | payload.Entries = append(payload.Entries, hostbox.StopInventoryEntry{Kind: "timer", ArtifactID: "timer", ManagedRoot: "registry", RelativePath: in.Host + "/backup", RowIdentity: "timer", Ownership: "owner", Materialization: "materialized"}) |
| 185 | put(payload) |
| 186 | owner := registry.ClaimOwnership{ClaimIncarnation: inc, ClaimStamp: 2, ClaimOwner: "owner", ClaimRowID: "row"} |
| 187 | route := registry.Route{Host: in.Host, ClaimOwnership: owner} |
| 188 | timer := registry.Timer{Host: in.Host, Name: "backup", Kind: "cron", Registration: "timer", ClaimOwnership: owner} |
| 189 | timer.ClaimRowID = "timer" |
| 190 | rows := func(rt registry.Route, timers []registry.Timer) { |
| 191 | t.Helper() |
| 192 | writer, err := openRegistryChild(filepath.Join(dataDir(), "registry.db"), uid, gid) |
| 193 | if err != nil { |
| 194 | t.Fatal(err) |
| 195 | } |
| 196 | defer writer.Close() |
| 197 | if err := writer.Delete(in.Host); err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | if err := writer.ReplaceTimers(in.Host, timers); err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | if rt.Host != "" { |
| 204 | if err := writer.Upsert(rt); err != nil { |
| 205 | t.Fatal(err) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | rows(route, []registry.Timer{timer}) |
| 210 | certificate := func() release.GenerationCompleteness { |
| 211 | t.Helper() |
| 212 | state, err := rootstate.Open(release.RootGenerationsDir, false, false) |
| 213 | if err != nil { |
| 214 | t.Fatal(err) |
| 215 | } |
| 216 | defer state.Close() |
| 217 | c, err := (&release.RootGenerations{Store: state}).Completeness() |
| 218 | if err != nil { |
| 219 | t.Fatal(err) |
| 220 | } |
| 221 | return c |
| 222 | } |
| 223 | source := productionRootGenerationPins() |
| 224 | if local { |
| 225 | t.Setenv("CARLOS_DEPLOYMENT_DIR", "") |
| 226 | t.Setenv("CARLOS_DEPLOYMENT_BUCKET", "") |
| 227 | if err := registerProductionLaunchVerifier(); err != nil { |
| 228 | t.Fatal(err) |
| 229 | } |
| 230 | source = generationPinSource{open: func() (rootstate.Store, error) { return rootstate.Open(release.RootGenerationsDir, false, false) }, reconcile: (generationReconciler{read: productionLocalGenerationInventory, kernel: productionGenerationKernelCensus}).reconcile} |
| 231 | } |
| 232 | |
| 233 | pinsUnderRelease := func() (release.StagedPaths, error) { |
| 234 | var pins release.StagedPaths |
| 235 | err := withClaimRootRelease(t.Context(), true, func() (rootstate.Store, error) { return rootstate.Open(rootReleasePrivate, false, false) }, func() error { |
| 236 | var err error |
| 237 | pins, err = source.StagedPins(t.Context(), dir) |
| 238 | return err |
| 239 | }) |
| 240 | return pins, err |
| 241 | } |
| 242 | floorIdle := func() error { |
| 243 | l, err := rootparticipation.Open() |
| 244 | if err != nil { |
| 245 | return err |
| 246 | } |
| 247 | defer l.State.Close() |
| 248 | return l.WithLaunchExclusion(t.Context(), func(ctx context.Context) error { |
| 249 | return l.WithTransaction(ctx, rootFloorDowngradeIdle) |
| 250 | }) |
| 251 | } |
| 252 | accept := func() { |
| 253 | t.Helper() |
| 254 | if pins, err := pinsUnderRelease(); err != nil || len(pins) != 0 || !certificate().Complete { |
| 255 | snapshot, readErr := generationInventoryAt(t.Context(), rootparticipation.Namespace, "/var/lib/carlos-claims/authority/fences", "/var/lib/carlos-claims/journals.db") |
| 256 | t.Logf("root inventory: %v", readErr) |
| 257 | if readErr == nil { |
| 258 | snapshot, readErr = generationRequiredInventory(t.Context(), store, snapshot, productionGenerationCreation) |
| 259 | t.Logf("required artifacts: %v", readErr) |
| 260 | } |
| 261 | if readErr == nil { |
| 262 | _, readErr = productionGenerationRegistryInventory(t.Context(), snapshot) |
| 263 | t.Logf("registry artifacts: %v", readErr) |
| 264 | } |
| 265 | _, _, kernelErr := productionGenerationKernelCensus(t.Context()) |
| 266 | t.Logf("kernel census: %v", kernelErr) |
| 267 | t.Fatalf("production required artifacts: %v %v", pins, err) |
| 268 | } |
| 269 | } |
| 270 | accept() |
| 271 | if local && floorIdle() != nil { |
| 272 | t.Fatal("local floor recovery refused complete offline obligations") |
| 273 | } |
| 274 | if local { |
| 275 | state, err := rootstate.Open(rootparticipation.Namespace, false, false) |
| 276 | if err != nil { |
| 277 | t.Fatal(err) |
| 278 | } |
| 279 | defer state.Close() |
| 280 | l := &rootparticipation.Ledger{State: state, VerifyLaunches: (rootLaunchVerifier{exclude: participationRelease, open: func() (rootstate.Store, error) { return rootstate.Open(release.RootGenerationsDir, false, false) }, reconcile: generationReconciler{read: productionLocalGenerationInventory, kernel: productionGenerationKernelCensus}}).verify, ExcludeLaunches: participationRelease.With} |
| 281 | a := &systemApplier{participation: l, dataDir: dataDir()} |
| 282 | if err := a.controllersRecovered(context.Background()); err != nil { |
| 283 | t.Fatal("boot participation recovery without deployment storage", err) |
| 284 | } |
| 285 | } |
| 286 | for _, omit := range []string{"route", "state", "timer", "historical-state", "timer-both"} { |
| 287 | t.Run(omit, func(t *testing.T) { |
| 288 | initial := certificate() |
| 289 | omitEntry := omit |
| 290 | if omit == "historical-state" { |
| 291 | omitEntry = "state" |
| 292 | changed := rec |
| 293 | changed.Kind, changed.Addr = "service", "127.0.0.1:8080" |
| 294 | plan14PutRecord(t, store, changed) |
| 295 | } |
| 296 | if omit == "timer-both" { |
| 297 | omitEntry = "timer" |
| 298 | rows(route, nil) |
| 299 | } |
| 300 | missing := payload |
| 301 | missing.Entries = nil |
| 302 | for _, entry := range payload.Entries { |
| 303 | if entry.ArtifactID != omitEntry { |
| 304 | missing.Entries = append(missing.Entries, entry) |
| 305 | } |
| 306 | } |
| 307 | put(missing) |
| 308 | if local && floorIdle() == nil { |
| 309 | t.Fatal("local floor recovery accepted omitted obligation", omit) |
| 310 | } |
| 311 | if pins, err := pinsUnderRelease(); err == nil || pins != nil { |
| 312 | t.Fatalf("production omitted %s certified: %v %v", omit, pins, err) |
| 313 | } |
| 314 | if c := certificate(); c.Complete || c.Revision <= initial.Revision { |
| 315 | t.Fatal("omission retained the completeness certificate") |
| 316 | } |
| 317 | if err := generationPayloadFiles(t.Context(), dataDir(), []hostbox.GenerationPayload{{Inventory: payload}}); err != nil { |
| 318 | t.Fatalf("omission damaged retained payload: %v", err) |
| 319 | } |
| 320 | put(payload) |
| 321 | plan14PutRecord(t, store, rec) |
| 322 | rows(route, []registry.Timer{timer}) |
| 323 | accept() |
| 324 | }) |
| 325 | } |
| 326 | for _, attack := range []string{"unknown-route-host", "unknown-timer-host", "route-missing", "route-owner", "timer-missing", "timer-registration", "extra-timer", "malformed-database"} { |
| 327 | t.Run(attack, func(t *testing.T) { |
| 328 | initial := certificate() |
| 329 | rt, tm := route, timer |
| 330 | switch attack { |
| 331 | case "route-missing": |
| 332 | rt = registry.Route{} |
| 333 | case "route-owner": |
| 334 | rt.ClaimOwner = "forged" |
| 335 | case "timer-registration": |
| 336 | tm.Registration = "forged" |
| 337 | } |
| 338 | timers := []registry.Timer{tm} |
| 339 | if attack == "timer-missing" { |
| 340 | timers = nil |
| 341 | } |
| 342 | if attack == "extra-timer" { |
| 343 | extra := timer |
| 344 | extra.Name, extra.Registration, extra.ClaimRowID = "extra", "extra", "extra" |
| 345 | timers = append(timers, extra) |
| 346 | } |
| 347 | cleanupUnknown := func() {} |
| 348 | rows(rt, timers) |
| 349 | if attack == "unknown-route-host" || attack == "unknown-timer-host" { |
| 350 | writer, err := openRegistryChild(filepath.Join(dataDir(), "registry.db"), uid, gid) |
| 351 | if err != nil { |
| 352 | t.Fatal(err) |
| 353 | } |
| 354 | if attack == "unknown-route-host" { |
| 355 | err = writer.Upsert(registry.Route{Host: "omitted.example"}) |
| 356 | } else { |
| 357 | err = writer.ReplaceTimers("omitted.example", []registry.Timer{{Host: "omitted.example", Name: "paused", Kind: "cron", NextAt: 0}}) |
| 358 | } |
| 359 | closeErr := writer.Close() |
| 360 | if err != nil || closeErr != nil { |
| 361 | t.Fatalf("unknown host setup: %v %v", err, closeErr) |
| 362 | } |
| 363 | cleanupUnknown = func() { |
| 364 | writer, err := openRegistryChild(filepath.Join(dataDir(), "registry.db"), uid, gid) |
| 365 | if err != nil { |
| 366 | t.Fatal(err) |
| 367 | } |
| 368 | defer writer.Close() |
| 369 | if err := writer.Delete("omitted.example"); err != nil { |
| 370 | t.Fatal(err) |
| 371 | } |
| 372 | if err := writer.ReplaceTimers("omitted.example", nil); err != nil { |
| 373 | t.Fatal(err) |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | path := filepath.Join(dataDir(), "registry.db") |
| 378 | if attack == "malformed-database" { |
| 379 | if err := os.Rename(path, path+".saved"); err != nil { |
| 380 | t.Fatal(err) |
| 381 | } |
| 382 | if err := os.WriteFile(path, []byte("not sqlite"), 0644); err != nil { |
| 383 | t.Fatal(err) |
| 384 | } |
| 385 | if err := os.Chown(path, uid, gid); err != nil { |
| 386 | t.Fatal(err) |
| 387 | } |
| 388 | } |
| 389 | if pins, err := pinsUnderRelease(); err == nil || pins != nil { |
| 390 | t.Fatalf("production %s certified: %v %v", attack, pins, err) |
| 391 | } |
| 392 | if c := certificate(); c.Complete || c.Revision <= initial.Revision { |
| 393 | t.Fatal("row mismatch retained certificate") |
| 394 | } |
| 395 | if attack == "malformed-database" { |
| 396 | if err := os.Remove(path); err != nil { |
| 397 | t.Fatal(err) |
| 398 | } |
| 399 | if err := os.Rename(path+".saved", path); err != nil { |
| 400 | t.Fatal(err) |
| 401 | } |
| 402 | } |
| 403 | cleanupUnknown() |
| 404 | rows(route, []registry.Timer{timer}) |
| 405 | accept() |
| 406 | }) |
| 407 | } |
| 408 | |
| 409 | } |
| 410 | |
| 411 | func generationRunnerEmptyRegistry(t *testing.T, opt string, uid, gid int) { |
| 412 | t.Helper() |
| 413 | journal, err := hostbox.OpenLaunchJournal("/var/lib/carlos-claims/journals.db") |
| 414 | if err != nil { |
| 415 | t.Fatal(err) |
| 416 | } |
| 417 | if _, err := hostbox.NewJournalService(journal); err != nil { |
| 418 | t.Fatal(err) |
| 419 | } |
| 420 | if err := journal.Close(); err != nil { |
| 421 | t.Fatal(err) |
| 422 | } |
| 423 | source := productionRootGenerationPins() |
| 424 | |
| 425 | check := func(t *testing.T, refuse bool) { |
| 426 | t.Helper() |
| 427 | err := withClaimRootRelease(t.Context(), true, func() (rootstate.Store, error) { return rootstate.Open(rootReleasePrivate, false, false) }, func() error { |
| 428 | pins, err := source.StagedPins(t.Context(), opt) |
| 429 | if len(pins) != 0 { |
| 430 | t.Fatalf("empty fixture has pins: %v", pins) |
| 431 | } |
| 432 | return err |
| 433 | }) |
| 434 | if (err != nil) != refuse { |
| 435 | t.Fatalf("empty-root registry census: refuse=%v error=%v", refuse, err) |
| 436 | } |
| 437 | state, err := rootstate.Open(release.RootGenerationsDir, false, false) |
| 438 | if err != nil { |
| 439 | t.Fatal(err) |
| 440 | } |
| 441 | defer state.Close() |
| 442 | c, err := (&release.RootGenerations{Store: state}).Completeness() |
| 443 | if err != nil || c.Complete == refuse { |
| 444 | t.Fatalf("empty-root certificate: %+v %v", c, err) |
| 445 | } |
| 446 | } |
| 447 | check(t, false) |
| 448 | for _, kind := range []string{"route", "timer"} { |
| 449 | t.Run("empty-root-unknown-"+kind, func(t *testing.T) { |
| 450 | writer, err := openRegistryChild(filepath.Join(dataDir(), "registry.db"), uid, gid) |
| 451 | if err != nil { |
| 452 | t.Fatal(err) |
| 453 | } |
| 454 | if kind == "route" { |
| 455 | err = writer.Upsert(registry.Route{Host: "omitted.example"}) |
| 456 | } else { |
| 457 | err = writer.ReplaceTimers("omitted.example", []registry.Timer{{Host: "omitted.example", Name: "paused", Kind: "cron", NextAt: 0}}) |
| 458 | } |
| 459 | closeErr := writer.Close() |
| 460 | if err != nil || closeErr != nil { |
| 461 | t.Fatalf("unknown host setup: %v %v", err, closeErr) |
| 462 | } |
| 463 | check(t, true) |
| 464 | writer, err = openRegistryChild(filepath.Join(dataDir(), "registry.db"), uid, gid) |
| 465 | if err != nil { |
| 466 | t.Fatal(err) |
| 467 | } |
| 468 | if err := writer.Delete("omitted.example"); err != nil { |
| 469 | t.Fatal(err) |
| 470 | } |
| 471 | if err := writer.ReplaceTimers("omitted.example", nil); err != nil { |
| 472 | t.Fatal(err) |
| 473 | } |
| 474 | if err := writer.Close(); err != nil { |
| 475 | t.Fatal(err) |
| 476 | } |
| 477 | check(t, false) |
| 478 | }) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | func generationRunnerCreation(t *testing.T, opt string) { |
| 483 | t.Helper() |
| 484 | ctx := t.Context() |
| 485 | store, err := openDeployStore(ctx) |
| 486 | if err != nil { |
| 487 | t.Fatal(err) |
| 488 | } |
| 489 | rec := provision.Record{Schema: 1, Host: "new.example", Account: "bab", App: "shop", Channel: "stable", CreatedAt: time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)} |
| 490 | plan14PutRecord(t, store, rec) |
| 491 | inc, err := hostclaim.Incarnation(rec.Account, rec.App, rec.Host, rec.CreatedAt) |
| 492 | if err != nil { |
| 493 | t.Fatal(err) |
| 494 | } |
| 495 | in := hostguard.RootInstallation{Artifacts: hostguard.InstallationArtifacts{Schema: 1, StateDirectory: true}, Host: rec.Host, Account: rec.Account, App: rec.App, Record: provision.RecordKey(rec.Account, rec.App, rec.Host), Incarnation: inc, Stamp: 1, Box: "runner", Enrollment: "en", Migration: 1, InventoryRevision: 1, Empty: true} |
| 496 | fences, err := rootstate.Open("/var/lib/carlos-claims/authority/fences", false, false) |
| 497 | if err != nil { |
| 498 | t.Fatal(err) |
| 499 | } |
| 500 | defer fences.Close() |
| 501 | raw, err := json.Marshal(map[string]any{"Schema": 1, "Installation": in, "State": map[string]any{"v": 1, "host": in.Host, "incarnation": inc, "active_stamp": in.Stamp}}) |
| 502 | if err != nil { |
| 503 | t.Fatal(err) |
| 504 | } |
| 505 | if err := fences.Write(in.Host+".json", raw); err != nil { |
| 506 | t.Fatal(err) |
| 507 | } |
| 508 | journal, err := hostbox.OpenLaunchJournal("/var/lib/carlos-claims/journals.db") |
| 509 | if err != nil { |
| 510 | t.Fatal(err) |
| 511 | } |
| 512 | service, err := hostbox.NewJournalService(journal) |
| 513 | if err != nil { |
| 514 | t.Fatal(err) |
| 515 | } |
| 516 | if err := service.Inventory.WriteStopInventory(ctx, hostbox.StopInventory{Complete: true, Host: in.Host, Incarnation: inc, InstallationStamp: in.Stamp, Box: in.Box, Enrollment: in.Enrollment, DirectoryRevision: "1"}); err != nil { |
| 517 | t.Fatal(err) |
| 518 | } |
| 519 | if err := journal.Close(); err != nil { |
| 520 | t.Fatal(err) |
| 521 | } |
| 522 | historyState, err := rootstate.Open(release.RootHistoryDir, true, false) |
| 523 | if err != nil { |
| 524 | t.Fatal(err) |
| 525 | } |
| 526 | defer historyState.Close() |
| 527 | history := &release.RootHistory{Store: historyState} |
| 528 | binding := release.HistoryBinding{Host: in.Host, Account: in.Account, App: in.App} |
| 529 | check := func(refuse bool) { |
| 530 | t.Helper() |
| 531 | err := withClaimRootRelease(ctx, true, func() (rootstate.Store, error) { return rootstate.Open(rootReleasePrivate, false, false) }, func() error { |
| 532 | pins, err := productionRootGenerationPins().StagedPins(ctx, opt) |
| 533 | if len(pins) != 0 { |
| 534 | t.Fatalf("creation fixture has pins: %v", pins) |
| 535 | } |
| 536 | return err |
| 537 | }) |
| 538 | if (err != nil) != refuse { |
| 539 | t.Fatalf("creation census: refuse=%v error=%v", refuse, err) |
| 540 | } |
| 541 | state, err := rootstate.Open(release.RootGenerationsDir, false, false) |
| 542 | if err != nil { |
| 543 | t.Fatal(err) |
| 544 | } |
| 545 | defer state.Close() |
| 546 | c, err := (&release.RootGenerations{Store: state}).Completeness() |
| 547 | if err != nil || c.Complete == refuse { |
| 548 | t.Fatalf("creation certificate: %+v %v", c, err) |
| 549 | } |
| 550 | } |
| 551 | check(true) |
| 552 | if err := history.RecordCreation(binding, rootHistoryInstallation(in)); err != nil { |
| 553 | t.Fatal(err) |
| 554 | } |
| 555 | check(false) |
| 556 | names, err := historyState.Names() |
| 557 | if err != nil || len(names) != 1 { |
| 558 | t.Fatalf("creation files: %v %v", names, err) |
| 559 | } |
| 560 | saved, err := historyState.Read(names[0]) |
| 561 | if err != nil { |
| 562 | t.Fatal(err) |
| 563 | } |
| 564 | for _, attack := range []string{"erased", "corrupt", "consumed", "wrong-installation"} { |
| 565 | t.Run("creation-"+attack, func(t *testing.T) { |
| 566 | switch attack { |
| 567 | case "erased", "wrong-installation": |
| 568 | if err := historyState.Remove(names[0]); err != nil { |
| 569 | t.Fatal(err) |
| 570 | } |
| 571 | if attack == "wrong-installation" { |
| 572 | if err := history.RecordCreation(binding, "other"); err != nil { |
| 573 | t.Fatal(err) |
| 574 | } |
| 575 | } |
| 576 | case "corrupt": |
| 577 | if err := historyState.Write(names[0], []byte("{")); err != nil { |
| 578 | t.Fatal(err) |
| 579 | } |
| 580 | case "consumed": |
| 581 | if err := history.ConsumeCreation(binding); err != nil { |
| 582 | t.Fatal(err) |
| 583 | } |
| 584 | } |
| 585 | check(true) |
| 586 | if err := historyState.Write(names[0], saved); err != nil { |
| 587 | t.Fatal(err) |
| 588 | } |
| 589 | check(false) |
| 590 | }) |
| 591 | } |
| 592 | // This fixture owns the isolated stores. Remove its complete fixture before |
| 593 | // the caller installs its separate predecessor case. |
| 594 | if err := fences.Remove(in.Host + ".json"); err != nil { |
| 595 | t.Fatal(err) |
| 596 | } |
| 597 | if err := historyState.Remove(names[0]); err != nil { |
| 598 | t.Fatal(err) |
| 599 | } |
| 600 | if err := os.Remove("/var/lib/carlos-claims/journals.db"); err != nil { |
| 601 | t.Fatal(err) |
| 602 | } |
| 603 | if err := store.Delete(ctx, in.Record); err != nil { |
| 604 | t.Fatal(err) |
| 605 | } |
| 606 | } |
| 607 | |