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"
]
}
]
}
我试过使用“not”、“notContains”、“equals”、“subMatch”和/或“predicateMatchSpec”的组合,但没有任何效果。
为了测试它,我有目的地创建了应该失败的防火墙规则和应该通过检查的其他规则。当我检查失败时,它适用于所有规则,而不仅仅是一些规则。同样,当我获得检查通过时,它适用于所有规则,而不仅仅是一些规则。
可能有用的文档:tfsec 自定义检查
任何帮助表示赞赏。不幸的是,“tfsec”不是标签,所以我希望这是我面临的地形问题。
解决方案
我认为现在看它的格式很明显,它source_ranges
是google_compute_firewall
资源的子项。该ports
属性是 的子属性allow
。您的检查假设它ports
是source_ranges
.
我认为这个检查可以通过以下方式实现 - 它会根据需要进行谓词检查是否有 source_range 并且有一个名为 allow 的块,其属性端口包含 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"
]
}
]
}
我已经针对以下主体对其进行了测试
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"
}
推荐阅读
- reactjs - 如何解决 useReducer 钩子的类型错误“参数不可分配给 never 类型的参数”?
- django - 从 Django 创建 websocket 连接
- sveltekit - 是否可以使用 svelte-kit 为 ./routes 中的子目录创建 _layout.svelte?
- python - 从 for 循环内递增
- javascript - 为什么 chrome.storage.sync.set 不能立即加载用户设置?
- git - Azure Pipelines 错误:pathspec 'refs/remotes/origin/master' 与 git 已知的任何文件都不匹配
- c# - C# - 在 Linq Where Any 语句中使用的要列出的字符串
- python - 使用 Pandas 为间隙添加容差
- javascript - 我找到了一个符合我兴趣的 jquery 代码。我的问题是我希望将此 jquery 代码转换为本机 javascript。答案赞赏
- ffmpeg - 如何使用 ffmpeg 将视频 720:1280 更大的高度、宽度和裁剪调整为 1280:720?