From fb9b3221f1b1d8492f679c1c4453508026253b5e Mon Sep 17 00:00:00 2001 From: francois samin Date: Tue, 5 Apr 2022 14:18:02 +0200 Subject: [PATCH 1/2] fix(api): hooks uservice calls getWorkflowRunHandler Signed-off-by: francois samin --- engine/api/api_routes.go | 2 +- engine/api/workflow.go | 4 +-- engine/api/workflow_queue_test.go | 12 +++---- engine/api/workflow_run.go | 8 +++-- engine/api/workflow_run_test.go | 54 +++++++++++++++---------------- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/engine/api/api_routes.go b/engine/api/api_routes.go index a03a6dae2b..cea0fac14f 100644 --- a/engine/api/api_routes.go +++ b/engine/api/api_routes.go @@ -270,7 +270,7 @@ func (api *API) InitRouter() { 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/{permWorkflowNameAdvanced}/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.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/links", Scopes(sdk.AuthConsumerScopeRun, sdk.AuthConsumerScopeRunExecution), r.GET(api.getWorkflowRunArtifactLinksHandler)) diff --git a/engine/api/workflow.go b/engine/api/workflow.go index 5c24cb0c60..8e19012f67 100644 --- a/engine/api/workflow.go +++ b/engine/api/workflow.go @@ -84,7 +84,7 @@ func (api *API) getWorkflowsHandler() service.Handler { } func (api *API) setWorkflowURLs(w1 *sdk.Workflow) { - w1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowHandler, map[string]string{"key": w1.ProjectKey, "permWorkflowName": w1.Name}) + w1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowHandler, map[string]string{"key": w1.ProjectKey, "permWorkflowNameAdvanced": w1.Name}) w1.URLs.UIURL = api.Config.URL.UI + "/project/" + w1.ProjectKey + "/workflow/" + w1.Name for j := range w1.Runs { @@ -94,7 +94,7 @@ func (api *API) setWorkflowURLs(w1 *sdk.Workflow) { } func (api *API) setWorkflowRunURLs(r1 *sdk.WorkflowRun) { - r1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{"key": r1.Workflow.ProjectKey, "permWorkflowName": r1.Workflow.Name, "number": strconv.FormatInt(r1.Number, 10)}) + r1.URLs.APIURL = api.Config.URL.API + api.Router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{"key": r1.Workflow.ProjectKey, "permWorkflowNameAdvanced": r1.Workflow.Name, "number": strconv.FormatInt(r1.Number, 10)}) r1.URLs.UIURL = api.Config.URL.UI + "/project/" + r1.Workflow.ProjectKey + "/workflow/" + r1.Workflow.Name + "/run/" + strconv.FormatInt(r1.Number, 10) } diff --git a/engine/api/workflow_queue_test.go b/engine/api/workflow_queue_test.go index 88a658a243..79a14153ca 100644 --- a/engine/api/workflow_queue_test.go +++ b/engine/api/workflow_queue_test.go @@ -243,9 +243,9 @@ func testRunWorkflowForProject(t *testing.T, api *API, router *Router, proj *sdk cpt := 0 for { varsGet := map[string]string{ - "key": proj.Key, - "permWorkflowName": w1.Name, - "number": fmt.Sprintf("%d", wr.Number), + "key": proj.Key, + "permWorkflowNameAdvanced": w1.Name, + "number": fmt.Sprintf("%d", wr.Number), } uriGet := router.GetRoute("GET", api.getWorkflowRunHandler, varsGet) require.NotEmpty(t, uriGet) @@ -765,9 +765,9 @@ func Test_postWorkflowJobResultHandler(t *testing.T) { require.Equal(t, 204, rec.Code) uri = router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": ctx.project.Key, - "permWorkflowName": ctx.workflow.Name, - "number": fmt.Sprintf("%d", ctx.run.Number), + "key": ctx.project.Key, + "permWorkflowNameAdvanced": ctx.workflow.Name, + "number": fmt.Sprintf("%d", ctx.run.Number), }) req = assets.NewJWTAuthentifiedRequest(t, ctx.userToken, "GET", uri+"?withDetails=true", res) diff --git a/engine/api/workflow_run.go b/engine/api/workflow_run.go index 54e2c63e6f..4857a45594 100644 --- a/engine/api/workflow_run.go +++ b/engine/api/workflow_run.go @@ -265,7 +265,7 @@ func (api *API) getWorkflowRunHandler() service.Handler { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { vars := mux.Vars(r) key := vars["key"] - name := vars["permWorkflowName"] + name := vars["permWorkflowNameAdvanced"] number, err := requestVarInt(r, "number") if err != nil { return err @@ -311,9 +311,13 @@ func (api *API) getWorkflowRunHandler() service.Handler { func (api *API) deleteWorkflowRunHandler() service.Handler { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { + if isHooks(ctx) { + return sdk.WithStack(sdk.ErrForbidden) + } + vars := mux.Vars(r) key := vars["key"] - name := vars["permWorkflowName"] + name := vars["permWorkflowNameAdvanced"] number, err := requestVarInt(r, "number") if err != nil { return err diff --git a/engine/api/workflow_run_test.go b/engine/api/workflow_run_test.go index 89d566a71f..200dfcb38e 100644 --- a/engine/api/workflow_run_test.go +++ b/engine/api/workflow_run_test.go @@ -667,9 +667,9 @@ func Test_getWorkflowRunHandler(t *testing.T) { //Prepare request vars := map[string]string{ - "key": proj.Key, - "permWorkflowName": w1.Name, - "number": "9", + "key": proj.Key, + "permWorkflowNameAdvanced": w1.Name, + "number": "9", } uri := router.GetRoute("GET", api.getWorkflowRunHandler, vars) test.NotEmpty(t, uri) @@ -1429,9 +1429,9 @@ func Test_postWorkflowRunAsyncFailedHandler(t *testing.T) { for { t.Logf("Attempt getWorkflowRunHandler #%d", cpt) varsGet := map[string]string{ - "key": proj.Key, - "permWorkflowName": w1.Name, - "number": "1", + "key": proj.Key, + "permWorkflowNameAdvanced": w1.Name, + "number": "1", } uriGet := router.GetRoute("GET", api.getWorkflowRunHandler, varsGet) reqGet := assets.NewAuthentifiedRequest(t, u, pass, "GET", uriGet, nil) @@ -1969,9 +1969,9 @@ func Test_postWorkflowRunHandlerMutexRelease(t *testing.T) { try++ t.Logf("Attempt #%d on getWorkflowRunHandler for run 1", try) uri := router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": proj.Key, - "permWorkflowName": wkf.Name, - "number": "1", + "key": proj.Key, + "permWorkflowNameAdvanced": wkf.Name, + "number": "1", }) req := assets.NewAuthentifiedRequest(t, u, jwt, "GET", uri, nil) rec := httptest.NewRecorder() @@ -2015,9 +2015,9 @@ func Test_postWorkflowRunHandlerMutexRelease(t *testing.T) { try++ t.Logf("Attempt #%d on getWorkflowRunHandler for run 2", try) uri := router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": proj.Key, - "permWorkflowName": wkf.Name, - "number": "2", + "key": proj.Key, + "permWorkflowNameAdvanced": wkf.Name, + "number": "2", }) req := assets.NewAuthentifiedRequest(t, u, jwt, "GET", uri, nil) rec := httptest.NewRecorder() @@ -2064,9 +2064,9 @@ func Test_postWorkflowRunHandlerMutexRelease(t *testing.T) { try++ t.Logf("Attempt #%d on getWorkflowRunHandler for run 3", try) uri := router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": proj.Key, - "permWorkflowName": wkf.Name, - "number": "3", + "key": proj.Key, + "permWorkflowNameAdvanced": wkf.Name, + "number": "3", }) req := assets.NewAuthentifiedRequest(t, u, jwt, "GET", uri, nil) rec := httptest.NewRecorder() @@ -2110,9 +2110,9 @@ func Test_postWorkflowRunHandlerMutexRelease(t *testing.T) { try++ t.Logf("Attempt #%d on getWorkflowRunHandler for run 1", try) uri := router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": proj.Key, - "permWorkflowName": wkf.Name, - "number": "1", + "key": proj.Key, + "permWorkflowNameAdvanced": wkf.Name, + "number": "1", }) req := assets.NewAuthentifiedRequest(t, u, jwt, "GET", uri, nil) rec := httptest.NewRecorder() @@ -2142,9 +2142,9 @@ func Test_postWorkflowRunHandlerMutexRelease(t *testing.T) { try++ t.Logf("Attempt #%d on getWorkflowRunHandler for run 2", try) uri := router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": proj.Key, - "permWorkflowName": wkf.Name, - "number": "2", + "key": proj.Key, + "permWorkflowNameAdvanced": wkf.Name, + "number": "2", }) req := assets.NewAuthentifiedRequest(t, u, jwt, "GET", uri, nil) rec := httptest.NewRecorder() @@ -2174,9 +2174,9 @@ func Test_postWorkflowRunHandlerMutexRelease(t *testing.T) { try++ t.Logf("Attempt #%d on getWorkflowRunHandler for run 3", try) uri := router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{ - "key": proj.Key, - "permWorkflowName": wkf.Name, - "number": "3", + "key": proj.Key, + "permWorkflowNameAdvanced": wkf.Name, + "number": "3", }) req := assets.NewAuthentifiedRequest(t, u, jwt, "GET", uri, nil) rec := httptest.NewRecorder() @@ -2827,9 +2827,9 @@ func Test_deleteWorkflowRunHandler(t *testing.T) { //Prepare request vars = map[string]string{ - "key": proj.Key, - "permWorkflowName": w1.Name, - "number": fmt.Sprintf("%d", wr.Number), + "key": proj.Key, + "permWorkflowNameAdvanced": w1.Name, + "number": fmt.Sprintf("%d", wr.Number), } uri = router.GetRoute("GET", api.getWorkflowRunHandler, vars) test.NotEmpty(t, uri) From 361816c3e789e2844fd097f5b77deacd22dfe5fd Mon Sep 17 00:00:00 2001 From: francois samin Date: Tue, 5 Apr 2022 14:36:01 +0200 Subject: [PATCH 2/2] wip Signed-off-by: francois samin --- engine/api/workflow_run_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/engine/api/workflow_run_test.go b/engine/api/workflow_run_test.go index 200dfcb38e..789aa0d80f 100644 --- a/engine/api/workflow_run_test.go +++ b/engine/api/workflow_run_test.go @@ -2812,9 +2812,9 @@ func Test_deleteWorkflowRunHandler(t *testing.T) { assert.NoError(t, err) //Prepare request vars := map[string]string{ - "key": proj.Key, - "permWorkflowName": w1.Name, - "number": fmt.Sprintf("%d", wr.Number), + "key": proj.Key, + "permWorkflowNameAdvanced": w1.Name, + "number": fmt.Sprintf("%d", wr.Number), } uri := router.GetRoute("DELETE", api.deleteWorkflowRunHandler, vars) test.NotEmpty(t, uri)