Azure策略DeployIfNotExists:SQL服务器的保留天数必须大于X天

时间:2019-05-29 14:13:33

标签: azure azure-resource-manager azure-policy

我正在尝试开发一个Azure策略(json),以确保对于启用了审核(无需检查)的给定SQL Server,保留天数期限已设置为大于X的值(假设在90天我的情况)。

我尝试使用deployIfNotExists效果,在existenceCondition字段上使用retentionDays(大于90)。在deployment部分中,我将该字段设置为365。

我将策略分配给了一个我拥有一个SQL Server的资源组,其中SQL Server的审核和保留天数等于20。

但是,该政策仍显示为“合规”,且保留天数保持不变。这是代码:

  "if": {
    "field": "type",
    "equals": "Microsoft.Sql/servers"
  },
  "then": {
    "effect": "deployIfNotExists",
    "details": {
      "type": "Microsoft.Sql/servers/auditingSettings",
      "roleDefinitionIds": [
        "/providers/Microsoft.Authorization/roleDefinitions/XXXXXXXX"
      ],
      "existenceCondition": {
        "field": "Microsoft.Sql/servers/auditingSettings/retentionDays",
        "greater": "90"
      },
      "deployment": {
        "properties": {
          "mode": "incremental",
          "template": {
            "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
            "contentVersion": "1.0.0.0",
            "parameters": {
              "resourceName": {
                "type": "string"
              },
              "location": {
                "type": "string"
              },
              "retentionDays": {
                "type": "string"
              }
            },
            "variables": {},
            "resources": [{
              "type": "Microsoft.Sql/servers/auditingSettings",
              "apiVersion": "2017-03-01-preview",
              "name": "[concat(parameters('resourceName'), '/Default')]",
              "location": "[parameters('location')]",
              "dependsOn": [],
              "properties": {
                "retentionDays": "[parameters('retentionDays')]"
              }
            }],
            "outputs": {}
          },
          "parameters": {
            "location": {
              "value": "[field('location')]"
            },
            "resourceName": {
              "value": "[field('name')]"
            },
            "retentionDays": {
              "value": "365"
            }
          }
        }
      }
    }
}

我想知道我是否在正确的位置使用正确的别名。有任何线索吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是我的代码!!!!

{
  "properties": {
    "displayName": "deploy-sql-db-backupshorttermretentionpolicies",
    "policyType": "Custom",
    "mode": "All",
    "description": "Deploy If Not Exists backupshorttermretentionpolicies",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy."
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "retentionDays": {
        "type": "String",
        "metadata": {
          "displayName": "Retention Days",
          "description": "Set the number of Backup Retention Days."
        },
        "defaultValue": "35"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Sql/servers/databases"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Sql/servers/databases/backupShortTermRetentionPolicies",
          "name": "default",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/####
          ],
          "existenceCondition": {
            "field": "Microsoft.Sql/servers/databases/backupShortTermRetentionPolicies/retentionDays",
            "equals": "[parameters('retentionDays')]"
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "serverName": {
                    "type": "string"
                  },
                  "shortTermRetention": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "name": "[concat(parameters('serverName'),'/default')]",
                    "type": "Microsoft.Sql/servers/databases/backupShortTermRetentionPolicies",
                    "apiVersion": "2017-10-01-preview",
                    "properties": {
                      "retentionDays": "[parameters('shortTermRetention')]"
                    }
                  }
                ]
              },
              "parameters": {
                "serverName": {
                  "value": "[field('fullname')]"
                },
                "shortTermRetention": {
                  "value": "[parameters('retentionDays')]"
                }
              }
            }
          }
        }
      }
    }
  }
}