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/arvadosclient"
17 "git.arvados.org/arvados.git/sdk/go/auth"
18 "git.arvados.org/arvados.git/sdk/go/ctxlog"
19 "git.arvados.org/arvados.git/sdk/go/httpserver"
20 "github.com/gorilla/mux"
21 "github.com/sirupsen/logrus"
31 // Return an error if request body exceeds this size. 0 means
35 // If wrapCalls is not nil, it is called once for each API
36 // method, and the returned method is used in its place. This
37 // can be used to install hooks before and after each API call
38 // and alter responses; see localdb.WrapCallsInTransaction for
40 WrapCalls func(api.RoutableFunc) api.RoutableFunc
43 // New returns a new router (which implements the http.Handler
44 // interface) that serves requests by calling Arvados API methods on
46 func New(backend arvados.API, config Config) *router {
56 func (rtr *router) addRoutes() {
57 for _, route := range []struct {
58 endpoint arvados.APIEndpoint
59 defaultOpts func() interface{}
63 arvados.EndpointConfigGet,
64 func() interface{} { return &struct{}{} },
65 func(ctx context.Context, opts interface{}) (interface{}, error) {
66 return rtr.backend.ConfigGet(ctx)
70 arvados.EndpointVocabularyGet,
71 func() interface{} { return &struct{}{} },
72 func(ctx context.Context, opts interface{}) (interface{}, error) {
73 return rtr.backend.VocabularyGet(ctx)
77 arvados.EndpointLogin,
78 func() interface{} { return &arvados.LoginOptions{} },
79 func(ctx context.Context, opts interface{}) (interface{}, error) {
80 return rtr.backend.Login(ctx, *opts.(*arvados.LoginOptions))
84 arvados.EndpointLogout,
85 func() interface{} { return &arvados.LogoutOptions{} },
86 func(ctx context.Context, opts interface{}) (interface{}, error) {
87 return rtr.backend.Logout(ctx, *opts.(*arvados.LogoutOptions))
91 arvados.EndpointAuthorizedKeyCreate,
92 func() interface{} { return &arvados.CreateOptions{} },
93 func(ctx context.Context, opts interface{}) (interface{}, error) {
94 return rtr.backend.AuthorizedKeyCreate(ctx, *opts.(*arvados.CreateOptions))
98 arvados.EndpointAuthorizedKeyUpdate,
99 func() interface{} { return &arvados.UpdateOptions{} },
100 func(ctx context.Context, opts interface{}) (interface{}, error) {
101 return rtr.backend.AuthorizedKeyUpdate(ctx, *opts.(*arvados.UpdateOptions))
105 arvados.EndpointAuthorizedKeyGet,
106 func() interface{} { return &arvados.GetOptions{} },
107 func(ctx context.Context, opts interface{}) (interface{}, error) {
108 return rtr.backend.AuthorizedKeyGet(ctx, *opts.(*arvados.GetOptions))
112 arvados.EndpointAuthorizedKeyList,
113 func() interface{} { return &arvados.ListOptions{Limit: -1} },
114 func(ctx context.Context, opts interface{}) (interface{}, error) {
115 return rtr.backend.AuthorizedKeyList(ctx, *opts.(*arvados.ListOptions))
119 arvados.EndpointAuthorizedKeyDelete,
120 func() interface{} { return &arvados.DeleteOptions{} },
121 func(ctx context.Context, opts interface{}) (interface{}, error) {
122 return rtr.backend.AuthorizedKeyDelete(ctx, *opts.(*arvados.DeleteOptions))
126 arvados.EndpointCollectionCreate,
127 func() interface{} { return &arvados.CreateOptions{} },
128 func(ctx context.Context, opts interface{}) (interface{}, error) {
129 return rtr.backend.CollectionCreate(ctx, *opts.(*arvados.CreateOptions))
133 arvados.EndpointCollectionUpdate,
134 func() interface{} { return &arvados.UpdateOptions{} },
135 func(ctx context.Context, opts interface{}) (interface{}, error) {
136 return rtr.backend.CollectionUpdate(ctx, *opts.(*arvados.UpdateOptions))
140 arvados.EndpointCollectionGet,
141 func() interface{} { return &arvados.GetOptions{} },
142 func(ctx context.Context, opts interface{}) (interface{}, error) {
143 return rtr.backend.CollectionGet(ctx, *opts.(*arvados.GetOptions))
147 arvados.EndpointCollectionList,
148 func() interface{} { return &arvados.ListOptions{Limit: -1} },
149 func(ctx context.Context, opts interface{}) (interface{}, error) {
150 return rtr.backend.CollectionList(ctx, *opts.(*arvados.ListOptions))
154 arvados.EndpointCollectionProvenance,
155 func() interface{} { return &arvados.GetOptions{} },
156 func(ctx context.Context, opts interface{}) (interface{}, error) {
157 return rtr.backend.CollectionProvenance(ctx, *opts.(*arvados.GetOptions))
161 arvados.EndpointCollectionUsedBy,
162 func() interface{} { return &arvados.GetOptions{} },
163 func(ctx context.Context, opts interface{}) (interface{}, error) {
164 return rtr.backend.CollectionUsedBy(ctx, *opts.(*arvados.GetOptions))
168 arvados.EndpointCollectionDelete,
169 func() interface{} { return &arvados.DeleteOptions{} },
170 func(ctx context.Context, opts interface{}) (interface{}, error) {
171 return rtr.backend.CollectionDelete(ctx, *opts.(*arvados.DeleteOptions))
175 arvados.EndpointCollectionTrash,
176 func() interface{} { return &arvados.DeleteOptions{} },
177 func(ctx context.Context, opts interface{}) (interface{}, error) {
178 return rtr.backend.CollectionTrash(ctx, *opts.(*arvados.DeleteOptions))
182 arvados.EndpointCollectionUntrash,
183 func() interface{} { return &arvados.UntrashOptions{} },
184 func(ctx context.Context, opts interface{}) (interface{}, error) {
185 return rtr.backend.CollectionUntrash(ctx, *opts.(*arvados.UntrashOptions))
189 arvados.EndpointComputedPermissionList,
190 func() interface{} { return &arvados.ListOptions{Limit: -1} },
191 func(ctx context.Context, opts interface{}) (interface{}, error) {
192 return rtr.backend.ComputedPermissionList(ctx, *opts.(*arvados.ListOptions))
196 arvados.EndpointContainerCreate,
197 func() interface{} { return &arvados.CreateOptions{} },
198 func(ctx context.Context, opts interface{}) (interface{}, error) {
199 return rtr.backend.ContainerCreate(ctx, *opts.(*arvados.CreateOptions))
203 arvados.EndpointContainerPriorityUpdate,
204 func() interface{} { return &arvados.UpdateOptions{} },
205 func(ctx context.Context, opts interface{}) (interface{}, error) {
206 return rtr.backend.ContainerPriorityUpdate(ctx, *opts.(*arvados.UpdateOptions))
210 arvados.EndpointContainerUpdate,
211 func() interface{} { return &arvados.UpdateOptions{} },
212 func(ctx context.Context, opts interface{}) (interface{}, error) {
213 return rtr.backend.ContainerUpdate(ctx, *opts.(*arvados.UpdateOptions))
217 arvados.EndpointContainerGet,
218 func() interface{} { return &arvados.GetOptions{} },
219 func(ctx context.Context, opts interface{}) (interface{}, error) {
220 return rtr.backend.ContainerGet(ctx, *opts.(*arvados.GetOptions))
224 arvados.EndpointContainerList,
225 func() interface{} { return &arvados.ListOptions{Limit: -1} },
226 func(ctx context.Context, opts interface{}) (interface{}, error) {
227 return rtr.backend.ContainerList(ctx, *opts.(*arvados.ListOptions))
231 arvados.EndpointContainerDelete,
232 func() interface{} { return &arvados.DeleteOptions{} },
233 func(ctx context.Context, opts interface{}) (interface{}, error) {
234 return rtr.backend.ContainerDelete(ctx, *opts.(*arvados.DeleteOptions))
238 arvados.EndpointContainerLock,
240 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
242 func(ctx context.Context, opts interface{}) (interface{}, error) {
243 return rtr.backend.ContainerLock(ctx, *opts.(*arvados.GetOptions))
247 arvados.EndpointContainerUnlock,
249 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
251 func(ctx context.Context, opts interface{}) (interface{}, error) {
252 return rtr.backend.ContainerUnlock(ctx, *opts.(*arvados.GetOptions))
256 arvados.EndpointContainerSSH,
257 func() interface{} { return &arvados.ContainerSSHOptions{} },
258 func(ctx context.Context, opts interface{}) (interface{}, error) {
259 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
263 arvados.EndpointContainerSSHCompat,
264 func() interface{} { return &arvados.ContainerSSHOptions{} },
265 func(ctx context.Context, opts interface{}) (interface{}, error) {
266 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
270 // arvados-client built before commit
271 // bdc29d3129f6d75aa9ce0a24ffb849a272b06f08
272 // used GET with params in headers instead of
274 arvados.APIEndpoint{"GET", "arvados/v1/connect/{uuid}/ssh", ""},
275 func() interface{} { return &arvados.ContainerSSHOptions{} },
276 func(ctx context.Context, opts interface{}) (interface{}, error) {
277 return nil, httpError(http.StatusGone, fmt.Errorf("API endpoint is obsolete -- please upgrade your arvados-client program"))
281 arvados.EndpointContainerGatewayTunnel,
282 func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
283 func(ctx context.Context, opts interface{}) (interface{}, error) {
284 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
288 arvados.EndpointContainerGatewayTunnelCompat,
289 func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
290 func(ctx context.Context, opts interface{}) (interface{}, error) {
291 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
295 arvados.EndpointContainerRequestCreate,
296 func() interface{} { return &arvados.CreateOptions{} },
297 func(ctx context.Context, opts interface{}) (interface{}, error) {
298 return rtr.backend.ContainerRequestCreate(ctx, *opts.(*arvados.CreateOptions))
302 arvados.EndpointContainerRequestUpdate,
303 func() interface{} { return &arvados.UpdateOptions{} },
304 func(ctx context.Context, opts interface{}) (interface{}, error) {
305 return rtr.backend.ContainerRequestUpdate(ctx, *opts.(*arvados.UpdateOptions))
309 arvados.EndpointContainerRequestGet,
310 func() interface{} { return &arvados.GetOptions{} },
311 func(ctx context.Context, opts interface{}) (interface{}, error) {
312 return rtr.backend.ContainerRequestGet(ctx, *opts.(*arvados.GetOptions))
316 arvados.EndpointContainerRequestList,
317 func() interface{} { return &arvados.ListOptions{Limit: -1} },
318 func(ctx context.Context, opts interface{}) (interface{}, error) {
319 return rtr.backend.ContainerRequestList(ctx, *opts.(*arvados.ListOptions))
323 arvados.EndpointContainerRequestDelete,
324 func() interface{} { return &arvados.DeleteOptions{} },
325 func(ctx context.Context, opts interface{}) (interface{}, error) {
326 return rtr.backend.ContainerRequestDelete(ctx, *opts.(*arvados.DeleteOptions))
330 arvados.EndpointContainerRequestContainerStatus,
331 func() interface{} { return &arvados.GetOptions{} },
332 func(ctx context.Context, opts interface{}) (interface{}, error) {
333 return rtr.backend.ContainerRequestContainerStatus(ctx, *opts.(*arvados.GetOptions))
337 arvados.EndpointContainerRequestLog,
338 func() interface{} { return &arvados.ContainerLogOptions{} },
339 func(ctx context.Context, opts interface{}) (interface{}, error) {
340 return rtr.backend.ContainerRequestLog(ctx, *opts.(*arvados.ContainerLogOptions))
344 arvados.EndpointGroupCreate,
345 func() interface{} { return &arvados.CreateOptions{} },
346 func(ctx context.Context, opts interface{}) (interface{}, error) {
347 return rtr.backend.GroupCreate(ctx, *opts.(*arvados.CreateOptions))
351 arvados.EndpointGroupUpdate,
352 func() interface{} { return &arvados.UpdateOptions{} },
353 func(ctx context.Context, opts interface{}) (interface{}, error) {
354 return rtr.backend.GroupUpdate(ctx, *opts.(*arvados.UpdateOptions))
358 arvados.EndpointGroupList,
359 func() interface{} { return &arvados.ListOptions{Limit: -1} },
360 func(ctx context.Context, opts interface{}) (interface{}, error) {
361 return rtr.backend.GroupList(ctx, *opts.(*arvados.ListOptions))
365 arvados.EndpointGroupContents,
366 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
367 func(ctx context.Context, opts interface{}) (interface{}, error) {
368 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
372 arvados.EndpointGroupContentsUUIDInPath,
373 func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
374 func(ctx context.Context, opts interface{}) (interface{}, error) {
375 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
379 arvados.EndpointGroupShared,
380 func() interface{} { return &arvados.ListOptions{Limit: -1} },
381 func(ctx context.Context, opts interface{}) (interface{}, error) {
382 return rtr.backend.GroupShared(ctx, *opts.(*arvados.ListOptions))
386 arvados.EndpointGroupGet,
387 func() interface{} { return &arvados.GetOptions{} },
388 func(ctx context.Context, opts interface{}) (interface{}, error) {
389 return rtr.backend.GroupGet(ctx, *opts.(*arvados.GetOptions))
393 arvados.EndpointGroupDelete,
394 func() interface{} { return &arvados.DeleteOptions{} },
395 func(ctx context.Context, opts interface{}) (interface{}, error) {
396 return rtr.backend.GroupDelete(ctx, *opts.(*arvados.DeleteOptions))
400 arvados.EndpointGroupTrash,
401 func() interface{} { return &arvados.DeleteOptions{} },
402 func(ctx context.Context, opts interface{}) (interface{}, error) {
403 return rtr.backend.GroupTrash(ctx, *opts.(*arvados.DeleteOptions))
407 arvados.EndpointGroupUntrash,
408 func() interface{} { return &arvados.UntrashOptions{} },
409 func(ctx context.Context, opts interface{}) (interface{}, error) {
410 return rtr.backend.GroupUntrash(ctx, *opts.(*arvados.UntrashOptions))
414 arvados.EndpointLinkCreate,
415 func() interface{} { return &arvados.CreateOptions{} },
416 func(ctx context.Context, opts interface{}) (interface{}, error) {
417 return rtr.backend.LinkCreate(ctx, *opts.(*arvados.CreateOptions))
421 arvados.EndpointLinkUpdate,
422 func() interface{} { return &arvados.UpdateOptions{} },
423 func(ctx context.Context, opts interface{}) (interface{}, error) {
424 return rtr.backend.LinkUpdate(ctx, *opts.(*arvados.UpdateOptions))
428 arvados.EndpointLinkList,
429 func() interface{} { return &arvados.ListOptions{Limit: -1} },
430 func(ctx context.Context, opts interface{}) (interface{}, error) {
431 return rtr.backend.LinkList(ctx, *opts.(*arvados.ListOptions))
435 arvados.EndpointLinkGet,
436 func() interface{} { return &arvados.GetOptions{} },
437 func(ctx context.Context, opts interface{}) (interface{}, error) {
438 return rtr.backend.LinkGet(ctx, *opts.(*arvados.GetOptions))
442 arvados.EndpointLinkDelete,
443 func() interface{} { return &arvados.DeleteOptions{} },
444 func(ctx context.Context, opts interface{}) (interface{}, error) {
445 return rtr.backend.LinkDelete(ctx, *opts.(*arvados.DeleteOptions))
449 arvados.EndpointLogCreate,
450 func() interface{} { return &arvados.CreateOptions{} },
451 func(ctx context.Context, opts interface{}) (interface{}, error) {
452 return rtr.backend.LogCreate(ctx, *opts.(*arvados.CreateOptions))
456 arvados.EndpointLogUpdate,
457 func() interface{} { return &arvados.UpdateOptions{} },
458 func(ctx context.Context, opts interface{}) (interface{}, error) {
459 return rtr.backend.LogUpdate(ctx, *opts.(*arvados.UpdateOptions))
463 arvados.EndpointLogList,
464 func() interface{} { return &arvados.ListOptions{Limit: -1} },
465 func(ctx context.Context, opts interface{}) (interface{}, error) {
466 return rtr.backend.LogList(ctx, *opts.(*arvados.ListOptions))
470 arvados.EndpointLogGet,
471 func() interface{} { return &arvados.GetOptions{} },
472 func(ctx context.Context, opts interface{}) (interface{}, error) {
473 return rtr.backend.LogGet(ctx, *opts.(*arvados.GetOptions))
477 arvados.EndpointLogDelete,
478 func() interface{} { return &arvados.DeleteOptions{} },
479 func(ctx context.Context, opts interface{}) (interface{}, error) {
480 return rtr.backend.LogDelete(ctx, *opts.(*arvados.DeleteOptions))
484 arvados.EndpointAPIClientAuthorizationCreate,
485 func() interface{} { return &arvados.CreateOptions{} },
486 func(ctx context.Context, opts interface{}) (interface{}, error) {
487 return rtr.backend.APIClientAuthorizationCreate(ctx, *opts.(*arvados.CreateOptions))
491 arvados.EndpointAPIClientAuthorizationUpdate,
492 func() interface{} { return &arvados.UpdateOptions{} },
493 func(ctx context.Context, opts interface{}) (interface{}, error) {
494 return rtr.backend.APIClientAuthorizationUpdate(ctx, *opts.(*arvados.UpdateOptions))
498 arvados.EndpointAPIClientAuthorizationDelete,
499 func() interface{} { return &arvados.DeleteOptions{} },
500 func(ctx context.Context, opts interface{}) (interface{}, error) {
501 return rtr.backend.APIClientAuthorizationDelete(ctx, *opts.(*arvados.DeleteOptions))
505 arvados.EndpointAPIClientAuthorizationList,
506 func() interface{} { return &arvados.ListOptions{Limit: -1} },
507 func(ctx context.Context, opts interface{}) (interface{}, error) {
508 return rtr.backend.APIClientAuthorizationList(ctx, *opts.(*arvados.ListOptions))
512 arvados.EndpointAPIClientAuthorizationCurrent,
513 func() interface{} { return &arvados.GetOptions{} },
514 func(ctx context.Context, opts interface{}) (interface{}, error) {
515 return rtr.backend.APIClientAuthorizationCurrent(ctx, *opts.(*arvados.GetOptions))
519 arvados.EndpointAPIClientAuthorizationGet,
520 func() interface{} { return &arvados.GetOptions{} },
521 func(ctx context.Context, opts interface{}) (interface{}, error) {
522 return rtr.backend.APIClientAuthorizationGet(ctx, *opts.(*arvados.GetOptions))
526 arvados.EndpointUserCreate,
527 func() interface{} { return &arvados.CreateOptions{} },
528 func(ctx context.Context, opts interface{}) (interface{}, error) {
529 return rtr.backend.UserCreate(ctx, *opts.(*arvados.CreateOptions))
533 arvados.EndpointUserMerge,
534 func() interface{} { return &arvados.UserMergeOptions{} },
535 func(ctx context.Context, opts interface{}) (interface{}, error) {
536 return rtr.backend.UserMerge(ctx, *opts.(*arvados.UserMergeOptions))
540 arvados.EndpointUserActivate,
541 func() interface{} { return &arvados.UserActivateOptions{} },
542 func(ctx context.Context, opts interface{}) (interface{}, error) {
543 return rtr.backend.UserActivate(ctx, *opts.(*arvados.UserActivateOptions))
547 arvados.EndpointUserSetup,
548 func() interface{} { return &arvados.UserSetupOptions{} },
549 func(ctx context.Context, opts interface{}) (interface{}, error) {
550 return rtr.backend.UserSetup(ctx, *opts.(*arvados.UserSetupOptions))
554 arvados.EndpointUserUnsetup,
555 func() interface{} { return &arvados.GetOptions{} },
556 func(ctx context.Context, opts interface{}) (interface{}, error) {
557 return rtr.backend.UserUnsetup(ctx, *opts.(*arvados.GetOptions))
561 arvados.EndpointUserGetCurrent,
562 func() interface{} { return &arvados.GetOptions{} },
563 func(ctx context.Context, opts interface{}) (interface{}, error) {
564 return rtr.backend.UserGetCurrent(ctx, *opts.(*arvados.GetOptions))
568 arvados.EndpointUserGetSystem,
569 func() interface{} { return &arvados.GetOptions{} },
570 func(ctx context.Context, opts interface{}) (interface{}, error) {
571 return rtr.backend.UserGetSystem(ctx, *opts.(*arvados.GetOptions))
575 arvados.EndpointUserGet,
576 func() interface{} { return &arvados.GetOptions{} },
577 func(ctx context.Context, opts interface{}) (interface{}, error) {
578 return rtr.backend.UserGet(ctx, *opts.(*arvados.GetOptions))
582 arvados.EndpointUserUpdate,
583 func() interface{} { return &arvados.UpdateOptions{} },
584 func(ctx context.Context, opts interface{}) (interface{}, error) {
585 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
589 arvados.EndpointUserList,
590 func() interface{} { return &arvados.ListOptions{Limit: -1} },
591 func(ctx context.Context, opts interface{}) (interface{}, error) {
592 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
596 arvados.EndpointUserBatchUpdate,
597 func() interface{} { return &arvados.UserBatchUpdateOptions{} },
598 func(ctx context.Context, opts interface{}) (interface{}, error) {
599 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
603 arvados.EndpointUserDelete,
604 func() interface{} { return &arvados.DeleteOptions{} },
605 func(ctx context.Context, opts interface{}) (interface{}, error) {
606 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
610 arvados.EndpointUserAuthenticate,
611 func() interface{} { return &arvados.UserAuthenticateOptions{} },
612 func(ctx context.Context, opts interface{}) (interface{}, error) {
613 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
618 if rtr.config.WrapCalls != nil {
619 exec = rtr.config.WrapCalls(exec)
621 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
623 rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
624 if req.Method == "OPTIONS" {
625 // For non-webdav endpoints, return an empty
626 // response with the CORS headers we already
627 // added in ServeHTTP.
628 w.WriteHeader(http.StatusOK)
631 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
633 rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
634 if req.Method == "OPTIONS" {
635 // For non-webdav endpoints, return an empty
636 // response with the CORS headers we already
637 // added in ServeHTTP.
638 w.WriteHeader(http.StatusOK)
641 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
645 var altMethod = map[string]string{
646 "PATCH": "PUT", // Accept PUT as a synonym for PATCH
647 "GET": "HEAD", // Accept HEAD at any GET route
650 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
651 methods := []string{endpoint.Method}
652 if alt, ok := altMethod[endpoint.Method]; ok {
653 methods = append(methods, alt)
655 if strings.HasSuffix(endpoint.Path, ".*}") {
657 methods = append(methods, "OPTIONS", "PROPFIND")
659 rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
660 logger := ctxlog.FromContext(req.Context())
661 opts := defaultOpts()
662 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey, opts)
664 logger.WithFields(logrus.Fields{
666 "method": endpoint.Method,
667 "endpoint": endpoint,
668 }).WithError(err).Debug("error loading request params")
669 rtr.sendError(w, err)
672 respOpts, err := rtr.responseOptions(opts)
674 logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
675 rtr.sendError(w, err)
679 creds := auth.CredentialsFromRequest(req)
680 err = creds.LoadTokensFromHTTPRequestBody(req)
682 rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
685 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
686 for _, t := range rt {
687 if t, ok := t.(string); ok {
688 creds.Tokens = append(creds.Tokens, t)
692 ctx := auth.NewContext(req.Context(), creds)
693 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
694 req = req.WithContext(ctx)
696 httpserver.SetResponseLogFields(ctx, logrus.Fields{"tokenUUIDs": creds.TokenUUIDs()})
698 logger.WithFields(logrus.Fields{
699 "apiEndpoint": endpoint,
700 "apiOptsType": fmt.Sprintf("%T", opts),
703 resp, err := exec(ctx, opts)
705 logger.WithError(err).Debugf("returning error type %T", err)
706 rtr.sendError(w, err)
709 rtr.sendResponse(w, req, resp, respOpts)
713 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
714 if len(r.Host) > 28 && arvadosclient.UUIDMatch(r.Host[:27]) && r.Host[27] == '-' {
716 fmt.Sscanf(r.Host[28:], "%d", &port)
718 rtr.sendError(w, httpError(http.StatusBadRequest, fmt.Errorf("cannot parse port number from vhost %q", r.Host)))
721 rtr.serveContainerHTTPProxy(w, r, r.Host[:27], port)
724 switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
725 case "login", "logout", "auth":
727 w.Header().Set("Access-Control-Allow-Origin", "*")
728 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, PROPFIND, PUT, POST, PATCH, DELETE")
729 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Range, X-Http-Method-Override")
730 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
731 w.Header().Set("Access-Control-Max-Age", "86486400")
734 // Wrap r.Body in a http.MaxBytesReader(), otherwise
735 // r.ParseForm() uses a default max request body size
736 // of 10 megabytes. Note we rely on the Nginx
737 // configuration to enforce the real max body size.
738 max := int64(rtr.config.MaxRequestSize)
740 max = math.MaxInt64 - 1
742 r.Body = http.MaxBytesReader(w, r.Body, max)
744 if r.Method == "POST" {
747 if err.Error() == "http: request body too large" {
748 err = httpError(http.StatusRequestEntityTooLarge, err)
750 rtr.sendError(w, err)
753 if m := r.FormValue("_method"); m != "" {
757 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
763 rtr.mux.ServeHTTP(w, r)
766 func (rtr *router) serveContainerHTTPProxy(w http.ResponseWriter, req *http.Request, uuid string, port int) {
767 // This API bypasses the generic auth middleware in
768 // addRoute(), so here we need to load tokens into ctx, log
769 // their UUIDs, and propagate the incoming X-Request-Id.
771 if cookie, err := req.Cookie("arvados_api_token"); err == nil && len(cookie.Value) != 0 {
772 if token, err := auth.DecodeTokenCookie(cookie.Value); err == nil {
773 creds := auth.NewCredentials(string(token))
774 ctx = auth.NewContext(ctx, creds)
775 httpserver.SetResponseLogFields(ctx, logrus.Fields{"tokenUUIDs": creds.TokenUUIDs()})
779 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
780 req = req.WithContext(ctx)
782 // Load the NoForward value from the X-Arvados-No-Forward
783 // header, but don't pass the header through in the proxied
785 noForward := req.Header.Get("X-Arvados-No-Forward") != ""
786 req.Header.Del("X-Arvados-No-Forward")
788 handler, err := rtr.backend.ContainerHTTPProxy(req.Context(), arvados.ContainerHTTPProxyOptions{
792 NoForward: noForward,
795 rtr.sendError(w, err)
798 handler.ServeHTTP(w, req)