Bylaw

Gallery › Cosmos DB › Azure built-in policies

Configure CosmosDB accounts with private endpoints

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Private endpoints connect your virtual network to Azure services without a public IP address at the source or destination. By mapping private endpoints to your CosmosDB account, you can reduce data leakage risks. Learn more about private links at: https://docs.microsoft.com/azure/cosmos-db/how-to-configure-private-endpoints.

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

Azure Policy JSONCosmos_PrivateEndpoint_DINE.json · 182 lines
{
  "properties": {
    "displayName": "Configure CosmosDB accounts with private endpoints ",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Private endpoints connect your virtual network to Azure services without a public IP address at the source or destination. By mapping private endpoints to your CosmosDB account, you can reduce data leakage risks. Learn more about private links at: https://docs.microsoft.com/azure/cosmos-db/how-to-configure-private-endpoints.",
    "metadata": {
      "version": "1.0.0",
      "category": "Cosmos DB"
    },
    "version": "1.0.0",
    "parameters": {
      "privateEndpointSubnetId": {
        "type": "String",
        "metadata": {
          "displayName": "privateEndpointSubnetId",
          "description": "A subnet in the location",
          "strongType": "Microsoft.Network/virtualNetworks/subnets"
        }
      },
      "privateEndpointGroupId": {
        "type": "String",
        "metadata": {
          "displayName": "privateEndpointGroupId",
          "description": "A group Id for the private endpoint"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.DocumentDB/databaseAccounts"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.DocumentDB/databaseAccounts/privateEndpointConnections",
          "existenceCondition": {
            "field": "Microsoft.DocumentDB/databaseAccounts/privateEndpointConnections/privateLinkServiceConnectionState.status",
            "equals": "Approved"
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
            "/providers/Microsoft.Authorization/roleDefinitions/5bd9cd88-fe45-4216-938b-f97437e15450"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "name": {
                  "value": "[field('name')]"
                },
                "serviceId": {
                  "value": "[field('id')]"
                },
                "privateEndpointSubnetId": {
                  "value": "[parameters('privateEndpointSubnetId')]"
                },
                "privateEndpointGroupId": {
                  "value": "[parameters('privateEndpointGroupId')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "name": {
                    "type": "String"
                  },
                  "serviceId": {
                    "type": "String"
                  },
                  "privateEndpointSubnetId": {
                    "type": "String"
                  },
                  "privateEndpointGroupId": {
                    "type": "String"
                  }
                },
                "variables": {
                  "privateEndpointName": "[concat('pe-',substring(parameters('name'),0,min(length(parameters('name')),50)),'-',uniquestring(deployment().name))]"
                },
                "resources": [
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('privateEndpointName')]",
                    "apiVersion": "2020-06-01",
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "serviceId": {
                            "type": "string"
                          },
                          "privateEndpointSubnetId": {
                            "type": "string"
                          },
                          "privateEndpointGroupId": {
                            "type": "String"
                          },
                          "subnetLocation": {
                            "type": "string"
                          }
                        },
                        "variables": {
                          "privateEndpointName": "[deployment().name]"
                        },
                        "resources": [
                          {
                            "name": "[variables('privateEndpointName')]",
                            "type": "Microsoft.Network/privateEndpoints",
                            "apiVersion": "2020-07-01",
                            "location": "[parameters('subnetLocation')]",
                            "tags": {},
                            "properties": {
                              "subnet": {
                                "id": "[parameters('privateEndpointSubnetId')]"
                              },
                              "privateLinkServiceConnections": [
                                {
                                  "name": "[variables('privateEndpointName')]",
                                  "properties": {
                                    "privateLinkServiceId": "[parameters('serviceId')]",
                                    "groupIds": [
                                      "[parameters('privateEndpointGroupId')]"
                                    ],
                                    "requestMessage": "autoapprove"
                                  }
                                }
                              ],
                              "manualPrivateLinkServiceConnections": []
                            }
                          }
                        ]
                      },
                      "parameters": {
                        "serviceId": {
                          "value": "[parameters('serviceId')]"
                        },
                        "privateEndpointSubnetId": {
                          "value": "[parameters('privateEndpointSubnetId')]"
                        },
                        "privateEndpointGroupId": {
                          "value": "[parameters('privateEndpointGroupId')]"
                        },
                        "subnetLocation": {
                          "value": "[reference(first(take(split(parameters('privateEndpointSubnetId'),'/subnets'),1)),'2020-07-01','Full').location]"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/b609e813-3156-4079-91fa-a8494c1471c4",
  "name": "b609e813-3156-4079-91fa-a8494c1471c4"
}
Bylawb609e813-3156-4079-91fa-a8494c1471c4.bylaw.ps1 · 44 lines
# Converted from Cosmos_PrivateEndpoint_DINE.json by ConvertTo-Bylaw.
Policy 'b609e813-3156-4079-91fa-a8494c1471c4' {

    Properties @{
        DisplayName   = 'Configure CosmosDB accounts with private endpoints '
        Description   = 'Private endpoints connect your virtual network to Azure services without a public IP address at the source or destination. By mapping private endpoints to your CosmosDB account, you can reduce data leakage risks. Learn more about private links at: https://docs.microsoft.com/azure/cosmos-db/how-to-configure-private-endpoints.'
        Version       = '1.0.0'
        Category      = 'Cosmos DB'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter PrivateEndpointSubnetId @{
        Type        = 'String'
        DisplayName = 'privateEndpointSubnetId'
        Description = 'A subnet in the location'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/virtualNetworks/subnets' }
    }
    Parameter PrivateEndpointGroupId @{
        Type        = 'String'
        DisplayName = 'privateEndpointGroupId'
        Description = 'A group Id for the private endpoint'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.DocumentDB/databaseAccounts'

    DeployIfNotExists 'Microsoft.DocumentDB/databaseAccounts/privateEndpointConnections' -Role 'Contributor', 'DocumentDB Account Contributor' -Effect (Param Effect) {
        Require 'privateLinkServiceConnectionState.status is Approved' {
            Field privateLinkServiceConnectionState.status | Must -Be 'Approved'
        }
        Deployment -TemplateFile './templates/b609e813-3156-4079-91fa-a8494c1471c4.json' -Parameters ([ordered]@{
                name                    = (Arm '[field(''name'')]')
                serviceId               = (Arm '[field(''id'')]')
                privateEndpointSubnetId = (Param PrivateEndpointSubnetId)
                privateEndpointGroupId  = (Param PrivateEndpointGroupId)
            })
    }
}

Its deployment templates are written beside it:

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