| 1 | //go:build unix |
| 2 | |
| 3 | package hostguard |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "os" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestPlan14RetiredInstallationEnumeration(t *testing.T) { |
| 14 | for _, oldFormat := range []bool{false, true} { |
| 15 | t.Run(map[bool]string{false: "retained", true: "old-cleared-stamp"}[oldFormat], func(t *testing.T) { |
| 16 | f, target, captured := fenceFixture(t) |
| 17 | store := &rootTestStore{dir: t.TempDir()} |
| 18 | f.g.Root = NewRootAuthority(store) |
| 19 | f.obs.Claim.Holder.Account, f.obs.Claim.Holder.App = "bab", "app" |
| 20 | in := RootInstallation{Host: target.Host, Incarnation: captured.Incarnation, Stamp: captured.Stamp, Account: "bab", App: "app", Record: "record", Box: target.Box, Enrollment: target.Enrollment, Migration: 1, InventoryRevision: 7, Artifacts: InstallationArtifacts{Schema: 1, StateDirectory: true, Timers: "kept"}} |
| 21 | if err := f.g.EstablishRoot(t.Context(), in, allow); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if retired, err := f.g.Root.HostRetired(t.Context(), target.Host); err != nil || retired { |
| 25 | t.Fatal("active host marked retired", retired, err) |
| 26 | } |
| 27 | if err := f.g.RetireInstallationWith(t.Context(), target.Host, captured, allow, func() error { return os.ErrPermission }); !errors.Is(err, os.ErrPermission) { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if oldFormat { |
| 31 | raw, err := store.Read(target.Host + ".json") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | var r rootRecord |
| 36 | if err := json.Unmarshal(raw, &r); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | r.Installation.Stamp = 0 |
| 40 | raw, _ = json.Marshal(r) |
| 41 | if err := store.Write(target.Host+".json", raw); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | } |
| 45 | f.g.Root = NewRootAuthority(&rootTestStore{dir: store.dir}) |
| 46 | if retired, err := f.g.Root.HostRetired(t.Context(), target.Host); err != nil || !retired { |
| 47 | t.Fatal("retired host unavailable", retired, err) |
| 48 | } |
| 49 | for _, enumerate := range []func(context.Context) ([]FenceView, error){f.g.Root.MigrationInstallations, f.g.Root.GenerationInstallations} { |
| 50 | views, err := enumerate(t.Context()) |
| 51 | if err != nil || len(views) != 1 || views[0].Installation != in { |
| 52 | t.Fatal("retired inventory lost", views, err) |
| 53 | } |
| 54 | if err := views[0].admit(target.Host, captured, target.Session); err == nil { |
| 55 | t.Fatal("retired view admitted") |
| 56 | } |
| 57 | } |
| 58 | if err := f.g.With(t.Context(), target.Host, captured, allow, func() error { t.Fatal("retired execution"); return nil }); err == nil { |
| 59 | t.Fatal("retired execution admitted") |
| 60 | } |
| 61 | if err := f.g.RetireInstallationWith(t.Context(), target.Host, captured, allow, func() error { return nil }); err != nil { |
| 62 | t.Fatal("cleanup retry", err) |
| 63 | } |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |