Bylaw

Gallery › Compute › Azure Community Policy

COMPUTE - Deploy Defender for Servers

Uses Condition Policy AuditIfNotExists DeployIfNotExists Disabled Mode all Version 1.0.0

Uses a DeployIfNotExists policy to automatically deploy the Defender for Servers

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 JSONazurepolicy.json · 88 lines
{
  "name": "71eb9bc3-ca83-4f21-b567-853f6627dff6",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "COMPUTE - Deploy Defender for Servers",
    "description": "Uses a DeployIfNotExists policy to automatically deploy the Defender for Servers",
    "metadata": {
      "category": "Compute",
      "version": "1.0.0"
    },
    "mode": "all",
    "parameters": {
      "AccountLocation": {
        "type": "String",
        "metadata": {
          "description": "Location where the policy should apply"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Resources/subscriptions"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Security/pricings",
          "name": "VirtualMachines",
          "deploymentScope": "subscription",
          "existenceScope": "subscription",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/fb1c8493-542b-48eb-b624-b4c8fea62acd"
          ],
          "existenceCondition": {
            "value": "field(microsoft.security/pricings/pricingTier)",
            "equals": "Free"
          },
          "deployment": {
            "location": "[parameters('AccountLocation')]",
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "AccountLocation": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Security/pricings",
                    "apiVersion": "2018-06-01",
                    "name": "VirtualMachines",
                    "properties": {
                      "pricingTier": "standard"
                    }
                  }
                ],
                "outputs": {}
              },
              "parameters": {
                "AccountLocation": {
                  "value": "[parameters('AccountLocation')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw71eb9bc3-ca83-4f21-b567-853f6627dff6.bylaw.ps1 · 39 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '71eb9bc3-ca83-4f21-b567-853f6627dff6' {

    Properties @{
        DisplayName = 'COMPUTE - Deploy Defender for Servers'
        Description = 'Uses a DeployIfNotExists policy to automatically deploy the Defender for Servers'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter AccountLocation @{
        Type        = 'String'
        Description = 'Location where the policy should apply'
        JsonName    = 'AccountLocation'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Resources/subscriptions'

    DeployIfNotExists 'Microsoft.Security/pricings' -Role 'Security Admin' -Name 'VirtualMachines' -ExistenceScope Subscription -Effect (Param Effect) {
        Require 'Requirement 1' {
            Condition -Compliant '{ "value": "field(microsoft.security/pricings/pricingTier)", "equals": "Free" }'
        }
        Deployment @{
            TemplateFile = './templates/71eb9bc3-ca83-4f21-b567-853f6627dff6.json'
            Scope        = 'Subscription'
            Location     = (Param AccountLocation)
            Parameters   = [ordered]@{
                AccountLocation = (Param AccountLocation)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Compute/compute-deploy-defender-for-servers/azurepolicy.json in Azure/Community-Policy, under the MIT licence.