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