Bylaw

Gallery › Security Center - Granular Pricing › Azure built-in policies

Configure Azure Defender for Servers to be enabled (with 'P1' subplan) for all resources (resource level)

Converted Policy DeployIfNotExists Disabled Mode All Version 1.1.0

Azure Defender for Servers provides real-time threat protection for server workloads and generates hardening recommendations as well as alerts about suspicious activities. This policy will enable the Defender for Servers plan (with 'P1' subplan) for all resources (VMs and ARC Machines) in the selected scope (subscription or resource group).

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONASC_Azure_Defender_Servers_EnableP1_ResourceLevel_DINE.json · 97 lines
{
  "properties": {
    "displayName": "Configure Azure Defender for Servers to be enabled (with 'P1' subplan) for all resources (resource level)",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "Azure Defender for Servers provides real-time threat protection for server workloads and generates hardening recommendations as well as alerts about suspicious activities. This policy will enable the Defender for Servers plan (with 'P1' subplan) for all resources (VMs and ARC Machines) in the selected scope (subscription or resource group).",
    "metadata": {
      "version": "1.1.0",
      "category": "Security Center - Granular Pricing"
    },
    "version": "1.1.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": [
          "Microsoft.Compute/virtualMachines",
          "Microsoft.HybridCompute/machines"
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Security/pricings",
          "name": "VirtualMachines",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/fb1c8493-542b-48eb-b624-b4c8fea62acd"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Security/pricings/pricingTier",
                "equals": "Standard"
              },
              {
                "field": "Microsoft.Security/pricings/subPlan",
                "equals": "P1"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "fullResourceName": {
                  "value": "[field('id')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "fullResourceName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.Security/pricings",
                    "apiVersion": "2024-01-01",
                    "scope": "[parameters('fullResourceName')]",
                    "name": "VirtualMachines",
                    "properties": {
                      "subplan": "P1",
                      "pricingTier": "Standard"
                    }
                  }
                ],
                "outputs": {}
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.0",
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/1b8c0040-b224-4ea1-be6a-47254dd5a207",
  "name": "1b8c0040-b224-4ea1-be6a-47254dd5a207"
}
Bylaw1b8c0040-b224-4ea1-be6a-47254dd5a207.bylaw.ps1 · 36 lines
# Converted from ASC_Azure_Defender_Servers_EnableP1_ResourceLevel_DINE.json by ConvertTo-Bylaw.
Policy '1b8c0040-b224-4ea1-be6a-47254dd5a207' {

    Properties @{
        DisplayName   = 'Configure Azure Defender for Servers to be enabled (with ''P1'' subplan) for all resources (resource level)'
        Description   = 'Azure Defender for Servers provides real-time threat protection for server workloads and generates hardening recommendations as well as alerts about suspicious activities. This policy will enable the Defender for Servers plan (with ''P1'' subplan) for all resources (VMs and ARC Machines) in the selected scope (subscription or resource group).'
        Version       = '1.1.0'
        Category      = 'Security Center - Granular Pricing'
        PolicyVersion = '1.1.0'
        Mode          = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource * {

        Require 'type is not one of 2 values' {
            Field type | Must -Not -BeIn @('Microsoft.Compute/virtualMachines', 'Microsoft.HybridCompute/machines') -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Security/pricings' -Role 'Security Admin' -Name 'VirtualMachines' -Effect (Param Effect) {
        Require 'pricingTier is Standard and subPlan is P1' {
            Field pricingTier | Must -Be 'Standard'
            Field subPlan | Must -Be 'P1'
        }
        Deployment -TemplateFile './templates/1b8c0040-b224-4ea1-be6a-47254dd5a207.json' -Parameters ([ordered]@{
                fullResourceName = (Arm '[field(''id'')]')
            })
    }
}

Its deployment templates are written beside it:

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