| 1 | package memoria |
| 2 | |
| 3 | // Team mode's write guard: the request's standing checked again inside |
| 4 | // the transaction that commits a person's write, before and after it |
| 5 | // (teammode.Guard; Home implementation/teammode-package.md §3, invariant |
| 6 | // 13, and Fable's deviation 7 on oficina/sheets #52). The route's gate |
| 7 | // (team_access.go) decides whether the write may start; this decides |
| 8 | // whether it may commit, so a lease that lapses between the two fails the |
| 9 | // write instead of landing it. It never asks the roster: it reads the |
| 10 | // Access the request carries, whose lease check never waits, so it is |
| 11 | // safe under the one-connection SQLite writer. |
| 12 | // |
| 13 | // Memoria has no shares, so nothing a person writes rests on anything but |
| 14 | // standing: a member's write needs Member, and a connector's needs the |
| 15 | // roster's owner role. |
| 16 | // |
| 17 | // Outside team mode Store.team is false and every helper here is the |
| 18 | // write path exactly as it was. |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | "net/http" |
| 24 | |
| 25 | "gorm.io/gorm" |
| 26 | |
| 27 | "amadan.net/oficina/home/roster" |
| 28 | "amadan.net/oficina/home/teammode" |
| 29 | ) |
| 30 | |
| 31 | // writeRule is what a write's standing must be. |
| 32 | type writeRule int |
| 33 | |
| 34 | const ( |
| 35 | // memberWrite: anything a member may do — add or delete content, |
| 36 | // report a fact, rate an answer. |
| 37 | memberWrite writeRule = iota |
| 38 | // ownerWrite: a connector's credential and runs, which are the |
| 39 | // roster's owners' alone (requireOwner). |
| 40 | ownerWrite |
| 41 | ) |
| 42 | |
| 43 | // rosterOwner is the roster's owner role. |
| 44 | const rosterOwner = "owner" |
| 45 | |
| 46 | // teamGuard is teammode.Guard. A variable only so a test can let the |
| 47 | // lease run out between a write and its commit on every write route. |
| 48 | var teamGuard = teammode.Guard |
| 49 | |
| 50 | type systemWriteKey struct{} |
| 51 | |
| 52 | // systemWrite marks ctx as carrying writes that nobody's standing makes: |
| 53 | // a connector run already started, a sibling poll. They are the team's |
| 54 | // own derived work, not a person's, so they are not guarded — and any |
| 55 | // Access the context carried is dropped, so a run that outlives the |
| 56 | // request that started it is not bound to that request's lease. |
| 57 | func systemWrite(ctx context.Context) context.Context { |
| 58 | return context.WithValue(teammode.WithAccess(ctx, nil), systemWriteKey{}, true) |
| 59 | } |
| 60 | |
| 61 | func isSystemWrite(ctx context.Context) bool { |
| 62 | v, _ := ctx.Value(systemWriteKey{}).(bool) |
| 63 | return v |
| 64 | } |
| 65 | |
| 66 | // authorizeFor is the Guard authorize for rule over acc: Member, and for |
| 67 | // ownerWrite the roster's owner role, which Role re-checks against the |
| 68 | // same latched lease. |
| 69 | func authorizeFor(rule writeRule, acc *teammode.Access) func(roster.State) error { |
| 70 | return func(st roster.State) error { |
| 71 | if err := teammode.RequireMember(st); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | if rule == ownerWrite && acc.Role() != rosterOwner { |
| 75 | // Role re-reads the lease: a lapse between Guard's standing and |
| 76 | // this read is Unknown, the retryable 503, as standing() answers |
| 77 | // at the route — never a 404 to an owner because Home was slow. |
| 78 | if acc.State() == roster.Unknown { |
| 79 | return teammode.ErrUnavailable |
| 80 | } |
| 81 | return teammode.ErrNotMember |
| 82 | } |
| 83 | return nil |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // guard runs write under rule. Call it INSIDE the write's transaction, |
| 88 | // with write using that transaction. Outside team mode, and for a |
| 89 | // systemWrite, it is write alone. In team mode a context with no Access |
| 90 | // is refused: a person-made write that lost its standing on the way here |
| 91 | // is a bug, and the closed answer is the one that cannot leak. |
| 92 | func (s *Store) guard(ctx context.Context, rule writeRule, write func() error) error { |
| 93 | if !s.team || isSystemWrite(ctx) { |
| 94 | return write() |
| 95 | } |
| 96 | acc, ok := teammode.FromContext(ctx) |
| 97 | if !ok { |
| 98 | return teammode.ErrUnavailable |
| 99 | } |
| 100 | return teamGuard(ctx, authorizeFor(rule, acc), write) |
| 101 | } |
| 102 | |
| 103 | // allowed is guard's authorize alone, for a step that must be refused |
| 104 | // before it happens rather than rolled back after (Delete's index |
| 105 | // removal, which precedes its transaction). |
| 106 | func (s *Store) allowed(ctx context.Context, rule writeRule) error { |
| 107 | return s.guard(ctx, rule, func() error { return nil }) |
| 108 | } |
| 109 | |
| 110 | // guarded runs write against the store's database exactly as it always |
| 111 | // has outside team mode, and in team mode in one transaction, inside |
| 112 | // guard, against that transaction (never the outer handle: the writer |
| 113 | // pool has one connection). |
| 114 | func (s *Store) guarded(ctx context.Context, rule writeRule, write func(db *gorm.DB) error) error { |
| 115 | if !s.team { |
| 116 | return write(s.db.G.WithContext(ctx)) |
| 117 | } |
| 118 | return s.db.G.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 119 | return s.guard(ctx, rule, func() error { return write(tx) }) |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | // isStandingErr reports whether err is the guard's refusal. |
| 124 | func isStandingErr(err error) bool { |
| 125 | return errors.Is(err, teammode.ErrUnavailable) || errors.Is(err, teammode.ErrNotMember) |
| 126 | } |
| 127 | |
| 128 | // guardRefused answers a write the guard refused, and reports whether it |
| 129 | // did. Standing lapsed between the route's check and the commit: the same |
| 130 | // retryable 503 as Unknown anywhere else, and a 404 for a definite "no" |
| 131 | // (which one request's Access cannot reach once it has said Member, but |
| 132 | // costs nothing to answer correctly). Outside team mode no error is ever |
| 133 | // one of these. |
| 134 | func (a *app) guardRefused(w http.ResponseWriter, r *http.Request, err error) bool { |
| 135 | switch { |
| 136 | case errors.Is(err, teammode.ErrUnavailable): |
| 137 | a.teamUnavailable(w, r) |
| 138 | case errors.Is(err, teammode.ErrNotMember): |
| 139 | a.errorPage(w, r, http.StatusNotFound, kindRoute, "") |
| 140 | default: |
| 141 | return false |
| 142 | } |
| 143 | return true |
| 144 | } |
| 145 | |