23 Terraform
この章では、Terraform の設定を検査します。Terraform は、Conftest で検査できる対象が 2 つあります。HCL で書いたソース(.tf ファイル)と、変更の計画(plan)を JSON にしたものです。前半でそれぞれの構造を説明し、後半で同じ決まりを両方に対して書いて比べます。
23.1 ドメインの情報: HCL のソースと plan
Terraform では、作りたいクラウドのリソースを HCL という言語で .tf ファイルに書きます。terraform plan を実行すると、Terraform は現在の状態と比べて、どのリソースをどう変更するかの計画を作ります。この計画を terraform show -json で JSON に変換したものを、ここでは plan の JSON と呼びます。
$ terraform plan -out tfplan
$ terraform show -json tfplan > plan.jsonこの章では、次の 3 つのリソースを例にします。
main.tf
variable "tags" {
type = map(string)
default = {
Owner = "platform"
}
}
resource "aws_s3_bucket" "logs" {
bucket = "example-logs"
tags = var.tags
}
resource "aws_s3_bucket" "tmp" {
bucket = "example-tmp"
}
resource "aws_security_group_rule" "ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "sg-12345678"
}aws_s3_bucket.logsは、S3 のバケットです。タグを変数var.tagsで指定していて、その既定値は{Owner = "platform"}です。aws_s3_bucket.tmpは、タグのないバケットです。aws_security_group_rule.sshは、SSH(22 番ポート)への接続を、すべての IP アドレス(0.0.0.0/0)から許可する設定です。
23.1.1 HCL のソースの構造
Conftest は、.tf ファイルを次のような input に変換します。
$ conftest parse main.tf
{
"resource": {
"aws_s3_bucket": {
"logs": [
{
"bucket": "example-logs",
"tags": "${var.tags}"
}
],
"tmp": [
{
"bucket": "example-tmp"
}
]
},
"aws_security_group_rule": {
"ssh": [
{
"cidr_blocks": [
"0.0.0.0/0"
],
"from_port": 22,
"protocol": "tcp",
"security_group_id": "sg-12345678",
"to_port": 22,
"type": "ingress"
}
]
}
},
"variable": {
"tags": [
{
"default": {
"Owner": "platform"
},
"type": "${map(string)}"
}
]
}
}resource の下に、リソースの種類(aws_s3_bucket)、名前(logs)の順に入れ子になっています。ブロックの内容は配列の要素として入っています。
注目したいのは、logs の tags が "${var.tags}" という文字列になっていることです。HCL のソースには、変数を参照するという書き方しか書かれていません。変数の値を展開するのは Terraform なので、ソースを読むだけでは実際のタグは分かりません。
23.1.2 plan の JSON の構造
plan の JSON は、Terraform が変数やモジュールを展開した後の値を含みます。plan の JSON を作るために使った .tf ファイルには、AWS に接続せずに plan を作るためのプロバイダの設定を加えています。
main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = "ap-northeast-1"
access_key = "dummy"
secret_key = "dummy"
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
}
variable "tags" {
type = map(string)
default = {
Owner = "platform"
}
}
resource "aws_s3_bucket" "logs" {
bucket = "example-logs"
tags = var.tags
}
resource "aws_s3_bucket" "tmp" {
bucket = "example-tmp"
}
resource "aws_security_group_rule" "ssh" {
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "sg-12345678"
}変更の内容は resource_changes にあります。1 件を、json.filter(オブジェクトから指定したパスだけを残す組み込み関数)で主な項目に絞って表示します。
$ opa eval -i plan.json --format pretty 'json.filter(input.resource_changes[0], ["address", "mode", "type", "name", "change/actions", "change/after"])'
{
"address": "aws_s3_bucket.logs",
"change": {
"actions": [
"create"
],
"after": {
"bucket": "example-logs",
"force_destroy": false,
"region": "ap-northeast-1",
"tags": {
"Owner": "platform"
},
"tags_all": {
"Owner": "platform"
},
"timeouts": null
}
},
"mode": "managed",
"name": "logs",
"type": "aws_s3_bucket"
}| 項目 | 内容 |
|---|---|
address |
リソースの住所(種類.名前) |
mode |
作るリソースは managed、読み込むだけのデータソースは data |
type、name |
リソースの種類と名前 |
change.actions |
変更の種類。create、update、delete など。作り直す場合は ["delete", "create"] |
change.after |
変更後の値。削除する場合は null |
change.after.tags には、変数を展開した {"Owner": "platform"} が入っています。
23.2 Rego の書き方
次の決まりを検査します。
- S3 のバケットには
Ownerのタグを付ける - SSH をすべての IP アドレスに開放しない
- (plan だけ)リソースを削除しない
23.2.1 HCL のソースを検査する
main.rego
package main
import future.keywords.not
deny contains msg if {
some name, blocks in input.resource.aws_s3_bucket
some bucket in blocks
not bucket.tags.Owner
msg := $"aws_s3_bucket.{name}: tags.Owner is required"
}
deny contains msg if {
some name, blocks in input.resource.aws_security_group_rule
some rule in blocks
rule.type == "ingress"
rule.from_port <= 22
rule.to_port >= 22
"0.0.0.0/0" in rule.cidr_blocks
msg := $"aws_security_group_rule.{name}: SSH must not be open to the internet"
}$ conftest test main.tf
FAIL - main.tf - main - aws_s3_bucket.logs: tags.Owner is required
FAIL - main.tf - main - aws_s3_bucket.tmp: tags.Owner is required
FAIL - main.tf - main - aws_security_group_rule.ssh: SSH must not be open to the internet
3 tests, 0 passed, 0 warnings, 3 failures, 0 exceptionsinput.resource.aws_s3_bucket はリソース名をキーにしたオブジェクトなので、some name, blocks in で名前とブロックの配列を取り出し、さらに some bucket in blocks でブロックを取り出しています。
結果を見ると、logs も tags.Owner is required で失敗しています。logs の tags は "${var.tags}" という文字列なので、bucket.tags.Owner は undefined になるからです。実際には Owner のタグが付くのに、ソースからは分からないので、誤って失敗にしてしまいました。
HCL のソースを検査する方法は、terraform plan を実行しなくてよいので手軽です。ただし、変数・モジュール・関数で決まる値は検査できません。
23.2.2 plan の JSON を検査する
main.rego
package main
import future.keywords.not
# 作成・更新後の値があるリソースの変更
changes contains rc if {
some rc in input.resource_changes
rc.mode == "managed"
is_object(rc.change.after)
}
deny contains msg if {
some rc in changes
rc.type == "aws_s3_bucket"
not rc.change.after.tags.Owner
msg := $"{rc.address}: tags.Owner is required"
}
deny contains msg if {
some rc in changes
rc.type == "aws_security_group_rule"
after := rc.change.after
after.type == "ingress"
after.from_port <= 22
after.to_port >= 22
"0.0.0.0/0" in after.cidr_blocks
msg := $"{rc.address}: SSH must not be open to the internet"
}
deny contains msg if {
some rc in input.resource_changes
"delete" in rc.change.actions
msg := $"{rc.address}: deleting resources is not allowed"
}changes は、作成・更新後の値(change.after)があるリソースの変更を集める補助のルールです。削除の場合は change.after が null なので、is_object で除いています。データソース(mode が data)も除いています。
削除を禁止するルールは、change.actions に "delete" が含まれるかを調べています。作り直し(["delete", "create"])も含まれます。この例の plan には削除がないので、テストで確かめます。
main_test.rego
package main
test_delete_is_denied if {
deny["aws_s3_bucket.old: deleting resources is not allowed"] with input as {"resource_changes": [{
"address": "aws_s3_bucket.old",
"mode": "managed",
"type": "aws_s3_bucket",
"change": {"actions": ["delete"], "after": null},
}]}
}$ conftest test plan.json
FAIL - plan.json - main - aws_s3_bucket.tmp: tags.Owner is required
FAIL - plan.json - main - aws_security_group_rule.ssh: SSH must not be open to the internet
3 tests, 1 passed, 0 warnings, 2 failures, 0 exceptions
$ conftest verify
1 test, 1 passed, 0 warnings, 0 failures, 0 exceptions, 0 skippedconftest test plan.json で失敗したのは、tmp のバケットと SSH の設定だけです。plan では logs の実際のタグが見えるので、誤って失敗にすることがなくなりました。conftest verify では、削除のテストが成功しています。
plan の JSON を検査する方法は、Terraform が計算した実際の値を検査できます。ただし、terraform plan を実行する必要があり、そのためにはプロバイダの認証情報などが必要になることがあります。また、作成するまで決まらない値(ID など)は、plan では値がなく after_unknown に記録されます。
23.3 まとめ
| HCL のソース | plan の JSON | |
|---|---|---|
| 準備 | 不要 | terraform plan と terraform show -json |
input の構造 |
resource.種類.名前 の下にブロックの配列 |
resource_changes の配列。値は change.after |
| 変数などの値 | 展開されない | 展開される |
| 変更の種類 | 分からない | change.actions で分かる |
- HCL のソースは手軽に検査できますが、変数などで決まる値は検査できません。
- plan の JSON は、変数を展開した実際の値と、変更の種類を検査できます。