|
| 1 | +// Copyright 2019 Yunion |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tokens |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "yunion.io/x/pkg/errors" |
| 21 | + "yunion.io/x/pkg/util/rbacscope" |
| 22 | + |
| 23 | + api "yunion.io/x/onecloud/pkg/apis/identity" |
| 24 | + "yunion.io/x/onecloud/pkg/cloudcommon/policy" |
| 25 | + "yunion.io/x/onecloud/pkg/httperrors" |
| 26 | + "yunion.io/x/onecloud/pkg/keystone/models" |
| 27 | + "yunion.io/x/onecloud/pkg/mcclient" |
| 28 | +) |
| 29 | + |
| 30 | +// authUserByAssume allows an admin token to login as any user without password |
| 31 | +func authUserByAssume(ctx context.Context, input mcclient.SAuthenticationInputV3) (*api.SUserExtended, error) { |
| 32 | + // Validate admin token |
| 33 | + if len(input.Auth.Identity.Token.Id) == 0 { |
| 34 | + return nil, httperrors.NewInputParameterError("admin token is required for assume authentication") |
| 35 | + } |
| 36 | + |
| 37 | + // Decode and validate the admin token |
| 38 | + adminToken, err := TokenStrDecode(ctx, input.Auth.Identity.Token.Id) |
| 39 | + if err != nil { |
| 40 | + return nil, errors.Wrap(err, "decode admin token") |
| 41 | + } |
| 42 | + |
| 43 | + // Check if admin token is expired |
| 44 | + if adminToken.IsExpired() { |
| 45 | + return nil, ErrExpiredToken |
| 46 | + } |
| 47 | + |
| 48 | + scopedProject, err := models.ProjectManager.FetchProject( |
| 49 | + input.Auth.Scope.Project.Id, |
| 50 | + input.Auth.Scope.Project.Name, |
| 51 | + input.Auth.Scope.Project.Domain.Id, |
| 52 | + input.Auth.Scope.Project.Domain.Name, |
| 53 | + ) |
| 54 | + if err != nil { |
| 55 | + return nil, errors.Wrap(err, "fetch scoped project") |
| 56 | + } |
| 57 | + |
| 58 | + var requireScope rbacscope.TRbacScope |
| 59 | + if adminToken.ProjectId == scopedProject.Id { |
| 60 | + requireScope = rbacscope.ScopeProject |
| 61 | + } else if adminToken.DomainId == scopedProject.DomainId { |
| 62 | + requireScope = rbacscope.ScopeDomain |
| 63 | + } else { |
| 64 | + requireScope = rbacscope.ScopeSystem |
| 65 | + } |
| 66 | + |
| 67 | + adminToken.ProjectId = scopedProject.Id |
| 68 | + adminToken.DomainId = scopedProject.DomainId |
| 69 | + |
| 70 | + adminTokenCred, err := adminToken.GetSimpleUserCred(input.Auth.Identity.Token.Id) |
| 71 | + if err != nil { |
| 72 | + return nil, errors.Wrap(err, "get admin token credential") |
| 73 | + } |
| 74 | + |
| 75 | + // Validate target user information |
| 76 | + assumeUser := input.Auth.Identity.Assume.User |
| 77 | + if len(assumeUser.Id) == 0 && len(assumeUser.Name) == 0 { |
| 78 | + return nil, httperrors.NewInputParameterError("target user id or name is required") |
| 79 | + } |
| 80 | + |
| 81 | + // Fetch target user |
| 82 | + targetUser, err := models.UserManager.FetchUserExtended( |
| 83 | + assumeUser.Id, |
| 84 | + assumeUser.Name, |
| 85 | + assumeUser.Domain.Id, |
| 86 | + assumeUser.Domain.Name, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return nil, errors.Wrap(err, "fetch target user") |
| 90 | + } |
| 91 | + |
| 92 | + if adminTokenCred.GetUserId() != targetUser.Id { |
| 93 | + if policy.PolicyManager.Allow(requireScope, adminTokenCred, api.SERVICE_TYPE, "tokens", "perform", "assume").Result.IsDeny() { |
| 94 | + return nil, httperrors.NewForbiddenError("%s not allow to assume user in project %s", adminTokenCred.GetUserName(), scopedProject.Name) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Set audit IDs to include both admin token and assume operation |
| 99 | + targetUser.AuditIds = []string{adminTokenCred.GetUserId()} |
| 100 | + |
| 101 | + return targetUser, nil |
| 102 | +} |
0 commit comments