From 4421b590a8a56df2e16a463b1b444c81385a0c9e Mon Sep 17 00:00:00 2001 From: Richard LT Date: Mon, 22 Nov 2021 10:18:29 +0100 Subject: [PATCH] fix(api): take care of integration model requirements for region check (#6021) --- engine/api/environment_import.go | 6 +++-- engine/api/project.go | 25 ++++++++----------- engine/api/project/loader.go | 1 - engine/api/workflow/execute_node_run.go | 2 +- engine/api/workflow/run_workflow.go | 6 ++--- ui/src/app/service/project/project.service.ts | 6 ++--- 6 files changed, 20 insertions(+), 26 deletions(-) diff --git a/engine/api/environment_import.go b/engine/api/environment_import.go index 76e4f28ed3..4bc2069a3d 100644 --- a/engine/api/environment_import.go +++ b/engine/api/environment_import.go @@ -82,8 +82,10 @@ func (api *API) importNewEnvironmentHandler() service.Handler { vars := mux.Vars(r) key := vars[permProjectKey] - proj, err := project.Load(ctx, api.mustDB(), key, project.LoadOptions.Default, - project.LoadOptions.WithGroups, project.LoadOptions.WithPermission) + proj, err := project.Load(ctx, api.mustDB(), key, + project.LoadOptions.Default, + project.LoadOptions.WithGroups, + ) if err != nil { return sdk.WrapError(err, "cannot load %s", key) } diff --git a/engine/api/project.go b/engine/api/project.go index 9961a39bf6..409e629e87 100644 --- a/engine/api/project.go +++ b/engine/api/project.go @@ -67,7 +67,6 @@ func (api *API) getProjectsHandler_FilterByRepo(ctx context.Context, w http.Resp } opts := []project.LoadOptionFunc{ - project.LoadOptions.WithPermission, project.LoadOptions.WithApplications, project.LoadOptions.WithWorkflows, } @@ -139,10 +138,7 @@ func (api *API) getProjectsHandler() service.Handler { } } - opts := []project.LoadOptionFunc{ - project.LoadOptions.WithPermission, - } - + var opts []project.LoadOptionFunc if withIcon { opts = append(opts, project.LoadOptions.WithIcon) } @@ -273,7 +269,6 @@ func (api *API) getProjectHandler() service.Handler { withEnvironments := service.FormBool(r, "withEnvironments") withEnvironmentNames := service.FormBool(r, "withEnvironmentNames") withGroups := service.FormBool(r, "withGroups") - withPermission := service.FormBool(r, "withPermission") withKeys := service.FormBool(r, "withKeys") withWorkflows := service.FormBool(r, "withWorkflows") withWorkflowNames := service.FormBool(r, "withWorkflowNames") @@ -308,9 +303,6 @@ func (api *API) getProjectHandler() service.Handler { if withGroups { opts = append(opts, project.LoadOptions.WithGroups) } - if withPermission { - opts = append(opts, project.LoadOptions.WithPermission) - } if withKeys { opts = append(opts, project.LoadOptions.WithKeys) } @@ -432,12 +424,16 @@ func (api *API) putProjectLabelsHandler() service.Handler { return sdk.WithStack(err) } - p, errP := project.Load(ctx, db, key, - project.LoadOptions.WithLabels, project.LoadOptions.WithWorkflowNames, project.LoadOptions.WithVariables, + p, err := project.Load(ctx, db, key, + project.LoadOptions.WithLabels, + project.LoadOptions.WithWorkflowNames, + project.LoadOptions.WithVariables, project.LoadOptions.WithFavorites(getAPIConsumer(ctx).AuthentifiedUser.ID), - project.LoadOptions.WithKeys, project.LoadOptions.WithPermission, project.LoadOptions.WithIntegrations) - if errP != nil { - return sdk.WrapError(errP, "putProjectLabelsHandler> Cannot load project updated from db") + project.LoadOptions.WithKeys, + project.LoadOptions.WithIntegrations, + ) + if err != nil { + return sdk.WrapError(err, "cannot load project updated from db") } p.Permissions.Readable = true @@ -605,7 +601,6 @@ func (api *API) postProjectHandler() service.Handler { project.LoadOptions.WithWorkflowNames, project.LoadOptions.WithFavorites(consumer.AuthentifiedUser.ID), project.LoadOptions.WithKeys, - project.LoadOptions.WithPermission, project.LoadOptions.WithIntegrations, project.LoadOptions.WithVariables, ) diff --git a/engine/api/project/loader.go b/engine/api/project/loader.go index 2ebfda98be..ae42f0594a 100644 --- a/engine/api/project/loader.go +++ b/engine/api/project/loader.go @@ -31,7 +31,6 @@ var LoadOptions = struct { WithEnvironments LoadOptionFunc WithEnvironmentNames LoadOptionFunc WithGroups LoadOptionFunc - WithPermission LoadOptionFunc WithApplicationVariables LoadOptionFunc WithApplicationKeys LoadOptionFunc WithApplicationWithDeploymentStrategies LoadOptionFunc diff --git a/engine/api/workflow/execute_node_run.go b/engine/api/workflow/execute_node_run.go index 46a87180ac..573e488542 100644 --- a/engine/api/workflow/execute_node_run.go +++ b/engine/api/workflow/execute_node_run.go @@ -499,7 +499,7 @@ jobLoop: } if exist := featureflipping.Exists(ctx, gorpmapping.Mapper, db, sdk.FeatureRegion); exist { - if err := checkJobRegion(ctx, db, wr.Workflow.ProjectKey, wr.Workflow.Name, *job); err != nil { + if err := checkJobRegion(ctx, db, wr.Workflow.ProjectKey, wr.Workflow.Name, jobRequirements); err != nil { spawnErrs.Append(sdk.ErrRegionNotAllowed) } } diff --git a/engine/api/workflow/run_workflow.go b/engine/api/workflow/run_workflow.go index 1d3e535e26..3db34e944f 100644 --- a/engine/api/workflow/run_workflow.go +++ b/engine/api/workflow/run_workflow.go @@ -204,7 +204,7 @@ func CheckRegion(ctx context.Context, db gorp.SqlExecutor, wf sdk.Workflow) erro for _, p := range wf.Pipelines { for _, s := range p.Stages { for _, j := range s.Jobs { - if err := checkJobRegion(ctx, db, wf.ProjectKey, wf.Name, j); err != nil { + if err := checkJobRegion(ctx, db, wf.ProjectKey, wf.Name, j.Action.Requirements); err != nil { return err } } @@ -213,8 +213,8 @@ func CheckRegion(ctx context.Context, db gorp.SqlExecutor, wf sdk.Workflow) erro return nil } -func checkJobRegion(ctx context.Context, db gorp.SqlExecutor, projKey, wName string, j sdk.Job) error { - for _, req := range j.Action.Requirements { +func checkJobRegion(ctx context.Context, db gorp.SqlExecutor, projKey, wName string, jobRequirements sdk.RequirementList) error { + for _, req := range jobRequirements { if req.Type != sdk.RegionRequirement { continue } diff --git a/ui/src/app/service/project/project.service.ts b/ui/src/app/service/project/project.service.ts index 3ef6e0eec6..52a1b6e5d7 100644 --- a/ui/src/app/service/project/project.service.ts +++ b/ui/src/app/service/project/project.service.ts @@ -27,13 +27,11 @@ export class ProjectService { if (Array.isArray(opts) && opts.length) { opts = opts.concat([ - new LoadOpts('withGroups', 'groups'), - new LoadOpts('withPermission', 'permission') + new LoadOpts('withGroups', 'groups') ]); } else { opts = [ - new LoadOpts('withGroups', 'groups'), - new LoadOpts('withPermission', 'permission') + new LoadOpts('withGroups', 'groups') ]; } opts.push(new LoadOpts('withFeatures', 'features'));