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.EndpointVocabularyGet,
70 func() interface{} { return &struct{}{} },
71 func(ctx context.Context, opts interface{}) (interface{}, error) {
72 return rtr.backend.VocabularyGet(ctx)
76 arvados.EndpointLogin,
77 func() interface{} { return &arvados.LoginOptions{} },
78 func(ctx context.Context, opts interface{}) (interface{}, error) {
79 return rtr.backend.Login(ctx, *opts.(*arvados.LoginOptions))
83 arvados.EndpointLogout,
84 func() interface{} { return &arvados.LogoutOptions{} },
85 func(ctx context.Context, opts interface{}) (interface{}, error) {
86 return rtr.backend.Logout(ctx, *opts.(*arvados.LogoutOptions))
90 arvados.EndpointAuthorizedKeyCreate,
91 func() interface{} { return &arvados.CreateOptions{} },
92 func(ctx context.Context, opts interface{}) (interface{}, error) {
93 return rtr.backend.AuthorizedKeyCreate(ctx, *opts.(*arvados.CreateOptions))
97 arvados.EndpointAuthorizedKeyUpdate,
98 func() interface{} { return &arvados.UpdateOptions{} },
99 func(ctx context.Context, opts interface{}) (interface{}, error) {
100 return rtr.backend.AuthorizedKeyUpdate(ctx, *opts.(*arvados.UpdateOptions))
104 arvados.EndpointAuthorizedKeyGet,
105 func() interface{} { return &arvados.GetOptions{} },
106 func(ctx context.Context, opts interface{}) (interface{}, error) {
107 return rtr.backend.AuthorizedKeyGet(ctx, *opts.(*arvados.GetOptions))
111 arvados.EndpointAuthorizedKeyList,
112 func() interface{} { return &arvados.ListOptions{Limit: -1} },
113 func(ctx context.Context, opts interface{}) (interface{}, error) {
114 return rtr.backend.AuthorizedKeyList(ctx, *opts.(*arvados.ListOptions))
118 arvados.EndpointAuthorizedKeyDelete,
119 func() interface{} { return &arvados.DeleteOptions{} },
120 func(ctx context.Context, opts interface{}) (interface{}, error) {
121 return rtr.backend.AuthorizedKeyDelete(ctx, *opts.(*arvados.DeleteOptions))
125 arvados.EndpointCollectionCreate,
126 func() interface{} { return &arvados.CreateOptions{} },
127 func(ctx context.Context, opts interface{}) (interface{}, error) {
128 return rtr.backend.CollectionCreate(ctx, *opts.(*arvados.CreateOptions))
132 arvados.EndpointCollectionUpdate,
133 func() interface{} { return &arvados.UpdateOptions{} },
134 func(ctx context.Context, opts interface{}) (interface{}, error) {
135 return rtr.backend.CollectionUpdate(ctx, *opts.(*arvados.UpdateOptions))
139 arvados.EndpointCollectionGet,
140 func() interface{} { return &arvados.GetOptions{} },
141 func(ctx context.Context, opts interface{}) (interface{}, error) {
142 return rtr.backend.CollectionGet(ctx, *opts.(*arvados.GetOptions))
146 arvados.EndpointCollectionList,
147 func() interface{} { return &arvados.ListOptions{Limit: -1} },
148 func(ctx context.Context, opts interface{}) (interface{}, error) {
149 return rtr.backend.CollectionList(ctx, *opts.(*arvados.ListOptions))
153 arvados.EndpointCollectionProvenance,
154 func() interface{} { return &arvados.GetOptions{} },
155 func(ctx context.Context, opts interface{}) (interface{}, error) {
156 return rtr.backend.CollectionProvenance(ctx, *opts.(*arvados.GetOptions))
160 arvados.EndpointCollectionUsedBy,
161 func() interface{} { return &arvados.GetOptions{} },
162 func(ctx context.Context, opts interface{}) (interface{}, error) {
163 return rtr.backend.CollectionUsedBy(ctx, *opts.(*arvados.GetOptions))
167 arvados.EndpointCollectionDelete,
168 func() interface{} { return &arvados.DeleteOptions{} },
169 func(ctx context.Context, opts interface{}) (interface{}, error) {
170 return rtr.backend.CollectionDelete(ctx, *opts.(*arvados.DeleteOptions))
174 arvados.EndpointCollectionTrash,
175 func() interface{} { return &arvados.DeleteOptions{} },
176 func(ctx context.Context, opts interface{}) (interface{}, error) {
177 return rtr.backend.CollectionTrash(ctx, *opts.(*arvados.DeleteOptions))
181 arvados.EndpointCollectionUntrash,
182 func() interface{} { return &arvados.UntrashOptions{} },
183 func(ctx context.Context, opts interface{}) (interface{}, error) {
184 return rtr.backend.CollectionUntrash(ctx, *opts.(*arvados.UntrashOptions))
188 arvados.EndpointContainerCreate,
189 func() interface{} { return &arvados.CreateOptions{} },
190 func(ctx context.Context, opts interface{}) (interface{}, error) {
191 return rtr.backend.ContainerCreate(ctx, *opts.(*arvados.CreateOptions))
195 arvados.EndpointContainerPriorityUpdate,
196 func() interface{} { return &arvados.UpdateOptions{} },
197 func(ctx context.Context, opts interface{}) (interface{}, error) {
198 return rtr.backend.ContainerPriorityUpdate(ctx, *opts.(*arvados.UpdateOptions))
202 arvados.EndpointContainerUpdate,
203 func() interface{} { return &arvados.UpdateOptions{} },
204 func(ctx context.Context, opts interface{}) (interface{}, error) {
205 return rtr.backend.ContainerUpdate(ctx, *opts.(*arvados.UpdateOptions))
209 arvados.EndpointContainerGet,
210 func() interface{} { return &arvados.GetOptions{} },
211 func(ctx context.Context, opts interface{}) (interface{}, error) {
212 return rtr.backend.ContainerGet(ctx, *opts.(*arvados.GetOptions))
216 arvados.EndpointContainerList,
217 func() interface{} { return &arvados.ListOptions{Limit: -1} },
218 func(ctx context.Context, opts interface{}) (interface{}, error) {
219 return rtr.backend.ContainerList(ctx, *opts.(*arvados.ListOptions))
223 arvados.EndpointContainerDelete,
224 func() interface{} { return &arvados.DeleteOptions{} },
225 func(ctx context.Context, opts interface{}) (interface{}, error) {
226 return rtr.backend.ContainerDelete(ctx, *opts.(*arvados.DeleteOptions))
230 arvados.EndpointContainerLock,
232 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
234 func(ctx context.Context, opts interface{}) (interface{}, error) {
235 return rtr.backend.ContainerLock(ctx, *opts.(*arvados.GetOptions))
239 arvados.EndpointContainerUnlock,
241 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
243 func(ctx context.Context, opts interface{}) (interface{}, error) {
244 return rtr.backend.ContainerUnlock(ctx, *opts.(*arvados.GetOptions))
248 arvados.EndpointContainerSSH,
249 func() interface{} { return &arvados.ContainerSSHOptions{} },
250 func(ctx context.Context, opts interface{}) (interface{}, error) {
251 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
255 arvados.EndpointContainerSSHCompat,
256 func() interface{} { return &arvados.ContainerSSHOptions{} },
257 func(ctx context.Context, opts interface{}) (interface{}, error) {
258 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
262 // arvados-client built before commit
263 // bdc29d3129f6d75aa9ce0a24ffb849a272b06f08
264 // used GET with params in headers instead of
266 arvados.APIEndpoint{"GET", "arvados/v1/connect/{uuid}/ssh", ""},
267 func() interface{} { return &arvados.ContainerSSHOptions{} },
268 func(ctx context.Context, opts interface{}) (interface{}, error) {
269 return nil, httpError(http.StatusGone, fmt.Errorf("API endpoint is obsolete -- please upgrade your arvados-client program"))
273 arvados.EndpointContainerGatewayTunnel,
274 func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
275 func(ctx context.Context, opts interface{}) (interface{}, error) {
276 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
280 arvados.EndpointContainerGatewayTunnelCompat,
281 func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
282 func(ctx context.Context, opts interface{}) (interface{}, error) {
283 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
287 arvados.EndpointContainerRequestCreate,
288 func() interface{} { return &arvados.CreateOptions{} },
289 func(ctx context.Context, opts interface{}) (interface{}, error) {
290 return rtr.backend.ContainerRequestCreate(ctx, *opts.(*arvados.CreateOptions))
294 arvados.EndpointContainerRequestUpdate,
295 func() interface{} { return &arvados.UpdateOptions{} },
296 func(ctx context.Context, opts interface{}) (interface{}, error) {
297 return rtr.backend.ContainerRequestUpdate(ctx, *opts.(*arvados.UpdateOptions))
301 arvados.EndpointContainerRequestGet,
302 func() interface{} { return &arvados.GetOptions{} },
303 func(ctx context.Context, opts interface{}) (interface{}, error) {
304 return rtr.backend.ContainerRequestGet(ctx, *opts.(*arvados.GetOptions))
308 arvados.EndpointContainerRequestList,
309 func() interface{} { return &arvados.ListOptions{Limit: -1} },
310 func(ctx context.Context, opts interface{}) (interface{}, error) {
311 return rtr.backend.ContainerRequestList(ctx, *opts.(*arvados.ListOptions))
315 arvados.EndpointContainerRequestDelete,
316 func() interface{} { return &arvados.DeleteOptions{} },
317 func(ctx context.Context, opts interface{}) (interface{}, error) {
318 return rtr.backend.ContainerRequestDelete(ctx, *opts.(*arvados.DeleteOptions))
322 arvados.EndpointContainerRequestContainerStatus,
323 func() interface{} { return &arvados.GetOptions{} },
324 func(ctx context.Context, opts interface{}) (interface{}, error) {
325 return rtr.backend.ContainerRequestContainerStatus(ctx, *opts.(*arvados.GetOptions))
329 arvados.EndpointContainerRequestLog,
330 func() interface{} { return &arvados.ContainerLogOptions{} },
331 func(ctx context.Context, opts interface{}) (interface{}, error) {
332 return rtr.backend.ContainerRequestLog(ctx, *opts.(*arvados.ContainerLogOptions))
336 arvados.EndpointGroupCreate,
337 func() interface{} { return &arvados.CreateOptions{} },
338 func(ctx context.Context, opts interface{}) (interface{}, error) {
339 return rtr.backend.GroupCreate(ctx, *opts.(*arvados.CreateOptions))
343 arvados.EndpointGroupUpdate,
344 func() interface{} { return &arvados.UpdateOptions{} },
345 func(ctx context.Context, opts interface{}) (interface{}, error) {
346 return rtr.backend.GroupUpdate(ctx, *opts.(*arvados.UpdateOptions))
350 arvados.EndpointGroupList,
351 func() interface{} { return &arvados.ListOptions{Limit: -1} },
352 func(ctx context.Context, opts interface{}) (interface{}, error) {
353 return rtr.backend.GroupList(ctx, *opts.(*arvados.ListOptions))
357 arvados.EndpointGroupContents,
358 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
359 func(ctx context.Context, opts interface{}) (interface{}, error) {
360 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
364 arvados.EndpointGroupContentsUUIDInPath,
365 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
366 func(ctx context.Context, opts interface{}) (interface{}, error) {
367 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
371 arvados.EndpointGroupShared,
372 func() interface{} { return &arvados.ListOptions{Limit: -1} },
373 func(ctx context.Context, opts interface{}) (interface{}, error) {
374 return rtr.backend.GroupShared(ctx, *opts.(*arvados.ListOptions))
378 arvados.EndpointGroupGet,
379 func() interface{} { return &arvados.GetOptions{} },
380 func(ctx context.Context, opts interface{}) (interface{}, error) {
381 return rtr.backend.GroupGet(ctx, *opts.(*arvados.GetOptions))
385 arvados.EndpointGroupDelete,
386 func() interface{} { return &arvados.DeleteOptions{} },
387 func(ctx context.Context, opts interface{}) (interface{}, error) {
388 return rtr.backend.GroupDelete(ctx, *opts.(*arvados.DeleteOptions))
392 arvados.EndpointGroupTrash,
393 func() interface{} { return &arvados.DeleteOptions{} },
394 func(ctx context.Context, opts interface{}) (interface{}, error) {
395 return rtr.backend.GroupTrash(ctx, *opts.(*arvados.DeleteOptions))
399 arvados.EndpointGroupUntrash,
400 func() interface{} { return &arvados.UntrashOptions{} },
401 func(ctx context.Context, opts interface{}) (interface{}, error) {
402 return rtr.backend.GroupUntrash(ctx, *opts.(*arvados.UntrashOptions))
406 arvados.EndpointLinkCreate,
407 func() interface{} { return &arvados.CreateOptions{} },
408 func(ctx context.Context, opts interface{}) (interface{}, error) {
409 return rtr.backend.LinkCreate(ctx, *opts.(*arvados.CreateOptions))
413 arvados.EndpointLinkUpdate,
414 func() interface{} { return &arvados.UpdateOptions{} },
415 func(ctx context.Context, opts interface{}) (interface{}, error) {
416 return rtr.backend.LinkUpdate(ctx, *opts.(*arvados.UpdateOptions))
420 arvados.EndpointLinkList,
421 func() interface{} { return &arvados.ListOptions{Limit: -1} },
422 func(ctx context.Context, opts interface{}) (interface{}, error) {
423 return rtr.backend.LinkList(ctx, *opts.(*arvados.ListOptions))
427 arvados.EndpointLinkGet,
428 func() interface{} { return &arvados.GetOptions{} },
429 func(ctx context.Context, opts interface{}) (interface{}, error) {
430 return rtr.backend.LinkGet(ctx, *opts.(*arvados.GetOptions))
434 arvados.EndpointLinkDelete,
435 func() interface{} { return &arvados.DeleteOptions{} },
436 func(ctx context.Context, opts interface{}) (interface{}, error) {
437 return rtr.backend.LinkDelete(ctx, *opts.(*arvados.DeleteOptions))
441 arvados.EndpointLogCreate,
442 func() interface{} { return &arvados.CreateOptions{} },
443 func(ctx context.Context, opts interface{}) (interface{}, error) {
444 return rtr.backend.LogCreate(ctx, *opts.(*arvados.CreateOptions))
448 arvados.EndpointLogUpdate,
449 func() interface{} { return &arvados.UpdateOptions{} },
450 func(ctx context.Context, opts interface{}) (interface{}, error) {
451 return rtr.backend.LogUpdate(ctx, *opts.(*arvados.UpdateOptions))
455 arvados.EndpointLogList,
456 func() interface{} { return &arvados.ListOptions{Limit: -1} },
457 func(ctx context.Context, opts interface{}) (interface{}, error) {
458 return rtr.backend.LogList(ctx, *opts.(*arvados.ListOptions))
462 arvados.EndpointLogGet,
463 func() interface{} { return &arvados.GetOptions{} },
464 func(ctx context.Context, opts interface{}) (interface{}, error) {
465 return rtr.backend.LogGet(ctx, *opts.(*arvados.GetOptions))
469 arvados.EndpointLogDelete,
470 func() interface{} { return &arvados.DeleteOptions{} },
471 func(ctx context.Context, opts interface{}) (interface{}, error) {
472 return rtr.backend.LogDelete(ctx, *opts.(*arvados.DeleteOptions))
476 arvados.EndpointSpecimenCreate,
477 func() interface{} { return &arvados.CreateOptions{} },
478 func(ctx context.Context, opts interface{}) (interface{}, error) {
479 return rtr.backend.SpecimenCreate(ctx, *opts.(*arvados.CreateOptions))
483 arvados.EndpointSpecimenUpdate,
484 func() interface{} { return &arvados.UpdateOptions{} },
485 func(ctx context.Context, opts interface{}) (interface{}, error) {
486 return rtr.backend.SpecimenUpdate(ctx, *opts.(*arvados.UpdateOptions))
490 arvados.EndpointSpecimenGet,
491 func() interface{} { return &arvados.GetOptions{} },
492 func(ctx context.Context, opts interface{}) (interface{}, error) {
493 return rtr.backend.SpecimenGet(ctx, *opts.(*arvados.GetOptions))
497 arvados.EndpointSpecimenList,
498 func() interface{} { return &arvados.ListOptions{Limit: -1} },
499 func(ctx context.Context, opts interface{}) (interface{}, error) {
500 return rtr.backend.SpecimenList(ctx, *opts.(*arvados.ListOptions))
504 arvados.EndpointSpecimenDelete,
505 func() interface{} { return &arvados.DeleteOptions{} },
506 func(ctx context.Context, opts interface{}) (interface{}, error) {
507 return rtr.backend.SpecimenDelete(ctx, *opts.(*arvados.DeleteOptions))
511 arvados.EndpointAPIClientAuthorizationCreate,
512 func() interface{} { return &arvados.CreateOptions{} },
513 func(ctx context.Context, opts interface{}) (interface{}, error) {
514 return rtr.backend.APIClientAuthorizationCreate(ctx, *opts.(*arvados.CreateOptions))
518 arvados.EndpointAPIClientAuthorizationUpdate,
519 func() interface{} { return &arvados.UpdateOptions{} },
520 func(ctx context.Context, opts interface{}) (interface{}, error) {
521 return rtr.backend.APIClientAuthorizationUpdate(ctx, *opts.(*arvados.UpdateOptions))
525 arvados.EndpointAPIClientAuthorizationDelete,
526 func() interface{} { return &arvados.DeleteOptions{} },
527 func(ctx context.Context, opts interface{}) (interface{}, error) {
528 return rtr.backend.APIClientAuthorizationDelete(ctx, *opts.(*arvados.DeleteOptions))
532 arvados.EndpointAPIClientAuthorizationList,
533 func() interface{} { return &arvados.ListOptions{Limit: -1} },
534 func(ctx context.Context, opts interface{}) (interface{}, error) {
535 return rtr.backend.APIClientAuthorizationList(ctx, *opts.(*arvados.ListOptions))
539 arvados.EndpointAPIClientAuthorizationCurrent,
540 func() interface{} { return &arvados.GetOptions{} },
541 func(ctx context.Context, opts interface{}) (interface{}, error) {
542 return rtr.backend.APIClientAuthorizationCurrent(ctx, *opts.(*arvados.GetOptions))
546 arvados.EndpointAPIClientAuthorizationGet,
547 func() interface{} { return &arvados.GetOptions{} },
548 func(ctx context.Context, opts interface{}) (interface{}, error) {
549 return rtr.backend.APIClientAuthorizationGet(ctx, *opts.(*arvados.GetOptions))
553 arvados.EndpointUserCreate,
554 func() interface{} { return &arvados.CreateOptions{} },
555 func(ctx context.Context, opts interface{}) (interface{}, error) {
556 return rtr.backend.UserCreate(ctx, *opts.(*arvados.CreateOptions))
560 arvados.EndpointUserMerge,
561 func() interface{} { return &arvados.UserMergeOptions{} },
562 func(ctx context.Context, opts interface{}) (interface{}, error) {
563 return rtr.backend.UserMerge(ctx, *opts.(*arvados.UserMergeOptions))
567 arvados.EndpointUserActivate,
568 func() interface{} { return &arvados.UserActivateOptions{} },
569 func(ctx context.Context, opts interface{}) (interface{}, error) {
570 return rtr.backend.UserActivate(ctx, *opts.(*arvados.UserActivateOptions))
574 arvados.EndpointUserSetup,
575 func() interface{} { return &arvados.UserSetupOptions{} },
576 func(ctx context.Context, opts interface{}) (interface{}, error) {
577 return rtr.backend.UserSetup(ctx, *opts.(*arvados.UserSetupOptions))
581 arvados.EndpointUserUnsetup,
582 func() interface{} { return &arvados.GetOptions{} },
583 func(ctx context.Context, opts interface{}) (interface{}, error) {
584 return rtr.backend.UserUnsetup(ctx, *opts.(*arvados.GetOptions))
588 arvados.EndpointUserGetCurrent,
589 func() interface{} { return &arvados.GetOptions{} },
590 func(ctx context.Context, opts interface{}) (interface{}, error) {
591 return rtr.backend.UserGetCurrent(ctx, *opts.(*arvados.GetOptions))
595 arvados.EndpointUserGetSystem,
596 func() interface{} { return &arvados.GetOptions{} },
597 func(ctx context.Context, opts interface{}) (interface{}, error) {
598 return rtr.backend.UserGetSystem(ctx, *opts.(*arvados.GetOptions))
602 arvados.EndpointUserGet,
603 func() interface{} { return &arvados.GetOptions{} },
604 func(ctx context.Context, opts interface{}) (interface{}, error) {
605 return rtr.backend.UserGet(ctx, *opts.(*arvados.GetOptions))
609 arvados.EndpointUserUpdate,
610 func() interface{} { return &arvados.UpdateOptions{} },
611 func(ctx context.Context, opts interface{}) (interface{}, error) {
612 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
616 arvados.EndpointUserList,
617 func() interface{} { return &arvados.ListOptions{Limit: -1} },
618 func(ctx context.Context, opts interface{}) (interface{}, error) {
619 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
623 arvados.EndpointUserBatchUpdate,
624 func() interface{} { return &arvados.UserBatchUpdateOptions{} },
625 func(ctx context.Context, opts interface{}) (interface{}, error) {
626 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
630 arvados.EndpointUserDelete,
631 func() interface{} { return &arvados.DeleteOptions{} },
632 func(ctx context.Context, opts interface{}) (interface{}, error) {
633 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
637 arvados.EndpointUserAuthenticate,
638 func() interface{} { return &arvados.UserAuthenticateOptions{} },
639 func(ctx context.Context, opts interface{}) (interface{}, error) {
640 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
645 if rtr.config.WrapCalls != nil {
646 exec = rtr.config.WrapCalls(exec)
648 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
650 rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
651 if req.Method == "OPTIONS" {
652 // For non-webdav endpoints, return an empty
653 // response with the CORS headers we already
654 // added in ServeHTTP.
655 w.WriteHeader(http.StatusOK)
658 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
660 rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
661 if req.Method == "OPTIONS" {
662 // For non-webdav endpoints, return an empty
663 // response with the CORS headers we already
664 // added in ServeHTTP.
665 w.WriteHeader(http.StatusOK)
668 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
672 var altMethod = map[string]string{
673 "PATCH": "PUT", // Accept PUT as a synonym for PATCH
674 "GET": "HEAD", // Accept HEAD at any GET route
677 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
678 methods := []string{endpoint.Method}
679 if alt, ok := altMethod[endpoint.Method]; ok {
680 methods = append(methods, alt)
682 if strings.HasSuffix(endpoint.Path, ".*}") {
684 methods = append(methods, "OPTIONS", "PROPFIND")
686 rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
687 logger := ctxlog.FromContext(req.Context())
688 opts := defaultOpts()
689 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey, opts)
691 logger.WithFields(logrus.Fields{
693 "method": endpoint.Method,
694 "endpoint": endpoint,
695 }).WithError(err).Debug("error loading request params")
696 rtr.sendError(w, err)
699 respOpts, err := rtr.responseOptions(opts)
701 logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
702 rtr.sendError(w, err)
706 creds := auth.CredentialsFromRequest(req)
707 err = creds.LoadTokensFromHTTPRequestBody(req)
709 rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
712 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
713 for _, t := range rt {
714 if t, ok := t.(string); ok {
715 creds.Tokens = append(creds.Tokens, t)
719 ctx := auth.NewContext(req.Context(), creds)
720 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
721 req = req.WithContext(ctx)
723 // Extract the token UUIDs (or a placeholder for v1 tokens)
724 var tokenUUIDs []string
725 for _, t := range creds.Tokens {
726 if strings.HasPrefix(t, "v2/") {
727 tokenParts := strings.Split(t, "/")
728 if len(tokenParts) >= 3 {
729 tokenUUIDs = append(tokenUUIDs, tokenParts[1])
736 tokenUUIDs = append(tokenUUIDs, "v1 token ending in "+end)
739 httpserver.SetResponseLogFields(ctx, logrus.Fields{"tokenUUIDs": tokenUUIDs})
741 logger.WithFields(logrus.Fields{
742 "apiEndpoint": endpoint,
743 "apiOptsType": fmt.Sprintf("%T", opts),
746 resp, err := exec(ctx, opts)
748 logger.WithError(err).Debugf("returning error type %T", err)
749 rtr.sendError(w, err)
752 rtr.sendResponse(w, req, resp, respOpts)
756 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
757 switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
758 case "login", "logout", "auth":
760 w.Header().Set("Access-Control-Allow-Origin", "*")
761 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, PROPFIND, PUT, POST, PATCH, DELETE")
762 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Range, X-Http-Method-Override")
763 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
764 w.Header().Set("Access-Control-Max-Age", "86486400")
767 // Wrap r.Body in a http.MaxBytesReader(), otherwise
768 // r.ParseForm() uses a default max request body size
769 // of 10 megabytes. Note we rely on the Nginx
770 // configuration to enforce the real max body size.
771 max := int64(rtr.config.MaxRequestSize)
773 max = math.MaxInt64 - 1
775 r.Body = http.MaxBytesReader(w, r.Body, max)
777 if r.Method == "POST" {
780 if err.Error() == "http: request body too large" {
781 err = httpError(http.StatusRequestEntityTooLarge, err)
783 rtr.sendError(w, err)
786 if m := r.FormValue("_method"); m != "" {
790 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
796 rtr.mux.ServeHTTP(w, r)