如何在 tfsec 中创建自定义检查

Lev*_*Lev 0 terraform tfsec

我希望使用 tfsec 在 IaC 代码扫描中实施以下策略:

Custom Check: GCP Firewall rule allows all traffic on Telnet port (23)

以下是我的自定义检查 .json 格式:

{
  "checks": 
    [
      {
        "code": "CUS003",
        "description": "Custom Check: GCP Firewall rule allows all traffic on Telnet port (23)",
        "requiredTypes": 
          [
            "resource"
          ],
          "requiredLabels": 
          [
            "google_compute_firewall"
          ],
          "severity": "WARNING",
          "matchSpec": 
          {
            "name": "CUS003_matchSpec_name",
            "action": "and",
            "predicateMatchSpec": 
            [
                  {
                    "name": "source_ranges",
                    "action": "contains",
                    "value": "0.0.0.0/0"
                },
                {
                    "name": "ports",
                    "action": "contains",
                    "value": "23"
                }
            ]
          },
        "errorMessage": "[WARNING] GCP Firewall rule allows all traffic on Telnet port (23)",
        "relatedLinks": 
          [
            "https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall"
          ]
      }
    ]  
}
Run Code Online (Sandbox Code Playgroud)

我尝试过使用“not”、“notContains”、“equals”、“subMatch”和/或“predicateMatchSpec”的组合,但没有任何效果。

为了测试它,我特意创建了应该失败的防火墙规则和其他应该通过检查的防火墙规则。当我检查失败时,是针对所有规则,而不仅仅是少数规则。同样,当我获得检查通过时,它适用于所有规则,而不仅仅是少数规则。

可能有用的文档:tfsec 自定义检查

任何帮助表示赞赏。不幸的是“tfsec”不是一个标签,所以我希望这是我面临的一个地形问题。

小智 5

我认为现在查看它的格式很清楚,它source_ranges是资源的子项google_compute_firewall。该ports属性是 的子属性allow。您的检查假设ports是 的同级source_ranges

我认为这个检查可以通过以下方式实现 - 它进行谓词检查,检查是否存在所需的 source_range 并且有一个名为 allowed 的块,其属性端口包含 23

{
  "checks": [
    {
      "code": "CUS003",
      "description": "Custom Check: GCP Firewall rule allows all traffic on Telnet port (23)",
      "requiredTypes": [
        "resource"
      ],
      "requiredLabels": [
        "google_compute_firewall"
      ],
      "severity": "WARNING",
      "matchSpec": {
        "action": "and",
        "predicateMatchSpec": [
          {
            "name": "source_ranges",
            "action": "contains",
            "value": "0.0.0.0/0"
          },
          {
            "name": "allow",
            "action": "isPresent",
            "subMatch": {
              "name": "ports",
              "action": "contains",
              "value": "23",
              "ignoreUndefined": true
            }
          }
        ]
      },
      "errorMessage": "[WARNING] GCP Firewall rule allows all traffic on Telnet port (23)",
      "relatedLinks": [
        "https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall"
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我已经针对以下身体进行了测试

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

  allow {
    protocol = "tcp"
    ports    = ["23", "8080", "1000-2000"]
  }
  source_ranges = ["0.0.0.0/0"]
  source_tags = ["web"]
}

resource "google_compute_network" "default" {
  name = "test-network"
}
Run Code Online (Sandbox Code Playgroud)