Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: use rollout policy (#8766)
p0ny32672d0832ff
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex d7a37ccd6b..27f5af7107 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -6,6 +6,7 @@ import ("fmt""log/slog""sort"+ "strings""time""github.com/google/go-cmp/cmp"@@ -1208,83 +1209,54 @@ func (s *RolloutService) createPipeline(ctx context.Context, project *store.Proj}// canUserRunStageTasks returns if a user can run the tasks in a stage.+// TODO(p0ny): support roles/LAST_APPROVER.func canUserRunStageTasks(ctx context.Context, s *store.Store, user *store.UserMessage, issue *store.IssueMessage, stageEnvironmentID int) (bool, error) {// the workspace owner and DBA roles can always run tasks.if user.Role == api.Owner || user.Role == api.DBA {return true, nil}- groupValue, err := getGroupValueForIssueTypeEnvironment(ctx, s, issue.Type, stageEnvironmentID)++ p, err := s.GetRolloutPolicy(ctx, stageEnvironmentID)if err != nil {- return false, errors.Wrapf(err, "failed to get assignee group value for issueID %d", issue.UID)+ return false, errors.Wrapf(err, "failed to get rollout policy for stageEnvironmentID %d", stageEnvironmentID)}- // as the policy says, the project owner has the privilege to run.- if groupValue == api.AssigneeGroupValueProjectOwner {- policy, err := s.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &issue.Project.UID})- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get project %d policy", issue.Project.UID)++ policy, err := s.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &issue.Project.UID})+ if err != nil {+ return false, common.Wrapf(err, common.Internal, "failed to get project %d policy", issue.Project.UID)+ }++ allowedProjectRoles := map[api.Role]bool{}+ for _, role := range p.ProjectRoles {+ allowedProjectRoles[api.Role(strings.TrimPrefix(role, "roles/"))] = true+ }+ for _, binding := range policy.Bindings {+ if !allowedProjectRoles[binding.Role] {+ continue}- for _, binding := range policy.Bindings {- if binding.Role != api.Owner {- continue+ for _, member := range binding.Members {+ if member.ID == user.ID {+ return true, nil}- for _, member := range binding.Members {- if member.ID == user.ID {- return true, nil- }+ }+ }++ if user.ID == issue.Creator.ID {+ for _, issueRole := range p.IssueRoles {+ if issueRole == "roles/CREATOR" {+ return true, nil}}}+return false, nil}// canUserCancelStageTaskRun returns if a user can cancel the task runs in a stage.func canUserCancelStageTaskRun(ctx context.Context, s *store.Store, user *store.UserMessage, issue *store.IssueMessage, stageEnvironmentID int) (bool, error) {- // the workspace owner and DBA roles can always cancel task runs.- if user.Role == api.Owner || user.Role == api.DBA {- return true, nil- }// The creator can cancel task runs.if user.ID == issue.Creator.ID {return true, nil}- groupValue, err := getGroupValueForIssueTypeEnvironment(ctx, s, issue.Type, stageEnvironmentID)- if err != nil {- return false, errors.Wrapf(err, "failed to get assignee group value for issueID %d", issue.UID)- }- // as the policy says, the project owner has the privilege to cancel.- if groupValue == api.AssigneeGroupValueProjectOwner {- policy, err := s.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{UID: &issue.Project.UID})- if err != nil {- return false, common.Wrapf(err, common.Internal, "failed to get project %d policy", issue.Project.UID)- }- for _, binding := range policy.Bindings {- if binding.Role != api.Owner {- continue- }- for _, member := range binding.Members {- if member.ID == user.ID {- return true, nil- }- }- }- }- return false, nil-}--func getGroupValueForIssueTypeEnvironment(ctx context.Context, s *store.Store, issueType api.IssueType, environmentID int) (api.AssigneeGroupValue, error) {- defaultGroupValue := api.AssigneeGroupValueWorkspaceOwnerOrDBA- policy, err := s.GetPipelineApprovalPolicy(ctx, environmentID)- if err != nil {- return defaultGroupValue, errors.Wrapf(err, "failed to get pipeline approval policy by environmentID %d", environmentID)- }- if policy == nil {- return defaultGroupValue, nil- }-- for _, assigneeGroup := range policy.AssigneeGroupList {- if assigneeGroup.IssueType == issueType {- return assigneeGroup.Value, nil- }- }- return defaultGroupValue, nil+ return canUserRunStageTasks(ctx, s, user, issue, stageEnvironmentID)}diff --git a/backend/migrator/migration/dev/LATEST_DATA.sql b/backend/migrator/migration/dev/LATEST_DATA.sqlindex 1ccb8373c3..6647b8bfe6 100644--- a/backend/migrator/migration/dev/LATEST_DATA.sql+++ b/backend/migrator/migration/dev/LATEST_DATA.sql@@ -58,8 +58,8 @@ VALUES'ENVIRONMENT',101,TRUE,- 'bb.policy.pipeline-approval',- '{"value":"MANUAL_APPROVAL_NEVER"}'+ 'bb.policy.rollout',+ '{"automatic": true}');INSERT INTO@@ -81,8 +81,8 @@ VALUES'ENVIRONMENT',102,TRUE,- 'bb.policy.pipeline-approval',- '{"value":"MANUAL_APPROVAL_ALWAYS"}'+ 'bb.policy.rollout',+ '{"automatic": false, "workspaceRoles": ["roles/OWNER","roles/DBA"]}');INSERT INTOdiff --git a/backend/migrator/migration/prod/LATEST_DATA.sql b/backend/migrator/migration/prod/LATEST_DATA.sqlindex 7a11d4b044..7c734e1df5 100644--- a/backend/migrator/migration/prod/LATEST_DATA.sql+++ b/backend/migrator/migration/prod/LATEST_DATA.sql@@ -58,8 +58,8 @@ VALUES'ENVIRONMENT',101,TRUE,- 'bb.policy.pipeline-approval',- '{"value":"MANUAL_APPROVAL_NEVER"}'+ 'bb.policy.rollout',+ '{"automatic": true}');INSERT INTO@@ -81,8 +81,8 @@ VALUES'ENVIRONMENT',102,TRUE,- 'bb.policy.pipeline-approval',- '{"value":"MANUAL_APPROVAL_NEVER"}'+ 'bb.policy.rollout',+ '{"automatic": true}');ALTER SEQUENCE policy_id_seq RESTART WITH 103;diff --git a/backend/runner/taskrun/schedulerv2.go b/backend/runner/taskrun/schedulerv2.goindex 4b228013d0..89f473d1bf 100644--- a/backend/runner/taskrun/schedulerv2.go+++ b/backend/runner/taskrun/schedulerv2.go@@ -106,11 +106,11 @@ func (s *SchedulerV2) scheduleAutoRolloutTasks(ctx context.Context) error {var autoRolloutEnvironmentIDs []intfor _, environment := range environments {- policy, err := s.store.GetPipelineApprovalPolicy(ctx, environment.UID)+ policy, err := s.store.GetRolloutPolicy(ctx, environment.UID)if err != nil {- return errors.Wrapf(err, "failed to get approval policy for environment ID %d", environment.UID)+ return errors.Wrapf(err, "failed to get rollout policy for environment ID %d", environment.UID)}- if policy.Value != api.PipelineApprovalValueManualNever {+ if !policy.Automatic {continue}autoRolloutEnvironmentIDs = append(autoRolloutEnvironmentIDs, environment.UID)diff --git a/backend/store/policy.go b/backend/store/policy.goindex f56d6d67b0..a36989e0c2 100644--- a/backend/store/policy.go+++ b/backend/store/policy.go@@ -34,25 +34,29 @@ func (s *Store) GetBackupPlanPolicyByEnvID(ctx context.Context, environmentID inreturn api.UnmarshalBackupPlanPolicy(policy.Payload)}-// GetPipelineApprovalPolicy will get the pipeline approval policy for an environment.-func (s *Store) GetPipelineApprovalPolicy(ctx context.Context, environmentID int) (*api.PipelineApprovalPolicy, error) {+func (s *Store) GetRolloutPolicy(ctx context.Context, environmentID int) (*storepb.RolloutPolicy, error) {resourceType := api.PolicyResourceTypeEnvironment- pType := api.PolicyTypePipelineApproval+ pType := api.PolicyTypeRolloutpolicy, err := s.GetPolicyV2(ctx, &FindPolicyMessage{ResourceType: &resourceType,ResourceUID: &environmentID,Type: &pType,})if err != nil {- return nil, err+ return nil, errors.Wrapf(err, "failed to get policy")}if policy == nil {- return &api.PipelineApprovalPolicy{- Value: api.PipelineApprovalValueManualAlways,+ return &storepb.RolloutPolicy{+ Automatic: true,}, nil}- return api.UnmarshalPipelineApprovalPolicy(policy.Payload)+ p := &storepb.RolloutPolicy{}+ if err := protojson.Unmarshal([]byte(policy.Payload), p); err != nil {+ return nil, errors.Wrapf(err, "failed to unmarshal rollout policy")+ }++ return p, nil}// GetSQLReviewPolicy will get the SQL review policy for an environment.diff --git a/backend/tests/tests.go b/backend/tests/tests.goindex 345b1d2360..2ceb4b342d 100644--- a/backend/tests/tests.go+++ b/backend/tests/tests.go@@ -273,10 +273,10 @@ func (ctl *controller) StartServerWithExternalPg(ctx context.Context, config *coif _, err := ctl.orgPolicyServiceClient.CreatePolicy(metaCtx, &v1pb.CreatePolicyRequest{Parent: fmt.Sprintf("environments/%s", environment),Policy: &v1pb.Policy{- Type: v1pb.PolicyType_DEPLOYMENT_APPROVAL,- Policy: &v1pb.Policy_DeploymentApprovalPolicy{- DeploymentApprovalPolicy: &v1pb.DeploymentApprovalPolicy{- DefaultStrategy: v1pb.ApprovalStrategy_MANUAL,+ Type: v1pb.PolicyType_ROLLOUT_POLICY,+ Policy: &v1pb.Policy_RolloutPolicy{+ RolloutPolicy: &v1pb.RolloutPolicy{+ Automatic: false,},},},