Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: update user acl check (#13067)

Danny Xu783d099011f7
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 1258d19216..8b88d74e22 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -124,11 +124,20 @@ func (s *AuthService) CreateUser(ctx context.Context, request *v1pb.CreateUserRe
}
if setting.DisallowSignup {
- // TODO(d): use fine-grained permission.
- rolePtr := ctx.Value(common.RoleContextKey)
- if rolePtr == nil || rolePtr.(api.Role) != api.WorkspaceAdmin {
+ if _, ok := ctx.Value(common.PrincipalIDContextKey).(int); !ok {
return nil, status.Errorf(codes.PermissionDenied, "sign up is disallowed")
}
+ callerUser, err := s.getCallerUser(ctx)
+ if err != nil {
+ return nil, err
+ }
+ ok, err := s.iamManager.CheckPermission(ctx, iam.PermissionUsersCreate, callerUser)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "user does not have permission %q", iam.PermissionUsersCreate)
+ }
}
if request.User == nil {
return nil, status.Errorf(codes.InvalidArgument, "user must be set")
@@ -274,12 +283,18 @@ func (s *AuthService) UpdateUser(ctx context.Context, request *v1pb.UpdateUserRe
return nil, status.Errorf(codes.NotFound, "user %q has been deleted", userID)
}
- role, ok := ctx.Value(common.RoleContextKey).(api.Role)
- if !ok {
- return nil, status.Errorf(codes.Internal, "role not found")
- }
- if principalID != userID && role != api.WorkspaceAdmin {
- return nil, status.Errorf(codes.PermissionDenied, "only workspace owner or user itself can update the user %d", userID)
+ if principalID != userID {
+ callerUser, err := s.getCallerUser(ctx)
+ if err != nil {
+ return nil, err
+ }
+ ok, err := s.iamManager.CheckPermission(ctx, iam.PermissionUsersUpdate, callerUser)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "user does not have permission %q", iam.PermissionUsersUpdate)
+ }
}
setting, err := s.store.GetWorkspaceGeneralSetting(ctx)
@@ -421,6 +436,19 @@ func (s *AuthService) DeleteUser(ctx context.Context, request *v1pb.DeleteUserRe
if !ok {
return nil, status.Errorf(codes.Internal, "principal ID not found")
}
+
+ callerUser, err := s.getCallerUser(ctx)
+ if err != nil {
+ return nil, err
+ }
+ ok, err = s.iamManager.CheckPermission(ctx, iam.PermissionUsersDelete, callerUser)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "user does not have permission %q", iam.PermissionUsersDelete)
+ }
+
userID, err := common.GetUserID(request.Name)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
@@ -436,13 +464,6 @@ func (s *AuthService) DeleteUser(ctx context.Context, request *v1pb.DeleteUserRe
return nil, status.Errorf(codes.NotFound, "user %q has been deleted", userID)
}
- role, ok := ctx.Value(common.RoleContextKey).(api.Role)
- if !ok {
- return nil, status.Errorf(codes.Internal, "role not found")
- }
- if role != api.WorkspaceAdmin {
- return nil, status.Errorf(codes.PermissionDenied, "only workspace owner can delete the user %d", userID)
- }
// Check if the user is the only workspace admin.
isAdmin, err := s.iamManager.CheckUserContainsWorkspaceRoles(ctx, user, api.WorkspaceAdmin)
if err != nil {
@@ -474,6 +495,19 @@ func (s *AuthService) UndeleteUser(ctx context.Context, request *v1pb.UndeleteUs
if !ok {
return nil, status.Errorf(codes.Internal, "principal ID not found")
}
+
+ callerUser, err := s.getCallerUser(ctx)
+ if err != nil {
+ return nil, err
+ }
+ ok, err = s.iamManager.CheckPermission(ctx, iam.PermissionUsersUndelete, callerUser)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "user does not have permission %q", iam.PermissionUsersUndelete)
+ }
+
userID, err := common.GetUserID(request.Name)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
@@ -489,14 +523,6 @@ func (s *AuthService) UndeleteUser(ctx context.Context, request *v1pb.UndeleteUs
return nil, status.Errorf(codes.InvalidArgument, "user %q is already active", userID)
}
- role, ok := ctx.Value(common.RoleContextKey).(api.Role)
- if !ok {
- return nil, status.Errorf(codes.Internal, "role not found")
- }
- if role != api.WorkspaceAdmin {
- return nil, status.Errorf(codes.PermissionDenied, "only workspace owner can undelete the user %d", userID)
- }
-
user, err = s.store.UpdateUser(ctx, user, &store.UpdateUserMessage{Delete: &undeletePatch}, principalID)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
@@ -979,3 +1005,18 @@ func (s *AuthService) userCountGuard(ctx context.Context) error {
}
return nil
}
+
+func (s *AuthService) getCallerUser(ctx context.Context) (*store.UserMessage, error) {
+ principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
+ if !ok {
+ return nil, status.Errorf(codes.Internal, "principal ID not found")
+ }
+ user, err := s.store.GetUserByID(ctx, principalID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get user, error: %v", err)
+ }
+ if user == nil {
+ return nil, status.Errorf(codes.NotFound, "user %d not found", principalID)
+ }
+ return user, nil
+}
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 8d8b6dfc34..2e954a430a 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -1651,38 +1651,10 @@ func (s *DatabaseService) ListSlowQueries(ctx context.Context, request *v1pb.Lis
return nil, status.Errorf(codes.Internal, "failed to find database list %q", err.Error())
}
- var canAccessDBs []*store.DatabaseMessage
-
- user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage)
- if !ok {
- return nil, status.Errorf(codes.Internal, "user not found")
- }
- role, ok := ctx.Value(common.RoleContextKey).(api.Role)
- if !ok {
- return nil, status.Errorf(codes.Internal, "role not found")
- }
-
- switch role {
- case api.WorkspaceAdmin, api.WorkspaceDBA:
- canAccessDBs = databases
- case api.WorkspaceMember:
- for _, database := range databases {
- ok, err := s.iamManager.CheckPermission(ctx, iam.PermissionSlowQueriesList, user, database.ProjectID)
- if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to check permission, err: %v", err.Error())
- }
- if ok {
- canAccessDBs = append(canAccessDBs, database)
- }
- }
- default:
- return nil, status.Errorf(codes.PermissionDenied, "unknown role %q", role)
- }
-
result := &v1pb.ListSlowQueriesResponse{}
instanceMap := make(map[string]*totalValue)
- for _, database := range canAccessDBs {
+ for _, database := range databases {
instance, err := s.store.GetInstanceV2(ctx, &store.FindInstanceMessage{
ResourceID: &database.InstanceID,
})
diff --git a/backend/component/iam/acl.yaml b/backend/component/iam/acl.yaml
index e686fdbd42..149f56462a 100644
--- a/backend/component/iam/acl.yaml
+++ b/backend/component/iam/acl.yaml
@@ -114,6 +114,10 @@ roles:
- bb.userGroups.get
- bb.userGroups.list
- bb.userGroups.update
+ - bb.users.create
+ - bb.users.update
+ - bb.users.delete
+ - bb.users.undelete
- bb.worksheets.manage
- bb.worksheets.get
- name: roles/workspaceDBA
@@ -243,6 +247,7 @@ roles:
- bb.policies.list
- bb.risks.list
- bb.roles.list
+ - bb.settings.get
- bb.settings.list
- bb.userGroups.get
- bb.userGroups.list
diff --git a/backend/component/iam/permission.go b/backend/component/iam/permission.go
index 2fb8d697cc..18470004f5 100644
--- a/backend/component/iam/permission.go
+++ b/backend/component/iam/permission.go
@@ -57,6 +57,10 @@ const (
PermissionUserGroupsGet Permission = "bb.userGroups.get"
PermissionUserGroupsList Permission = "bb.userGroups.list"
PermissionUserGroupsUpdate Permission = "bb.userGroups.update"
+ PermissionUsersCreate Permission = "bb.users.create"
+ PermissionUsersUpdate Permission = "bb.users.update"
+ PermissionUsersDelete Permission = "bb.users.delete"
+ PermissionUsersUndelete Permission = "bb.users.undelete"
PermissionSettingsGet Permission = "bb.settings.get"
PermissionSettingsList Permission = "bb.settings.list"
PermissionSettingsSet Permission = "bb.settings.set"
@@ -177,6 +181,10 @@ var allWorkspacePermissions = []Permission{
PermissionUserGroupsGet,
PermissionUserGroupsList,
PermissionUserGroupsUpdate,
+ PermissionUsersCreate,
+ PermissionUsersUpdate,
+ PermissionUsersDelete,
+ PermissionUsersUndelete,
PermissionSettingsGet,
PermissionSettingsList,
PermissionSettingsSet,
diff --git a/backend/component/iam/permission.yaml b/backend/component/iam/permission.yaml
index 124ebbf504..df7cfe287b 100644
--- a/backend/component/iam/permission.yaml
+++ b/backend/component/iam/permission.yaml
@@ -103,6 +103,10 @@ permissions:
- bb.userGroups.delete
- bb.userGroups.list
- bb.userGroups.get
+ - bb.users.create
+ - bb.users.update
+ - bb.users.delete
+ - bb.users.undelete
- bb.settings.get
- bb.settings.list
- bb.settings.set
diff --git a/frontend/src/types/proto/v1/auth_service.ts b/frontend/src/types/proto/v1/auth_service.ts
index 97c1c999bc..a37e7e37e0 100644
--- a/frontend/src/types/proto/v1/auth_service.ts
+++ b/frontend/src/types/proto/v1/auth_service.ts
@@ -1526,7 +1526,7 @@ export const AuthServiceDefinition = {
},
},
},
- /** Only the user itself and the user with bb.users.create permission on the workspace can update the user. */
+ /** Only the user itself and the user with bb.users.update permission on the workspace can update the user. */
updateUser: {
name: "UpdateUser",
requestType: UpdateUserRequest,
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index 25a5a8c14a..fc62c247f9 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -7457,7 +7457,7 @@ paths:
patch:
tags:
- AuthService
- description: Only the user itself and the user with bb.users.create permission on the workspace can update the user.
+ description: Only the user itself and the user with bb.users.update permission on the workspace can update the user.
operationId: AuthService_UpdateUser
parameters:
- name: user
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 47e0db5ef1..0bc8d26dcf 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -1718,7 +1718,7 @@ The user's `name` field is used to identify the user to update. Format: user
| GetUser | [GetUserRequest](#bytebase-v1-GetUserRequest) | [User](#bytebase-v1-User) | Get the user. Any authenticated user can get the user. |
| ListUsers | [ListUsersRequest](#bytebase-v1-ListUsersRequest) | [ListUsersResponse](#bytebase-v1-ListUsersResponse) | List all users. Any authenticated user can list users. |
| CreateUser | [CreateUserRequest](#bytebase-v1-CreateUserRequest) | [User](#bytebase-v1-User) | Create a user. When Disallow Signup is enabled, only the caller with bb.users.create on the workspace can create a user. Otherwise, any unauthenticated user can create a user. |
-| UpdateUser | [UpdateUserRequest](#bytebase-v1-UpdateUserRequest) | [User](#bytebase-v1-User) | Only the user itself and the user with bb.users.create permission on the workspace can update the user. |
+| UpdateUser | [UpdateUserRequest](#bytebase-v1-UpdateUserRequest) | [User](#bytebase-v1-User) | Only the user itself and the user with bb.users.update permission on the workspace can update the user. |
| DeleteUser | [DeleteUserRequest](#bytebase-v1-DeleteUserRequest) | [.google.protobuf.Empty](#google-protobuf-Empty) | Only the user with bb.users.delete permission on the workspace can delete the user. The last remaining workspace admin cannot be deleted. |
| UndeleteUser | [UndeleteUserRequest](#bytebase-v1-UndeleteUserRequest) | [User](#bytebase-v1-User) | Only the user with bb.users.undelete permission on the workspace can undelete the user. |
| Login | [LoginRequest](#bytebase-v1-LoginRequest) | [LoginResponse](#bytebase-v1-LoginResponse) | |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 32e020005c..ed08112d3b 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -5332,7 +5332,7 @@ Otherwise, any unauthenticated user can create a user.</p></td>
<td>UpdateUser</td>
<td><a href="#bytebase.v1.UpdateUserRequest">UpdateUserRequest</a></td>
<td><a href="#bytebase.v1.User">User</a></td>
- <td><p>Only the user itself and the user with bb.users.create permission on the workspace can update the user.</p></td>
+ <td><p>Only the user itself and the user with bb.users.update permission on the workspace can update the user.</p></td>
</tr>
<tr>
diff --git a/proto/generated-go/v1/auth_service_grpc.pb.go b/proto/generated-go/v1/auth_service_grpc.pb.go
index db729b6c8e..c1b5fc3260 100644
--- a/proto/generated-go/v1/auth_service_grpc.pb.go
+++ b/proto/generated-go/v1/auth_service_grpc.pb.go
@@ -44,7 +44,7 @@ type AuthServiceClient interface {
// When Disallow Signup is enabled, only the caller with bb.users.create on the workspace can create a user.
// Otherwise, any unauthenticated user can create a user.
CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*User, error)
- // Only the user itself and the user with bb.users.create permission on the workspace can update the user.
+ // Only the user itself and the user with bb.users.update permission on the workspace can update the user.
UpdateUser(ctx context.Context, in *UpdateUserRequest, opts ...grpc.CallOption) (*User, error)
// Only the user with bb.users.delete permission on the workspace can delete the user.
// The last remaining workspace admin cannot be deleted.
@@ -157,7 +157,7 @@ type AuthServiceServer interface {
// When Disallow Signup is enabled, only the caller with bb.users.create on the workspace can create a user.
// Otherwise, any unauthenticated user can create a user.
CreateUser(context.Context, *CreateUserRequest) (*User, error)
- // Only the user itself and the user with bb.users.create permission on the workspace can update the user.
+ // Only the user itself and the user with bb.users.update permission on the workspace can update the user.
UpdateUser(context.Context, *UpdateUserRequest) (*User, error)
// Only the user with bb.users.delete permission on the workspace can delete the user.
// The last remaining workspace admin cannot be deleted.
diff --git a/proto/v1/auth_service.proto b/proto/v1/auth_service.proto
index 9574c0de6c..352949be79 100644
--- a/proto/v1/auth_service.proto
+++ b/proto/v1/auth_service.proto
@@ -44,7 +44,7 @@ service AuthService {
option (bytebase.v1.audit) = true;
}
- // Only the user itself and the user with bb.users.create permission on the workspace can update the user.
+ // Only the user itself and the user with bb.users.update permission on the workspace can update the user.
rpc UpdateUser(UpdateUserRequest) returns (User) {
option (google.api.http) = {
patch: "/v1/{user.name=users/*}"