Skip to content

Commit

Permalink
fix(api): allow users with read/exec permission to start/stop a run (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Dec 22, 2021
1 parent 26818b9 commit af45f1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,20 +273,20 @@ func (api *API) InitRouter() {
// Workflows run
r.Handle("/project/{permProjectKey}/runs", Scope(sdk.AuthConsumerScopeProject), r.GET(api.getWorkflowAllRunsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowNameAdvanced}/artifact/{artifactId}", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getDownloadArtifactHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowRunsHandler), r.POST(api.postWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowRunsHandler), r.POSTEXECUTE(api.postWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/branch/{branch}", Scope(sdk.AuthConsumerScopeRun), r.DELETE(api.deleteWorkflowRunsBranchHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/latest", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getLatestWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/tags", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowRunTagsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/num", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowRunNumHandler), r.POST(api.postWorkflowRunNumHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getWorkflowRunHandler), r.DELETE(api.deleteWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/stop", Scope(sdk.AuthConsumerScopeRun), r.POST(api.stopWorkflowRunHandler, MaintenanceAware()))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/vcs/resync", Scope(sdk.AuthConsumerScopeRun), r.POST(api.postResyncVCSWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/stop", Scope(sdk.AuthConsumerScopeRun), r.POSTEXECUTE(api.stopWorkflowRunHandler, MaintenanceAware()))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/vcs/resync", Scope(sdk.AuthConsumerScopeRun), r.POSTEXECUTE(api.postResyncVCSWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowNameAdvanced}/runs/{number}/artifacts", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getWorkflowRunArtifactsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowNameAdvanced}/runs/{number}/artifacts/links", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getWorkflowRunArtifactLinksHandler))
r.Handle("/project/{key}/workflows/{permWorkflowNameAdvanced}/runs/{number}/results", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getWorkflowRunResultsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeRunID}", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowNodeRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeRunID}/results", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getWorkflowNodeRunResultsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeRunID}/stop", Scope(sdk.AuthConsumerScopeRun), r.POST(api.stopWorkflowNodeRunHandler, MaintenanceAware()))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeRunID}/stop", Scope(sdk.AuthConsumerScopeRun), r.POSTEXECUTE(api.stopWorkflowNodeRunHandler, MaintenanceAware()))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeID}/history", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowNodeRunHistoryHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/{nodeName}/commits", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowCommitsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeRunID}/job/{runJobID}/info", Scope(sdk.AuthConsumerScopeRun), r.GET(api.getWorkflowNodeRunJobSpawnInfosHandler))
Expand Down
26 changes: 26 additions & 0 deletions engine/api/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ func (api *API) stopWorkflowRunHandler() service.Handler {
return err
}

consumer := getAPIConsumer(ctx)

// This POST exec handler should not be called by workers
if consumer.Worker != nil {
return sdk.WrapError(sdk.ErrForbidden, "not authorized for worker")
}

run, err := workflow.LoadRun(ctx, api.mustDB(), key, name, number, workflow.LoadRunOptions{
WithDeleted: true,
})
Expand Down Expand Up @@ -692,6 +699,13 @@ func (api *API) stopWorkflowNodeRunHandler() service.Handler {
return err
}

consumer := getAPIConsumer(ctx)

// This POST exec handler should not be called by workers
if consumer.Worker != nil {
return sdk.WrapError(sdk.ErrForbidden, "not authorized for worker")
}

p, err := project.Load(ctx, api.mustDB(), key, project.LoadOptions.WithVariables)
if err != nil {
return sdk.WrapError(err, "cannot load project")
Expand Down Expand Up @@ -812,6 +826,11 @@ func (api *API) postWorkflowRunHandler() service.Handler {

consumer := getAPIConsumer(ctx)

// This POST exec handler should not be called by workers
if consumer.Worker != nil {
return sdk.WrapError(sdk.ErrForbidden, "not authorized for worker")
}

telemetry.Current(ctx,
telemetry.Tag(telemetry.TagProjectKey, key),
telemetry.Tag(telemetry.TagWorkflow, name),
Expand Down Expand Up @@ -1609,6 +1628,13 @@ func (api *API) postResyncVCSWorkflowRunHandler() service.Handler {
return err
}

consumer := getAPIConsumer(ctx)

// This POST exec handler should not be called by workers
if consumer.Worker != nil {
return sdk.WrapError(sdk.ErrForbidden, "not authorized for worker")
}

proj, err := project.Load(ctx, api.mustDB(), key, project.LoadOptions.WithVariables)
if err != nil {
return sdk.WrapError(err, "cannot load project")
Expand Down

0 comments on commit af45f1e

Please sign in to comment.