oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package team
2
3import (
4 "context"
5 "sync"
6 "time"
7)
8
9// AdmissionRequest asks the Authority whether a grant currently permits an
10// operation for an account. The store builds it from stored job fields,
11// never from a worker's arguments.
12type AdmissionRequest struct {
13 Project ProjectID
14 Grant string
15 Account AccountID
16 Target TargetBinding
17 Operation Operation
18}
19
20// Admission is the Authority's positive answer: the grant's current version
21// and the authority's epoch, valid until ExpiresAt (at most five seconds
22// after IssuedAt under the default policy). The store compares Version and
23// Epoch with the job's stored snapshot inside the admission transaction.
24type Admission struct {
25 Grant string
26 Version int64
27 Epoch int64
28 IssuedAt time.Time
29 ExpiresAt time.Time
30}
31
32// Authority is the online grant check. The console owns grants; this
33// package only sees Introspect. Errors: ErrGrantRevoked when the grant is
34// revoked, unknown, or does not cover the request; ErrAuthorityUnavailable
35// when no answer could be obtained. The second never counts as revocation.
36type Authority interface {
37 Introspect(ctx context.Context, req AdmissionRequest) (Admission, error)
38}
39
40// Grant is a FakeAuthority entry.
41type Grant struct {
42 Account AccountID
43 Operations map[Operation]bool
44 Repository string // "" matches any repository target
45 Subject string // "" matches any business target
46 Version int64
47 Revoked bool
48}
49
50// FakeAuthority is the deterministic in-memory Authority used by tests and
51// the fixture. Versions and the epoch move only through its methods; it
52// can be made unavailable to exercise fail-closed paths.
53type FakeAuthority struct {
54 mu sync.Mutex
55 clock Clock
56 ttl time.Duration
57 epoch int64
58 grants map[string]*Grant
59 unavailable bool
60 calls int
61}
62
63// NewFakeAuthority returns an authority issuing admissions valid for ttl.
64func NewFakeAuthority(clock Clock, ttl time.Duration) *FakeAuthority {
65 return &FakeAuthority{clock: clock, ttl: ttl, epoch: 1, grants: map[string]*Grant{}}
66}
67
68// Issue records a grant under ref at version 1.
69func (a *FakeAuthority) Issue(ref string, g Grant) {
70 a.mu.Lock()
71 defer a.mu.Unlock()
72 ops := map[Operation]bool{}
73 for k, v := range g.Operations {
74 ops[k] = v
75 }
76 g.Operations = ops
77 g.Version = 1
78 g.Revoked = false
79 a.grants[ref] = &g
80}
81
82// Revoke marks the grant revoked and bumps its version.
83func (a *FakeAuthority) Revoke(ref string) {
84 a.mu.Lock()
85 defer a.mu.Unlock()
86 if g, ok := a.grants[ref]; ok {
87 g.Revoked = true
88 g.Version++
89 }
90}
91
92// Amend bumps the grant's version without revoking it, the way a console
93// edit would. Stored snapshots then read as stale until revalidated.
94func (a *FakeAuthority) Amend(ref string) {
95 a.mu.Lock()
96 defer a.mu.Unlock()
97 if g, ok := a.grants[ref]; ok {
98 g.Version++
99 }
100}
101
102// RotateEpoch invalidates every stored snapshot, as a console restore does.
103func (a *FakeAuthority) RotateEpoch() {
104 a.mu.Lock()
105 defer a.mu.Unlock()
106 a.epoch++
107}
108
109// SetUnavailable makes Introspect answer ErrAuthorityUnavailable.
110func (a *FakeAuthority) SetUnavailable(v bool) {
111 a.mu.Lock()
112 defer a.mu.Unlock()
113 a.unavailable = v
114}
115
116// Calls counts Introspect calls, so a test can pin that admission went
117// online exactly once per dispatch.
118func (a *FakeAuthority) Calls() int {
119 a.mu.Lock()
120 defer a.mu.Unlock()
121 return a.calls
122}
123
124// Introspect implements Authority.
125func (a *FakeAuthority) Introspect(ctx context.Context, req AdmissionRequest) (Admission, error) {
126 if err := ctx.Err(); err != nil {
127 return Admission{}, fail(CodeAuthorityUnavailable, "%v", err)
128 }
129 a.mu.Lock()
130 defer a.mu.Unlock()
131 a.calls++
132 if a.unavailable {
133 return Admission{}, fail(CodeAuthorityUnavailable, "fake authority offline")
134 }
135 g, ok := a.grants[req.Grant]
136 if !ok || g.Revoked {
137 return Admission{}, fail(CodeGrantRevoked, "grant %q", req.Grant)
138 }
139 if g.Account != req.Account || !g.Operations[req.Operation] {
140 return Admission{}, fail(CodeGrantRevoked, "grant %q does not cover %s for %s", req.Grant, req.Operation, req.Account)
141 }
142 switch req.Target.Kind {
143 case TargetRepository, TargetProjectRead:
144 if req.Target.Kind == TargetProjectRead && req.Operation != OpProviderCall && req.Operation != OpJobContinue && req.Operation != OpAttemptContinue {
145 return Admission{}, ErrGrantRevoked
146 }
147 if g.Repository != "" && g.Repository != req.Target.Repository {
148 return Admission{}, fail(CodeGrantRevoked, "grant %q does not cover repository %s", req.Grant, req.Target.Repository)
149 }
150 case TargetConversation:
151 if req.Project != 0 || (req.Operation != OpProviderCall && req.Operation != OpJobContinue && req.Operation != OpAttemptContinue) {
152 return Admission{}, ErrGrantRevoked
153 }
154 case TargetBusiness:
155 if g.Subject != "" && g.Subject != req.Target.Subject {
156 return Admission{}, fail(CodeGrantRevoked, "grant %q does not cover subject %s", req.Grant, req.Target.Subject)
157 }
158 default:
159 return Admission{}, fail(CodeUnknownKind, "target kind %q", req.Target.Kind)
160 }
161 now := a.clock.Now()
162 return Admission{Grant: req.Grant, Version: g.Version, Epoch: a.epoch, IssuedAt: now, ExpiresAt: now.Add(a.ttl)}, nil
163}
164