Azure Policy: Delete lock on resource group

不打扰是莪最后的温柔 提交于 2020-04-16 02:27:15

问题


I try do use Azure Policy to check if all resource groups in my production subscription have a "CanNotDelete" lock.

I built a policy inspired by this question, the result can be found below.

While testing this, I found out that resource groups without a lock are correctly detected as "non-compliant". However, if a resource group contains a resource which has a dedicated lock (Scope=resource, e.g. only on a KeyVault), the whole resource group will be marked as compliant - even tough only one resource really has a lock. Is there a way to design the existenceCondition so that the lock has to be on the whole resource group?

{
  "mode": "All",
  "policyRule": {
    "if": {
      "field": "type",
      "equals": "Microsoft.Resources/subscriptions/resourceGroups"
    },
    "then": {
      "effect": "deployIfNotExists",
      "details": {
        "type": "Microsoft.Authorization/locks",
        "existenceCondition": {
          "field": "Microsoft.Authorization/locks/level",
          "equals": "CanNotDelete"
        },
        "roleDefinitionIds": [
          "/providers/Microsoft.Authorization/roleDefinitions/18d7d88d-d35e-4fb5-a5c3-7773c20a72d9",
          "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
        ],
        "deployment": {
          "properties": {
            "mode": "incremental",
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
              "contentVersion": "1.0.0.0",
              "resources": [
                {
                  "type": "Microsoft.Authorization/locks",
                  "apiVersion": "2017-04-01",
                  "name": "ResourceLock",
                  "properties": {
                    "level": "CanNotDelete",
                    "notes": "Prevent accidental deletion of resources"
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "parameters": {}
}


回答1:


I do not think so since there is nothing in the locks property that details what scope level is being blocked.



来源:https://stackoverflow.com/questions/60602082/azure-policy-delete-lock-on-resource-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!