1313# PROJECT: GCP project
1414# GCE_PD_SA_NAME: Name of the service account to create
1515# GCE_PD_SA_DIR: Directory to save the service account key
16- # ENABLE_KMS: If true, it will enable Cloud KMS and configure IAM ACLs.
16+ # ENABLE_KMS: Enable Cloud KMS and configure IAM ACLs.
17+ # ENABLE_KMS_ADMIN: Add service account permissions to destroy Cloud KMS keys.
1718# CREATE_SA_KEY: (Optional) If true, creates a new service account key and
1819# exports it if creating a new service account
1920
@@ -27,6 +28,7 @@ source "${PKGDIR}/deploy/common.sh"
2728ensure_var PROJECT
2829ensure_var GCE_PD_SA_NAME
2930ensure_var ENABLE_KMS
31+ ensure_var ENABLE_KMS_ADMIN
3032
3133# Allow the user to pass CREATE_SA_KEY=false to skip the SA key creation
3234# Ensure the SA directory set, if we're creating the SA_KEY
@@ -35,9 +37,8 @@ if [ "${CREATE_SA_KEY}" = true ]; then
3537 ensure_var GCE_PD_SA_DIR
3638fi
3739
38- # If the project id includes the org name in the format "org-name:project", the
39- # gCloud api will format the project part of the iam email domain as
40- # "project.org-name"
40+ # If the project ID includes the org name in the format "org-name:project",
41+ # gcloud will format the project in the IAM email domain as "project.org-name"
4142if [[ $PROJECT == * " :" * ]]; then
4243 IFS=' :' read -ra SPLIT <<< " $PROJECT"
4344 readonly IAM_PROJECT=" ${SPLIT[1]} .${SPLIT[0]} "
9091# Create or Update Custom Role
9192if gcloud iam roles describe gcp_compute_persistent_disk_csi_driver_custom_role --project " ${PROJECT} " ;
9293then
93- gcloud iam roles update gcp_compute_persistent_disk_csi_driver_custom_role --quiet \
94- --project " ${PROJECT} " \
95- --file " ${PKGDIR} /deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml"
94+ action=update
9695else
97- gcloud iam roles create gcp_compute_persistent_disk_csi_driver_custom_role --quiet \
98- --project " ${PROJECT} " \
99- --file " ${PKGDIR} /deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml"
96+ action=create
10097fi
98+ gcloud iam roles $action gcp_compute_persistent_disk_csi_driver_custom_role --quiet \
99+ --project " ${PROJECT} " \
100+ --file " ${PKGDIR} /deploy/gcp-compute-persistent-disk-csi-driver-custom-role.yaml"
101101
102102# Bind service account to roles
103103for role in ${BIND_ROLES}
@@ -113,6 +113,13 @@ then
113113 gcloud projects add-iam-policy-binding " ${PROJECT} " --member serviceAccount:" service-${PROJECT_NUMBER} @compute-system.iam.gserviceaccount.com" --role " roles/cloudkms.cryptoKeyEncrypterDecrypter"
114114fi
115115
116+ # Authorize SA to destroy Cloud KMS encryption keys.
117+ if [ " ${ENABLE_KMS_ADMIN} " = true ];
118+ then
119+ gcloud services enable cloudkms.googleapis.com --project=" ${PROJECT} "
120+ gcloud projects add-iam-policy-binding " ${PROJECT} " --member serviceAccount:" ${IAM_NAME} " --role " roles/cloudkms.admin"
121+ fi
122+
116123# Export key if needed
117124if [ " ${CREATE_SA} " = true ] && [ " ${CREATE_SA_KEY} " = true ];
118125then
0 commit comments