Bylaw

Gallery › Network › Azure built-in policies

Deploy a flow log resource with target network security group

Converted Policy DeployIfNotExists Mode Indexed Version 1.1.0

Configures flow log for specific network security group. It will allow to log information about IP traffic flowing through an network security group. Flow log helps to identify unknown or undesired traffic, verify network isolation and compliance with enterprise access rules, analyze network flows from compromised IPs and network interfaces.

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

Azure Policy JSONNetworkSecurityGroup_FlowLog_DINE.json · 171 lines
{
  "properties": {
    "displayName": "Deploy a flow log resource with target network security group",
    "description": "Configures flow log for specific network security group. It will allow to log information about IP traffic flowing through an network security group. Flow log helps to identify unknown or undesired traffic, verify network isolation and compliance with enterprise access rules, analyze network flows from compromised IPs and network interfaces.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "metadata": {
      "version": "1.1.0",
      "category": "Network"
    },
    "version": "1.1.0",
    "parameters": {
      "nsgRegion": {
        "type": "String",
        "metadata": {
          "displayName": "NSG Region",
          "description": "This Policy will review NSGs only in the selected region. You can create other assignments to include other regions.",
          "strongType": "location"
        }
      },
      "storageId": {
        "type": "String",
        "metadata": {
          "displayName": "Storage id",
          "description": "A string with the storage id for the flowlogs to be sent to. It will be used for deployment purposes only. Make sure this storage account is located in the same region as the NSG. The format must be: '/subscriptions/{subscription id}/resourceGroups/{resourceGroup name}/providers/Microsoft.Storage/storageAccounts/{storage account name}",
          "assignPermissions": true
        }
      },
      "networkWatcherRG": {
        "type": "String",
        "metadata": {
          "displayName": "Network Watchers RG",
          "description": "The name of the resource group where the flowLog resources will be created. This will be used only if a deployment is required. This is the resource group where the Network Watchers are located.",
          "strongType": "existingResourceGroups"
        }
      },
      "networkWatcherName": {
        "type": "String",
        "metadata": {
          "displayName": "Network Watcher name",
          "description": "The name of the network watcher under which the flowLog resources will be created. Make sure it belongs to the same region as the NSG."
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Network/networkSecurityGroups"
          },
          {
            "field": "location",
            "equals": "[parameters('nsgRegion')]"
          }
        ]
      },
      "then": {
        "effect": "deployIfNotExists",
        "details": {
          "type": "Microsoft.Network/networkWatchers/flowlogs",
          "resourceGroupName": "[if(empty(coalesce(field('Microsoft.Network/networkSecurityGroups/flowLogs'))), parameters('networkWatcherRG'), split(first(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id')), '/')[4])]",
          "name": "[if(empty(coalesce(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id'))), 'null/null', concat(split(first(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id')), '/')[8], '/', split(first(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id')), '/')[10]))]",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Network/networkWatchers/flowLogs/enabled",
                "equals": "true"
              },
              {
                "field": "Microsoft.Network/networkWatchers/flowLogs/storageId",
                "equals": "[parameters('storageId')]"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "storageId": {
                    "type": "String"
                  },
                  "networkWatcherRG": {
                    "type": "String"
                  },
                  "networkWatcherName": {
                    "type": "String"
                  },
                  "flowlogName": {
                    "type": "String"
                  },
                  "location": {
                    "type": "String"
                  },
                  "targetResource": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[concat('flowlogDeployment-', uniqueString(parameters('flowlogName')))]",
                    "apiVersion": "2019-10-01",
                    "resourceGroup": "[parameters('networkWatcherRG')]",
                    "properties": {
                      "mode": "incremental",
                      "parameters": {},
                      "template": {
                        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {},
                        "resources": [
                          {
                            "type": "Microsoft.Network/networkWatchers/flowLogs",
                            "name": "[concat(parameters('networkWatcherName'), '/', parameters('flowlogName'))]",
                            "apiVersion": "2019-11-01",
                            "location": "[parameters('location')]",
                            "properties": {
                              "targetResourceId": "[parameters('targetResource')]",
                              "storageId": "[parameters('storageId')]",
                              "enabled": "true",
                              "retentionPolicy": {
                                "days": "0",
                                "enabled": "false"
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "storageId": {
                  "value": "[parameters('storageId')]"
                },
                "networkWatcherRG": {
                  "value": "[if(empty(coalesce(field('Microsoft.Network/networkSecurityGroups/flowLogs'))), parameters('networkWatcherRG'), split(first(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id')), '/')[4])]"
                },
                "networkWatcherName": {
                  "value": "[if(empty(coalesce(field('Microsoft.Network/networkSecurityGroups/flowLogs'))), parameters('networkWatcherName'), split(first(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id')), '/')[8])]"
                },
                "flowlogName": {
                  "value": "[if(empty(coalesce(field('Microsoft.Network/networkSecurityGroups/flowLogs'))), concat(take(concat(field('name'), '-', resourceGroup().name), 72), '-', 'flowlog'), split(first(field('Microsoft.Network/networkSecurityGroups/flowLogs[*].id')), '/')[10])]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "targetResource": {
                  "value": "[concat(resourceGroup().id, '/providers/Microsoft.Network/networkSecurityGroups/', field('name'))]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/0db34a60-64f4-4bf6-bd44-f95c16cf34b9",
  "name": "0db34a60-64f4-4bf6-bd44-f95c16cf34b9"
}
Bylaw0db34a60-64f4-4bf6-bd44-f95c16cf34b9.bylaw.ps1 · 58 lines
# Converted from NetworkSecurityGroup_FlowLog_DINE.json by ConvertTo-Bylaw.
Policy '0db34a60-64f4-4bf6-bd44-f95c16cf34b9' {

    Properties @{
        DisplayName   = 'Deploy a flow log resource with target network security group'
        Description   = 'Configures flow log for specific network security group. It will allow to log information about IP traffic flowing through an network security group. Flow log helps to identify unknown or undesired traffic, verify network isolation and compliance with enterprise access rules, analyze network flows from compromised IPs and network interfaces.'
        Version       = '1.1.0'
        Category      = 'Network'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

    Parameter NsgRegion @{
        Type        = 'String'
        DisplayName = 'NSG Region'
        Description = 'This Policy will review NSGs only in the selected region. You can create other assignments to include other regions.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter StorageId @{
        Type        = 'String'
        DisplayName = 'Storage id'
        Description = 'A string with the storage id for the flowlogs to be sent to. It will be used for deployment purposes only. Make sure this storage account is located in the same region as the NSG. The format must be: ''/subscriptions/{subscription id}/resourceGroups/{resourceGroup name}/providers/Microsoft.Storage/storageAccounts/{storage account name}'
        Metadata    = [ordered]@{ assignPermissions = $true }
    }
    Parameter NetworkWatcherRG @{
        Type        = 'String'
        DisplayName = 'Network Watchers RG'
        Description = 'The name of the resource group where the flowLog resources will be created. This will be used only if a deployment is required. This is the resource group where the Network Watchers are located.'
        Metadata    = [ordered]@{ strongType = 'existingResourceGroups' }
    }
    Parameter NetworkWatcherName @{
        Type        = 'String'
        DisplayName = 'Network Watcher name'
        Description = 'The name of the network watcher under which the flowLog resources will be created. Make sure it belongs to the same region as the NSG.'
    }

    Resource 'Microsoft.Network/networkSecurityGroups' {

        Require 'location is not the NsgRegion parameter' {
            Field location | Must -Not -Be (Param NsgRegion) -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Network/networkWatchers/flowlogs' -Role 'Contributor' -Name (Arm '[if(empty(coalesce(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id''))), ''null/null'', concat(split(first(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id'')), ''/'')[8], ''/'', split(first(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id'')), ''/'')[10]))]') -ResourceGroupName (Arm '[if(empty(coalesce(field(''Microsoft.Network/networkSecurityGroups/flowLogs''))), parameters(''networkWatcherRG''), split(first(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id'')), ''/'')[4])]') {
        Require 'enabled is true and storageId is the StorageId parameter' {
            Field enabled | Must -Be 'true'
            Field storageId | Must -Be (Param StorageId)
        }
        Deployment -TemplateFile './templates/0db34a60-64f4-4bf6-bd44-f95c16cf34b9.json' -Parameters ([ordered]@{
                storageId          = (Param StorageId)
                networkWatcherRG   = (Arm '[if(empty(coalesce(field(''Microsoft.Network/networkSecurityGroups/flowLogs''))), parameters(''networkWatcherRG''), split(first(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id'')), ''/'')[4])]')
                networkWatcherName = (Arm '[if(empty(coalesce(field(''Microsoft.Network/networkSecurityGroups/flowLogs''))), parameters(''networkWatcherName''), split(first(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id'')), ''/'')[8])]')
                flowlogName        = (Arm '[if(empty(coalesce(field(''Microsoft.Network/networkSecurityGroups/flowLogs''))), concat(take(concat(field(''name''), ''-'', resourceGroup().name), 72), ''-'', ''flowlog''), split(first(field(''Microsoft.Network/networkSecurityGroups/flowLogs[*].id'')), ''/'')[10])]')
                location           = (Arm '[field(''location'')]')
                targetResource     = (Arm '[concat(resourceGroup().id, ''/providers/Microsoft.Network/networkSecurityGroups/'', field(''name''))]')
            })
    }
}

Its deployment templates are written beside it:

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