0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 15:23:14 -05:00

Harden runner updateTask and updateLog api (#32462)

Per proposal https://github.com/go-gitea/gitea/issues/32461

(cherry picked from commit f888e45432ccb86b18e6709fbd25223e07f2c422)
This commit is contained in:
ChristopherHX 2024-11-11 05:58:37 +01:00 committed by Earl Warren
parent dd3c4d7096
commit 7f51210672
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 10 additions and 2 deletions

View file

@ -341,7 +341,7 @@ func UpdateTask(ctx context.Context, task *ActionTask, cols ...string) error {
// UpdateTaskByState updates the task by the state. // UpdateTaskByState updates the task by the state.
// It will always update the task if the state is not final, even there is no change. // It will always update the task if the state is not final, even there is no change.
// So it will update ActionTask.Updated to avoid the task being judged as a zombie task. // So it will update ActionTask.Updated to avoid the task being judged as a zombie task.
func UpdateTaskByState(ctx context.Context, state *runnerv1.TaskState) (*ActionTask, error) { func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.TaskState) (*ActionTask, error) {
stepStates := map[int64]*runnerv1.StepState{} stepStates := map[int64]*runnerv1.StepState{}
for _, v := range state.Steps { for _, v := range state.Steps {
stepStates[v.Id] = v stepStates[v.Id] = v
@ -360,6 +360,8 @@ func UpdateTaskByState(ctx context.Context, state *runnerv1.TaskState) (*ActionT
return nil, err return nil, err
} else if !has { } else if !has {
return nil, util.ErrNotExist return nil, util.ErrNotExist
} else if runnerID != task.RunnerID {
return nil, fmt.Errorf("invalid runner for task")
} }
if task.Status.IsDone() { if task.Status.IsDone() {

View file

@ -177,7 +177,9 @@ func (s *Service) UpdateTask(
ctx context.Context, ctx context.Context,
req *connect.Request[runnerv1.UpdateTaskRequest], req *connect.Request[runnerv1.UpdateTaskRequest],
) (*connect.Response[runnerv1.UpdateTaskResponse], error) { ) (*connect.Response[runnerv1.UpdateTaskResponse], error) {
task, err := actions_model.UpdateTaskByState(ctx, req.Msg.State) runner := GetRunner(ctx)
task, err := actions_model.UpdateTaskByState(ctx, runner.ID, req.Msg.State)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "update task: %v", err) return nil, status.Errorf(codes.Internal, "update task: %v", err)
} }
@ -239,11 +241,15 @@ func (s *Service) UpdateLog(
ctx context.Context, ctx context.Context,
req *connect.Request[runnerv1.UpdateLogRequest], req *connect.Request[runnerv1.UpdateLogRequest],
) (*connect.Response[runnerv1.UpdateLogResponse], error) { ) (*connect.Response[runnerv1.UpdateLogResponse], error) {
runner := GetRunner(ctx)
res := connect.NewResponse(&runnerv1.UpdateLogResponse{}) res := connect.NewResponse(&runnerv1.UpdateLogResponse{})
task, err := actions_model.GetTaskByID(ctx, req.Msg.TaskId) task, err := actions_model.GetTaskByID(ctx, req.Msg.TaskId)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "get task: %v", err) return nil, status.Errorf(codes.Internal, "get task: %v", err)
} else if runner.ID != task.RunnerID {
return nil, status.Errorf(codes.Internal, "invalid runner for task")
} }
ack := task.LogLength ack := task.LogLength