Bylaw

Gallery › Kubernetes › Azure built-in policies

Configure Azure Kubernetes Service clusters to enable Defender profile

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 4.3.0

Microsoft Defender for Containers provides cloud-native Kubernetes security capabilities including environment hardening, workload protection, and run-time protection. When you enable the SecurityProfile.Defender on your Azure Kubernetes Service cluster, an agent is deployed to your cluster to collect security event data. Learn more about Microsoft Defender for Containers: https://docs.microsoft.com/azure/defender-for-cloud/defender-for-containers-introduction?tabs=defender-for-container-arch-aks.

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

Azure Policy JSONASC_Azure_Defender_AKS_SecurityProfile_DINE.json · 310 lines
{
  "properties": {
    "displayName": "Configure Azure Kubernetes Service clusters to enable Defender profile",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Microsoft Defender for Containers provides cloud-native Kubernetes security capabilities including environment hardening, workload protection, and run-time protection. When you enable the SecurityProfile.Defender on your Azure Kubernetes Service cluster, an agent is deployed to your cluster to collect security event data. Learn more about Microsoft Defender for Containers: https://docs.microsoft.com/azure/defender-for-cloud/defender-for-containers-introduction?tabs=defender-for-container-arch-aks.",
    "metadata": {
      "version": "4.3.0",
      "category": "Kubernetes"
    },
    "version": "4.3.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "logAnalyticsWorkspaceResourceId": {
        "type": "String",
        "metadata": {
          "displayName": "LogAnalyticsWorkspaceResourceId",
          "description": "Optional Log Analytics workspace resource id. If provided, will be used as part of the feature configuration. Otherwise, default workspace will be provisioned. Value format should be '/subscriptions/XXX/resourcegroups/XXX/providers/Microsoft.OperationalInsights/workspaces/XXX'.",
          "strongType": "Microsoft.OperationalInsights/workspaces",
          "assignPermissions": true
        },
        "defaultValue": ""
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.ContainerService/managedClusters"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.ContainerService/managedClusters",
          "deploymentScope": "subscription",
          "name": "[field('name')]",
          "evaluationDelay": "PT30M",
          "existenceCondition": {
            "field": "Microsoft.ContainerService/managedClusters/securityProfile.defender.securityMonitoring.enabled",
            "equals": "true"
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/5e93ba01-8f92-4c7a-b12a-801e3df23824",
            "/providers/microsoft.authorization/roleDefinitions/8bb6f106-b146-4ee6-a3f9-b9c5a96e0ae5"
          ],
          "deployment": {
            "location": "westeurope",
            "properties": {
              "mode": "incremental",
              "parameters": {
                "clusterRegion": {
                  "value": "[field('location')]"
                },
                "clusterResourceId": {
                  "value": "[field('id')]"
                },
                "clusterName": {
                  "value": "[field('name')]"
                },
                "logAnalyticsWorkspaceResourceId": {
                  "value": "[parameters('logAnalyticsWorkspaceResourceId')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "clusterRegion": {
                    "type": "string"
                  },
                  "clusterResourceId": {
                    "type": "string"
                  },
                  "clusterName": {
                    "type": "string"
                  },
                  "logAnalyticsWorkspaceResourceId": {
                    "type": "string"
                  }
                },
                "variables": {
                  "locationLongNameToShortMap": {
                    "australiacentral": "CAU",
                    "australiacentral2": "CBR2",
                    "australiaeast": "EAU",
                    "australiasoutheast": "SEAU",
                    "brazilsouth": "CQ",
                    "brazilsoutheast": "BRSE",
                    "canadacentral": "CCA",
                    "canadaeast": "YQ",
                    "centralindia": "CIN",
                    "centralus": "CUS",
                    "eastasia": "EA",
                    "eastus": "EUS",
                    "eastus2": "EUS2",
                    "eastus2euap": "eus2p",
                    "germanynorth": "DEN",
                    "germanywestcentral": "DEWC",
                    "francecentral": "PAR",
                    "francesouth": "MRS",
                    "israelcentral": "ILC",
                    "italynorth": "ITN",
                    "japaneast": "EJP",
                    "japanwest": "OS",
                    "jioindiacentral": "JINC",
                    "jioindiawest": "JINW",
                    "koreacentral": "SE",
                    "koreasouth": "PS",
                    "mexicocentral": "MXC",
                    "northcentralus": "NCUS",
                    "northeurope": "NEU",
                    "norwayeast": "NOE",
                    "norwaywest": "NOW",
                    "polandcentral": "PLC",
                    "qatarcentral": "QLC",
                    "southafricanorth": "JNB",
                    "southafricawest": "CPT",
                    "southcentralus": "SCUS",
                    "southeastasia": "SEA",
                    "southindia": "MA",
                    "spaincentral": "ESC",
                    "swedencentral": "SEC",
                    "switzerlandnorth": "CHN",
                    "switzerlandwest": "CHW",
                    "uaecentral": "AUH",
                    "uaenorth": "DXB",
                    "uksouth": "SUK",
                    "ukwest": "WUK",
                    "westcentralus": "WCUS",
                    "westeurope": "WEU",
                    "westus": "WUS",
                    "westus2": "WUS2",
                    "westus3": "USW3",
                    "usgovvirginia": "USGV",
                    "usgovarizona": "USGA",
                    "chinaeast3": "NE3",
                    "chinanorth3": "NN3",
                    "chinaeast2": "E2"
                  },
                  "locationCode": "[variables('locationLongNameToShortMap')[parameters('clusterRegion')]]",
                  "subscriptionId": "[subscription().subscriptionId]",
                  "shouldProvisionDefaultWorkspace": "[empty(parameters('logAnalyticsWorkspaceResourceId'))]",
                  "defaultRGName": "[concat('DefaultResourceGroup-', variables('locationCode'))]",
                  "workspaceName": "[concat('DefaultWorkspace-', variables('subscriptionId'),'-', variables('locationCode'))]",
                  "deployDefaultAscResourceGroup": "[concat('deployDefaultAscResourceGroup-', uniqueString(deployment().name))]"
                },
                "resources": [
                  {
                    "condition": "[variables('shouldProvisionDefaultWorkspace')]",
                    "type": "Microsoft.Resources/resourceGroups",
                    "name": "[variables('defaultRGName')]",
                    "apiVersion": "2019-05-01",
                    "location": "[parameters('clusterRegion')]"
                  },
                  {
                    "condition": "[variables('shouldProvisionDefaultWorkspace')]",
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('deployDefaultAscResourceGroup')]",
                    "apiVersion": "2020-06-01",
                    "resourceGroup": "[variables('defaultRGName')]",
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "parameters": {
                        "clusterRegion": {
                          "value": "[parameters('clusterRegion')]"
                        },
                        "workspaceName": {
                          "value": "[variables('workspaceName')]"
                        }
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "clusterRegion": {
                            "type": "string"
                          },
                          "workspaceName": {
                            "type": "string"
                          }
                        },
                        "variables": {},
                        "resources": [
                          {
                            "type": "Microsoft.OperationalInsights/workspaces",
                            "name": "[parameters('workspaceName')]",
                            "apiVersion": "2021-06-01",
                            "location": "[parameters('clusterRegion')]",
                            "properties": {
                              "sku": {
                                "name": "pernode"
                              },
                              "retentionInDays": 30,
                              "features": {
                                "searchVersion": 1
                              }
                            }
                          }
                        ]
                      }
                    },
                    "dependsOn": [
                      "[resourceId('Microsoft.Resources/resourceGroups', variables('defaultRGName'))]"
                    ]
                  },
                  {
                    "type": "Microsoft.Resources/deployments",
                    "name": "[concat('securityprofile-deploy-', uniqueString(parameters('clusterResourceId')))]",
                    "apiVersion": "2020-10-01",
                    "subscriptionId": "[variables('subscriptionId')]",
                    "resourceGroup": "[split(parameters('clusterResourceId'),'/')[4]]",
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "parameters": {
                        "workspaceResourceId": {
                          "value": "[if(variables('shouldProvisionDefaultWorkspace'), concat('/subscriptions/', variables('subscriptionId'), '/resourcegroups/', variables('defaultRGName'), '/providers/Microsoft.OperationalInsights/workspaces/', variables('workspaceName')), parameters('logAnalyticsWorkspaceResourceId'))]"
                        },
                        "clusterName": {
                          "value": "[parameters('clusterName')]"
                        },
                        "clusterRegion": {
                          "value": "[parameters('clusterRegion')]"
                        },
                        "aksClusterContent": {
                          "value": "[reference(parameters('clusterResourceId'), '2022-06-01', 'Full')]"
                        }
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "workspaceResourceId": {
                            "type": "string"
                          },
                          "clusterName": {
                            "type": "string"
                          },
                          "clusterRegion": {
                            "type": "string"
                          },
                          "aksClusterContent": {
                            "type": "object"
                          }
                        },
                        "resources": [
                          {
                            "type": "Microsoft.ContainerService/ManagedClusters",
                            "name": "[parameters('clusterName')]",
                            "apiVersion": "2022-06-01",
                            "location": "[parameters('clusterRegion')]",
                            "properties": {
                              "securityProfile": {
                                "defender": {
                                  "securityMonitoring": {
                                    "enabled": true
                                  },
                                  "logAnalyticsWorkspaceResourceId": "[parameters('workspaceResourceId')]"
                                }
                              },
                              "nodeResourceGroup": "[parameters('aksClusterContent').properties.nodeResourceGroup]",
                              "enableRBAC": "[if(contains(parameters('aksClusterContent').properties, 'enableRBAC'), parameters('aksClusterContent').properties.enableRBAC, json('null'))]",
                              "enablePodSecurityPolicy": "[if(contains(parameters('aksClusterContent').properties, 'enablePodSecurityPolicy'), parameters('aksClusterContent').properties.enablePodSecurityPolicy, json('null'))]",
                              "networkProfile": "[if(contains(parameters('aksClusterContent').properties, 'networkProfile'), parameters('aksClusterContent').properties.networkProfile, json('null'))]",
                              "aadProfile": "[if(contains(parameters('aksClusterContent').properties, 'aadProfile'), parameters('aksClusterContent').properties.aadProfile, json('null'))]",
                              "autoScalerProfile": "[if(contains(parameters('aksClusterContent').properties, 'autoScalerProfile'), parameters('aksClusterContent').properties.autoScalerProfile, json('null'))]",
                              "apiServerAccessProfile": "[if(contains(parameters('aksClusterContent').properties, 'apiServerAccessProfile'), parameters('aksClusterContent').properties.apiServerAccessProfile, json('null'))]",
                              "diskEncryptionSetID": "[if(contains(parameters('aksClusterContent').properties, 'diskEncryptionSetID'), parameters('aksClusterContent').properties.diskEncryptionSetID, json('null'))]",
                              "identityProfile": "[if(contains(parameters('aksClusterContent').properties, 'identityProfile'), parameters('aksClusterContent').properties.identityProfile, json('null'))]"
                            }
                          }
                        ]
                      }
                    },
                    "dependsOn": [
                      "[variables('deployDefaultAscResourceGroup')]"
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "4.3.0",
      "4.2.0",
      "4.1.0",
      "4.0.4"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/64def556-fbad-4622-930e-72d1d5589bf5",
  "name": "64def556-fbad-4622-930e-72d1d5589bf5"
}
Bylaw64def556-fbad-4622-930e-72d1d5589bf5.bylaw.ps1 · 44 lines
# Converted from ASC_Azure_Defender_AKS_SecurityProfile_DINE.json by ConvertTo-Bylaw.
Policy '64def556-fbad-4622-930e-72d1d5589bf5' {

    Properties @{
        DisplayName   = 'Configure Azure Kubernetes Service clusters to enable Defender profile'
        Description   = 'Microsoft Defender for Containers provides cloud-native Kubernetes security capabilities including environment hardening, workload protection, and run-time protection. When you enable the SecurityProfile.Defender on your Azure Kubernetes Service cluster, an agent is deployed to your cluster to collect security event data. Learn more about Microsoft Defender for Containers: https://docs.microsoft.com/azure/defender-for-cloud/defender-for-containers-introduction?tabs=defender-for-container-arch-aks.'
        Version       = '4.3.0'
        Category      = 'Kubernetes'
        PolicyVersion = '4.3.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter LogAnalyticsWorkspaceResourceId @{
        Default     = ''
        DisplayName = 'LogAnalyticsWorkspaceResourceId'
        Description = 'Optional Log Analytics workspace resource id. If provided, will be used as part of the feature configuration. Otherwise, default workspace will be provisioned. Value format should be ''/subscriptions/XXX/resourcegroups/XXX/providers/Microsoft.OperationalInsights/workspaces/XXX''.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.OperationalInsights/workspaces'; assignPermissions = $true }
    }

    Resource 'Microsoft.ContainerService/managedClusters'

    DeployIfNotExists 'Microsoft.ContainerService/managedClusters' -Role 'Defender Containers Sensor', 'Defender Kubernetes Agent Operator' -Name (Arm '[field(''name'')]') -EvaluationDelay PT30M -Effect (Param Effect) {
        Require 'securityProfile.defender.securityMonitoring.enabled is true' {
            Field securityProfile.defender.securityMonitoring.enabled | Must -Be 'true'
        }
        Deployment @{
            TemplateFile = './templates/64def556-fbad-4622-930e-72d1d5589bf5.json'
            Scope        = 'Subscription'
            Location     = 'westeurope'
            Parameters   = [ordered]@{
                clusterRegion                   = (Arm '[field(''location'')]')
                clusterResourceId               = (Arm '[field(''id'')]')
                clusterName                     = (Arm '[field(''name'')]')
                logAnalyticsWorkspaceResourceId = (Param LogAnalyticsWorkspaceResourceId)
            }
        }
    }
}

Its deployment templates are written beside it:

Source: built-in-policies/policyDefinitions/Kubernetes/ASC_Azure_Defender_AKS_SecurityProfile_DINE.json in Azure/azure-policy, under the MIT licence.