gpt4 book ai didi

azure - 如何通过 Azure 策略审核诊断设置中的日志记录类别?

转载 作者:行者123 更新时间:2023-12-04 17:26:15 35 4
gpt4 key购买 nike

我有一项策略,用于在不存在具有特定 Azure 服务的特定配置的诊断设置时进行审核。这将应用于多个服务(事件中心、Key Vault、Postgres 单服务器),以确保预定义的日志记录配置到位。

我面临的问题是审核每个诊断设置中选择的特定日志记录类别。以 Postgres 为例,它有以下日志类别:

  • PostgreSQLLogs
  • QueryStoreRuntimeStatistics
  • QueryStoreWaitStatistics

并说我只想强制在我的 DS 中仅选择“PostgreSQLLogs”。使用策略别名“Microsoft.Insights/diagnosticSettings/logs[*].enabled”这将意味着如下配置:[true, false, false]。因此,这是我应该在策略中设置的值,以审核其合规性。但无论我在策略中尝试哪种值,Azure 都不会接受或无法实现我的目标。

以下是我的策略规则代码:

"policyRule": {
"if": {
"equals": "Microsoft.DBforPostgreSQL/servers",
"field": "type"
},
"then": {
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[ true, false, false ]"
},
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
"equals": "['PostgreSQLLogs']"
},
{
"field": "Microsoft.Insights/diagnosticSettings/workspaceId",
"matchInsensitively": "[parameters('my_logAnalytics')]"
}
]
}
},
"effect": "AuditIfNotExists"
}

保存在门户中时会生成此错误:

readyState: 4responseText: {"error":{"code":"InvalidPolicyParameters","message":"A function or parameter in policy 'e8b95433-42c5-4ed4-8a0c-1e7ad5ac2572' could not be validated. If using template functions, try following the tips in: https://aka.ms/policy-avoiding-template-failures. The inner exception 'Unable to parse language expression ' true, false, false ': expected token 'LeftParenthesis' and actual 'Comma'.'."}}status: 400statusText: error

引用数组的元素会引发相同的错误:

        {
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[ 'true','false','false' ]"
},

当我尝试这样的事情时,它会被接受:

        {
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "[ 'true' ]"
},

但这不是我想要的,因为它将我的资源标记为不符合以下消息(请参阅 screenshot ):

Reason for non-compliance: Current value must be equal to the target value
Field: Microsoft.Insights/diagnosticSettings/logs[*].enabled
Path: properties.logs[*].enabled
Current value: [true,false,false]
Target value: "true"

最佳答案

如果我理解正确的话,您需要检查 logs 数组的每个值并测试 .enabled 时是否为 true .categoryPostgresSQLLogs。您确实需要使用 count这里 - “这会计算条件表达式的每个 [*] 别名数组成员并对真实结果求和”。您可以在条件中使用复杂的表达式:

      "existenceCondition": {
"allOf": [
{
"count": {
"field": "Microsoft.Insights/diagnosticSettings/logs[*]",
"where": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "True"
},
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
"equals": "PostgresSQLLogs"
}
]
}
},
"greater": 0
},
{
"field": "Microsoft.Insights/diagnosticSettings/workspaceId",
"matchInsensitively": "[parameters('my_logAnalytics')]"
}
]
}

对于其他类别值,您需要在外部“allOf”内部有一个类似的计数 block 。

关于azure - 如何通过 Azure 策略审核诊断设置中的日志记录类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63171019/

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