1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/lib/controller/api"
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/arvados.git/sdk/go/auth"
16 "git.arvados.org/arvados.git/sdk/go/ctxlog"
17 "git.arvados.org/arvados.git/sdk/go/httpserver"
18 "github.com/gorilla/mux"
19 "github.com/sirupsen/logrus"
25 wrapCalls func(api.RoutableFunc) api.RoutableFunc
28 // New returns a new router (which implements the http.Handler
29 // interface) that serves requests by calling Arvados API methods on
32 // If wrapCalls is not nil, it is called once for each API method, and
33 // the returned method is used in its place. This can be used to
34 // install hooks before and after each API call and alter responses;
35 // see localdb.WrapCallsInTransaction for an example.
36 func New(backend arvados.API, wrapCalls func(api.RoutableFunc) api.RoutableFunc) *router {
46 func (rtr *router) addRoutes() {
47 for _, route := range []struct {
48 endpoint arvados.APIEndpoint
49 defaultOpts func() interface{}
53 arvados.EndpointConfigGet,
54 func() interface{} { return &struct{}{} },
55 func(ctx context.Context, opts interface{}) (interface{}, error) {
56 return rtr.backend.ConfigGet(ctx)
60 arvados.EndpointLogin,
61 func() interface{} { return &arvados.LoginOptions{} },
62 func(ctx context.Context, opts interface{}) (interface{}, error) {
63 return rtr.backend.Login(ctx, *opts.(*arvados.LoginOptions))
67 arvados.EndpointLogout,
68 func() interface{} { return &arvados.LogoutOptions{} },
69 func(ctx context.Context, opts interface{}) (interface{}, error) {
70 return rtr.backend.Logout(ctx, *opts.(*arvados.LogoutOptions))
74 arvados.EndpointCollectionCreate,
75 func() interface{} { return &arvados.CreateOptions{} },
76 func(ctx context.Context, opts interface{}) (interface{}, error) {
77 return rtr.backend.CollectionCreate(ctx, *opts.(*arvados.CreateOptions))
81 arvados.EndpointCollectionUpdate,
82 func() interface{} { return &arvados.UpdateOptions{} },
83 func(ctx context.Context, opts interface{}) (interface{}, error) {
84 return rtr.backend.CollectionUpdate(ctx, *opts.(*arvados.UpdateOptions))
88 arvados.EndpointCollectionGet,
89 func() interface{} { return &arvados.GetOptions{} },
90 func(ctx context.Context, opts interface{}) (interface{}, error) {
91 return rtr.backend.CollectionGet(ctx, *opts.(*arvados.GetOptions))
95 arvados.EndpointCollectionList,
96 func() interface{} { return &arvados.ListOptions{Limit: -1} },
97 func(ctx context.Context, opts interface{}) (interface{}, error) {
98 return rtr.backend.CollectionList(ctx, *opts.(*arvados.ListOptions))
102 arvados.EndpointCollectionProvenance,
103 func() interface{} { return &arvados.GetOptions{} },
104 func(ctx context.Context, opts interface{}) (interface{}, error) {
105 return rtr.backend.CollectionProvenance(ctx, *opts.(*arvados.GetOptions))
109 arvados.EndpointCollectionUsedBy,
110 func() interface{} { return &arvados.GetOptions{} },
111 func(ctx context.Context, opts interface{}) (interface{}, error) {
112 return rtr.backend.CollectionUsedBy(ctx, *opts.(*arvados.GetOptions))
116 arvados.EndpointCollectionDelete,
117 func() interface{} { return &arvados.DeleteOptions{} },
118 func(ctx context.Context, opts interface{}) (interface{}, error) {
119 return rtr.backend.CollectionDelete(ctx, *opts.(*arvados.DeleteOptions))
123 arvados.EndpointCollectionTrash,
124 func() interface{} { return &arvados.DeleteOptions{} },
125 func(ctx context.Context, opts interface{}) (interface{}, error) {
126 return rtr.backend.CollectionTrash(ctx, *opts.(*arvados.DeleteOptions))
130 arvados.EndpointCollectionUntrash,
131 func() interface{} { return &arvados.UntrashOptions{} },
132 func(ctx context.Context, opts interface{}) (interface{}, error) {
133 return rtr.backend.CollectionUntrash(ctx, *opts.(*arvados.UntrashOptions))
137 arvados.EndpointContainerCreate,
138 func() interface{} { return &arvados.CreateOptions{} },
139 func(ctx context.Context, opts interface{}) (interface{}, error) {
140 return rtr.backend.ContainerCreate(ctx, *opts.(*arvados.CreateOptions))
144 arvados.EndpointContainerUpdate,
145 func() interface{} { return &arvados.UpdateOptions{} },
146 func(ctx context.Context, opts interface{}) (interface{}, error) {
147 return rtr.backend.ContainerUpdate(ctx, *opts.(*arvados.UpdateOptions))
151 arvados.EndpointContainerGet,
152 func() interface{} { return &arvados.GetOptions{} },
153 func(ctx context.Context, opts interface{}) (interface{}, error) {
154 return rtr.backend.ContainerGet(ctx, *opts.(*arvados.GetOptions))
158 arvados.EndpointContainerList,
159 func() interface{} { return &arvados.ListOptions{Limit: -1} },
160 func(ctx context.Context, opts interface{}) (interface{}, error) {
161 return rtr.backend.ContainerList(ctx, *opts.(*arvados.ListOptions))
165 arvados.EndpointContainerDelete,
166 func() interface{} { return &arvados.DeleteOptions{} },
167 func(ctx context.Context, opts interface{}) (interface{}, error) {
168 return rtr.backend.ContainerDelete(ctx, *opts.(*arvados.DeleteOptions))
172 arvados.EndpointContainerLock,
174 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
176 func(ctx context.Context, opts interface{}) (interface{}, error) {
177 return rtr.backend.ContainerLock(ctx, *opts.(*arvados.GetOptions))
181 arvados.EndpointContainerUnlock,
183 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
185 func(ctx context.Context, opts interface{}) (interface{}, error) {
186 return rtr.backend.ContainerUnlock(ctx, *opts.(*arvados.GetOptions))
190 arvados.EndpointSpecimenCreate,
191 func() interface{} { return &arvados.CreateOptions{} },
192 func(ctx context.Context, opts interface{}) (interface{}, error) {
193 return rtr.backend.SpecimenCreate(ctx, *opts.(*arvados.CreateOptions))
197 arvados.EndpointSpecimenUpdate,
198 func() interface{} { return &arvados.UpdateOptions{} },
199 func(ctx context.Context, opts interface{}) (interface{}, error) {
200 return rtr.backend.SpecimenUpdate(ctx, *opts.(*arvados.UpdateOptions))
204 arvados.EndpointSpecimenGet,
205 func() interface{} { return &arvados.GetOptions{} },
206 func(ctx context.Context, opts interface{}) (interface{}, error) {
207 return rtr.backend.SpecimenGet(ctx, *opts.(*arvados.GetOptions))
211 arvados.EndpointSpecimenList,
212 func() interface{} { return &arvados.ListOptions{Limit: -1} },
213 func(ctx context.Context, opts interface{}) (interface{}, error) {
214 return rtr.backend.SpecimenList(ctx, *opts.(*arvados.ListOptions))
218 arvados.EndpointSpecimenDelete,
219 func() interface{} { return &arvados.DeleteOptions{} },
220 func(ctx context.Context, opts interface{}) (interface{}, error) {
221 return rtr.backend.SpecimenDelete(ctx, *opts.(*arvados.DeleteOptions))
225 arvados.EndpointUserCreate,
226 func() interface{} { return &arvados.CreateOptions{} },
227 func(ctx context.Context, opts interface{}) (interface{}, error) {
228 return rtr.backend.UserCreate(ctx, *opts.(*arvados.CreateOptions))
232 arvados.EndpointUserMerge,
233 func() interface{} { return &arvados.UserMergeOptions{} },
234 func(ctx context.Context, opts interface{}) (interface{}, error) {
235 return rtr.backend.UserMerge(ctx, *opts.(*arvados.UserMergeOptions))
239 arvados.EndpointUserActivate,
240 func() interface{} { return &arvados.UserActivateOptions{} },
241 func(ctx context.Context, opts interface{}) (interface{}, error) {
242 return rtr.backend.UserActivate(ctx, *opts.(*arvados.UserActivateOptions))
246 arvados.EndpointUserSetup,
247 func() interface{} { return &arvados.UserSetupOptions{} },
248 func(ctx context.Context, opts interface{}) (interface{}, error) {
249 return rtr.backend.UserSetup(ctx, *opts.(*arvados.UserSetupOptions))
253 arvados.EndpointUserUnsetup,
254 func() interface{} { return &arvados.GetOptions{} },
255 func(ctx context.Context, opts interface{}) (interface{}, error) {
256 return rtr.backend.UserUnsetup(ctx, *opts.(*arvados.GetOptions))
260 arvados.EndpointUserGetCurrent,
261 func() interface{} { return &arvados.GetOptions{} },
262 func(ctx context.Context, opts interface{}) (interface{}, error) {
263 return rtr.backend.UserGetCurrent(ctx, *opts.(*arvados.GetOptions))
267 arvados.EndpointUserGetSystem,
268 func() interface{} { return &arvados.GetOptions{} },
269 func(ctx context.Context, opts interface{}) (interface{}, error) {
270 return rtr.backend.UserGetSystem(ctx, *opts.(*arvados.GetOptions))
274 arvados.EndpointUserGet,
275 func() interface{} { return &arvados.GetOptions{} },
276 func(ctx context.Context, opts interface{}) (interface{}, error) {
277 return rtr.backend.UserGet(ctx, *opts.(*arvados.GetOptions))
281 arvados.EndpointUserUpdateUUID,
282 func() interface{} { return &arvados.UpdateUUIDOptions{} },
283 func(ctx context.Context, opts interface{}) (interface{}, error) {
284 return rtr.backend.UserUpdateUUID(ctx, *opts.(*arvados.UpdateUUIDOptions))
288 arvados.EndpointUserUpdate,
289 func() interface{} { return &arvados.UpdateOptions{} },
290 func(ctx context.Context, opts interface{}) (interface{}, error) {
291 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
295 arvados.EndpointUserList,
296 func() interface{} { return &arvados.ListOptions{Limit: -1} },
297 func(ctx context.Context, opts interface{}) (interface{}, error) {
298 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
302 arvados.EndpointUserBatchUpdate,
303 func() interface{} { return &arvados.UserBatchUpdateOptions{} },
304 func(ctx context.Context, opts interface{}) (interface{}, error) {
305 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
309 arvados.EndpointUserDelete,
310 func() interface{} { return &arvados.DeleteOptions{} },
311 func(ctx context.Context, opts interface{}) (interface{}, error) {
312 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
316 arvados.EndpointUserAuthenticate,
317 func() interface{} { return &arvados.UserAuthenticateOptions{} },
318 func(ctx context.Context, opts interface{}) (interface{}, error) {
319 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
324 if rtr.wrapCalls != nil {
325 exec = rtr.wrapCalls(exec)
327 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
329 rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
330 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
332 rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
333 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
337 var altMethod = map[string]string{
338 "PATCH": "PUT", // Accept PUT as a synonym for PATCH
339 "GET": "HEAD", // Accept HEAD at any GET route
342 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
343 methods := []string{endpoint.Method}
344 if alt, ok := altMethod[endpoint.Method]; ok {
345 methods = append(methods, alt)
347 rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
348 logger := ctxlog.FromContext(req.Context())
349 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey)
351 logger.WithFields(logrus.Fields{
353 "method": endpoint.Method,
354 "endpoint": endpoint,
355 }).WithError(err).Debug("error loading request params")
356 rtr.sendError(w, err)
359 opts := defaultOpts()
360 err = rtr.transcode(params, opts)
362 logger.WithField("params", params).WithError(err).Debugf("error transcoding params to %T", opts)
363 rtr.sendError(w, err)
366 respOpts, err := rtr.responseOptions(opts)
368 logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
369 rtr.sendError(w, err)
373 creds := auth.CredentialsFromRequest(req)
374 err = creds.LoadTokensFromHTTPRequestBody(req)
376 rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
379 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
380 for _, t := range rt {
381 if t, ok := t.(string); ok {
382 creds.Tokens = append(creds.Tokens, t)
386 ctx := auth.NewContext(req.Context(), creds)
387 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
388 logger.WithFields(logrus.Fields{
389 "apiEndpoint": endpoint,
390 "apiOptsType": fmt.Sprintf("%T", opts),
393 resp, err := exec(ctx, opts)
395 logger.WithError(err).Debugf("returning error type %T", err)
396 rtr.sendError(w, err)
399 rtr.sendResponse(w, req, resp, respOpts)
403 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
404 switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
405 case "login", "logout", "auth":
407 w.Header().Set("Access-Control-Allow-Origin", "*")
408 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, PUT, POST, PATCH, DELETE")
409 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Http-Method-Override")
410 w.Header().Set("Access-Control-Max-Age", "86486400")
412 if r.Method == "OPTIONS" {
415 if r.Method == "POST" {
417 if m := r.FormValue("_method"); m != "" {
421 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
427 rtr.mux.ServeHTTP(w, r)