1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/controller/api"
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/auth"
17 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 "git.arvados.org/arvados.git/sdk/go/httpserver"
19 "github.com/gorilla/mux"
20 "github.com/sirupsen/logrus"
30 // Return an error if request body exceeds this size. 0 means
34 // If wrapCalls is not nil, it is called once for each API
35 // method, and the returned method is used in its place. This
36 // can be used to install hooks before and after each API call
37 // and alter responses; see localdb.WrapCallsInTransaction for
39 WrapCalls func(api.RoutableFunc) api.RoutableFunc
42 // New returns a new router (which implements the http.Handler
43 // interface) that serves requests by calling Arvados API methods on
45 func New(backend arvados.API, config Config) *router {
55 func (rtr *router) addRoutes() {
56 for _, route := range []struct {
57 endpoint arvados.APIEndpoint
58 defaultOpts func() interface{}
62 arvados.EndpointConfigGet,
63 func() interface{} { return &struct{}{} },
64 func(ctx context.Context, opts interface{}) (interface{}, error) {
65 return rtr.backend.ConfigGet(ctx)
69 arvados.EndpointLogin,
70 func() interface{} { return &arvados.LoginOptions{} },
71 func(ctx context.Context, opts interface{}) (interface{}, error) {
72 return rtr.backend.Login(ctx, *opts.(*arvados.LoginOptions))
76 arvados.EndpointLogout,
77 func() interface{} { return &arvados.LogoutOptions{} },
78 func(ctx context.Context, opts interface{}) (interface{}, error) {
79 return rtr.backend.Logout(ctx, *opts.(*arvados.LogoutOptions))
83 arvados.EndpointCollectionCreate,
84 func() interface{} { return &arvados.CreateOptions{} },
85 func(ctx context.Context, opts interface{}) (interface{}, error) {
86 return rtr.backend.CollectionCreate(ctx, *opts.(*arvados.CreateOptions))
90 arvados.EndpointCollectionUpdate,
91 func() interface{} { return &arvados.UpdateOptions{} },
92 func(ctx context.Context, opts interface{}) (interface{}, error) {
93 return rtr.backend.CollectionUpdate(ctx, *opts.(*arvados.UpdateOptions))
97 arvados.EndpointCollectionGet,
98 func() interface{} { return &arvados.GetOptions{} },
99 func(ctx context.Context, opts interface{}) (interface{}, error) {
100 return rtr.backend.CollectionGet(ctx, *opts.(*arvados.GetOptions))
104 arvados.EndpointCollectionList,
105 func() interface{} { return &arvados.ListOptions{Limit: -1} },
106 func(ctx context.Context, opts interface{}) (interface{}, error) {
107 return rtr.backend.CollectionList(ctx, *opts.(*arvados.ListOptions))
111 arvados.EndpointCollectionProvenance,
112 func() interface{} { return &arvados.GetOptions{} },
113 func(ctx context.Context, opts interface{}) (interface{}, error) {
114 return rtr.backend.CollectionProvenance(ctx, *opts.(*arvados.GetOptions))
118 arvados.EndpointCollectionUsedBy,
119 func() interface{} { return &arvados.GetOptions{} },
120 func(ctx context.Context, opts interface{}) (interface{}, error) {
121 return rtr.backend.CollectionUsedBy(ctx, *opts.(*arvados.GetOptions))
125 arvados.EndpointCollectionDelete,
126 func() interface{} { return &arvados.DeleteOptions{} },
127 func(ctx context.Context, opts interface{}) (interface{}, error) {
128 return rtr.backend.CollectionDelete(ctx, *opts.(*arvados.DeleteOptions))
132 arvados.EndpointCollectionTrash,
133 func() interface{} { return &arvados.DeleteOptions{} },
134 func(ctx context.Context, opts interface{}) (interface{}, error) {
135 return rtr.backend.CollectionTrash(ctx, *opts.(*arvados.DeleteOptions))
139 arvados.EndpointCollectionUntrash,
140 func() interface{} { return &arvados.UntrashOptions{} },
141 func(ctx context.Context, opts interface{}) (interface{}, error) {
142 return rtr.backend.CollectionUntrash(ctx, *opts.(*arvados.UntrashOptions))
146 arvados.EndpointContainerCreate,
147 func() interface{} { return &arvados.CreateOptions{} },
148 func(ctx context.Context, opts interface{}) (interface{}, error) {
149 return rtr.backend.ContainerCreate(ctx, *opts.(*arvados.CreateOptions))
153 arvados.EndpointContainerUpdate,
154 func() interface{} { return &arvados.UpdateOptions{} },
155 func(ctx context.Context, opts interface{}) (interface{}, error) {
156 return rtr.backend.ContainerUpdate(ctx, *opts.(*arvados.UpdateOptions))
160 arvados.EndpointContainerGet,
161 func() interface{} { return &arvados.GetOptions{} },
162 func(ctx context.Context, opts interface{}) (interface{}, error) {
163 return rtr.backend.ContainerGet(ctx, *opts.(*arvados.GetOptions))
167 arvados.EndpointContainerList,
168 func() interface{} { return &arvados.ListOptions{Limit: -1} },
169 func(ctx context.Context, opts interface{}) (interface{}, error) {
170 return rtr.backend.ContainerList(ctx, *opts.(*arvados.ListOptions))
174 arvados.EndpointContainerDelete,
175 func() interface{} { return &arvados.DeleteOptions{} },
176 func(ctx context.Context, opts interface{}) (interface{}, error) {
177 return rtr.backend.ContainerDelete(ctx, *opts.(*arvados.DeleteOptions))
181 arvados.EndpointContainerRequestCreate,
182 func() interface{} { return &arvados.CreateOptions{} },
183 func(ctx context.Context, opts interface{}) (interface{}, error) {
184 return rtr.backend.ContainerRequestCreate(ctx, *opts.(*arvados.CreateOptions))
188 arvados.EndpointContainerRequestUpdate,
189 func() interface{} { return &arvados.UpdateOptions{} },
190 func(ctx context.Context, opts interface{}) (interface{}, error) {
191 return rtr.backend.ContainerRequestUpdate(ctx, *opts.(*arvados.UpdateOptions))
195 arvados.EndpointContainerRequestGet,
196 func() interface{} { return &arvados.GetOptions{} },
197 func(ctx context.Context, opts interface{}) (interface{}, error) {
198 return rtr.backend.ContainerRequestGet(ctx, *opts.(*arvados.GetOptions))
202 arvados.EndpointContainerRequestList,
203 func() interface{} { return &arvados.ListOptions{Limit: -1} },
204 func(ctx context.Context, opts interface{}) (interface{}, error) {
205 return rtr.backend.ContainerRequestList(ctx, *opts.(*arvados.ListOptions))
209 arvados.EndpointContainerRequestDelete,
210 func() interface{} { return &arvados.DeleteOptions{} },
211 func(ctx context.Context, opts interface{}) (interface{}, error) {
212 return rtr.backend.ContainerRequestDelete(ctx, *opts.(*arvados.DeleteOptions))
216 arvados.EndpointContainerLock,
218 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
220 func(ctx context.Context, opts interface{}) (interface{}, error) {
221 return rtr.backend.ContainerLock(ctx, *opts.(*arvados.GetOptions))
225 arvados.EndpointContainerUnlock,
227 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
229 func(ctx context.Context, opts interface{}) (interface{}, error) {
230 return rtr.backend.ContainerUnlock(ctx, *opts.(*arvados.GetOptions))
234 arvados.EndpointContainerSSH,
235 func() interface{} { return &arvados.ContainerSSHOptions{} },
236 func(ctx context.Context, opts interface{}) (interface{}, error) {
237 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
241 arvados.EndpointGroupCreate,
242 func() interface{} { return &arvados.CreateOptions{} },
243 func(ctx context.Context, opts interface{}) (interface{}, error) {
244 return rtr.backend.GroupCreate(ctx, *opts.(*arvados.CreateOptions))
248 arvados.EndpointGroupUpdate,
249 func() interface{} { return &arvados.UpdateOptions{} },
250 func(ctx context.Context, opts interface{}) (interface{}, error) {
251 return rtr.backend.GroupUpdate(ctx, *opts.(*arvados.UpdateOptions))
255 arvados.EndpointGroupList,
256 func() interface{} { return &arvados.ListOptions{Limit: -1} },
257 func(ctx context.Context, opts interface{}) (interface{}, error) {
258 return rtr.backend.GroupList(ctx, *opts.(*arvados.ListOptions))
262 arvados.EndpointGroupContents,
263 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
264 func(ctx context.Context, opts interface{}) (interface{}, error) {
265 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
269 arvados.EndpointGroupContentsUUIDInPath,
270 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
271 func(ctx context.Context, opts interface{}) (interface{}, error) {
272 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
276 arvados.EndpointGroupShared,
277 func() interface{} { return &arvados.ListOptions{Limit: -1} },
278 func(ctx context.Context, opts interface{}) (interface{}, error) {
279 return rtr.backend.GroupShared(ctx, *opts.(*arvados.ListOptions))
283 arvados.EndpointGroupGet,
284 func() interface{} { return &arvados.GetOptions{} },
285 func(ctx context.Context, opts interface{}) (interface{}, error) {
286 return rtr.backend.GroupGet(ctx, *opts.(*arvados.GetOptions))
290 arvados.EndpointGroupDelete,
291 func() interface{} { return &arvados.DeleteOptions{} },
292 func(ctx context.Context, opts interface{}) (interface{}, error) {
293 return rtr.backend.GroupDelete(ctx, *opts.(*arvados.DeleteOptions))
297 arvados.EndpointGroupTrash,
298 func() interface{} { return &arvados.DeleteOptions{} },
299 func(ctx context.Context, opts interface{}) (interface{}, error) {
300 return rtr.backend.GroupTrash(ctx, *opts.(*arvados.DeleteOptions))
304 arvados.EndpointGroupUntrash,
305 func() interface{} { return &arvados.UntrashOptions{} },
306 func(ctx context.Context, opts interface{}) (interface{}, error) {
307 return rtr.backend.GroupUntrash(ctx, *opts.(*arvados.UntrashOptions))
311 arvados.EndpointSpecimenCreate,
312 func() interface{} { return &arvados.CreateOptions{} },
313 func(ctx context.Context, opts interface{}) (interface{}, error) {
314 return rtr.backend.SpecimenCreate(ctx, *opts.(*arvados.CreateOptions))
318 arvados.EndpointSpecimenUpdate,
319 func() interface{} { return &arvados.UpdateOptions{} },
320 func(ctx context.Context, opts interface{}) (interface{}, error) {
321 return rtr.backend.SpecimenUpdate(ctx, *opts.(*arvados.UpdateOptions))
325 arvados.EndpointSpecimenGet,
326 func() interface{} { return &arvados.GetOptions{} },
327 func(ctx context.Context, opts interface{}) (interface{}, error) {
328 return rtr.backend.SpecimenGet(ctx, *opts.(*arvados.GetOptions))
332 arvados.EndpointSpecimenList,
333 func() interface{} { return &arvados.ListOptions{Limit: -1} },
334 func(ctx context.Context, opts interface{}) (interface{}, error) {
335 return rtr.backend.SpecimenList(ctx, *opts.(*arvados.ListOptions))
339 arvados.EndpointSpecimenDelete,
340 func() interface{} { return &arvados.DeleteOptions{} },
341 func(ctx context.Context, opts interface{}) (interface{}, error) {
342 return rtr.backend.SpecimenDelete(ctx, *opts.(*arvados.DeleteOptions))
346 arvados.EndpointUserCreate,
347 func() interface{} { return &arvados.CreateOptions{} },
348 func(ctx context.Context, opts interface{}) (interface{}, error) {
349 return rtr.backend.UserCreate(ctx, *opts.(*arvados.CreateOptions))
353 arvados.EndpointUserMerge,
354 func() interface{} { return &arvados.UserMergeOptions{} },
355 func(ctx context.Context, opts interface{}) (interface{}, error) {
356 return rtr.backend.UserMerge(ctx, *opts.(*arvados.UserMergeOptions))
360 arvados.EndpointUserActivate,
361 func() interface{} { return &arvados.UserActivateOptions{} },
362 func(ctx context.Context, opts interface{}) (interface{}, error) {
363 return rtr.backend.UserActivate(ctx, *opts.(*arvados.UserActivateOptions))
367 arvados.EndpointUserSetup,
368 func() interface{} { return &arvados.UserSetupOptions{} },
369 func(ctx context.Context, opts interface{}) (interface{}, error) {
370 return rtr.backend.UserSetup(ctx, *opts.(*arvados.UserSetupOptions))
374 arvados.EndpointUserUnsetup,
375 func() interface{} { return &arvados.GetOptions{} },
376 func(ctx context.Context, opts interface{}) (interface{}, error) {
377 return rtr.backend.UserUnsetup(ctx, *opts.(*arvados.GetOptions))
381 arvados.EndpointUserGetCurrent,
382 func() interface{} { return &arvados.GetOptions{} },
383 func(ctx context.Context, opts interface{}) (interface{}, error) {
384 return rtr.backend.UserGetCurrent(ctx, *opts.(*arvados.GetOptions))
388 arvados.EndpointUserGetSystem,
389 func() interface{} { return &arvados.GetOptions{} },
390 func(ctx context.Context, opts interface{}) (interface{}, error) {
391 return rtr.backend.UserGetSystem(ctx, *opts.(*arvados.GetOptions))
395 arvados.EndpointUserGet,
396 func() interface{} { return &arvados.GetOptions{} },
397 func(ctx context.Context, opts interface{}) (interface{}, error) {
398 return rtr.backend.UserGet(ctx, *opts.(*arvados.GetOptions))
402 arvados.EndpointUserUpdateUUID,
403 func() interface{} { return &arvados.UpdateUUIDOptions{} },
404 func(ctx context.Context, opts interface{}) (interface{}, error) {
405 return rtr.backend.UserUpdateUUID(ctx, *opts.(*arvados.UpdateUUIDOptions))
409 arvados.EndpointUserUpdate,
410 func() interface{} { return &arvados.UpdateOptions{} },
411 func(ctx context.Context, opts interface{}) (interface{}, error) {
412 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
416 arvados.EndpointUserList,
417 func() interface{} { return &arvados.ListOptions{Limit: -1} },
418 func(ctx context.Context, opts interface{}) (interface{}, error) {
419 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
423 arvados.EndpointUserBatchUpdate,
424 func() interface{} { return &arvados.UserBatchUpdateOptions{} },
425 func(ctx context.Context, opts interface{}) (interface{}, error) {
426 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
430 arvados.EndpointUserDelete,
431 func() interface{} { return &arvados.DeleteOptions{} },
432 func(ctx context.Context, opts interface{}) (interface{}, error) {
433 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
437 arvados.EndpointUserAuthenticate,
438 func() interface{} { return &arvados.UserAuthenticateOptions{} },
439 func(ctx context.Context, opts interface{}) (interface{}, error) {
440 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
445 if rtr.config.WrapCalls != nil {
446 exec = rtr.config.WrapCalls(exec)
448 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
450 rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
451 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
453 rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
454 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
458 var altMethod = map[string]string{
459 "PATCH": "PUT", // Accept PUT as a synonym for PATCH
460 "GET": "HEAD", // Accept HEAD at any GET route
463 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
464 methods := []string{endpoint.Method}
465 if alt, ok := altMethod[endpoint.Method]; ok {
466 methods = append(methods, alt)
468 rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
469 logger := ctxlog.FromContext(req.Context())
470 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey)
472 logger.WithFields(logrus.Fields{
474 "method": endpoint.Method,
475 "endpoint": endpoint,
476 }).WithError(err).Debug("error loading request params")
477 rtr.sendError(w, err)
480 opts := defaultOpts()
481 err = rtr.transcode(params, opts)
483 logger.WithField("params", params).WithError(err).Debugf("error transcoding params to %T", opts)
484 rtr.sendError(w, err)
487 respOpts, err := rtr.responseOptions(opts)
489 logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
490 rtr.sendError(w, err)
494 creds := auth.CredentialsFromRequest(req)
495 err = creds.LoadTokensFromHTTPRequestBody(req)
497 rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
500 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
501 for _, t := range rt {
502 if t, ok := t.(string); ok {
503 creds.Tokens = append(creds.Tokens, t)
507 ctx := auth.NewContext(req.Context(), creds)
509 if reqId = req.Header.Get("X-Request-Id"); reqId == "" {
510 reqIDGen := httpserver.IDGenerator{Prefix: "req-"}
511 reqId = reqIDGen.Next()
513 ctx = arvados.ContextWithRequestID(ctx, reqId)
514 logger.WithFields(logrus.Fields{
515 "apiEndpoint": endpoint,
516 "apiOptsType": fmt.Sprintf("%T", opts),
519 resp, err := exec(ctx, opts)
521 logger.WithError(err).Debugf("returning error type %T", err)
522 rtr.sendError(w, err)
525 rtr.sendResponse(w, req, resp, respOpts, reqId)
529 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
530 switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
531 case "login", "logout", "auth":
533 w.Header().Set("Access-Control-Allow-Origin", "*")
534 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, PUT, POST, PATCH, DELETE")
535 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Http-Method-Override")
536 w.Header().Set("Access-Control-Max-Age", "86486400")
538 if r.Method == "OPTIONS" {
542 // Wrap r.Body in a http.MaxBytesReader(), otherwise
543 // r.ParseForm() uses a default max request body size
544 // of 10 megabytes. Note we rely on the Nginx
545 // configuration to enforce the real max body size.
546 max := int64(rtr.config.MaxRequestSize)
548 max = math.MaxInt64 - 1
550 r.Body = http.MaxBytesReader(w, r.Body, max)
552 if r.Method == "POST" {
555 if err.Error() == "http: request body too large" {
556 err = httpError(http.StatusRequestEntityTooLarge, err)
558 rtr.sendError(w, err)
561 if m := r.FormValue("_method"); m != "" {
565 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
571 rtr.mux.ServeHTTP(w, r)