You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Policy实现特定前缀标签防篡改(删除/修改)的方案求助

问题分析

你的两个Policy未生效的核心原因如下:

  1. 模式不匹配:使用Indexed模式仅覆盖支持标签的普通资源,不包含订阅这类顶层资源,需改为All模式。
  2. 资源类型错误:Microsoft.Resources/tags并非订阅级标签对应的资源类型,订阅的正确资源类型是Microsoft.Resources/subscriptions。
  3. 标签匹配逻辑错误:原条件"field": "tags", "contains": "prefix"是检查标签值包含前缀,而非标签键;且未正确实现“标签键以指定前缀开头”的匹配规则。
  4. 动作名称不精确:原delete是泛称,需使用Azure RBAC定义的具体标签操作动作。
解决方案

以下是针对订阅级特定前缀标签保护的正确Policy示例,支持自定义前缀参数,同时阻止标签修改与删除操作:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Resources/subscriptions"
        },
        {
          "value": "[anyOf(keys(tags()), startsWith(@, parameters('tagPrefix')))]",
          "equals": true
        }
      ]
    },
    "then": {
      "effect": "DenyAction",
      "details": {
        "actionNames": [
          "Microsoft.Resources/subscriptions/tags/write",
          "Microsoft.Resources/subscriptions/tags/delete"
        ]
      }
    }
  },
  "parameters": {
    "tagPrefix": {
      "type": "String",
      "metadata": {
        "description": "需要保护的标签前缀,例如 'cmdb-'、'cost-' 等"
      }
    }
  }
}

关键说明

  • mode: All:确保Policy覆盖订阅这类顶层资源。
  • 标签匹配逻辑:anyOf(keys(tags()), startsWith(@, parameters('tagPrefix')))用于检查订阅的标签键中是否存在以指定前缀开头的项。
  • 动作范围:
    • Microsoft.Resources/subscriptions/tags/write:阻止修改或添加符合前缀规则的标签。
    • Microsoft.Resources/subscriptions/tags/delete:阻止删除符合前缀规则的标签。

扩展:同时保护资源组标签

如果需要同步保护资源组上的特定前缀标签,可调整Policy如下:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "anyOf": [
            {
              "field": "type",
              "equals": "Microsoft.Resources/subscriptions"
            },
            {
              "field": "type",
              "equals": "Microsoft.Resources/resourceGroups"
            }
          ]
        },
        {
          "value": "[anyOf(keys(tags()), startsWith(@, parameters('tagPrefix')))]",
          "equals": true
        }
      ]
    },
    "then": {
      "effect": "DenyAction",
      "details": {
        "actionNames": [
          "Microsoft.Resources/subscriptions/tags/write",
          "Microsoft.Resources/subscriptions/tags/delete",
          "Microsoft.Resources/resourceGroups/tags/write",
          "Microsoft.Resources/resourceGroups/tags/delete"
        ]
      }
    }
  },
  "parameters": {
    "tagPrefix": {
      "type": "String",
      "metadata": {
        "description": "需要保护的标签前缀,例如 'cmdb-'、'cost-' 等"
      }
    }
  }
}

验证步骤

  1. 将Policy分配至目标订阅(或管理组,覆盖下属所有订阅)。
  2. 使用具备订阅/资源组标签修改权限的账号,尝试删除或修改符合前缀规则的标签,验证是否被阻止。

内容的提问来源于stack exchange,提问作者chr0nk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:50:20