tfsec 规则深度解析:强制 AWS Elasticsearch 域启用 HTTPS(aws-elastic-search-enforce-https)
tfsec 规则深度解析:强制 AWS Elasticsearch 域启用 HTTPS(aws-elastic-search-enforce-https)
本篇指南围绕 tfsec 内置检查规则 aws-elastic-search-enforce-https 展开,说明该规则为什么以 critical(严重) 级别要求 AWS Elasticsearch 域强制启用 HTTPS,并通过可直接运行的 Terraform 违规/合规示例、tfsec 命令行实操与配置项讲解,帮助读者在本地与 CI 流程中落实“明文 HTTP 一律拒绝”的安全基线。读完本文,你将掌握该规则的判定逻辑、与 TLS 策略及节点间加密等关联规则的组合用法,以及如何在 tfsec 中针对单条规则做筛选、排除、忽略和严重级别定制。
规则速览:检查 ID、严重级别与归属
aws-elastic-search-enforce-https 是 tfsec 内置的 AWS 检查规则之一,归属 elastic-search 服务检查族。在 rules.md 中,该规则被描述为:
Elasticsearch doesn't enforce HTTPS traffic.
规则元信息如下:
| 项目 | 值 |
|---|---|
| 检查 ID | aws-elastic-search-enforce-https |
| 旧版(legacy)ID | AWS033(见 internal/pkg/legacy/map.go) |
| 所属云厂商 | aws |
| 所属服务检查族 | elastic-search(参见 docs/checks/aws/elastic-search/index.md) |
| 默认严重级别 | critical(严重) |
在 tfsec 的 docs/checks/aws/elastic-search/ 检查族目录下,与该规则并列的还有 enable-domain-encryption(静态加密)、enable-in-transit-encryption(节点间传输加密)、enable-domain-logging(域日志)与 use-secure-tls-policy(TLS 策略),共同构成对 Elasticsearch 域数据保护的完整检查矩阵。
为什么必须强制 HTTPS:风险原理解读
该规则对应的原始说明(见 docs/checks/aws/elastic-search/enforce-https/index.md)给出了两条核心论据:
- 明文 HTTP 可读且不加密:明文 HTTP 未经过任何加密,内容对人类可读。一旦恶意攻击者对连接进行监听(eavesdrop),就能看到双方交互过程中流经的所有数据。
- HTTPS 是带加密的 HTTP:HTTPS 本质是 HTTP over TLS(加密连接),监听者无法读取传输内容。
由此得到该规则的 Possible Impact(潜在影响):
HTTP traffic can be intercepted and the contents read (HTTP 流量可能被中间人截获,且内容可被直接读取)
对于承载业务索引、日志或分析数据的 Elasticsearch 域而言,客户端(如 Kibana、业务应用)与域端点之间的通信若使用明文 HTTP,等于把数据明文暴露在网络上。因此该规则给出的 Suggested Resolution(建议修复) 只有一句话:Enforce the use of HTTPS for ElasticSearch(强制 Elasticsearch 使用 HTTPS)。
违规示例:enforce_https = false 会被判失败
以下 Terraform 配置将无法通过 aws-elastic-search-enforce-https 检查:
resource "aws_elasticsearch_domain" "bad_example" {
domain_name = "domain-foo"
domain_endpoint_options {
enforce_https = false
}
}
判定要点:
- 检查对象是
aws_elasticsearch_domain资源; - 判定依据是
domain_endpoint_options块内的enforce_https参数; - 当
enforce_https显式设置为false(或未显式开启)时,tfsec 会认为该域允许明文 HTTP 访问,从而命中规则。
需要说明的是,domain_endpoint_options 是 AWS Elasticsearch 域端点行为的配置块,其中 enforce_https 参数用于要求访问域端点时强制使用 HTTPS;在该规则的安全示例中,该参数必须显式置为 true。
安全示例:显式开启 enforce_https = true
以下配置可通过检查,是官方文档推荐的合规写法:
resource "aws_elasticsearch_domain" "good_example" {
domain_name = "domain-foo"
domain_endpoint_options {
enforce_https = true
}
}
实践建议(结合仓库同族规则深化):
- 显式声明而非依赖默认值:即便云厂商默认行为可能趋向安全,在 IaC 中显式写出
enforce_https = true更能表达配置意图,也便于 tfsec 这类静态扫描工具直接判定。 - 配合 TLS 策略规则一起使用:仅开启 HTTPS 还不够,
domain_endpoint_options中的tls_security_policy若使用过时策略(如Policy-Min-TLS-1-0-2019-07),会触发同族的aws-elastic-search-use-secure-tls-policy(默认 high)。参考其安全示例,应使用Policy-Min-TLS-1-2-2019-07一类的现代 TLS 策略(见 docs/checks/aws/elastic-search/use-secure-tls-policy/index.md)。 - 纵深防御组合:
aws-elastic-search-enforce-https解决客户端到端点的传输加密;同族aws-elastic-search-enable-in-transit-encryption(默认 high)解决节点到节点(node-to-node)的明文通信问题;aws-elastic-search-enable-domain-encryption则覆盖静态数据加密。生产环境建议三条规则同时满足,形成“传输层 + 节点层 + 静态层”的完整加密闭环。
在 tfsec 中运行与验证
基本扫描
tfsec 默认扫描当前目录(也支持指定目录),扫描到问题后退出码非零:
tfsec .
若仓库中存在上述 bad_example 配置,运行后会输出包含 aws-elastic-search-enforce-https 与 critical 严重级别的检查结果。
只关注本条规则的筛选与验证
结合 docs/guides/usage.md 中的命令行参数,可用以下方式精确验证:
# 仅显示该规则的结果(含已通过的检查,便于确认修复生效)
tfsec --filter-results aws-elastic-search-enforce-https --include-passed .
# 只报告 critical 及以上级别,同时以 JSON 输出便于 CI 解析
tfsec --minimum-severity CRITICAL --format json .
参数说明:
| 参数 | 作用 |
|---|---|
--filter-results string |
仅返回指定检查的结果,支持逗号分隔多个规则 |
--include-passed |
把通过的检查也纳入输出,适合验证修复前后差异 |
--minimum-severity string / -m |
最低报告级别,取值 CRITICAL、HIGH、MEDIUM、LOW |
--format string / -f |
输出格式:lovely(默认)、json、csv、checkstyle、junit、sarif、text、markdown、html、gif |
--soft-fail / -s |
运行检查但抑制非零退出码,适合在暂不阻塞的流水线阶段使用 |
完整参数表可参见 docs/guides/usage.md,也可以直接运行 tfsec --help 查看。
配置层面的管理方式
通过配置文件设置最低严重级别
aws-elastic-search-enforce-https 默认是 critical,只要在配置文件中把 minimum_severity 设为 CRITICAL,该规则就会始终进入报告范围(低于该级别的规则会被过滤)。tfsec 会自动加载扫描根目录 .tfsec 文件夹下的 config.json 或 config.yml,也可用 --config-file 指定(详见 docs/guides/configuration/config.md):
{
"minimum_severity": "CRITICAL"
}
覆盖严重级别
如果团队认为该规则对某些场景过于严格,可通过 severity_overrides 调整级别(用法见 docs/guides/configuration/config.md):
---
severity_overrides:
aws-elastic-search-enforce-https: HIGH
排除与忽略
- 整体排除:在配置文件
exclude中列出规则 ID,即可在整次扫描中关闭该规则:
命令行也可用--- exclude: - aws-elastic-search-enforce-https--exclude(-e)传逗号分隔的规则 ID 列表。 - 定向忽略:若只需在个别资源上豁免(例如确证非生产、无敏感数据的实验域),可在对应行添加
#tfsec:ignore:<rule>注释:
忽略注释支持resource "aws_elasticsearch_domain" "example" { domain_name = "domain-foo" domain_endpoint_options { enforce_https = false #tfsec:ignore:aws-elastic-search-enforce-https } }exp:yyyy-mm-dd过期日期与ws:workspace工作区限定,完整语法见 docs/guides/configuration/ignores.md。
旧代码兼容:legacy ID 与忽略迁移
在旧版 tfsec 中,本规则使用三位代码编号 AWS033。从源码 internal/pkg/legacy/map.go 可以看到映射关系:
"AWS033": "aws-elastic-search-enforce-https",
这意味着旧式写法 #tfsec:ignore:AWS033 依然能被识别。若希望把仓库中所有旧式忽略注释批量迁移到新 ID,可运行:
tfsec --migrate-ignores .
迁移逻辑由 internal/pkg/ignores/migration.go 实现,它会遍历指定目录下的 .tf 文件,将匹配 tfsec:ignore:([A-Z]{3}\d{3}) 的旧代码替换为新的规则 ID。
小结与检查清单
aws-elastic-search-enforce-https 以 critical 级别守卫 AWS Elasticsearch 域的传输安全,判定与修复都非常直接:
- 在
aws_elasticsearch_domain的domain_endpoint_options中显式设置enforce_https = true; - 配合
use-secure-tls-policy使用现代 TLS 策略(如Policy-Min-TLS-1-2-2019-07),配合enable-in-transit-encryption与enable-domain-encryption构建全链路加密; - 在 CI 中通过
--filter-results、--include-passed、--minimum-severity验证规则生效,必要时用配置文件的severity_overrides、exclude或行内#tfsec:ignore注释做分级管控; - 存量代码若使用旧代码
AWS033,用tfsec --migrate-ignores .平滑迁移。
更多参考:规则族索引见 docs/checks/aws/elastic-search/index.md,规则全量清单见 rules.md,官方说明文档见 docs/checks/aws/elastic-search/enforce-https/index.md(其中原始文档引用的 Terraform AWS Provider 与 AWS Elasticsearch Service 数据保护官方资料可作为延伸阅读)。