跳到主要内容

infraguard policy

管理合规策略。

子命令

list

列出所有可用策略:

infraguard policy list

get

获取特定策略的详细信息:

infraguard policy get rule:aliyun:ecs-instance-no-public-ip
infraguard policy get pack:aliyun:mlps-level-3-pre-check-compliance-pack

update

更新策略库:

infraguard policy update

new

生成一条新的自定义规则脚手架(Rego 骨架 + 测试 fixture):

# Generate a rule for both ROS and Terraform
infraguard policy new ecs-instance-must-have-owner-tag \
--iac both --severity medium \
--resource-type ALIYUN::ECS::Instance \
--tf-resource-type alicloud_instance

# Generate a compliance pack skeleton
infraguard policy new --pack my-team-baseline

生成的文件位于 --dir 之下(默认 ./policies),可直接配合 infraguard scan -p ./policies <template>infraguard policy test 使用。请参阅编写自定义规则

选项说明默认值
--iac目标 IaC:rosterraformbothboth
--severityhighmediumlowmedium
--resource-typeROS 资源类型(可重复)
--tf-resource-typeTerraform 资源类型(可重复)
--dir输出根目录./policies
--name-en / --name-zh规则名称规则 ID
--desc-en / --desc-zh规则描述TODO
--no-test不生成测试 fixturefalse
--force覆盖已有文件false
--pack使用给定的 ID 生成包骨架

test

使用规则的 fixture 运行其行为测试:

infraguard policy test --dir ./policies
infraguard policy test --dir ./policies --rule my-rule --iac terraform
infraguard policy test --dir ./policies --format json

对于每条规则,<dir>/testdata/aliyun/rules/<rule>/ 下的 fixture 会被评估:compliant fixture 必须产生该规则的违规,violation fixture 必须产生至少一个违规。当所有用例通过时退出代码为 0,失败时为 1,未找到 fixture 时为 2(除非指定 --allow-empty)。请参阅测试规则

选项说明默认值
--dir包含 rules/testdata/ 的根目录./policies
--rule仅测试给定的规则 ID(可重复)全部
--iac要测试的 IaC:rosterraformbothboth
--format输出格式:tablejsontable
--allow-empty即使未找到 fixture 也以 0 退出false

validate

验证自定义策略:

infraguard policy validate my-rule.rego
infraguard policy validate ./policies/ --lang zh

format

格式化策略文件:

infraguard policy format rule.rego
infraguard policy format rule.rego --write
infraguard policy format rule.rego --diff

clean

清理用户策略目录:

infraguard policy clean # 交互式模式,需要确认
infraguard policy clean --force # 跳过确认
infraguard policy clean -f # 短标志

删除 ~/.infraguard/policies/ 中的所有策略。不会影响内嵌策略或工作区策略。

有关更多详细信息,请参阅管理策略