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: validate change task status privilege by role & policy (#2244)
p0ny80490b20c022
diff --git a/server/issue.go b/server/issue.goindex db3f604cf2..5ba46335f9 100644--- a/server/issue.go+++ b/server/issue.go@@ -128,12 +128,6 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusBadRequest, "Malformed update issue request").SetInternal(err)}- if issuePatch.AssigneeID != nil {- if err := s.validateAssigneeRoleByID(ctx, *issuePatch.AssigneeID); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Cannot set assignee with user id %d", *issuePatch.AssigneeID)).SetInternal(err)- }- }-issue, err := s.store.GetIssueByID(ctx, id)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue ID when updating issue: %v", id)).SetInternal(err)@@ -142,6 +136,17 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to find issue ID to update: %d", id))}+ if issuePatch.AssigneeID != nil {+ environmentID := getActiveTaskEnvironmentID(issue.Pipeline)+ ok, err := s.canPrincipalBeAssignee(ctx, *issuePatch.AssigneeID, environmentID, issue.ProjectID, issue.Type)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to check if the assignee can be changed").SetInternal(err)+ }+ if !ok {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Cannot set assignee with user id %d", *issuePatch.AssigneeID)).SetInternal(err)+ }+ }+updatedIssue, err := s.store.PatchIssue(ctx, issuePatch)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to update issue with ID %d", id)).SetInternal(err)@@ -249,22 +254,6 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {})}-// Only allow Bot/Owner/DBA as the assignee, not Developer.-func (s *Server) validateAssigneeRoleByID(ctx context.Context, assigneeID int) error {- assignee, err := s.store.GetPrincipalByID(ctx, assigneeID)- if err != nil {- return err- }- if assignee == nil {- return fmt.Errorf("principal ID not found: %d", assigneeID)- }- if assignee.Role != api.Owner && assignee.Role != api.DBA {- return fmt.Errorf("%s is not allowed as assignee", assignee.Role)- }-- return nil-}-func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate, creatorID int) (*api.Issue, error) {// Run pre-condition check first to make sure all tasks are valid, otherwise we will create partial pipelines// since we are not creating pipeline/stage list/task list in a single transaction.@@ -274,11 +263,6 @@ func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate,return nil, echo.NewHTTPError(http.StatusBadRequest, "Failed to create issue, assignee missing")}- if err := s.validateAssigneeRoleByID(ctx, issueCreate.AssigneeID); err != nil {- return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Cannot set assignee with user id %d", issueCreate.AssigneeID)).SetInternal(err)- }-- // If frontend does not pass the stageList, we will generate it from backend.pipeline, err := s.createPipelineFromIssue(ctx, issueCreate, creatorID, issueCreate.ValidateOnly)if err != nil {return nil, err@@ -343,12 +327,13 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.Iif err != nil {return nil, err}-+ var environmentID int// Return an error if the issue has no task to be executedhasTask := falsefor _, stage := range pipelineCreate.StageList {if len(stage.TaskList) > 0 {hasTask = true+ environmentID = stage.EnvironmentIDbreak}}@@ -357,6 +342,14 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.Ireturn nil, echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)}+ ok, err := s.canPrincipalBeAssignee(ctx, creatorID, environmentID, issueCreate.ProjectID, issueCreate.Type)+ if err != nil {+ return nil, echo.NewHTTPError(http.StatusInternalServerError, "Failed to check if the assignee can be set for the new issue").SetInternal(err)+ }+ if !ok {+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Cannot set assignee with user id %d", issueCreate.AssigneeID)).SetInternal(err)+ }+// Create the pipeline, stages, and tasks.if validateOnly {return s.store.CreatePipelineValidateOnly(ctx, pipelineCreate, creatorID)@@ -1293,3 +1286,18 @@ func (s *Server) setTaskProgressForIssue(issue *api.Issue) {}}}++func getActiveTaskEnvironmentID(pipeline *api.Pipeline) int {+ for _, stage := range pipeline.StageList {+ for _, task := range stage.TaskList {+ if task.Status == api.TaskPendingApproval ||+ task.Status == api.TaskPending ||+ task.Status == api.TaskRunning ||+ task.Status == api.TaskCanceled {+ return stage.EnvironmentID+ }+ }+ }+ // use the last stage if all done+ return pipeline.StageList[len(pipeline.StageList)-1].EnvironmentID+}diff --git a/server/task.go b/server/task.goindex 9f3719848e..fe30fee9b6 100644--- a/server/task.go+++ b/server/task.go@@ -497,6 +497,48 @@ func (s *Server) patchTask(ctx context.Context, task *api.Task, taskPatch *api.Treturn taskPatched, nil}+func (s *Server) canPrincipalBeAssignee(ctx context.Context, principalID int, environmentID int, projectID int, issueType api.IssueType) (bool, error) {+ policy, err := s.store.GetPipelineApprovalPolicy(ctx, environmentID)+ if err != nil {+ return false, err+ }+ var groupValue *api.AssigneeGroupValue+ for i, group := range policy.AssigneeGroupList {+ if group.IssueType == issueType {+ groupValue = &policy.AssigneeGroupList[i].Value+ break+ }+ }+ if groupValue == nil {+ // default+ // the assignee group is the workspace owner and DBA role.+ principal, err := s.store.GetPrincipalByID(ctx, principalID)+ if err != nil {+ return false, common.Errorf(common.Internal, "failed to get principal by ID %d, error: %w", principalID, err)+ }+ if principal == nil {+ return false, common.Errorf(common.NotFound, "principal not found by ID %d", principalID)+ }+ if principal.Role == api.Owner || principal.Role == api.DBA {+ return true, nil+ }+ }+ // as the policy says, the assignee group is the project owner.+ if *groupValue == api.AssigneeGroupValueProjectOwner {+ member, err := s.store.GetProjectMember(ctx, &api.ProjectMemberFind{+ ProjectID: &projectID,+ PrincipalID: &principalID,+ })+ if err != nil {+ return false, common.Errorf(common.Internal, "failed to get project member by projectID %d, principalID %d, error: %w", projectID, principalID, err)+ }+ if member != nil && member.Role == string(api.Owner) {+ return true, nil+ }+ }+ return false, nil+}+// canPrincipalChangeTaskStatus validates if the principal has the privilege to update task status, judging from the principal role and the environment policy.func (s *Server) canPrincipalChangeTaskStatus(ctx context.Context, principalID int, task *api.Task) (bool, error) {// the workspace owner and DBA roles can always change task status.