Bylaw

Gallery › Storage › Azure built-in policies

Storage accounts should use private link

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 2.1.0

Azure Private Link lets you connect your virtual network to Azure services without a public IP address at the source or destination. The Private Link platform handles the connectivity between the consumer and services over the Azure backbone network. By mapping private endpoints to your storage account, data leakage risks are reduced. Learn more about private links at - https://aka.ms/azureprivatelinkoverview

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

Azure Policy JSONStorageAccountPrivateEndpointEnabled_Audit.json · 115 lines
{
  "properties": {
    "displayName": "Storage accounts should use private link",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Azure Private Link lets you connect your virtual network to Azure services without a public IP address at the source or destination. The Private Link platform handles the connectivity between the consumer and services over the Azure backbone network. By mapping private endpoints to your storage account, data leakage risks are reduced. Learn more about private links at - https://aka.ms/azureprivatelinkoverview",
    "metadata": {
      "version": "2.1.0",
      "category": "Storage"
    },
    "version": "2.1.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      },
      "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
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Storage/storageAccounts/privateEndpointConnections",
          "existenceCondition": {
            "field": "Microsoft.Storage/storageAccounts/privateEndpointConnections/privateLinkServiceConnectionState.status",
            "equals": "Approved"
          }
        }
      }
    },
    "versions": [
      "2.1.0",
      "2.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/6edd7eda-6dd8-40f7-810d-67160c639cd9",
  "name": "6edd7eda-6dd8-40f7-810d-67160c639cd9"
}
Bylaw6edd7eda-6dd8-40f7-810d-67160c639cd9.bylaw.ps1 · 58 lines
# Converted from StorageAccountPrivateEndpointEnabled_Audit.json by ConvertTo-Bylaw.
Policy '6edd7eda-6dd8-40f7-810d-67160c639cd9' {

    Properties @{
        DisplayName   = 'Storage accounts should use private link'
        Description   = 'Azure Private Link lets you connect your virtual network to Azure services without a public IP address at the source or destination. The Private Link platform handles the connectivity between the consumer and services over the Azure backbone network. By mapping private endpoints to your storage account, data leakage risks are reduced. Learn more about private links at - https://aka.ms/azureprivatelinkoverview'
        Version       = '2.1.0'
        Category      = 'Storage'
        PolicyVersion = '2.1.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    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/'
            }
        }

        Require 'Requirement 1' {
            Arm '[contains(parameters(''excludedManagedByResourceProviders''), ''All managed resource groups'')]' | Must -Not -Be $false
        }

        Require 'Requirement 2' {
            Arm '[empty(resourcegroup().managedBy)]' | Must -Not -Be $true
        }
    }

    AuditIfNotExists 'Microsoft.Storage/storageAccounts/privateEndpointConnections' -Effect (Param Effect) {
        Require 'privateLinkServiceConnectionState.status is Approved' {
            Field privateLinkServiceConnectionState.status | Must -Be 'Approved'
        }
    }
}

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