mcpField
T
terraform-policy

terraform-policy

github.com/hashicorp云与 DevOps9091.5k 次安装今天更新
代码仓库

关于 terraform-policy

terraform-policy 是用于编写和测试 Terraform Policy 文件的实用技能。它将策略创建或转换交给 tfpolicy-author,将测试交给 tfpolicy-test,并依据已安装的 tfpolicy CLI 版本提供指导。

主要功能

  1. 01编写和转换 .policy.hcl 文件
  2. 02编写和调试 .policytest.hcl 文件
  3. 03将任务分派给专用子技能
  4. 04区分不同 tfpolicy CLI 版本的行为

适用场景

  1. 01根据需求创建策略
  2. 02转换 Sentinel 策略
  3. 03调试策略测试
  4. 04选择适用于当前版本的编写指导

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/hashicorp/agent-skills --skill terraform-policy

安装后在 Claude Code 中输入/terraform-policy确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

.gitignore
examples/conversion/cloudfront-associated-with-waf/
cloudfront-associated-with-waf.policy.hcl
cloudfront-associated-with-waf.sentinel
README.md
examples/conversion/cloudtrail-server-side-encryption-enabled/
cloudtrail-server-side-encryption-enabled.policy.hcl
cloudtrail-server-side-encryption-enabled.sentinel
README.md
examples/conversion/dms-endpoint-should-be-ssl-configured/
dms-endpoint-should-be-ssl-configured.policy.hcl
dms-endpoint-should-be-ssl-configured.sentinel
README.md
examples/conversion/dms-endpoints-should-use-ssl/
dms-endpoints-should-use-ssl.policy.hcl
dms-endpoints-should-use-ssl.sentinel
README.md
examples/conversion/ec2-network-acl-should-have-subnet-ids/
ec2-network-acl-should-have-subnet-ids.policy.hcl
ec2-network-acl-should-have-subnet-ids.sentinel
README.md
examples/conversion/ec2-vpc-default-security-group-no-traffic/
ec2-vpc-default-security-group-no-traffic.policy.hcl
ec2-vpc-default-security-group-no-traffic.sentinel
README.md
examples/conversion/efs-access-point-should-enforce-user-identity/
efs-access-point-should-enforce-user-identity.policy.hcl
efs-access-point-should-enforce-user-identity.sentinel
README.md
examples/conversion/elasticache-redis-replication-group-encryption-at-transit-enabled/
elasticache-redis-replication-group-encryption-at-transit-enabled.policy.hcl
elasticache-redis-replication-group-encryption-at-transit-enabled.sentinel
README.md
examples/conversion/elasticsearch-encrypted-at-rest/
elasticsearch-encrypted-at-rest.policy.hcl
elasticsearch-encrypted-at-rest.sentinel
README.md
examples/conversion/elasticsearch-https-required/
elasticsearch-https-required.policy.hcl
elasticsearch-https-required.sentinel

详细介绍

terraform-policy

工具技能 — 调用:tfpolicy-author | tfpolicy-test

适用于:

  • 根据描述或需求编写新的 .policy.hcl 策略
  • 将 .sentinel 策略转换为 Terraform Policy
  • 编写或调试 .policytest.hcl 测试文件
  • 将 Sentinel 策略库迁移到 Terraform Policy

在提供编写或测试说明之前,请检查已安装的 tfpolicy CLI 版本,并据此调整指导。本技能维护最近两个次要版本系列 0.2.x 和 0.3.x 的指导;发布新的次要版本时,移除最旧的系列并加入新系列。

  • 如果 CLI 为 0.2.x(基线版本),编写包含资源或提供程序策略的 .policy.hcl 文件时,请加入顶层 policy { required_providers { ... } } 块。tfpolicy validate 要求必须包含此块;版本范围验证尽力而为,且不会对 resource_policy "*" 等通配符目标进行架构验证。tfpolicy test 不会根据提供程序架构预先检查模拟的 attrs/prior_attrs,core::alltrue/core::anytrue 不存在,并且仅在此 0.2.x 系列中,模拟的 resource {} 块可以完全省略 attrs/prior_attrs。
  • 如果 CLI 为 0.3.x 或更高版本,上述其他指导仍然适用,但不再允许像 0.2.x 那样省略资源状态:.policytest.hcl 文件中的每个模拟 resource {} 块都必须声明 attrs 或 prior_attrs;如果两者的计算结果都为空,则会跳过该测试用例(provider {} 和 module {} 模拟不受影响)(参见 tfpolicy-test)。tfpolicy test 会复用目标 .policy.hcl 中现有的顶层 policy { required_providers { ... } } 块(无需在 .policytest.hcl 层级单独声明),在运行任何测试之前,根据已解析的提供程序架构验证提供程序、资源和数据源策略,以及 core::getdatasource()/core::getresources() 参数;如果架构不匹配,整个运行将失败(参见 tfpolicy-test)。也可以使用 core::alltrue(list) 和 core::anytrue(list)——优先使用它们,而不是 core::length() 列表推导式变通方案(参见 tfpolicy-author)。资源、提供程序和模块策略可以使用 meta.tfe_stack 和 meta.tfe_workspace.tags;在 Stack 评估之外,Stack 字段为空。
  • 如果 CLI 版本未知,请先让用户检查版本,或提供明确区分 0.2.x 和 0.3.x 路径的指导。

不适用于:

  • 为 Terraform 模块编写 .tftest.hcl 文件——请使用 terraform-test
  • 编写通用 Terraform HCL——请使用 terraform-style-guide

路由

任务子技能
编写或转换 .policy.hcl 策略tfpolicy-author
编写或调试 .policytest.hcl 测试tfpolicy-test

示例

故障排除

  • 触发了错误的技能? 直接从上方路由表加载子技能。
npx skills add hashicorp/agent-skills/terraform/terraform-policy/skills/tfpolicy-author
npx skills add hashicorp/agent-skills/terraform/terraform-policy/skills/tfpolicy-test

评论

支持 Markdown