Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
405 lines (377 loc) · 12.6 KB
/
Copy pathhandler.go
File metadata and controls
405 lines (377 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package telltale
import (
"context"
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/flanksource/commons-db/connection/store"
"github.com/flanksource/telltale/pkg/traceserve"
)
// maxStartBody bounds a start request.
const maxStartBody = 1 << 20
// stopWait bounds how long Close waits for the runs it stopped to finish.
const stopWait = 30 * time.Second
// HandlerOptions say which runs a Handler serves and where it is mounted.
type HandlerOptions struct {
// DataDir holds the run stores, under traces.Dir; new runs' commands run
// in it.
DataDir string
// Prefix is the path the handler is mounted at, such as /api/traces;
// requests reach it with the prefix still on their path.
Prefix string
// Connections answer the connection references of the runs it starts,
// and are what it lists at <prefix>/connections.
Connections *store.Store
// Definitions are the trace definitions it serves at
// <prefix>/definitions and arms each run it starts with.
Definitions Definitions
}
// RunsList is the runs a Handler lists, newest first.
type RunsList struct {
Runs []MountedRun `json:"runs"`
}
// MountedRun is one run and where its store is served.
type MountedRun struct {
RunInfo
// Mount is <prefix>/<run id>, the root of the run's traceserve routes.
Mount string `json:"mount"`
}
// StartForm is the start form's schema and the values it starts from.
type StartForm struct {
Schema any `json:"schema"`
Defaults map[string]any `json:"defaults"`
}
// Handler starts, stops, lists and serves the runs of a data dir, mounted at
// a prefix:
//
// GET <prefix> the runs, filtered by ?q, ?status, ?kind, ?since (RFC 3339), ?limit
// POST <prefix> start a run: a StartRequest JSON body, from the server's own origin
// GET <prefix>/start the start form (StartForm)
// GET <prefix>/connections the connections its runs can name (ConnectionInfo), of ?type alone
// * <prefix>/definitions/... the trace definitions (HandlerOptions.Definitions); a write only from its own origin
// DELETE <prefix>/<run id> stop a run this process is running
// * <prefix>/<run id>/... the run's store (traceserve.Handler): live from its
// running captures, else reopened read-only
//
// Runs it started stop when it is closed.
type Handler struct {
options HandlerOptions
ctx context.Context
cancel context.CancelFunc
mu sync.Mutex
opened map[string]openedStore
started map[string]*Run
}
type openedStore struct {
handler http.Handler
close func() error
}
// NewHandler serves the runs of options.DataDir at options.Prefix.
func NewHandler(options HandlerOptions) (*Handler, error) {
if options.DataDir == "" || !strings.HasPrefix(options.Prefix, "/") || strings.HasSuffix(options.Prefix, "/") {
return nil, fmt.Errorf("trace handler: a data dir and a prefix like /api/traces are required (got %q, %q)", options.DataDir, options.Prefix)
}
if options.Connections == nil {
return nil, fmt.Errorf("trace handler: a connection store is required, such as OpenConnections()")
}
if options.Definitions == nil {
return nil, fmt.Errorf("trace handler: trace definitions are required, such as definition.NewService")
}
ctx, cancel := context.WithCancel(context.Background())
return &Handler{options: options, ctx: ctx, cancel: cancel, opened: map[string]openedStore{}, started: map[string]*Run{}}, nil
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
prefix := h.options.Prefix
switch r.URL.Path {
case prefix:
h.serveRuns(w, r)
return
case prefix + "/start":
h.serveStartForm(w, r)
return
case prefix + "/connections":
h.serveConnections(w, r)
return
}
if r.URL.Path == prefix+"/definitions" || strings.HasPrefix(r.URL.Path, prefix+"/definitions/") {
h.serveDefinitions(w, r)
return
}
runID, rest, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, prefix+"/"), "/")
if !strings.HasPrefix(r.URL.Path, prefix+"/") || runID == "" || strings.Contains(runID, "..") {
http.Error(w, "name a run: "+prefix+"/<run id>/...", http.StatusNotFound)
return
}
if rest == "" && r.Method == http.MethodDelete {
h.stop(w, r, runID)
return
}
if rest == "" && !strings.HasSuffix(r.URL.Path, "/") {
w.Header().Set("Location", runID+"/")
w.WriteHeader(http.StatusMovedPermanently)
return
}
handler, err := h.store(runID)
switch {
case errors.Is(err, os.ErrNotExist):
http.Error(w, fmt.Sprintf("the trace store of run %s is gone: pruned, or never recorded", runID), http.StatusNotFound)
case err != nil:
http.Error(w, err.Error(), http.StatusInternalServerError)
default:
http.StripPrefix(prefix+"/"+runID, handler).ServeHTTP(w, r)
}
}
func (h *Handler) serveRuns(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
h.list(w, r)
case http.MethodPost:
h.start(w, r)
default:
w.Header().Set("Allow", "GET, POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
func (h *Handler) list(w http.ResponseWriter, r *http.Request) {
options, err := listOptions(h.options.DataDir, r.URL.Query())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
runs, err := List(options)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
list := RunsList{Runs: make([]MountedRun, 0, len(runs))}
for _, run := range runs {
list.Runs = append(list.Runs, h.mounted(run))
}
writeJSON(w, http.StatusOK, list)
}
// listOptions reads a list's filters: q, status (repeated or comma
// separated), kind, since (RFC 3339) and limit.
func listOptions(dataDir string, query url.Values) (ListOptions, error) {
options := ListOptions{DataDir: dataDir, Query: query.Get("q"), Kind: query.Get("kind")}
for _, values := range query["status"] {
for _, value := range strings.Split(values, ",") {
status, err := ParseStatus(value)
if err != nil {
return options, err
}
options.Status = append(options.Status, status)
}
}
if since := query.Get("since"); since != "" {
parsed, err := time.Parse(time.RFC3339, since)
if err != nil {
return options, fmt.Errorf("since: want an RFC 3339 time: %w", err)
}
options.Since = parsed
}
if limit := query.Get("limit"); limit != "" {
parsed, err := strconv.Atoi(limit)
if err != nil || parsed < 0 {
return options, fmt.Errorf("limit: want a count of runs, got %q", limit)
}
options.Limit = parsed
}
return options, nil
}
func (h *Handler) mounted(run RunInfo) MountedRun {
return MountedRun{RunInfo: run, Mount: h.options.Prefix + "/" + run.RunID}
}
func (h *Handler) serveStartForm(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
schema, defaults, err := StartSchema()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, http.StatusOK, StartForm{Schema: schema, Defaults: defaults})
}
func (h *Handler) serveConnections(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
connections, err := ListConnections(h.options.Connections, r.URL.Query().Get("type"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, http.StatusOK, map[string][]ConnectionInfo{"connections": connections})
}
// start runs the command a StartRequest names. It runs a shell command, so
// it takes only a JSON body from the server's own origin: a cross-site form
// cannot send one, and a cross-site script is held to a CORS preflight the
// handler never answers.
func (h *Handler) start(w http.ResponseWriter, r *http.Request) {
if err := errors.Join(sameOrigin(r), jsonBody(r)); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
var request StartRequest
decoder := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxStartBody))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&request); err != nil {
writeJSON(w, http.StatusBadRequest, rootError("decode start request: "+err.Error()))
return
}
options, err := request.Options(StartOptions{DataDir: h.options.DataDir, Connections: h.options.Connections}, h.options.Definitions)
var invalid *ValidationError
if errors.As(err, &invalid) {
writeJSON(w, http.StatusBadRequest, invalid)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
run, err := Start(h.ctx, options)
if err != nil {
writeJSON(w, http.StatusBadRequest, rootError(err.Error()))
return
}
h.track(run)
writeJSON(w, http.StatusCreated, h.mounted(run.Info()))
}
// serveDefinitions hands the definitions routes to HandlerOptions.Definitions,
// refusing a write from another origin as a start is refused.
func (h *Handler) serveDefinitions(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
if err := sameOrigin(r); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
}
http.StripPrefix(h.options.Prefix, h.options.Definitions).ServeHTTP(w, r)
}
// rootError refuses a start request as a whole, such as a capture that could
// not start, rather than one of its fields.
func rootError(message string) *ValidationError {
return &ValidationError{Fields: []FieldError{{InstancePath: "", Message: message}}}
}
func jsonBody(r *http.Request) error {
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil || mediaType != "application/json" {
return fmt.Errorf("send an application/json body, not %q", r.Header.Get("Content-Type"))
}
return nil
}
func sameOrigin(r *http.Request) error {
origin := r.Header.Get("Origin")
if origin == "" {
return nil
}
if parsed, err := url.Parse(origin); err != nil || parsed.Host != r.Host {
return fmt.Errorf("start or stop a run from the server's own origin, not %q", origin)
}
return nil
}
// stop stops a run this process is running. DELETE is never a simple
// request, so a cross-site script is held to the preflight as a POST is.
func (h *Handler) stop(w http.ResponseWriter, r *http.Request, runID string) {
if err := sameOrigin(r); err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
err := Stop(StopOptions{DataDir: h.options.DataDir, RunID: runID})
switch {
case errors.Is(err, ErrNotLive):
http.Error(w, err.Error(), http.StatusConflict)
case err != nil:
http.Error(w, err.Error(), http.StatusInternalServerError)
default:
w.WriteHeader(http.StatusAccepted)
}
}
// track keeps a run the handler started until it is done, for Close to wait on.
func (h *Handler) track(run *Run) {
h.mu.Lock()
h.started[run.ID()] = run
h.mu.Unlock()
go func() {
<-run.Done()
h.mu.Lock()
delete(h.started, run.ID())
h.mu.Unlock()
}()
}
// store is the handler of runID's store: the live run's own runtime while
// this process runs it, else the store reopened (Reopen), a standing store's
// through its own opener.
func (h *Handler) store(runID string) (http.Handler, error) {
run, ok, err := Live(h.options.DataDir, runID)
if err != nil {
return nil, err
}
if ok {
return traceserve.Handler(run.Runtime())
}
h.mu.Lock()
defer h.mu.Unlock()
if store, ok := h.opened[runID]; ok {
return store.handler, nil
}
runtime, closeStore, err := Reopen(h.options.DataDir, runID)
if err != nil {
return nil, err
}
handler, err := traceserve.Handler(runtime)
if err != nil {
return nil, errors.Join(err, closeStore())
}
h.opened[runID] = openedStore{handler: handler, close: closeStore}
return handler, nil
}
func writeJSON(w http.ResponseWriter, status int, value any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(value); err != nil {
panic(http.ErrAbortHandler)
}
}
// Close stops the runs the handler started, waits for them to write their
// manifests, and closes every store it reopened.
func (h *Handler) Close() error {
h.cancel()
h.mu.Lock()
running := make([]*Run, 0, len(h.started))
for _, run := range h.started {
running = append(running, run)
}
h.mu.Unlock()
var errs []error
deadline := time.After(stopWait)
for _, run := range running {
select {
case <-run.Done():
if _, err := run.Wait(); err != nil {
errs = append(errs, err)
}
case <-deadline:
errs = append(errs, fmt.Errorf("run %s did not finish within %s of the handler closing", run.ID(), stopWait))
}
}
h.mu.Lock()
defer h.mu.Unlock()
for runID, store := range h.opened {
errs = append(errs, store.close())
delete(h.opened, runID)
}
return errors.Join(errs...)
}