oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runnerproto
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/team"
5 "context"
6 "github.com/go-chi/chi/v5"
7 "mime"
8 "net/http"
9 "strconv"
10 "strings"
11 "time"
12)
13
14type ExecutionAPI interface {
15 RecoverCoordinator(context.Context, RunnerPrincipal, string, int64) (CoordinatorClaim, error)
16 CoordinatorToolResult(context.Context, RunnerPrincipal, CoordinatorClaim, CoordinatorToolCall) (CoordinatorToolResult, error)
17 CallCoordinatorTool(context.Context, RunnerPrincipal, CoordinatorClaim, CoordinatorToolCall) (CoordinatorToolResult, error)
18 PollCoordinator(context.Context, RunnerPrincipal) ([]CoordinatorTurn, error)
19 ClaimCoordinator(context.Context, RunnerPrincipal, string, int64) (CoordinatorClaim, error)
20 RenewCoordinator(context.Context, RunnerPrincipal, CoordinatorClaim) (time.Time, error)
21 CoordinatorEvent(context.Context, RunnerPrincipal, CoordinatorClaim, Event) error
22 ContainCoordinator(context.Context, RunnerPrincipal, CoordinatorClaim, ContainmentProof) error
23
24 Sync(context.Context) error
25 Poll(context.Context, RunnerPrincipal, string) ([]Assignment, error)
26 Claim(context.Context, RunnerPrincipal, ClaimRequest) (ClaimResponse, error)
27 RecoverClaim(context.Context, RunnerPrincipal, ClaimRequest) (ClaimResponse, error)
28 Mutate(context.Context, RunnerPrincipal, string, RunnerRequest) (any, error)
29 Cleanup(context.Context, string, string, RunnerRequest) (any, error)
30 AcknowledgeRotation(context.Context, RunnerPrincipal, string) error
31 ReadArtifact(context.Context, RunnerPrincipal, RunnerRequest, team.ArtifactID) (team.Artifact, error)
32}
33type Handler struct {
34 ReportRepositories func(context.Context, RunnerPrincipal, []string) error
35 AccountSetups AccountSetupAPI
36 Execution func() ExecutionAPI
37 AuthenticateRunner func(context.Context, string) (RunnerPrincipal, error)
38 ReportConnections func(context.Context, RunnerPrincipal, []ConnectionReport) error
39 PutBlob func(context.Context, RunnerPrincipal, team.Scope, team.Handle, string, []byte) error
40 Blob func(context.Context, RunnerPrincipal, team.Scope, string) ([]byte, error)
41 Failure func(http.ResponseWriter, error)
42 JSON func(http.ResponseWriter, int, any)
43 Invalid, Forbidden, Unavailable error
44 MaxBlobBytes int64
45}
46
47func (s *Handler) Router() chi.Router {
48 r := chi.NewRouter()
49 r.Post("/account-setups", s.runnerAccountSetup)
50 // Thin v1: coordinator turns and work arrive on the runner stream
51 // (/api/runner/stream). The poll and claim routes are gone.
52 r.Post("/blobs", s.runnerPutBlob)
53 r.Post("/blobs/read", s.runnerReadBlob)
54 for _, op := range []string{"validate", "evidence", "freeze", "freeze_feature", "conclude", "publication_ready", "publication_authorize", "question_resolved", "questions", "answers", "renew", "prepare", "events", "artifacts", "intend", "admit", "receipts", "lost", "complete", "release", "stop", "contained"} {
55 r.Post("/"+op, s.runnerMutation(op))
56 }
57 r.Post("/connections", s.runnerConnections)
58 r.Post("/heartbeat", s.runnerConnections)
59 r.Get("/artifacts/{assignment}/{artifact}", s.runnerArtifact)
60 return r
61}
62func (s *Handler) Body(w http.ResponseWriter, r *http.Request, v any, limit int64) bool {
63 content, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
64 if err != nil || content != "application/json" || DecodeLimit(r.Body, v, limit) != nil {
65 s.Failure(w, s.Invalid)
66 return false
67 }
68 return true
69}
70func (s *Handler) Bearer(w http.ResponseWriter, r *http.Request) (RunnerPrincipal, string, bool) {
71 w.Header().Set("Cache-Control", "no-store")
72 values := r.Header.Values("Authorization")
73 if len(r.Header.Values("Cookie")) != 0 || len(values) != 1 || !strings.HasPrefix(values[0], "Bearer ") {
74 s.Failure(w, s.Forbidden)
75 return RunnerPrincipal{}, "", false
76 }
77 token := strings.TrimPrefix(values[0], "Bearer ")
78 p, err := s.AuthenticateRunner(r.Context(), token)
79 if err != nil {
80 s.Failure(w, err)
81 return p, "", false
82 }
83 return p, token, true
84}
85func (s *Handler) runnerMutation(op string) http.HandlerFunc {
86 return func(w http.ResponseWriter, r *http.Request) {
87 if s.Execution() == nil {
88 s.Failure(w, s.Unavailable)
89 return
90 }
91 var q RunnerRequest
92 limit := int64(MaxActionBytes)
93 if op == "artifacts" {
94 limit = 2 * MaxArtifactBytes
95 }
96 if op == "events" {
97 limit = MaxEventBytes + 4096
98 }
99 if !s.Body(w, r, &q, limit) {
100 return
101 }
102 var value any
103 var err error
104 if strings.HasPrefix(r.Header.Get("Authorization"), "Cleanup ") {
105 if len(r.Header.Values("Authorization")) != 1 || len(r.Header.Values("Cookie")) != 0 {
106 s.Failure(w, s.Forbidden)
107 return
108 }
109 value, err = s.Execution().Cleanup(r.Context(), strings.TrimPrefix(r.Header.Get("Authorization"), "Cleanup "), op, q)
110 } else {
111 p, _, ok := s.Bearer(w, r)
112 if !ok {
113 return
114 }
115 value, err = s.Execution().Mutate(r.Context(), p, op, q)
116 }
117 if err != nil {
118 s.Failure(w, err)
119 return
120 }
121 s.JSON(w, 200, WorkResponse{Version: 2, Result: value})
122 }
123}
124func (s *Handler) runnerConnections(w http.ResponseWriter, r *http.Request) {
125 p, _, ok := s.Bearer(w, r)
126 if !ok {
127 return
128 }
129 var q HeartbeatRequest
130 if !s.Body(w, r, &q, MaxActionBytes) {
131 return
132 }
133 if q.Version != 2 || q.RequestID == "" || len(q.RequestID) > 256 {
134 s.Failure(w, s.Invalid)
135 return
136 }
137 if q.RotationRequestID != "" {
138 if s.Execution() == nil {
139 s.Failure(w, s.Unavailable)
140 return
141 }
142 if err := s.Execution().AcknowledgeRotation(r.Context(), p, q.RotationRequestID); err != nil {
143 s.Failure(w, err)
144 return
145 }
146 }
147 if len(q.Reports) > 0 {
148 var reports []ConnectionReport
149 if DecodeLimit(strings.NewReader(string(q.Reports)), &reports, MaxActionBytes) != nil {
150 s.Failure(w, s.Invalid)
151 return
152 }
153 if err := s.ReportConnections(r.Context(), p, reports); err != nil {
154 s.Failure(w, err)
155 return
156 }
157 }
158 if s.ReportRepositories != nil && len(q.Reports) > 0 {
159 if err := s.ReportRepositories(r.Context(), p, q.Repositories); err != nil {
160 s.Failure(w, err)
161 return
162 }
163 }
164 s.JSON(w, 200, WorkResponse{Version: 2, Result: struct{}{}})
165}
166func (s *Handler) runnerArtifact(w http.ResponseWriter, r *http.Request) {
167 p, _, ok := s.Bearer(w, r)
168 if !ok {
169 return
170 }
171 if s.Execution() == nil {
172 s.Failure(w, s.Unavailable)
173 return
174 }
175 id, err := strconv.ParseInt(chi.URLParam(r, "artifact"), 10, 64)
176 if err != nil {
177 s.Failure(w, s.Invalid)
178 return
179 }
180 q := r.URL.Query()
181 if q.Get("version") != "1" {
182 s.Failure(w, s.Invalid)
183 return
184 }
185 attempt, err := strconv.ParseInt(q.Get("attempt"), 10, 64)
186 if err != nil {
187 s.Failure(w, s.Invalid)
188 return
189 }
190 fence, err := strconv.ParseInt(q.Get("fence"), 10, 64)
191 if err != nil {
192 s.Failure(w, s.Invalid)
193 return
194 }
195 job, err := strconv.ParseInt(q.Get("job"), 10, 64)
196 if err != nil {
197 s.Failure(w, s.Invalid)
198 return
199 }
200 request := RunnerRequest{Version: 2, AssignmentID: chi.URLParam(r, "assignment"), RequestID: q.Get("request_id"), Scope: Scope{Account: team.AccountID(q.Get("account")), Job: team.JobID(job)}, Handle: team.Handle{Attempt: team.AttemptID(attempt), Fence: fence, Runner: p.RunnerID}}
201 value, err := s.Execution().ReadArtifact(r.Context(), p, request, team.ArtifactID(id))
202 if err != nil {
203 s.Failure(w, err)
204 return
205 }
206 s.JSON(w, 200, WorkResponse{Version: 2, Result: value})
207}
208
209func (s *Handler) runnerPutBlob(w http.ResponseWriter, r *http.Request) {
210 p, _, ok := s.Bearer(w, r)
211 if !ok {
212 return
213 }
214 var q BlobRequest
215 if !s.Body(w, r, &q, 2*s.MaxBlobBytes) {
216 return
217 }
218 if q.Version != 2 {
219 s.Failure(w, s.Invalid)
220 return
221 }
222 if err := s.PutBlob(r.Context(), p, q.Scope, q.Handle, q.Digest, q.Bytes); err != nil {
223 s.Failure(w, err)
224 return
225 }
226 s.JSON(w, 200, WorkResponse{Version: 2, Result: struct{}{}})
227}
228func (s *Handler) runnerReadBlob(w http.ResponseWriter, r *http.Request) {
229 p, _, ok := s.Bearer(w, r)
230 if !ok {
231 return
232 }
233 var q BlobReadRequest
234 if !s.Body(w, r, &q, 8192) {
235 return
236 }
237 if q.Version != 2 {
238 s.Failure(w, s.Invalid)
239 return
240 }
241 b, err := s.Blob(r.Context(), p, q.Scope, q.Digest)
242 if err != nil {
243 s.Failure(w, err)
244 return
245 }
246 s.JSON(w, 200, WorkResponse{Version: 2, Result: b})
247}
248