Gallery › Network › Azure built-in policies
Configure network security groups to use specific workspace, storage account and flowlog retention policy for traffic analytics
Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.2.0
If it already has traffic analytics enabled, then policy will overwrite its existing settings with the ones provided during policy creation. Traffic analytics is a cloud-based solution that provides visibility into user and application activity in cloud networks.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONNetworkSecurityGroup_FlowLog_TrafficAnalytics_Update.json · 336 lines
{
"properties": {
"displayName": "Configure network security groups to use specific workspace, storage account and flowlog retention policy for traffic analytics",
"description": "If it already has traffic analytics enabled, then policy will overwrite its existing settings with the ones provided during policy creation. Traffic analytics is a cloud-based solution that provides visibility into user and application activity in cloud networks.",
"policyType": "BuiltIn",
"mode": "Indexed",
"metadata": {
"version": "1.2.0",
"category": "Network"
},
"version": "1.2.0",
"parameters": {
"effect": {
"type": "string",
"defaultValue": "DeployIfNotExists",
"allowedValues": [
"DeployIfNotExists",
"Disabled"
],
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
}
},
"nsgRegion": {
"type": "String",
"metadata": {
"displayName": "Network security group Region",
"description": "Configures for network security groups in the selected region only.",
"strongType": "location"
}
},
"storageId": {
"type": "String",
"metadata": {
"displayName": "Storage Resource ID",
"description": "Resource ID of the storage account where the flow logs are written. Make sure this storage account is located in the selected network security group region. The format must be: '/subscriptions/{subscription id}/resourceGroups/{resourceGroup name}/providers/Microsoft.Storage/storageAccounts/{storage account name}",
"assignPermissions": true
}
},
"timeInterval": {
"type": "String",
"metadata": {
"displayName": "Traffic analytics processing interval in minutes",
"description": "Traffic analytics processes blobs at the selected frequency."
},
"defaultValue": "60",
"allowedValues": [
"10",
"60"
]
},
"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"
}
},
"networkWatcherRG": {
"type": "String",
"metadata": {
"displayName": "Network Watcher resource group",
"description": "The Network Watcher regional instance is present in this resource group. The network security group flow logs resources are also created. This is used only if a deployment is required. By default, it is named 'NetworkWatcherRG'.",
"strongType": "existingResourceGroups"
}
},
"networkWatcherName": {
"type": "String",
"metadata": {
"displayName": "Network Watcher name",
"description": "The name of the network watcher under which the flow log resources are created. Make sure it belongs to the same region as the network security group."
}
},
"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"
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Network/networkSecurityGroups"
},
{
"field": "location",
"equals": "[parameters('nsgRegion')]"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"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": {
"anyof": [
{
"field": "Microsoft.Network/networkWatchers/flowLogs/enabled",
"equals": "false"
},
{
"allof": [
{
"field": "Microsoft.Network/networkWatchers/flowLogs/enabled",
"equals": "true"
},
{
"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.trafficAnalyticsInterval",
"in": [
"10",
"60"
]
},
{
"field": "Microsoft.Network/networkWatchers/flowLogs/flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.workspaceId",
"equals": "[parameters('workspaceId')]"
},
{
"anyof": [
{
"allof": [
{
"value": "[parameters('retentionDays')]",
"equals": "0"
},
{
"anyof": [
{
"field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.days",
"equals": "0"
},
{
"field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.enabled",
"equals": "false"
}
]
}
]
},
{
"allof": [
{
"value": "[parameters('retentionDays')]",
"notequals": "0"
},
{
"allof": [
{
"field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.days",
"notequals": "0"
},
{
"field": "Microsoft.Network/networkWatchers/flowLogs/retentionPolicy.enabled",
"equals": "true"
}
]
}
]
}
]
}
]
}
]
},
"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"
},
"timeInterval": {
"type": "String"
},
"workspaceId": {
"type": "String"
},
"workspaceRegion": {
"type": "String"
},
"workspaceResourceId": {
"type": "String"
},
"networkWatcherRG": {
"type": "String"
},
"networkWatcherName": {
"type": "String"
},
"flowlogName": {
"type": "String"
},
"location": {
"type": "String"
},
"targetResource": {
"type": "string"
},
"retentionDays": {
"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",
"flowAnalyticsConfiguration": {
"networkWatcherFlowAnalyticsConfiguration": {
"enabled": true,
"workspaceId": "[parameters('workspaceId')]",
"workspaceRegion": "[parameters('workspaceRegion')]",
"workspaceResourceId": "[parameters('workspaceResourceId')]",
"trafficAnalyticsInterval": "[parameters('timeInterval')]"
}
},
"retentionPolicy": {
"days": "[parameters('retentionDays')]",
"enabled": "true"
},
"format": {
"type": "JSON",
"version": 2
}
}
}
]
}
}
}
]
},
"parameters": {
"storageId": {
"value": "[parameters('storageId')]"
},
"timeInterval": {
"value": "[parameters('timeInterval')]"
},
"workspaceId": {
"value": "[parameters('workspaceId')]"
},
"workspaceRegion": {
"value": "[parameters('workspaceRegion')]"
},
"workspaceResourceId": {
"value": "[parameters('workspaceResourceId')]"
},
"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'))]"
},
"retentionDays": {
"value": "[parameters('retentionDays')]"
}
}
}
}
}
}
},
"versions": [
"1.2.0"
]
},
"id": "/providers/Microsoft.Authorization/policyDefinitions/5e1cd26a-5090-4fdb-9d6a-84a90335e22d",
"name": "5e1cd26a-5090-4fdb-9d6a-84a90335e22d"
}Bylaw5e1cd26a-5090-4fdb-9d6a-84a90335e22d.bylaw.ps1 · 120 lines
# Converted from NetworkSecurityGroup_FlowLog_TrafficAnalytics_Update.json by ConvertTo-Bylaw.
Policy '5e1cd26a-5090-4fdb-9d6a-84a90335e22d' {
Properties @{
DisplayName = 'Configure network security groups to use specific workspace, storage account and flowlog retention policy for traffic analytics'
Description = 'If it already has traffic analytics enabled, then policy will overwrite its existing settings with the ones provided during policy creation. Traffic analytics is a cloud-based solution that provides visibility into user and application activity in cloud networks.'
Version = '1.2.0'
Category = 'Network'
PolicyVersion = '1.2.0'
Mode = 'Indexed'
}
Parameter Effect @{
Type = 'string'
AllowedValues = @('DeployIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'Enable or disable the execution of the policy'
}
Parameter NsgRegion @{
Type = 'String'
DisplayName = 'Network security group Region'
Description = 'Configures for network security groups in the selected region only.'
Metadata = [ordered]@{ strongType = 'location' }
}
Parameter StorageId @{
Type = 'String'
DisplayName = 'Storage Resource ID'
Description = 'Resource ID of the storage account where the flow logs are written. Make sure this storage account is located in the selected network security group region. The format must be: ''/subscriptions/{subscription id}/resourceGroups/{resourceGroup name}/providers/Microsoft.Storage/storageAccounts/{storage account name}'
Metadata = [ordered]@{ assignPermissions = $true }
}
Parameter TimeInterval @{
AllowedValues = @('10', '60')
Default = '60'
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'
}
Parameter NetworkWatcherRG @{
Type = 'String'
DisplayName = 'Network Watcher resource group'
Description = 'The Network Watcher regional instance is present in this resource group. The network security group flow logs resources are also created. This is used only if a deployment is required. By default, it is named ''NetworkWatcherRG''.'
Metadata = [ordered]@{ strongType = 'existingResourceGroups' }
}
Parameter NetworkWatcherName @{
Type = 'String'
DisplayName = 'Network Watcher name'
Description = 'The name of the network watcher under which the flow log resources are created. Make sure it belongs to the same region as the network security group.'
}
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.'
}
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])]') -Effect (Param Effect) {
Require 'enabled, storageId, flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled and 4 more are as required' {
AnyOf {
Field enabled | Must -Be 'false'
AllOf {
Field enabled | Must -Be 'true'
Field storageId | Must -Be (Param StorageId)
Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.enabled | Must -Be 'true'
Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.trafficAnalyticsInterval | Must -BeIn @('10', '60')
Field flowAnalyticsConfiguration.networkWatcherFlowAnalyticsConfiguration.workspaceId | Must -Be (Param WorkspaceId)
AnyOf {
AllOf {
Param RetentionDays | Must -Be '0'
AnyOf {
Field retentionPolicy.days | Must -Be '0'
Field retentionPolicy.enabled | Must -Be 'false'
}
}
AllOf {
Param RetentionDays | Must -Not -Be '0'
Field retentionPolicy.days | Must -Not -Be '0' -IfPresent
Field retentionPolicy.enabled | Must -Be 'true'
}
}
}
}
}
Deployment -TemplateFile './templates/5e1cd26a-5090-4fdb-9d6a-84a90335e22d.json' -Parameters ([ordered]@{
storageId = (Param StorageId)
timeInterval = (Param TimeInterval)
workspaceId = (Param WorkspaceId)
workspaceRegion = (Param WorkspaceRegion)
workspaceResourceId = (Param WorkspaceResourceId)
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''))]')
retentionDays = (Param RetentionDays)
})
}
}Its deployment templates are written beside it:
templates/5e1cd26a-5090-4fdb-9d6a-84a90335e22d.json
Source: built-in-policies/policyDefinitions/Network/NetworkSecurityGroup_FlowLog_TrafficAnalytics_Update.json in Azure/azure-policy, under the MIT licence.
Bylaw