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.EndpointUserUpdate,
403 func() interface{} { return &arvados.UpdateOptions{} },
404 func(ctx context.Context, opts interface{}) (interface{}, error) {
405 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
409 arvados.EndpointUserList,
410 func() interface{} { return &arvados.ListOptions{Limit: -1} },
411 func(ctx context.Context, opts interface{}) (interface{}, error) {
412 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
416 arvados.EndpointUserBatchUpdate,
417 func() interface{} { return &arvados.UserBatchUpdateOptions{} },
418 func(ctx context.Context, opts interface{}) (interface{}, error) {
419 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
423 arvados.EndpointUserDelete,
424 func() interface{} { return &arvados.DeleteOptions{} },
425 func(ctx context.Context, opts interface{}) (interface{}, error) {
426 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
430 arvados.EndpointUserAuthenticate,
431 func() interface{} { return &arvados.UserAuthenticateOptions{} },
432 func(ctx context.Context, opts interface{}) (interface{}, error) {
433 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
438 if rtr.config.WrapCalls != nil {
439 exec = rtr.config.WrapCalls(exec)
441 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
443 rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
444 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
446 rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
447 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
451 var altMethod = map[string]string{
452 "PATCH": "PUT", // Accept PUT as a synonym for PATCH
453 "GET": "HEAD", // Accept HEAD at any GET route
456 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
457 methods := []string{endpoint.Method}
458 if alt, ok := altMethod[endpoint.Method]; ok {
459 methods = append(methods, alt)
461 rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
462 logger := ctxlog.FromContext(req.Context())
463 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey)
465 logger.WithFields(logrus.Fields{
467 "method": endpoint.Method,
468 "endpoint": endpoint,
469 }).WithError(err).Debug("error loading request params")
470 rtr.sendError(w, err)
473 opts := defaultOpts()
474 err = rtr.transcode(params, opts)
476 logger.WithField("params", params).WithError(err).Debugf("error transcoding params to %T", opts)
477 rtr.sendError(w, err)
480 respOpts, err := rtr.responseOptions(opts)
482 logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
483 rtr.sendError(w, err)
487 creds := auth.CredentialsFromRequest(req)
488 err = creds.LoadTokensFromHTTPRequestBody(req)
490 rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
493 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
494 for _, t := range rt {
495 if t, ok := t.(string); ok {
496 creds.Tokens = append(creds.Tokens, t)
500 ctx := auth.NewContext(req.Context(), creds)
501 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
502 logger.WithFields(logrus.Fields{
503 "apiEndpoint": endpoint,
504 "apiOptsType": fmt.Sprintf("%T", opts),
507 resp, err := exec(ctx, opts)
509 logger.WithError(err).Debugf("returning error type %T", err)
510 rtr.sendError(w, err)
513 rtr.sendResponse(w, req, resp, respOpts)
517 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
518 switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
519 case "login", "logout", "auth":
521 w.Header().Set("Access-Control-Allow-Origin", "*")
522 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, PUT, POST, PATCH, DELETE")
523 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Http-Method-Override")
524 w.Header().Set("Access-Control-Max-Age", "86486400")
526 if r.Method == "OPTIONS" {
530 // Wrap r.Body in a http.MaxBytesReader(), otherwise
531 // r.ParseForm() uses a default max request body size
532 // of 10 megabytes. Note we rely on the Nginx
533 // configuration to enforce the real max body size.
534 max := int64(rtr.config.MaxRequestSize)
536 max = math.MaxInt64 - 1
538 r.Body = http.MaxBytesReader(w, r.Body, max)
540 if r.Method == "POST" {
543 if err.Error() == "http: request body too large" {
544 err = httpError(http.StatusRequestEntityTooLarge, err)
546 rtr.sendError(w, err)
549 if m := r.FormValue("_method"); m != "" {
553 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
559 rtr.mux.ServeHTTP(w, r)