17014: Port container request federation code to new style (WIP).
[arvados.git] / lib / controller / federation / conn.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package federation
6
7 import (
8         "bytes"
9         "context"
10         "encoding/json"
11         "errors"
12         "fmt"
13         "net/http"
14         "net/url"
15         "regexp"
16         "strings"
17         "time"
18
19         "git.arvados.org/arvados.git/lib/config"
20         "git.arvados.org/arvados.git/lib/controller/localdb"
21         "git.arvados.org/arvados.git/lib/controller/rpc"
22         "git.arvados.org/arvados.git/sdk/go/arvados"
23         "git.arvados.org/arvados.git/sdk/go/auth"
24         "git.arvados.org/arvados.git/sdk/go/ctxlog"
25 )
26
27 type Conn struct {
28         cluster *arvados.Cluster
29         local   *localdb.Conn
30         remotes map[string]backend
31 }
32
33 func New(cluster *arvados.Cluster) *Conn {
34         local := localdb.NewConn(cluster)
35         remotes := map[string]backend{}
36         for id, remote := range cluster.RemoteClusters {
37                 if !remote.Proxy || id == cluster.ClusterID {
38                         continue
39                 }
40                 conn := rpc.NewConn(id, &url.URL{Scheme: remote.Scheme, Host: remote.Host}, remote.Insecure, saltedTokenProvider(local, id))
41                 // Older versions of controller rely on the Via header
42                 // to detect loops.
43                 conn.SendHeader = http.Header{"Via": {"HTTP/1.1 arvados-controller"}}
44                 remotes[id] = conn
45         }
46
47         return &Conn{
48                 cluster: cluster,
49                 local:   local,
50                 remotes: remotes,
51         }
52 }
53
54 // Return a new rpc.TokenProvider that takes the client-provided
55 // tokens from an incoming request context, determines whether they
56 // should (and can) be salted for the given remoteID, and returns the
57 // resulting tokens.
58 func saltedTokenProvider(local backend, remoteID string) rpc.TokenProvider {
59         return func(ctx context.Context) ([]string, error) {
60                 var tokens []string
61                 incoming, ok := auth.FromContext(ctx)
62                 if !ok {
63                         return nil, errors.New("no token provided")
64                 }
65                 for _, token := range incoming.Tokens {
66                         salted, err := auth.SaltToken(token, remoteID)
67                         switch err {
68                         case nil:
69                                 tokens = append(tokens, salted)
70                         case auth.ErrSalted:
71                                 tokens = append(tokens, token)
72                         case auth.ErrObsoleteToken:
73                                 ctx := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{token}})
74                                 aca, err := local.APIClientAuthorizationCurrent(ctx, arvados.GetOptions{})
75                                 if errStatus(err) == http.StatusUnauthorized {
76                                         // pass through unmodified
77                                         tokens = append(tokens, token)
78                                         continue
79                                 } else if err != nil {
80                                         return nil, err
81                                 }
82                                 salted, err := auth.SaltToken(aca.TokenV2(), remoteID)
83                                 if err != nil {
84                                         return nil, err
85                                 }
86                                 tokens = append(tokens, salted)
87                         default:
88                                 return nil, err
89                         }
90                 }
91                 return tokens, nil
92         }
93 }
94
95 // Return suitable backend for a query about the given cluster ID
96 // ("aaaaa") or object UUID ("aaaaa-dz642-abcdefghijklmno").
97 func (conn *Conn) chooseBackend(id string) backend {
98         if len(id) == 27 {
99                 id = id[:5]
100         } else if len(id) != 5 {
101                 // PDH or bogus ID
102                 return conn.local
103         }
104         if id == conn.cluster.ClusterID {
105                 return conn.local
106         } else if be, ok := conn.remotes[id]; ok {
107                 return be
108         } else {
109                 // TODO: return an "always error" backend?
110                 return conn.local
111         }
112 }
113
114 func (conn *Conn) localOrLoginCluster() backend {
115         if conn.cluster.Login.LoginCluster != "" {
116                 return conn.chooseBackend(conn.cluster.Login.LoginCluster)
117         }
118         return conn.local
119 }
120
121 // Call fn with the local backend; then, if fn returned 404, call fn
122 // on the available remote backends (possibly concurrently) until one
123 // succeeds.
124 //
125 // The second argument to fn is the cluster ID of the remote backend,
126 // or "" for the local backend.
127 //
128 // A non-nil error means all backends failed.
129 func (conn *Conn) tryLocalThenRemotes(ctx context.Context, forwardedFor string, fn func(context.Context, string, backend) error) error {
130         if err := fn(ctx, "", conn.local); err == nil || errStatus(err) != http.StatusNotFound || forwardedFor != "" {
131                 // Note: forwardedFor != "" means this request came
132                 // from a remote cluster, so we don't take a second
133                 // hop. This avoids cycles, redundant calls to a
134                 // mutually reachable remote, and use of double-salted
135                 // tokens.
136                 return err
137         }
138
139         ctx, cancel := context.WithCancel(ctx)
140         defer cancel()
141         errchan := make(chan error, len(conn.remotes))
142         for remoteID, be := range conn.remotes {
143                 remoteID, be := remoteID, be
144                 go func() {
145                         errchan <- fn(ctx, remoteID, be)
146                 }()
147         }
148         all404 := true
149         var errs []error
150         for i := 0; i < cap(errchan); i++ {
151                 err := <-errchan
152                 if err == nil {
153                         return nil
154                 }
155                 all404 = all404 && errStatus(err) == http.StatusNotFound
156                 errs = append(errs, err)
157         }
158         if all404 {
159                 return notFoundError{}
160         }
161         return httpErrorf(http.StatusBadGateway, "errors: %v", errs)
162 }
163
164 func (conn *Conn) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
165         return conn.chooseBackend(options.ClusterID).CollectionCreate(ctx, options)
166 }
167
168 func (conn *Conn) CollectionUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Collection, error) {
169         return conn.chooseBackend(options.UUID).CollectionUpdate(ctx, options)
170 }
171
172 func rewriteManifest(mt, remoteID string) string {
173         return regexp.MustCompile(` [0-9a-f]{32}\+[^ ]*`).ReplaceAllStringFunc(mt, func(tok string) string {
174                 return strings.Replace(tok, "+A", "+R"+remoteID+"-", -1)
175         })
176 }
177
178 func (conn *Conn) ConfigGet(ctx context.Context) (json.RawMessage, error) {
179         var buf bytes.Buffer
180         err := config.ExportJSON(&buf, conn.cluster)
181         return json.RawMessage(buf.Bytes()), err
182 }
183
184 func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arvados.LoginResponse, error) {
185         if id := conn.cluster.Login.LoginCluster; id != "" && id != conn.cluster.ClusterID {
186                 // defer entire login procedure to designated cluster
187                 remote, ok := conn.remotes[id]
188                 if !ok {
189                         return arvados.LoginResponse{}, fmt.Errorf("configuration problem: designated login cluster %q is not defined", id)
190                 }
191                 baseURL := remote.BaseURL()
192                 target, err := baseURL.Parse(arvados.EndpointLogin.Path)
193                 if err != nil {
194                         return arvados.LoginResponse{}, fmt.Errorf("internal error getting redirect target: %s", err)
195                 }
196                 params := url.Values{
197                         "return_to": []string{options.ReturnTo},
198                 }
199                 if options.Remote != "" {
200                         params.Set("remote", options.Remote)
201                 }
202                 target.RawQuery = params.Encode()
203                 return arvados.LoginResponse{
204                         RedirectLocation: target.String(),
205                 }, nil
206         }
207         return conn.local.Login(ctx, options)
208 }
209
210 func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
211         // If the logout request comes with an API token from a known
212         // remote cluster, redirect to that cluster's logout handler
213         // so it has an opportunity to clear sessions, expire tokens,
214         // etc. Otherwise use the local endpoint.
215         reqauth, ok := auth.FromContext(ctx)
216         if !ok || len(reqauth.Tokens) == 0 || len(reqauth.Tokens[0]) < 8 || !strings.HasPrefix(reqauth.Tokens[0], "v2/") {
217                 return conn.local.Logout(ctx, options)
218         }
219         id := reqauth.Tokens[0][3:8]
220         if id == conn.cluster.ClusterID {
221                 return conn.local.Logout(ctx, options)
222         }
223         remote, ok := conn.remotes[id]
224         if !ok {
225                 return conn.local.Logout(ctx, options)
226         }
227         baseURL := remote.BaseURL()
228         target, err := baseURL.Parse(arvados.EndpointLogout.Path)
229         if err != nil {
230                 return arvados.LogoutResponse{}, fmt.Errorf("internal error getting redirect target: %s", err)
231         }
232         target.RawQuery = url.Values{"return_to": {options.ReturnTo}}.Encode()
233         return arvados.LogoutResponse{RedirectLocation: target.String()}, nil
234 }
235
236 func (conn *Conn) CollectionGet(ctx context.Context, options arvados.GetOptions) (arvados.Collection, error) {
237         if len(options.UUID) == 27 {
238                 // UUID is really a UUID
239                 c, err := conn.chooseBackend(options.UUID).CollectionGet(ctx, options)
240                 if err == nil && options.UUID[:5] != conn.cluster.ClusterID {
241                         c.ManifestText = rewriteManifest(c.ManifestText, options.UUID[:5])
242                 }
243                 return c, err
244         }
245         // UUID is a PDH
246         first := make(chan arvados.Collection, 1)
247         err := conn.tryLocalThenRemotes(ctx, options.ForwardedFor, func(ctx context.Context, remoteID string, be backend) error {
248                 remoteOpts := options
249                 remoteOpts.ForwardedFor = conn.cluster.ClusterID + "-" + options.ForwardedFor
250                 c, err := be.CollectionGet(ctx, remoteOpts)
251                 if err != nil {
252                         return err
253                 }
254                 // options.UUID is either hash+size or
255                 // hash+size+hints; only hash+size need to
256                 // match the computed PDH.
257                 if pdh := arvados.PortableDataHash(c.ManifestText); pdh != options.UUID && !strings.HasPrefix(options.UUID, pdh+"+") {
258                         err = httpErrorf(http.StatusBadGateway, "bad portable data hash %q received from remote %q (expected %q)", pdh, remoteID, options.UUID)
259                         ctxlog.FromContext(ctx).Warn(err)
260                         return err
261                 }
262                 if remoteID != "" {
263                         c.ManifestText = rewriteManifest(c.ManifestText, remoteID)
264                 }
265                 select {
266                 case first <- c:
267                         return nil
268                 default:
269                         // lost race, return value doesn't matter
270                         return nil
271                 }
272         })
273         if err != nil {
274                 return arvados.Collection{}, err
275         }
276         return <-first, nil
277 }
278
279 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
280         return conn.generated_CollectionList(ctx, options)
281 }
282
283 func (conn *Conn) CollectionProvenance(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
284         return conn.chooseBackend(options.UUID).CollectionProvenance(ctx, options)
285 }
286
287 func (conn *Conn) CollectionUsedBy(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
288         return conn.chooseBackend(options.UUID).CollectionUsedBy(ctx, options)
289 }
290
291 func (conn *Conn) CollectionDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
292         return conn.chooseBackend(options.UUID).CollectionDelete(ctx, options)
293 }
294
295 func (conn *Conn) CollectionTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
296         return conn.chooseBackend(options.UUID).CollectionTrash(ctx, options)
297 }
298
299 func (conn *Conn) CollectionUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Collection, error) {
300         return conn.chooseBackend(options.UUID).CollectionUntrash(ctx, options)
301 }
302
303 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
304         return conn.generated_ContainerList(ctx, options)
305 }
306
307 func (conn *Conn) ContainerCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Container, error) {
308         return conn.chooseBackend(options.ClusterID).ContainerCreate(ctx, options)
309 }
310
311 func (conn *Conn) ContainerUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
312         return conn.chooseBackend(options.UUID).ContainerUpdate(ctx, options)
313 }
314
315 func (conn *Conn) ContainerGet(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
316         return conn.chooseBackend(options.UUID).ContainerGet(ctx, options)
317 }
318
319 func (conn *Conn) ContainerDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Container, error) {
320         return conn.chooseBackend(options.UUID).ContainerDelete(ctx, options)
321 }
322
323 func (conn *Conn) ContainerLock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
324         return conn.chooseBackend(options.UUID).ContainerLock(ctx, options)
325 }
326
327 func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
328         return conn.chooseBackend(options.UUID).ContainerUnlock(ctx, options)
329 }
330
331 func (conn *Conn) ContainerRequestList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerRequestList, error) {
332         return conn.generated_ContainerRequestList(ctx, options)
333 }
334
335 func (conn *Conn) ContainerRequestCreate(ctx context.Context, options arvados.CreateOptions) (arvados.ContainerRequest, error) {
336         be := conn.chooseBackend(options.ClusterID)
337         if be == conn.local {
338                 return be.ContainerRequestCreate(ctx, options)
339         }
340         if _, ok := options.Attrs["runtime_token"]; !ok {
341                 // If runtime_token is not set, create a new token
342                 aca, err := conn.local.APIClientAuthorizationCurrent(ctx, arvados.GetOptions{})
343                 if err != nil {
344                         // This should probably be StatusUnauthorized
345                         // (need to update test in
346                         // lib/controller/federation_test.go):
347                         return arvados.ContainerRequest{}, httpErrorf(http.StatusForbidden, "%w", err)
348                 }
349                 user, err := conn.local.UserGetCurrent(ctx, arvados.GetOptions{})
350                 if err != nil {
351                         return arvados.ContainerRequest{}, err
352                 }
353                 if len(aca.Scopes) != 0 || aca.Scopes[0] != "all" {
354                         return arvados.ContainerRequest{}, httpErrorf(http.StatusForbidden, "token scope is not [all]")
355                 }
356                 if strings.HasPrefix(aca.UUID, conn.cluster.ClusterID) {
357                         // Local user, submitting to a remote cluster.
358                         // Create a new (FIXME: needs to be
359                         // time-limited!) token.
360                         aca, err = localdb.CreateAPIClientAuthorization(ctx, conn.local, conn.cluster.SystemRootToken, rpc.UserSessionAuthInfo{UserUUID: user.UUID})
361                         if err != nil {
362                                 return arvados.ContainerRequest{}, err
363                         }
364                         options.Attrs["runtime_token"] = aca.TokenV2()
365                 } else {
366                         // Remote user. Container request will use the
367                         // current token, minus the trailing portion
368                         // (optional container uuid).
369                         options.Attrs["runtime_token"] = aca.TokenV2()
370                 }
371         }
372         return be.ContainerRequestCreate(ctx, options)
373 }
374
375 func (conn *Conn) ContainerRequestUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.ContainerRequest, error) {
376         return conn.chooseBackend(options.UUID).ContainerRequestUpdate(ctx, options)
377 }
378
379 func (conn *Conn) ContainerRequestGet(ctx context.Context, options arvados.GetOptions) (arvados.ContainerRequest, error) {
380         return conn.chooseBackend(options.UUID).ContainerRequestGet(ctx, options)
381 }
382
383 func (conn *Conn) ContainerRequestDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.ContainerRequest, error) {
384         return conn.chooseBackend(options.UUID).ContainerRequestDelete(ctx, options)
385 }
386
387 func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions) (arvados.SpecimenList, error) {
388         return conn.generated_SpecimenList(ctx, options)
389 }
390
391 func (conn *Conn) SpecimenCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Specimen, error) {
392         return conn.chooseBackend(options.ClusterID).SpecimenCreate(ctx, options)
393 }
394
395 func (conn *Conn) SpecimenUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Specimen, error) {
396         return conn.chooseBackend(options.UUID).SpecimenUpdate(ctx, options)
397 }
398
399 func (conn *Conn) SpecimenGet(ctx context.Context, options arvados.GetOptions) (arvados.Specimen, error) {
400         return conn.chooseBackend(options.UUID).SpecimenGet(ctx, options)
401 }
402
403 func (conn *Conn) SpecimenDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Specimen, error) {
404         return conn.chooseBackend(options.UUID).SpecimenDelete(ctx, options)
405 }
406
407 var userAttrsCachedFromLoginCluster = map[string]bool{
408         "created_at":  true,
409         "email":       true,
410         "first_name":  true,
411         "is_active":   true,
412         "is_admin":    true,
413         "last_name":   true,
414         "modified_at": true,
415         "prefs":       true,
416         "username":    true,
417
418         "etag":                    false,
419         "full_name":               false,
420         "identity_url":            false,
421         "is_invited":              false,
422         "modified_by_client_uuid": false,
423         "modified_by_user_uuid":   false,
424         "owner_uuid":              false,
425         "uuid":                    false,
426         "writable_by":             false,
427 }
428
429 func (conn *Conn) batchUpdateUsers(ctx context.Context,
430         options arvados.ListOptions,
431         items []arvados.User) (err error) {
432
433         id := conn.cluster.Login.LoginCluster
434         logger := ctxlog.FromContext(ctx)
435         batchOpts := arvados.UserBatchUpdateOptions{Updates: map[string]map[string]interface{}{}}
436         for _, user := range items {
437                 if !strings.HasPrefix(user.UUID, id) {
438                         continue
439                 }
440                 logger.Debugf("cache user info for uuid %q", user.UUID)
441
442                 // If the remote cluster has null timestamps
443                 // (e.g., test server with incomplete
444                 // fixtures) use dummy timestamps (instead of
445                 // the zero time, which causes a Rails API
446                 // error "year too big to marshal: 1 UTC").
447                 if user.ModifiedAt.IsZero() {
448                         user.ModifiedAt = time.Now()
449                 }
450                 if user.CreatedAt.IsZero() {
451                         user.CreatedAt = time.Now()
452                 }
453
454                 var allFields map[string]interface{}
455                 buf, err := json.Marshal(user)
456                 if err != nil {
457                         return fmt.Errorf("error encoding user record from remote response: %s", err)
458                 }
459                 err = json.Unmarshal(buf, &allFields)
460                 if err != nil {
461                         return fmt.Errorf("error transcoding user record from remote response: %s", err)
462                 }
463                 updates := allFields
464                 if len(options.Select) > 0 {
465                         updates = map[string]interface{}{}
466                         for _, k := range options.Select {
467                                 if v, ok := allFields[k]; ok && userAttrsCachedFromLoginCluster[k] {
468                                         updates[k] = v
469                                 }
470                         }
471                 } else {
472                         for k := range updates {
473                                 if !userAttrsCachedFromLoginCluster[k] {
474                                         delete(updates, k)
475                                 }
476                         }
477                 }
478                 batchOpts.Updates[user.UUID] = updates
479         }
480         if len(batchOpts.Updates) > 0 {
481                 ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{conn.cluster.SystemRootToken}})
482                 _, err = conn.local.UserBatchUpdate(ctxRoot, batchOpts)
483                 if err != nil {
484                         return fmt.Errorf("error updating local user records: %s", err)
485                 }
486         }
487         return nil
488 }
489
490 func (conn *Conn) UserList(ctx context.Context, options arvados.ListOptions) (arvados.UserList, error) {
491         if id := conn.cluster.Login.LoginCluster; id != "" && id != conn.cluster.ClusterID && !options.BypassFederation {
492                 resp, err := conn.chooseBackend(id).UserList(ctx, options)
493                 if err != nil {
494                         return resp, err
495                 }
496                 err = conn.batchUpdateUsers(ctx, options, resp.Items)
497                 if err != nil {
498                         return arvados.UserList{}, err
499                 }
500                 return resp, nil
501         }
502         return conn.generated_UserList(ctx, options)
503 }
504
505 func (conn *Conn) UserCreate(ctx context.Context, options arvados.CreateOptions) (arvados.User, error) {
506         return conn.chooseBackend(options.ClusterID).UserCreate(ctx, options)
507 }
508
509 func (conn *Conn) UserUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.User, error) {
510         if options.BypassFederation {
511                 return conn.local.UserUpdate(ctx, options)
512         }
513         return conn.chooseBackend(options.UUID).UserUpdate(ctx, options)
514 }
515
516 func (conn *Conn) UserUpdateUUID(ctx context.Context, options arvados.UpdateUUIDOptions) (arvados.User, error) {
517         return conn.local.UserUpdateUUID(ctx, options)
518 }
519
520 func (conn *Conn) UserMerge(ctx context.Context, options arvados.UserMergeOptions) (arvados.User, error) {
521         return conn.local.UserMerge(ctx, options)
522 }
523
524 func (conn *Conn) UserActivate(ctx context.Context, options arvados.UserActivateOptions) (arvados.User, error) {
525         return conn.localOrLoginCluster().UserActivate(ctx, options)
526 }
527
528 func (conn *Conn) UserSetup(ctx context.Context, options arvados.UserSetupOptions) (map[string]interface{}, error) {
529         upstream := conn.localOrLoginCluster()
530         if upstream != conn.local {
531                 // When LoginCluster is in effect, and we're setting
532                 // up a remote user, and we want to give that user
533                 // access to a local VM, we can't include the VM in
534                 // the setup call, because the remote cluster won't
535                 // recognize it.
536
537                 // Similarly, if we want to create a git repo,
538                 // it should be created on the local cluster,
539                 // not the remote one.
540
541                 upstreamOptions := options
542                 upstreamOptions.VMUUID = ""
543                 upstreamOptions.RepoName = ""
544
545                 ret, err := upstream.UserSetup(ctx, upstreamOptions)
546                 if err != nil {
547                         return ret, err
548                 }
549         }
550
551         return conn.local.UserSetup(ctx, options)
552 }
553
554 func (conn *Conn) UserUnsetup(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
555         return conn.localOrLoginCluster().UserUnsetup(ctx, options)
556 }
557
558 func (conn *Conn) UserGet(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
559         resp, err := conn.chooseBackend(options.UUID).UserGet(ctx, options)
560         if err != nil {
561                 return resp, err
562         }
563         if options.UUID != resp.UUID {
564                 return arvados.User{}, httpErrorf(http.StatusBadGateway, "Had requested %v but response was for %v", options.UUID, resp.UUID)
565         }
566         if options.UUID[:5] != conn.cluster.ClusterID {
567                 err = conn.batchUpdateUsers(ctx, arvados.ListOptions{Select: options.Select}, []arvados.User{resp})
568                 if err != nil {
569                         return arvados.User{}, err
570                 }
571         }
572         return resp, nil
573 }
574
575 func (conn *Conn) UserGetCurrent(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
576         return conn.local.UserGetCurrent(ctx, options)
577 }
578
579 func (conn *Conn) UserGetSystem(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
580         return conn.chooseBackend(options.UUID).UserGetSystem(ctx, options)
581 }
582
583 func (conn *Conn) UserDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.User, error) {
584         return conn.chooseBackend(options.UUID).UserDelete(ctx, options)
585 }
586
587 func (conn *Conn) UserBatchUpdate(ctx context.Context, options arvados.UserBatchUpdateOptions) (arvados.UserList, error) {
588         return conn.local.UserBatchUpdate(ctx, options)
589 }
590
591 func (conn *Conn) UserAuthenticate(ctx context.Context, options arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
592         return conn.local.UserAuthenticate(ctx, options)
593 }
594
595 func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
596         return conn.chooseBackend(options.UUID).APIClientAuthorizationCurrent(ctx, options)
597 }
598
599 type backend interface {
600         arvados.API
601         BaseURL() url.URL
602 }
603
604 type notFoundError struct{}
605
606 func (notFoundError) HTTPStatus() int { return http.StatusNotFound }
607 func (notFoundError) Error() string   { return "not found" }
608
609 func errStatus(err error) int {
610         if httpErr, ok := err.(interface{ HTTPStatus() int }); ok {
611                 return httpErr.HTTPStatus()
612         }
613         return http.StatusInternalServerError
614 }