Bylaw

Gallery › Event Grid › Azure built-in policies

Configure Azure Event Grid namespace MQTT broker with private endpoints

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Private endpoints lets you connect your virtual network to Azure services without a public IP address at the source or destination. By mapping private endpoints to your resources, they'll be protected against data leakage risks. Learn more at: https://aka.ms/aeg-ns-privateendpoints.

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

Azure Policy JSONNamespaceMQTTBroker_PrivateEndpoint_DINE.json · 179 lines
{
  "properties": {
    "displayName": "Configure Azure Event Grid namespace MQTT broker with private endpoints",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Private endpoints lets you connect your virtual network to Azure services without a public IP address at the source or destination. By mapping private endpoints to your resources, they'll be protected against data leakage risks. Learn more at: https://aka.ms/aeg-ns-privateendpoints.",
    "metadata": {
      "version": "1.0.0",
      "category": "Event Grid"
    },
    "version": "1.0.0",
    "parameters": {
      "privateEndpointSubnetId": {
        "type": "String",
        "metadata": {
          "displayName": "Private endpoint subnet id",
          "description": "Specifies the subnet to use to configure private endpoint",
          "strongType": "Microsoft.Network/virtualNetworks/subnets"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.EventGrid/namespaces"
          },
          {
            "field": "Microsoft.EventGrid/namespaces/topicSpacesConfiguration.state",
            "equals": "Enabled"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.EventGrid/namespaces/privateEndpointConnections",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.EventGrid/namespaces/privateEndpointConnections[*].privateLinkServiceConnectionState.status",
                "equals": "Approved"
              },
              {
                "field": "Microsoft.EventGrid/namespaces/privateEndpointConnections[*].groupIds[*]",
                "equals": "topicspace"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/4d97b98b-1d4f-4787-a291-c67834d212e7",
            "/providers/Microsoft.Authorization/roleDefinitions/1e241071-0855-49ea-94dc-649edcd759de"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "name": {
                  "value": "[field('name')]"
                },
                "serviceId": {
                  "value": "[field('id')]"
                },
                "privateEndpointSubnetId": {
                  "value": "[parameters('privateEndpointSubnetId')]"
                }
              },
              "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"
                  }
                },
                "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"
                          },
                          "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": [
                                      "topicspace"
                                    ],
                                    "requestMessage": "autoapprove"
                                  }
                                }
                              ],
                              "manualPrivateLinkServiceConnections": []
                            }
                          }
                        ]
                      },
                      "parameters": {
                        "serviceId": {
                          "value": "[parameters('serviceId')]"
                        },
                        "privateEndpointSubnetId": {
                          "value": "[parameters('privateEndpointSubnetId')]"
                        },
                        "subnetLocation": {
                          "value": "[reference(first(take(split(parameters('privateEndpointSubnetId'),'/subnets'),1)),'2020-07-01','Full').location]"
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/cddcbb7e-a7b1-4380-b4d8-45cf77b0d561",
  "name": "cddcbb7e-a7b1-4380-b4d8-45cf77b0d561"
}
Bylawcddcbb7e-a7b1-4380-b4d8-45cf77b0d561.bylaw.ps1 · 53 lines
# Converted from NamespaceMQTTBroker_PrivateEndpoint_DINE.json by ConvertTo-Bylaw.
Policy 'cddcbb7e-a7b1-4380-b4d8-45cf77b0d561' {

    Properties @{
        DisplayName   = 'Configure Azure Event Grid namespace MQTT broker with private endpoints'
        Description   = 'Private endpoints lets you connect your virtual network to Azure services without a public IP address at the source or destination. By mapping private endpoints to your resources, they''ll be protected against data leakage risks. Learn more at: https://aka.ms/aeg-ns-privateendpoints.'
        Version       = '1.0.0'
        Category      = 'Event Grid'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter PrivateEndpointSubnetId @{
        Type        = 'String'
        DisplayName = 'Private endpoint subnet id'
        Description = 'Specifies the subnet to use to configure private endpoint'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/virtualNetworks/subnets' }
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.EventGrid/namespaces' {

        Require 'topicSpacesConfiguration.state is not Enabled' {
            Field topicSpacesConfiguration.state | Must -Not -Be 'Enabled' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.EventGrid/namespaces/privateEndpointConnections' -Role 'Network Contributor', 'EventGrid Contributor' -Effect (Param Effect) {
        Require 'privateEndpointConnections, privateLinkServiceConnectionState.status and groupIds are as required' {
            Field 'Microsoft.EventGrid/namespaces/privateEndpointConnections' | Must -Exist
            Each 'Microsoft.EventGrid/namespaces/privateEndpointConnections[*]' {
                Field privateLinkServiceConnectionState.status | Must -Be 'Approved' -Strict
            }
            Field 'Microsoft.EventGrid/namespaces/privateEndpointConnections' | Must -Exist
            Each 'Microsoft.EventGrid/namespaces/privateEndpointConnections[*]' {
                Field groupIds | Must -Exist
                Each 'groupIds[*]' {
                    Element | Must -Be 'topicspace'
                }
            }
        }
        Deployment -TemplateFile './templates/cddcbb7e-a7b1-4380-b4d8-45cf77b0d561.json' -Parameters ([ordered]@{
                name                    = (Arm '[field(''name'')]')
                serviceId               = (Arm '[field(''id'')]')
                privateEndpointSubnetId = (Param PrivateEndpointSubnetId)
            })
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Event Grid/NamespaceMQTTBroker_PrivateEndpoint_DINE.json in Azure/azure-policy, under the MIT licence.