oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "bytes"
5 "context"
6 "encoding/base64"
7 "errors"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/home/roster"
12 family "amadan.net/rastrillo/rastrillo/crypto"
13 "amadan.net/rastrillo/rastrillo/migrate"
14 "github.com/carlosframework/docs/internal/docs/principalstore"
15 "github.com/go-json-experiment/json"
16 "gorm.io/gorm"
17)
18
19func teamConfigFixture(t *testing.T) (*TeamConfig, *homeSettings) {
20 t.Helper()
21 op, e := family.Generate()
22 if e != nil {
23 t.Fatal(e)
24 }
25 key, e := family.Generate()
26 if e != nil {
27 t.Fatal(e)
28 }
29 b := roster.Bindings{Issuer: "https://id.example", Directory: "https://home.example", Consumer: "docs-A", App: "docs", Team: "TeamA", Audience: "https://docs.example"}
30 p := roster.Provisioning{Version: 1, Operation: "provision1", Sequence: 1, Bindings: b, Generation: base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{1}, 32)), MinEpoch: 1, Keys: []roster.Key{{ID: "roster1", Public: base64.RawURLEncoding.EncodeToString(key.SignPub())}}}
31 raw, e := roster.SignProvisioning(op, "operator1", p)
32 if e != nil {
33 t.Fatal(e)
34 }
35 keys, _ := json.Marshal([]roster.Key{{ID: "operator1", Public: base64.RawURLEncoding.EncodeToString(op.SignPub())}})
36 return &TeamConfig{TeamID: b.Team, DirectoryOrigin: b.Directory, ProvisioningBase64: base64.StdEncoding.EncodeToString(raw), OperatorKeysBase64: base64.StdEncoding.EncodeToString(keys), RosterToken: base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32))}, &homeSettings{issuer: b.Issuer, consumerID: b.Consumer, dek: bytes.Repeat([]byte{9}, 32)}
37}
38func TestTeamConfigPinsAndColdMarker(t *testing.T) {
39 cfg, home := teamConfigFixture(t)
40 settings, e := validatedTeamConfig(cfg, home, "https://docs.example")
41 if e != nil {
42 t.Fatal(e)
43 }
44 for _, change := range []func(*TeamConfig){func(c *TeamConfig) { c.TeamID = "Other" }, func(c *TeamConfig) { c.DirectoryOrigin = "https://other.example" }, func(c *TeamConfig) { c.RosterToken += "=" }, func(c *TeamConfig) { c.ProvisioningBase64 += "\n" }, func(c *TeamConfig) { c.OperatorKeysBase64 = "e30=" }} {
45 bad := *cfg
46 change(&bad)
47 if _, e := validatedTeamConfig(&bad, home, "https://docs.example"); e != errTeamConfig {
48 t.Fatal("bad team config")
49 }
50 }
51 if _, e := validatedTeamConfig(cfg, nil, "https://docs.example"); e != errTeamConfig {
52 t.Fatal("team without Home")
53 }
54 app := newSampleDocumentsInternalTestApp(t, "0")
55 if _, e := migrate.Apply(t.Context(), app.db, TeamBootSchema); e != nil {
56 t.Fatal(e)
57 }
58 rt, e := newTeamRuntime(t.Context(), settings, app.db.Writer(), home)
59 if e != nil {
60 t.Fatal(e)
61 }
62 if rt.cache == nil || rt.principals == nil {
63 t.Fatal("runtime missing")
64 }
65 if got, yes, e := teamState.Required(t.Context(), app.db.Writer()); e != nil || !yes || got != settings.Fingerprint() {
66 t.Fatal("runtime did not commit marker")
67 }
68 var count int
69 _ = app.db.Writer().QueryRow("SELECT COUNT(*) FROM docs_home_identities").Scan(&count)
70 if count != 0 {
71 t.Fatal("runtime seeded identity")
72 }
73}
74func TestTeamIdentityAddressAbsenceNeverRedeemsEmptyGrant(t *testing.T) {
75 app := newSampleDocumentsInternalTestApp(t, "0")
76 ctx := context.Background()
77 s := app.Store()
78 local, e := s.EnsurePerson(ctx, "owner@example.com")
79 if e != nil {
80 t.Fatal(e)
81 }
82 doc, e := s.createDocument(ctx, local, "private", "", VisibilityPrivate)
83 if e != nil {
84 t.Fatal(e)
85 }
86 if _, e = migrate.Apply(ctx, app.db, TeamBootSchema); e != nil {
87 t.Fatal(e)
88 }
89 ps, e := principalstore.New(app.db.Writer(), principalstore.Config{Issuer: "https://id.example", Team: "TeamA", RequireMembership: func(context.Context, principalstore.MembershipRequest) error { return nil }})
90 if e != nil {
91 t.Fatal(e)
92 }
93 subject := "pr_" + strings.Repeat("a", 26)
94 p, e := ps.EnsureHome(ctx, principalstore.HomeIdentity{Issuer: "https://id.example", Subject: subject})
95 if e != nil {
96 t.Fatal(e)
97 }
98 rt, _, _ := testTeamRoster(t, s, app)
99 rt.principals = ps
100 s.team = rt
101 resolver := identityResolver{store: s, team: rt, links: homeOnlySubjects(rt)}
102 id, e := resolver.resolve(identityRequest("GET", subject, homeAssertionMethod))
103 if e != nil || id.Actor.PersonID != p.ID || id.Actor.VerifiedAddress != nil || id.HomeSubject != subject {
104 t.Fatal("wrong Home principal", e)
105 }
106 ctx = context.WithValue(ctx, accessContextKey{}, rt.resolveAccess(ctx, id))
107 if e = s.db.Create(&Grant{DocID: doc.ID, Subject: "", Role: RoleWriter, CreatedBy: local}).Error; e != nil {
108 t.Fatal(e)
109 }
110 if _, e = s.GetFor(ctx, id.Actor, doc.Sqid); !errors.Is(e, gorm.ErrRecordNotFound) {
111 t.Fatal("nil address redeemed malformed grant", e)
112 }
113 if s.CanWrite(ctx, id.Actor, doc.ID) {
114 t.Fatal("nil address redeemed malformed writer grant")
115 }
116 own, e := s.Create(ctx, p.ID, "Home owned")
117 if e != nil {
118 t.Fatal(e)
119 }
120 if e = s.Grant(ctx, p.ID, own.Sqid, "guest@example.com", RoleReader); e != nil {
121 t.Fatal("addressless owner could not grant", e)
122 }
123 var people int64
124 s.db.Model(&Person{}).Count(&people)
125 if people != 1 {
126 t.Fatal("Home identity created legacy person")
127 }
128 if _, e = resolver.resolve(identityRequest("GET", "pr_"+strings.Repeat("b", 26), homeAssertionMethod)); e != errIdentityRefused {
129 t.Fatal("unknown identity allocated")
130 }
131 if _, e = resolver.resolve(identityRequest("GET", "unknown@example.com", "unsupported")); e != errIdentityRefused {
132 t.Fatal("unsupported local method accepted")
133 }
134}
135
136// Activation refuses a database holding a Home identity from another issuer,
137// in either of Docs' tables, and writes no marker: the marker pins one issuer,
138// and a row from another would be read under it.
139func TestTeamActivationCensusCoversBothIdentityTables(t *testing.T) {
140 for _, table := range []string{"home_identity_links", "docs_home_identities"} {
141 t.Run(table, func(t *testing.T) {
142 cfg, home := teamConfigFixture(t)
143 settings, e := validatedTeamConfig(cfg, home, "https://docs.example")
144 if e != nil {
145 t.Fatal(e)
146 }
147 app := newSampleDocumentsInternalTestApp(t, "0")
148 ctx := t.Context()
149 migrateTeam(t, app)
150 const other = "https://other.example"
151 subject := "pr_" + strings.Repeat("a", 26)
152 switch table {
153 case "home_identity_links":
154 person, e := app.Store().EnsurePerson(ctx, "linked@example.com")
155 if e != nil {
156 t.Fatal(e)
157 }
158 if _, e = app.db.Writer().Exec("INSERT INTO home_identity_links(issuer,subject,person_id) VALUES(?,?,?)", other, subject, person); e != nil {
159 t.Fatal(e)
160 }
161 default:
162 ps, e := principalstore.New(app.db.Writer(), principalstore.Config{Issuer: other, Team: "TeamA", RequireMembership: func(context.Context, principalstore.MembershipRequest) error { return nil }})
163 if e != nil {
164 t.Fatal(e)
165 }
166 if _, e = ps.EnsureHome(ctx, principalstore.HomeIdentity{Issuer: other, Subject: subject}); e != nil {
167 t.Fatal(e)
168 }
169 }
170 if _, e = newTeamRuntime(ctx, settings, app.db.Writer(), home); e != errTeamConfig {
171 t.Fatal("activated over another issuer's identity", e)
172 }
173 if _, activated, e := teamState.Required(ctx, app.db.Writer()); e != nil || activated {
174 t.Fatal("a refused census wrote the marker", e)
175 }
176 })
177 }
178}
179