| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/home/roster" |
| 9 | "amadan.net/oficina/home/teammode" |
| 10 | "amadan.net/oficina/home/teammode/teammodetest" |
| 11 | assertioncrypto "amadan.net/rastrillo/rastrillo/crypto" |
| 12 | "amadan.net/rastrillo/rastrillo/db" |
| 13 | "amadan.net/rastrillo/rastrillo/migrate" |
| 14 | ) |
| 15 | |
| 16 | func teamKey(t *testing.T) *assertioncrypto.Keypair { |
| 17 | t.Helper() |
| 18 | key, err := assertioncrypto.Generate() |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | return key |
| 23 | } |
| 24 | |
| 25 | // bootTeam is New over d with cfg, closing the app it built. |
| 26 | func bootTeam(t *testing.T, d *db.DB, cfg Config) error { |
| 27 | t.Helper() |
| 28 | _, a, err := New(d, cfg) |
| 29 | if a != nil { |
| 30 | a.Close() |
| 31 | } |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | // The record is verified against bindings built only from configuration: |
| 36 | // Home's issuer and consumer ID, the app, the team, the directory and |
| 37 | // this instance's origin. A record signed for any other value of any one |
| 38 | // of them refuses boot, before anything migrates. |
| 39 | func TestTeamConfigPinsTheProvisioningRecord(t *testing.T) { |
| 40 | key := teamKey(t) |
| 41 | cases := map[string]func(*roster.Bindings, *teammode.Config){ |
| 42 | "issuer": func(b *roster.Bindings, _ *teammode.Config) { b.Issuer = "https://id.other.example" }, |
| 43 | "consumer": func(b *roster.Bindings, _ *teammode.Config) { b.Consumer = "another-consumer" }, |
| 44 | "app": func(b *roster.Bindings, _ *teammode.Config) { b.App = "sheets" }, |
| 45 | "origin": func(b *roster.Bindings, _ *teammode.Config) { b.Audience = "https://other.example.com" }, |
| 46 | "team": func(_ *roster.Bindings, c *teammode.Config) { c.TeamID = "TeamB" }, |
| 47 | "directory": func(_ *roster.Bindings, c *teammode.Config) { c.DirectoryOrigin = "https://dir.other.example" }, |
| 48 | } |
| 49 | for name, change := range cases { |
| 50 | t.Run(name, func(t *testing.T) { |
| 51 | b := teamTestBindings() |
| 52 | change(&b, &teammode.Config{}) |
| 53 | cfg, _, _ := teamConfigFor(t, b, key) |
| 54 | change(&roster.Bindings{}, cfg.Team) |
| 55 | d := testDB(t) |
| 56 | if err := bootTeam(t, d, cfg); !errors.Is(err, teammode.ErrConfig) { |
| 57 | t.Fatalf("a record for another %s = %v, want ErrConfig", name, err) |
| 58 | } |
| 59 | if _, activated, _ := teamState.Required(context.Background(), d.Writer()); activated { |
| 60 | t.Fatal("a refused record left a marker") |
| 61 | } |
| 62 | var tables int |
| 63 | d.Writer().QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='users'").Scan(&tables) |
| 64 | if tables != 0 { |
| 65 | t.Fatal("a refused record migrated the database") |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | // And the right record boots. |
| 70 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 71 | if err := bootTeam(t, testDB(t), cfg); err != nil { |
| 72 | t.Fatalf("the instance's own record: %v", err) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Five variables, all or none, and only beside Home. One present is a |
| 77 | // configuration, and an invalid one: there is no falling back to the |
| 78 | // allowlist. In team mode SLIDES_ALLOWED_ADDRESSES is optional even |
| 79 | // outside development, and empty admits nobody locally. |
| 80 | func TestTeamEnvIsAllOrNoneAndTheAllowlistBecomesOptional(t *testing.T) { |
| 81 | key := teamKey(t) |
| 82 | names := teammode.EnvNames("SLIDES") |
| 83 | if names != [5]string{"SLIDES_TEAM_ID", "SLIDES_TEAM_DIRECTORY_ORIGIN", "SLIDES_TEAM_PROVISIONING_BASE64", "SLIDES_TEAM_OPERATOR_KEYS_BASE64", "SLIDES_TEAM_ROSTER_TOKEN"} { |
| 84 | t.Fatalf("env names = %v", names) |
| 85 | } |
| 86 | for _, only := range names { |
| 87 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 88 | cfg.Team = teammode.ConfigFromLookup("SLIDES", func(n string) string { |
| 89 | if n == only { |
| 90 | return "x" |
| 91 | } |
| 92 | return "" |
| 93 | }) |
| 94 | if cfg.Team == nil { |
| 95 | t.Fatalf("%s alone read as team mode off", only) |
| 96 | } |
| 97 | if err := bootTeam(t, testDB(t), cfg); !errors.Is(err, teammode.ErrConfig) { |
| 98 | t.Fatalf("%s alone = %v, want ErrConfig", only, err) |
| 99 | } |
| 100 | } |
| 101 | if teammode.ConfigFromLookup("SLIDES", func(string) string { return "" }) != nil { |
| 102 | t.Fatal("no variables read as team mode on") |
| 103 | } |
| 104 | |
| 105 | // Team mode requires Home. |
| 106 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 107 | cfg.Home = nil |
| 108 | if err := bootTeam(t, testDB(t), cfg); !errors.Is(err, teammode.ErrConfig) { |
| 109 | t.Fatalf("team mode without Home = %v, want ErrConfig", err) |
| 110 | } |
| 111 | |
| 112 | // Production, no allowlist: boots in team mode, admits nobody locally. |
| 113 | cfg, _, _ = teamConfigFor(t, teamTestBindings(), key) |
| 114 | cfg.Env, cfg.InstanceKey, cfg.AllowedAddresses = EnvProduction, "a-real-instance-key", nil |
| 115 | _, a, err := New(testDB(t), cfg) |
| 116 | if err != nil { |
| 117 | t.Fatalf("team mode with no allowlist in production: %v", err) |
| 118 | } |
| 119 | defer a.Close() |
| 120 | if a.allowed.allows("anyone@example.com") { |
| 121 | t.Fatal("an empty team-mode allowlist admits an address") |
| 122 | } |
| 123 | // Without team mode the same is still a refusal. |
| 124 | cfg.Team = nil |
| 125 | if err := bootTeam(t, testDB(t), cfg); err == nil { |
| 126 | t.Fatal("no allowlist outside team mode booted") |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // The marker locks a database into team mode: once activated it refuses |
| 131 | // to boot without team configuration, or with another binding, before |
| 132 | // anything migrates; the same binding boots again. |
| 133 | func TestTeamMarkerLocksTheDatabaseIntoTeamMode(t *testing.T) { |
| 134 | key := teamKey(t) |
| 135 | d := testDB(t) |
| 136 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 137 | if err := bootTeam(t, d, cfg); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | fp, activated, err := teamState.Required(context.Background(), d.Writer()) |
| 141 | if err != nil || !activated || fp != teammode.Fingerprint(teamTestBindings()) { |
| 142 | t.Fatalf("marker = %q %v %v", fp, activated, err) |
| 143 | } |
| 144 | |
| 145 | off := cfg |
| 146 | off.Team = nil |
| 147 | if err := bootTeam(t, d, off); !errors.Is(err, teammode.ErrConfig) { |
| 148 | t.Fatalf("activated database without team configuration = %v, want ErrConfig", err) |
| 149 | } |
| 150 | other := teamTestBindings() |
| 151 | other.Team = "TeamB" |
| 152 | rebound, _, _ := teamConfigFor(t, other, key) |
| 153 | if err := bootTeam(t, d, rebound); !errors.Is(err, teammode.ErrConfig) { |
| 154 | t.Fatalf("activated database with another binding = %v, want ErrConfig", err) |
| 155 | } |
| 156 | if err := bootTeam(t, d, cfg); err != nil { |
| 157 | t.Fatalf("the same binding again: %v", err) |
| 158 | } |
| 159 | // The marker's triggers refuse a rewrite. |
| 160 | if _, err := d.Writer().Exec("UPDATE slides_team_required SET bindings='x'"); err == nil { |
| 161 | t.Fatal("the marker was rewritten") |
| 162 | } |
| 163 | if _, err := d.Writer().Exec("DELETE FROM slides_team_required"); err == nil { |
| 164 | t.Fatal("the marker was deleted") |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // The census: a Home identity from another issuer, in either table that |
| 169 | // holds one, refuses activation, and no marker is written. |
| 170 | func TestTeamCensusRefusesAForeignIssuer(t *testing.T) { |
| 171 | key := teamKey(t) |
| 172 | for _, table := range []string{"home_identity_links", "slides_team_home_users"} { |
| 173 | t.Run(table, func(t *testing.T) { |
| 174 | d := testDB(t) |
| 175 | cfg, _, _ := teamConfigFor(t, teamTestBindings(), key) |
| 176 | // The team schema applied and never activated: what a boot |
| 177 | // refused after migrating leaves behind. |
| 178 | if _, err := migrate.Apply(context.Background(), d, teamBoot(migrate.Merge(BootSchema, homeSchema))); err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | if _, err := d.Writer().Exec("INSERT INTO users(email,name,colour,created_at) VALUES(NULL,'','#000000',CURRENT_TIMESTAMP)"); err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | if _, err := d.Writer().Exec("INSERT INTO "+table+"(issuer,subject,"+map[string]string{"home_identity_links": "person_id", "slides_team_home_users": "user_id"}[table]+") VALUES('https://id.other.example',?,1)", teamSubject('f')); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | if err := bootTeam(t, d, cfg); !errors.Is(err, teammode.ErrConfig) { |
| 188 | t.Fatalf("a %s row from another issuer = %v, want ErrConfig", table, err) |
| 189 | } |
| 190 | if _, activated, _ := teamState.Required(context.Background(), d.Writer()); activated { |
| 191 | t.Fatal("a refused census left a marker") |
| 192 | } |
| 193 | }) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | var _ = teammodetest.Team |
| 198 | |