tfsec 检查项详解:Google Compute Disk 应使用客户管理密钥加密(google-compute-disk-encryption-customer-keys)
tfsec 检查项详解:Google Compute Disk 应使用客户管理密钥加密(google-compute-disk-encryption-customer-keys)
tfsec 内置的 google-compute-disk-encryption-customer-keys 检查用于扫描 Google Cloud 计算磁盘(google_compute_disk),强制要求磁盘使用客户管理密钥(Customer Managed Encryption Key, CMEK)进行加密。本文以 tfsec 官方检查文档 disk-encryption-customer-key 为骨架,结合仓库中的规则注册表与命令行用法,完整说明该检查的判定逻辑、不安全/安全配置写法,以及在实际扫描流程中的排除与严重级别过滤方法,帮助你在 Terraform 代码评审阶段自动发现"密钥托管缺失"的风险。
检查项基本信息
在 tfsec 的规则体系中,该检查对应如下元信息(见 rules.md):
| 属性 | 值 |
|---|---|
| 规则 ID | google-compute-disk-encryption-customer-keys |
| 提供方 | google |
| 分类 | compute |
| 检查目标 | google_compute_disk 资源 |
| 默认严重级别 | low(低) |
该检查位于 Google Compute 检查族之中,与它同族的磁盘安全相关检查还包括:
google-compute-disk-encryption-no-plaintext-key:禁止在代码中以明文(raw_key)方式提供加密密钥,默认严重级别为critical(见 disk-encryption-no-plaintext-key 文档);google-compute-vm-disk-encryption-customer-key:针对google_compute_instance的启动盘与附加盘,要求同样使用客户管理密钥(见 vm-disk-encryption-customer-key 文档)。
三者共同构成了 tfsec 对 Google Compute 磁盘加密的完整校验面,其中本文的主角负责检查"独立磁盘资源是否声明了托管密钥"。
检查原因与影响分析
官方文档对该检查的解释如下:
使用非托管(unmanaged)密钥会使密钥轮换和一般性的密钥管理变得困难。
可能的影响: 使用非托管密钥意味着无法进行规范的密钥管理,包括无法按组织策略定期轮换密钥、无法集中审计密钥生命周期,也无法在密钥泄露时通过 Google Cloud KMS 快速撤销或更换。
建议的修复方式: 使用托管密钥(managed keys)来加密磁盘,即在 google_compute_disk 中通过 disk_encryption_key 块引用 Google Cloud KMS 密钥。
从云平台最佳实践角度看,这对应的是 Google Cloud 的 CMEK 能力:由客户在 Cloud KMS 中创建密钥环与加密密钥,再通过 kms_key_self_link 关联到磁盘。这样密钥的创建、轮换、权限授予和销毁都由客户自己的 KMS 体系管理,符合多数企业的合规与审计要求。
不安全示例(会触发检查)
以下 Terraform 配置会被 google-compute-disk-encryption-customer-keys 检查判定为失败,因为它虽然完整定义了磁盘的名称、类型、可用区、镜像、标签和物理块大小,却完全没有声明任何加密密钥:
resource "google_compute_disk" "bad_example" {
name = "test-disk"
type = "pd-ssd"
zone = "us-central1-a"
image = "debian-9-stretch-v20200805"
labels = {
environment = "dev"
}
physical_block_size_bytes = 4096
}
此时磁盘将使用 Google 默认的、由平台自动管理的密钥进行加密(SSE 默认加密仍会开启),但该密钥不受你的组织控制,无法自定义轮换周期,也无法按合规要求进行密钥级审计。
安全示例(通过检查)
以下配置在磁盘上添加了 disk_encryption_key 块,并通过 kms_key_self_link 引用客户管理的 KMS 密钥,即可通过本检查:
resource "google_compute_disk" "good_example" {
name = "test-disk"
type = "pd-ssd"
zone = "us-central1-a"
image = "debian-9-stretch-v20200805"
labels = {
environment = "dev"
}
physical_block_size_bytes = 4096
disk_encryption_key {
kms_key_self_link = "something"
}
}
在实际生产配置中,kms_key_self_link 不应是字面量,而应引用真实的 KMS 密钥资源。更推荐的写法是先声明 KMS 资源,再通过引用传入,例如:
resource "google_kms_key_ring" "keyring" {
name = "disk-keyring"
location = "us-central1"
}
resource "google_kms_crypto_key" "example" {
name = "disk-crypto-key"
key_ring = google_kms_key_ring.keyring.id
}
resource "google_compute_disk" "good_example" {
name = "test-disk"
type = "pd-ssd"
zone = "us-central1-a"
image = "debian-9-stretch-v20200805"
disk_encryption_key {
kms_key_self_link = google_kms_crypto_key.example.id
}
}
这样既满足 tfsec 检查,也符合 Terraform 引用资源属性的最佳实践,避免在代码中出现魔法字符串。同理,在 google_compute_instance 的 boot_disk 中使用 kms_key_self_link,对应的是 vm-disk-encryption-customer-key 检查的通过形态。
在扫描流程中如何应用该检查
直接运行扫描
tfsec 默认扫描当前目录下的 Terraform 文件,无需任何参数:
tfsec .
命中本检查后,输出会标记规则 ID google-compute-disk-encryption-customer-keys,并附上失败资源所在文件的代码片段。
按严重级别过滤
由于本检查默认严重级别为 low,如果你只关心中等级别以上的问题,可以通过 --minimum-severity(短参数 -m)过滤,low 级别的结果将被省略:
tfsec . -m MEDIUM
完整参数说明可在 usage.md 中查看,或直接运行 tfsec --help。
临时排除该检查
在 README 的排除示例中,tfsec 明确给出了将本检查与其他规则一起排除的写法(见 README.md):
tfsec . -e general-secrets-sensitive-in-variable,google-compute-disk-encryption-customer-keys
-e / --exclude 接受逗号分隔的规则 ID 列表,适用于"某批存量资源暂未整改、先放行再分批治理"的场景。需要说明的是,排除属于临时策略,长期更推荐在资源声明中补齐 disk_encryption_key,或在代码中通过 ignores 机制 显式记录豁免理由。
与其他磁盘加密检查的协同
| 检查 ID | 检查对象 | 核心要求 | 默认严重级别 |
|---|---|---|---|
google-compute-disk-encryption-customer-keys |
google_compute_disk |
必须通过 kms_key_self_link 使用客户管理密钥 |
low |
google-compute-disk-encryption-no-plaintext-key |
google_compute_disk |
禁止 raw_key 明文密钥 |
critical |
google-compute-vm-disk-encryption-customer-key |
google_compute_instance |
启动盘/附加盘必须使用客户管理密钥 | low |
建议在 CI 流水线(如 GitHub Actions、PR 检查)中同时启用这三条规则,并配合 --minimum-severity MEDIUM 与 --format sarif 等输出格式,将结果上报到代码扫描平台。对于存量低风险资源,可结合 -e 排除与 ignores 豁免逐步完成密钥托管改造。
小结
google-compute-disk-encryption-customer-keys 是 tfsec 内置的 Google Compute 磁盘加密治理规则,其判定逻辑非常简单清晰:google_compute_disk 若未声明 disk_encryption_key 且未通过 kms_key_self_link 关联客户管理的 KMS 密钥,即判定为不合规。通过本文的不安全/安全示例,你可以快速在现有 Terraform 代码中定位并修复此类问题;结合 -m、-e 等命令行参数与同族检查(disk-encryption-no-plaintext-key、vm-disk-encryption-customer-key),即可在代码提交前系统性落实 Google Compute 磁盘的密钥托管要求。