Skip to content

Commit

Permalink
fix: Enforce policy checks for overridden apply reqs (#3960)
Browse files Browse the repository at this point in the history
* Enforce policy checks for overriden apply reqs

* Another take on the fix

This new version only includes the 'policies_passed' req back if
policy checks are enabled for the project.

* Fix test

* Fix comment

* Fix spelling

---------

Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
danielfm and jamengual authored Dec 12, 2023
1 parent d1661da commit 95c5fe0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions server/core/config/valid/global_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ func (g GlobalCfg) MergeProjectCfg(log logging.SimpleLogging, repoID string, pro
if proj.ApplyRequirements != nil {
log.Debug("overriding server-defined %s with repo settings: [%s]", ApplyRequirementsKey, strings.Join(proj.ApplyRequirements, ","))
applyReqs = proj.ApplyRequirements

// Preserve policies_passed req if policy check is enabled
if policyCheck {
applyReqs = append(applyReqs, PoliciesPassedCommandReq)
}
}
case ImportRequirementsKey:
if proj.ImportRequirements != nil {
Expand Down
63 changes: 63 additions & 0 deletions server/core/config/valid/global_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,69 @@ repos:
CustomPolicyCheck: false,
},
},
"repo-side apply reqs should include non-overrideable 'policies_passed' req when overridden and policies enabled": {
gCfg: `
repos:
- id: /.*/
allowed_overrides: [apply_requirements]
apply_requirements: [approved]
policy_check: true
`,
repoID: "github.com/owner/repo",
proj: valid.Project{
Dir: ".",
Workspace: "default",
PlanRequirements: []string{},
ApplyRequirements: []string{"mergeable"},
ImportRequirements: []string{},
},
repoWorkflows: nil,
exp: valid.MergedProjectCfg{
PlanRequirements: []string{},
ApplyRequirements: []string{"mergeable", "policies_passed"},
ImportRequirements: []string{},
Workflow: defaultWorkflow,
RepoRelDir: ".",
Workspace: "default",
Name: "",
AutoplanEnabled: false,
PolicySets: emptyPolicySets,
RepoLocking: true,
CustomPolicyCheck: false,
PolicyCheck: true,
},
},
"repo-side apply reqs should not include non-overrideable 'policies_passed' req when overridden and policies disabled": {
gCfg: `
repos:
- id: /.*/
allowed_overrides: [apply_requirements]
apply_requirements: [approved]
`,
repoID: "github.com/owner/repo",
proj: valid.Project{
Dir: ".",
Workspace: "default",
PlanRequirements: []string{},
ApplyRequirements: []string{"mergeable"},
ImportRequirements: []string{},
},
repoWorkflows: nil,
exp: valid.MergedProjectCfg{
PlanRequirements: []string{},
ApplyRequirements: []string{"mergeable"},
ImportRequirements: []string{},
Workflow: defaultWorkflow,
RepoRelDir: ".",
Workspace: "default",
Name: "",
AutoplanEnabled: false,
PolicySets: emptyPolicySets,
RepoLocking: true,
CustomPolicyCheck: false,
PolicyCheck: false,
},
},
"repo-side import reqs win out if allowed": {
gCfg: `
repos:
Expand Down
2 changes: 1 addition & 1 deletion server/events/project_command_builder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ workflows:
Pull: pull,
ProjectName: "",
PlanRequirements: []string{"policies_passed"},
ApplyRequirements: []string{},
ApplyRequirements: []string{"policies_passed"},
ImportRequirements: []string{"policies_passed"},
RepoConfigVersion: 3,
RePlanCmd: "atlantis plan -d project1 -w myworkspace -- flag",
Expand Down

0 comments on commit 95c5fe0

Please sign in to comment.