1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.arvados.org/arvados.git/lib/config"
21 "git.arvados.org/arvados.git/lib/controller/localdb"
22 "git.arvados.org/arvados.git/lib/controller/rpc"
23 "git.arvados.org/arvados.git/sdk/go/arvados"
24 "git.arvados.org/arvados.git/sdk/go/auth"
25 "git.arvados.org/arvados.git/sdk/go/ctxlog"
26 "git.arvados.org/arvados.git/sdk/go/health"
27 "github.com/jmoiron/sqlx"
32 cluster *arvados.Cluster
34 remotes map[string]backend
37 func New(bgCtx context.Context, cluster *arvados.Cluster, healthFuncs *map[string]health.Func, getdb func(context.Context) (*sqlx.DB, error)) *Conn {
38 local := localdb.NewConn(bgCtx, cluster, getdb)
39 remotes := map[string]backend{}
40 for id, remote := range cluster.RemoteClusters {
41 if !remote.Proxy || id == cluster.ClusterID {
44 conn := rpc.NewConn(id, &url.URL{Scheme: remote.Scheme, Host: remote.Host}, remote.Insecure, saltedTokenProvider(cluster, local, id))
45 // Older versions of controller rely on the Via header
47 conn.SendHeader = http.Header{"Via": {"HTTP/1.1 arvados-controller"}}
51 if healthFuncs != nil {
52 hf := map[string]health.Func{"vocabulary": local.LastVocabularyError}
64 // Return a new rpc.TokenProvider that takes the client-provided
65 // tokens from an incoming request context, determines whether they
66 // should (and can) be salted for the given remoteID, and returns the
68 func saltedTokenProvider(cluster *arvados.Cluster, local backend, remoteID string) rpc.TokenProvider {
69 return func(ctx context.Context) ([]string, error) {
71 incoming, ok := auth.FromContext(ctx)
73 return nil, errors.New("no token provided")
75 for _, token := range incoming.Tokens {
76 if strings.HasPrefix(token, "v2/"+cluster.ClusterID+"-") &&
77 !strings.HasPrefix(token, "v2/"+cluster.ClusterID+"-gj3su-anonymouspublic/") &&
78 remoteID == cluster.Login.LoginCluster {
79 // If we did this, the login cluster would call back to us and then
80 // reject our response because the user UUID prefix (i.e., the
81 // LoginCluster prefix) won't match the token UUID prefix (i.e., our
82 // prefix). The anonymous token is OK to forward, because (unlike other
83 // local tokens for real users) the validation callback will return the
84 // locally issued anonymous user ID instead of a login-cluster user ID.
85 // That anonymous user ID gets mapped to the local anonymous user
86 // automatically on the login cluster.
87 return nil, httpErrorf(http.StatusUnauthorized, "cannot use a locally issued token to forward a request to our login cluster (%s)", remoteID)
89 salted, err := auth.SaltToken(token, remoteID)
92 tokens = append(tokens, salted)
94 tokens = append(tokens, token)
95 case auth.ErrTokenFormat:
96 // pass through unmodified (assume it's an OIDC access token)
97 tokens = append(tokens, token)
98 case auth.ErrObsoleteToken:
99 ctx := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{token}})
100 aca, err := local.APIClientAuthorizationCurrent(ctx, arvados.GetOptions{})
101 if errStatus(err) == http.StatusUnauthorized {
102 // pass through unmodified
103 tokens = append(tokens, token)
105 } else if err != nil {
108 if strings.HasPrefix(aca.UUID, remoteID) {
109 // We have it cached here, but
110 // the token belongs to the
111 // remote target itself, so
112 // pass it through unmodified.
113 tokens = append(tokens, token)
116 salted, err := auth.SaltToken(aca.TokenV2(), remoteID)
120 tokens = append(tokens, salted)
129 // Return suitable backend for a query about the given cluster ID
130 // ("aaaaa") or object UUID ("aaaaa-dz642-abcdefghijklmno").
131 func (conn *Conn) chooseBackend(id string) backend {
134 } else if len(id) != 5 {
138 if id == conn.cluster.ClusterID {
140 } else if be, ok := conn.remotes[id]; ok {
143 // TODO: return an "always error" backend?
148 func (conn *Conn) localOrLoginCluster() backend {
149 if conn.cluster.Login.LoginCluster != "" {
150 return conn.chooseBackend(conn.cluster.Login.LoginCluster)
155 // Call fn with the local backend; then, if fn returned 404, call fn
156 // on the available remote backends (possibly concurrently) until one
159 // The second argument to fn is the cluster ID of the remote backend,
160 // or "" for the local backend.
162 // A non-nil error means all backends failed.
163 func (conn *Conn) tryLocalThenRemotes(ctx context.Context, forwardedFor string, fn func(context.Context, string, backend) error) error {
164 if err := fn(ctx, "", conn.local); err == nil || errStatus(err) != http.StatusNotFound || forwardedFor != "" {
165 // Note: forwardedFor != "" means this request came
166 // from a remote cluster, so we don't take a second
167 // hop. This avoids cycles, redundant calls to a
168 // mutually reachable remote, and use of double-salted
173 ctx, cancel := context.WithCancel(ctx)
175 errchan := make(chan error, len(conn.remotes))
176 for remoteID, be := range conn.remotes {
177 remoteID, be := remoteID, be
179 errchan <- fn(ctx, remoteID, be)
182 returncode := http.StatusNotFound
184 for i := 0; i < cap(errchan); i++ {
189 errs = append(errs, err)
190 if code := errStatus(err); code >= 500 || code == http.StatusTooManyRequests {
191 // If any of the remotes have a retryable
192 // error (and none succeed) we'll return 502.
193 returncode = http.StatusBadGateway
194 } else if code != http.StatusNotFound && returncode != http.StatusBadGateway {
195 // If some of the remotes have non-retryable
196 // non-404 errors (and none succeed or have
197 // retryable errors) we'll return 422.
198 returncode = http.StatusUnprocessableEntity
201 if returncode == http.StatusNotFound {
202 return notFoundError{}
204 return httpErrorf(returncode, "errors: %v", errs)
207 func (conn *Conn) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
208 return conn.chooseBackend(options.ClusterID).CollectionCreate(ctx, options)
211 func (conn *Conn) CollectionUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Collection, error) {
212 return conn.chooseBackend(options.UUID).CollectionUpdate(ctx, options)
215 func rewriteManifest(mt, remoteID string) string {
216 return regexp.MustCompile(` [0-9a-f]{32}\+[^ ]*`).ReplaceAllStringFunc(mt, func(tok string) string {
217 return strings.Replace(tok, "+A", "+R"+remoteID+"-", -1)
221 func (conn *Conn) ConfigGet(ctx context.Context) (json.RawMessage, error) {
223 err := config.ExportJSON(&buf, conn.cluster)
224 return json.RawMessage(buf.Bytes()), err
227 func (conn *Conn) VocabularyGet(ctx context.Context) (arvados.Vocabulary, error) {
228 return conn.local.VocabularyGet(ctx)
231 func (conn *Conn) DiscoveryDocument(ctx context.Context) (arvados.DiscoveryDocument, error) {
232 return conn.local.DiscoveryDocument(ctx)
235 func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arvados.LoginResponse, error) {
236 if id := conn.cluster.Login.LoginCluster; id != "" && id != conn.cluster.ClusterID {
237 // defer entire login procedure to designated cluster
238 remote, ok := conn.remotes[id]
240 return arvados.LoginResponse{}, fmt.Errorf("configuration problem: designated login cluster %q is not defined", id)
242 baseURL := remote.BaseURL()
243 target, err := baseURL.Parse(arvados.EndpointLogin.Path)
245 return arvados.LoginResponse{}, fmt.Errorf("internal error getting redirect target: %s", err)
247 params := url.Values{
248 "return_to": []string{options.ReturnTo},
250 if options.Remote != "" {
251 params.Set("remote", options.Remote)
253 target.RawQuery = params.Encode()
254 return arvados.LoginResponse{
255 RedirectLocation: target.String(),
258 return conn.local.Login(ctx, options)
261 var v2TokenRegexp = regexp.MustCompile(`^v2/[a-z0-9]{5}-gj3su-[a-z0-9]{15}/`)
263 func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
264 // If the token was issued by another cluster, we want to issue a logout
265 // request to the issuing instance to invalidate the token federation-wide.
266 // If this federation has a login cluster, that's always considered the
268 // Otherwise, if this is a v2 token, use the UUID to find the issuing
270 // Note that remoteBE may still be conn.local even *after* one of these
271 // conditions is true.
272 var remoteBE backend = conn.local
273 if conn.cluster.Login.LoginCluster != "" {
274 remoteBE = conn.chooseBackend(conn.cluster.Login.LoginCluster)
276 reqauth, ok := auth.FromContext(ctx)
277 if ok && len(reqauth.Tokens) > 0 && v2TokenRegexp.MatchString(reqauth.Tokens[0]) {
278 remoteBE = conn.chooseBackend(reqauth.Tokens[0][3:8])
282 // We always want to invalidate the token locally. Start that process.
283 var localResponse arvados.LogoutResponse
285 wg := sync.WaitGroup{}
288 localResponse, localErr = conn.local.Logout(ctx, options)
292 // If the token was issued by another cluster, log out there too.
293 if remoteBE != conn.local {
294 response, err := remoteBE.Logout(ctx, options)
295 // If the issuing cluster returns a redirect or error, that's more
296 // important to return to the user than anything that happens locally.
297 if response.RedirectLocation != "" || err != nil {
302 // Either the local cluster is the issuing cluster, or the issuing cluster's
303 // response was uninteresting.
305 return localResponse, localErr
308 func (conn *Conn) AuthorizedKeyCreate(ctx context.Context, options arvados.CreateOptions) (arvados.AuthorizedKey, error) {
309 return conn.chooseBackend(options.ClusterID).AuthorizedKeyCreate(ctx, options)
312 func (conn *Conn) AuthorizedKeyUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.AuthorizedKey, error) {
313 return conn.chooseBackend(options.UUID).AuthorizedKeyUpdate(ctx, options)
316 func (conn *Conn) AuthorizedKeyGet(ctx context.Context, options arvados.GetOptions) (arvados.AuthorizedKey, error) {
317 return conn.chooseBackend(options.UUID).AuthorizedKeyGet(ctx, options)
320 func (conn *Conn) AuthorizedKeyList(ctx context.Context, options arvados.ListOptions) (arvados.AuthorizedKeyList, error) {
321 return conn.generated_AuthorizedKeyList(ctx, options)
324 func (conn *Conn) AuthorizedKeyDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.AuthorizedKey, error) {
325 return conn.chooseBackend(options.UUID).AuthorizedKeyDelete(ctx, options)
328 func (conn *Conn) CollectionGet(ctx context.Context, options arvados.GetOptions) (arvados.Collection, error) {
329 if len(options.UUID) == 27 {
330 // UUID is really a UUID
331 c, err := conn.chooseBackend(options.UUID).CollectionGet(ctx, options)
332 if err == nil && options.UUID[:5] != conn.cluster.ClusterID {
333 c.ManifestText = rewriteManifest(c.ManifestText, options.UUID[:5])
337 if len(options.UUID) < 34 || options.UUID[32] != '+' {
338 return arvados.Collection{}, httpErrorf(http.StatusNotFound, "invalid UUID or PDH %q", options.UUID)
341 first := make(chan arvados.Collection, 1)
342 err := conn.tryLocalThenRemotes(ctx, options.ForwardedFor, func(ctx context.Context, remoteID string, be backend) error {
343 remoteOpts := options
344 remoteOpts.ForwardedFor = conn.cluster.ClusterID + "-" + options.ForwardedFor
345 c, err := be.CollectionGet(ctx, remoteOpts)
350 if options.Select != nil {
352 for _, s := range options.Select {
353 if s == "manifest_text" {
360 pdh := arvados.PortableDataHash(c.ManifestText)
361 // options.UUID is either hash+size or
362 // hash+size+hints; only hash+size need to
363 // match the computed PDH.
364 if pdh != options.UUID && !strings.HasPrefix(options.UUID, pdh+"+") {
365 err = httpErrorf(http.StatusBadGateway, "bad portable data hash %q received from remote %q (expected %q)", pdh, remoteID, options.UUID)
366 ctxlog.FromContext(ctx).Warn(err)
371 c.ManifestText = rewriteManifest(c.ManifestText, remoteID)
377 // lost race, return value doesn't matter
382 return arvados.Collection{}, err
387 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
388 return conn.generated_CollectionList(ctx, options)
391 func (conn *Conn) CollectionProvenance(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
392 return conn.chooseBackend(options.UUID).CollectionProvenance(ctx, options)
395 func (conn *Conn) CollectionUsedBy(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
396 return conn.chooseBackend(options.UUID).CollectionUsedBy(ctx, options)
399 func (conn *Conn) CollectionDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
400 return conn.chooseBackend(options.UUID).CollectionDelete(ctx, options)
403 func (conn *Conn) CollectionTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
404 return conn.chooseBackend(options.UUID).CollectionTrash(ctx, options)
407 func (conn *Conn) CollectionUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Collection, error) {
408 return conn.chooseBackend(options.UUID).CollectionUntrash(ctx, options)
411 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
412 return conn.generated_ContainerList(ctx, options)
415 func (conn *Conn) ContainerCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Container, error) {
416 return conn.chooseBackend(options.ClusterID).ContainerCreate(ctx, options)
419 func (conn *Conn) ContainerUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
420 return conn.chooseBackend(options.UUID).ContainerUpdate(ctx, options)
423 func (conn *Conn) ContainerPriorityUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
424 return conn.chooseBackend(options.UUID).ContainerPriorityUpdate(ctx, options)
427 func (conn *Conn) ContainerGet(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
428 return conn.chooseBackend(options.UUID).ContainerGet(ctx, options)
431 func (conn *Conn) ContainerDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Container, error) {
432 return conn.chooseBackend(options.UUID).ContainerDelete(ctx, options)
435 func (conn *Conn) ContainerLock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
436 return conn.chooseBackend(options.UUID).ContainerLock(ctx, options)
439 func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
440 return conn.chooseBackend(options.UUID).ContainerUnlock(ctx, options)
443 func (conn *Conn) ContainerSSH(ctx context.Context, options arvados.ContainerSSHOptions) (arvados.ConnectionResponse, error) {
444 return conn.chooseBackend(options.UUID).ContainerSSH(ctx, options)
447 func (conn *Conn) ContainerGatewayTunnel(ctx context.Context, options arvados.ContainerGatewayTunnelOptions) (arvados.ConnectionResponse, error) {
448 return conn.chooseBackend(options.UUID).ContainerGatewayTunnel(ctx, options)
451 func (conn *Conn) ContainerRequestList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerRequestList, error) {
452 return conn.generated_ContainerRequestList(ctx, options)
455 func (conn *Conn) ContainerRequestCreate(ctx context.Context, options arvados.CreateOptions) (arvados.ContainerRequest, error) {
456 be := conn.chooseBackend(options.ClusterID)
457 if be == conn.local {
458 return be.ContainerRequestCreate(ctx, options)
460 if _, ok := options.Attrs["runtime_token"]; !ok {
461 // If runtime_token is not set, create a new token
462 aca, err := conn.local.APIClientAuthorizationCurrent(ctx, arvados.GetOptions{})
464 // This should probably be StatusUnauthorized
465 // (need to update test in
466 // lib/controller/federation_test.go):
467 // When RoR is out of the picture this should be:
468 // return arvados.ContainerRequest{}, httpErrorf(http.StatusUnauthorized, "%w", err)
469 return arvados.ContainerRequest{}, httpErrorf(http.StatusForbidden, "%s", "invalid API token")
471 user, err := conn.local.UserGetCurrent(ctx, arvados.GetOptions{})
473 return arvados.ContainerRequest{}, err
475 if len(aca.Scopes) == 0 || aca.Scopes[0] != "all" {
476 return arvados.ContainerRequest{}, httpErrorf(http.StatusForbidden, "token scope is not [all]")
478 if strings.HasPrefix(aca.UUID, conn.cluster.ClusterID) {
479 // Local user, submitting to a remote cluster.
480 // Create a new time-limited token.
481 local, ok := conn.local.(*localdb.Conn)
483 return arvados.ContainerRequest{}, httpErrorf(http.StatusInternalServerError, "bug: local backend is a %T, not a *localdb.Conn", conn.local)
485 aca, err = local.CreateAPIClientAuthorization(ctx, conn.cluster.SystemRootToken, rpc.UserSessionAuthInfo{UserUUID: user.UUID,
486 ExpiresAt: time.Now().UTC().Add(conn.cluster.Collections.BlobSigningTTL.Duration())})
488 return arvados.ContainerRequest{}, err
490 options.Attrs["runtime_token"] = aca.TokenV2()
492 // Remote user. Container request will use the
493 // current token, minus the trailing portion
494 // (optional container uuid).
495 options.Attrs["runtime_token"] = aca.TokenV2()
498 return be.ContainerRequestCreate(ctx, options)
501 func (conn *Conn) ContainerRequestUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.ContainerRequest, error) {
502 return conn.chooseBackend(options.UUID).ContainerRequestUpdate(ctx, options)
505 func (conn *Conn) ContainerRequestGet(ctx context.Context, options arvados.GetOptions) (arvados.ContainerRequest, error) {
506 return conn.chooseBackend(options.UUID).ContainerRequestGet(ctx, options)
509 func (conn *Conn) ContainerRequestDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.ContainerRequest, error) {
510 return conn.chooseBackend(options.UUID).ContainerRequestDelete(ctx, options)
513 func (conn *Conn) ContainerRequestContainerStatus(ctx context.Context, options arvados.GetOptions) (arvados.ContainerStatus, error) {
514 return conn.chooseBackend(options.UUID).ContainerRequestContainerStatus(ctx, options)
517 func (conn *Conn) ContainerRequestLog(ctx context.Context, options arvados.ContainerLogOptions) (http.Handler, error) {
518 return conn.chooseBackend(options.UUID).ContainerRequestLog(ctx, options)
521 func (conn *Conn) GroupCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Group, error) {
522 return conn.chooseBackend(options.ClusterID).GroupCreate(ctx, options)
525 func (conn *Conn) GroupUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Group, error) {
526 return conn.chooseBackend(options.UUID).GroupUpdate(ctx, options)
529 func (conn *Conn) GroupGet(ctx context.Context, options arvados.GetOptions) (arvados.Group, error) {
530 return conn.chooseBackend(options.UUID).GroupGet(ctx, options)
533 func (conn *Conn) GroupList(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
534 return conn.generated_GroupList(ctx, options)
537 var userUuidRe = regexp.MustCompile(`^[0-9a-z]{5}-tpzed-[0-9a-z]{15}$`)
539 func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
540 if options.ClusterID != "" {
541 // explicitly selected cluster
542 return conn.chooseBackend(options.ClusterID).GroupContents(ctx, options)
543 } else if userUuidRe.MatchString(options.UUID) {
544 // user, get the things they own on the local cluster
545 return conn.local.GroupContents(ctx, options)
547 // a group, potentially want to make federated request
548 return conn.chooseBackend(options.UUID).GroupContents(ctx, options)
552 func (conn *Conn) GroupShared(ctx context.Context, options arvados.ListOptions) (arvados.GroupList, error) {
553 return conn.chooseBackend(options.ClusterID).GroupShared(ctx, options)
556 func (conn *Conn) GroupDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
557 return conn.chooseBackend(options.UUID).GroupDelete(ctx, options)
560 func (conn *Conn) GroupTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Group, error) {
561 return conn.chooseBackend(options.UUID).GroupTrash(ctx, options)
564 func (conn *Conn) GroupUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Group, error) {
565 return conn.chooseBackend(options.UUID).GroupUntrash(ctx, options)
568 func (conn *Conn) LinkCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Link, error) {
569 return conn.chooseBackend(options.ClusterID).LinkCreate(ctx, options)
572 func (conn *Conn) LinkUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Link, error) {
573 return conn.chooseBackend(options.UUID).LinkUpdate(ctx, options)
576 func (conn *Conn) LinkGet(ctx context.Context, options arvados.GetOptions) (arvados.Link, error) {
577 return conn.chooseBackend(options.UUID).LinkGet(ctx, options)
580 func (conn *Conn) LinkList(ctx context.Context, options arvados.ListOptions) (arvados.LinkList, error) {
581 return conn.generated_LinkList(ctx, options)
584 func (conn *Conn) LinkDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Link, error) {
585 return conn.chooseBackend(options.UUID).LinkDelete(ctx, options)
588 func (conn *Conn) LogCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Log, error) {
589 return conn.chooseBackend(options.ClusterID).LogCreate(ctx, options)
592 func (conn *Conn) LogUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Log, error) {
593 return conn.chooseBackend(options.UUID).LogUpdate(ctx, options)
596 func (conn *Conn) LogGet(ctx context.Context, options arvados.GetOptions) (arvados.Log, error) {
597 return conn.chooseBackend(options.UUID).LogGet(ctx, options)
600 func (conn *Conn) LogList(ctx context.Context, options arvados.ListOptions) (arvados.LogList, error) {
601 return conn.generated_LogList(ctx, options)
604 func (conn *Conn) LogDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Log, error) {
605 return conn.chooseBackend(options.UUID).LogDelete(ctx, options)
608 func (conn *Conn) SysTrashSweep(ctx context.Context, options struct{}) (struct{}, error) {
609 return conn.local.SysTrashSweep(ctx, options)
612 var userAttrsCachedFromLoginCluster = map[string]bool{
627 "identity_url": false,
628 "modified_by_client_uuid": false,
629 "modified_by_user_uuid": false,
632 "writable_by": false,
637 func (conn *Conn) batchUpdateUsers(ctx context.Context,
638 options arvados.ListOptions,
639 items []arvados.User,
640 includeAdminAndInvited bool) (err error) {
642 id := conn.cluster.Login.LoginCluster
643 logger := ctxlog.FromContext(ctx)
644 batchOpts := arvados.UserBatchUpdateOptions{Updates: map[string]map[string]interface{}{}}
645 for _, user := range items {
646 if !strings.HasPrefix(user.UUID, id) {
649 logger.Debugf("cache user info for uuid %q", user.UUID)
651 // If the remote cluster has null timestamps
652 // (e.g., test server with incomplete
653 // fixtures) use dummy timestamps (instead of
654 // the zero time, which causes a Rails API
655 // error "year too big to marshal: 1 UTC").
656 if user.ModifiedAt.IsZero() {
657 user.ModifiedAt = time.Now()
659 if user.CreatedAt.IsZero() {
660 user.CreatedAt = time.Now()
663 var allFields map[string]interface{}
664 buf, err := json.Marshal(user)
666 return fmt.Errorf("error encoding user record from remote response: %s", err)
668 err = json.Unmarshal(buf, &allFields)
670 return fmt.Errorf("error transcoding user record from remote response: %s", err)
673 if len(options.Select) > 0 {
674 updates = map[string]interface{}{}
675 for _, k := range options.Select {
676 if v, ok := allFields[k]; ok && userAttrsCachedFromLoginCluster[k] {
681 for k := range updates {
682 if !userAttrsCachedFromLoginCluster[k] {
687 if !includeAdminAndInvited {
688 // make sure we don't send these fields.
689 delete(updates, "is_admin")
690 delete(updates, "is_invited")
692 batchOpts.Updates[user.UUID] = updates
694 if len(batchOpts.Updates) > 0 {
695 ctxRoot := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{conn.cluster.SystemRootToken}})
696 _, err = conn.local.UserBatchUpdate(ctxRoot, batchOpts)
698 return fmt.Errorf("error updating local user records: %s", err)
704 func (conn *Conn) includeAdminAndInvitedInBatchUpdate(ctx context.Context, be backend, updateUserUUID string) (bool, error) {
705 // API versions prior to 20231117 would only include the
706 // is_invited and is_admin fields if the current user is an
707 // admin, or is requesting their own user record. If those
708 // fields aren't actually valid then we don't want to
709 // send them in the batch update.
710 dd, err := be.DiscoveryDocument(ctx)
712 // couldn't get discovery document
715 if dd.Revision >= "20231117" {
716 // newer version, fields are valid.
719 selfuser, err := be.UserGetCurrent(ctx, arvados.GetOptions{})
721 // couldn't get our user record
724 if selfuser.IsAdmin || selfuser.UUID == updateUserUUID {
725 // we are an admin, or the current user is the same as
726 // the user that we are updating.
729 // Better safe than sorry.
733 func (conn *Conn) UserList(ctx context.Context, options arvados.ListOptions) (arvados.UserList, error) {
734 if id := conn.cluster.Login.LoginCluster; id != "" && id != conn.cluster.ClusterID && !options.BypassFederation {
735 be := conn.chooseBackend(id)
736 resp, err := be.UserList(ctx, options)
740 includeAdminAndInvited, err := conn.includeAdminAndInvitedInBatchUpdate(ctx, be, "")
742 return arvados.UserList{}, err
744 err = conn.batchUpdateUsers(ctx, options, resp.Items, includeAdminAndInvited)
746 return arvados.UserList{}, err
750 return conn.generated_UserList(ctx, options)
753 func (conn *Conn) UserCreate(ctx context.Context, options arvados.CreateOptions) (arvados.User, error) {
754 return conn.chooseBackend(options.ClusterID).UserCreate(ctx, options)
757 func (conn *Conn) UserUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.User, error) {
758 if options.BypassFederation {
759 return conn.local.UserUpdate(ctx, options)
761 be := conn.chooseBackend(options.UUID)
762 resp, err := be.UserUpdate(ctx, options)
766 if !strings.HasPrefix(options.UUID, conn.cluster.ClusterID) {
767 includeAdminAndInvited, err := conn.includeAdminAndInvitedInBatchUpdate(ctx, be, options.UUID)
769 return arvados.User{}, err
771 // Copy the updated user record to the local cluster
772 err = conn.batchUpdateUsers(ctx, arvados.ListOptions{}, []arvados.User{resp}, includeAdminAndInvited)
774 return arvados.User{}, err
780 func (conn *Conn) UserMerge(ctx context.Context, options arvados.UserMergeOptions) (arvados.User, error) {
781 return conn.local.UserMerge(ctx, options)
784 func (conn *Conn) UserActivate(ctx context.Context, options arvados.UserActivateOptions) (arvados.User, error) {
785 return conn.localOrLoginCluster().UserActivate(ctx, options)
788 func (conn *Conn) UserSetup(ctx context.Context, options arvados.UserSetupOptions) (map[string]interface{}, error) {
789 upstream := conn.localOrLoginCluster()
790 if upstream != conn.local {
791 // When LoginCluster is in effect, and we're setting
792 // up a remote user, and we want to give that user
793 // access to a local VM, we can't include the VM in
794 // the setup call, because the remote cluster won't
797 // Similarly, if we want to create a git repo,
798 // it should be created on the local cluster,
799 // not the remote one.
801 upstreamOptions := options
802 upstreamOptions.VMUUID = ""
803 upstreamOptions.RepoName = ""
805 ret, err := upstream.UserSetup(ctx, upstreamOptions)
811 return conn.local.UserSetup(ctx, options)
814 func (conn *Conn) UserUnsetup(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
815 return conn.localOrLoginCluster().UserUnsetup(ctx, options)
818 func (conn *Conn) UserGet(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
819 be := conn.chooseBackend(options.UUID)
820 resp, err := be.UserGet(ctx, options)
824 if options.UUID != resp.UUID {
825 return arvados.User{}, httpErrorf(http.StatusBadGateway, "Had requested %v but response was for %v", options.UUID, resp.UUID)
827 if options.UUID[:5] != conn.cluster.ClusterID {
828 includeAdminAndInvited, err := conn.includeAdminAndInvitedInBatchUpdate(ctx, be, options.UUID)
830 return arvados.User{}, err
832 err = conn.batchUpdateUsers(ctx, arvados.ListOptions{Select: options.Select}, []arvados.User{resp}, includeAdminAndInvited)
834 return arvados.User{}, err
840 func (conn *Conn) UserGetCurrent(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
841 return conn.local.UserGetCurrent(ctx, options)
844 func (conn *Conn) UserGetSystem(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
845 return conn.chooseBackend(options.UUID).UserGetSystem(ctx, options)
848 func (conn *Conn) UserDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.User, error) {
849 return conn.chooseBackend(options.UUID).UserDelete(ctx, options)
852 func (conn *Conn) UserBatchUpdate(ctx context.Context, options arvados.UserBatchUpdateOptions) (arvados.UserList, error) {
853 return conn.local.UserBatchUpdate(ctx, options)
856 func (conn *Conn) UserAuthenticate(ctx context.Context, options arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
857 return conn.local.UserAuthenticate(ctx, options)
860 func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
861 return conn.chooseBackend(options.UUID).APIClientAuthorizationCurrent(ctx, options)
864 func (conn *Conn) APIClientAuthorizationCreate(ctx context.Context, options arvados.CreateOptions) (arvados.APIClientAuthorization, error) {
865 if conn.cluster.Login.LoginCluster != "" {
866 return conn.chooseBackend(conn.cluster.Login.LoginCluster).APIClientAuthorizationCreate(ctx, options)
868 ownerUUID, ok := options.Attrs["owner_uuid"].(string)
869 if ok && ownerUUID != "" {
870 return conn.chooseBackend(ownerUUID).APIClientAuthorizationCreate(ctx, options)
872 return conn.local.APIClientAuthorizationCreate(ctx, options)
875 func (conn *Conn) APIClientAuthorizationUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.APIClientAuthorization, error) {
876 if options.BypassFederation {
877 return conn.local.APIClientAuthorizationUpdate(ctx, options)
879 return conn.chooseBackend(options.UUID).APIClientAuthorizationUpdate(ctx, options)
882 func (conn *Conn) APIClientAuthorizationDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.APIClientAuthorization, error) {
883 return conn.chooseBackend(options.UUID).APIClientAuthorizationDelete(ctx, options)
886 func (conn *Conn) APIClientAuthorizationList(ctx context.Context, options arvados.ListOptions) (arvados.APIClientAuthorizationList, error) {
887 if id := conn.cluster.Login.LoginCluster; id != "" && id != conn.cluster.ClusterID && !options.BypassFederation {
888 return conn.chooseBackend(conn.cluster.Login.LoginCluster).APIClientAuthorizationList(ctx, options)
890 return conn.generated_APIClientAuthorizationList(ctx, options)
893 func (conn *Conn) APIClientAuthorizationGet(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
894 return conn.chooseBackend(options.UUID).APIClientAuthorizationGet(ctx, options)
897 type backend interface {
902 type notFoundError struct{}
904 func (notFoundError) HTTPStatus() int { return http.StatusNotFound }
905 func (notFoundError) Error() string { return "not found" }
907 func errStatus(err error) int {
908 if httpErr, ok := err.(interface{ HTTPStatus() int }); ok {
909 return httpErr.HTTPStatus()
911 return http.StatusInternalServerError