Skip to content

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

chore: skip rollout iam checks & fix BatchSkipTasks (#10533)

p0nyae1a0781982a
diff --git a/backend/api/v1/acl_iam.go b/backend/api/v1/acl_iam.go
index a1744bdc68..17a2738b5f 100644
--- a/backend/api/v1/acl_iam.go
+++ b/backend/api/v1/acl_iam.go
@@ -281,6 +281,13 @@ func isSkippedMethod(fullMethod string) bool {
v1pb.IssueService_RejectIssue_FullMethodName,
v1pb.IssueService_RequestIssue_FullMethodName:
return true
+ // skip checking for the rollout-related.
+ // these are determined by the rollout policy.
+ case
+ v1pb.RolloutService_BatchCancelTaskRuns_FullMethodName,
+ v1pb.RolloutService_BatchSkipTasks_FullMethodName,
+ v1pb.RolloutService_BatchRunTasks_FullMethodName:
+ return true
// handled in the method because checking is complex.
case
v1pb.InstanceService_SearchInstances_FullMethodName,
@@ -363,7 +370,7 @@ func (*ACLInterceptor) getProjectIDsForChangelistService(_ context.Context, req
}
func (*ACLInterceptor) getProjectIDsForRolloutService(_ context.Context, req any) ([]string, error) {
- var projects, rollouts, plans, tasks, stages []string
+ var projects, rollouts, plans, tasks []string
switch r := req.(type) {
case *v1pb.GetRolloutRequest:
rollouts = append(rollouts, r.GetName())
@@ -381,12 +388,6 @@ func (*ACLInterceptor) getProjectIDsForRolloutService(_ context.Context, req any
plans = append(plans, r.GetParent())
case *v1pb.RunPlanChecksRequest:
plans = append(plans, r.GetName())
- case *v1pb.BatchRunTasksRequest:
- stages = append(stages, r.GetParent())
- case *v1pb.BatchSkipTasksRequest:
- stages = append(stages, r.GetParent())
- case *v1pb.BatchCancelTaskRunsRequest:
- tasks = append(tasks, r.GetParent())
}
var projectIDs []string
@@ -411,13 +412,6 @@ func (*ACLInterceptor) getProjectIDsForRolloutService(_ context.Context, req any
}
projectIDs = append(projectIDs, projectID)
}
- for _, stage := range stages {
- projectID, _, _, err := common.GetProjectIDRolloutIDMaybeStageID(stage)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse stage %q", stage)
- }
- projectIDs = append(projectIDs, projectID)
- }
for _, task := range tasks {
projectID, _, _, _, err := common.GetProjectIDRolloutIDMaybeStageIDMaybeTaskID(task)
if err != nil {
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 3eb5795365..6d73479634 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -613,14 +613,15 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, request *v1pb.Batch
taskByID[task.ID] = task
}
- principalID, ok := ctx.Value(common.PrincipalIDContextKey).(int)
+ user, ok := ctx.Value(common.UserContextKey).(*store.UserMessage)
if !ok {
- return nil, status.Errorf(codes.Internal, "principal ID not found")
+ return nil, status.Errorf(codes.Internal, "user not found")
}
var taskUIDs []int
var tasksToSkip []*store.TaskMessage
+ stageIDSet := map[int]struct{}{}
for _, task := range request.Tasks {
- _, _, _, taskID, err := common.GetProjectIDRolloutIDStageIDTaskID(task)
+ _, _, stageID, taskID, err := common.GetProjectIDRolloutIDStageIDTaskID(task)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
@@ -629,9 +630,33 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, request *v1pb.Batch
}
taskUIDs = append(taskUIDs, taskID)
tasksToSkip = append(tasksToSkip, taskByID[taskID])
+ stageIDSet[stageID] = struct{}{}
+ }
+
+ stages, err := s.store.ListStageV2(ctx, rolloutID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to list stages, error: %v", err)
+ }
+ stageMap := map[int]*store.StageMessage{}
+ for _, stage := range stages {
+ stageMap[stage.ID] = stage
+ }
+
+ for stageID := range stageIDSet {
+ stage, ok := stageMap[stageID]
+ if !ok {
+ return nil, status.Errorf(codes.Internal, "stage ID %v not found in stages of rollout %v", stageID, rolloutID)
+ }
+ ok, err = canUserSkipStageTasks(ctx, s.store, user, issue, stage.EnvironmentID)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to check if the user can run tasks, error: %v", err)
+ }
+ if !ok {
+ return nil, status.Errorf(codes.PermissionDenied, "not allowed to skip tasks in stage %q", stage.Name)
+ }
}
- if err := s.store.BatchSkipTasks(ctx, taskUIDs, request.Reason, principalID); err != nil {
+ if err := s.store.BatchSkipTasks(ctx, taskUIDs, request.Reason, user.ID); err != nil {
return nil, status.Errorf(codes.Internal, "failed to skip tasks, error: %v", err)
}
@@ -639,7 +664,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, request *v1pb.Batch
s.stateCfg.TaskSkippedOrDoneChan <- task.ID
}
- if err := s.activityManager.BatchCreateActivitiesForSkipTasks(ctx, tasksToSkip, issue, request.Reason, principalID); err != nil {
+ if err := s.activityManager.BatchCreateActivitiesForSkipTasks(ctx, tasksToSkip, issue, request.Reason, user.ID); err != nil {
slog.Error("failed to batch create activities for skipping tasks", log.BBError(err))
}
@@ -720,7 +745,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, request *v1pb.
return nil, status.Errorf(codes.Internal, "failed to check if the user can run tasks, error: %v", err)
}
if !ok {
- return nil, status.Errorf(codes.PermissionDenied, "Not allowed to run tasks")
+ return nil, status.Errorf(codes.PermissionDenied, "Not allowed to cancel tasks")
}
var taskRunIDs []int
@@ -1414,10 +1439,10 @@ func canUserRunStageTasks(ctx context.Context, s *store.Store, user *store.UserM
// 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 creator can cancel task runs.
- if user.ID == issue.Creator.ID {
- return true, nil
- }
+ return canUserRunStageTasks(ctx, s, user, issue, stageEnvironmentID)
+}
+
+func canUserSkipStageTasks(ctx context.Context, s *store.Store, user *store.UserMessage, issue *store.IssueMessage, stageEnvironmentID int) (bool, error) {
return canUserRunStageTasks(ctx, s, user, issue, stageEnvironmentID)
}