| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/go-json-experiment/json" |
| 15 | |
| 16 | "amadan.net/oficina/home/internal/home/directorydata" |
| 17 | "amadan.net/oficina/home/internal/home/sealtest" |
| 18 | "amadan.net/oficina/home/internal/operator" |
| 19 | ) |
| 20 | |
| 21 | // Exercise both real HTTP routers and their fresh browser confirmations, not |
| 22 | // just the operator methods. Synthetic fixtures never touch production data. |
| 23 | func TestExistingAdoptionHTTPPreservesCredentialAndLocalBoundary(t *testing.T) { |
| 24 | f := newOperatorFixtureConsumers(t, map[string]string{"docs": "https://docs.oficina.works"}) |
| 25 | signInFully(t, f.credential, sealtest.CanaryAddress) |
| 26 | f.openWindows(t, "initialize", nil) |
| 27 | if r, _ := f.run(t, "directory", "directory.initialize", func(b operator.InputBase) any { |
| 28 | return operator.InitializeInput{InputBase: b, Generation: generationToken(), Baseline: "operator-attests-never-provisioned"} |
| 29 | }); r.Code != 200 { |
| 30 | t.Fatal("initialize", r.Code) |
| 31 | } |
| 32 | f.expireWindows(t) |
| 33 | var owner string |
| 34 | if e := f.credential.db.Writer().QueryRow("SELECT person_ref FROM people WHERE blind_index=?", blindIndex(f.credential.secrets.Pepper, sealtest.CanaryAddress)).Scan(&owner); e != nil { |
| 35 | t.Fatal(e) |
| 36 | } |
| 37 | for _, role := range []string{"directory", "credential"} { |
| 38 | raw := f.plan(t, role, "create-team", []string{"docs"}, func(p *operator.Plan) { |
| 39 | p.ExistingTeam = "Uk" |
| 40 | if role == "directory" { |
| 41 | p.Label = "oficina" |
| 42 | } |
| 43 | }) |
| 44 | if r := f.post(role, operatorPlanPath, raw, nil); r.Code != 200 { |
| 45 | t.Fatal("plan", role, r.Code) |
| 46 | } |
| 47 | } |
| 48 | cred := f.paulCredential(t) |
| 49 | dir := f.paulDirectory(t, cred) |
| 50 | for role, session := range map[string]*http.Cookie{"directory": dir, "credential": cred} { |
| 51 | page := exchangeRequest(f.app(role), "GET", roleOrigin(role)+operatorConfirm, "", nil, session) |
| 52 | for _, want := range []string{"Connect existing apps to a team", "docs.oficina.works", "Uk", "content permissions"} { |
| 53 | if !strings.Contains(page.Body.String(), want) { |
| 54 | t.Fatalf("%s confirmation omitted %s", role, want) |
| 55 | } |
| 56 | } |
| 57 | if strings.Contains(page.Body.String(), "oficina.docs.oficina.works") { |
| 58 | t.Fatal("confirmation displayed a replacement instance") |
| 59 | } |
| 60 | if role == "credential" && (strings.Contains(page.Body.String(), operatorName) || strings.Contains(page.Body.String(), sealtest.CanaryAddress)) { |
| 61 | t.Fatal("credential page rendered account text") |
| 62 | } |
| 63 | address := "" |
| 64 | if role == "credential" { |
| 65 | address = sealtest.CanaryAddress |
| 66 | } |
| 67 | if r := f.confirm(t, role, session, address); r.Code != 200 { |
| 68 | t.Fatal("confirmation", role, r.Code) |
| 69 | } |
| 70 | } |
| 71 | r, seed := f.run(t, "credential", "credential.owner-seed", func(b operator.InputBase) any { |
| 72 | return operator.OwnerInput{InputBase: b, Directory: directoryOrigin, TeamID: 1, TeamSQID: "Uk", Address: sealtest.CanaryAddress, ExistingOnly: true} |
| 73 | }) |
| 74 | if r.Code != 200 { |
| 75 | t.Fatal("owner seed", r.Code) |
| 76 | } |
| 77 | if r, _ = f.run(t, "directory", "directory.seed-team", func(b operator.InputBase) any { |
| 78 | return operator.SeedInput{InputBase: b, Generation: generationToken(), TeamID: 1, TeamSQID: "Uk", Label: "oficina", Name: operatorName, Receipt: seed.Receipt} |
| 79 | }); r.Code != 200 { |
| 80 | t.Fatal("team seed", r.Code) |
| 81 | } |
| 82 | r, provision := f.run(t, "directory", "directory.provision-consumer", func(b operator.InputBase) any { |
| 83 | return operator.ProvisionInput{InputBase: b, Generation: generationToken(), TeamSQID: "Uk", App: "docs", Consumer: "docs", Audience: "https://docs.oficina.works"} |
| 84 | }) |
| 85 | if r.Code != 200 { |
| 86 | t.Fatal("provision", r.Code) |
| 87 | } |
| 88 | request := f.operation(t, "credential", "credential.register-consumer", func(b operator.InputBase) any { return operator.RegisterInput{InputBase: b, Record: provision.Record} }) |
| 89 | first := f.post("credential", operatorPath, request, nil) |
| 90 | again := f.post("credential", operatorPath, request, nil) |
| 91 | if first.Code != 200 || again.Code != 200 || first.Body.String() != again.Body.String() { |
| 92 | t.Fatal("registration exact replay", first.Code, again.Code) |
| 93 | } |
| 94 | var n int |
| 95 | if e := f.credential.db.Writer().QueryRow("SELECT count(*) FROM consumers").Scan(&n); e != nil || n != 0 { |
| 96 | t.Fatal("registration duplicated", e, n) |
| 97 | } |
| 98 | if got, ok := f.credential.consumers()["docs"]; !ok || got != "https://docs.oficina.works" { |
| 99 | t.Fatal("existing sign-in changed", got, ok) |
| 100 | } |
| 101 | var registered operator.Result |
| 102 | if err := json.Unmarshal(first.Body.Bytes(), ®istered); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | now := time.Now().Unix() |
| 106 | digest := sha256.Sum256(provision.Record) |
| 107 | v := operator.VerificationInput{Consumer: "docs", TeamSQID: "Uk", App: "docs", Audience: "https://docs.oficina.works", Generation: generationToken(), RecordSequence: 1, RecordDigest: base64.RawURLEncoding.EncodeToString(digest[:]), Epoch: provision.Epoch, Owner: owner, Issued: now, Expires: now + 600} |
| 108 | r, attempt := f.run(t, "directory", "directory.verify-consumer", func(b operator.InputBase) any { v.InputBase = b; return v }) |
| 109 | if r.Code != 200 { |
| 110 | t.Fatal("verify", r.Code) |
| 111 | } |
| 112 | ev := operator.ActivationEvidence{Version: 1, Policy: "oficina/existing-app-activation/v1", Attempt: attempt.Attempt, Consumer: v.Consumer, TeamSQID: v.TeamSQID, App: v.App, Audience: v.Audience, Generation: v.Generation, RecordSequence: 1, RecordDigest: v.RecordDigest, Epoch: v.Epoch, Owner: owner, AppVersion: strings.Repeat("a", 40), ArtifactDigest: fixtureToken(80), EnvironmentBinding: fixtureToken(81), ManifestDigest: registered.Manifest, AliasHost: "docs.oficina.works", Started: now, Completed: now} |
| 113 | if r, _ = f.run(t, "directory", "directory.activate-consumer", func(b operator.InputBase) any { return operator.ActivationInput{InputBase: b, Evidence: ev} }); r.Code != 200 { |
| 114 | t.Fatal("activation", r.Code) |
| 115 | } |
| 116 | store, e := directorydata.New(f.directory.db.Writer(), f.directory.secrets.Sealer, directorydata.Config{Apps: operatorApps(), ValidateRoute: directoryRouteAllowed}) |
| 117 | if e != nil { |
| 118 | t.Fatal(e) |
| 119 | } |
| 120 | if choices, e := store.AgentChoices(context.Background(), owner); e != nil || len(choices) != 1 || choices[0].ConsumerID != "docs" { |
| 121 | t.Fatal("live route", choices, e) |
| 122 | } |
| 123 | // Home has not granted the unrelated operator team membership merely |
| 124 | // because they approved the setup. Local item authority is still app-side. |
| 125 | if choices, e := store.AgentChoices(context.Background(), f.paul); e != nil || len(choices) != 0 { |
| 126 | t.Fatal("operator gained standing", e) |
| 127 | } |
| 128 | for _, path := range []string{f.credential.dbPath, f.directoryPath} { |
| 129 | for _, suffix := range []string{"", "-wal", "-shm"} { |
| 130 | b, e := os.ReadFile(path + suffix) |
| 131 | if e != nil && !os.IsNotExist(e) { |
| 132 | t.Fatal(e) |
| 133 | } |
| 134 | for _, secret := range []string{sealtest.CanaryAddress, operatorName, string(seed.Receipt), provision.Bearer} { |
| 135 | if secret != "" && bytes.Contains(b, []byte(secret)) { |
| 136 | t.Fatal("adoption retained private plaintext") |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | for _, secret := range []string{sealtest.CanaryAddress, operatorName, provision.Bearer} { |
| 142 | if strings.Contains(f.logs.String(), secret) { |
| 143 | t.Fatal("adoption logged private input") |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestCanonicalDirectoryPolicyIsExact(t *testing.T) { |
| 149 | for _, app := range operatorApps() { |
| 150 | r := directorydata.Route{TeamID: 1, TeamSQID: "Uk", Label: "oficina", App: app, ConsumerID: app, Origin: "https://" + app + ".oficina.works"} |
| 151 | if !directoryRouteAllowed(r) { |
| 152 | t.Fatal("canonical tuple refused", app) |
| 153 | } |
| 154 | for _, edit := range []func(*directorydata.Route){func(r *directorydata.Route) { r.Label = "other" }, func(r *directorydata.Route) { r.ConsumerID = "other" }, func(r *directorydata.Route) { r.Origin = "https://oficina." + app + ".oficina.works" }, func(r *directorydata.Route) { r.Origin += ".evil.test" }} { |
| 155 | bad := r |
| 156 | edit(&bad) |
| 157 | if directoryRouteAllowed(bad) { |
| 158 | t.Fatal("canonical substitution admitted", bad) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |