Skip to content

Commit 165edb8

Browse files
AErmiepilor
authored andcommittedJan 15, 2019
Audit Resource Locks on Resource Groups based on Tags (#299)
Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.
1 parent 100d50b commit 165edb8

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Audit Resource Locks on Resource Groups based on Tags
2+
3+
Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.
4+
Within this Policy, you sepcify the Tag Name and Tag Value that will be used for identifying the Resource Groups to audit.
5+
6+
## Try in the Azure Portal
7+
8+
[![Deploy to Azure](http://azuredeploy.net/deploybutton.png)](https://portal.azure.com/#blade/Microsoft_Azure_Policy/CreatePolicyDefinitionBlade/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-policy%2Fmaster%2Fsamples%2FResourceGroup%2Faudit-resourceGroup-resourceLocks%2Fazurepolicy.json)
9+
10+
## Try with PowerShell
11+
12+
````powershell
13+
$definition = New-AzureRmPolicyDefinition -Name "audit-resourceGroup-resourceLocks" -DisplayName "Audit Resource Locks on Resource Groups based on Tags" -description "Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock." -Policy 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/ResourceGroup/audit-resourceGroup-resourceLocks/azurepolicy.rules.json' -Parameter 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/ResourceGroup/audit-resourceGroup-resourceLocks/azurepolicy.parameters.json' -Mode All
14+
$definition
15+
$assignment = New-AzureRMPolicyAssignment -Name <assignmentname> -Scope <scope> -tagName <tagName> -tagValue <tagValue> -PolicyDefinition $definition
16+
$assignment
17+
````
18+
19+
## Try with CLI
20+
21+
````cli
22+
az policy definition create --name 'audit-resourceGroup-resourceLocks' --display-name 'Audit Resource Locks on Resource Groups based on Tags' --description 'Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.' --rules 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/ResourceGroup/audit-resourceGroup-resourceLocks/azurepolicy.rules.json' --params 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/ResourceGroup/audit-resourceGroup-resourceLocks/azurepolicy.parameters.json' --mode All
23+
24+
az policy assignment create --name <assignmentname> --scope <scope> --policy "audit-resourceGroup-resourceLocks"
25+
````
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"properties": {
3+
"displayName": "Audit Resource Locks on Resource Groups based on Tags",
4+
"description": "Audits all Resource Groups that have a specific Tag, for the CanNotDelete Resource Lock.",
5+
"mode": "All",
6+
"parameters": {
7+
"tagName": {
8+
"type": "String",
9+
"metadata": {
10+
"displayName": "Tag Name",
11+
"description": "The Tag namne to audit against (i.e. Environment, CostCenter, etc.)"
12+
}
13+
},
14+
"tagValue": {
15+
"type": "String",
16+
"metadata": {
17+
"displayName": "Tag Value",
18+
"description": "Value of the tag to audit against (i.e. Prod/UAT/TEST, 12345, etc.)"
19+
}
20+
}
21+
},
22+
"policyRule": {
23+
"if": {
24+
"allOf": [
25+
{
26+
"field": "type",
27+
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
28+
},
29+
{
30+
"field": "[concat('tags[', parameters('tagName'), ']')]",
31+
"equals": "[parameters('tagValue')]"
32+
}
33+
]
34+
},
35+
"then": {
36+
"effect": "auditIfNotExists",
37+
"details": {
38+
"type": "Microsoft.Authorization/locks",
39+
"existenceCondition": {
40+
"field": "Microsoft.Authorization/locks/level",
41+
"equals": "CanNotDelete"
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"tagName": {
3+
"type": "String",
4+
"metadata": {
5+
"displayName": "Tag Name",
6+
"description": "The tag namne to audit against (i.e. Environment, CostCenter, etc.)"
7+
}
8+
},
9+
"tagValue": {
10+
"type": "String",
11+
"metadata": {
12+
"displayName": "Tag Value",
13+
"description": "Value of the tag to audit against (i.e. Prod/UAT/TEST, 12345, etc.)"
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"if": {
3+
"allOf": [
4+
{
5+
"field": "type",
6+
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
7+
},
8+
{
9+
"field": "[concat('tags[', parameters('tagName'), ']')]",
10+
"equals": "[parameters('tagValue')]"
11+
}
12+
]
13+
},
14+
"then": {
15+
"effect": "auditIfNotExists",
16+
"details": {
17+
"type": "Microsoft.Authorization/locks",
18+
"existenceCondition": {
19+
"field": "Microsoft.Authorization/locks/level",
20+
"equals": "CanNotDelete"
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)
Please sign in to comment.