Bylaw

Gallery › Network › Azure Community Policy

Deploy a Traffic Analytics enabled Flow Log resource with target virtual network

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Configures flow log with enabled Traffic Analytics for specific virtual network. It will allow to log information about IP traffic flowing through an virtual network. Virtual Netowork Flow logs help 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 JSONazurepolicy.json · 275 lines
{
    "type": "Microsoft.Authorization/policyDefinitions",
    "name": "582b01ec-5ed4-408e-ad80-d7667d7b8de2",
    "properties": {
      "displayName": "Deploy a Traffic Analytics enabled Flow Log resource with target virtual network",
      "mode": "Indexed",
      "description": "Configures flow log with enabled Traffic Analytics for specific virtual network. It will allow to log information about IP traffic flowing through an virtual network. Virtual Netowork Flow logs help to identify unknown or undesired traffic, verify network isolation and compliance with enterprise access rules, analyze network flows from compromised IPs and network interfaces.",
      "metadata": {
        "version": "1.0.0",
        "category": "Network"
      },
      "version": "1.0.0",
      "parameters": {
        "effect": {
          "type": "String",
          "metadata": {
            "displayName": "Effect",
            "description": "Enable or disable the execution of the policy"
          },
          "allowedValues": ["DeployIfNotExists", "Disabled"],
          "defaultValue": "DeployIfNotExists"
        },
        "vnetRegion": {
          "type": "String",
          "metadata": {
            "displayName": "Virtual Network Region",
            "description": "This Policy will review Vnets only in the selected region. You can create other assignments to include other regions.",
            "strongType": "location"
          }
        },
        "storageId": {
          "type": "String",
          "metadata": {
            "displayName": "Storage Account",
            "description": "The resource id of storage account where flowlogs will be sent to. It will be used for deployment purposes only. Make sure this storage account is located in the same region as the Vnet.",
            "assignPermissions": true,
            "strongType": "Microsoft.Storage/storageAccounts"
          }
        },
        "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"
          },
          "defaultValue": "NetworkWatcherRG"
        },
        "networkWatcherName": {
          "type": "String",
          "metadata": {
            "displayName": "Network Watcher",
            "description": "The resource id of the network watcher under which the flowLog resources will be created. Make sure it belongs to the same region as the Vnet.",
            "strongType": "Microsoft.Network/networkWatchers"
          }
        },
        "retentionDays": {
          "type": "String",
          "metadata": {
            "displayName": "Number of days to retain flowlogs",
            "description": "The number of days for which flowlog data will be retained in storage account. If you want to retain data forever and do not want to apply any retention policy, set retention (days) to 0."
          },
          "defaultValue": "30"
        },
        "timeInterval": {
          "type": "String",
          "metadata": {
            "displayName": "Traffic analytics processing interval in minutes",
            "description": "Traffic analytics processes blobs at the selected frequency."
          },
          "allowedValues": ["10", "60"],
          "defaultValue": "10"
        },
        "workspaceResourceId": {
          "type": "String",
          "metadata": {
            "displayName": "Workspace resource ID",
            "description": "Log Analytics workspace resource id",
            "assignPermissions": true
          }
        },
        "workspaceRegion": {
          "type": "String",
          "metadata": {
            "displayName": "Workspace region",
            "description": "Log Analytics workspace region",
            "strongType": "location"
          }
        },
        "workspaceId": {
          "type": "String",
          "metadata": {
            "displayName": "Workspace ID",
            "description": "Log Analytics workspace GUID"
          }
        }
      },
      "policyRule": {
        "if": {
          "allOf": [
            {
              "field": "type",
              "equals": "Microsoft.Network/virtualNetworks"
            },
            {
              "field": "location",
              "equals": "[parameters('vnetRegion')]"
            }
          ]
        },
        "then": {
          "effect": "[parameters('effect')]",
          "details": {
            "type": "Microsoft.Network/networkWatchers/flowlogs",
            "resourceGroupName": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), parameters('networkWatcherRG'), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[4])]",
            "name": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs[*].id'))), 'null/null', concat(split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[8], '/', split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[10]))]",
            "existenceCondition": {
              "allOf": [
                {
                  "field": "Microsoft.Network/networkWatchers/flowLogs/enabled",
                  "equals": "true"
                },
                {
                  "field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.days",
                  "equals": "[parameters('retentionDays')]"
                },
                {
                  "field": "Microsoft.Network/networkWatchers/flowLogs/storageId",
                  "equals": "[parameters('storageId')]"
                },
                {
                  "field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled",
                  "equals": "true"
                },
                {
                  "field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.workspaceId",
                  "equals": "[parameters('workspaceId')]"
                },
                {
                  "field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.trafficAnalyticsInterval",
                  "equals": "[parameters('timeInterval')]"
                }
              ]
            },
            "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"
                    },
                    "retentionDays": {
                      "type": "string"
                    },
                    "timeInterval": {
                      "type": "String"
                    },
                    "workspaceId": {
                      "type": "String"
                    },
                    "workspaceRegion": {
                      "type": "String"
                    },
                    "workspaceResourceId": {
                      "type": "String"
                    }
                  },
                  "resources": [
                    {
                      "type": "Microsoft.Resources/deployments",
                      "name": "[concat('flowlogDeployment-', uniqueString(parameters('flowlogName')))]",
                      "apiVersion": "2022-09-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": "2022-09-01",
                              "location": "[parameters('location')]",
                              "properties": {
                                "targetResourceId": "[parameters('targetResource')]",
                                "storageId": "[parameters('storageId')]",
                                "enabled": "true",
                                "retentionPolicy": {
                                  "days": "[parameters('retentionDays')]",
                                  "enabled": "true"
                                },
                                "flowAnalyticsConfiguration": {
                                  "networkWatcherFlowAnalyticsConfiguration": {
                                    "enabled": true,
                                    "workspaceId": "[parameters('workspaceId')]",
                                    "workspaceRegion": "[parameters('workspaceRegion')]",
                                    "workspaceResourceId": "[parameters('workspaceResourceId')]",
                                    "trafficAnalyticsInterval": "[parameters('timeInterval')]"
                                  }
                                }
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                },
                "parameters": {
                  "storageId": {
                    "value": "[parameters('storageId')]"
                  },
                  "networkWatcherRG": {
                    "value": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), parameters('networkWatcherRG'), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[4])]"
                  },
                  "networkWatcherName": {
                    "value": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), last(split(parameters('networkWatcherName'), '/')), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[8])]"
                  },
                  "flowlogName": {
                    "value": "[if(empty(coalesce(field('Microsoft.Network/virtualNetworks/flowLogs'))), concat(take(concat(field('name'), '-', resourceGroup().name), 72), '-', 'flowlog'), split(first(field('Microsoft.Network/virtualNetworks/flowLogs[*].id')), '/')[10])]"
                  },
                  "location": {
                    "value": "[field('location')]"
                  },
                  "targetResource": {
                    "value": "[concat(resourceGroup().id, '/providers/Microsoft.Network/virtualNetworks/', field('name'))]"
                  },
                  "retentionDays": {
                    "value": "[parameters('retentionDays')]"
                  },
                  "timeInterval": {
                    "value": "[parameters('timeInterval')]"
                  },
                  "workspaceId": {
                    "value": "[parameters('workspaceId')]"
                  },
                  "workspaceRegion": {
                    "value": "[parameters('workspaceRegion')]"
                  },
                  "workspaceResourceId": {
                    "value": "[parameters('workspaceResourceId')]"
                  }
                }
              }
            }
          }
        }
      },
      "versions": ["1.0.0"]
    }
  }
Bylaw582b01ec-5ed4-408e-ad80-d7667d7b8de2.bylaw.ps1 · 102 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '582b01ec-5ed4-408e-ad80-d7667d7b8de2' {

    Properties @{
        DisplayName   = 'Deploy a Traffic Analytics enabled Flow Log resource with target virtual network'
        Description   = 'Configures flow log with enabled Traffic Analytics for specific virtual network. It will allow to log information about IP traffic flowing through an virtual network. Virtual Netowork Flow logs help 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.0.0'
        Category      = 'Network'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter VnetRegion @{
        Type        = 'String'
        DisplayName = 'Virtual Network Region'
        Description = 'This Policy will review Vnets only in the selected region. You can create other assignments to include other regions.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter StorageId @{
        Type        = 'String'
        DisplayName = 'Storage Account'
        Description = 'The resource id of storage account where flowlogs will be sent to. It will be used for deployment purposes only. Make sure this storage account is located in the same region as the Vnet.'
        Metadata    = [ordered]@{ assignPermissions = $true; strongType = 'Microsoft.Storage/storageAccounts' }
    }
    Parameter NetworkWatcherRG @{
        Default     = 'NetworkWatcherRG'
        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'
        Description = 'The resource id of the network watcher under which the flowLog resources will be created. Make sure it belongs to the same region as the Vnet.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Network/networkWatchers' }
    }
    Parameter RetentionDays @{
        Default     = '30'
        DisplayName = 'Number of days to retain flowlogs'
        Description = 'The number of days for which flowlog data will be retained in storage account. If you want to retain data forever and do not want to apply any retention policy, set retention (days) to 0.'
    }
    Parameter TimeInterval @{
        AllowedValues = @('10', '60')
        Default       = '10'
        DisplayName   = 'Traffic analytics processing interval in minutes'
        Description   = 'Traffic analytics processes blobs at the selected frequency.'
    }
    Parameter WorkspaceResourceId @{
        Type        = 'String'
        DisplayName = 'Workspace resource ID'
        Description = 'Log Analytics workspace resource id'
        Metadata    = [ordered]@{ assignPermissions = $true }
    }
    Parameter WorkspaceRegion @{
        Type        = 'String'
        DisplayName = 'Workspace region'
        Description = 'Log Analytics workspace region'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter WorkspaceId @{
        Type        = 'String'
        DisplayName = 'Workspace ID'
        Description = 'Log Analytics workspace GUID'
    }

    Resource 'Microsoft.Network/virtualNetworks' {

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

    DeployIfNotExists 'Microsoft.Network/networkWatchers/flowlogs' -Role 'Contributor' -Name (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id''))), ''null/null'', concat(split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[8], ''/'', split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[10]))]') -ResourceGroupName (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), parameters(''networkWatcherRG''), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[4])]') -Effect (Param Effect) {
        Require 'enabled, retentionPolicy.days, storageId and 3 more are as required' {
            Field enabled | Must -Be 'true'
            Field retentionPolicy.days | Must -Be (Param RetentionDays)
            Field storageId | Must -Be (Param StorageId)
            Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled | Must -Be 'true'
            Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.workspaceId | Must -Be (Param WorkspaceId)
            Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.trafficAnalyticsInterval | Must -Be (Param TimeInterval)
        }
        Deployment -TemplateFile './templates/582b01ec-5ed4-408e-ad80-d7667d7b8de2.json' -Parameters ([ordered]@{
                storageId           = (Param StorageId)
                networkWatcherRG    = (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), parameters(''networkWatcherRG''), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[4])]')
                networkWatcherName  = (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), last(split(parameters(''networkWatcherName''), ''/'')), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[8])]')
                flowlogName         = (Arm '[if(empty(coalesce(field(''Microsoft.Network/virtualNetworks/flowLogs''))), concat(take(concat(field(''name''), ''-'', resourceGroup().name), 72), ''-'', ''flowlog''), split(first(field(''Microsoft.Network/virtualNetworks/flowLogs[*].id'')), ''/'')[10])]')
                location            = (Arm '[field(''location'')]')
                targetResource      = (Arm '[concat(resourceGroup().id, ''/providers/Microsoft.Network/virtualNetworks/'', field(''name''))]')
                retentionDays       = (Param RetentionDays)
                timeInterval        = (Param TimeInterval)
                workspaceId         = (Param WorkspaceId)
                workspaceRegion     = (Param WorkspaceRegion)
                workspaceResourceId = (Param WorkspaceResourceId)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Network/deploy-a-traffic-analytics-enabled-flow-log-resource-with-target-virtual-network/azurepolicy.json in Azure/Community-Policy, under the MIT licence.