Bylaw

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

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

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.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) that have the selected tag name and tag value(s).

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

Azure Policy JSONASC_Azure_Defender_Servers_EnableP1_ResourceLevel_ByTag_DINE.json · 112 lines
{
  "properties": {
    "displayName": "Configure Azure Defender for Servers to be enabled ('P1' subplan) for all resources (resource level) with the selected tag",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "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) that have the selected tag name and tag value(s).",
    "metadata": {
      "version": "1.0.0",
      "category": "Security Center - Granular Pricing"
    },
    "version": "1.0.0",
    "parameters": {
      "inclusionTagName": {
        "type": "String",
        "metadata": {
          "displayName": "Inclusion Tag Name",
          "description": "Name of the tag to use for including resources in the scope of this policy. This should be used along with the Inclusion Tag Value parameter."
        },
        "defaultValue": ""
      },
      "inclusionTagValues": {
        "type": "Array",
        "metadata": {
          "displayName": "Inclusion Tag Values",
          "description": "Value of the tag to use for including resources in the scope of this policy (in case of multiple values, use a comma-separated list). This should be used along with the Inclusion Tag Name parameter."
        },
        "defaultValue": []
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "in": [
              "Microsoft.Compute/virtualMachines",
              "Microsoft.HybridCompute/machines"
            ]
          },
          {
            "field": "[concat('tags[', parameters('inclusionTagName'), ']')]",
            "in": "[parameters('inclusionTagValues')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Security/pricings",
          "name": "VirtualMachines",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/fb1c8493-542b-48eb-b624-b4c8fea62acd"
          ],
          "existenceCondition": {
            "field": "Microsoft.Security/pricings/pricingTier",
            "equals": "Standard"
          },
          "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.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/9e4879d9-c2a0-4e40-8017-1a5a5327c843",
  "name": "9e4879d9-c2a0-4e40-8017-1a5a5327c843"
}
Bylaw9e4879d9-c2a0-4e40-8017-1a5a5327c843.bylaw.ps1 · 50 lines
# Converted from ASC_Azure_Defender_Servers_EnableP1_ResourceLevel_ByTag_DINE.json by ConvertTo-Bylaw.
Policy '9e4879d9-c2a0-4e40-8017-1a5a5327c843' {

    Properties @{
        DisplayName   = 'Configure Azure Defender for Servers to be enabled (''P1'' subplan) for all resources (resource level) with the selected tag'
        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) that have the selected tag name and tag value(s).'
        Version       = '1.0.0'
        Category      = 'Security Center - Granular Pricing'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter InclusionTagName @{
        Default     = ''
        DisplayName = 'Inclusion Tag Name'
        Description = 'Name of the tag to use for including resources in the scope of this policy. This should be used along with the Inclusion Tag Value parameter.'
    }
    Parameter InclusionTagValues @{
        Type        = 'Array'
        Default     = @()
        DisplayName = 'Inclusion Tag Values'
        Description = 'Value of the tag to use for including resources in the scope of this policy (in case of multiple values, use a comma-separated list). This should be used along with the Inclusion Tag Name parameter.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource * {

        When {
            Field type | Must -BeIn @('Microsoft.Compute/virtualMachines', 'Microsoft.HybridCompute/machines')
        }

        Require 'tag (Param InclusionTagName) is not one of the InclusionTagValues parameter' {
            Tag (Param InclusionTagName) | Must -Not -BeIn (Param InclusionTagValues) -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Security/pricings' -Role 'Security Admin' -Name 'VirtualMachines' -Effect (Param Effect) {
        Require 'pricingTier is Standard' {
            Field pricingTier | Must -Be 'Standard'
        }
        Deployment -TemplateFile './templates/9e4879d9-c2a0-4e40-8017-1a5a5327c843.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_ByTag_DINE.json in Azure/azure-policy, under the MIT licence.