Gallery › Monitoring › Azure Community Policy
Enable logging by category group for Notification Hubs namespaces (microsoft.notificationhubs/namespaces) to Event Hub
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.1.0
Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. This policy deploys a diagnostic setting using a category group to route logs to an Event Hub for Notification Hubs namespace (microsoft.notificationhubs/namespaces).
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
{
"name": "c66c8050-7a45-4549-90bd-1c875571017c",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Enable logging by category group for Notification Hubs namespaces (microsoft.notificationhubs/namespaces) to Event Hub",
"description": "Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. This policy deploys a diagnostic setting using a category group to route logs to an Event Hub for Notification Hubs namespace (microsoft.notificationhubs/namespaces).",
"metadata": {
"category": "Monitoring",
"version": "1.1.0"
},
"mode": "Indexed",
"parameters": {
"diagnosticSettingName": {
"type": "String",
"metadata": {
"displayName": "Diagnostic Setting Name",
"description": "Diagnostic Setting Name"
},
"defaultValue": "setByPolicy-EventHub"
},
"categoryGroup": {
"type": "String",
"metadata": {
"displayName": "Category Group",
"description": "Diagnostic category group - none, audit, or allLogs."
},
"allowedValues": [
"audit",
"allLogs"
],
"defaultValue": "audit"
},
"resourceLocation": {
"type": "String",
"metadata": {
"displayName": "Resource Location",
"description": "Resource Location must be in the same location as the Event Hub Namespace.",
"strongType": "location"
}
},
"eventHubAuthorizationRuleId": {
"type": "String",
"metadata": {
"displayName": "Event Hub Authorization Rule Id",
"description": "Event Hub Authorization Rule Id - the authorization rule needs to be at Event Hub namespace level. e.g. /subscriptions/{subscription Id}/resourceGroups/{resource group}/providers/Microsoft.EventHub/namespaces/{Event Hub namespace}/authorizationrules/{authorization rule}",
"strongType": "Microsoft.EventHub/Namespaces/AuthorizationRules",
"assignPermissions": true
}
},
"eventHubName": {
"type": "String",
"metadata": {
"displayName": "Event Hub Name",
"description": "Event Hub Name."
},
"defaultValue": "Monitoring"
},
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
},
"allowedValues": [
"DeployIfNotExists",
"AuditIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "microsoft.notificationhubs/namespaces"
},
{
"field": "location",
"equals": "[parameters('resourceLocation')]"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"existenceCondition": {
"allOf": [
{
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[equals(parameters('categoryGroup'), 'audit')]"
},
{
"field": "microsoft.insights/diagnosticSettings/logs[*].categoryGroup",
"equals": "audit"
}
]
}
},
"equals": 1
},
{
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[equals(parameters('categoryGroup'), 'allLogs')]"
},
{
"field": "microsoft.insights/diagnosticSettings/logs[*].categoryGroup",
"equals": "allLogs"
}
]
}
},
"equals": 1
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubAuthorizationRuleId",
"equals": "[parameters('eventHubAuthorizationRuleId')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/eventHubName",
"equals": "[parameters('eventHubName')]"
}
]
},
"roleDefinitionIds": [
"/providers/Microsoft.Authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293",
"/providers/Microsoft.Authorization/roleDefinitions/f526a384-b230-433a-b45c-95f59c4a2dec"
],
"deployment": {
"properties": {
"mode": "incremental",
"template": {
"$schema": "http://schema.management.azure.com/schemas/2019-08-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"diagnosticSettingName": {
"type": "string"
},
"categoryGroup": {
"type": "String"
},
"eventHubName": {
"type": "string"
},
"eventHubAuthorizationRuleId": {
"type": "string"
},
"resourceLocation": {
"type": "string"
},
"resourceName": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "microsoft.notificationhubs/namespaces/providers/diagnosticSettings",
"apiVersion": "2021-05-01-preview",
"name": "[concat(parameters('resourceName'), '/', 'Microsoft.Insights/', parameters('diagnosticSettingName'))]",
"location": "[parameters('resourceLocation')]",
"properties": {
"eventHubName": "[parameters('eventHubName')]",
"eventHubAuthorizationRuleId": "[parameters('eventHubAuthorizationRuleId')]",
"logs": [
{
"categoryGroup": "audit",
"enabled": "[equals(parameters('categoryGroup'), 'audit')]"
},
{
"categoryGroup": "allLogs",
"enabled": "[equals(parameters('categoryGroup'), 'allLogs')]"
}
],
"metrics": []
}
}
],
"outputs": {
"policy": {
"type": "string",
"value": "[concat('Diagnostic setting ', parameters('diagnosticSettingName'), ' for type Notification Hubs namespace (microsoft.notificationhubs/namespaces), resourceName ', parameters('resourceName'), ' to EventHub ', parameters('eventHubAuthorizationRuleId'), ':', parameters('eventHubName'), ' configured')]"
}
}
},
"parameters": {
"diagnosticSettingName": {
"value": "[parameters('diagnosticSettingName')]"
},
"categoryGroup": {
"value": "[parameters('categoryGroup')]"
},
"eventHubName": {
"value": "[parameters('eventHubName')]"
},
"eventHubAuthorizationRuleId": {
"value": "[parameters('eventHubAuthorizationRuleId')]"
},
"resourceLocation": {
"value": "[field('location')]"
},
"resourceName": {
"value": "[field('name')]"
}
}
}
}
}
}
}
}
}# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'c66c8050-7a45-4549-90bd-1c875571017c' {
Properties @{
DisplayName = 'Enable logging by category group for Notification Hubs namespaces (microsoft.notificationhubs/namespaces) to Event Hub'
Description = 'Resource logs should be enabled to track activities and events that take place on your resources and give you visibility and insights into any changes that occur. This policy deploys a diagnostic setting using a category group to route logs to an Event Hub for Notification Hubs namespace (microsoft.notificationhubs/namespaces).'
Category = 'Monitoring'
Version = '1.1.0'
Mode = 'Indexed'
}
Parameter DiagnosticSettingName @{
Default = 'setByPolicy-EventHub'
DisplayName = 'Diagnostic Setting Name'
Description = 'Diagnostic Setting Name'
}
Parameter CategoryGroup @{
AllowedValues = @('audit', 'allLogs')
Default = 'audit'
DisplayName = 'Category Group'
Description = 'Diagnostic category group - none, audit, or allLogs.'
}
Parameter ResourceLocation @{
Type = 'String'
DisplayName = 'Resource Location'
Description = 'Resource Location must be in the same location as the Event Hub Namespace.'
Metadata = [ordered]@{ strongType = 'location' }
}
Parameter EventHubAuthorizationRuleId @{
Type = 'String'
DisplayName = 'Event Hub Authorization Rule Id'
Description = 'Event Hub Authorization Rule Id - the authorization rule needs to be at Event Hub namespace level. e.g. /subscriptions/{subscription Id}/resourceGroups/{resource group}/providers/Microsoft.EventHub/namespaces/{Event Hub namespace}/authorizationrules/{authorization rule}'
Metadata = [ordered]@{ strongType = 'Microsoft.EventHub/Namespaces/AuthorizationRules'; assignPermissions = $true }
}
Parameter EventHubName @{
Default = 'Monitoring'
DisplayName = 'Event Hub Name'
Description = 'Event Hub Name.'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'microsoft.notificationhubs/namespaces' {
Require 'location is not the ResourceLocation parameter' {
Field location | Must -Not -Be (Param ResourceLocation) -IfPresent
}
}
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Log Analytics Contributor', 'Azure Event Hubs Data Owner' -Effect (Param Effect) {
Require 'logs, enabled, categoryGroup and 2 more are as required' {
Count 'logs[*]' -Where {
Field enabled | Must -Be (Arm '[equals(parameters(''categoryGroup''), ''audit'')]')
Field categoryGroup | Must -Be 'audit'
} | Must -Be 1
Count 'logs[*]' -Where {
Field enabled | Must -Be (Arm '[equals(parameters(''categoryGroup''), ''allLogs'')]')
Field categoryGroup | Must -Be 'allLogs'
} | Must -Be 1
Field eventHubAuthorizationRuleId | Must -Be (Param EventHubAuthorizationRuleId)
Field eventHubName | Must -Be (Param EventHubName)
}
Deployment -TemplateFile './templates/c66c8050-7a45-4549-90bd-1c875571017c.json' -Parameters ([ordered]@{
diagnosticSettingName = (Param DiagnosticSettingName)
categoryGroup = (Param CategoryGroup)
eventHubName = (Param EventHubName)
eventHubAuthorizationRuleId = (Param EventHubAuthorizationRuleId)
resourceLocation = (Arm '[field(''location'')]')
resourceName = (Arm '[field(''name'')]')
})
}
}Its deployment templates are written beside it:
templates/c66c8050-7a45-4549-90bd-1c875571017c.json
Source: policyDefinitions/Monitoring/enable-logging-by-category-group-for-notification-hubs-namespaces-to-event-hub/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw