Gallery › Storage › Azure Community Policy
Enforce or extend Storage Account IPRules if tag match
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0
This policy enforces or extends IP ranges if the Storage Account has a matching tag/value (deviating existing IP ranges may be kept or overwritten (parameter: existingIPRangesHandling)). 'publicNetworkAccess' will be enabled if disabled.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 191 lines
{
"name": "03e90fcf-943b-4c73-baea-829054ca4060",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Enforce or extend Storage Account IPRules if tag match",
"description": "This policy enforces or extends IP ranges if the Storage Account has a matching tag/value (deviating existing IP ranges may be kept or overwritten (parameter: existingIPRangesHandling)). 'publicNetworkAccess' will be enabled if disabled.",
"metadata": {
"version": "1.0.0",
"category": "Storage"
},
"mode": "Indexed",
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"displayName": "tagName",
"description": "name of the tag that must exist for the Storage Account"
}
},
"tagValue": {
"type": "String",
"metadata": {
"displayName": "tagValue",
"description": "value of the tagName that must match in order to apply the policy"
}
},
"enforceIPRanges": {
"type": "Array",
"metadata": {
"displayName": "enforceIPRanges",
"description": "IP/ranges that must be applied"
},
"defaultValue": [
"219.94.47.192/28",
"209.127.0.0/16"
]
},
"existingIPRangesHandling": {
"type": "String",
"metadata": {
"displayName": "existingIPRangesHandling",
"description": "retain: keep existing IP/ranges that are not defined in 'enforceIPRanges' (union exiting and enforced IP/ranges); remove: enforce 'enforceIPRanges' and remove the existing IP/ranges that are not defined in 'enforceIPRanges'"
},
"allowedValues": [
"retain",
"remove"
],
"defaultValue": "retain"
},
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Storage/storageAccounts"
},
{
"field": "[concat('tags[', parameters('tagName'), ']')]",
"equals": "[parameters('tagValue')]"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"evaluationDelay": "AfterProvisioningSuccess",
"type": "Microsoft.Storage/storageAccounts",
"name": "[field('name')]",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Storage/storageAccounts/networkAcls.ipRules",
"exists": "true"
},
{
"field": "Microsoft.Storage/storageAccounts/networkAcls.defaultAction",
"equals": "Deny"
},
{
"field": "Microsoft.Storage/storageAccounts/publicNetworkAccess",
"equals": "Enabled"
},
{
"count": {
"field": "Microsoft.Storage/storageAccounts/networkAcls.ipRules[*]",
"where": {
"anyOf": [
{
"field": "Microsoft.Storage/storageAccounts/networkAcls.ipRules[*].value",
"in": "[parameters('enforceIPRanges')]"
}
]
}
},
"equals": "[length(parameters('enforceIPRanges'))]"
}
]
},
"roleDefinitionIds": [
"/providers/microsoft.authorization/roleDefinitions/17d1049b-9a84-46fb-8f53-869881c3d3ab"
],
"deployment": {
"properties": {
"mode": "Incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"resourceName": {
"type": "String"
},
"enforceIPRanges": {
"type": "Array"
},
"location": {
"type": "String"
},
"ipRulesExisting": {
"type": "Array"
},
"existingIPRangesHandling": {
"type": "String"
}
},
"variables": {
"varIPRangesToApply": "[if(equals(parameters('existingIPRangesHandling'), 'retain'), union(parameters('enforceIPRanges'), parameters('ipRulesExisting')), parameters('enforceIPRanges'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "[parameters('resourceName')]",
"location": "[parameters('location')]",
"properties": {
"publicNetworkAccess": "Enabled",
"networkAcls": {
"defaultAction": "Deny",
"copy": [
{
"name": "ipRules",
"count": "[length(variables('varIPRangesToApply'))]",
"input": {
"value": "[variables('varIPRangesToApply')[copyIndex('ipRules')]]",
"action": "Allow"
}
}
]
}
}
}
],
"outputs": {}
},
"parameters": {
"resourceName": {
"value": "[field('name')]"
},
"enforceIPRanges": {
"value": "[parameters('enforceIPRanges')]"
},
"location": {
"value": "[field('location')]"
},
"ipRulesExisting": {
"value": "[field('Microsoft.Storage/storageAccounts/networkAcls.ipRules[*].value')]"
},
"existingIPRangesHandling": {
"value": "[parameters('existingIPRangesHandling')]"
}
}
}
}
}
}
}
}
}Bylaw03e90fcf-943b-4c73-baea-829054ca4060.bylaw.ps1 · 67 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '03e90fcf-943b-4c73-baea-829054ca4060' {
Properties @{
DisplayName = 'Enforce or extend Storage Account IPRules if tag match'
Description = 'This policy enforces or extends IP ranges if the Storage Account has a matching tag/value (deviating existing IP ranges may be kept or overwritten (parameter: existingIPRangesHandling)). ''publicNetworkAccess'' will be enabled if disabled.'
Version = '1.0.0'
Category = 'Storage'
Mode = 'Indexed'
}
Parameter TagName @{
Type = 'String'
DisplayName = 'tagName'
Description = 'name of the tag that must exist for the Storage Account'
}
Parameter TagValue @{
Type = 'String'
DisplayName = 'tagValue'
Description = 'value of the tagName that must match in order to apply the policy'
}
Parameter EnforceIPRanges @{
Type = 'Array'
Default = @('219.94.47.192/28', '209.127.0.0/16')
DisplayName = 'enforceIPRanges'
Description = 'IP/ranges that must be applied'
}
Parameter ExistingIPRangesHandling @{
AllowedValues = @('retain', 'remove')
Default = 'retain'
DisplayName = 'existingIPRangesHandling'
Description = 'retain: keep existing IP/ranges that are not defined in ''enforceIPRanges'' (union exiting and enforced IP/ranges); remove: enforce ''enforceIPRanges'' and remove the existing IP/ranges that are not defined in ''enforceIPRanges'''
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'Microsoft.Storage/storageAccounts' {
Require 'tag (Param TagName) is not the TagValue parameter' {
Tag (Param TagName) | Must -Not -Be (Param TagValue) -IfPresent
}
}
DeployIfNotExists 'Microsoft.Storage/storageAccounts' -Role 'Storage Account Contributor' -Name (Arm '[field(''name'')]') -EvaluationDelay AfterProvisioningSuccess -Effect (Param Effect) {
Require 'networkAcls.ipRules, networkAcls.defaultAction, publicNetworkAccess and 1 more are as required' {
Field networkAcls.ipRules | Must -Exist
Field networkAcls.defaultAction | Must -Be 'Deny'
Field publicNetworkAccess | Must -Be 'Enabled'
Count 'networkAcls.ipRules[*]' -Where {
AnyOf {
Field value | Must -BeIn (Param EnforceIPRanges)
}
} | Must -Be (Param EnforceIPRanges -Length)
}
Deployment -TemplateFile './templates/03e90fcf-943b-4c73-baea-829054ca4060.json' -Parameters ([ordered]@{
resourceName = (Arm '[field(''name'')]')
enforceIPRanges = (Param EnforceIPRanges)
location = (Arm '[field(''location'')]')
ipRulesExisting = (Arm '[field(''Microsoft.Storage/storageAccounts/networkAcls.ipRules[*].value'')]')
existingIPRangesHandling = (Param ExistingIPRangesHandling)
})
}
}Its deployment templates are written beside it:
templates/03e90fcf-943b-4c73-baea-829054ca4060.json
Source: policyDefinitions/Storage/enforce-or-extend-storage-account-iprules-if-tag-match/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw