KMS Admin Roles Should Not Have CryptoKey Role
More Info:
Ensure that no users have the KMS admin role and any one of the CryptoKey roles follows separation of duties, where no user have access to resources out of the scope of duty.
Risk Level
Critical
Address
Security
Compliance Standards
- CIS GCP
- CIS GCP 2.0.0
- Cloudanix Best Practice
- ISO 27001
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the misconfiguration "KMS Admin Roles Should Not Have CryptoKey Role" in GCP using GCP console, you can follow the below steps:
- Login to your GCP console and navigate to the IAM & Admin page.
- In the IAM & Admin page, select the "Roles" tab.
- Search for the "Cloud KMS Admin" role and click on it.
- Under the "Permissions" tab, search for the "cloudkms.cryptoKeyRoles.*" permission.
- Click on the pencil icon next to the "cloudkms.cryptoKeyRoles.*" permission to edit it.
- Uncheck the "cloudkms.cryptoKeyRoles.*" permission and click on the "Save" button.
- Verify that the "cloudkms.cryptoKeyRoles.*" permission is no longer present under the "Permissions" tab for the "Cloud KMS Admin" role.
By following the above steps, you have successfully remediated the misconfiguration "KMS Admin Roles Should Not Have CryptoKey Role" in GCP using GCP console.
Using CLI
To remediate this issue in GCP using GCP CLI, you can follow the below steps:
-
Open the Google Cloud Console and go to the Cloud Shell.
-
Run the following command to list all the KMS admin roles in your project:
gcloud kms roles list -
Identify the KMS admin role that has the
roles/cloudkms.cryptoKeyEncrypterDecrypterrole. -
Run the following command to remove the
roles/cloudkms.cryptoKeyEncrypterDecrypterrole from the KMS admin role:gcloud kms roles revoke [KMS_ADMIN_ROLE] --permission=cloudkms.cryptoKeyEncrypterDecrypterReplace
[KMS_ADMIN_ROLE]with the name of the KMS admin role that you identified in step 3. -
Verify that the
roles/cloudkms.cryptoKeyEncrypterDecrypterrole has been removed from the KMS admin role by running the following command:gcloud kms roles describe [KMS_ADMIN_ROLE]Replace
[KMS_ADMIN_ROLE]with the name of the KMS admin role that you identified in step 3.This command should output the details of the KMS admin role, which should not include the
roles/cloudkms.cryptoKeyEncrypterDecrypterrole.
By following these steps, you should be able to remediate the issue of KMS admin roles having the roles/cloudkms.cryptoKeyEncrypterDecrypter role in GCP using GCP CLI.
Using Python
To remediate the misconfiguration "KMS Admin Roles Should Not Have CryptoKey Role" in GCP using Python, you can follow the below steps:
Step 1: Create a list of all the KMS admin roles that have CryptoKey role.
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Set the credentials
credentials = service_account.Credentials.from_service_account_file(
'path/to/service_account.json')
# Set the project id
project_id = 'your_project_id'
# Create the KMS client
kms_client = build('cloudkms', 'v1', credentials=credentials)
# Get the list of all the KMS admin roles
roles_list = kms_client.projects().locations().keyRings().cryptoKeys().getIamPolicy(resource='projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(project_id, location, keyring_name, cryptokey_name)).execute()
# Create a list of all the KMS admin roles that have CryptoKey role
kms_admin_cryptokey_roles = []
for role in roles_list['bindings']:
if 'roles/cloudkms.admin' in role['role']:
for member in role['members']:
if 'cryptoKey' in member:
kms_admin_cryptokey_roles.append(role['role'])
Step 2: Remove the CryptoKey role from all the KMS admin roles.
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Set the credentials
credentials = service_account.Credentials.from_service_account_file(
'path/to/service_account.json')
# Set the project id
project_id = 'your_project_id'
# Create the KMS client
kms_client = build('cloudkms', 'v1', credentials=credentials)
# Remove the CryptoKey role from all the KMS admin roles
for role in kms_admin_cryptokey_roles:
policy = kms_client.projects().locations().keyRings().cryptoKeys().getIamPolicy(resource='projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(project_id, location, keyring_name, cryptokey_name)).execute()
for binding in policy['bindings']:
if binding['role'] == role:
binding['members'] = [member for member in binding['members'] if 'cryptoKey' not in member]
kms_client.projects().locations().keyRings().cryptoKeys().setIamPolicy(resource='projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}'.format(project_id, location, keyring_name, cryptokey_name), body={'policy': policy}).execute()
Note: Replace the path/to/service_account.json, your_project_id, location, keyring_name, and cryptokey_name with the actual values in your GCP environment.
Using Terraform
resource "google_kms_crypto_key_iam_binding" "cryptokey_encrypter_decrypter" {
crypto_key_id = GOOGLE_KMS_CRYPTO_KEY_ID # e.g. "projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME"
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
# List only the principals that SHOULD keep this CryptoKey role.
# Ensure that any principal with "roles/cloudkms.admin" is NOT listed here.
members = [
"user:ALLOWED_USER@example.com",
"serviceAccount:ALLOWED_SA@PROJECT_ID.iam.gserviceaccount.com",
]
}
resource "google_project_iam_binding" "kms_admins" {
project = "PROJECT_ID"
role = "roles/cloudkms.admin"
# KMS admins defined here must not also be in any CryptoKey *_iam_* for the same keys.
members = [
"user:KMS_ADMIN_USER@example.com",
"group:kms-admins@example.com",
]
}
Substitute:
GOOGLE_KMS_CRYPTO_KEY_IDwith the full resource ID of the CryptoKey.PROJECT_IDwith your GCP project ID.- Adjust the
memberslists so that no identity appears in bothgoogle_project_iam_binding.kms_adminsand anygoogle_kms_crypto_key_iam_*resource (binding/member/policy) for the same keys.
This cannot be remediated on a “gcp-securityandidentity-iam-user” resource directly because IAM in Terraform is managed on the project/organization, key ring, or crypto key resources, not on the user itself; you must update the IAM bindings as shown.
Applying this change does not delete any KMS resources but will revoke the CryptoKey role from any removed principals. terraform plan should show updates (~) to the affected google_kms_crypto_key_iam_* and/or google_project_iam_* resources where members are being added/removed, with no new resources created or destroyed.