このページでは、Identity-Aware Proxy(IAP)を使用して Google Kubernetes Engine(GKE)インスタンスを保護する方法について説明します。
概要
IAP は GKE の Ingress によって統合されます。これにより、VPN ではなく、リソースレベルで従業員によるアクセスを制御できます。
GKE クラスタで、受信トラフィックは Cloud Load Balancing のコンポーネントである HTTP(S) 負荷分散によって処理されます。HTTP(S) ロードバランサは通常、Kubernetes Ingress コントローラによって構成されます。Ingress コントローラは、Kubernetes Ingress オブジェクトから構成情報を取得します。このオブジェクトは、1 つ以上の Service オブジェクトに関連付けられています。Service オブジェクトは、受信リクエストを特定の Pod とポートに転送するために使用するルーティング情報を保持します。
Kubernetes バージョン 1.10.5-gke.3 以降では、サービスと BackendConfig オブジェクトを関連付けることで、ロードバランサの構成を追加できます。BackendConfig は、kubernetes/ingress-gce リポジトリに定義されるカスタム リソース定義(CRD)です。
Kubernetes Ingress コントローラは、BackendConfig から構成情報を読み取り、それに従ってロードバランサを設定します。BackendConfig は、Cloud Load Balancing に固有の構成情報を保持しています。これにより、HTTP(S) バックエンド サービスごとに個別の構成を定義できます。
始める前に
GKE で IAP を有効にするには、次のものが必要です。
- 課金が有効になっている Google Cloud コンソール プロジェクト。
- HTTPS ロードバランサで処理される 1 つ以上の GKE インスタンスを含むグループ。GKE クラスタに Ingress オブジェクトを作成すると、ロードバランサが自動的に作成されます。
- HTTPS の Ingress の作成をご覧ください。
- ロードバランサのアドレスに登録されたドメイン名。
- すべてのリクエストに ID があることを確認するアプリコード。
- ユーザーの ID の取得をご覧ください。
IAP の有効化
If you haven't configured your project's OAuth consent screen, you'll be prompted to do so. To configure your OAuth consent screen, see Setting up your OAuth consent screen.
If you are running GKE clusters version 1.24 or later, you can configure
IAP and GKE by using the Kubernetes Gateway API. To do so, complete
the following steps and then follow the instructions in
Configure IAP.
Do not configure BackendConfig
.
Setting up IAP access
-
Go to the
Identity-Aware Proxy page.
Go to the Identity-Aware Proxy page - Select the project you want to secure with IAP.
-
Select the checkbox next to the resource you want to grant access to.
If you don't see a resource, ensure that the resource is created and that the BackendConfig Compute Engine ingress controller is synced.
To verify that the backend service is available, run the following gcloud command:
gcloud compute backend-services list
- On the right side panel, click Add principal.
-
In the Add principals dialog that appears, enter the email addresses of groups or
individuals who should have the IAP-secured Web App User role for the project.
The following kinds of principals can have this role:
- Google Account: user@gmail.com
- Google Group: admins@googlegroups.com
- Service account: server@example.gserviceaccount.com
- Google Workspace domain: example.com
Make sure to add a Google Account that you have access to.
- Select Cloud IAP > IAP-secured Web App User from the Roles drop-down list.
- Click Save.
Configuring BackendConfig
To configure BackendConfig for IAP, create a Kubernetes Secret and then
add an iap
block to the BackendConfig.
Creating a Kubernetes Secret
The BackendConfig uses a Kubernetes
Secret
to wrap the OAuth client you created earlier. Kubernetes Secrets are managed like
other Kubernetes objects by using the
kubectl
command-line interface (CLI). To create a Secret, run the following command where
client_id_key and client_secret_key are the keys from the JSON file you
downloaded when you created OAuth credentials:
kubectl create secret generic my-secret --from-literal=client_id=client_id_key \ --from-literal=client_secret=client_secret_key
The preceding command displays output to confirm when the Secret is successfully created:
secret "my-secret" created
Adding an iap
block to the BackendConfig
To configure the BackendConfig for IAP, you need to specify the
enabled
and secretName
values. To specify these values, ensure
that you have the compute.backendServices.update
permission and add the
iap
block to BackendConfig. In this block, my-secret is
the Kubernetes Secret name you created previously:
For GKE versions 1.16.8-gke.3 and higher, use the `cloud.google.com/v1` API version. If you are using an earlier GKE version, use `cloud.google.com/v1beta1`.
apiVersion: cloud.google.com/v1 kind: BackendConfig metadata: name: config-default namespace: my-namespace spec: iap: enabled: true oauthclientCredentials: secretName: my-secret
You also need to associate Service ports with your BackendConfig to trigger turning on IAP. 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"}'
To test the configuration, run kubectl get event
. If you see the message
"no BackendConfig for service port exists
", then you successfully
associated a service port with your BackendConfig, but the BackendConfig
resource wasn't found. This error can occur if you haven't created the BackendConfig resource,
created it in the wrong namespace, or misspelled the reference in the Service annotation.
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 the previous section. -
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 correctclient_id
andclient_secret
keys in the JSON you downloaded previously.
When the enabled
flag is set to true
and
the secretName
is correctly set, IAP is configured
for your selected resource.
Turning IAP off
To turn IAP off, you must set enabled
to
false
in the BackendConfig. If you delete the IAP
block from BackendConfig, the settings will persist. For example, if IAP is
enabled with secretName: my_secret
and you delete the block, then
IAP will still be turned on with the OAuth credentials stored in
my_secret
.