Cloud Load BalancingでのHTTPS強制設定について

このブログシリーズ 「クラウドセキュリティ 実践集」 では、一般的なセキュリティ課題を取り上げ、「なぜ危険なのか?」 というリスクの解説から、 「どうやって直すのか?」 という具体的な修復手順(コンソール、gcloud CLI、Terraformなど)まで、分かりやすく解説します。

この記事では、Google Cloud Load BalancingでHTTPSを強制して通信を暗号化する設定手順について、リスクと対策を解説します。

ポリシーの説明

Google Cloud Load BalancingでHTTP(非暗号化通信)が使用されている場合、クライアントとロードバランサー間の通信が平文で送信されます。これにより、通信内容が盗聴される可能性があり、機密情報の漏洩やセッション情報の盗取などのセキュリティリスクが発生します。HTTPS通信を強制することで、すべての通信を暗号化し、データの機密性と完全性を確保することが重要です。

修復方法

コンソールでの修復手順

Google Cloud コンソールを使用して、Cloud Load BalancingでHTTPSを強制する設定を行います。

1. 既存のHTTP(S)ロードバランサーの確認

  1. Google Cloud Consoleにログインします
  2. ナビゲーションメニューから「ネットワーキング」→「ネットワーク サービス」→「ロードバランシング」を選択します
  3. 既存のロードバランサーの一覧から、HTTPを使用しているロードバランサーを特定します

2. SSL証明書の準備

  1. 「ロードバランシング」画面で「証明書」タブを選択します
  2. 「証明書を作成」をクリックします
  3. 以下のいずれかの方法で証明書を設定します:
    • Google マネージド証明書(推奨): ドメイン名を入力するだけで自動的に証明書が発行・更新されます
    • 自己署名証明書: テスト環境での使用に限定してください
    • カスタム証明書: 既存の証明書をアップロードします

3. HTTPS フロントエンドの設定

  1. 対象のロードバランサーを選択し、「編集」をクリックします
  2. 「フロントエンドの設定」セクションで以下を設定します:
    • プロトコル: HTTPS
    • IPアドレス: 既存のIPまたは新規作成
    • ポート: 443
    • 証明書: 手順2で作成した証明書を選択

5. HTTPからHTTPSへのリダイレクト設定

  1. 「ホストとパスのルール」セクションで「詳細設定」を展開します
  2. 「URL リダイレクト」を有効にします
  3. 以下の設定を行います:
    • リダイレクトタイプ: 301(永続的なリダイレクト)
    • HTTPSリダイレクト: 有効
    • ホストリダイレクト: 元のホストを維持
    • パスリダイレクト: 元のパスを維持

4. HTTPフロントエンドの削除(オプション)

※ リダイレクトをするのも手段ですが、基本的には特別な要件がなければ、HTTPのポートを閉じておく方が良いと思われます。

HTTPSリダイレクトを設定した後、HTTPフロントエンドを完全に削除することも可能です:

  1. 「フロントエンドの設定」でHTTP(ポート80)のエントリを選択
  2. 「削除」アイコンをクリックして削除します

Terraformでの修復手順

Cloud Load BalancingでHTTPSを強制するTerraformコードと、主要な修正ポイントを説明します。

# Google マネージド SSL証明書の作成
resource "google_compute_managed_ssl_certificate" "default" {
  name = "example-ssl-cert"

  managed {
    domains = ["example.com", "www.example.com"]
  }
}

# グローバル転送ルール(HTTPS)
resource "google_compute_global_forwarding_rule" "https" {
  name       = "example-https-forwarding-rule"
  target     = google_compute_target_https_proxy.default.self_link
  port_range = "443"
  ip_address = google_compute_global_address.default.address
}

# HTTPS ターゲットプロキシ
resource "google_compute_target_https_proxy" "default" {
  name             = "example-https-proxy"
  url_map          = google_compute_url_map.default.self_link
  ssl_certificates = [google_compute_managed_ssl_certificate.default.self_link]
}

# HTTPからHTTPSへのリダイレクト用URL マップ
resource "google_compute_url_map" "http_redirect" {
  name = "http-redirect"

  default_url_redirect {
    https_redirect         = true
    redirect_response_code = "MOVED_PERMANENTLY_DEFAULT"
    strip_query            = false
  }
}

# HTTP用のターゲットプロキシ(リダイレクト用)
resource "google_compute_target_http_proxy" "http_redirect" {
  name    = "http-redirect-proxy"
  url_map = google_compute_url_map.http_redirect.self_link
}

# グローバル転送ルール(HTTPリダイレクト用)
resource "google_compute_global_forwarding_rule" "http_redirect" {
  name       = "http-redirect-rule"
  target     = google_compute_target_http_proxy.http_redirect.self_link
  port_range = "80"
  ip_address = google_compute_global_address.default.address
}

# 静的IPアドレス
resource "google_compute_global_address" "default" {
  name = "example-ip"
}

# HSTSを含むセキュリティヘッダーの設定(Cloud Armor経由)
resource "google_compute_security_policy" "default" {
  name = "example-security-policy"

  rule {
    action   = "allow"
    priority = "2147483647"
    match {
      versioned_expr = "SRC_IPS_V1"
      config {
        src_ip_ranges = ["*"]
      }
    }
  }
}

# バックエンドサービス
resource "google_compute_backend_service" "default" {
  name        = "example-backend"
  port_name   = "http"
  protocol    = "HTTP"
  timeout_sec = 10

  security_policy = google_compute_security_policy.default.self_link

  backend {
    group = google_compute_instance_group_manager.default.instance_group
  }

  health_checks = [google_compute_health_check.default.self_link]

  # カスタムヘッダーの追加(HSTS等)
  custom_response_headers = [
    "Strict-Transport-Security: max-age=31536000; includeSubDomains",
    "X-Content-Type-Options: nosniff",
    "X-Frame-Options: SAMEORIGIN"
  ]
}

# メインのURL マップ
resource "google_compute_url_map" "default" {
  name            = "example-url-map"
  default_service = google_compute_backend_service.default.self_link

  # 追加のルーティングルール(必要に応じて)
  host_rule {
    hosts        = ["example.com"]
    path_matcher = "allpaths"
  }

  path_matcher {
    name            = "allpaths"
    default_service = google_compute_backend_service.default.self_link

    path_rule {
      paths   = ["/*"]
      service = google_compute_backend_service.default.self_link
    }
  }
}

 

主要な修正ポイント

  1. SSL証明書の管理: Google マネージド証明書を使用することで、証明書の発行と更新を自動化
  2. HTTPSプロキシの設定: google_compute_target_https_proxyでSSL証明書を関連付け
  3. HTTPリダイレクト: 専用のURL マップでHTTPからHTTPSへの自動リダイレクトを設定
  4. 統一IPアドレス: HTTPとHTTPSで同じIPアドレスを使用し、ユーザーの利便性を向上
  5. セキュリティヘッダー: HSTSヘッダーを追加して、ブラウザ側でもHTTPS接続を強制

最後に

この記事では、Google Cloud Load BalancingでHTTPSを強制して通信を暗号化する設定手順について、リスクと対策を解説しました。

この問題の検出は弊社が提供するSecurifyのCSPM機能で簡単に検出及び管理する事が可能です。 運用が非常に楽に出来る製品になっていますので、ぜひ興味がある方はお問い合わせお待ちしております。 最後までお読みいただきありがとうございました。この記事が皆さんの役に立てば幸いです。

参考情報

この記事をシェアする

クラウドセキュリティ対策実践集一覧へ戻る

貴社の利用状況に合わせた見積もりを作成します。

料金プランを詳しく見る