tfsec入門してみる
セキュリティの機運が高まってきて、tfsecを導入しようというような動きがあるので事前に学習してみる。
- tfsecのinstall
$ brew install tfsec
- tfsecでチェックするためのterraformのコードを用意
- ガバガバな全開放のセキュリティグループをnginxを入れたEC2インスタンス立てる
resource "aws_instance" "nginx" { ami = "ami-0218d08a1f9dac831" vpc_security_group_ids = [aws_security_group.nginx_security_group.id] instance_type = "t3.micro" tags = { Name = "nginx_server" } user_data = <<EOF #!/bin/bash amazon-linux-extras install -y nginx1 systemctl restart nginx.service EOF } resource "aws_security_group" "nginx_security_group" { name = "ec2" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } }
- tfsecを実行
- 実際はもっと大量にでていますが、省略して一部のみ。
Result #1 CRITICAL Security group rule allows ingress from public internet. ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── main.tf Line 22 ───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 22 │ cidr_blocks = ["0.0.0.0/0"] ───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ID aws-vpc-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.19.1/checks/aws/vpc/no-public-ingress-sgr/ - https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule#cidr_blocks ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
上記で引っかかっているのは↓のようです。
aws-vpc-no-public-ingress-sgr
というIDで管理されているみたい。
resource "aws_security_group" "nginx_security_group" { name = "ec2" ingress { from_port = 80 to_port = 80 protocol = "tcp" # tfsec:ignore:aws-vpc-no-public-ingress-sgr cidr_blocks = ["0.0.0.0/0"] } }
ID指定で一旦無視させることでtfsecのチェックから除外することができる。
なお、workspace単位でignoreすることもできるので、途中からtfsecを導入してどえらい量の警告が出た場合は一旦workspace単位で無視してみると導入しやすそう。
危険な状態である。という認識を得るだけでも意味がある。
どうせならGHAで動かしたいので探してみるといくつかあった。
今回はRun tfsec PR commenterを使ってみる。
name: tfsec-pr-commenter on: pull_request: workflow_dispatch: jobs: tfsec: name: tfsec PR commenter runs-on: ubuntu-latest steps: - name: Clone repo uses: actions/checkout@master - name: tfsec uses: aquasecurity/tfsec-pr-commenter-action@v1.2.0 with: tfsec_args: --soft-fail --force-all-dirs github_token: ${{ secrets.GITHUB_TOKEN }}
全ディレクトリを見てほしかったので、 --force-all-dirs
を追加。
その他に使える引数は以下。 tfsec --help
で出てくるのはだいたい使えるみたい。
結果。自動でPRにコメントしてくれます。
中々使い勝手がよいです。デメリットは特にはないかな・・・?