Skip to content

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

chore: introduce project viewer that can list all databases and create request privilege issues (#9075)

Danny Xu7282de62948e
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index c59c949d01..4ae7d600e7 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -1584,7 +1584,7 @@ func (s *DatabaseService) checkDatabasePermission(ctx context.Context, projectID
projectRoles := make(map[common.ProjectRole]bool)
for _, binding := range policy.Bindings {
for _, member := range binding.Members {
- if member.ID == principalID {
+ if member.ID == principalID || member.Email == api.AllUsers {
projectRoles[common.ProjectRole(binding.Role)] = true
break
}
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index 667b3c9b6b..85f1f4ec7d 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -460,20 +460,27 @@ func (s *IssueService) CreateIssue(ctx context.Context, request *v1pb.CreateIssu
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
- ok, err := isUserAtLeastProjectDeveloper(ctx, s.store, projectID)
- if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to check if the user can create issue, error: %v", err)
- }
- if !ok {
- return nil, status.Errorf(codes.PermissionDenied, "permission denied")
- }
switch request.Issue.Type {
case v1pb.Issue_TYPE_UNSPECIFIED:
return nil, status.Errorf(codes.InvalidArgument, "issue type is required")
case v1pb.Issue_GRANT_REQUEST:
+ ok, err := isUserAtLeastProjectViewer(ctx, s.store, projectID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to check if the user can create issue, error: %v", err)
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "permission denied")
+ }
return s.createIssueGrantRequest(ctx, request)
case v1pb.Issue_DATABASE_CHANGE:
+ ok, err := isUserAtLeastProjectDeveloper(ctx, s.store, projectID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to check if the user can create issue, error: %v", err)
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "permission denied")
+ }
return s.createIssueDatabaseChange(ctx, request)
default:
return nil, status.Errorf(codes.InvalidArgument, "unknown issue type %q", request.Issue.Type)
@@ -1831,6 +1838,46 @@ func getUserBelongingProjects(ctx context.Context, s *store.Store, userUID int)
return projectIDs, nil
}
+func isUserAtLeastProjectViewer(ctx context.Context, s *store.Store, requestProjectID string) (bool, error) {
+ principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
+ if !ok {
+ return false, status.Errorf(codes.Internal, "principal ID not found")
+ }
+ user, err := s.GetUserByID(ctx, principalID)
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to get user %d", principalID)
+ }
+
+ if isOwnerOrDBA(user.Role) {
+ return true, nil
+ }
+
+ policy, err := s.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{ProjectID: &requestProjectID})
+ if err != nil {
+ return false, errors.Wrapf(err, "failed to get project iam policy")
+ }
+
+ if isProjectOwnerDeveloperOrViewer(principalID, policy) {
+ return true, nil
+ }
+ return false, nil
+}
+
+// isProjectOwnerDeveloperOrViewer returns whether a principal is a project owner or developer in the project.
+func isProjectOwnerDeveloperOrViewer(principalID int, projectPolicy *store.IAMPolicyMessage) bool {
+ for _, binding := range projectPolicy.Bindings {
+ if binding.Role != api.Owner && binding.Role != api.Developer && binding.Role != api.ProjectViewer {
+ continue
+ }
+ for _, member := range binding.Members {
+ if member.ID == principalID || member.Email == "allUsers" {
+ return true
+ }
+ }
+ }
+ return false
+}
+
func isUserAtLeastProjectDeveloper(ctx context.Context, s *store.Store, requestProjectID string) (bool, error) {
principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
if !ok {
diff --git a/backend/common/role.go b/backend/common/role.go
index 45613dfc38..87afb3a564 100644
--- a/backend/common/role.go
+++ b/backend/common/role.go
@@ -12,4 +12,8 @@ const (
ProjectExporter ProjectRole = "EXPORTER"
// ProjectQuerier is the querier of a project.
ProjectQuerier ProjectRole = "QUERIER"
+ // Releaser is the RELEASER role.
+ Releaser ProjectRole = "RELEASER"
+ // ProjectViewer is the viewer of a project.
+ ProjectViewer ProjectRole = "VIEWER"
)
diff --git a/backend/legacyapi/member.go b/backend/legacyapi/member.go
index 7bbeb8be5b..20aea0ca39 100644
--- a/backend/legacyapi/member.go
+++ b/backend/legacyapi/member.go
@@ -28,6 +28,8 @@ const (
Querier Role = "QUERIER"
// Releaser is the RELEASER role.
Releaser Role = "RELEASER"
+ // ProjectViewer is the database viewer role.
+ ProjectViewer Role = "VIEWER"
// UnknownRole is the unknown role.
UnknownRole Role = "UNKNOWN"
)
diff --git a/backend/legacyapi/principal.go b/backend/legacyapi/principal.go
index d9c24ab11a..a1edfeddca 100644
--- a/backend/legacyapi/principal.go
+++ b/backend/legacyapi/principal.go
@@ -6,6 +6,12 @@ const SystemBotID = 1
// SystemBotEmail is the email of the system robot.
const SystemBotEmail = "[email protected]"
+// AllUsersID is the ID of the allUsers robot.
+const AllUsersID = 2
+
+// AllUsers is the email of the pseudo allUsers account.
+const AllUsers = "allUsers"
+
// PrincipalType is the type of a principal.
type PrincipalType string
diff --git a/backend/legacyapi/project_member.go b/backend/legacyapi/project_member.go
index 11170fa7ce..28a5792a5d 100644
--- a/backend/legacyapi/project_member.go
+++ b/backend/legacyapi/project_member.go
@@ -35,19 +35,19 @@ const (
// ProjectPermission returns whether a particular permission is granted to a particular project role in a particular plan.
func ProjectPermission(permission ProjectPermissionType, plan PlanType, roles map[common.ProjectRole]bool) bool {
// a map from the a particular feature to the respective enablement of a project developer and owner.
- projectPermissionMatrix := map[ProjectPermissionType][2]bool{
- ProjectPermissionManageGeneral: {false, true},
- ProjectPermissionManageMember: {false, true},
- ProjectPermissionCreateSheet: {true, true},
- ProjectPermissionAdminSheet: {false, true},
- ProjectPermissionOrganizeSheet: {true, true},
- ProjectPermissionSyncSheet: {true, true},
- ProjectPermissionChangeDatabase: {true, true},
- ProjectPermissionAdminDatabase: {false, true},
+ projectPermissionMatrix := map[ProjectPermissionType][3]bool{
+ ProjectPermissionManageGeneral: {false, true, true},
+ ProjectPermissionManageMember: {false, true, false},
+ ProjectPermissionCreateSheet: {true, true, false},
+ ProjectPermissionAdminSheet: {false, true, false},
+ ProjectPermissionOrganizeSheet: {true, true, false},
+ ProjectPermissionSyncSheet: {true, true, false},
+ ProjectPermissionChangeDatabase: {true, true, false},
+ ProjectPermissionAdminDatabase: {false, true, false},
// If dba-workflow is disabled, then project developer can also create database.
- ProjectPermissionCreateDatabase: {!Feature(FeatureDBAWorkflow, plan), true},
+ ProjectPermissionCreateDatabase: {!Feature(FeatureDBAWorkflow, plan), true, false},
// If dba-workflow is disabled, then project developer can also transfer database.
- ProjectPermissionTransferDatabase: {!Feature(FeatureDBAWorkflow, plan), true},
+ ProjectPermissionTransferDatabase: {!Feature(FeatureDBAWorkflow, plan), true, false},
}
for role := range roles {
@@ -60,6 +60,10 @@ func ProjectPermission(permission ProjectPermissionType, plan PlanType, roles ma
if projectPermissionMatrix[permission][1] {
return true
}
+ case common.ProjectViewer:
+ if projectPermissionMatrix[permission][2] {
+ return true
+ }
}
}
diff --git a/backend/migrator/migration/dev/LATEST.sql b/backend/migrator/migration/dev/LATEST.sql
index 3289bd724a..246b7a3179 100644
--- a/backend/migrator/migration/dev/LATEST.sql
+++ b/backend/migrator/migration/dev/LATEST.sql
@@ -78,6 +78,28 @@ VALUES
''
);
+-- Pseudo allUsers account id is 2.
+INSERT INTO
+ principal (
+ id,
+ creator_id,
+ updater_id,
+ type,
+ name,
+ email,
+ password_hash
+ )
+VALUES
+ (
+ 2,
+ 2,
+ 2,
+ 'SYSTEM_BOT',
+ 'All Users',
+ 'allUsers',
+ ''
+ );
+
ALTER SEQUENCE principal_id_seq RESTART WITH 101;
-- Setting
diff --git a/backend/migrator/migration/prod/2.10/0004##all_users.sql b/backend/migrator/migration/prod/2.10/0004##all_users.sql
new file mode 100644
index 0000000000..d791f49c54
--- /dev/null
+++ b/backend/migrator/migration/prod/2.10/0004##all_users.sql
@@ -0,0 +1,21 @@
+-- Pseudo allUsers account id is 2.
+INSERT INTO
+ principal (
+ id,
+ creator_id,
+ updater_id,
+ type,
+ name,
+ email,
+ password_hash
+ )
+VALUES
+ (
+ 2,
+ 2,
+ 2,
+ 'SYSTEM_BOT',
+ 'All Users',
+ 'allUsers',
+ ''
+ );
\ No newline at end of file
diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sql
index 3c5a67bea7..7897e18887 100644
--- a/backend/migrator/migration/prod/LATEST.sql
+++ b/backend/migrator/migration/prod/LATEST.sql
@@ -78,6 +78,28 @@ VALUES
''
);
+-- Pseudo allUsers account id is 2.
+INSERT INTO
+ principal (
+ id,
+ creator_id,
+ updater_id,
+ type,
+ name,
+ email,
+ password_hash
+ )
+VALUES
+ (
+ 2,
+ 2,
+ 2,
+ 'SYSTEM_BOT',
+ 'All Users',
+ 'allUsers',
+ ''
+ );
+
ALTER SEQUENCE principal_id_seq RESTART WITH 101;
-- Setting
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index b5e7afed93..d4f75cc335 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -231,5 +231,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("2.10.3"), releaseVersion)
+ require.Equal(t, semver.MustParse("2.10.4"), releaseVersion)
}
diff --git a/backend/store/role.go b/backend/store/role.go
index a083122d7a..068c60a443 100644
--- a/backend/store/role.go
+++ b/backend/store/role.go
@@ -93,6 +93,12 @@ func (s *Store) ListRoles(ctx context.Context) ([]*RoleMessage, error) {
Name: "Project releaser",
Description: "",
},
+ &RoleMessage{
+ CreatorID: api.SystemBotID,
+ ResourceID: api.ProjectViewer.String(),
+ Name: "Project database viewer",
+ Description: "",
+ },
&RoleMessage{
CreatorID: api.SystemBotID,
ResourceID: api.Exporter.String(),
diff --git a/frontend/src/types/v1/role.ts b/frontend/src/types/v1/role.ts
index 6ac0ee8571..0249273115 100644
--- a/frontend/src/types/v1/role.ts
+++ b/frontend/src/types/v1/role.ts
@@ -4,6 +4,7 @@ export const PresetRoleType = {
QUERIER: "roles/QUERIER",
EXPORTER: "roles/EXPORTER",
RELEASER: "roles/RELEASER",
+ VIEWER: "roles/VIEWER",
};
export const PresetRoleTypeList = [
@@ -12,6 +13,7 @@ export const PresetRoleTypeList = [
PresetRoleType.QUERIER,
PresetRoleType.EXPORTER,
PresetRoleType.RELEASER,
+ PresetRoleType.VIEWER,
];
export const VirtualRoleType = {