| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/home/roster" |
| 14 | "amadan.net/oficina/home/teammode" |
| 15 | assertioncrypto "amadan.net/rastrillo/rastrillo/crypto" |
| 16 | "amadan.net/rastrillo/rastrillo/migrate" |
| 17 | ) |
| 18 | |
| 19 | // tableExists reports whether path's database has table, read without the |
| 20 | // app. |
| 21 | func tableExists(t *testing.T, path, table string) bool { |
| 22 | t.Helper() |
| 23 | d := openTeamDB(t, path) |
| 24 | var n int |
| 25 | if err := d.Writer().QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", table).Scan(&n); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | return n == 1 |
| 29 | } |
| 30 | |
| 31 | // markerFor reports whether path holds an activation marker. |
| 32 | func markerFor(t *testing.T, path string) bool { |
| 33 | t.Helper() |
| 34 | d := openTeamDB(t, path) |
| 35 | _, activated, err := teamState.Required(context.Background(), d.Writer()) |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | return activated |
| 40 | } |
| 41 | |
| 42 | // bootAt builds the app over path with cfg, returning the error. |
| 43 | func bootAt(t *testing.T, path string, cfg Config) error { |
| 44 | t.Helper() |
| 45 | d := openTeamDB(t, path) |
| 46 | _, _, err := buildForTest(t, d, cfg) |
| 47 | return err |
| 48 | } |
| 49 | |
| 50 | func TestTeamEnvNamesAreTheFive(t *testing.T) { |
| 51 | want := [5]string{"CALLS_TEAM_ID", "CALLS_TEAM_DIRECTORY_ORIGIN", "CALLS_TEAM_PROVISIONING_BASE64", "CALLS_TEAM_OPERATOR_KEYS_BASE64", "CALLS_TEAM_ROSTER_TOKEN"} |
| 52 | if got := teammode.EnvNames("CALLS"); got != want { |
| 53 | t.Fatalf("CALLS team variables %v", got) |
| 54 | } |
| 55 | if teammode.ConfigFromLookup("CALLS", func(string) string { return "" }) != nil { |
| 56 | t.Fatal("no team variables read as team mode") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Every binding the provisioning record names is pinned: one signed for |
| 61 | // another issuer, consumer, app, origin, team or directory refuses boot |
| 62 | // before anything migrates, and writes no marker. |
| 63 | func TestTeamConfigPinsTheProvisioningRecord(t *testing.T) { |
| 64 | key, err := assertioncrypto.Generate() |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | for name, mutate := range map[string]func(*roster.Bindings){ |
| 69 | "issuer": func(b *roster.Bindings) { b.Issuer = "https://id.elsewhere.test" }, |
| 70 | "consumer": func(b *roster.Bindings) { b.Consumer = "someone-else" }, |
| 71 | "app": func(b *roster.Bindings) { b.App = "calendar" }, |
| 72 | "origin": func(b *roster.Bindings) { b.Audience = "https://calls.elsewhere.test" }, |
| 73 | "team": func(b *roster.Bindings) { b.Team = "TeamB" }, |
| 74 | "directory": func(b *roster.Bindings) { b.Directory = "https://home.elsewhere.test" }, |
| 75 | } { |
| 76 | t.Run(name, func(t *testing.T) { |
| 77 | b := teamTestBindings() |
| 78 | mutate(&b) |
| 79 | cfg, _, _ := teamConfigFor(t, b, key) |
| 80 | if name == "team" || name == "directory" { |
| 81 | // The record names the team and directory; the |
| 82 | // instance's own configuration must name the same ones. |
| 83 | good, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 84 | good.Team.ProvisioningBase64 = cfg.Team.ProvisioningBase64 |
| 85 | good.Team.OperatorKeysBase64 = cfg.Team.OperatorKeysBase64 |
| 86 | cfg = good |
| 87 | } |
| 88 | path := filepath.Join(t.TempDir(), "calls.db") |
| 89 | if err := bootAt(t, path, cfg); !errors.Is(err, teammode.ErrConfig) { |
| 90 | t.Fatalf("a record signed for another %s: %v", name, err) |
| 91 | } |
| 92 | if tableExists(t, path, "persons") || markerFor(t, path) { |
| 93 | t.Fatalf("a refused %s migrated or wrote the marker", name) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestTeamEnvIsAllOrNoneAndNeedsHome(t *testing.T) { |
| 100 | key, _ := assertioncrypto.Generate() |
| 101 | for _, name := range teammode.EnvNames("CALLS") { |
| 102 | env := map[string]string{} |
| 103 | for _, n := range teammode.EnvNames("CALLS") { |
| 104 | env[n] = "x" |
| 105 | } |
| 106 | delete(env, name) |
| 107 | c := teammode.ConfigFromLookup("CALLS", func(k string) string { return env[k] }) |
| 108 | if c == nil { |
| 109 | t.Fatalf("four of five (%s missing) read as team mode off", name) |
| 110 | } |
| 111 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 112 | cfg.Team = c |
| 113 | path := filepath.Join(t.TempDir(), "calls.db") |
| 114 | if err := bootAt(t, path, cfg); err == nil { |
| 115 | t.Fatalf("four of five (%s missing) booted", name) |
| 116 | } |
| 117 | if tableExists(t, path, "persons") { |
| 118 | t.Fatalf("four of five (%s missing) migrated", name) |
| 119 | } |
| 120 | } |
| 121 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 122 | cfg.Home = nil |
| 123 | if err := bootAt(t, filepath.Join(t.TempDir(), "calls.db"), cfg); err == nil { |
| 124 | t.Fatal("team mode booted without Home") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestTeamTheAllowlistBecomesOptionalAndAdmitsOnlyLocally(t *testing.T) { |
| 129 | team := &teammode.Config{TeamID: "TeamA"} |
| 130 | for _, env := range []string{"production", EnvDevelopment} { |
| 131 | list, err := resolveAllowlist(Config{Env: env, Team: team}) |
| 132 | if err != nil { |
| 133 | t.Fatalf("%s team mode with no allowlist: %v", env, err) |
| 134 | } |
| 135 | // Never the development default that admits everyone. |
| 136 | if list.addrs == nil || list.allows("anyone@example.test") { |
| 137 | t.Fatalf("an empty %s team allowlist admits somebody locally", env) |
| 138 | } |
| 139 | list, err = resolveAllowlist(Config{Env: env, Team: team, AllowedAddresses: []string{"alice@example.test"}}) |
| 140 | if err != nil || !list.allows("alice@example.test") || list.allows("bob@example.test") { |
| 141 | t.Fatalf("a %s team allowlist is not the local door: %v", env, err) |
| 142 | } |
| 143 | } |
| 144 | if _, err := resolveAllowlist(Config{Env: "production"}); err == nil { |
| 145 | t.Fatal("production without team mode accepted no allowlist") |
| 146 | } |
| 147 | // Alta's shape: no allowlist at all, in production. It boots, and |
| 148 | // the local door admits nobody. |
| 149 | key, _ := assertioncrypto.Generate() |
| 150 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 151 | cfg.AllowedAddresses = nil |
| 152 | _, a, err := buildForTest(t, openTeamDB(t, filepath.Join(t.TempDir(), "calls.db")), cfg) |
| 153 | if err != nil { |
| 154 | t.Fatalf("team mode with no allowlist: %v", err) |
| 155 | } |
| 156 | if a.authorize("anyone@example.test") { |
| 157 | t.Fatal("an empty team allowlist admits an address at the local door") |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Once activated, a database refuses to boot without team configuration |
| 162 | // or with another binding, before anything migrates; the marker's |
| 163 | // triggers refuse a rewrite. |
| 164 | func TestTeamMarkerLocksTheDatabaseIntoTeamMode(t *testing.T) { |
| 165 | f := newTeamFixture(t) |
| 166 | if !markerFor(t, f.dbPath) { |
| 167 | t.Fatal("team mode wrote no marker") |
| 168 | } |
| 169 | off := f.cfg |
| 170 | off.Team = nil |
| 171 | if err := bootAt(t, f.dbPath, off); err == nil { |
| 172 | t.Fatal("an activated database booted without team mode") |
| 173 | } |
| 174 | key, _ := assertioncrypto.Generate() |
| 175 | b := teamTestBindings() |
| 176 | b.Team = "TeamB" |
| 177 | other, _, _ := teamConfigFor(t, b, key) |
| 178 | other.Team.TeamID = "TeamB" |
| 179 | if err := bootAt(t, f.dbPath, other); err == nil { |
| 180 | t.Fatal("an activated database booted with another team's binding") |
| 181 | } |
| 182 | if err := bootAt(t, f.dbPath, f.cfg); err != nil { |
| 183 | t.Fatalf("the same binding refused: %v", err) |
| 184 | } |
| 185 | for _, stmt := range []string{"UPDATE calls_team_required SET fingerprint='x'", "DELETE FROM calls_team_required"} { |
| 186 | if _, err := f.app.db.Writer().Exec(stmt); err == nil { |
| 187 | t.Fatalf("%s: allowed", stmt) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // The census: a Home identity from another issuer in either table refuses |
| 193 | // activation, and no marker is written. The tables are named here, not |
| 194 | // read from teamIssuerTables: a list that lost a table would otherwise |
| 195 | // lose its test with it. |
| 196 | func TestTeamCensusRefusesAForeignIssuer(t *testing.T) { |
| 197 | for _, table := range []string{"home_identity_links", "calls_team_home_persons"} { |
| 198 | t.Run(table, func(t *testing.T) { |
| 199 | key, _ := assertioncrypto.Generate() |
| 200 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 201 | // A database migrated in team mode's shape but not yet |
| 202 | // activated, holding one row from another issuer. |
| 203 | path := filepath.Join(t.TempDir(), "calls.db") |
| 204 | d := openTeamDB(t, path) |
| 205 | if _, err := migrate.Apply(context.Background(), d, teamBoot(BootSchema)); err != nil { |
| 206 | t.Fatal(err) |
| 207 | } |
| 208 | w := d.Writer() |
| 209 | res, err := w.Exec("INSERT INTO persons(email,name,created_at,anonymous) VALUES('home-only:00','n',0,0)") |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | person, _ := res.LastInsertId() |
| 214 | if _, err := w.Exec("INSERT INTO "+table+"(issuer,subject,person_id) VALUES('https://id.elsewhere.test',?,?)", teamSubject('q'), person); err != nil { |
| 215 | t.Fatal(err) |
| 216 | } |
| 217 | if _, _, err := buildForTest(t, d, cfg); !errors.Is(err, teammode.ErrConfig) { |
| 218 | t.Fatalf("a foreign issuer in %s: %v", table, err) |
| 219 | } |
| 220 | if markerFor(t, path) { |
| 221 | t.Fatalf("a refused census in %s wrote the marker", table) |
| 222 | } |
| 223 | }) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // GET /api/team: {"team_mode":false} off; on, only {team_mode, consumer, |
| 228 | // roster} with the roster's three states, no-store, whoever asks. |
| 229 | func TestTeamProbeSaysOnlyItsState(t *testing.T) { |
| 230 | probe := func(h http.Handler) (map[string]any, *httptest.ResponseRecorder) { |
| 231 | w := httptest.NewRecorder() |
| 232 | h.ServeHTTP(w, httptest.NewRequest("GET", homeTestOrigin+"/api/team", nil)) |
| 233 | var body map[string]any |
| 234 | if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { |
| 235 | t.Fatalf("probe body %q", w.Body.String()) |
| 236 | } |
| 237 | if w.Code != http.StatusOK || w.Header().Get("Cache-Control") != "no-store" { |
| 238 | t.Fatalf("probe = %d %q", w.Code, w.Header().Get("Cache-Control")) |
| 239 | } |
| 240 | return body, w |
| 241 | } |
| 242 | off := newHomeRouterFixture(t) |
| 243 | if body, w := probe(off.handler); len(body) != 1 || body["team_mode"] != false { |
| 244 | t.Fatalf("team mode off: %s", w.Body.String()) |
| 245 | } |
| 246 | f := newTeamFixture(t) |
| 247 | f.dir.SetDown(true) |
| 248 | if body, _ := probe(f.handler); body["roster"] != "unknown" { |
| 249 | t.Fatalf("before any roster: %v", body) |
| 250 | } |
| 251 | f.refreshed() |
| 252 | body, w := probe(f.handler) |
| 253 | if len(body) != 3 || body["team_mode"] != true || body["consumer"] != teamTestConsumer || body["roster"] != "ok" { |
| 254 | t.Fatalf("roster held: %s", w.Body.String()) |
| 255 | } |
| 256 | for _, leak := range []string{"TeamA", homeTestIssuer, "pr_", "@"} { |
| 257 | if strings.Contains(w.Body.String(), leak) { |
| 258 | t.Fatalf("the probe says %q: %s", leak, w.Body.String()) |
| 259 | } |
| 260 | } |
| 261 | f.expired() |
| 262 | if body, _ := probe(f.handler); body["roster"] != "stale" { |
| 263 | t.Fatalf("aged out: %v", body) |
| 264 | } |
| 265 | } |
| 266 | |