22226: Simplify test.
[arvados.git] / lib / controller / router / router.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package router
6
7 import (
8         "context"
9         "fmt"
10         "math"
11         "net/http"
12         "strings"
13
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"
21 )
22
23 type router struct {
24         mux     *mux.Router
25         backend arvados.API
26         config  Config
27 }
28
29 type Config struct {
30         // Return an error if request body exceeds this size. 0 means
31         // unlimited.
32         MaxRequestSize int
33
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
38         // an example.
39         WrapCalls func(api.RoutableFunc) api.RoutableFunc
40 }
41
42 // New returns a new router (which implements the http.Handler
43 // interface) that serves requests by calling Arvados API methods on
44 // the given backend.
45 func New(backend arvados.API, config Config) *router {
46         rtr := &router{
47                 mux:     mux.NewRouter(),
48                 backend: backend,
49                 config:  config,
50         }
51         rtr.addRoutes()
52         return rtr
53 }
54
55 func (rtr *router) addRoutes() {
56         for _, route := range []struct {
57                 endpoint    arvados.APIEndpoint
58                 defaultOpts func() interface{}
59                 exec        api.RoutableFunc
60         }{
61                 {
62                         arvados.EndpointConfigGet,
63                         func() interface{} { return &struct{}{} },
64                         func(ctx context.Context, opts interface{}) (interface{}, error) {
65                                 return rtr.backend.ConfigGet(ctx)
66                         },
67                 },
68                 {
69                         arvados.EndpointVocabularyGet,
70                         func() interface{} { return &struct{}{} },
71                         func(ctx context.Context, opts interface{}) (interface{}, error) {
72                                 return rtr.backend.VocabularyGet(ctx)
73                         },
74                 },
75                 {
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))
80                         },
81                 },
82                 {
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))
87                         },
88                 },
89                 {
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))
94                         },
95                 },
96                 {
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))
101                         },
102                 },
103                 {
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))
108                         },
109                 },
110                 {
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))
115                         },
116                 },
117                 {
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))
122                         },
123                 },
124                 {
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))
129                         },
130                 },
131                 {
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))
136                         },
137                 },
138                 {
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))
143                         },
144                 },
145                 {
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))
150                         },
151                 },
152                 {
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))
157                         },
158                 },
159                 {
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))
164                         },
165                 },
166                 {
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))
171                         },
172                 },
173                 {
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))
178                         },
179                 },
180                 {
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))
185                         },
186                 },
187                 {
188                         arvados.EndpointComputedPermissionList,
189                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
190                         func(ctx context.Context, opts interface{}) (interface{}, error) {
191                                 return rtr.backend.ComputedPermissionList(ctx, *opts.(*arvados.ListOptions))
192                         },
193                 },
194                 {
195                         arvados.EndpointContainerCreate,
196                         func() interface{} { return &arvados.CreateOptions{} },
197                         func(ctx context.Context, opts interface{}) (interface{}, error) {
198                                 return rtr.backend.ContainerCreate(ctx, *opts.(*arvados.CreateOptions))
199                         },
200                 },
201                 {
202                         arvados.EndpointContainerPriorityUpdate,
203                         func() interface{} { return &arvados.UpdateOptions{} },
204                         func(ctx context.Context, opts interface{}) (interface{}, error) {
205                                 return rtr.backend.ContainerPriorityUpdate(ctx, *opts.(*arvados.UpdateOptions))
206                         },
207                 },
208                 {
209                         arvados.EndpointContainerUpdate,
210                         func() interface{} { return &arvados.UpdateOptions{} },
211                         func(ctx context.Context, opts interface{}) (interface{}, error) {
212                                 return rtr.backend.ContainerUpdate(ctx, *opts.(*arvados.UpdateOptions))
213                         },
214                 },
215                 {
216                         arvados.EndpointContainerGet,
217                         func() interface{} { return &arvados.GetOptions{} },
218                         func(ctx context.Context, opts interface{}) (interface{}, error) {
219                                 return rtr.backend.ContainerGet(ctx, *opts.(*arvados.GetOptions))
220                         },
221                 },
222                 {
223                         arvados.EndpointContainerList,
224                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
225                         func(ctx context.Context, opts interface{}) (interface{}, error) {
226                                 return rtr.backend.ContainerList(ctx, *opts.(*arvados.ListOptions))
227                         },
228                 },
229                 {
230                         arvados.EndpointContainerDelete,
231                         func() interface{} { return &arvados.DeleteOptions{} },
232                         func(ctx context.Context, opts interface{}) (interface{}, error) {
233                                 return rtr.backend.ContainerDelete(ctx, *opts.(*arvados.DeleteOptions))
234                         },
235                 },
236                 {
237                         arvados.EndpointContainerLock,
238                         func() interface{} {
239                                 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
240                         },
241                         func(ctx context.Context, opts interface{}) (interface{}, error) {
242                                 return rtr.backend.ContainerLock(ctx, *opts.(*arvados.GetOptions))
243                         },
244                 },
245                 {
246                         arvados.EndpointContainerUnlock,
247                         func() interface{} {
248                                 return &arvados.GetOptions{Select: []string{"uuid", "state", "priority", "auth_uuid", "locked_by_uuid"}}
249                         },
250                         func(ctx context.Context, opts interface{}) (interface{}, error) {
251                                 return rtr.backend.ContainerUnlock(ctx, *opts.(*arvados.GetOptions))
252                         },
253                 },
254                 {
255                         arvados.EndpointContainerSSH,
256                         func() interface{} { return &arvados.ContainerSSHOptions{} },
257                         func(ctx context.Context, opts interface{}) (interface{}, error) {
258                                 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
259                         },
260                 },
261                 {
262                         arvados.EndpointContainerSSHCompat,
263                         func() interface{} { return &arvados.ContainerSSHOptions{} },
264                         func(ctx context.Context, opts interface{}) (interface{}, error) {
265                                 return rtr.backend.ContainerSSH(ctx, *opts.(*arvados.ContainerSSHOptions))
266                         },
267                 },
268                 {
269                         // arvados-client built before commit
270                         // bdc29d3129f6d75aa9ce0a24ffb849a272b06f08
271                         // used GET with params in headers instead of
272                         // POST form
273                         arvados.APIEndpoint{"GET", "arvados/v1/connect/{uuid}/ssh", ""},
274                         func() interface{} { return &arvados.ContainerSSHOptions{} },
275                         func(ctx context.Context, opts interface{}) (interface{}, error) {
276                                 return nil, httpError(http.StatusGone, fmt.Errorf("API endpoint is obsolete -- please upgrade your arvados-client program"))
277                         },
278                 },
279                 {
280                         arvados.EndpointContainerGatewayTunnel,
281                         func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
282                         func(ctx context.Context, opts interface{}) (interface{}, error) {
283                                 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
284                         },
285                 },
286                 {
287                         arvados.EndpointContainerGatewayTunnelCompat,
288                         func() interface{} { return &arvados.ContainerGatewayTunnelOptions{} },
289                         func(ctx context.Context, opts interface{}) (interface{}, error) {
290                                 return rtr.backend.ContainerGatewayTunnel(ctx, *opts.(*arvados.ContainerGatewayTunnelOptions))
291                         },
292                 },
293                 {
294                         arvados.EndpointContainerRequestCreate,
295                         func() interface{} { return &arvados.CreateOptions{} },
296                         func(ctx context.Context, opts interface{}) (interface{}, error) {
297                                 return rtr.backend.ContainerRequestCreate(ctx, *opts.(*arvados.CreateOptions))
298                         },
299                 },
300                 {
301                         arvados.EndpointContainerRequestUpdate,
302                         func() interface{} { return &arvados.UpdateOptions{} },
303                         func(ctx context.Context, opts interface{}) (interface{}, error) {
304                                 return rtr.backend.ContainerRequestUpdate(ctx, *opts.(*arvados.UpdateOptions))
305                         },
306                 },
307                 {
308                         arvados.EndpointContainerRequestGet,
309                         func() interface{} { return &arvados.GetOptions{} },
310                         func(ctx context.Context, opts interface{}) (interface{}, error) {
311                                 return rtr.backend.ContainerRequestGet(ctx, *opts.(*arvados.GetOptions))
312                         },
313                 },
314                 {
315                         arvados.EndpointContainerRequestList,
316                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
317                         func(ctx context.Context, opts interface{}) (interface{}, error) {
318                                 return rtr.backend.ContainerRequestList(ctx, *opts.(*arvados.ListOptions))
319                         },
320                 },
321                 {
322                         arvados.EndpointContainerRequestDelete,
323                         func() interface{} { return &arvados.DeleteOptions{} },
324                         func(ctx context.Context, opts interface{}) (interface{}, error) {
325                                 return rtr.backend.ContainerRequestDelete(ctx, *opts.(*arvados.DeleteOptions))
326                         },
327                 },
328                 {
329                         arvados.EndpointContainerRequestContainerStatus,
330                         func() interface{} { return &arvados.GetOptions{} },
331                         func(ctx context.Context, opts interface{}) (interface{}, error) {
332                                 return rtr.backend.ContainerRequestContainerStatus(ctx, *opts.(*arvados.GetOptions))
333                         },
334                 },
335                 {
336                         arvados.EndpointContainerRequestLog,
337                         func() interface{} { return &arvados.ContainerLogOptions{} },
338                         func(ctx context.Context, opts interface{}) (interface{}, error) {
339                                 return rtr.backend.ContainerRequestLog(ctx, *opts.(*arvados.ContainerLogOptions))
340                         },
341                 },
342                 {
343                         arvados.EndpointGroupCreate,
344                         func() interface{} { return &arvados.CreateOptions{} },
345                         func(ctx context.Context, opts interface{}) (interface{}, error) {
346                                 return rtr.backend.GroupCreate(ctx, *opts.(*arvados.CreateOptions))
347                         },
348                 },
349                 {
350                         arvados.EndpointGroupUpdate,
351                         func() interface{} { return &arvados.UpdateOptions{} },
352                         func(ctx context.Context, opts interface{}) (interface{}, error) {
353                                 return rtr.backend.GroupUpdate(ctx, *opts.(*arvados.UpdateOptions))
354                         },
355                 },
356                 {
357                         arvados.EndpointGroupList,
358                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
359                         func(ctx context.Context, opts interface{}) (interface{}, error) {
360                                 return rtr.backend.GroupList(ctx, *opts.(*arvados.ListOptions))
361                         },
362                 },
363                 {
364                         arvados.EndpointGroupContents,
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))
368                         },
369                 },
370                 {
371                         arvados.EndpointGroupContentsUUIDInPath,
372                         func() interface{} { return &arvados.GroupContentsOptions{Limit: -1} },
373                         func(ctx context.Context, opts interface{}) (interface{}, error) {
374                                 return rtr.backend.GroupContents(ctx, *opts.(*arvados.GroupContentsOptions))
375                         },
376                 },
377                 {
378                         arvados.EndpointGroupShared,
379                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
380                         func(ctx context.Context, opts interface{}) (interface{}, error) {
381                                 return rtr.backend.GroupShared(ctx, *opts.(*arvados.ListOptions))
382                         },
383                 },
384                 {
385                         arvados.EndpointGroupGet,
386                         func() interface{} { return &arvados.GetOptions{} },
387                         func(ctx context.Context, opts interface{}) (interface{}, error) {
388                                 return rtr.backend.GroupGet(ctx, *opts.(*arvados.GetOptions))
389                         },
390                 },
391                 {
392                         arvados.EndpointGroupDelete,
393                         func() interface{} { return &arvados.DeleteOptions{} },
394                         func(ctx context.Context, opts interface{}) (interface{}, error) {
395                                 return rtr.backend.GroupDelete(ctx, *opts.(*arvados.DeleteOptions))
396                         },
397                 },
398                 {
399                         arvados.EndpointGroupTrash,
400                         func() interface{} { return &arvados.DeleteOptions{} },
401                         func(ctx context.Context, opts interface{}) (interface{}, error) {
402                                 return rtr.backend.GroupTrash(ctx, *opts.(*arvados.DeleteOptions))
403                         },
404                 },
405                 {
406                         arvados.EndpointGroupUntrash,
407                         func() interface{} { return &arvados.UntrashOptions{} },
408                         func(ctx context.Context, opts interface{}) (interface{}, error) {
409                                 return rtr.backend.GroupUntrash(ctx, *opts.(*arvados.UntrashOptions))
410                         },
411                 },
412                 {
413                         arvados.EndpointLinkCreate,
414                         func() interface{} { return &arvados.CreateOptions{} },
415                         func(ctx context.Context, opts interface{}) (interface{}, error) {
416                                 return rtr.backend.LinkCreate(ctx, *opts.(*arvados.CreateOptions))
417                         },
418                 },
419                 {
420                         arvados.EndpointLinkUpdate,
421                         func() interface{} { return &arvados.UpdateOptions{} },
422                         func(ctx context.Context, opts interface{}) (interface{}, error) {
423                                 return rtr.backend.LinkUpdate(ctx, *opts.(*arvados.UpdateOptions))
424                         },
425                 },
426                 {
427                         arvados.EndpointLinkList,
428                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
429                         func(ctx context.Context, opts interface{}) (interface{}, error) {
430                                 return rtr.backend.LinkList(ctx, *opts.(*arvados.ListOptions))
431                         },
432                 },
433                 {
434                         arvados.EndpointLinkGet,
435                         func() interface{} { return &arvados.GetOptions{} },
436                         func(ctx context.Context, opts interface{}) (interface{}, error) {
437                                 return rtr.backend.LinkGet(ctx, *opts.(*arvados.GetOptions))
438                         },
439                 },
440                 {
441                         arvados.EndpointLinkDelete,
442                         func() interface{} { return &arvados.DeleteOptions{} },
443                         func(ctx context.Context, opts interface{}) (interface{}, error) {
444                                 return rtr.backend.LinkDelete(ctx, *opts.(*arvados.DeleteOptions))
445                         },
446                 },
447                 {
448                         arvados.EndpointLogCreate,
449                         func() interface{} { return &arvados.CreateOptions{} },
450                         func(ctx context.Context, opts interface{}) (interface{}, error) {
451                                 return rtr.backend.LogCreate(ctx, *opts.(*arvados.CreateOptions))
452                         },
453                 },
454                 {
455                         arvados.EndpointLogUpdate,
456                         func() interface{} { return &arvados.UpdateOptions{} },
457                         func(ctx context.Context, opts interface{}) (interface{}, error) {
458                                 return rtr.backend.LogUpdate(ctx, *opts.(*arvados.UpdateOptions))
459                         },
460                 },
461                 {
462                         arvados.EndpointLogList,
463                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
464                         func(ctx context.Context, opts interface{}) (interface{}, error) {
465                                 return rtr.backend.LogList(ctx, *opts.(*arvados.ListOptions))
466                         },
467                 },
468                 {
469                         arvados.EndpointLogGet,
470                         func() interface{} { return &arvados.GetOptions{} },
471                         func(ctx context.Context, opts interface{}) (interface{}, error) {
472                                 return rtr.backend.LogGet(ctx, *opts.(*arvados.GetOptions))
473                         },
474                 },
475                 {
476                         arvados.EndpointLogDelete,
477                         func() interface{} { return &arvados.DeleteOptions{} },
478                         func(ctx context.Context, opts interface{}) (interface{}, error) {
479                                 return rtr.backend.LogDelete(ctx, *opts.(*arvados.DeleteOptions))
480                         },
481                 },
482                 {
483                         arvados.EndpointAPIClientAuthorizationCreate,
484                         func() interface{} { return &arvados.CreateOptions{} },
485                         func(ctx context.Context, opts interface{}) (interface{}, error) {
486                                 return rtr.backend.APIClientAuthorizationCreate(ctx, *opts.(*arvados.CreateOptions))
487                         },
488                 },
489                 {
490                         arvados.EndpointAPIClientAuthorizationUpdate,
491                         func() interface{} { return &arvados.UpdateOptions{} },
492                         func(ctx context.Context, opts interface{}) (interface{}, error) {
493                                 return rtr.backend.APIClientAuthorizationUpdate(ctx, *opts.(*arvados.UpdateOptions))
494                         },
495                 },
496                 {
497                         arvados.EndpointAPIClientAuthorizationDelete,
498                         func() interface{} { return &arvados.DeleteOptions{} },
499                         func(ctx context.Context, opts interface{}) (interface{}, error) {
500                                 return rtr.backend.APIClientAuthorizationDelete(ctx, *opts.(*arvados.DeleteOptions))
501                         },
502                 },
503                 {
504                         arvados.EndpointAPIClientAuthorizationList,
505                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
506                         func(ctx context.Context, opts interface{}) (interface{}, error) {
507                                 return rtr.backend.APIClientAuthorizationList(ctx, *opts.(*arvados.ListOptions))
508                         },
509                 },
510                 {
511                         arvados.EndpointAPIClientAuthorizationCurrent,
512                         func() interface{} { return &arvados.GetOptions{} },
513                         func(ctx context.Context, opts interface{}) (interface{}, error) {
514                                 return rtr.backend.APIClientAuthorizationCurrent(ctx, *opts.(*arvados.GetOptions))
515                         },
516                 },
517                 {
518                         arvados.EndpointAPIClientAuthorizationGet,
519                         func() interface{} { return &arvados.GetOptions{} },
520                         func(ctx context.Context, opts interface{}) (interface{}, error) {
521                                 return rtr.backend.APIClientAuthorizationGet(ctx, *opts.(*arvados.GetOptions))
522                         },
523                 },
524                 {
525                         arvados.EndpointUserCreate,
526                         func() interface{} { return &arvados.CreateOptions{} },
527                         func(ctx context.Context, opts interface{}) (interface{}, error) {
528                                 return rtr.backend.UserCreate(ctx, *opts.(*arvados.CreateOptions))
529                         },
530                 },
531                 {
532                         arvados.EndpointUserMerge,
533                         func() interface{} { return &arvados.UserMergeOptions{} },
534                         func(ctx context.Context, opts interface{}) (interface{}, error) {
535                                 return rtr.backend.UserMerge(ctx, *opts.(*arvados.UserMergeOptions))
536                         },
537                 },
538                 {
539                         arvados.EndpointUserActivate,
540                         func() interface{} { return &arvados.UserActivateOptions{} },
541                         func(ctx context.Context, opts interface{}) (interface{}, error) {
542                                 return rtr.backend.UserActivate(ctx, *opts.(*arvados.UserActivateOptions))
543                         },
544                 },
545                 {
546                         arvados.EndpointUserSetup,
547                         func() interface{} { return &arvados.UserSetupOptions{} },
548                         func(ctx context.Context, opts interface{}) (interface{}, error) {
549                                 return rtr.backend.UserSetup(ctx, *opts.(*arvados.UserSetupOptions))
550                         },
551                 },
552                 {
553                         arvados.EndpointUserUnsetup,
554                         func() interface{} { return &arvados.GetOptions{} },
555                         func(ctx context.Context, opts interface{}) (interface{}, error) {
556                                 return rtr.backend.UserUnsetup(ctx, *opts.(*arvados.GetOptions))
557                         },
558                 },
559                 {
560                         arvados.EndpointUserGetCurrent,
561                         func() interface{} { return &arvados.GetOptions{} },
562                         func(ctx context.Context, opts interface{}) (interface{}, error) {
563                                 return rtr.backend.UserGetCurrent(ctx, *opts.(*arvados.GetOptions))
564                         },
565                 },
566                 {
567                         arvados.EndpointUserGetSystem,
568                         func() interface{} { return &arvados.GetOptions{} },
569                         func(ctx context.Context, opts interface{}) (interface{}, error) {
570                                 return rtr.backend.UserGetSystem(ctx, *opts.(*arvados.GetOptions))
571                         },
572                 },
573                 {
574                         arvados.EndpointUserGet,
575                         func() interface{} { return &arvados.GetOptions{} },
576                         func(ctx context.Context, opts interface{}) (interface{}, error) {
577                                 return rtr.backend.UserGet(ctx, *opts.(*arvados.GetOptions))
578                         },
579                 },
580                 {
581                         arvados.EndpointUserUpdate,
582                         func() interface{} { return &arvados.UpdateOptions{} },
583                         func(ctx context.Context, opts interface{}) (interface{}, error) {
584                                 return rtr.backend.UserUpdate(ctx, *opts.(*arvados.UpdateOptions))
585                         },
586                 },
587                 {
588                         arvados.EndpointUserList,
589                         func() interface{} { return &arvados.ListOptions{Limit: -1} },
590                         func(ctx context.Context, opts interface{}) (interface{}, error) {
591                                 return rtr.backend.UserList(ctx, *opts.(*arvados.ListOptions))
592                         },
593                 },
594                 {
595                         arvados.EndpointUserBatchUpdate,
596                         func() interface{} { return &arvados.UserBatchUpdateOptions{} },
597                         func(ctx context.Context, opts interface{}) (interface{}, error) {
598                                 return rtr.backend.UserBatchUpdate(ctx, *opts.(*arvados.UserBatchUpdateOptions))
599                         },
600                 },
601                 {
602                         arvados.EndpointUserDelete,
603                         func() interface{} { return &arvados.DeleteOptions{} },
604                         func(ctx context.Context, opts interface{}) (interface{}, error) {
605                                 return rtr.backend.UserDelete(ctx, *opts.(*arvados.DeleteOptions))
606                         },
607                 },
608                 {
609                         arvados.EndpointUserAuthenticate,
610                         func() interface{} { return &arvados.UserAuthenticateOptions{} },
611                         func(ctx context.Context, opts interface{}) (interface{}, error) {
612                                 return rtr.backend.UserAuthenticate(ctx, *opts.(*arvados.UserAuthenticateOptions))
613                         },
614                 },
615         } {
616                 exec := route.exec
617                 if rtr.config.WrapCalls != nil {
618                         exec = rtr.config.WrapCalls(exec)
619                 }
620                 rtr.addRoute(route.endpoint, route.defaultOpts, exec)
621         }
622         rtr.mux.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
623                 if req.Method == "OPTIONS" {
624                         // For non-webdav endpoints, return an empty
625                         // response with the CORS headers we already
626                         // added in ServeHTTP.
627                         w.WriteHeader(http.StatusOK)
628                         return
629                 }
630                 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusNotFound)
631         })
632         rtr.mux.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
633                 if req.Method == "OPTIONS" {
634                         // For non-webdav endpoints, return an empty
635                         // response with the CORS headers we already
636                         // added in ServeHTTP.
637                         w.WriteHeader(http.StatusOK)
638                         return
639                 }
640                 httpserver.Errors(w, []string{"API endpoint not found"}, http.StatusMethodNotAllowed)
641         })
642 }
643
644 var altMethod = map[string]string{
645         "PATCH": "PUT",  // Accept PUT as a synonym for PATCH
646         "GET":   "HEAD", // Accept HEAD at any GET route
647 }
648
649 func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() interface{}, exec api.RoutableFunc) {
650         methods := []string{endpoint.Method}
651         if alt, ok := altMethod[endpoint.Method]; ok {
652                 methods = append(methods, alt)
653         }
654         if strings.HasSuffix(endpoint.Path, ".*}") {
655                 // webdav methods
656                 methods = append(methods, "OPTIONS", "PROPFIND")
657         }
658         rtr.mux.Methods(methods...).Path("/" + endpoint.Path).HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
659                 logger := ctxlog.FromContext(req.Context())
660                 opts := defaultOpts()
661                 params, err := rtr.loadRequestParams(req, endpoint.AttrsKey, opts)
662                 if err != nil {
663                         logger.WithFields(logrus.Fields{
664                                 "req":      req,
665                                 "method":   endpoint.Method,
666                                 "endpoint": endpoint,
667                         }).WithError(err).Debug("error loading request params")
668                         rtr.sendError(w, err)
669                         return
670                 }
671                 respOpts, err := rtr.responseOptions(opts)
672                 if err != nil {
673                         logger.WithField("opts", opts).WithError(err).Debugf("error getting response options from %T", opts)
674                         rtr.sendError(w, err)
675                         return
676                 }
677
678                 creds := auth.CredentialsFromRequest(req)
679                 err = creds.LoadTokensFromHTTPRequestBody(req)
680                 if err != nil {
681                         rtr.sendError(w, fmt.Errorf("error loading tokens from request body: %s", err))
682                         return
683                 }
684                 if rt, _ := params["reader_tokens"].([]interface{}); len(rt) > 0 {
685                         for _, t := range rt {
686                                 if t, ok := t.(string); ok {
687                                         creds.Tokens = append(creds.Tokens, t)
688                                 }
689                         }
690                 }
691                 ctx := auth.NewContext(req.Context(), creds)
692                 ctx = arvados.ContextWithRequestID(ctx, req.Header.Get("X-Request-Id"))
693                 req = req.WithContext(ctx)
694
695                 // Extract the token UUIDs (or a placeholder for v1 tokens)
696                 var tokenUUIDs []string
697                 for _, t := range creds.Tokens {
698                         if strings.HasPrefix(t, "v2/") {
699                                 tokenParts := strings.Split(t, "/")
700                                 if len(tokenParts) >= 3 {
701                                         tokenUUIDs = append(tokenUUIDs, tokenParts[1])
702                                 }
703                         } else {
704                                 end := t
705                                 if len(t) > 5 {
706                                         end = t[len(t)-5:]
707                                 }
708                                 tokenUUIDs = append(tokenUUIDs, "v1 token ending in "+end)
709                         }
710                 }
711                 httpserver.SetResponseLogFields(ctx, logrus.Fields{"tokenUUIDs": tokenUUIDs})
712
713                 logger.WithFields(logrus.Fields{
714                         "apiEndpoint": endpoint,
715                         "apiOptsType": fmt.Sprintf("%T", opts),
716                         "apiOpts":     opts,
717                 }).Debug("exec")
718                 resp, err := exec(ctx, opts)
719                 if err != nil {
720                         logger.WithError(err).Debugf("returning error type %T", err)
721                         rtr.sendError(w, err)
722                         return
723                 }
724                 rtr.sendResponse(w, req, resp, respOpts)
725         })
726 }
727
728 func (rtr *router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
729         switch strings.SplitN(strings.TrimLeft(r.URL.Path, "/"), "/", 2)[0] {
730         case "login", "logout", "auth":
731         default:
732                 w.Header().Set("Access-Control-Allow-Origin", "*")
733                 w.Header().Set("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, PROPFIND, PUT, POST, PATCH, DELETE")
734                 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Range, X-Http-Method-Override")
735                 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
736                 w.Header().Set("Access-Control-Max-Age", "86486400")
737         }
738         if r.Body != nil {
739                 // Wrap r.Body in a http.MaxBytesReader(), otherwise
740                 // r.ParseForm() uses a default max request body size
741                 // of 10 megabytes. Note we rely on the Nginx
742                 // configuration to enforce the real max body size.
743                 max := int64(rtr.config.MaxRequestSize)
744                 if max < 1 {
745                         max = math.MaxInt64 - 1
746                 }
747                 r.Body = http.MaxBytesReader(w, r.Body, max)
748         }
749         if r.Method == "POST" {
750                 err := r.ParseForm()
751                 if err != nil {
752                         if err.Error() == "http: request body too large" {
753                                 err = httpError(http.StatusRequestEntityTooLarge, err)
754                         }
755                         rtr.sendError(w, err)
756                         return
757                 }
758                 if m := r.FormValue("_method"); m != "" {
759                         r2 := *r
760                         r = &r2
761                         r.Method = m
762                 } else if m = r.Header.Get("X-Http-Method-Override"); m != "" {
763                         r2 := *r
764                         r = &r2
765                         r.Method = m
766                 }
767         }
768         rtr.mux.ServeHTTP(w, r)
769 }