Bylaw

Gallery › Cosmos DB › Azure built-in policies

Azure Cosmos DB allowed locations

Converted Policy Audit Deny Disabled Mode Indexed Version 1.1.0

This policy enables you to restrict the locations your organization can specify when deploying Azure Cosmos DB resources. Use to enforce your geo-compliance requirements.

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

Azure Policy JSONCosmos_Locations_Deny.json · 67 lines
{
  "properties": {
    "displayName": "Azure Cosmos DB allowed locations",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "This policy enables you to restrict the locations your organization can specify when deploying Azure Cosmos DB resources. Use to enforce your geo-compliance requirements.",
    "metadata": {
      "version": "1.1.0",
      "category": "Cosmos DB"
    },
    "version": "1.1.0",
    "parameters": {
      "listOfAllowedLocations": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed locations",
          "description": "The list of locations that can be specified when deploying Azure Cosmos DB resources.",
          "strongType": "location"
        }
      },
      "policyEffect": {
        "type": "String",
        "metadata": {
          "displayName": "Policy Effect",
          "description": "The desired effect of the policy."
        },
        "allowedValues": [
          "audit",
          "Audit",
          "deny",
          "Deny",
          "disabled",
          "Disabled"
        ],
        "defaultValue": "Deny"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.DocumentDB/databaseAccounts"
          },
          {
            "count": {
              "field": "Microsoft.DocumentDB/databaseAccounts/Locations[*]",
              "where": {
                "value": "[replace(toLower(first(field('Microsoft.DocumentDB/databaseAccounts/Locations[*].locationName'))), ' ', '')]",
                "in": "[parameters('listOfAllowedLocations')]"
              }
            },
            "notEquals": "[length(field('Microsoft.DocumentDB/databaseAccounts/Locations[*]'))]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('policyEffect')]"
      }
    },
    "versions": [
      "1.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/0473574d-2d43-4217-aefe-941fcdf7e684",
  "name": "0473574d-2d43-4217-aefe-941fcdf7e684"
}
Bylaw0473574d-2d43-4217-aefe-941fcdf7e684.bylaw.ps1 · 36 lines
# Converted from Cosmos_Locations_Deny.json by ConvertTo-Bylaw.
Policy '0473574d-2d43-4217-aefe-941fcdf7e684' {

    Properties @{
        DisplayName   = 'Azure Cosmos DB allowed locations'
        Description   = 'This policy enables you to restrict the locations your organization can specify when deploying Azure Cosmos DB resources. Use to enforce your geo-compliance requirements.'
        Version       = '1.1.0'
        Category      = 'Cosmos DB'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

    Parameter ListOfAllowedLocations @{
        Type        = 'Array'
        DisplayName = 'Allowed locations'
        Description = 'The list of locations that can be specified when deploying Azure Cosmos DB resources.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter PolicyEffect @{
        AllowedValues = @('audit', 'Audit', 'deny', 'Deny', 'disabled', 'Disabled')
        Default       = 'Deny'
        DisplayName   = 'Policy Effect'
        Description   = 'The desired effect of the policy.'
    }

    Resource 'Microsoft.DocumentDB/databaseAccounts' {

        Require 'Locations and Locations.locationName are as required' {
            Count 'Locations[*]' -Where {
                Arm '[replace(toLower(first(field(''Microsoft.DocumentDB/databaseAccounts/Locations[*].locationName''))), '' '', '''')]' | Must -BeIn (Param ListOfAllowedLocations)
            } | Must -Be (Arm '[length(field(''Microsoft.DocumentDB/databaseAccounts/Locations[*]''))]')
        }
    }

    Effect (Param PolicyEffect)
}

Source: built-in-policies/policyDefinitions/Cosmos DB/Cosmos_Locations_Deny.json in Azure/azure-policy, under the MIT licence.