Bylaw

Gallery › Storage › Azure built-in policies

Storage accounts should restrict network access

Converted Policy Audit Deny Disabled Mode Indexed Version 1.2.0

Network access to storage accounts should be restricted. Configure network rules so only applications from allowed networks can access the storage account. To allow connections from specific internet or on-premises clients, access can be granted to traffic from specific Azure virtual networks or to public internet IP address ranges

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

Azure Policy JSONStorage_NetworkAcls_Audit.json · 113 lines
{
  "properties": {
    "displayName": "Storage accounts should restrict network access",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Network access to storage accounts should be restricted. Configure network rules so only applications from allowed networks can access the storage account. To allow connections from specific internet or on-premises clients, access can be granted to traffic from specific Azure virtual networks or to public internet IP address ranges",
    "metadata": {
      "version": "1.2.0",
      "category": "Storage"
    },
    "version": "1.2.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "The effect determines what happens when the policy rule is evaluated to match"
        }
      },
      "excludedManagedByResourceProviders": {
        "type": "Array",
        "metadata": {
          "displayName": "Managed services to exclude",
          "description": "Select managed services whose storage accounts should be excluded from this policy, or select all managed resource groups. Leave empty to evaluate all storage accounts."
        },
        "allowedValues": [
          "Azure Red Hat OpenShift",
          "Azure Databricks",
          "Microsoft Purview",
          "All managed resource groups"
        ],
        "defaultValue": []
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Storage/storageAccounts"
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'Azure Red Hat OpenShift')]",
                "equals": false
              },
              {
                "value": "[resourcegroup().managedBy]",
                "notContains": "/providers/Microsoft.RedHatOpenShift/"
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'Azure Databricks')]",
                "equals": false
              },
              {
                "value": "[resourcegroup().managedBy]",
                "notContains": "/providers/Microsoft.Databricks/"
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'Microsoft Purview')]",
                "equals": false
              },
              {
                "value": "[resourcegroup().managedBy]",
                "notContains": "/providers/Microsoft.Purview/"
              }
            ]
          },
          {
            "anyOf": [
              {
                "value": "[contains(parameters('excludedManagedByResourceProviders'), 'All managed resource groups')]",
                "equals": false
              },
              {
                "value": "[empty(resourcegroup().managedBy)]",
                "equals": true
              }
            ]
          },
          {
            "field": "Microsoft.Storage/storageAccounts/networkAcls.defaultAction",
            "notEquals": "Deny"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.2.0",
      "1.1.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/34c877ad-507e-4c82-993e-3452a6e0ad3c",
  "name": "34c877ad-507e-4c82-993e-3452a6e0ad3c"
}
Bylaw34c877ad-507e-4c82-993e-3452a6e0ad3c.bylaw.ps1 · 55 lines
# Converted from Storage_NetworkAcls_Audit.json by ConvertTo-Bylaw.
Policy '34c877ad-507e-4c82-993e-3452a6e0ad3c' {

    Properties @{
        DisplayName   = 'Storage accounts should restrict network access'
        Description   = 'Network access to storage accounts should be restricted. Configure network rules so only applications from allowed networks can access the storage account. To allow connections from specific internet or on-premises clients, access can be granted to traffic from specific Azure virtual networks or to public internet IP address ranges'
        Version       = '1.2.0'
        Category      = 'Storage'
        PolicyVersion = '1.2.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'The effect determines what happens when the policy rule is evaluated to match'
    }
    Parameter ExcludedManagedByResourceProviders @{
        Type          = 'Array'
        AllowedValues = @('Azure Red Hat OpenShift', 'Azure Databricks', 'Microsoft Purview', 'All managed resource groups')
        Default       = @()
        DisplayName   = 'Managed services to exclude'
        Description   = 'Select managed services whose storage accounts should be excluded from this policy, or select all managed resource groups. Leave empty to evaluate all storage accounts.'
    }

    Resource 'Microsoft.Storage/storageAccounts' {

        When {
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''Azure Red Hat OpenShift'')]' | Must -Be $false
                Arm '[resourcegroup().managedBy]' | Must -Not -ContainText '/providers/Microsoft.RedHatOpenShift/'
            }
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''Azure Databricks'')]' | Must -Be $false
                Arm '[resourcegroup().managedBy]' | Must -Not -ContainText '/providers/Microsoft.Databricks/'
            }
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''Microsoft Purview'')]' | Must -Be $false
                Arm '[resourcegroup().managedBy]' | Must -Not -ContainText '/providers/Microsoft.Purview/'
            }
            AnyOf {
                Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''All managed resource groups'')]' | Must -Be $false
                Arm '[empty(resourcegroup().managedBy)]' | Must -Be $true
            }
        }

        Require 'networkAcls.defaultAction is Deny' {
            Field networkAcls.defaultAction | Must -Be 'Deny'
        }
    }

    Effect (Param Effect)
}

Source: built-in-policies/policyDefinitions/Storage/Storage_NetworkAcls_Audit.json in Azure/azure-policy, under the MIT licence.