| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "log/slog" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/alta/console" |
| 18 | "amadan.net/oficina/alta/console/fakeconsole" |
| 19 | "amadan.net/oficina/alta/homeop" |
| 20 | "amadan.net/oficina/alta/homeop/fakehome" |
| 21 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | ownerCanary = "till-owner-canary@lagged.test" |
| 26 | credOrigin = "https://id.oficina.works" |
| 27 | dirOrigin = "https://oficina.works" |
| 28 | account = "bds" |
| 29 | ) |
| 30 | |
| 31 | type harness struct { |
| 32 | t *testing.T |
| 33 | now time.Time |
| 34 | home *fakehome.Home |
| 35 | dir *httptest.Server |
| 36 | cred *httptest.Server |
| 37 | con *fakeconsole.Console |
| 38 | key *family.Keypair |
| 39 | pre string |
| 40 | path string |
| 41 | store *Store |
| 42 | tokens map[string]string |
| 43 | logs *bytes.Buffer |
| 44 | } |
| 45 | |
| 46 | func newHarness(t *testing.T) *harness { |
| 47 | t.Helper() |
| 48 | h := &harness{t: t, now: time.Unix(2_000_000_000, 0), logs: &bytes.Buffer{}} |
| 49 | var err error |
| 50 | if h.key, err = family.Generate(); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | h.pre = homeop.Token() |
| 54 | h.home = fakehome.New(h.key.SignPub(), h.pre, credOrigin, dirOrigin, h.clock) |
| 55 | h.home.AddPerson(ownerCanary) |
| 56 | h.dir = httptest.NewServer(h.home.Handler(homeop.Directory)) |
| 57 | h.cred = httptest.NewServer(h.home.Handler(homeop.Credential)) |
| 58 | t.Cleanup(h.dir.Close) |
| 59 | t.Cleanup(h.cred.Close) |
| 60 | h.con = fakeconsole.New(account, "alta") |
| 61 | t.Cleanup(h.con.Close) |
| 62 | h.tokens = map[string]string{} |
| 63 | for _, a := range Apps { |
| 64 | h.tokens[a.Slug] = h.con.Mint(a.Slug) |
| 65 | h.con.Versions[a.Slug] = "0123456789abcdef0123456789abcdef01234567" |
| 66 | h.con.MailPrefix[a.Slug] = a.MailPrefixes[0] |
| 67 | } |
| 68 | h.con.Relay["calls"] = true |
| 69 | h.path = filepath.Join(t.TempDir(), "alta.db") |
| 70 | db, err := OpenDB(h.path) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | t.Cleanup(func() { db.Close() }) |
| 75 | if h.store, err = NewStore(context.Background(), db, bytes.Repeat([]byte{4}, 32), h.clock); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | return h |
| 79 | } |
| 80 | |
| 81 | func (h *harness) clock() time.Time { return h.now } |
| 82 | func (h *harness) tick(d time.Duration) { h.now = h.now.Add(d) } |
| 83 | func (h *harness) ctx() context.Context { return context.Background() } |
| 84 | |
| 85 | func (h *harness) engine(hook func(string) error) *Engine { |
| 86 | roles := map[string]homeop.Role{} |
| 87 | for _, r := range []string{homeop.Credential, homeop.Directory} { |
| 88 | p := h.home.Pins(r) |
| 89 | origin, url := credOrigin, h.cred.URL |
| 90 | if r == homeop.Directory { |
| 91 | origin, url = dirOrigin, h.dir.URL |
| 92 | } |
| 93 | roles[r] = homeop.Role{Name: r, Origin: origin, URL: url, Installation: p.Installation, Registry: p.Registry, Device: p.Device} |
| 94 | } |
| 95 | hc := &homeop.Client{Key: h.key, Prefilter: h.pre, Roles: roles, Now: h.clock} |
| 96 | cc := &console.Client{Base: h.con.URL(), Account: account, Tokens: h.tokens, Guard: Guard{Store: h.store, Account: account}} |
| 97 | return New(Config{Store: h.store, Home: hc, Console: cc, Public: &http.Client{Transport: h.con.Transport()}, |
| 98 | HomeCfg: HomeConfig{Issuer: credOrigin, Directory: dirOrigin, KeysBase64: "eyJrMSI6IkJBPT0ifQ==", Provisioner: h.home.ProvisioningPublic()}, |
| 99 | Account: account, Now: h.clock, Logger: slog.New(slog.NewTextHandler(h.logs, nil)), AfterAct: hook, Debug: func(err error) { h.t.Logf("transient: %v", err) }}) |
| 100 | } |
| 101 | |
| 102 | // confirm is Paul on Home's confirm pages, directory first. |
| 103 | func (h *harness) confirm(kind string) { |
| 104 | h.t.Helper() |
| 105 | if err := h.home.Confirm(homeop.Directory, ""); err != nil { |
| 106 | h.t.Fatal(err) |
| 107 | } |
| 108 | if kind == homeop.KindInitialize { |
| 109 | return |
| 110 | } |
| 111 | owner := "" |
| 112 | if kind == homeop.KindCreateTeam { |
| 113 | owner = ownerCanary |
| 114 | } |
| 115 | if err := h.home.Confirm(homeop.Credential, owner); err != nil { |
| 116 | h.t.Fatal(err) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // drive advances run until it is done, failed, or stuck for rounds, |
| 121 | // letting the walks run and time pass a little between passes. |
| 122 | func (h *harness) drive(e *Engine, run int64, rounds int) Run { |
| 123 | h.t.Helper() |
| 124 | var r Run |
| 125 | for i := 0; i < rounds; i++ { |
| 126 | if err := e.Advance(h.ctx(), run); err != nil && !errors.Is(err, errCrash) { |
| 127 | var fl *Fail |
| 128 | if !errors.As(err, &fl) { |
| 129 | h.t.Fatalf("advance: %v", err) |
| 130 | } |
| 131 | } |
| 132 | var err error |
| 133 | if r, err = h.store.Run(h.ctx(), run); err != nil { |
| 134 | h.t.Fatal(err) |
| 135 | } |
| 136 | if r.State == "done" || r.State == "failed" { |
| 137 | return r |
| 138 | } |
| 139 | h.con.Walk() |
| 140 | h.tick(5 * time.Second) |
| 141 | } |
| 142 | return r |
| 143 | } |
| 144 | |
| 145 | // initialized runs the first production run, initialize, and lets its |
| 146 | // window expire so the next plan can be confirmed. |
| 147 | func (h *harness) initialized() { |
| 148 | h.t.Helper() |
| 149 | e := h.engine(nil) |
| 150 | run, err := e.StartInitialize(h.ctx(), InitializeAttestation) |
| 151 | if err != nil { |
| 152 | h.t.Fatal(err) |
| 153 | } |
| 154 | h.confirm(homeop.KindInitialize) |
| 155 | if r := h.drive(e, run, 3); r.State != "done" { |
| 156 | h.t.Fatalf("initialize: %s %s", r.State, r.Code) |
| 157 | } |
| 158 | h.tick(homeop.WindowLifetime * time.Second) |
| 159 | } |
| 160 | |
| 161 | var allApps = []string{"docs", "sheets", "slides", "calendar", "calls", "memoria"} |
| 162 | |
| 163 | func (h *harness) createLagged(e *Engine) int64 { |
| 164 | h.t.Helper() |
| 165 | run, err := e.CreateTeam(h.ctx(), NewTeam{Label: "lagged", Name: "Lagged", Owner: ownerCanary, Apps: allApps}) |
| 166 | if err != nil { |
| 167 | h.t.Fatal(err) |
| 168 | } |
| 169 | h.confirm(homeop.KindCreateTeam) |
| 170 | return run |
| 171 | } |
| 172 | |
| 173 | // scan reports whether needle is anywhere in Alta's database files. |
| 174 | func (h *harness) scan(needle string) bool { |
| 175 | h.t.Helper() |
| 176 | for _, suffix := range []string{"", "-wal", "-shm"} { |
| 177 | f, err := os.Open(h.path + suffix) |
| 178 | if err != nil { |
| 179 | continue |
| 180 | } |
| 181 | b, _ := io.ReadAll(f) |
| 182 | f.Close() |
| 183 | if bytes.Contains(b, []byte(needle)) { |
| 184 | return true |
| 185 | } |
| 186 | } |
| 187 | return false |
| 188 | } |
| 189 | |
| 190 | func b64(s string) []byte { |
| 191 | b, _ := base64.StdEncoding.DecodeString(s) |
| 192 | return b |
| 193 | } |
| 194 | |
| 195 | type fakeVar = fakeconsole.Var |
| 196 | |
| 197 | func sealFor(t *testing.T, h *harness, app, value string) string { |
| 198 | t.Helper() |
| 199 | e, err := console.Seal(h.con.SecretsPub(), "", app, []byte(value)) |
| 200 | if err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | return e |
| 204 | } |
| 205 | |