Gallery › Monitoring › Azure Community Policy
Apply Diagnostic Settings for Azure Key Vault to a Log Analytics workspace
Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0
This policy automatically deploys diagnostic settings for Azure Key Vault to a Log Analytics workspace.
ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.
Azure Policy JSONazurepolicy.json · 198 lines
{
"name": "7adb2f86-c465-4149-899e-2b1e66efa66f",
"type": "Microsoft.Authorization/policyDefinitions",
"properties": {
"displayName": "Apply Diagnostic Settings for Azure Key Vault to a Log Analytics workspace",
"description": "This policy automatically deploys diagnostic settings for Azure Key Vault to a Log Analytics workspace.",
"metadata": {
"category": "Monitoring",
"version": "1.0.0"
},
"mode": "Indexed",
"parameters": {
"profileName": {
"type": "String",
"metadata": {
"displayName": "Profile Name for Config",
"description": "The profile name Azure Diagnostics"
}
},
"logAnalytics": {
"type": "String",
"metadata": {
"displayName": "Log Analyitcs workspace",
"description": "The target Log Analytics Workspace for Azure Diagnostics",
"strongType": "omsWorkspace"
}
},
"azureRegions": {
"type": "Array",
"metadata": {
"displayName": "Allowed Locations",
"description": "The list of locations that can be specified when deploying resources",
"strongType": "location"
}
},
"metricsEnabled": {
"type": "String",
"metadata": {
"displayName": "Enable Metrics",
"description": "Enable Metrics - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "False"
},
"logsEnabled": {
"type": "String",
"metadata": {
"displayName": "Enable Logs",
"description": "Enable Logs - True or False"
},
"allowedValues": [
"True",
"False"
],
"defaultValue": "True"
},
"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.KeyVault/vaults"
},
{
"field": "location",
"in": "[parameters('AzureRegions')]"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
"equals": "[parameters('logsEnabled')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
"equals": "[parameters('metricsEnabled')]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/workspaceId",
"equals": "[parameters('logAnalytics')]"
}
]
},
"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": {
"vaultName": {
"type": "string"
},
"location": {
"type": "string"
},
"logAnalytics": {
"type": "string"
},
"metricsEnabled": {
"type": "string"
},
"logsEnabled": {
"type": "string"
},
"profileName": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.KeyVault/vaults/providers/diagnosticSettings",
"apiVersion": "2017-05-01-preview",
"name": "[concat(parameters('vaultName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
"location": "[parameters('location')]",
"dependsOn": [],
"properties": {
"workspaceId": "[parameters('logAnalytics')]",
"metrics": [
{
"category": "AllMetrics",
"enabled": "[parameters('metricsEnabled')]",
"retentionPolicy": {
"enabled": false,
"days": 0
}
}
],
"logs": [
{
"category": "AuditEvent",
"enabled": "[parameters('logsEnabled')]"
}
]
}
}
],
"outputs": {
"policy": {
"type": "string",
"value": "[concat(parameters('logAnalytics'), 'configured for diagnostic logs for ', ': ', parameters('name'))]"
}
}
},
"parameters": {
"logAnalytics": {
"value": "[parameters('logAnalytics')]"
},
"location": {
"value": "[field('location')]"
},
"vaultName": {
"value": "[field('name')]"
},
"metricsEnabled": {
"value": "[parameters('metricsEnabled')]"
},
"logsEnabled": {
"value": "[parameters('logsEnabled')]"
},
"profileName": {
"value": "[parameters('profileName')]"
}
}
}
}
}
}
}
}
}Bylaw7adb2f86-c465-4149-899e-2b1e66efa66f.bylaw.ps1 · 70 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '7adb2f86-c465-4149-899e-2b1e66efa66f' {
Properties @{
DisplayName = 'Apply Diagnostic Settings for Azure Key Vault to a Log Analytics workspace'
Description = 'This policy automatically deploys diagnostic settings for Azure Key Vault to a Log Analytics workspace.'
Category = 'Monitoring'
Version = '1.0.0'
Mode = 'Indexed'
}
Parameter ProfileName @{
Type = 'String'
DisplayName = 'Profile Name for Config'
Description = 'The profile name Azure Diagnostics'
}
Parameter LogAnalytics @{
Type = 'String'
DisplayName = 'Log Analyitcs workspace'
Description = 'The target Log Analytics Workspace for Azure Diagnostics'
Metadata = [ordered]@{ strongType = 'omsWorkspace' }
}
Parameter AzureRegions @{
Type = 'Array'
DisplayName = 'Allowed Locations'
Description = 'The list of locations that can be specified when deploying resources'
Metadata = [ordered]@{ strongType = 'location' }
}
Parameter MetricsEnabled @{
AllowedValues = @('True', 'False')
Default = 'False'
DisplayName = 'Enable Metrics'
Description = 'Enable Metrics - True or False'
}
Parameter LogsEnabled @{
AllowedValues = @('True', 'False')
Default = 'True'
DisplayName = 'Enable Logs'
Description = 'Enable Logs - True or False'
}
Parameter Effect @{
AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
Default = 'DeployIfNotExists'
DisplayName = 'Effect'
Description = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
}
Resource 'Microsoft.KeyVault/vaults' {
Require 'location is not one of (Arm ''[parameters(''''AzureRegions'''')]'')' {
Field location | Must -Not -BeIn (Arm '[parameters(''AzureRegions'')]') -IfPresent
}
}
DeployIfNotExists 'Microsoft.Insights/diagnosticSettings' -Role 'Contributor' -Effect (Param Effect) {
Require 'logs.enabled, metrics.enabled and workspaceId are as required' {
Field logs.enabled | Must -Be (Param LogsEnabled)
Field metrics.enabled | Must -Be (Param MetricsEnabled)
Field workspaceId | Must -Be (Param LogAnalytics)
}
Deployment -TemplateFile './templates/7adb2f86-c465-4149-899e-2b1e66efa66f.json' -Parameters ([ordered]@{
logAnalytics = (Param LogAnalytics)
location = (Arm '[field(''location'')]')
vaultName = (Arm '[field(''name'')]')
metricsEnabled = (Param MetricsEnabled)
logsEnabled = (Param LogsEnabled)
profileName = (Param ProfileName)
})
}
}Its deployment templates are written beside it:
templates/7adb2f86-c465-4149-899e-2b1e66efa66f.json
Source: policyDefinitions/Monitoring/apply-diagnostic-settings-for-azure-key-vault-to-a-log-analytics-workspace/azurepolicy.json in Azure/Community-Policy, under the MIT licence.
Bylaw