Bylaw

Gallery › Monitoring › Azure Community Policy

Apply Diagnostic Settings for Azure Public IPAddresses to a Log Analytics workspace

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy automatically deploys diagnostic settings for Azure Public IPAddresses 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": "61cdc27b-2446-42a7-902d-133124fb335d",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Apply Diagnostic Settings for Azure Public IPAddresses to a Log Analytics workspace",
    "description": "This policy automatically deploys diagnostic settings for Azure Public IPAddresses 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"
        }
      },
      "azureRegions": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed Locations",
          "description": "The list of locations that can be specified when deploying resources",
          "strongType": "location"
        }
      },
      "logAnalytics": {
        "type": "string",
        "metadata": {
          "displayName": "logAnalytics",
          "description": "The target Log Analytics Workspace for Azure Diagnostics",
          "strongType": "omsWorkspace"
        }
      },
      "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.Network/publicIPAddresses"
          },
          {
            "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": {
                  "Name": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "logAnalytics": {
                    "type": "string"
                  },
                  "metricsEnabled": {
                    "type": "string"
                  },
                  "logsEnabled": {
                    "type": "string"
                  },
                  "profileName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Network/publicIPAddresses/providers/diagnosticSettings",
                    "apiVersion": "2017-05-01-preview",
                    "name": "[concat(parameters('Name'), '/', '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": "DDoSProtectionNotifications",
                          "enabled": "[parameters('logsEnabled')]"
                        }
                      ]
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat(parameters('logAnalytics'), 'configured for diagnostic logs for ', ': ', parameters('name'))]"
                  }
                }
              },
              "parameters": {
                "location": {
                  "value": "[field('location')]"
                },
                "name": {
                  "value": "[field('name')]"
                },
                "logAnalytics": {
                  "value": "[parameters('logAnalytics')]"
                },
                "metricsEnabled": {
                  "value": "[parameters('metricsEnabled')]"
                },
                "logsEnabled": {
                  "value": "[parameters('logsEnabled')]"
                },
                "profileName": {
                  "value": "[parameters('profileName')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw61cdc27b-2446-42a7-902d-133124fb335d.bylaw.ps1 · 70 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '61cdc27b-2446-42a7-902d-133124fb335d' {

    Properties @{
        DisplayName = 'Apply Diagnostic Settings for Azure Public IPAddresses to a Log Analytics workspace'
        Description = 'This policy automatically deploys diagnostic settings for Azure Public IPAddresses 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 AzureRegions @{
        Type        = 'Array'
        DisplayName = 'Allowed Locations'
        Description = 'The list of locations that can be specified when deploying resources'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter LogAnalytics @{
        Type        = 'string'
        DisplayName = 'logAnalytics'
        Description = 'The target Log Analytics Workspace for Azure Diagnostics'
        Metadata    = [ordered]@{ strongType = 'omsWorkspace' }
    }
    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.Network/publicIPAddresses' {

        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/61cdc27b-2446-42a7-902d-133124fb335d.json' -Parameters ([ordered]@{
                location       = (Arm '[field(''location'')]')
                name           = (Arm '[field(''name'')]')
                logAnalytics   = (Param LogAnalytics)
                metricsEnabled = (Param MetricsEnabled)
                logsEnabled    = (Param LogsEnabled)
                profileName    = (Param ProfileName)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/apply-diagnostic-settings-for-azure-public-ipaddresses-to-a-log-analytics-workspace/azurepolicy.json in Azure/Community-Policy, under the MIT licence.