Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: branch protection backend (#8476)
Danny Xu89319e0cfbce
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.goindex 62ea03fe36..7616a23274 100644--- a/backend/api/v1/project_service.go+++ b/backend/api/v1/project_service.go@@ -32,6 +32,7 @@ import (webhookPlugin "github.com/bytebase/bytebase/backend/plugin/webhook""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1")@@ -2082,13 +2083,91 @@ func (s *ProjectService) GetSchemaGroup(ctx context.Context, request *v1pb.GetSc}// GetProjectProtectionRules gets a project protection rules.-func (*ProjectService) GetProjectProtectionRules(_ context.Context, _ *v1pb.GetProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {- return nil, nil+func (s *ProjectService) GetProjectProtectionRules(ctx context.Context, request *v1pb.GetProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {+ projectResourceID, err := common.TrimSuffix(request.Name, common.ProtectionRulesSuffix)+ if err != nil {+ return nil, status.Errorf(codes.InvalidArgument, err.Error())+ }+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{+ ResourceID: &projectResourceID,+ })+ if err != nil {+ return nil, status.Errorf(codes.Internal, err.Error())+ }+ if project == nil {+ return nil, status.Errorf(codes.NotFound, "project %q not found", projectResourceID)+ }+ if project.Deleted {+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectResourceID)+ }++ resp := convertProtectionRules(project)+ return resp, nil}// UpdateProjectProtectionRules updates a project protection rules.-func (*ProjectService) UpdateProjectProtectionRules(_ context.Context, _ *v1pb.UpdateProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {- return nil, nil+func (s *ProjectService) UpdateProjectProtectionRules(ctx context.Context, request *v1pb.UpdateProjectProtectionRulesRequest) (*v1pb.ProtectionRules, error) {+ if request.ProtectionRules == nil {+ return nil, status.Errorf(codes.InvalidArgument, "protection rules must be set")+ }+ projectResourceID, err := common.TrimSuffix(request.ProtectionRules.Name, common.ProtectionRulesSuffix)+ if err != nil {+ return nil, status.Errorf(codes.InvalidArgument, err.Error())+ }+ project, err := s.store.GetProjectV2(ctx, &store.FindProjectMessage{+ ResourceID: &projectResourceID,+ })+ if err != nil {+ return nil, status.Errorf(codes.Internal, err.Error())+ }+ if project == nil {+ return nil, status.Errorf(codes.NotFound, "project %q not found", projectResourceID)+ }+ if project.Deleted {+ return nil, status.Errorf(codes.NotFound, "project %q has been deleted", projectResourceID)+ }++ var rules []*storepb.ProtectionRule+ for _, rule := range request.ProtectionRules.Rules {+ rules = append(rules, &storepb.ProtectionRule{+ Id: rule.Id,+ Target: storepb.ProtectionRule_Target(rule.Target),+ NameFilter: rule.NameFilter,+ CreateAllowedRoles: rule.CreateAllowedRoles,+ })+ }++ principalID := ctx.Value(common.PrincipalIDContextKey).(int)+ setting := project.Setting+ setting.ProtectionRules = rules+ project, err = s.store.UpdateProjectV2(ctx, &store.UpdateProjectMessage{+ UpdaterID: principalID,+ ResourceID: project.ResourceID,+ Setting: setting,+ })+ if err != nil {+ return nil, status.Errorf(codes.Internal, err.Error())+ }++ resp := convertProtectionRules(project)+ return resp, nil+}++func convertProtectionRules(project *store.ProjectMessage) *v1pb.ProtectionRules {+ resp := &v1pb.ProtectionRules{+ Name: fmt.Sprintf("%s%s%s", common.ProjectNamePrefix, project.ResourceID, common.ProtectionRulesSuffix),+ }+ if project.Setting != nil {+ for _, rule := range project.Setting.ProtectionRules {+ resp.Rules = append(resp.Rules, &v1pb.ProtectionRule{+ Id: rule.Id,+ Target: v1pb.ProtectionRule_Target(rule.Target),+ NameFilter: rule.NameFilter,+ CreateAllowedRoles: rule.CreateAllowedRoles,+ })+ }+ }+ return resp}func (s *ProjectService) convertStoreToAPIDatabaseGroupFull(ctx context.Context, databaseGroup *store.DatabaseGroupMessage, projectResourceID string) (*v1pb.DatabaseGroup, error) {diff --git a/backend/common/resource_name.go b/backend/common/resource_name.goindex f669119bcd..29a050a667 100644--- a/backend/common/resource_name.go+++ b/backend/common/resource_name.go@@ -45,10 +45,11 @@ const (DeploymentConfigPrefix = "deploymentConfigs/"ChangelistsPrefix = "changelists/"- BackupSettingSuffix = "/backupSetting"- SchemaSuffix = "/schema"- MetadataSuffix = "/metadata"- GitOpsInfoSuffix = "/gitOpsInfo"+ BackupSettingSuffix = "/backupSetting"+ SchemaSuffix = "/schema"+ MetadataSuffix = "/metadata"+ GitOpsInfoSuffix = "/gitOpsInfo"+ ProtectionRulesSuffix = "/protectionRules")// GetProjectID returns the project ID from a resource name.diff --git a/backend/store/project.go b/backend/store/project.goindex 0f8c727159..acd0b71d03 100644--- a/backend/store/project.go+++ b/backend/store/project.go@@ -7,9 +7,11 @@ import ("strings""github.com/pkg/errors"+ "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"api "github.com/bytebase/bytebase/backend/legacyapi"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")// ProjectMessage is the message for project.@@ -24,6 +26,7 @@ type ProjectMessage struct {SchemaChangeType api.ProjectSchemaChangeTypeWebhooks []*ProjectWebhookMessageDataClassificationConfigID string+ Setting *storepb.Project// The following fields are output only and not used for create().UID intDeleted bool@@ -50,6 +53,7 @@ type UpdateProjectMessage struct {Workflow *api.ProjectWorkflowTypeSchemaChangeType *api.ProjectSchemaChangeTypeDataClassificationConfigID *string+ Setting *storepb.ProjectDelete *bool}@@ -134,6 +138,13 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, creif err != nil {return nil, err}+ if create.Setting == nil {+ create.Setting = &storepb.Project{}+ }+ payload, err := protojson.Marshal(create.Setting)+ if err != nil {+ return nil, err+ }tx, err := s.db.BeginTx(ctx, nil)if err != nil {@@ -151,6 +162,7 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, creDBNameTemplate: create.DBNameTemplate,SchemaChangeType: create.SchemaChangeType,DataClassificationConfigID: create.DataClassificationConfigID,+ Setting: create.Setting,}if err := tx.QueryRowContext(ctx, `INSERT INTO project (@@ -164,9 +176,10 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, cretenant_mode,db_name_template,schema_change_type,- data_classification_config_id+ data_classification_config_id,+ setting)- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)RETURNING id`,creatorID,@@ -180,6 +193,7 @@ func (s *Store) CreateProjectV2(ctx context.Context, create *ProjectMessage, crecreate.DBNameTemplate,create.SchemaChangeType,create.DataClassificationConfigID,+ payload,).Scan(&project.UID,); err != nil {@@ -263,10 +277,19 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePrif v := patch.DataClassificationConfigID; v != nil {set, args = append(set, fmt.Sprintf("data_classification_config_id = $%d", len(args)+1)), append(args, *v)}+ if v := patch.Setting; v != nil {+ payload, err := protojson.Marshal(patch.Setting)+ if err != nil {+ return nil, err+ }+ set, args = append(set, fmt.Sprintf("setting = $%d", len(args)+1)), append(args, payload)+ }+args = append(args, patch.ResourceID)project := &ProjectMessage{}var rowStatus string+ var payload []byteif err := tx.QueryRowContext(ctx, fmt.Sprintf(`UPDATE projectSET `+strings.Join(set, ", ")+`@@ -282,6 +305,7 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePrdb_name_template,schema_change_type,data_classification_config_id,+ setting,row_status`, len(args)),args...,@@ -296,6 +320,7 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePr&project.DBNameTemplate,&project.SchemaChangeType,&project.DataClassificationConfigID,+ &payload,&rowStatus,); err != nil {if err == sql.ErrNoRows {@@ -303,6 +328,12 @@ func (s *Store) updateProjectImplV2(ctx context.Context, tx *Tx, patch *UpdatePr}return nil, err}+ setting := &storepb.Project{}+ if err := protojsonUnmarshaler.Unmarshal(payload, setting); err != nil {+ return nil, err+ }+ project.Setting = setting+projectWebhooks, err := s.findProjectWebhookImplV2(ctx, tx, &FindProjectWebhookMessage{ProjectID: &project.UID})if err != nil {return nil, err@@ -337,6 +368,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProjectdb_name_template,schema_change_type,data_classification_config_id,+ setting,row_statusFROM projectWHERE `+strings.Join(where, " AND "),@@ -349,6 +381,7 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProjectfor rows.Next() {var projectMessage ProjectMessage+ var payload []bytevar rowStatus stringif err := rows.Scan(&projectMessage.UID,@@ -361,10 +394,16 @@ func (s *Store) listProjectImplV2(ctx context.Context, tx *Tx, find *FindProject&projectMessage.DBNameTemplate,&projectMessage.SchemaChangeType,&projectMessage.DataClassificationConfigID,+ &payload,&rowStatus,); err != nil {return nil, err}+ setting := &storepb.Project{}+ if err := protojsonUnmarshaler.Unmarshal(payload, setting); err != nil {+ return nil, err+ }+ projectMessage.Setting = settingprojectMessage.Deleted = convertRowStatusToDeleted(rowStatus)projectMessages = append(projectMessages, &projectMessage)}