安全方面的最佳做法
本文档提供了有关如何安全地将 Terraform 用于 Google Cloud 的准则和建议。Terraform 需要对您的云基础架构的敏感访问权限才能运行。遵循安全性方面的最佳实践有助于最大限度地降低相关风险并提高整体云安全性。
本指南未介绍 Terraform。如需了解如何将 Terraform 与 Google Cloud 搭配使用,请参阅 Terraform 使用入门。
使用远程状态
对于 Google Cloud 客户,我们建议使用 Cloud Storage 状态后端。此方法锁定了状态,允许作为团队进行协作。它还将状态和所有可能的敏感信息与版本控制分开。
确保只有构建系统和具有高度特权的管理员才能访问用于远程状态的存储桶。
为防止意外将开发状态提交到源代码控制系统,请为 Terraform 状态文件使用 gitignore。
加密状态
虽然 Google Cloud 存储桶已经过静态加密,但您可以使用客户提供的加密密钥提供另一层保护。为此,请使用 GOOGLE_ENCRYPTION_KEY
环境变量。即使状态文件中不应包含任何 Secret,也请始终将状态加密作为额外的防御措施。
不要将密文存储在状态中
Terraform 中有许多资源和数据提供商在状态文件中以明文形式存储密文值。请尽可能避免将密文存储在状态中。以下是以明文形式存储密文的一些提供商示例:
标记敏感输出
与其尝试手动加密敏感值,不如依靠 Terraform 对敏感状态管理的内置支持。将敏感值导出到输出时,请确保将这些值标记为敏感。
确保职责分离
如果您无法从用户无权访问的自动化系统运行 Terraform,请通过分离权限和目录来履行职责分离。例如,网络项目对应于网络 Terraform 服务账号或其访问权限仅限于此项目的用户。
运行持续审核
执行 terraform apply
命令后,请运行自动化安全检查。这些检查有助于确保基础架构不会偏移到不安全的状态。以下工具是此类检查的有效选择:
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-12-22。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-12-22。"],[[["Utilize the Cloud Storage state backend for Terraform state management to enable team collaboration and separate sensitive information from version control."],["Encrypt the Terraform state using customer-supplied encryption keys via the `GOOGLE_ENCRYPTION_KEY` environment variable, even though no secrets should be in the state file."],["Avoid storing secrets directly within the Terraform state file, as certain providers like `vault_generic_secret` and `google_service_account_key` may store them in plaintext."],["Mark sensitive outputs as such within Terraform configurations to prevent the exposure of sensitive data in command-line output and implement separation of duties by restricting access to Terraform resources."],["Implement pre-apply checks with tools like `gcloud terraform vet` and continuous audits using Security Health Analytics, InSpec, or Serverspec to detect security regressions and ensure infrastructure security."]]],[]]