微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

psql 防火墙的自定义 Azure 策略不起作用

如何解决psql 防火墙的自定义 Azure 策略不起作用

我创建了一个自定义策略,不允许在 Azure Postgresql 服务器的防火墙规则中使用 IP:0.0.0.0,但是它在资源合规性下显示 0 资源,并且当我使用 startIP 创建防火墙规则时它不会拒绝它: 0.0.0.0

这是我的代码

resource "azurerm_policy_deFinition" "db_fw" {
  name         = "ap-psqldb-fw-test"
  policy_type  = "Custom"
  mode         = "Indexed"
  display_name = "Test policy for psql firewall rule"

  Metadata = <<MetaDATA
    {
      "version": "1.0.2","category": "sql"
    }

MetaDATA


  policy_rule = file("../src/mypolicy.json")

}
resource "azurerm_policy_set_deFinition" "ap_set" {
  name         = "apset-db-fw-test"
  policy_type  = "Custom"
  display_name = "Test policyset for psql firewall rule"

  policy_deFinition_reference {
    policy_deFinition_id = "/subscriptions/mysubid/providers/Microsoft.Authorization/policyDeFinitions/ap-psqldb-fw-test"
  }
}
resource "azurerm_policy_assignment" "ap_assign" {
  name                 = "test-policy-assignment"
  scope                = var.resource_group_id
  policy_deFinition_id = azurerm_policy_deFinition.db_fw.id
  description          = "Testing Policy Assignment"
  display_name         = "Test DB Policy Assignment"

  Metadata = <<MetaDATA
    {
      "version": "1.0.2","category": "sql"
    }
MetaDATA

}

mypolicy.json:

{
    "if": {
      "anyOf": [
        {
          "allOf": [
            {
                "field": "type","equals": "Microsoft.DBforPostgresql/servers/firewallRules"
            },{
                "field": "Microsoft.DBforPostgresql/servers/firewallRules/startIpAddress","equals": "0.0.0.0"
            }
          ]
        },{
          "allOf": [
            {
                "field": "type",{
                "field": "Microsoft.DBforPostgresql/servers/firewallRules/endIpAddress","equals": "0.0.0.0"
            }
          ]
        }
      ]
    },"then" : {
      "effect" : "Deny"
    }
}

合规状态为:合规 资源合规性:100%(0 分之 0) 范围:mysub/myresourcegroup

我想知道我在这个设置中缺少什么? 如果有人能帮忙解决这个问题,我将不胜感激。

解决方法

以下是对我有用的政策:

政策定义:

 {
  "properties": {
    "displayName": "Not allow unspecified IP/Not allow SourceIP equal to EndIP","policyType": "Custom","mode": "All","description": "","metadata": {
      "category": "SQL","version": "1.0.2"
    },"parameters": {
      "effect": {
        "type": "String","metadata": {
          "description": "Enable or disable the execution of the policy","displayName": "Effect"
        },"allowedValues": [
          "audit","disabled","deny"
        ],"defaultValue": "deny"
      },"listOfStartIpAddresses": {
        "type": "Array","metadata": {
          "description": "List of not-allowed Start IP Addresses for PSQL","displayName": "List of not-allowed Start IP Addresses for PSQL"
        },"defaultValue": [
          "0.0.0.0"
        ]
      }
    },"policyRule": {
      "if": {
        "anyof": [
          {
            "field": "Microsoft.DBforPostgreSQL/servers/firewallRules/startIpAddress","notEquals": "[field('Microsoft.DBforPostgreSQL/servers/firewallRules/endIpAddress')]"
          },{
            "field": "Microsoft.DBforPostgreSQL/servers/firewallRules/startIpAddress","in": "[parameters('listOfStartIpAddresses')]"
          }
        ]
      },"then": {
        "effect": "[parameters('effect')]"
      }
    }
  },"id": "...","type": "Microsoft.Authorization/policyDefinitions","name": "..."
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。