Skip to content

Commit 65802f5

Browse files
Define policy to test Location in Json (#2)
1 parent 7f6ca96 commit 65802f5

File tree

4 files changed

+88
-45
lines changed

4 files changed

+88
-45
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ For each test case, the following directory structure must be created:
1313
| +-- terraform files for test cases
1414
```
1515

16+
Note: the definition of the policy and the content of the terraform test code is up to you. The folders `policy_defintions` and `test` aim to be used as examples.
17+
You may define policies in Json (see `policy_defintions/location`), in TF (any other definition), in ARM template or whatever suits your need as long as you are able to wrap it in Terraform.
18+
1619
Additionally, a `.yaml` configuration file must describe the test as following:
1720

1821
```yaml
Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
1-
resource "azurerm_policy_definition" "Prevent_from_bad_location" {
2-
name = "Prevent from bad location${var.suffix}"
3-
policy_type = "Custom"
4-
mode = "All"
5-
display_name = "Prevent from bad location${var.suffix}"
1+
locals {
2+
file = jsondecode(file("${path.module}/policy.json"))
3+
name = "${local.file.name}${var.suffix}"
4+
policy_type = local.file.properties.policyType
5+
mode = local.file.properties.mode
6+
display_name = "${local.file.properties.displayName}${var.suffix}"
7+
description = local.file.properties.description
8+
rule = jsonencode(local.file.properties.policyRule)
9+
parameters = jsonencode(local.file.properties.parameters)
10+
metadata = jsonencode(local.file.properties.metadata)
11+
}
612

7-
policy_rule = jsonencode(
8-
{
9-
"if" : {
10-
"not" : {
11-
"field" : "location",
12-
"in" : "[parameters('allowedLocations')]"
13-
}
14-
},
15-
"then" : {
16-
"effect" : "[parameters('effect')]"
17-
}
18-
})
13+
resource "azurerm_policy_definition" "policy" {
14+
name = local.name
15+
policy_type = local.policy_type
16+
mode = local.mode
17+
display_name = local.display_name
18+
policy_rule = local.rule
19+
parameters = local.parameters
20+
}
1921

20-
parameters = jsonencode(
21-
{
22-
"allowedLocations" : {
23-
"type" : "Array",
24-
"defaultValue" : ["North Europe", "---", "East US 2"],
25-
"metadata" : {
26-
"description" : "The list of allowed locations for resources.",
27-
"displayName" : "Allowed locations",
28-
"strongType" : "location"
29-
}
30-
},
31-
"effect" : {
32-
"type" : "String",
33-
"metadata" : {
34-
"displayName" : "Effect",
35-
"description" : "Enable or disable or change the execution of this policy"
36-
},
37-
"allowedValues" : [
38-
"Audit",
39-
"Deny",
40-
"Disabled"
41-
],
42-
"defaultValue" : "Audit"
43-
}
44-
})
22+
output "policy_id" {
23+
value = azurerm_policy_definition.policy.id
4524
}

policy_definitions/location/output.tf

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"properties": {
3+
"displayName": "Prevent from bad location",
4+
"policyType": "Custom",
5+
"mode": "All",
6+
"description": "",
7+
"metadata": {
8+
"createdBy": "f3a7783b-3b3b-4568-9b0f-03775e509e8e",
9+
"createdOn": "2021-11-17T09:10:14.99062Z",
10+
"updatedBy": null,
11+
"updatedOn": null
12+
},
13+
"parameters": {
14+
"allowedLocations": {
15+
"type": "Array",
16+
"metadata": {
17+
"description": "The list of allowed locations for resources.",
18+
"displayName": "Allowed locations",
19+
"strongType": "location"
20+
},
21+
"defaultValue": [
22+
"North Europe",
23+
"---",
24+
"East US 2"
25+
]
26+
},
27+
"effect": {
28+
"type": "String",
29+
"metadata": {
30+
"description": "Enable or disable or change the execution of this policy",
31+
"displayName": "Effect"
32+
},
33+
"allowedValues": [
34+
"Audit",
35+
"Deny",
36+
"Disabled"
37+
],
38+
"defaultValue": "Audit"
39+
}
40+
},
41+
"policyRule": {
42+
"if": {
43+
"not": {
44+
"field": "location",
45+
"in": "[parameters('allowedLocations')]"
46+
}
47+
},
48+
"then": {
49+
"effect": "[parameters('effect')]"
50+
}
51+
}
52+
},
53+
"id": "/subscriptions/cdd50b30-b156-4574-b9dd-57ec4a587268/providers/Microsoft.Authorization/policyDefinitions/Prevent from bad location",
54+
"type": "Microsoft.Authorization/policyDefinitions",
55+
"name": "Prevent from bad location",
56+
"systemData": {
57+
"createdBy": "[email protected]",
58+
"createdByType": "User",
59+
"createdAt": "2021-11-17T09:10:14.9450521Z",
60+
"lastModifiedBy": "[email protected]",
61+
"lastModifiedByType": "User",
62+
"lastModifiedAt": "2021-11-17T09:10:14.9450521Z"
63+
}
64+
}

0 commit comments

Comments
 (0)