1

我正在尝试将谷歌云盔甲添加到我的 Terraform 项目中,该项目使用 Kubernetes 部署应用程序。我按照这个例子。但是,就我而言,我想创建此规则: https ://github.com/hashicorp/terraform-provider-google/blob/master/examples/cloud-armor/main.tf

关闭所有端口上所有 IP 的所有流量,但打开端口 80 和 443 上所有 IP 的流量

  • web_application_firewall.tf然后我在目录下添加了一个文件,它terraform/kubernetes具有以下配置:
# Cloud Armor Security policies
resource "google_compute_security_policy" "web-app-firewall" {
  name        = "armor-security-policy"
  description = "Web application security policy to close all traffics for all IPs on all ports but open traffic for all IPs on port 80 and 443"

  # Reject all traffics for all IPs on all ports
  rule {
    description = "Default rule, higher priority overrides it"

    action   = "deny(403)"
    priority = "2147483647"

    match {
      versioned_expr = "SRC_IPS_V1"

      config {
        src_ip_ranges = ["*"]
      }
    }

    
  }

  # Open traffic for all IPs on port 80 and 443
  #rule {
  #  description = "allow traffic for all IPs on port 80 and 443"

  #  action   = "allow"
  #  priority = "1000"

  #  match {
  #    versioned_expr = "SRC_IPS_V1"

  #    config {
  #      src_ip_ranges = ["*"]
  #    }
  #  }
  #}
}

resource "google_compute_firewall" "firewall-allow-ports" {
  name    = "firewall-allow-ports"
  network = google_compute_network.default.name

  allow {
    protocol = "icmp"
  }

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }

  source_tags = ["web"]
}

resource "google_compute_network" "default" {
  name = "test-network"
}

在这里,我停用了端口 445,但重新部署后,我仍然可以访问 Web 应用程序。你能告诉我我在这里做错了什么吗?先感谢您。

4

1 回答 1

0

首先,我想澄清一些事情。

云铠

Google Cloud Armor仅为在外部负载平衡器后面运行的应用程序提供保护,并且一些功能仅适用于外部 HTTP(S) 负载平衡器。

简而言之,它可以过滤IP地址但不能阻止端口,它是防火墙的作用。

有问题deny的是,您对所有 IP 和规则都有allow规则(已注释),但是这两个规则都src_ip_ranges = ["*"]适用于所有 IP,这有点毫无意义。

地形片段

我已尝试将terraform-provider-google与您的更改一起应用,但是我不确定这是否正是您所拥有的。如果您可以发布整个代码,那么复制整个场景会更有帮助。

正如我之前提到的,要阻止端口,您需要使用防火墙规则。防火墙规则适用于特定的 VPC 网络,而不是全部。当我试图复制您的问题时,我发现您:

创建新的 VPC 网络

resource "google_compute_network" "default" {
  name = "test-network"
}

创建防火墙规则

resource "google_compute_firewall" "firewall-allow-ports" {
  name    = "firewall-allow-ports"
  network = google_compute_network.default.name

  allow {
    protocol = "icmp"
  }

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }

  source_tags = ["web"]
}

但是您在哪里创建了虚拟机?如果您遵循 github 代码,则您的 VM 已在defaultVPC 中创建:

  network_interface {
    network = "default" ### this line
    access_config {
      # Ephemeral IP
    }

Terraform 文档中,您可以找到该值指示 VM 将连接到哪个网络的信息。

network_interface- (必需)要附加到实例的网络。这可以指定多次。

问题摘要

简而言之,您已经创建了新的 VPC ( test-network),创建了 VPC 规则 ( "firewall-allow-ports") 以仅允许新 VPC端口上的ICMP协议和协议-但是您的 VM 已在VPC 中创建,它可能具有不同的防火墙规则以允许整个流量,允许端口 445 或更多变体上的流量。TCP80source_tags = webtest-networkdefault

可能的解决方案

在terraformdefault中用作资源的名称可能很危险/很棘手,因为它可以在与您想要的不同的位置创建资源。我已经稍微更改了这段代码来创建一个 VPC 网络 -test-network将它用于防火墙规则和资源中"google_compute_instance"

resource "google_compute_network" "test-network" {
  name = "test-network"
}

resource "google_compute_firewall" "firewall-allow-ports" {
  name    = "firewall-allow-ports"
  network = google_compute_network.test-network.name

  allow {
    protocol = "icmp"
  }

  allow {
    protocol = "tcp"
    ports    = ["80", "443"] ### before was only 80
  }

  source_tags = ["web"]
}

resource "google_compute_instance" "cluster1" {
  name         = "armor-gce-333" ### previous VM name was "armor-gce-222"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "test-network"
    access_config {

...

正如您在下面的屏幕上看到的,它还为端口 443 创建了防火墙规则,在 VPC 中test-network您可以看到 VM "armor-gce-333"

摘要 您的主要问题与您已使用防火墙规则配置新 VPC 相关,但您的实例可能是在另一个允许端口 445 上的流量的 VPC 网络中创建的。

于 2021-12-28T11:53:03.427 回答