【问题标题】:How do you policy enforce integer number of tag value in Azure你如何在 Azure 中强制执行整数个标记值
【发布时间】:2020-10-21 14:14:23
【问题描述】:

我的 Azure 政策拒绝评估以下错误的表达式。我认为问题在于,当您在门户中提供标记值时,即使打算传递整数值,它们也会作为字符串传递给 ARM。试图弄清楚如何为结果标签强制执行整数值

{
 "field": "tags['Longevity']",
 "less": 1
}

错误

"The policy assignment 'd9c1d0b06de841559a1cbafe' associated with the policy definition 'dee67dc2-7393-4c02-916f-92511146c970' could not be evaluated. 
A 'less' or 'lessOrEquals' or 'greater' or 'greaterOrEquals' expression expects operands of same type for comparison. The supported types are string, integer, float, ISO 8601 datetime. Please either fix the 
policy or remove the policy assignment to unblock. See https://aka.ms/policy-conditions for usage details."

【问题讨论】:

    标签: azure-policy


    【解决方案1】:

    微软支持提供了答案 答案在下面,它按预期工作。必须使用 concat() 函数看起来像是逃避抱怨方括号内的方括号

    {
     "value": "[int(field(concat('tags[', 'Longevity', ']')))]",
     "less": 0
    },
    

    【讨论】:

      【解决方案2】:

      我还可以通过在策略中提供以下格式(即“less”:1)来重现错误。

      "field": "[concat('tags[', parameters('tagName'), ']')]",
      "less": 1
      

      我看到标签的底层系统类型是“System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.String]”所以是的,你的假设是正确的,即,当您在门户中提供标记值时,即使打算传递整数值,它们也会作为字符串传递给 ARM。

      我尝试了以下策略来为标签值强制使用整数(作为字符串),我相信它有点工作(仅适用于正数),因为如果我提供了

      • 以下策略第 26 行中的“1”,然后如果我尝试创建资源(例如 Azure VM),则如果提供的标记值为 0,则验证通过,并且在所有其他情况下验证失败。
      • 以下策略第 26 行中的“2”,然后如果我尝试创建资源(例如 Azure VM),则如果提供的标记值介于 -1 和 1 之间,则验证通过,并且在所有其他情况下验证失败。
      • ...
      • ...
      • ...

      所以我相信即使标签的底层系统类型是字符串,如果我们提供“less”作为condition,数字字符串比较也会在后端进行。

      {
        "properties": {
          "displayName": "Require a tag and its value on resources Test",
          "policyType": "Custom",
          "mode": "Indexed",
          "metadata": {
            "version": "1.0.1",
            "createdBy": "xxxxxxxxxxxxxxxxxxxxxxx",
            "createdOn": "2020-10-22T07:58:29.0108355Z",
            "updatedBy": "xxxxxxxxxxxxxxxxxxxxxxx",
            "updatedOn": "2020-10-22T10:45:42.4517009Z"
          },
          "parameters": {
            "tagName": {
              "type": "String",
              "metadata": {
                "displayName": "Tag Name",
                "description": "Name of the tag, such as 'environment'"
              }
            }
          },
          "policyRule": {
            "if": {
              "not": {
                "field": "[concat('tags[', parameters('tagName'), ']')]",
                "less": "1"
              }
            },
            "then": {
              "effect": "deny"
            }
          }
        },
        "id": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/providers/Microsoft.Authorization/policyDefinitions/xxxxxxxxxxxxxxxxxxxxxxx",
        "type": "Microsoft.Authorization/policyDefinitions",
        "name": "xxxxxxxxxxxxxxxxxxxxxxx"
      }
      

      但是,如果有兴趣,在this Uservoice 或反馈论坛中,您可以提出功能请求以允许非字符串类型的标记值。

      【讨论】:

      • 测试,违反直觉,需要在定义中提供 string 的值以允许在评估期间进行整数比较,但令人惊讶的是它有效
      • 这对数字很有效,但不幸的是允许输入通过过滤器的字符串值,所以问题仍然存在如何强制整数值
      • 是的,你是对的。为此,我们需要一个允许非字符串类型的标签值的功能。这就是我建议如果有兴趣在 UserVoice 中提出相同功能请求的原因。
      • 解决方案发布在下方,来自 Microsoft 支持
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      相关资源
      最近更新 更多