Skip to content

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

chore: branch protection (#8479)

Danny Xubf1c1282a06a
diff --git a/backend/api/v1/schema_design_service.go b/backend/api/v1/schema_design_service.go
index 8647552035..a3beeb6cc2 100644
--- a/backend/api/v1/schema_design_service.go
+++ b/backend/api/v1/schema_design_service.go
@@ -3,6 +3,7 @@ package v1
import (
"context"
"fmt"
+ "path"
"strconv"
"golang.org/x/exp/slices"
@@ -119,6 +120,12 @@ func (s *SchemaDesignService) CreateSchemaDesign(ctx context.Context, request *v
}
currentPrincipalID := ctx.Value(common.PrincipalIDContextKey).(int)
schemaDesign := request.SchemaDesign
+
+ // Branch protection check.
+ if err := s.checkProtectionRules(ctx, project, schemaDesign, currentPrincipalID); err != nil {
+ return nil, err
+ }
+
sanitizeSchemaDesignSchemaMetadata(schemaDesign)
if err := checkDatabaseMetadata(schemaDesign.Engine, schemaDesign.SchemaMetadata); err != nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid schema design: %v", err))
@@ -222,6 +229,58 @@ func (s *SchemaDesignService) CreateSchemaDesign(ctx context.Context, request *v
return schemaDesign, nil
}
+func (s *SchemaDesignService) checkProtectionRules(ctx context.Context, project *store.ProjectMessage, schemaDesign *v1pb.SchemaDesign, currentPrincipalID int) error {
+ if project.Setting == nil {
+ return nil
+ }
+ user, err := s.store.GetUserByID(ctx, currentPrincipalID)
+ if err != nil {
+ return err
+ }
+ policy, err := s.store.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{ProjectID: &project.ResourceID})
+ if err != nil {
+ return err
+ }
+ for _, rule := range project.Setting.ProtectionRules {
+ if rule.Target != storepb.ProtectionRule_BRANCH {
+ continue
+ }
+ ok, err := path.Match(rule.NameFilter, schemaDesign.Title)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ continue
+ }
+ pass := false
+ for _, binding := range policy.Bindings {
+ matchUser := false
+ for _, member := range binding.Members {
+ if member.Email == user.Email {
+ matchUser = true
+ break
+ }
+ }
+ if matchUser {
+ for _, role := range rule.CreateAllowedRoles {
+ // Convert role format.
+ if role == convertToProjectRole(binding.Role) {
+ pass = true
+ }
+ break
+ }
+ }
+ if pass {
+ break
+ }
+ }
+ if !pass {
+ return status.Errorf(codes.InvalidArgument, "not allowed to create branch by project protection rules")
+ }
+ }
+ return nil
+}
+
// UpdateSchemaDesign updates an existing schema design.
func (s *SchemaDesignService) UpdateSchemaDesign(ctx context.Context, request *v1pb.UpdateSchemaDesignRequest) (*v1pb.SchemaDesign, error) {
// TODO(steven): Only allow personal draft schema design to be updated.
diff --git a/frontend/src/types/proto/store/project.ts b/frontend/src/types/proto/store/project.ts
index a768640b44..d31549e01a 100644
--- a/frontend/src/types/proto/store/project.ts
+++ b/frontend/src/types/proto/store/project.ts
@@ -13,7 +13,10 @@ export interface ProtectionRule {
target: ProtectionRule_Target;
/** The name of the branch/changelist or wildcard. */
nameFilter: string;
- /** The roles allowed to create branches or changelists. */
+ /**
+ * The roles allowed to create branches or changelists.
+ * Format: roles/OWNER.
+ */
createAllowedRoles: string[];
}
diff --git a/frontend/src/types/proto/v1/project_service.ts b/frontend/src/types/proto/v1/project_service.ts
index cb6779bf88..0c5ebfaac6 100644
--- a/frontend/src/types/proto/v1/project_service.ts
+++ b/frontend/src/types/proto/v1/project_service.ts
@@ -1194,7 +1194,10 @@ export interface ProtectionRule {
target: ProtectionRule_Target;
/** The name of the branch/changelist or wildcard. */
nameFilter: string;
- /** The roles allowed to create branches or changelists. */
+ /**
+ * The roles allowed to create branches or changelists.
+ * Format: roles/OWNER.
+ */
createAllowedRoles: string[];
}
diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.md
index e6846d6076..3ecd5abf1a 100644
--- a/proto/gen/grpc-doc/store/README.md
+++ b/proto/gen/grpc-doc/store/README.md
@@ -2106,7 +2106,7 @@ MaskingExceptionPolicy is the allowlist of users who can access sensitive data.
| id | [string](#string) | | A unique identifier for a node in UUID format. |
| target | [ProtectionRule.Target](#bytebase-store-ProtectionRule-Target) | | |
| name_filter | [string](#string) | | The name of the branch/changelist or wildcard. |
-| create_allowed_roles | [string](#string) | repeated | The roles allowed to create branches or changelists. |
+| create_allowed_roles | [string](#string) | repeated | The roles allowed to create branches or changelists. Format: roles/OWNER. |
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 6ff9ff4101..f98925fe19 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -6448,7 +6448,7 @@ When paginating, all other parameters provided to `ListSchemaGroups` must match
| id | [string](#string) | | A unique identifier for a node in UUID format. |
| target | [ProtectionRule.Target](#bytebase-v1-ProtectionRule-Target) | | |
| name_filter | [string](#string) | | The name of the branch/changelist or wildcard. |
-| create_allowed_roles | [string](#string) | repeated | The roles allowed to create branches or changelists. |
+| create_allowed_roles | [string](#string) | repeated | The roles allowed to create branches or changelists. Format: roles/OWNER. |
diff --git a/proto/generated-go/store/project.pb.go b/proto/generated-go/store/project.pb.go
index e9cbbf2ca3..709d34e68b 100644
--- a/proto/generated-go/store/project.pb.go
+++ b/proto/generated-go/store/project.pb.go
@@ -128,6 +128,7 @@ type ProtectionRule struct {
// The name of the branch/changelist or wildcard.
NameFilter string `protobuf:"bytes,3,opt,name=name_filter,json=nameFilter,proto3" json:"name_filter,omitempty"`
// The roles allowed to create branches or changelists.
+ // Format: roles/OWNER.
CreateAllowedRoles []string `protobuf:"bytes,4,rep,name=create_allowed_roles,json=createAllowedRoles,proto3" json:"create_allowed_roles,omitempty"`
}
diff --git a/proto/generated-go/v1/project_service.pb.go b/proto/generated-go/v1/project_service.pb.go
index fcdbf53d11..d1e03a0c19 100644
--- a/proto/generated-go/v1/project_service.pb.go
+++ b/proto/generated-go/v1/project_service.pb.go
@@ -3744,6 +3744,7 @@ type ProtectionRule struct {
// The name of the branch/changelist or wildcard.
NameFilter string `protobuf:"bytes,3,opt,name=name_filter,json=nameFilter,proto3" json:"name_filter,omitempty"`
// The roles allowed to create branches or changelists.
+ // Format: roles/OWNER.
CreateAllowedRoles []string `protobuf:"bytes,4,rep,name=create_allowed_roles,json=createAllowedRoles,proto3" json:"create_allowed_roles,omitempty"`
}
diff --git a/proto/store/project.proto b/proto/store/project.proto
index f1d0aca48e..d68983dcea 100644
--- a/proto/store/project.proto
+++ b/proto/store/project.proto
@@ -25,5 +25,6 @@ message ProtectionRule {
string name_filter = 3;
// The roles allowed to create branches or changelists.
+ // Format: roles/OWNER.
repeated string create_allowed_roles = 4;
}
diff --git a/proto/v1/project_service.proto b/proto/v1/project_service.proto
index febbea9543..fd2be2f8ca 100644
--- a/proto/v1/project_service.proto
+++ b/proto/v1/project_service.proto
@@ -882,5 +882,6 @@ message ProtectionRule {
string name_filter = 3;
// The roles allowed to create branches or changelists.
+ // Format: roles/OWNER.
repeated string create_allowed_roles = 4;
}