mcpField
A
auditing-terraform-infrastructure-for-security

auditing-terraform-infrastructure-for-security

github.com/mukul975セキュリティ35.2k267 回インストール8 週間前に更新
リポジトリ

auditing-terraform-infrastructure-for-security について

Terraform のコード、プラン、状態ファイルをデプロイ前後に監査するセキュリティスキルです。IaC スキャナー、独自の OPA ポリシー、CI/CD チェックを組み合わせ、クラウド基盤の管理に役立ちます。

主な機能

  1. 01Checkov と tfsec による Terraform スキャン
  2. 02Terrascan によるコンプライアンス検査
  3. 03独自 OPA ポリシーの定義
  4. 04CI/CD へのセキュリティゲート追加
  5. 05Terraform 状態の設定不備監査

活用シーン

  1. 01適用前の Terraform 変更レビュー
  2. 02プルリクエストでのセキュリティ検査
  3. 03Terraform 状態から稼働中の問題を発見

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mukul975/anthropic-cybersecurity-skills --skill auditing-terraform-infrastructure-for-security

インストール後、Claude Code で/auditing-terraform-infrastructure-for-securityを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

LICENSE
references/
api-reference.md
scripts/
agent.py

詳細

セキュリティのための Terraform インフラストラクチャ監査

使用する場面

  • Terraform のデプロイ向け CI/CD パイプラインにセキュリティスキャンを組み込む場合
  • 適用前に、セキュリティのベストプラクティスに沿って Terraform のプランやモジュールをレビューする場合
  • クラウドインフラストラクチャのプロビジョニングに、ポリシー・アズ・コードのガードレールを構築する場合
  • 既存の Terraform の状態ファイルを監査し、デプロイ済みの設定ミスを特定する場合
  • 複数の Terraform プロジェクトに組織のセキュリティ標準を適用する場合

使用しないでください。ランタイムのセキュリティ監視(CSPM ツールを使用)、アプリケーションのセキュリティテスト(SAST/DAST ツールを使用)、クラウド設定のドリフト検出(デプロイ後に AWS Config または Azure Policy を使用)には適していません。

前提条件

  • Checkov がインストールされていること(pip install checkov)
  • tfsec がインストールされていること(brew install tfsec または GitHub のバイナリ)
  • Terrascan がインストールされていること(brew install terrascan)
  • プラン生成用の Terraform v1.0 以降
  • カスタムポリシーの適用用の OPA(Open Policy Agent)
  • 監査対象の Terraform コードを含む Git リポジトリ

ワークフロー

ステップ 1:Checkov で Terraform コードをスキャンする

組み込みポリシーとカスタムポリシーを使用して、包括的な IaC セキュリティスキャンを行うために Checkov を実行します。

# Scan a Terraform directory
checkov -d ./terraform/ --framework terraform

# Scan with specific check categories
checkov -d ./terraform/ --check CKV_AWS_18,CKV_AWS_19,CKV_AWS_20,CKV_AWS_21

# Scan and output results in JSON
checkov -d ./terraform/ --output json > checkov-results.json

# Scan a Terraform plan file for more accurate analysis
terraform init && terraform plan -out=tfplan
terraform show -json tfplan > tfplan.json
checkov -f tfplan.json --framework terraform_plan

# Skip specific checks with justification
checkov -d ./terraform/ --skip-check CKV_AWS_145 \
  --bc-api-key $BRIDGECREW_API_KEY

# Scan Terraform modules
checkov -d ./modules/ --framework terraform --compact

# List all available checks
checkov --list --framework terraform | grep CKV_AWS
ステップ 2:tfsec で Terraform 固有の問題をスキャンする

Terraform に特化したセキュリティ分析と詳細な修正ガイダンスには、tfsec を使用します。

# Scan a Terraform directory
tfsec ./terraform/

# Scan with minimum severity threshold
tfsec ./terraform/ --minimum-severity HIGH

# Output in JSON for CI/CD processing
tfsec ./terraform/ --format json > tfsec-results.json

# Scan with custom checks
tfsec ./terraform/ --custom-check-dir ./custom-checks/

# Exclude specific rules
tfsec ./terraform/ --exclude-downloaded-modules \
  --exclude aws-s3-enable-bucket-logging

# Scan and fail on specific severity
tfsec ./terraform/ --minimum-severity CRITICAL --soft-fail

# Generate SARIF output for GitHub Security tab
tfsec ./terraform/ --format sarif > tfsec.sarif
ステップ 3:Terrascan で複数のフレームワークへの準拠を確認する

CIS、NIST、SOC 2 のフレームワークに対するコンプライアンスチェックのために Terrascan を実行します。

# Scan Terraform against CIS AWS benchmark
terrascan scan -t aws -i terraform -d ./terraform/ \
  --policy-type aws --verbose

# Scan against specific compliance frameworks
terrascan scan -t aws -i terraform -d ./terraform/ \
  --policy-type aws \
  --categories "Compliance Validation"

# Output in JSON
terrascan scan -t aws -i terraform -d ./terraform/ \
  --output json > terrascan-results.json

# Scan a Terraform plan
terrascan scan -t aws -i terraform \
  --iac-file tfplan.json \
  --iac-type tfplan

# List available policies
terrascan scan --list-policies -t aws
ステップ 4:組織標準向けのカスタム OPA ポリシーを作成する

組織固有のセキュリティ要件に対応する Rego ポリシーを記述します。

# policy/aws_s3_encryption.rego
package terraform.aws.s3

deny[msg] {
    resource := input.resource.aws_s3_bucket[name]
    not resource.server_side_encryption_configuration
    msg := sprintf("S3 bucket '%s' must have server-side encryption enabled", [name])
}

# policy/aws_iam_no_wildcards.rego
package terraform.aws.iam

deny[msg] {
    resource := input.resource.aws_iam_policy[name]
    statement := resource.policy.Statement[_]
    statement.Action == "*"
    statement.Effect == "Allow"
    msg := sprintf("IAM policy '%s' must not use wildcard (*) actions", [name])
}

deny[msg] {
    resource := input.resource.aws_iam_policy[name]
    statement := resource.policy.Statement[_]
    statement.Resource == "*"
    statement.Effect == "Allow"
    contains(statement.Action[_], "*")
    msg := sprintf("IAM policy '%s' has overly permissive actions on wildcard resources", [name])
}

# policy/aws_no_public_ingress.rego
package terraform.aws.security_group

deny[msg] {
    resource := input.resource.aws_security_group_rule[name]
    resource.type == "ingress"
    resource.cidr_blocks[_] == "0.0.0.0/0"
    resource.from_port <= 22
    resource.to_port >= 22
    msg := sprintf("Security group rule '%s' allows SSH from 0.0.0.0/0", [name])
}
# Evaluate Terraform plan against OPA policies
terraform show -json tfplan | opa eval \
  --data ./policy/ \
  --input /dev/stdin \
  "data.terraform.aws" \
  --format pretty

# Run Conftest for easier OPA policy testing
conftest test tfplan.json --policy ./policy/ --output json
ステップ 5:セキュリティスキャンを CI/CD パイプラインに組み込む

IaC セキュリティスキャンを必須の CI/CD ゲートとして追加します。

# GitHub Actions: Terraform security pipeline
name: Terraform Security Scan
on:
  pull_request:
    paths: ['terraform/**']

jobs:
  security-scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3

      - name: Terraform Init & Plan
        run: |
          cd terraform/
          terraform init
          terraform plan -out=tfplan
          terraform show -json tfplan > tfplan.json

      - name: Checkov Scan
        uses: bridgecrewio/checkov-action@master
        with:
          directory: terraform/
          framework: terraform
          output_format: sarif
          output_file_path: checkov.sarif
          soft_fail: false

      - name: tfsec Scan
        uses: aquasecurity/[email protected]
        with:
          working_directory: terraform/
          soft_fail: false

      - name: Upload SARIF
        uses: github/codeql-action/upload-sarif@v2
        with:
          sarif_file: checkov.sarif

      - name: OPA Policy Check
        run: |
          conftest test terraform/tfplan.json \
            --policy ./policy/ \
            --output json
ステップ 6:Terraform の状態をスキャンしてデプロイ済みの設定ミスを特定する

現在の Terraform の状態を監査し、すでにデプロイされているセキュリティ上の問題を特定します。

# Export current state as JSON
terraform show -json > terraform-state.json

# Scan the state with Checkov
checkov -f terraform-state.json --framework terraform_plan

# Query state for specific security issues
terraform state list | while read resource; do
  terraform state show "$resource" 2>/dev/null | grep -i "public\|0.0.0.0\|encrypt.*false\|password"
done

# Find resources without required tags
terraform state list | grep aws_instance | while read resource; do
  tags=$(terraform state show "$resource" | grep -A20 "tags")
  if ! echo "$tags" | grep -q "Environment"; then
    echo "MISSING TAG: $resource lacks 'Environment' tag"
  fi
done

主な概念

用語定義
Infrastructure as Code手動でコンソールを操作する代わりに、宣言型設定ファイル(Terraform、CloudFormation)を使用してクラウドインフラストラクチャを管理する手法
Policy as Codeインフラストラクチャ定義に対して自動評価できる、実行可能なコード(Rego、Python)としてセキュリティポリシーとコンプライアンスポリシーを表現すること
Shift Left Securityプロビジョニング後に監査するのではなく、デプロイ前に IaC をスキャンすることで、開発ライフサイクルの早い段階にセキュリティチェックを移すこと
Terraform PlanTerraform が行う変更のプレビュー。変更を適用する前にセキュリティスキャンできるよう、JSON としてエクスポートできます
CheckovTerraform、CloudFormation、Kubernetes、Docker に対応し、1000 以上の組み込みポリシーを備えた、IaC 向けのオープンソース静的解析ツール
OPA/Regoカスタムセキュリティルールを定義し、構造化データの入力に対して評価するための Open Policy Agent と、そのポリシー言語 Rego

ツールとシステム

  • Checkov:Terraform、CloudFormation、Kubernetes、ARM、Dockerfile 向けの1000以上のポリシーを備えた、包括的な IaC スキャナー
  • tfsec:詳細な修正ガイダンスと SARIF 出力に対応した、Terraform 専用の静的解析ツール
  • Terrascan:ポリシー・アズ・コードにより、コンプライアンスフレームワーク(CIS、NIST、SOC 2)に対応するマルチ IaC スキャナー
  • OPA/Conftest:Rego 言語を使用して組織固有のセキュリティルールを定義する、カスタムポリシーエンジン
  • Bridgecrew:Checkov を基盤とする商用プラットフォーム。ドリフト検出とサプライチェーンセキュリティを提供します

一般的なシナリオ

シナリオ:既存の Terraform CI/CD パイプラインへのセキュリティゲートの追加

背景:DevOps チームは GitHub Actions で Terraform を使ってインフラをデプロイしていますが、セキュリティスキャンは実施していません。最近の監査で、暗号化されていない S3 バケットや、インターネットからの SSH 接続を許可しているセキュリティグループが複数見つかりました。

アプローチ:

  1. GitHub Actions のワークフローに、最初のセキュリティゲートとして Checkov を追加する
  2. checkov -d ./terraform/ を実行して、現在の指摘事項のベースラインを把握する
  3. 既存の指摘事項をトリアージする:CRITICAL の問題は修正し、HIGH はチケットを作成し、許容するリスクは抑制する
  4. Terraform 固有のチェックを行う副次的なスキャナーとして tfsec を追加する
  5. 組織の標準(必須タグ、命名規則)に対応するカスタム OPA ポリシーを作成する
  6. CRITICAL または HIGH の指摘事項があるプルリクエストをブロックするようにパイプラインを設定する
  7. GitHub の Security タブとの統合に向けて SARIF レポートを生成する

注意点:既存のプロジェクトにセキュリティスキャンを追加すると、最初は何百件もの指摘事項が出る可能性があります。まずは CRITICAL のみをブロックし、その後 HIGH へと段階的に拡大してください。意図的な例外には、根拠を文書化したうえで、インラインの抑制コメント(#checkov:skip=CKV_AWS_18:Public bucket for static website)を使用してください。

出力形式

Terraform Security Audit Report
==================================
Repository: acme-corp/infrastructure
Branch: main
Scan Date: 2026-02-23
Tools: Checkov 3.x, tfsec 1.x, OPA custom policies

SCAN RESULTS:
  Checkov checks passed:    187
  Checkov checks failed:     34
  tfsec checks passed:      156
  tfsec checks failed:       28
  OPA custom policies:       12 passed, 3 failed

CRITICAL FINDINGS:
[TF-001] S3 Bucket Without Encryption
  File: modules/storage/main.tf:24
  Resource: aws_s3_bucket.data_lake
  Check: CKV_AWS_19
  Fix: Add server_side_encryption_configuration block

[TF-002] Security Group Allows SSH from 0.0.0.0/0
  File: modules/network/security.tf:45
  Resource: aws_security_group_rule.ssh_access
  Check: CKV_AWS_24
  Fix: Restrict cidr_blocks to bastion subnet

[TF-003] IAM Policy with Wildcard Actions
  File: modules/iam/policies.tf:12
  Resource: aws_iam_policy.developer_policy
  Check: CKV_AWS_1
  Fix: Scope actions to specific services required

SUMMARY BY SEVERITY:
  Critical:  6 findings
  High:     14 findings
  Medium:   28 findings
  Low:      18 findings
  Info:     12 findings

コメント