Skip to content

Commit 974d1c0

Browse files
nehalk-tfapeabody
andauthored
feat(TPG>=6.14)!: add parameter field support into org policy (#163)
Co-authored-by: Andrew Peabody <[email protected]>
1 parent 17308dd commit 974d1c0

File tree

9 files changed

+69
-6
lines changed

9 files changed

+69
-6
lines changed

examples/v2_boolean_org_enforce/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This boolean constraint, when set to true, enables OS Login on all newly created
1616
| Name | Description |
1717
|------|-------------|
1818
| constraint | Policy Constraint Identifier |
19+
| parameterized\_constraint | Policy with parameters for Managed Constraint Identifier |
1920
| policy\_root | Policy Root in the hierarchy for the given policy |
2021
| policy\_root\_id | Project Root ID at which the policy is applied |
2122

examples/v2_boolean_org_enforce/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,17 @@ module "gcp_org_policy_v2" {
2929
constraint = "compute.requireOsLogin"
3030
policy_type = "boolean"
3131
}
32+
33+
module "parameterized_org_policy_v2" {
34+
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2"
35+
version = "~> 7.0"
36+
37+
policy_root = "organization"
38+
policy_root_id = var.org_id
39+
rules = [{
40+
enforcement = true
41+
parameters = jsonencode({"allowedDomains" : ["@abc.com"]})
42+
}]
43+
constraint = "essentialcontacts.managed.allowedContactDomains"
44+
policy_type = "boolean"
45+
}

examples/v2_boolean_org_enforce/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ output "constraint" {
2929
value = module.gcp_org_policy_v2.constraint
3030
}
3131

32+
output "parameterized_constraint" {
33+
description = "Policy with parameters for Managed Constraint Identifier"
34+
value = module.parameterized_org_policy_v2.constraint
35+
}

modules/org_policy_v2/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ module "gcp_org_policy_v2_bool" {
4242
}
4343
```
4444

45+
- Parameterized Bool organization policy
46+
47+
```hcl
48+
module "parameterized_org_policy_v2_bool" {
49+
source = "terraform-google-modules/org-policy/google//modules/org_policy_v2"
50+
version = "~> 7.0"
51+
52+
policy_root = "organization" # either of organization, folder or project
53+
policy_root_id = "123456789" # either of org id, folder id or project id
54+
constraint = "constraint name" # constraint identifier without constraints/ prefix. Example "compute.requireOsLogin"
55+
policy_type = "boolean" # either of list or boolean
56+
exclude_folders = []
57+
exclude_projects = []
58+
59+
rules = [
60+
# Rule 1
61+
{
62+
enforcement = false
63+
},
64+
# Rule 2
65+
{
66+
enforcement = true
67+
parameters = jsonencode({"allowedDomains" : ["@abc.com"]})
68+
conditions = [{
69+
description = "description of the condition"
70+
expression = "resource.matchTagId('tagKeys/123456789', 'tagValues/123456789') && resource.matchTag('123456789/1234', 'abcd')"
71+
location = "sample-location.log"
72+
title = "Title of the condition"
73+
}]
74+
},
75+
]
76+
}
77+
```
78+
4579
- List organization policy
4680

4781
```hcl
@@ -81,6 +115,7 @@ To control module's behavior, change variables' values regarding the following:
81115
- `exclude_projects`: a list of project IDs to be excluded from this policy. They must be lower in the hierarchy than the policy root.
82116
- `rules`: Specify policy rules and conditions. Rules contain the following parameters:
83117
- `enforcement`: if `true` or `null`then policy will `deny_all`; if `false` then policy will `allow_all`. Applies for `boolean` based policies.
118+
- `parameters`: Applies for `boolean` type policies for `managed` constraints, if constraint has parameters defined. Pass parameter values when policy enforcement is enabled. Ensure that parameter value types match those defined in the constraint definition. For example: `{"allowedLocations" : ["us-east1", "us-west1"], "allowAll" : true }`
84119
- `allow`: list of values to include in the policy with ALLOW behavior. Set `enforce` to `null` to use it.
85120
- `deny`: list of values to include in the policy with DENY behavior. Set `enforce` to `null` to use it.
86121
- `conditions`: [Organization tags](https://cloud.google.com/resource-manager/docs/organization-policy/tags-organization-policy) provides a way to conditionally allow or deny policies based on whether a resource has a specific tag. You can use tags and conditional enforcement of organization policies to provide centralized control of the resources in your hierarchy. Each condition has the following properties:
@@ -117,7 +152,7 @@ To control module's behavior, change variables' values regarding the following:
117152
| policy\_root | Resource hierarchy node to apply the policy to: can be one of `organization`, `folder`, or `project`. | `string` | `"organization"` | no |
118153
| policy\_root\_id | The policy root id, either of organization\_id, folder\_id or project\_id | `string` | `null` | no |
119154
| policy\_type | The constraint type to work with (either 'boolean' or 'list') | `string` | `"list"` | no |
120-
| rules | List of rules per policy. | <pre>list(object(<br> {<br> enforcement = bool<br> allow = optional(list(string), [])<br> deny = optional(list(string), [])<br> conditions = optional(list(object(<br> {<br> description = string<br> expression = string<br> title = string<br> location = string<br> }<br> )), [])<br> }<br> ))</pre> | n/a | yes |
155+
| rules | List of rules per policy. | <pre>list(object(<br> {<br> enforcement = bool<br> parameters = optional(string, null)<br> allow = optional(list(string), [])<br> deny = optional(list(string), [])<br> conditions = optional(list(object(<br> {<br> description = string<br> expression = string<br> title = string<br> location = string<br> }<br> )), [])<br> }<br> ))</pre> | n/a | yes |
121156

122157
## Outputs
123158

modules/org_policy_v2/boolean_constraints.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ resource "google_org_policy_policy" "org_policy_boolean" {
2727
dynamic "rules" {
2828
for_each = local.rules
2929
content {
30-
enforce = rules.value.enforcement != false ? "TRUE" : "FALSE"
30+
enforce = rules.value.enforcement != false ? "TRUE" : "FALSE"
31+
parameters = rules.value.parameters
3132
dynamic "condition" {
3233
for_each = { for k, v in rules.value.conditions : k => v if length(rules.value.conditions) > 0 }
3334
content {
@@ -55,7 +56,8 @@ resource "google_org_policy_policy" "folder_policy_boolean" {
5556
dynamic "rules" {
5657
for_each = local.rules
5758
content {
58-
enforce = rules.value.enforcement != false ? "TRUE" : "FALSE"
59+
enforce = rules.value.enforcement != false ? "TRUE" : "FALSE"
60+
parameters = rules.value.parameters
5961
dynamic "condition" {
6062
for_each = { for k, v in rules.value.conditions : k => v if length(rules.value.conditions) > 0 }
6163
content {
@@ -83,7 +85,8 @@ resource "google_org_policy_policy" "project_policy_boolean" {
8385
dynamic "rules" {
8486
for_each = local.rules
8587
content {
86-
enforce = rules.value.enforcement != false ? "TRUE" : "FALSE"
88+
enforce = rules.value.enforcement != false ? "TRUE" : "FALSE"
89+
parameters = rules.value.parameters
8790
dynamic "condition" {
8891
for_each = { for k, v in rules.value.conditions : k => v if length(rules.value.conditions) > 0 }
8992
content {

modules/org_policy_v2/metadata.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ spec:
7979
list(object(
8080
{
8181
enforcement = bool
82+
parameters = optional(string, null)
8283
allow = optional(list(string), [])
8384
deny = optional(list(string), [])
8485
conditions = optional(list(object(
@@ -114,4 +115,4 @@ spec:
114115
- orgpolicy.googleapis.com
115116
providerVersions:
116117
- source: hashicorp/google
117-
version: ">= 3.53, < 7"
118+
version: ">= 6.14, < 7"

modules/org_policy_v2/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ variable "rules" {
6464
type = list(object(
6565
{
6666
enforcement = bool
67+
parameters = optional(string, null)
6768
allow = optional(list(string), [])
6869
deny = optional(list(string), [])
6970
conditions = optional(list(object(

modules/org_policy_v2/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ terraform {
2020

2121
google = {
2222
source = "hashicorp/google"
23-
version = ">= 3.53, < 7"
23+
version = ">= 6.14, < 7"
2424
}
2525
}
2626

test/integration/v2_boolean_org_enforce/v2_boolean_org_enforce_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func TestVersion2BooleanOrgEnforce(t *testing.T) {
3838

3939
constraintImplemented := utils.GetFirstMatchResult(t, op, "constraint", constraintName).Get("constraint").String()
4040
assert.Equal(constraintImplemented, constraintName, "Org policy is created and exists")
41+
42+
parameterizedConstraintName := "constraints/" + orgPolicyv2T.GetStringOutput("parameterized_constraint")
43+
parameterizedConstraintImplemented := utils.GetFirstMatchResult(t, op, "constraint", parameterizedConstraintName).Get("constraint").String()
44+
assert.Equal(parameterizedConstraintImplemented, parameterizedConstraintName, "Org policy is created and exists")
4145
})
4246
orgPolicyv2T.Test()
4347
}

0 commit comments

Comments
 (0)