🍣
sp-initiated, authncontext無効化
SAMLをCloud Storageに保存してIAPで認証認可を行い、SP-initiated設定とAuthnContextなしの設定を実装するためのTerraformコードを作成します。
# Cloud Storage バケットの作成 (SAML証明書保存用)
resource "google_storage_bucket" "saml_bucket" {
name = "saml-certificates-${var.project_id}"
location = "asia-northeast1"
force_destroy = false
uniform_bucket_level_access = true
versioning {
enabled = true
}
}
# SAML証明書のアップロード
resource "google_storage_bucket_object" "saml_metadata" {
name = "idp-metadata.xml"
bucket = google_storage_bucket.saml_bucket.name
source = "path/to/local/idp-metadata.xml" # ローカルのSAML IDPメタデータファイル
}
# SAMLメタデータを読み込むためのサービスアカウント
resource "google_service_account" "saml_reader" {
account_id = "saml-metadata-reader"
display_name = "SAML Metadata Reader"
}
# サービスアカウントにCloud Storageオブジェクト閲覧権限を付与
resource "google_storage_bucket_iam_binding" "saml_reader_binding" {
bucket = google_storage_bucket.saml_bucket.name
role = "roles/storage.objectViewer"
members = [
"serviceAccount:${google_service_account.saml_reader.email}",
]
}
# IAP用ブランドの作成
resource "google_iap_brand" "project_brand" {
support_email = "admin@example.com"
application_title = "My Application"
project = var.project_id
}
# IAPクライアントの作成
resource "google_iap_client" "project_client" {
display_name = "My SAML Client"
brand = google_iap_brand.project_brand.name
}
# SAMLプロバイダーの設定
resource "google_iap_identity_provider" "saml_provider" {
brand = google_iap_brand.project_brand.name
idp_id = "my-saml-provider"
name = "My SAML Provider"
# SAML設定
saml {
# Cloud Storageから読み込むための準備
idp_metadata = file(google_storage_bucket_object.saml_metadata.output_path)
# SP-initiated設定: グループマッピングの設定
groups_claim = "groups"
# AuthnContextの無効化
force_authn = false
# 必要に応じて追加設定
assertion_consumer_redirect_uris = [
"https://app.example.com/oauth2/callback"
]
request_binding = "HTTP_POST" # または "HTTP_REDIRECT"
}
}
# ロードバランサー設定
resource "google_compute_backend_service" "default" {
name = "backend-service"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10
backend {
group = google_compute_instance_group_manager.default.instance_group
}
# IAPの有効化
iap {
oauth2_client_id = google_iap_client.project_client.client_id
oauth2_client_secret = google_iap_client.project_client.secret
}
}
# IAP設定
resource "google_iap_web_backend_service_iam_binding" "binding" {
project = var.project_id
web_backend_service = google_compute_backend_service.default.name
role = "roles/iap.httpsResourceAccessor"
members = [
"group:admin@example.com",
"domain:example.com"
]
}
# SP-Initiated用のリダイレクトURLの出力
output "saml_sp_initiated_url" {
value = "https://iap.googleapis.com/v1/projects/${var.project_number}/brands/${google_iap_brand.project_brand.name}/identityProviders/${google_iap_identity_provider.saml_provider.idp_id}"
description = "SAML SP-initiated URL for identity provider configuration"
}
# ACS URLの出力(IdP側の設定で必要)
output "saml_acs_url" {
value = "https://iap.googleapis.com/v1/projects/${var.project_number}/brands/${google_iap_brand.project_brand.name}/identityProviders/${google_iap_identity_provider.saml_provider.idp_id}/redirectUri"
description = "SAML ACS URL for identity provider configuration"
}
注意点と補足情報:
-
AuthnContextの無効化:
- 上記コードでは
force_authn = false
を設定していますが、実際のAuthnContextの無効化はIdP側でも設定が必要な場合があります - 多くのIdPでは、SP側からのリクエストを修正して特定のAuthnContextを無視する設定が可能です
- 上記コードでは
-
Cloud Storageの証明書保存:
- セキュリティ向上のため、バケットにはバージョニングを有効化
- 証明書ファイルへのアクセスは特定のサービスアカウントのみに制限
-
SP-initiated設定:
- SP-initiatedフローを有効にするには、ユーザーがアクセスするURLと、IdPにリダイレクトするURLを正しく設定する必要があります
- 外部IdPの設定では、
saml_sp_initiated_url
の出力値をSPのエンティティIDとして使用します
-
IdP側の設定:
- IdP側で以下を設定する必要があります:
- SPエンティティID
- AssertionConsumerService (ACS) URL
- 適切な属性マッピング(例:メールアドレス、グループ)
- AuthnContextの設定解除(IdP依存)
- IdP側で以下を設定する必要があります:
-
デバッグと運用:
- SAML認証のトラブルシューティングのため、Cloud Loggingでの監査ログ収集を設定することをお勧めします
- IdPとの通信に問題がある場合、SAML認証のデバッグツールを使用して問題を特定できます
このコードを実行すると、IAPがSAML認証の設定とCloud Storage上の証明書を使用して、SP-initiated SSOフローを処理できるようになります。
Discussion