Bylaw

Gallery › Storage › Azure Community Policy

Disable Storage ATP Based on Tag

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This Policy will disable Advanced Threat Protection and a storage account that has a matching tag key/value.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 95 lines
{
  "name": "c0a60ee5-fcff-454d-ab6a-be8287aeec46",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Disable Storage ATP Based on Tag",
    "description": "This Policy will disable Advanced Threat Protection and a storage account that has a matching tag key/value.",
    "metadata": {
      "version": "1.0.0",
      "category": "Storage"
    },
    "mode": "Indexed",
    "parameters": {
      "tagName": {
        "type": "String"
      },
      "tagValue": {
        "type": "String"
      },
      "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": {
          "type": "Microsoft.Security/advancedThreatProtectionSettings",
          "name": "current",
          "existenceCondition": {
            "anyOf": [
              {
                "field": "Microsoft.Security/advancedThreatProtectionSettings/isEnabled",
                "equals": "false"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/fb1c8493-542b-48eb-b624-b4c8fea62acd"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "storageAccountName": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "apiVersion": "2019-01-01",
                    "type": "Microsoft.Storage/storageAccounts/providers/advancedThreatProtectionSettings",
                    "name": "[concat(parameters('storageAccountName'), '/Microsoft.Security/current')]",
                    "properties": {
                      "isEnabled": false
                    }
                  }
                ]
              },
              "parameters": {
                "storageAccountName": {
                  "value": "[field('name')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylawc0a60ee5-fcff-454d-ab6a-be8287aeec46.bylaw.ps1 · 42 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'c0a60ee5-fcff-454d-ab6a-be8287aeec46' {

    Properties @{
        DisplayName = 'Disable Storage ATP Based on Tag'
        Description = 'This Policy will disable Advanced Threat Protection and a storage account that has a matching tag key/value.'
        Version     = '1.0.0'
        Category    = 'Storage'
        Mode        = 'Indexed'
    }

    Parameter TagName @{
        Type = 'String'
    }
    Parameter TagValue @{
        Type = 'String'
    }
    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.Security/advancedThreatProtectionSettings' -Role 'Security Admin' -Name 'current' -Effect (Param Effect) {
        Require 'isEnabled is as required' {
            AnyOf {
                Field isEnabled | Must -Be 'false'
            }
        }
        Deployment -TemplateFile './templates/c0a60ee5-fcff-454d-ab6a-be8287aeec46.json' -Parameters ([ordered]@{
                storageAccountName = (Arm '[field(''name'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Storage/disable-storage-atp-based-on-tag/azurepolicy.json in Azure/Community-Policy, under the MIT licence.