Bylaw

Gallery › Security Center › Azure built-in policies

Deploy Workflow Automation for Microsoft Defender for Cloud alerts

Uses Condition Policy DeployIfNotExists Mode All Version 5.0.1

Enable automation of Microsoft Defender for Cloud alerts. This policy deploys a workflow automation with your conditions and triggers on the assigned scope. To deploy this policy on newly created subscriptions, open the Compliance tab, select the relevant non-compliant assignment and create a remediation task.

ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.

Azure Policy JSONASC_WorkflowAutomationAzureSecurityCenterAlerts_DINE.json · 320 lines
{
  "properties": {
    "displayName": "Deploy Workflow Automation for Microsoft Defender for Cloud alerts",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "Enable automation of Microsoft Defender for Cloud alerts. This policy deploys a workflow automation with your conditions and triggers on the assigned scope. To deploy this policy on newly created subscriptions, open the Compliance tab, select the relevant non-compliant assignment and create a remediation task.",
    "metadata": {
      "version": "5.0.1",
      "category": "Security Center"
    },
    "version": "5.0.1",
    "parameters": {
      "automationName": {
        "type": "String",
        "metadata": {
          "displayName": "Automation name",
          "description": "This is the automation name."
        }
      },
      "resourceGroupName": {
        "type": "String",
        "metadata": {
          "displayName": "Resource group name",
          "description": "The resource group name where the workflow automation is created. If you enter a name for a resource group that doesn't exist, it'll be created in the subscription."
        }
      },
      "resourceGroupLocation": {
        "type": "String",
        "metadata": {
          "displayName": "Resource group location",
          "description": "The location where the resource group and the workflow automation are created.",
          "strongType": "location"
        }
      },
      "createResourceGroup": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Create resource group",
          "description": "If a resource group does not exists in the scope, a new resource group will be created. If the resource group exists and this flag is set to 'true' the policy will re-deploy the resource group. Please note this will reset any Azure Tag on the resource group."
        },
        "allowedValues": [
          true,
          false
        ],
        "defaultValue": true
      },
      "alertName": {
        "type": "String",
        "metadata": {
          "displayName": "Alert name contains",
          "description": "String included in the required alert name. For a full reference list of Microsoft Defender for Cloud's alerts, see https://docs.microsoft.com/azure/security-center/alerts-reference."
        },
        "defaultValue": ""
      },
      "alertSeverities": {
        "type": "Array",
        "metadata": {
          "displayName": "Alert severities",
          "description": "Determines alert severities. Example: High;Medium;Low;"
        },
        "allowedValues": [
          "High",
          "Medium",
          "Low"
        ],
        "defaultValue": [
          "High",
          "Medium",
          "Low"
        ]
      },
      "logicAppResourceId": {
        "type": "String",
        "metadata": {
          "displayName": "Logic App",
          "description": "The Logic App that is triggered.",
          "strongType": "Microsoft.Logic/workflows",
          "assignPermissions": true
        }
      },
      "logicAppTrigger": {
        "type": "String",
        "metadata": {
          "displayName": "Logic app trigger",
          "description": "The trigger connector of the logic app that is triggered. Possible values: 'Manual (Incoming HTTP request)', 'When an Azure Security Center Alert is created or triggered'."
        },
        "allowedValues": [
          "Manual (Incoming HTTP request)",
          "When an Azure Security Center Alert is created or triggered"
        ]
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Resources/subscriptions"
      },
      "then": {
        "effect": "deployIfNotExists",
        "details": {
          "type": "Microsoft.Security/automations",
          "name": "[parameters('automationName')]",
          "existenceScope": "resourcegroup",
          "ResourceGroupName": "[parameters('resourceGroupName')]",
          "deploymentScope": "subscription",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Security/automations/isEnabled",
                "equals": true
              },
              {
                "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*].expectedValue",
                "in": "[union(parameters('alertSeverities'),if(equals(parameters('alertName'), ''), array('3.'), array(parameters('alertName'))))]"
              },
              {
                "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*].propertyJPath",
                "in": "[union(array('Severity'),if(equals(parameters('alertName'), ''), array('Version'), array('AlertDisplayName')))]"
              },
              {
                "count": {
                  "value": "[parameters('alertSeverities')]",
                  "name": "alertSeverity",
                  "where": {
                    "count": {
                      "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*]",
                      "where": {
                        "allOf": [
                          {
                            "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*].propertyJPath",
                            "equals": "Severity"
                          },
                          {
                            "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*].expectedValue",
                            "equals": "[current('alertSeverity')]"
                          }
                        ]
                      }
                    },
                    "equals": 1
                  }
                },
                "equals": "[length(parameters('alertSeverities'))]"
              }
            ]
          },
          "deployment": {
            "location": "westeurope",
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "automationName": {
                    "type": "string"
                  },
                  "resourceGroupName": {
                    "type": "string"
                  },
                  "resourceGroupLocation": {
                    "type": "string"
                  },
                  "createResourceGroup": {
                    "type": "bool"
                  },
                  "alertName": {
                    "type": "string"
                  },
                  "alertSeverities": {
                    "type": "array"
                  },
                  "logicAppResourceId": {
                    "type": "string"
                  },
                  "logicAppTrigger": {
                    "type": "string"
                  },
                  "guidValue": {
                    "type": "string",
                    "defaultValue": "[newGuid()]"
                  }
                },
                "variables": {
                  "scopeDescription": "scope for subscription {0}",
                  "alertSeveritiesLength": "[length(parameters('alertSeverities'))]",
                  "alertSeveritiesLengthIfEmpty": "[if(equals(variables('alertSeveritiesLength'), 0), 1, variables('alertSeveritiesLength'))]",
                  "severityMap": {
                    "High": "high",
                    "Medium": "medium",
                    "Low": "low"
                  },
                  "triggerMap": {
                    "Manual (Incoming HTTP request)": "manual",
                    "When an Azure Security Center Alert is created or triggered": "When_a_Microsoft_Defender_for_Cloud_alert_is_created_or_triggered"
                  }
                },
                "resources": [
                  {
                    "condition": "[parameters('createResourceGroup')]",
                    "name": "[parameters('resourceGroupName')]",
                    "type": "Microsoft.Resources/resourceGroups",
                    "apiVersion": "2019-10-01",
                    "location": "[parameters('resourceGroupLocation')]"
                  },
                  {
                    "type": "Microsoft.Resources/deployments",
                    "apiVersion": "2019-10-01",
                    "name": "[concat('nestedAutomationDeployment', '_', parameters('guidValue'))]",
                    "resourceGroup": "[parameters('resourceGroupName')]",
                    "dependsOn": [
                      "[resourceId('Microsoft.Resources/resourceGroups/', parameters('resourceGroupName'))]"
                    ],
                    "properties": {
                      "mode": "Incremental",
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {},
                        "variables": {},
                        "resources": [
                          {
                            "tags": {},
                            "apiVersion": "2019-01-01-preview",
                            "location": "[parameters('resourceGroupLocation')]",
                            "name": "[parameters('automationName')]",
                            "type": "Microsoft.Security/automations",
                            "dependsOn": [],
                            "properties": {
                              "description": "Workflow Automation for Microsoft Defender for Cloud alerts via policy",
                              "isEnabled": true,
                              "scopes": [
                                {
                                  "description": "[replace(variables('scopeDescription'),'{0}', subscription().subscriptionId)]",
                                  "scopePath": "[subscription().id]"
                                }
                              ],
                              "sources": [
                                {
                                  "eventSource": "Alerts",
                                  "copy": [
                                    {
                                      "name": "ruleSets",
                                      "count": "[variables('alertSeveritiesLengthIfEmpty')]",
                                      "input": {
                                        "rules": [
                                          {
                                            "propertyJPath": "[if(equals(parameters('alertName'), ''), 'Version', 'AlertDisplayName')]",
                                            "propertyType": "string",
                                            "expectedValue": "[if(equals(parameters('alertName'), ''), '3.', parameters('alertName'))]",
                                            "operator": "Contains"
                                          },
                                          {
                                            "propertyJPath": "Severity",
                                            "propertyType": "string",
                                            "expectedValue": "[variables('severityMap')[parameters('alertSeverities')[mod(copyIndex('ruleSets'), variables('alertSeveritiesLengthIfEmpty'))]]]",
                                            "operator": "Equals"
                                          }
                                        ]
                                      }
                                    }
                                  ]
                                }
                              ],
                              "actions": [
                                {
                                  "actionType": "LogicApp",
                                  "logicAppResourceId": "[parameters('logicAppResourceId')]",
                                  "uri": "[listCallbackUrl(concat(parameters('logicAppResourceId'), '/triggers/', variables('triggerMap')[parameters('logicAppTrigger')]),'2016-06-01').value]"
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "automationName": {
                  "value": "[parameters('automationName')]"
                },
                "resourceGroupName": {
                  "value": "[parameters('resourceGroupName')]"
                },
                "resourceGroupLocation": {
                  "value": "[parameters('resourceGroupLocation')]"
                },
                "createResourceGroup": {
                  "value": "[parameters('createResourceGroup')]"
                },
                "alertName": {
                  "value": "[parameters('alertName')]"
                },
                "alertSeverities": {
                  "value": "[parameters('alertSeverities')]"
                },
                "logicAppResourceId": {
                  "value": "[parameters('logicAppResourceId')]"
                },
                "logicAppTrigger": {
                  "value": "[parameters('logicAppTrigger')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "5.0.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/f1525828-9a90-4fcf-be48-268cdd02361e",
  "name": "f1525828-9a90-4fcf-be48-268cdd02361e"
}
Bylawf1525828-9a90-4fcf-be48-268cdd02361e.bylaw.ps1 · 106 lines
# Converted from ASC_WorkflowAutomationAzureSecurityCenterAlerts_DINE.json by ConvertTo-Bylaw.
Policy 'f1525828-9a90-4fcf-be48-268cdd02361e' {

    Properties @{
        DisplayName   = 'Deploy Workflow Automation for Microsoft Defender for Cloud alerts'
        Description   = 'Enable automation of Microsoft Defender for Cloud alerts. This policy deploys a workflow automation with your conditions and triggers on the assigned scope. To deploy this policy on newly created subscriptions, open the Compliance tab, select the relevant non-compliant assignment and create a remediation task.'
        Version       = '5.0.1'
        Category      = 'Security Center'
        PolicyVersion = '5.0.1'
        Mode          = 'All'
    }

    Parameter AutomationName @{
        Type        = 'String'
        DisplayName = 'Automation name'
        Description = 'This is the automation name.'
    }
    Parameter ResourceGroupName @{
        Type        = 'String'
        DisplayName = 'Resource group name'
        Description = 'The resource group name where the workflow automation is created. If you enter a name for a resource group that doesn''t exist, it''ll be created in the subscription.'
    }
    Parameter ResourceGroupLocation @{
        Type        = 'String'
        DisplayName = 'Resource group location'
        Description = 'The location where the resource group and the workflow automation are created.'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter CreateResourceGroup @{
        Type          = 'Boolean'
        AllowedValues = @($true, $false)
        Default       = $true
        DisplayName   = 'Create resource group'
        Description   = 'If a resource group does not exists in the scope, a new resource group will be created. If the resource group exists and this flag is set to ''true'' the policy will re-deploy the resource group. Please note this will reset any Azure Tag on the resource group.'
    }
    Parameter AlertName @{
        Default     = ''
        DisplayName = 'Alert name contains'
        Description = 'String included in the required alert name. For a full reference list of Microsoft Defender for Cloud''s alerts, see https://docs.microsoft.com/azure/security-center/alerts-reference.'
    }
    Parameter AlertSeverities @{
        Type          = 'Array'
        AllowedValues = @('High', 'Medium', 'Low')
        Default       = @('High', 'Medium', 'Low')
        DisplayName   = 'Alert severities'
        Description   = 'Determines alert severities. Example: High;Medium;Low;'
    }
    Parameter LogicAppResourceId @{
        Type        = 'String'
        DisplayName = 'Logic App'
        Description = 'The Logic App that is triggered.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Logic/workflows'; assignPermissions = $true }
    }
    Parameter LogicAppTrigger @{
        Type          = 'String'
        AllowedValues = @('Manual (Incoming HTTP request)', 'When an Azure Security Center Alert is created or triggered')
        DisplayName   = 'Logic app trigger'
        Description   = 'The trigger connector of the logic app that is triggered. Possible values: ''Manual (Incoming HTTP request)'', ''When an Azure Security Center Alert is created or triggered''.'
    }

    Resource 'Microsoft.Resources/subscriptions'

    DeployIfNotExists 'Microsoft.Security/automations' -Role 'Contributor' -Name (Param AutomationName) -ResourceGroupName (Param ResourceGroupName) -ExistenceScope ResourceGroup {
        Require 'isEnabled, sources, ruleSets and 6 more are as required' {
            Field isEnabled | Must -Be $true
            Field sources | Must -Exist
            Each 'sources[*]' {
                Field ruleSets | Must -Exist
                Each 'ruleSets[*]' {
                    Field rules | Must -Exist
                    Each 'rules[*]' {
                        Field expectedValue | Must -BeIn (Arm '[union(parameters(''alertSeverities''),if(equals(parameters(''alertName''), ''''), array(''3.''), array(parameters(''alertName''))))]') -Strict
                    }
                }
            }
            Field sources | Must -Exist
            Each 'sources[*]' {
                Field ruleSets | Must -Exist
                Each 'ruleSets[*]' {
                    Field rules | Must -Exist
                    Each 'rules[*]' {
                        Field propertyJPath | Must -BeIn (Arm '[union(array(''Severity''),if(equals(parameters(''alertName''), ''''), array(''Version''), array(''AlertDisplayName'')))]') -Strict
                    }
                }
            }
            Count -Value (Param AlertSeverities) -Name alertSeverity -Where {
                Condition '{ "count": { "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*]", "where": { "allOf": [ { "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*].propertyJPath", "equals": "Severity" }, { "field": "Microsoft.Security/automations/sources[*].ruleSets[*].rules[*].expectedValue", "equals": "[current(''alertSeverity'')]" } ] } }, "equals": 1 }'
            } | Must -Be (Param AlertSeverities -Length)
        }
        Deployment @{
            TemplateFile = './templates/f1525828-9a90-4fcf-be48-268cdd02361e.json'
            Scope        = 'Subscription'
            Location     = 'westeurope'
            Parameters   = [ordered]@{
                automationName        = (Param AutomationName)
                resourceGroupName     = (Param ResourceGroupName)
                resourceGroupLocation = (Param ResourceGroupLocation)
                createResourceGroup   = (Param CreateResourceGroup)
                alertName             = (Param AlertName)
                alertSeverities       = (Param AlertSeverities)
                logicAppResourceId    = (Param LogicAppResourceId)
                logicAppTrigger       = (Param LogicAppTrigger)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Security Center/ASC_WorkflowAutomationAzureSecurityCenterAlerts_DINE.json in Azure/azure-policy, under the MIT licence.