Skip to content

Commit

Permalink
plan policy to warn on any resource changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielle committed Dec 29, 2023
1 parent e61a21f commit 1fd5928
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ _Access policies have been deprecated. Please [read this](./access/README.md) fo
- [Trusted engineers bypass review](./plan/trusted-engineers-bypass-review.rego)
- [Terrascan violated policies](./plan/terrascan-violated-policies.rego)
- [Tfsec high severity issues](./plan/tfsec-high-severity-issues.rego)
- [Warn On any Resource Actions](./plan/warn-on-any-resource-actions.rego)
- [Warn On Change To Sensitive Resources](./plan/warn-on-change-senstitive-resources.rego)

### Push Policy
Expand Down
22 changes: 22 additions & 0 deletions plan/warm-on-any-resource-actions_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package spacelift

Check failure on line 1 in plan/warm-on-any-resource-actions_test.rego

View workflow job for this annotation

GitHub Actions / Regal Check

Multiple tests with same name. To learn more, see: https://docs.styra.com/regal/rules/testing/identically-named-tests

# Test: No warning for non-destructive actions
test_no_warning_for_non_destructive {
warn_set := {msg | msg := warn[_]}
count(warn_set) == 0 with input as {"terraform": {"resource_changes": [{"address": "aws_instance.example", "change": {"actions": ["change"]}}]}}
}

# Test: Warning for a deleted resource
test_warning_for_deleted_resource {
warn with input as {"terraform": {"resource_changes": [{"address": "aws_instance.example", "change": {"actions": ["delete"]}}]}} == {"Warning: Resource 'aws_instance.example' is being deleted"}
}

# Test: Warning for a recreated resource
test_warning_for_recreated_resource {
warn with input as {"terraform": {"resource_changes": [{"address": "aws_instance.example", "change": {"actions": ["create"]}}]}} == {"Warning: Resource 'aws_instance.example' is being created"}
}

# Test: Warning for a recreated resource
test_warning_for_recreated_resource {
warn with input as {"terraform": {"resource_changes": [{"address": "aws_instance.example", "update": {"actions": ["update"]}}]}} == {"Warning: Resource 'aws_instance.example' is being updated"}
}
22 changes: 22 additions & 0 deletions plan/warn-on-any-resource-actions.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package spacelift

# Helper rule to generate warning messages for resources being deleted, created or updated
warn[msg] {
resource := input.terraform.resource_changes[_]
action := resource.change.actions[_]
is_destructive_action(action)
msg := sprintf("Warning: Resource '%s' is being %sd", [resource.address, action])
}

# Helper function to determine if an action is 'delete', 'create' or 'update'
is_destructive_action(action) {

Check failure on line 12 in plan/warn-on-any-resource-actions.rego

View workflow job for this annotation

GitHub Actions / Regal Check

Prefer pattern matching in function arguments. To learn more, see: https://docs.styra.com/regal/rules/idiomatic/equals-pattern-matching
action == "delete"
}

is_destructive_action(action) {

Check failure on line 16 in plan/warn-on-any-resource-actions.rego

View workflow job for this annotation

GitHub Actions / Regal Check

Prefer pattern matching in function arguments. To learn more, see: https://docs.styra.com/regal/rules/idiomatic/equals-pattern-matching
action == "create"
}

is_destructive_action(action) {

Check failure on line 20 in plan/warn-on-any-resource-actions.rego

View workflow job for this annotation

GitHub Actions / Regal Check

Prefer pattern matching in function arguments. To learn more, see: https://docs.styra.com/regal/rules/idiomatic/equals-pattern-matching
action == "update"
}

0 comments on commit 1fd5928

Please sign in to comment.