|
| 1 | +package enforce |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/lightwebinc/borg/borgrec" |
| 12 | + "github.com/lightwebinc/borg/reader" |
| 13 | +) |
| 14 | + |
| 15 | +// Decider is the decision procedure of spec section 13 for a service that |
| 16 | +// decides for itself, keeps no receipt and answers for any organization a |
| 17 | +// caller names: a renderer that admits writers by a permission, say. It is |
| 18 | +// Enforcer without the wallet and the receipt, and with what a service |
| 19 | +// that asks often and from many places needs: denies reused too, a failed |
| 20 | +// read remembered, and an allow kept through an outage for a bound the |
| 21 | +// service states. Make one and keep it; it is safe for concurrent use and |
| 22 | +// holds no lock over a read. |
| 23 | +// |
| 24 | +// It fails closed. A read it cannot complete is an error, and the answer is |
| 25 | +// a deny, except an allow read before the failure and within StaleFor, |
| 26 | +// which is answered true with an error that wraps ErrStale. |
| 27 | +type Decider struct { |
| 28 | + // Reader asks the hosts and verifies what they answer against the |
| 29 | + // service's own headers. Give it a Floor (reader.MemFloor at least), so |
| 30 | + // a host set that answers an older organization token than one already |
| 31 | + // verified is refused; without one a Decider still never goes back to |
| 32 | + // an earlier seq itself, but a restart, or a second Decider, can. |
| 33 | + Reader *reader.Client |
| 34 | + // Spends says whether an outpoint is spent. Without one nothing is |
| 35 | + // current and every call denies. |
| 36 | + Spends reader.Spends |
| 37 | + // AllowFor is how long an allow is reused, and how often the |
| 38 | + // organization's latest token is read again while every subject asked |
| 39 | + // about is allowed; zero is DefaultFreshness. A revoked grant is |
| 40 | + // answered at most this long past the hosts holding the token that |
| 41 | + // revoked it. |
| 42 | + AllowFor time.Duration |
| 43 | + // DenyFor is how long a deny, or a failed read of an organization, is |
| 44 | + // reused, and how often the latest token is read again while a subject |
| 45 | + // is denied: so a subject just granted waits this long once the hosts |
| 46 | + // hold the token. Zero is DefaultDenyFor; it is held to AllowFor. |
| 47 | + DenyFor time.Duration |
| 48 | + // StaleFor is how long an allow stands through failed reads, from the |
| 49 | + // read that allowed it, and only while no later token is verified. |
| 50 | + // Zero keeps none: a failed read denies. |
| 51 | + StaleFor time.Duration |
| 52 | + // Now is the clock; nil is time.Now. |
| 53 | + Now func() time.Time |
| 54 | + |
| 55 | + mu sync.Mutex |
| 56 | + views map[string]deciderView |
| 57 | + answers map[string]deciderAnswer |
| 58 | + failed map[string]deciderFailure |
| 59 | +} |
| 60 | + |
| 61 | +// DefaultDenyFor is how long a Decider reuses a deny when it is told no |
| 62 | +// bound of its own. |
| 63 | +const DefaultDenyFor = 10 * time.Second |
| 64 | + |
| 65 | +// ErrStale marks an allow kept through a failed read: the organization |
| 66 | +// could not be read, and the subject was allowed when it last was, within |
| 67 | +// StaleFor. |
| 68 | +var ErrStale = errors.New("enforce: the organization could not be read; its last allow stands") |
| 69 | + |
| 70 | +type deciderView struct { |
| 71 | + view *reader.OrgView |
| 72 | + at time.Time |
| 73 | +} |
| 74 | + |
| 75 | +type deciderAnswer struct { |
| 76 | + allow bool |
| 77 | + seq uint64 |
| 78 | + at time.Time |
| 79 | +} |
| 80 | + |
| 81 | +type deciderFailure struct { |
| 82 | + err error |
| 83 | + at time.Time |
| 84 | +} |
| 85 | + |
| 86 | +func (d *Decider) now() time.Time { |
| 87 | + if d.Now != nil { |
| 88 | + return d.Now() |
| 89 | + } |
| 90 | + return time.Now() |
| 91 | +} |
| 92 | + |
| 93 | +func (d *Decider) allowFor() time.Duration { |
| 94 | + if d.AllowFor > 0 { |
| 95 | + return d.AllowFor |
| 96 | + } |
| 97 | + return DefaultFreshness |
| 98 | +} |
| 99 | + |
| 100 | +func (d *Decider) denyFor() time.Duration { |
| 101 | + if d.DenyFor > 0 { |
| 102 | + return min(d.DenyFor, d.allowFor()) |
| 103 | + } |
| 104 | + return min(DefaultDenyFor, d.allowFor()) |
| 105 | +} |
| 106 | + |
| 107 | +// Allows decides whether subject holds permission in the organization org |
| 108 | +// of hall now. The permission is asked in full, with no wildcard. |
| 109 | +func (d *Decider) Allows(ctx context.Context, hall string, org, subject []byte, permission string) (bool, error) { |
| 110 | + if d.Reader == nil || d.Spends == nil { |
| 111 | + return false, errors.New("enforce: a reader and a spend source are required") |
| 112 | + } |
| 113 | + if _, err := borgrec.Asked(permission); err != nil { |
| 114 | + return false, fmt.Errorf("enforce: %q is not a permission a service asks: %w", permission, err) |
| 115 | + } |
| 116 | + now := d.now() |
| 117 | + vk := hall + "|" + hex.EncodeToString(org) |
| 118 | + ak := vk + "|" + hex.EncodeToString(subject) + "|" + permission |
| 119 | + d.mu.Lock() |
| 120 | + v, f := d.views[vk], d.failed[vk] |
| 121 | + a, seen := d.answers[ak] |
| 122 | + d.mu.Unlock() |
| 123 | + // fail is a read that failed. An allow read within StaleFor stands, |
| 124 | + // but only at the seq it was read at: once a later token is verified, |
| 125 | + // an allow of an earlier one is never answered, so a host set that |
| 126 | + // withholds a revoking policy set cannot keep it. |
| 127 | + fail := func(err error) (bool, error) { |
| 128 | + if d.StaleFor > 0 && seen && a.allow && now.Sub(a.at) <= d.StaleFor && (v.view == nil || a.seq == v.view.Token.Seq) { |
| 129 | + return true, fmt.Errorf("%w: %v", ErrStale, err) |
| 130 | + } |
| 131 | + return false, err |
| 132 | + } |
| 133 | + stale := d.allowFor() |
| 134 | + if !seen || !a.allow { |
| 135 | + stale = d.denyFor() |
| 136 | + } |
| 137 | + if v.view == nil || now.Sub(v.at) >= stale { |
| 138 | + if f.err != nil && now.Sub(f.at) < d.denyFor() { |
| 139 | + return fail(f.err) |
| 140 | + } |
| 141 | + ov, err := d.Reader.Org(ctx, hall, org) |
| 142 | + if err != nil { |
| 143 | + err = fmt.Errorf("enforce: the organization's chain: %w", err) |
| 144 | + // A read cut short by the caller's own deadline says nothing of |
| 145 | + // the hosts, so it is not kept for other callers. |
| 146 | + if ctx.Err() == nil { |
| 147 | + d.mu.Lock() |
| 148 | + if len(d.failed) >= MaxCached { |
| 149 | + d.failed = nil |
| 150 | + } |
| 151 | + d.failed = setIn(d.failed, vk, deciderFailure{err: err, at: now}) |
| 152 | + d.mu.Unlock() |
| 153 | + } |
| 154 | + return fail(err) |
| 155 | + } |
| 156 | + d.mu.Lock() |
| 157 | + // A read that lands after a later one keeps the later view: a |
| 158 | + // view never goes back to an earlier seq. |
| 159 | + if cur, ok := d.views[vk]; ok && cur.view.Token.Seq > ov.Token.Seq { |
| 160 | + v = cur |
| 161 | + } else { |
| 162 | + v = deciderView{view: ov, at: now} |
| 163 | + if len(d.views) >= MaxCached { |
| 164 | + d.views = nil |
| 165 | + } |
| 166 | + d.views = setIn(d.views, vk, v) |
| 167 | + } |
| 168 | + delete(d.failed, vk) |
| 169 | + d.mu.Unlock() |
| 170 | + } |
| 171 | + if seen && a.seq == v.view.Token.Seq && (a.allow && now.Sub(a.at) < d.allowFor() || !a.allow && now.Sub(a.at) < d.denyFor()) { |
| 172 | + return a.allow, nil |
| 173 | + } |
| 174 | + dec, err := d.Reader.Allows(ctx, v.view, subject, permission, d.Spends, now) |
| 175 | + if err != nil { |
| 176 | + return fail(err) |
| 177 | + } |
| 178 | + if dec.Reason == "incomplete" { |
| 179 | + // The policy set the token commits to could not be read whole: a |
| 180 | + // failed read, not a deny, and not kept as an answer. |
| 181 | + return fail(errors.New("enforce: the organization's policy set could not be read whole")) |
| 182 | + } |
| 183 | + if !dec.Allow && (ctx.Err() != nil || len(dec.HostErr) >= len(d.Reader.Hosts) && len(dec.Grants) == 0) { |
| 184 | + // No host answered the subject's grants (or the caller's context |
| 185 | + // ended the read): the reader folds that into "not-member" with |
| 186 | + // no error, and it is a failed read, not a deny, and not kept. |
| 187 | + return fail(fmt.Errorf("enforce: no host answered the subject's grants (%d host errors)", len(dec.HostErr))) |
| 188 | + } |
| 189 | + d.mu.Lock() |
| 190 | + if len(d.answers) >= MaxCached { |
| 191 | + d.answers = nil |
| 192 | + } |
| 193 | + d.answers = setIn(d.answers, ak, deciderAnswer{allow: dec.Allow, seq: dec.Seq, at: now}) |
| 194 | + d.mu.Unlock() |
| 195 | + return dec.Allow, nil |
| 196 | +} |
| 197 | + |
| 198 | +// Forget drops every reused answer, view and failure. |
| 199 | +func (d *Decider) Forget() { |
| 200 | + d.mu.Lock() |
| 201 | + defer d.mu.Unlock() |
| 202 | + d.views, d.answers, d.failed = nil, nil, nil |
| 203 | +} |
| 204 | + |
| 205 | +func setIn[V any](m map[string]V, k string, v V) map[string]V { |
| 206 | + if m == nil { |
| 207 | + m = map[string]V{} |
| 208 | + } |
| 209 | + m[k] = v |
| 210 | + return m |
| 211 | +} |
0 commit comments