Terraform
编写 Terraform 部署的 Sentinel 策略
在编写 Sentinel 策略时,你可以使用 Sentinel 导入来验证策略的限制,这些导入访问模拟数据。Sentinel 可以从 HCP Terraform API 导入多种类型的数据:配置、计划、状态和运行。
在本教程中,你将回顾一个 Sentinel 策略,并使用预生成的模拟导入数据在 Sentinel CLI 中对其进行测试。该示例策略强制执行 EC2 实例类型和标签限制。
先决条件
对于本教程,您需要
- Sentinel CLI
- 一个 GitHub 帐户
克隆示例 Terraform 配置
在您的终端中,克隆 示例代码仓库。此仓库包含一个示例 Sentinel 策略和一个 tfplan/v2 Sentinel 模拟导入。
$ git clone https://github.com/hashicorp-education/learn-sentinel-write-policy
导航到目录。
$ cd learn-sentinel-write-policy
查看 tfplan/v2 导入模拟数据结构
您将使用 tfplan/v2 导入类型来测试您的策略。此导入类型提供对 Terraform 计划数据的访问,该数据表示 Terraform 需要对基础设施进行哪些更改才能与编写的配置匹配。 tfplan 导入使您可以确定提议的更改是否满足您的策略标准。
在您的文本编辑器中打开名为 mock-tfplan-v2.sentinel 的文件。找到 resource_changes 集合。此 Terraform 数据是配置所需的所有提议资源更改的键/值集合。下面的数据已截断,但您的文件应包含这些值和此集合。
mock-tfplan-v2.sentinel
terraform_version = "1.0.1"
## ...
resource_changes = {
"aws_instance.ubuntu": {
"address": "aws_instance.ubuntu",
"change": {
"actions": [
"create",
],
"after": {
"ami": "ami-0fb1e27304d83032f",
"credit_specification": [],
"disable_api_termination": null,
"ebs_optimized": null,
"get_password_data": false,
"hibernation": null,
"iam_instance_profile": null,
"instance_initiated_shutdown_behavior": null,
"instance_type": "t2.micro",
"monitoring": null,
"source_dest_check": true,
"tags": {
"Name": "Provisioned by Terraform",
},
"timeouts": null,
"user_data": null,
"user_data_base64": null,
"volume_tags": null,
},
## ...
},
"deposed": "",
"index": null,
"mode": "managed",
"module_address": "",
"name": "ubuntu",
"provider_name": "registry.terraform.io/hashicorp/aws",
"type": "aws_instance",
},
## ...
}
Terraform 捕获在计划中创建或修改的任何资源的属性。您可以使用此数据在 Sentinel 策略中验证策略是否恰当地限制资源或属性。
测试策略时,请选择适合您策略限制的相应导入。例如,您可以使用 tfrun 导入来测试根据成本估算限制资源的策略。您可以使用 tfconfig/v2 导入来测试限制配置中使用的提供程序数量的策略。
在 Sentinel 中定义一个模拟
现在打开 sentinel.hcl 文件并查看配置。
mock "tfplan/v2" {
module {
source = "mock-tfplan-v2.sentinel"
}
}
这定义了一个名为 tfplan/v2 的模拟 (mock) ,其中包含来自 mock-tfplan-v2.sentinel 文件的的数据。 您的 Sentinel 策略将导入此模拟以测试该策略。
审查您的策略
Sentinel 将您的要求与导入的数据进行测试,结果为 通过 或 失败。 在编写策略时,您可以使用 变量 存储值,并使用 运算符 比较逻辑表达式。
打开 restrict-aws-instances-type-and-tag.sentinel 文件,其中包含 Sentinel 策略。
此 Sentinel 策略强制执行以下基础设施要求
- EC2 实例必须具有
Name标签。 - EC2 实例必须是类型
t2.micro、t2.small或t2.medium。
如果您的 EC2 实例不满足所有这些标准,Sentinel 将用 失败 标记运行。
restrict-aws-instances-type-and-tag.sentinel
# Imports mock data
import "tfplan/v2" as tfplan
# Get all AWS instances from all modules
ec2_instances = filter tfplan.resource_changes as _, rc {
rc.type is "aws_instance" and
(rc.change.actions contains "create" or rc.change.actions is ["update"])
}
# Mandatory Instance Tags
mandatory_tags = [
"Name",
]
# Allowed Types
allowed_types = [
"t2.micro",
"t2.small",
"t2.medium",
]
# Rule to enforce "Name" tag on all instances
mandatory_instance_tags = rule {
all ec2_instances as _, instance {
all mandatory_tags as mt {
instance.change.after.tags contains mt
}
}
}
# Rule to restrict instance types
instance_type_allowed = rule {
all ec2_instances as _, instance {
instance.change.after.instance_type in allowed_types
}
}
# Main rule that requires other rules to be true
main = rule {
(instance_type_allowed and mandatory_instance_tags) else true
}
此 Sentinel 策略可以分为七个部分。
import语句导入在sentinel.hcl中定义的模拟数据。 此模拟数据模拟了一个配置 EC2 实例的 Terraform 计划。ec2_instances变量筛选计划将创建或修改的 EC2 实例的模拟数据。Sentinel 根据
filter 表达式确定要从导入中评估的特定资源或数据。 在上述策略中,ec2_instances使用一个filter表达式。 该表达式返回一个tfplan.resource_changes的映射:一个选择器,用于在计划数据中搜索名为resource_changes的字段,以查找在计划中创建或更新的 EC2 实例。mandatory_tags变量包含所需标签的列表。allowed_types变量包含允许的 EC2 实例类型的列表。mandatory_instance_tags规则确保 EC2 实例具有Name标签。 该规则循环遍历筛选后的ec2_instances,并验证所有 EC2 实例是否具有mandatory_tags变量中定义的标签。mock-tfplan-v2.sentinel
resource_changes = { "aws_instance.ubuntu": { "address": "aws_instance.ubuntu", "change": { ## ... "after": { ## .. "tags": { "Name": "Provisioned by Terraform", }, ## .. } ## ... }, ## ... }, }规则
instance_type_allowed确保 EC2 实例是t2.micro、t2.small或t2.medium。该规则循环遍历经过筛选的ec2_instances,并检查该实例的instance_type是否inallowed_types。规则
main评估规则mandatory_instance_tags和instance_type_allowed。如果两者都为 true,Sentinel 允许策略通过。Sentinel 策略以这种方式划分规则,以保持主规则简短,并允许您根据多个规则标准评估策略。
在 Sentinel CLI 中运行你的策略
现在,使用 apply 命令针对模拟数据测试你的策略,并确保你的策略按预期执行。
$ sentinel apply restrict-aws-instances-type-and-tag.sentinel
Pass - restrict-aws-instances-type-and-tag.sentinel
执行通过,因为你的计划数据中的基础设施更改满足你的策略标准。
下一步
你回顾了 Sentinel 策略的各个部分,并使用模拟数据验证了策略。要了解有关 Sentinel 的更多信息,请查阅以下资源
- 了解如何 生成模拟策略数据
- 了解如何 测试 Sentinel 策略
- 了解如何 将 Sentinel 策略集上传到 HCP Terraform
- 了解有关不同的 Terraform 特定的
import类型。