みらい翻訳のバックエンドエンジニアのtoshと申します。
弊社ではインフラの構築にterraformを利用しています。
今回はterraformのセキュリティスキャンツールを紹介します。
tfsecとは
Aqua Securityという会社が開発しているオープンソースのterraformの静的解析ツールで、セキュリティスキャンをしてくれます。
特徴として、非常に高速に動作することと、ローカルはもちろんCI/CDパイプラインで動作させることが想定されています。
GitHub - aquasecurity/tfsec: Security scanner for your Terraform code
tfsec uses static analysis of your terraform code to spot potential misconfigurations.
tfsecは、Terraformコードの静的分析を使用して、潜在的な構成ミスを見つけます。
また、technology radarではAdoptになっていました。 Tools | Thoughtworks
For our projects using Terraform, tfsec has quickly become a default static analysis tool to detect potential security risks. It's easy to integrate into a CI pipeline and has a growing library of checks against all of the major cloud providers and platforms like Kubernetes. Given its ease of use, we believe tfsec could be a good addition to any Terraform project.
Terraformを使用するプロジェクトでは、tfsecはすぐに、潜在的なセキュリティリスクを検出する既定の静的分析ツールになりました。 CIパイプラインに簡単に統合でき、Kubernetesのような主要なクラウドプロバイダーやプラットフォームすべてに対するチェックのライブラリが増えています。 使いやすさを考えると、tfsecはTerraformプロジェクトに追加するのに適していると思います。
ローカルで試してみる
インストール
brew install tfsec
実行
tfsec実行のために、以下のようなセキュリティグループを作成するtfファイルを作成しておきました。
resource "aws_security_group" "test" {
name = "test-security-group"
description = "For test"
vpc_id = "vpc-xxxxxx"
}
resource "aws_security_group_rule" "api_ingress" {
security_group_id = aws_security_group.test.id
type = "ingress"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
resource "aws_security_group_rule" "egress_all" {
security_group_id = aws_security_group.test.id
type = "egress"
description = "Allow all"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
実行の仕方は以下の通りです。
# tfファイルがあるディレクトリで
tfsec
# パスを指定する場合
tfsec ${terraformファイルがあるディレクトリ}
実行結果
実行結果は下記の通りです。実行結果の中に実行にかかった時間が記載されていますが、非常に高速に動作したことがわかると思います。
また、指摘の詳細と一緒にterraformのドキュメントのリンクを一緒に吐いてくれるのは便利だと思いました。
$ tfsec
Result #1 CRITICAL Security group rule allows ingress from public internet.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
main.tf:13
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
7 resource "aws_security_group_rule" "api_ingress" {
8 security_group_id = aws_security_group.test.id
9 type = "ingress"
10 from_port = 443
11 to_port = 443
12 protocol = "tcp"
13 [ cidr_blocks = ["0.0.0.0/0"]
14 }
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ID aws-ec2-no-public-ingress-sgr
Impact Your port exposed to the internet
Resolution Set a more restrictive cidr range
More Information
- https://aquasecurity.github.io/tfsec/v1.27.1/checks/aws/ec2/no-public-ingress-sgr/
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule#cidr_blocks
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Result #2 CRITICAL Security group rule allows egress to multiple public internet addresses.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
main.tf:23
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
16 resource "aws_security_group_rule" "egress_all" {
17 security_group_id = aws_security_group.test.id
18 type = "egress"
19 description = "Allow all"
20 from_port = 0
21 to_port = 0
22 protocol = "-1"
23 [ cidr_blocks = ["0.0.0.0/0"]
24 }
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ID aws-ec2-no-public-egress-sgr
Impact Your port is egressing data to the internet
Resolution Set a more restrictive cidr range
More Information
- https://aquasecurity.github.io/tfsec/v1.27.1/checks/aws/ec2/no-public-egress-sgr/
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Result #3 LOW Security group rule does not have a description.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
main.tf:7-14
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
7 resource "aws_security_group_rule" "api_ingress" {
8 security_group_id = aws_security_group.test.id
9 type = "ingress"
10 from_port = 443
11 to_port = 443
12 protocol = "tcp"
13 cidr_blocks = ["0.0.0.0/0"]
14 }
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ID aws-ec2-add-description-to-security-group-rule
Impact Descriptions provide context for the firewall rule reasons
Resolution Add descriptions for all security groups rules
More Information
- https://aquasecurity.github.io/tfsec/v1.27.1/checks/aws/ec2/add-description-to-security-group-rule/
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
timings
──────────────────────────────────────────
disk i/o 116.642µs
parsing 870.418µs
adaptation 4.862873ms
checks 25.687667ms
total 31.5376ms
counts
──────────────────────────────────────────
modules downloaded 0
modules processed 1
blocks processed 3
files read 1
results
──────────────────────────────────────────
passed 2
ignored 0
critical 2
high 0
medium 0
low 1
2 passed, 3 potential problem(s) detected.
CIで動作させる
https://github.com/aquasecurity/tfsec-action
こちらを参考に以下のコードを作成しました。
name: tfsec
on:
workflow_dispatch:
jobs:
tfsec:
name: tfsec sarif report
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
steps:
- name: Clone repo
uses: actions/checkout@v2
with:
persist-credentials: false
- name: tfsec
uses: aquasecurity/tfsec-action@v1.0.0
with:
working_directory: ./tosh/test_tfsec

まとめ
セキュアなterraformコードを書くお供としてtfsec使ってみては良いのではないでしょうか。 インストールも簡単で非常に高速に動作するのでおすすめです!