mcpField
I
iac-security

iac-security

github.com/snykセキュリティ68151 回インストール1 週間前に更新
リポジトリ

iac-security について

iac-securityは、デプロイ前にInfrastructure as Codeをスキャンし、設定ミスの修正を支援するスキルです。複数のIaC形式に対応し、深刻度の分析と再スキャンによる確認を行います。

主な機能

  1. 01Terraform、Kubernetes、クラウドテンプレートのスキャン
  2. 02Terraformプランと変数ファイルへの対応
  3. 03深刻度とセキュリティ分野別の分類
  4. 04安全な設定変更の提案
  5. 05再スキャンによる修正確認

活用シーン

  1. 01インフラ変更の適用前に設定ミスを検出
  2. 02IaCの問題を深刻度と分野で優先順位付け
  3. 03設定修正で問題が解消したか確認

MCP 対応

このスキルでは、`snyk_iac_scan`ツールを使ったInfrastructure as Codeのスキャン方法を説明します。

インストール

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

$npx skills add https://github.com/snyk/studio-recipes --skill iac-security

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

同梱ファイルreferences/kubernetes-security-patterns.mdreferences/terraform-security-patterns.md

詳細

Infrastructure as Code のセキュリティ

Infrastructure as Code を包括的にスキャンし、設定ミスが本番環境の脆弱性になる前に検出します。

基本原則:セキュリティ上の問題は、本番環境よりコード内で修正するほうが低コストです。


クイックスタート

1. Identify IaC files (Terraform, K8s, CloudFormation, ARM)
2. Run snyk_iac_scan on the directory
3. Analyze misconfigurations by severity
4. Provide secure configuration alternatives

サポート対象の IaC 形式

プラットフォームファイルの種類
Terraform.tf、.tf.json、.tfvars
Terraform PlanJSON プランの出力(terraform show -json)
KubernetesapiVersion と kind を含む .yaml / .yml
Helmチャートテンプレート(Chart.yaml が必要)
AWS CloudFormationAWSTemplateFormatVersion を含む .json / .yaml
Azure ARM$schema に ARM の URL を含む .json
Serverless Frameworkserverless.yml

フェーズ 1:検出

目標:スキャンが必要な IaC ファイルをすべて特定します。

次の指標を確認して、IaC の種類を判別します。

  • Terraform:.tf ファイル、terraform.tfstate、provider ブロック
  • Kubernetes:apiVersion / kind を含む YAML、k8s や manifests という名前のディレクトリ
  • CloudFormation:AWSTemplateFormatVersion キー、AWS タイプを含む Resources セクション
  • Azure ARM:deploymentTemplate を含む $schema

続いて、スキャン範囲を決定します。単一ファイル、ディレクトリ、または再帰的なスキャンです。


フェーズ 2:スキャンの実行

目標:適切な IaC セキュリティスキャンを実行します。

基本的なスキャン
Run snyk_iac_scan with:
- path: <directory or file path>
変数を使用した Terraform
Run snyk_iac_scan with:
- path: <terraform directory>
- var_file: <path to .tfvars if using variables>
Terraform Plan(より正確)
terraform plan -out=tfplan
terraform show -json tfplan > tfplan.json
Run snyk_iac_scan with:
- path: tfplan.json
- scan: "planned-values"  # or "resource-changes"
カスタムルール
Run snyk_iac_scan with:
- path: <directory>
- rules: <path to custom rules bundle>

フェーズ 3:結果の分析

目標:設定ミスを理解し、分類します。

重大度の評価
重大度リスクレベル例
Critical差し迫ったリスクパブリックな S3、開放されたセキュリティグループ
High重大なリスク暗号化の欠如、過剰な権限
Medium中程度のリスクロギングの欠如、広範な IAM 権限
Lowベストプラクティスタグの欠如、最適でない設定
概要の生成
## IaC Security Scan Results

### Overview
| Severity | Count | Status |
|----------|-------|--------|
| Critical | X | 🔴 Block |
| High | Y | 🟠 Fix Required |
| Medium | Z | 🟡 Recommended |
| Low | W | 🔵 Optional |

### Critical Issues
| Resource | Issue | Location |
|----------|-------|----------|
| aws_s3_bucket.data | Public access enabled | main.tf:45 |
| aws_security_group.web | Open to 0.0.0.0/0 on port 22 | network.tf:23 |

### High Issues
| Resource | Issue | Location |
|----------|-------|----------|
| aws_rds_instance.db | Encryption not enabled | database.tf:12 |
ドメイン別に分類

修正を容易にするため、問題をグループ化します。

  • ネットワークセキュリティ:セキュリティグループ、ネットワーク ACL、ロードバランサーの設定、VPC の設定
  • データ保護:保存時/転送時の暗号化、バックアップ設定、鍵管理
  • アクセス制御:IAM ポリシー、サービスアカウント、RBAC 設定、API 権限
  • ロギングとモニタリング:CloudTrail/監査ログ、アクセスログ、アラート設定

フェーズ 4:修正

目標:安全な設定の修正方法を提示します。以下のパターンを各検出結果に適用します。代表的な例を示します。

Terraform の修正
S3 バケット — パブリックアクセスをブロック
# Insecure
resource "aws_s3_bucket" "data" {
  bucket = "my-bucket"
}

# Secure
resource "aws_s3_bucket" "data" {
  bucket = "my-bucket"
}

resource "aws_s3_bucket_public_access_block" "data" {
  bucket = aws_s3_bucket.data.id

  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}
セキュリティグループ — アクセスを制限
# Insecure - open to world
resource "aws_security_group" "web" {
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # BAD
  }
}

# Secure - restricted to VPN/internal range
resource "aws_security_group" "web" {
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["10.0.0.0/8"]
  }
}
RDS — 暗号化を有効化
# Secure
resource "aws_db_instance" "main" {
  engine              = "postgres"
  instance_class      = "db.t3.micro"
  storage_encrypted   = true
  kms_key_id          = aws_kms_key.rds.arn
  deletion_protection = true
}
Kubernetes の修正
Pod のセキュリティ — 非 root ユーザーとリソース制限
apiVersion: v1
kind: Pod
spec:
  securityContext:
    runAsNonRoot: true
    runAsUser: 1000
  containers:
  - name: app
    image: myapp
    securityContext:
      allowPrivilegeEscalation: false
      readOnlyRootFilesystem: true
      capabilities:
        drop:
          - ALL
    resources:
      limits:
        cpu: "500m"
        memory: "512Mi"
      requests:
        cpu: "200m"
        memory: "256Mi"
ネットワークポリシー — トラフィックを制限
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: app-network-policy
spec:
  podSelector:
    matchLabels:
      app: myapp
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          name: allowed-namespace
CloudFormation の修正
S3 バケット — 暗号化とパブリックアクセスブロック
Resources:
  DataBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: aws:kms
              KMSMasterKeyID: !Ref DataBucketKey
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true

フェーズ 5:検証

目標:修正が有効であることを確認します。

変更後の再スキャン
Run snyk_iac_scan with:
- path: <same directory>

Terraform の場合は、プランを再生成してスキャンします。

terraform plan -out=tfplan.new
terraform show -json tfplan.new > tfplan.new.json
改善点の報告
## Fix Verification

| Severity | Before | After | Change |
|----------|--------|-------|--------|
| Critical | 2 | 0 | -2 ✅ |
| High | 5 | 1 | -4 ✅ |
| Medium | 8 | 6 | -2 ✅ |

### Remaining Issues
- 1 High: Third-party module - opened issue
- 6 Medium: Accepted risk (documented)

ベストプラクティス

予防
  1. CI/CD でスキャン:重大な問題があればビルドを失敗させます
  2. プリコミットフック:コミット前に問題を検出します
  3. モジュールのセキュリティ:再利用可能なモジュールをスキャンします
  4. ポリシーをコードとして管理:組織の標準に合わせてカスタムルールを定義します
ポリシーファイルの使用

例外を管理するために .snyk を作成します。

ignore:
  SNYK-CC-TF-123:
    - '*':
        reason: 'Accepted risk - internal development environment'
        expires: 2025-06-01
        created: 2024-01-15
カスタムルール

組織固有の要件に対応するには:

  1. Rego(OPA)形式でルールを記述します
  2. .tar.gz としてバンドルします
  3. --rules オプションを指定してスキャンに渡します

エラー処理

エラー解決策
Terraform の状態を読み取れませんでしたterraform init を実行します。状態バックエンドを確認します。.tf ファイルを直接スキャンします
HCL の構文が無効ですterraform validate を実行します。構文を確認します。すべての変数が定義されていることを確認します
プランファイルを解析できませんでしたterraform show -json で再生成します。Terraform のバージョン互換性を確認します。JSON が有効であることを確認します

制約事項

  1. 適用前にスキャンする:スキャンしていない IaC は絶対に適用しないでください
  2. 重大な問題がある場合はブロックする:重大な問題は修正する必要があります
  3. 例外を文書化する:許容したリスクには .snyk ポリシーを使用します
  4. プランを検証する:Terraform ではファイルのスキャンよりもプランのスキャンを優先します
  5. 継続的に監視する:依存関係を更新したら再スキャンします

コメント