首页 > 解决方案 > 创建 Azure 策略以防止直接通过订阅访问 Internet

问题描述

我想创建一个 Azure 订阅,其中无法创建可以直接访问 Internet 的资源,而是需要路由回本地设备。

我创建了以下策略并将其应用于订阅

 {
"if": {
  "anyOf": [
    {
      "source": "action",
      "like": "Microsoft.Network/publicIPAddresses/*"
    }
  ]
},
"then": {
  "effect": "deny"
}

但是,看起来仍然可以访问互联网。我们是否需要为所有子网创建自定义 UDR 以将所有 0.0.0.0/0 流量路由回本地?

顺便说一句,在 AWS 中创建类似的 SCP 相当简单: https ://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies_example-scps.html#example_scp_5

标签: amazon-web-servicesazure-policy

解决方案


我有同样的问题,我解决了如下:

{
  "policyType": "Custom",
  "mode": "All",
  "displayName": "check_default_route_gateway_plus_default",
  "policyRule": {
    "if": {
      "count": {
        "field": "Microsoft.Network/routeTables/routes[*]",
        "where": {
          "allOf": [
            {
              "field": "Microsoft.Network/routeTables/routes[*].nextHopIpAddress",
              "equals": "[parameters('DefaultGW')]"
            },
            {
              "field": "Microsoft.Network/routeTables/routes[*].name",
              "equals": "DEFAULT"
            },
            {
              "field": "Microsoft.Network/routeTables/routes[*].addressPrefix",
              "equals": "0.0.0.0/0"
            }
          ]
        }
      },
      "notEquals": 1 
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {
    "DefaultGW": {
      "type": "String",
      "metadata": {
        "displayName": "Default Gateway IP",
        "description": "The default Gateway IP. It correspond to the Proxy IP"
      },
      "defaultValue": "PUT_HERE_THE_IP_OF_YOUR_APPLIANCE"
    }
  }
}

此策略将失败,因为没有使用指向您的设备DEFAULT的 addressPrefix调用默认路由。0.0.0.0/0

我花了一些时间才到达它,因为逻辑与我的预期行为相反。该策略在其表达式被评估为true时触发,而我认为它会触发然后评估为false

实现它的一大帮助来自 VSCode 插件Azure Policy,您可以在其中交互式测试您的策略,而无需等待云延迟。

此外https://docs.microsoft.com/en-us/azure/governance/policy/concepts/definition-structure对于查看所有策略结构很有用。


推荐阅读