Skip to content

Commit 3a5d275

Browse files
pseudomorphjamengualrkstricklandGenPage
authored andcommitted
feat(policies): Add granular policy_sets (#3086)
* Initial work. * Periodic push. * Fmt and start adding args to approve_policies cmd. * keep funcs for now. * Periodic push. * Periodic push. * fmt. * Move approve policies logic to project_command_runner. * update some tests * More test fixes. * update more tests. fix som logic. * more tests. add additional info to common data for custom templates. * fix apply with policies bug. update more tests/fmt * file perms * fix error parsing for conftest results. * Update more tests and linting. * update documentation. * Address no-fail case. Address comments. * Forgot changes. * fix markdown renderer * Fix policy fail logic. remove uneeded tmpl var * targeted policy approvals fix * Address PR comments. * empty commit to trigger build --------- Co-authored-by: PePe Amengual <[email protected]> Co-authored-by: rkstrickland <[email protected]> Co-authored-by: Dylan Page <[email protected]>
1 parent 4fec309 commit 3a5d275

File tree

63 files changed

+2187
-450
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2187
-450
lines changed

runatlantis.io/docs/custom-workflows.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ Or a custom command
456456
* `SHOWFILE` - Absolute path to the location where Atlantis expects the plan in json format to
457457
either be generated (by show) or already exist (if running policy checks). Can be used to
458458
override the built-in `plan`/`apply` commands, ex. `run: terraform show -json $PLANFILE > $SHOWFILE`.
459+
* `POLICYCHECKFILE` - Absolute path to the location of policy check output if Atlantis runs policy checks.
460+
See [policy checking](/docs/policy-checking.html#data-for-custom-run-steps) for information of data structure.
459461
* `BASE_REPO_NAME` - Name of the repository that the pull request will be merged into, ex. `atlantis`.
460462
* `BASE_REPO_OWNER` - Owner of the repository that the pull request will be merged into, ex. `runatlantis`.
461463
* `HEAD_REPO_NAME` - Name of the repository that is getting merged into the base repository, ex. `atlantis`.
423 KB
Loading
127 KB
Loading
746 KB
Loading

runatlantis.io/docs/policy-checking.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Enabling "policy checking" in addition to the [mergeable apply requirement](/doc
1616

1717
![Policy Check Apply Status Failure](./images/policy-check-apply-status-failure.png)
1818

19-
Any failures need to either be addressed in a successive commit, or approved by a blessed owner. This approval is independent of the approval apply requirement which can coexist in the policy checking workflow. After an approval, the apply can proceed.
19+
Any failures need to either be addressed in a successive commit, or approved by top-level owner(s) of policies or the owner(s) of the policy set in question. Policy approvals are independent of the approval apply requirement which can coexist in the policy checking workflow. After policies are approved, the apply can proceed.
2020

2121
![Policy Check Approval](./images/policy-check-approval.png)
2222

@@ -44,14 +44,23 @@ policies:
4444
users:
4545
- nishkrishnan
4646
policy_sets:
47-
- name: null_resource_warning
48-
path: <CODE_DIRECTORY>/policies/null_resource_warning/
47+
- name: deny_null_resource
48+
path: <CODE_DIRECTORY>/policies/deny_null_resource/
4949
source: local
50+
- name: deny_local_exec
51+
path: <CODE_DIRECTORY>/policies/deny_local_exec/
52+
source: local
53+
approve_count: 2
54+
owners:
55+
users:
56+
- pseudomorph
5057
```
5158

5259
- `name` - A name of your policy set.
5360
- `path` - Path to a policies directory. *Note: replace `<CODE_DIRECTORY>` with absolute dir path to conftest policy/policies.*
5461
- `source` - Tells atlantis where to fetch the policies from. Currently you can only host policies locally by using `local`.
62+
- `owners` - Defines the users/teams which are able to approve a specific policy set.
63+
- `approve_count` - Defines the number of approvals needed to bypass policy checks. Defaults to the top-level policies configuration, if not specified.
5564

5665
By default conftest is configured to only run the `main` package. If you wish to run specific/multiple policies consider passing `--namespace` or `--all-namespaces` to conftest with [`extra_args`](https://www.runatlantis.io/docs/custom-workflows.html#adding-extra-arguments-to-terraform-commands) via a custom workflow as shown in the below example.
5766

@@ -158,3 +167,21 @@ workflows:
158167
### Quiet policy checks
159168

160169
By default, Atlantis will add a comment to all pull requests with the policy check result - both successes and failures. Version 0.21.0 added the [`--quiet-policy-checks`](server-configuration.html#quiet-policy-checks) option, which will instead only add comments when policy checks fail, significantly reducing the number of comments when most policy check results succeed.
170+
171+
172+
### Data for custom run steps
173+
174+
When the policy check workflow runs, a file is created in the working directory which contains information about the status of each policy set tested. This data may be useful in custom run steps to generate metrics or notifications. The file contains JSON data in the following format:
175+
176+
```json
177+
[
178+
{
179+
"PolicySetName": "policy1",
180+
"ConftestOutput": "",
181+
"Passed": false,
182+
"ReqApprovals": 1,
183+
"CurApprovals": 0
184+
}
185+
]
186+
187+
```

runatlantis.io/docs/server-side-repo-config.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,12 @@ If you set a workflow with the key `default`, it will override this.
519519

520520
### Policies
521521

522-
| Key | Type | Default | Required | Description |
523-
|------------------------|-----------------|---------|-----------|------------------------------------------|
524-
| conftest_version | string | none | no | conftest version to run all policy sets |
525-
| owners | Owners(#Owners) | none | yes | owners that can approve failing policies |
526-
| policy_sets | []PolicySet | none | yes | set of policies to run on a plan output |
522+
| Key | Type | Default | Required | Description |
523+
|------------------------|-----------------|---------|-----------|----------------------------------------------------------|
524+
| conftest_version | string | none | no | conftest version to run all policy sets |
525+
| owners | Owners(#Owners) | none | yes | owners that can approve failing policies |
526+
| approve_count | int | 1 | no | number of approvals required to bypass failing policies. |
527+
| policy_sets | []PolicySet | none | yes | set of policies to run on a plan output |
527528

528529
### Owners
529530
| Key | Type | Default | Required | Description |

server/controllers/events/events_controller_e2e_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
12021202
Ok(t, err)
12031203

12041204
projectCommandRunner := &events.DefaultProjectCommandRunner{
1205+
VcsClient: e2eVCSClient,
12051206
Locker: projectLocker,
12061207
LockURLGenerator: &mockLockURLGenerator{},
12071208
InitStepRunner: &runtime.InitStepRunner{
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
Ran Policy Check for dir: `.` workspace: `default`
22

3-
**Policy Check Error**
4-
```
5-
exit status 1
6-
Checking plan against the following policies:
7-
test_policy
3+
**Policy Check Failed**: Some policy sets did not pass.
4+
#### Policy Set: `test_policy`
5+
```diff
86
FAIL - <redacted plan file> - main - WARNING: Null Resource creation is prohibited.
97

108
1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions
119

1210
```
13-
* :heavy_check_mark: To **approve** failing policies an authorized approver can comment:
11+
12+
13+
#### Policy Approval Status:
14+
```
15+
policy set: test_policy: requires: 1 approval(s), have: 0.
16+
```
17+
* :heavy_check_mark: To **approve** this project, comment:
18+
* `atlantis approve_policies -d .`
19+
* :put_litter_in_its_place: To **delete** this plan click [here](lock-url)
20+
* :repeat: To re-run policies **plan** this project again by commenting:
21+
* `atlantis plan -d .`
22+
23+
---
24+
* :heavy_check_mark: To **approve** all unapplied plans from this pull request, comment:
1425
* `atlantis approve_policies`
15-
* :repeat: Or, address the policy failure by modifying the codebase and re-planning.
26+
* :put_litter_in_its_place: To delete all plans and locks for the PR, comment:
27+
* `atlantis unlock`
28+
* :repeat: To re-run policies **plan** this project again by commenting:
29+
* `atlantis plan`
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
1+
Ran Approve Policies for 1 projects:
2+
3+
1. dir: `.` workspace: `default`
4+
5+
### 1. dir: `.` workspace: `default`
16
**Approve Policies Error**
27
```
3-
contact policy owners to approve failing policies
8+
1 error occurred:
9+
* policy set: test_policy user runatlantis is not a policy owner - please contact policy owners to approve failing policies
10+
11+
412
```
13+
#### Policy Approval Status:
14+
```
15+
policy set: test_policy: requires: 1 approval(s), have: 0.
16+
```
17+
* :heavy_check_mark: To **approve** this project, comment:
18+
* `atlantis approve_policies -d .`
19+
* :put_litter_in_its_place: To **delete** this plan click [here](lock-url)
20+
* :repeat: To re-run policies **plan** this project again by commenting:
21+
* `atlantis plan -d .`
22+
23+
---
24+
* :heavy_check_mark: To **approve** all unapplied plans from this pull request, comment:
25+
* `atlantis approve_policies`
26+
* :put_litter_in_its_place: To delete all plans and locks for the PR, comment:
27+
* `atlantis unlock`
28+
* :repeat: To re-run policies **plan** this project again by commenting:
29+
* `atlantis plan`
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
Ran Policy Check for dir: `.` workspace: `default`
22

3-
**Policy Check Error**
4-
```
5-
exit status 1
6-
Checking plan against the following policies:
7-
test_policy
3+
**Policy Check Failed**: Some policy sets did not pass.
4+
#### Policy Set: `test_policy`
5+
```diff
86
FAIL - <redacted plan file> - main - WARNING: Null Resource creation is prohibited.
97

108
1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions
119

1210
```
13-
* :heavy_check_mark: To **approve** failing policies an authorized approver can comment:
11+
12+
13+
#### Policy Approval Status:
14+
```
15+
policy set: test_policy: requires: 1 approval(s), have: 0.
16+
```
17+
* :heavy_check_mark: To **approve** this project, comment:
18+
* `atlantis approve_policies -d .`
19+
* :put_litter_in_its_place: To **delete** this plan click [here](lock-url)
20+
* :repeat: To re-run policies **plan** this project again by commenting:
21+
* `atlantis plan -d .`
22+
23+
---
24+
* :heavy_check_mark: To **approve** all unapplied plans from this pull request, comment:
1425
* `atlantis approve_policies`
15-
* :repeat: Or, address the policy failure by modifying the codebase and re-planning.
26+
* :put_litter_in_its_place: To delete all plans and locks for the PR, comment:
27+
* `atlantis unlock`
28+
* :repeat: To re-run policies **plan** this project again by commenting:
29+
* `atlantis plan`

0 commit comments

Comments
 (0)