Kubernetes workloads should not use cluster node service accounts to authenticate to Google Cloud APIs. Each Kubernetes Workload that needs to authenticate to other Google services using Cloud IAM should be provisioned a dedicated Service account. Enabling Workload Identity manages the distribution and rotation of Service account keys for the workloads to use.
To remediate the misconfiguration of using Dedicated GCP Service Accounts and Workload Identity for Clusters in GCP, follow these steps:
Open the GCP Console and navigate to the Kubernetes Engine.
Select the cluster for which you want to remediate the misconfiguration.
Click on the “Edit” button at the top of the page.
Under the “Security” section, select “Workload Identity.”
Select the checkbox “Enable Workload Identity.”
In the “Service Account” field, enter the name of the dedicated service account that you want to use for the cluster.
Click on the “Save” button to apply the changes.
Once the changes are applied, verify that the dedicated service account is being used for the cluster by running the following command in the Cloud Shell:
Copy
Ask AI
kubectl get pods --namespace kube-system -o=jsonpath='{.items[*].spec.serviceAccountName}'
This command will return the name of the service account being used by the pods in the kube-system namespace. Verify that it matches the dedicated service account that you specified in step 6.By following these steps, you have successfully remediated the misconfiguration of using Dedicated GCP Service Accounts and Workload Identity for Clusters in GCP.
To remediate the misconfiguration “Use Dedicated GCP Service Accounts And Workload Identity For Clusters” for GCP using GCP CLI, you can follow the below steps:
Create a dedicated service account for your cluster:
Copy
Ask AI
gcloud iam service-accounts create [SA-NAME] --display-name [SA-DISPLAY-NAME]
Replace [SA-NAME] with the name of the service account you want to create and [SA-DISPLAY-NAME] with the display name of the service account.
Grant the necessary permissions to the service account:
Replace [PROJECT-ID] with the ID of the project where the cluster is located, [SA-NAME] with the name of the service account you created in step 1, and [ROLE] with the necessary role to access the resources required by the cluster.
Replace [CLUSTER-NAME] with the name of the cluster and [PROJECT-ID] with the ID of the project where the cluster is located.
Associate the service account with the cluster:
Copy
Ask AI
gcloud iam service-accounts add-iam-policy-binding [SA-NAME]@[PROJECT-ID].iam.gserviceaccount.com --member="serviceAccount:[PROJECT-ID].svc.id.goog[NAMESPACE]/[SA-NAME]" --role="roles/iam.workloadIdentityUser"
Replace [SA-NAME] with the name of the service account you created in step 1, [PROJECT-ID] with the ID of the project where the cluster is located, and [NAMESPACE] with the namespace of the cluster.By following these steps, you will have remediated the misconfiguration “Use Dedicated GCP Service Accounts And Workload Identity For Clusters” for GCP using GCP CLI.
Using Python
To remediate the misconfiguration “Use Dedicated GCP Service Accounts And Workload Identity For Clusters” in GCP using Python, follow the below steps:
Create a dedicated GCP service account for the cluster. You can use the below Python code to create a service account:
Copy
Ask AI
from google.oauth2 import service_accountcredentials = service_account.Credentials.from_service_account_file( '/path/to/service_account_key.json')
Assign the required IAM roles to the service account based on the cluster’s requirements. You can use the below Python code to grant IAM roles to the service account:
Copy
Ask AI
from google.cloud import iamclient = iam.IAMClient(credentials=credentials)policy = client.get_policy(request={"resource": "projects/{project_id}"})bindings = policy.bindingsfor binding in bindings: if binding.role == "roles/editor": binding.members.append("serviceAccount:{service_account_email}") breakpolicy.bindings = bindingsclient.set_iam_policy(request={"resource": "projects/{project_id}", "policy": policy})
Enable Workload Identity for the cluster. You can use the below Python code to enable Workload Identity:
By following these steps, you can remediate the misconfiguration “Use Dedicated GCP Service Accounts And Workload Identity For Clusters” in GCP using Python.