我尝试使用 Azure Policy 检查我的生产订阅中的所有资源组是否都有“CanNotDelete”锁。
我在这个问题的启发下建立了一个策略,结果可以在下面找到。
在测试这个时,我发现没有锁的资源组被正确地检测为“不合规”。但是,如果资源组包含具有专用锁的资源(Scope=resource,例如仅在 KeyVault 上),则整个资源组将被标记为合规 - 即使只有一个资源真正具有锁。有没有办法设计existenceCondition
使锁必须在整个资源组上?
{
"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": {}
}