Skip to content

Commit 5e704c2

Browse files
authored
Merge pull request hashicorp#131 from hashicorp/fix-vmware-sentinel-policies
fix VMware policies for Terraform 0.12 and PTFE
2 parents be67890 + 8ab2b89 commit 5e704c2

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

governance/second-generation/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
This directory and its sub-directories contain second-generation Sentinel policies which were created in 2019 for several clouds including AWS, Microsoft Azure, Google Cloud Platform (GCP), and VMware. It also contains some common, re-usable functions and mocks that can be used to test the new policies with the [Sentinel Simulator](https://docs.hashicorp.com/sentinel/commands).
44

5+
These policies are intended for use with Terraform 0.11.x and 0.12.x.
6+
7+
## Note about Using with Private Terraform Enterprise (PTFE)
8+
The portion of these policies that test whether resources are being destroyed use a new [destroy](https://www.terraform.io/docs/cloud/sentinel/import/tfplan.html#value-destroy) value that is present in Terraform Cloud (https://app.terraform.io) since 8/15/2019 but is not yet available in Private Terraform Enterprise (PTFE).
9+
10+
If you use these policies with PTFE and are using Terraform 0.11.x in your workspaces, please simply comment out portions of the policies that look like this:
11+
```
12+
if r.destroy {
13+
print("Skipping resource", address, "that is being destroyed.")
14+
continue
15+
}
16+
```
17+
18+
However, if you are using Terraform 0.12.x in your PTFE workspaces, you actually do need to check whether each resource is being destroyed since the `applied` value will be missing for those. Until the destroy value is added to PTFE (currently expected in October, 2019), what you can do is test something like `length(r.diff.<attribute>.new) == 0` where `<attribute>` would be a specific top-level attribute of the resource that would generally have some value unless the resource is being destroyed.
19+
20+
You could also use the latter approach with Terraform 0.11 if you do want to prevent Sentinel policies from being applied to destroyed resources on your PTFE servers.
21+
22+
523
## Improvements
624
These new second-generation policies have several improvements over the older first-generation policies:
725
1. They use some common parameterized functions including [find_resources_from_plan(type)](./common-functions/plan/find_resources_from_plan.md) and [validate_attribute_in_list(type, attribute, allowed_values)](./common-functions/plan/validate_attribute_in_list.md), which can be used unchanged in all policies that use the associated import. Using these reduces the amount of changes needed when writing new policies.

governance/second-generation/vmware/restrict-vm-cpu-and-memory.sentinel

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,24 @@ validate_attribute_less_than_value = func(type, attribute, max_value) {
5454
# Skip resource instances that are being destroyed
5555
# to avoid unnecessary policy violations.
5656
# Used to be: if length(r.diff) == 0
57-
if r.destroy {
58-
print("Skipping resource", address, "that is being destroyed.")
59-
continue
57+
#if r.destroy {
58+
# print("Skipping resource", address, "that is being destroyed.")
59+
# continue
60+
#}
61+
62+
# Skip resource instances that are being destroyed
63+
# to avoid unnecessary policy violations.
64+
# We are using the following until r.destroy is added to PTFE
65+
if tfplan.terraform_version matches "^0\\.11\\.\\d+$" {
66+
if length(r.diff) == 0 {
67+
print("Skipping resource", address, "that is being destroyed.")
68+
continue
69+
}
70+
} else if tfplan.terraform_version matches "^0\\.12\\.\\d+$" {
71+
if length(r.diff.memory.new) == 0 {
72+
print("Skipping resource", address, "that is being destroyed.")
73+
continue
74+
}
6075
}
6176

6277
# Determine if the attribute is computed
@@ -68,7 +83,7 @@ validate_attribute_less_than_value = func(type, attribute, max_value) {
6883
# validated = false
6984
} else {
7085
# Validate that the attribute exists
71-
if length(r.applied[attribute]) else 0 > 0 {
86+
if length(string(r.applied[attribute])) else 0 > 0 {
7287
# Validate that each instance has desired value
7388
if float(r.applied[attribute]) > max_value {
7489
print("Resource", address, "has attribute", attribute, "with value",

governance/second-generation/vmware/restrict-vm-disk-size.sentinel

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,24 @@ validate_disk_size = func(disk_limit) {
5353
# Skip resources that are being destroyed
5454
# to avoid unnecessary policy violations.
5555
# Used to be: if length(r.diff) == 0
56-
if r.destroy {
57-
print("Skipping resource", address, "that is being destroyed.")
58-
continue
56+
#if r.destroy {
57+
# print("Skipping resource", address, "that is being destroyed.")
58+
# continue
59+
#}
60+
61+
# Skip resources that are being destroyed
62+
# to avoid unnecessary policy violations.
63+
# We are using the following until r.destroy is added to PTFE
64+
if tfplan.terraform_version matches "^0\\.11\\.\\d+$" {
65+
if length(r.diff) == 0 {
66+
print("Skipping resource", address, "that is being destroyed.")
67+
continue
68+
}
69+
} else if tfplan.terraform_version matches "^0\\.12\\.\\d+$" {
70+
if r.diff["disk.#"].new == "" {
71+
print("Skipping resource", address, "that is being destroyed.")
72+
continue
73+
}
5974
}
6075

6176
# Initialize disk_count

0 commit comments

Comments
 (0)