oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1package home
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "net/http"
8 "net/http/httptest"
9 "strings"
10 "testing"
11)
12
13func TestAgentDirectoryUsesCurrentStandingAndPinnedDestinations(t *testing.T) {
14 f := newMountedChooser(t)
15 team := f.team(t, "AAA111", "blue", "Blue team", chooserPerson)
16 f.team(t, "BBB222", "green", "Other team", chooserOther)
17 f.team(t, "CCC333", "red", "Red team", chooserPerson)
18 if _, err := f.p.directory.db.Writer().Exec("INSERT INTO team_instances VALUES(?,?,?,?,?)", team, "sheets", "https://blue.sheets.oficina.works", "blue-sheets", "live"); err != nil {
19 t.Fatal(err)
20 }
21 const token = "test-service-token-8a919e49f0294d6fa3cebf231948f93e"
22 sum := sha256.Sum256([]byte(token))
23 request := func(host, auth, body string, cookie *http.Cookie) *httptest.ResponseRecorder {
24 r := httptest.NewRequest("POST", host+agentDirectoryPath, strings.NewReader(body))
25 r.Header.Set("Content-Type", "application/json")
26 if auth != "" {
27 r.Header.Set("Authorization", auth)
28 }
29 if cookie != nil {
30 r.AddCookie(cookie)
31 }
32 w := httptest.NewRecorder()
33 f.p.directory.Router().ServeHTTP(w, r)
34 noStore(t, w)
35 return w
36 }
37 body := `{"subject":"` + chooserPerson + `"}`
38 if w := request(directoryOrigin, "Bearer "+token, body, nil); w.Code != 404 {
39 t.Fatal("enabled by default", w.Code)
40 }
41 f.p.directory.runtime.agentDirectoryHash = sum[:]
42 w := request(directoryOrigin, "Bearer "+token, body, nil)
43 var out struct {
44 Teams []agentDirectoryTeam `json:"teams"`
45 }
46 if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &out) != nil || len(out.Teams) != 2 {
47 t.Fatal("directory read", w.Code, w.Body.String())
48 }
49 if out.Teams[0].ID != "AAA111" || len(out.Teams[0].Apps) != 2 || out.Teams[0].Apps[0].Origin != "https://blue.docs.oficina.works" || out.Teams[0].Apps[1].Origin != "https://blue.sheets.oficina.works" || out.Teams[1].ID != "CCC333" {
50 t.Fatal("wrong route")
51 }
52 for _, auth := range []string{"", "Bearer wrong", "Basic " + token} {
53 if w := request(directoryOrigin, auth, body, f.session(t, chooserPerson)); w.Code != 401 {
54 t.Fatal("cookie or invalid bearer authorized", w.Code)
55 }
56 }
57 if w := request(Origin, "Bearer "+token, body, nil); w.Code != 404 {
58 t.Fatal("served credential host", w.Code)
59 }
60 for _, bad := range []string{`{"subject":"` + chooserPerson + `","subject":"` + chooserOther + `"}`, `{"subject":"` + chooserPerson + `","origin":"https://evil.example"}`, `{"subject":"a@example.test"}`} {
61 if w := request(directoryOrigin, "Bearer "+token, bad, nil); w.Code != 400 {
62 t.Fatal("accepted invalid request", w.Code)
63 }
64 }
65 if _, err := f.p.directory.db.Writer().Exec("UPDATE team_instances SET origin='https://evil.example' WHERE team_id=? AND app='docs'", team); err != nil {
66 t.Fatal(err)
67 }
68 if w := request(directoryOrigin, "Bearer "+token, body, nil); w.Code != 503 || strings.Contains(w.Body.String(), "evil.example") {
69 t.Fatal("untrusted destination returned", w.Code)
70 }
71 if _, err := f.p.directory.db.Writer().Exec("UPDATE team_instances SET origin='https://blue.docs.oficina.works' WHERE team_id=? AND app='docs'", team); err != nil {
72 t.Fatal(err)
73 }
74 if _, err := f.p.directory.db.Writer().Exec("UPDATE team_instances SET state='disabled' WHERE team_id=?", team); err != nil {
75 t.Fatal(err)
76 }
77 if w := request(directoryOrigin, "Bearer "+token, body, nil); w.Code != 200 || strings.Contains(w.Body.String(), "AAA111") {
78 t.Fatal("disabled destination returned")
79 }
80 if _, err := f.p.directory.db.Writer().Exec("UPDATE team_instances SET state='live' WHERE team_id=?", team); err != nil {
81 t.Fatal(err)
82 }
83 if _, err := f.p.directory.db.Writer().Exec("DELETE FROM memberships WHERE team_id=? AND person_ref=?", team, chooserPerson); err != nil {
84 t.Fatal(err)
85 }
86 if w := request(directoryOrigin, "Bearer "+token, body, nil); w.Code != 200 || strings.Contains(w.Body.String(), "AAA111") {
87 t.Fatal("removed membership returned")
88 }
89 for _, secret := range []string{token, "a@example.test", "Blue team", "https://evil.example"} {
90 if strings.Contains(f.p.logs.String(), secret) {
91 t.Fatal("directory request or data logged")
92 }
93 }
94 for _, tc := range []struct {
95 name, method, media, body string
96 duplicate bool
97 want int
98 }{
99 {"method", "GET", "application/json", body, false, 405},
100 {"media", "POST", "text/plain", body, false, 415},
101 {"oversized", "POST", "application/json", strings.Repeat(" ", 1025), false, 400},
102 {"duplicate bearer", "POST", "application/json", body, true, 401},
103 } {
104 t.Run(tc.name, func(t *testing.T) {
105 r := httptest.NewRequest(tc.method, directoryOrigin+agentDirectoryPath, strings.NewReader(tc.body))
106 r.Header.Set("Content-Type", tc.media)
107 r.Header.Set("Authorization", "Bearer "+token)
108 if tc.duplicate {
109 r.Header.Add("Authorization", "Bearer "+token)
110 }
111 w := httptest.NewRecorder()
112 f.p.directory.Router().ServeHTTP(w, r)
113 if w.Code != tc.want {
114 t.Fatal(w.Code)
115 }
116 noStore(t, w)
117 })
118 }
119 const replacement = "replacement-service-token-769283cc3ff94cc4ba52"
120 rotated := sha256.Sum256([]byte(replacement))
121 cfg := *f.p.directory.runtime
122 cfg.agentDirectoryHash = rotated[:]
123 reloaded, err := NewRuntime(cfg, f.p.directory.db, quietLogger())
124 if err != nil {
125 t.Fatal(err)
126 }
127 f.p.directory = reloaded
128 if w := request(directoryOrigin, "Bearer "+token, body, nil); w.Code != 401 {
129 t.Fatal("old service token survived configuration reload")
130 }
131 if w := request(directoryOrigin, "Bearer "+replacement, body, nil); w.Code != 200 {
132 t.Fatal("replacement service token refused", w.Code)
133 }
134}
135
136func TestAgentDirectoryConfigurationIsDirectoryOnly(t *testing.T) {
137 _, keys := runtimeEnv(t)
138 t.Setenv("HOME_ROLE", "directory")
139 t.Setenv("HOME_ASSERTION_KEYS", keys)
140 sum := sha256.Sum256([]byte("test-service-token"))
141 t.Setenv("HOME_AGENT_DIRECTORY_TOKEN_SHA256", hex.EncodeToString(sum[:]))
142 if _, err := LoadRuntime("directory.db", quietLogger()); err != nil {
143 t.Fatal(err)
144 }
145 for _, bad := range []string{"not-hex", strings.Repeat("1", 62)} {
146 t.Setenv("HOME_AGENT_DIRECTORY_TOKEN_SHA256", bad)
147 if _, err := LoadRuntime("directory.db", quietLogger()); err == nil {
148 t.Fatal("accepted invalid token hash")
149 }
150 }
151 t.Setenv("HOME_AGENT_DIRECTORY_TOKEN_SHA256", hex.EncodeToString(sum[:]))
152 for _, role := range []string{"", "credential"} {
153 t.Setenv("HOME_ROLE", role)
154 if _, err := LoadRuntime("home.db", quietLogger()); err == nil {
155 t.Fatal("accepted service authority on credential origin")
156 }
157 }
158}
159