IAP でカスタム OAuth クライアントを使用する

このドキュメントでは、Identity-Aware Proxy(IAP)でカスタム OAuth クライアントを使用するタイミングと方法について説明します。

デフォルトでは、IAP は Google が管理する OAuth クライアントを使用してユーザーを認証します。Google 管理の OAuth クライアントは、組織内の内部ユーザーのアクセス管理にのみ使用できます。

カスタム OAuth 構成を使用して、次の操作を行う必要があります。

  • 組織外の外部ユーザーに対して、IAP 対応アプリケーションへのアクセスを管理します。
  • Google Cloud 組織に属していないプロジェクト内のウェブ アプリケーションへのアクセスを管理します。
  • 認証中に同意画面にカスタム ブランド情報を表示します。

カスタム OAuth クライアントは、IAP またはプラットフォームで直接構成できます。

カスタム OAuth クライアントを使用する場合は、OAuth 同意画面を構成する必要があります。同意画面にカスタム ブランディングを表示するには、Google の検証のためにアプリケーションを送信する必要があります。確認プロセスの詳細については、OAuth 同意画面の設定をご覧ください。

カスタム OAuth クライアントを構成する場合は、認証情報の作成と管理はお客様の責任で行っていただきます。これには、クライアント シークレットの安全な保存と、必要に応じて承認済みクライアントとの共有が含まれます。

Google 管理の OAuth クライアントとカスタム OAuth クライアントを比較する

Google 管理の OAuth クライアントは、IAP で保護されたアプリケーションにプログラムでアクセスできません。ただし、Google 管理の OAuth クライアントを使用する IAP で保護されたアプリケーションには、programmatic_clients 設定またはサービス アカウントの JWT を介して構成された別の OAuth クライアントを使用して、プログラムでアクセスできます。

次の表に、Google マネージド OAuth クライアントとカスタム OAuth クライアントの比較を示します。

Google マネージド OAuth クライアント カスタム OAuth クライアント
ユーザー 内部専用 地域が分散した
ブランド Google Cloud brand 顧客所有のブランド
OAuth の設定 Google による構成 お客様による構成
Oauth 認証情報 Google が管理 顧客管理
アプリケーション アクセス ブラウザ フローのみ ブラウザフローとプログラムによるアクセス

ブランディング ページを構成する

Google Cloud コンソールを使用してカスタム ブランディング ページを構成する手順は次のとおりです。

  1. Google Cloud コンソールで、OAuth の [ブランディング] ページに移動します。

    [ブランディング] に移動

  2. [開始] をクリックします。

  3. [アプリ名] に、同意画面に表示するアプリケーションの名前を入力します。

  4. [ユーザー サポートメール] に、サポート用の管理者のメールアドレスを入力します。

  5. [対象ユーザー] で、組織内のユーザーにアクセスを制限する場合は [内部] を選択し、組織外のユーザーにアクセスを許可する場合は [外部] を選択します。

  6. [連絡先情報] に、OAuth クライアントで保護されているアプリの連絡先となる管理者メールアドレスを入力します。OAuth クライアントは後の手順で構成します。

  7. OAuth 構成を作成するには、[作成] をクリックします。

IAP でカスタム OAuth クライアントを構成する

このセクションでは、IAP でカスタム OAuth クライアントを設定する方法について説明します。

カスタム OAuth クライアントを作成する

このセクションでは、Google Cloud コンソールを使用してカスタム OAuth クライアントを作成する方法について説明します。カスタム IAP OAuth クライアントは、リソース階層の任意のレベルで設定できます。

Google Cloud コンソールを使用してリソースのカスタム OAuth クライアントを作成する手順は次のとおりです。

  1. Google Cloud コンソールで、[IAP] ページに移動します。

    IAP に移動

  2. [アプリケーション] タブのリソース リストで、構成するリソースを見つけます。

    プロジェクト レベルのリソースの場合は、次の操作を行います。

    • Google Cloud コンソールを使用して OAuth クライアントを作成します。

      1. [アクション] 列で、 [その他のオプション] > [設定] をクリックします。

      2. [設定] ダイアログで、[カスタム OAuth] を選択します。

      3. 同意画面を構成していない場合は、次の操作を行います。

        1. [同意画面を構成] をクリックします。

        2. このドキュメントの前半で説明した手順に沿って、ブランディング ページを構成します。

      4. IAP 設定ダイアログで、[Auto Generate Credentials] をクリックします。IAP は、このリソースで使用する新しい OAuth クライアントとシークレットを生成します。Google Auth Platform の [承認済みのリダイレクト URI] フィールドには、次の形式のエントリが含まれています。

        https://iap.googleapis.com/v1/oauth/clientIds/CLIENT_ID:handleRedirect
      5. クライアント ID とシークレットにアクセスするには、[認証情報をダウンロード] をクリックします。認証情報は JSON 形式のファイルに保存されます。このファイルにはリソースへのアクセスに必要な機密情報が含まれているため、ファイルが保護されているか削除されていることを確認してください。

      6. IAP OAuth 構成を保存して OAuth クライアントを IAP に適用するには、[保存] をクリックします。

カスタム OAuth クライアントを IAP に適用する

このセクションでは、OAuth クライアントを IAP に適用する方法について説明します。このメソッドは、プラットフォームにクライアントを直接適用する代わりに使用できます。

  1. カスタム OAuth クライアントを作成するには、このドキュメントで前述したGoogle Cloud コンソールの手順に沿って操作します。

  2. カスタム OAuth クライアントを適用します。

    gcloud

    gcloud CLI を使用してカスタム OAuth クライアントを適用する手順は次のとおりです。

    1. 設定 YAML ファイルを作成します。

      cat << EOF > iap-oauth.yaml
      accessSettings:
      oauthSettings:
        clientId: CLIENT_ID
        clientSecret: CLIENT_SECRET
      EOF
      

      次のように置き換えます。

      • CLIENT_ID: 前に生成した OAuth 認証情報のクライアント ID。
      • CLIENT_SECRET: 先ほど生成した OAuth 認証情報のクライアント シークレット。
    2. OAuth 構成を設定するには、次のいずれかを行います。

      • プロジェクト レベルで OAuth 構成を設定するには、次のコマンドを実行します。
      gcloud iap settings set iap-oauth.yaml

      リソース階層の別のレベルで構成を設定するには、--project フラグの代わりに次のいずれかのフラグを使用します。リソース階層のレベルでカスタム OAuth クライアントを設定すると、そのレベル内で実行されるすべてのサービスに同じカスタム ブランディングが提供されます。

      * <code>--folder=<var>FOLDER_ID</var></code>
      * <code>--organization=<var>ORGANIZATION_ID</var></code>
      
      • 特定のサービスに構成を設定するには、次のコマンドを実行します。
      gcloud iap settings set iap-oauth.yaml \
          --project=PROJECT_ID \
          --resource-type= RESOURCE_TYPE \
          --region=REGION \
          --service=SERVICE_NAME

      次のように置き換えます。

      • PROJECT_ID: プロジェクト リソースの ID。別のレベルで構成を設定するには、--project フラグの代わりに次のいずれかのフラグを使用します。

        • --folder=FOLDER_ID
        • --organization=ORGANIZATION_ID
      • RESOURCE_TYPE: リソースに応じて、次のいずれかのリソースタイプに置き換えます。

        • app-engine
        • backend-services
        • cloud-run
        • compute
        • folder
        • forwarding-rule
        • iap_web
        • organization
      • REGION: Cloud Run サービスを実行するリージョン。

      • SERVICE_NAME: サービスの名前。

    Terraform

    Terraform を使用してカスタム OAuth クライアントを適用する手順は次のとおりです。

    resource "google_iap_settings" "iap_settings" {
    name = IAP_RESOURCE_NAME
    access_settings {
      oauth_settings {
          oauth_client_id = CLIENT_ID
          oauth_client_secret = CLIENT_SECRET
      }
    }
    }
    

    次のように置き換えます。

    • IAP_RESOURCE_NAME: サービスの iap_settings リソースのリソース名。形式は projects/PROJECT_NUMBER/iap_web/REGION/services/SERVICE_NAME です。
    • CLIENT_ID: 先ほど生成した OAuth 認証情報のクライアント ID
    • CLIENT_SECRET: 先ほど生成した OAuth 認証情報のクライアント シークレット

    REST API

    1. 設定の JSON ファイルを作成します。

      cat << EOF > iap-oauth.json
      {
      "accessSettings": {
        "oauthSettings": {
          "clientId": "CLIENT_ID",
          "clientSecret": "CLIENT_SECRET"
        }
      }
      }
      EOF

      次のように置き換えます。

      • CLIENT_ID: 前に生成した OAuth 認証情報のクライアント ID。
      • CLIENT_SECRET: 先ほど生成した OAuth 認証情報のクライアント シークレット。
    2. 設定ファイルを適用します。

      curl -X PATCH \
      -H "Authorization: Bearer $(gcloud auth print-access-token)"
      -H "Content-Type: application/yaml" \
      "https://iap.googleapis.com/v1/projects/cb-managed-ingress-demo/iap_web/forwarding_rule-us-central1/services/psc-fr:iapSettings?update_mask=iapSettings.accessSettings.oauthSettings.oauthClientId,iapSettings.accessSettings.oauthSettings.oauthClientSecret" \
      -d @iap-oauth.json
      

OAuth クライアントを使用してウェブ アプリケーションが IAP で保護されていることをテストするには、アクセスをテストするをご覧ください。

リソースのカスタム OAuth クライアントの以前の構成

以降のセクションでは、特定のリソースタイプに対して IAP でカスタム OAuth クライアントを構成する以前の方法について説明します。このドキュメントで前述した方法を使用した場合は、このセクションをスキップできます。

App Engine

このセクションでは、App Engine でカスタム OAuth クライアントを有効にする方法について説明します。

gcloud

Before you set up your project and IAP, you need an up-to-date version of gcloud CLI. For instructions on how to install the gcloud CLI, see Install the gcloud CLI.

  1. To authenticate, use the Google Cloud CLI and run the following command.
    gcloud auth login
  2. To sign in, follow the URL that appears.
  3. After you sign in, copy the verification code that appears and paste it in the command line.
  4. Run the following command to specify the project that contains the resource that you want to protect with IAP.
    gcloud config set project PROJECT_ID
  5. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
  6. Save the OAuth client ID and secret.
  7. To enable IAP, run the following command.
    gcloud iap web enable \
        --oauth2-client-id=CLIENT_ID \
        --oauth2-client-secret=CLIENT_SECRET \
        --resource-type=app-engine

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

API

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Save the OAuth client ID and secret.

  3. Run the following command to prepare a settings.json file.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled": true,
        "oauth2ClientId": "CLIENT_ID",
        "oauth2ClientSecret":" CLIENT_SECRET"
      }
    }
    EOF
    

  4. Run the following command to enable IAP.

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d @settings.json \
    "https://appengine.googleapis.com/v1/apps/PROJECT_ID?updateMask=iap"
    

After you enable IAP, you can use the Google Cloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

GKE

このセクションでは、GKE でカスタム OAuth クライアントを有効にする方法について説明します。

Configure the BackendConfig

If you are running GKE clusters version 1.24 or later, you can configure IAP and GKE by using the Kubernetes Gateway API. See Configure IAP for instructions.

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Create a Kubernetes Secret to wrap the OAuth client.

    kubectl create secret generic MY_SECRET --from-literal=client_id=CLIENT_ID \
      --from-literal=client_secret=CLIENT_SECRET
    
    Replace the following:

    • MY_SECRET: The name of the secret to create
    • CLIENT_ID: The OAuth client ID
    • CLIENT_SECRET: The OAuth client secret

    You should receive confirmation, like the following output, that the Secret was successfully created:

    secret "MY_SECRET" created
    

  3. Add the OAuth credentials to the BackendConfig.

    apiVersion: cloud.google.com/v1
    kind: BackendConfig
    metadata:
      name: CONFIG_DEFAULT
      namespace: my-namespace
    spec:
    iap:
      enabled: true
      oauthclientCredentials:
        secretName: MY_SECRET
    

  4. Enable IAP by associating Service ports with your BackendConfig. See Associating BackendConfig with your Ingress. One way to make this association is to make all ports for the service default to your BackendConfig, which you can do by adding the following annotation to your Service resource:

    metadata:
      annotations:
          beta.cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}}'
    

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

Troubleshooting

If the secretName you referenced doesn't exist or isn't structured properly, one of the following error messages will display:

  • BackendConfig default/config-default is not valid: error retrieving secret "foo": secrets "foo" not found. To resolve this error, make sure that you've created the Kubernetes Secret correctly as described in step 2.

  • BackendConfig default/config-default is not valid: secret "foo" missing client_secret data. To resolve this error, make sure that you've created the OAuth credentials correctly. Also, make sure that you referenced the correct client_id and client_secret keys.

ロードバランサのバックエンド サービス

Compute Engine と Cloud Run のユーザー向けに、このセクションでは、ロードバランサ バックエンド サービスの IAP で OAuth クライアントを設定する方法について説明します。

gcloud

Before you set up your project and IAP, you need an up-to-date version of the gcloud CLI. For instructions on how to install the gcloud CLI, see Install the gcloud CLI.

  1. To authenticate, use the Google Cloud CLI and run the following command.
    gcloud auth login
  2. To sign in, follow the URL that appears.
  3. After you sign in, copy the verification code that appears and paste it in the command line.
  4. Run the following command to specify the project that contains the resource that you want to protect with IAP.
    gcloud config set project PROJECT_ID
  5. Follow the instructions in Creating OAuth clients for IAP. to configure the OAuth consent screen and create the OAuth client.
  6. Save the OAuth client ID and secret.
  7. To enable IAP, run either the globally or regionally scoped command.

    Global scope
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --global \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
    Regional scope
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --region REGION_NAME \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
        

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

API

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Save the OAuth client ID and secret.

  3. Run the following command to prepare a settings.json file.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled": true,
        "oauth2ClientId": "CLIENT_ID",
        "oauth2ClientSecret": "CLIENT_SECRET"
      }
    }
    EOF
    

  4. Run the following command to enable IAP.

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d @settings.json \
    "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/REGION/backendServices/BACKEND_SERVICE_NAME"
    

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

アクセスをテストする

カスタム OAuth クライアントを構成したら、次の操作を行って、IAP がそのクライアントを使用してサービスを保護していることをテストできます。

  1. IAP ページの [アプリケーション] タブで、IAP が管理するアプリケーションを表示します。

  2. いずれかのアプリケーションの URL にアクセスします。同意画面を構成してから初めてアプリケーションにアクセスする場合は、以前に構成した同意画面が表示されます。