gpt4 book ai didi

Azure Policy DeployIfNotExists 无法更改值

转载 作者:行者123 更新时间:2023-12-03 03:26:16 26 4
gpt4 key购买 nike

我当前正在创建一个 Azure 策略,如果当前计划不同,该策略(应该)使用计划 P1 部署 Microsoft Defender for Servers。我当前启用了 Microsoft Defender for Servers 的许多订阅,但使用计划 P2 而不是 P1,因此我使用 DeployIfNotExists 创建了一项策略,该策略应将值更改为正确的值,但它不起作用正如预期的那样。

政策如下(我使用的是 Terraform):

resource "azurerm_policy_definition" "dfservers_policy" {
name = "defenderForServers"
policy_type = "Custom"
mode = "All"
display_name = "Deploy Azure Defender for servers with plan"
description = "Azure Defender for servers provides real-time threat protection for server workloads and generates hardening recommendations as well as alerts about suspicious activities."
management_group_id = data.azurerm_management_group.root_management_group.id

metadata = <<METADATA
{
"category": "Security Center"
}

METADATA


policy_rule = <<POLICY_RULE
{
"if": {
"field": "type",
"equals": "Microsoft.Resources/subscriptions"
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.Security/pricings",
"name": "VirtualMachines",
"deploymentScope": "subscription",
"existenceScope": "subscription",
"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": "[parameters('plan_type')]"
}
]
},
"deployment": {
"location": "westeurope",
"properties": {
"mode": "incremental",
"parameters": {
"plan_type": {
"value": "[parameters('plan_type')]"
}
},
"template": {
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"plan_type": {
"type": "String",
"metadata": {
"displayName": "Server Plan Type",
"description": "Define which plan type to use. Either P1 or P2"
},
"allowedValues": [
"P1",
"P2"
],
"defaultValue": "P2"
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Security/pricings",
"apiVersion": "2018-06-01",
"name": "VirtualMachines",
"properties": {
"pricingTier": "Standard",
"subPlan": "[parameters('plan_type')]"
}
}
],
"outputs": {}
}
}
}
}
}
}
POLICY_RULE


parameters = <<PARAMETERS
{
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the policy"
},
"allowedValues": [
"DeployIfNotExists",
"Disabled"
],
"defaultValue": "DeployIfNotExists"
},
"plan_type": {
"type": "String",
"metadata": {
"displayName": "Server Plan Type",
"description": "Define which plan type to use. Either P1 or P2"
},
"allowedValues": [
"P1",
"P2"
],
"defaultValue": "P2"
}
}
PARAMETERS

}

分配如下:

resource "azurerm_management_group_policy_assignment" "dfserver_sandbox_assignment" {
name = "Def4ServersSB"
display_name = "Deploy 'Defender for Servers' with Plan P1"
location = var.location

policy_definition_id = azurerm_policy_definition.dfservers_policy.id
management_group_id = data.azurerm_management_group.sandbox_management_group.id

identity {
type = "SystemAssigned"
}

parameters = jsonencode({
"plan_type": {
"value": "P1"
},
})
}

我还将安全管理员角色附加到系统管理身份,因为似乎存在相关错误:

# This is needed due to a bug where the automatically created System Managed Identity does not have the required permissions (https://github.com/hashicorp/terraform-provider-azurerm/issues/6486)
resource "azurerm_role_assignment" "ra_dfserver_sandbox" {
scope = azurerm_management_group_policy_assignment.dfserver_sandbox_assignment.management_group_id
role_definition_name = "Security Admin"
principal_id = azurerm_management_group_policy_assignment.dfserver_sandbox_assignment.identity[0].principal_id
}

一段时间后,该政策似乎按预期发挥作用,获得了一些非投诉订阅: Non compliant subscriptions

我还能够创建修复任务并成功运行它: Deployment list

那么一切都好,对吧?不。如果去检查订阅的值,我可以看到它仍然是 P2: Old values continue

可能出现错误,参数设置为 P2 而不是 P1?没有: Deployment Value

也许由于某种原因部署没有运行?这是我当前的论文,但在值的事件日志中显示: Activity Log

你们知道为什么会发生这种情况吗?任何光线都会非常感激:)

最佳答案

您正在使用旧的 Microsoft.Security/pricings API 版本,请更新您的模板以使用 API 版本 2022-03-01

关于Azure Policy DeployIfNotExists 无法更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75619636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com