-
Notifications
You must be signed in to change notification settings - Fork 168
scope roles/iam.serviceAccountUser to node service accounts #2206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,9 @@ | |
| # ENABLE_KMS_ADMIN: Add service account permissions to destroy Cloud KMS keys. | ||
| # CREATE_SA_KEY: (Optional) If true, creates a new service account key and | ||
| # exports it if creating a new service account | ||
| # NODE_SERVICE_ACCOUNTS: (Optional) Comma-separated list of service accounts | ||
| # that the CSI driver should be allowed to impersonate. If not specified, | ||
| # defaults to project-level serviceAccountUser role. | ||
|
|
||
| set -o nounset | ||
| set -o errexit | ||
|
|
@@ -102,22 +105,36 @@ gcloud iam roles $action gcp_compute_persistent_disk_csi_driver_custom_role --qu | |
| # Bind service account to roles | ||
| for role in ${BIND_ROLES} | ||
| do | ||
| gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"${IAM_NAME}" --role "${role}" | ||
| gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"${IAM_NAME}" --role "${role}" --condition=None | ||
| done | ||
|
|
||
| # Grant scoped serviceAccountUser role for node service accounts | ||
| if use_scoped_sa_role; | ||
| then | ||
| IFS=',' read -ra NODE_SA_ARRAY <<< "${NODE_SERVICE_ACCOUNTS}" | ||
| for node_sa in "${NODE_SA_ARRAY[@]}"; | ||
| do | ||
| node_sa=$(echo "${node_sa}" | xargs) # trim whitespace | ||
| echo "Granting ${SA_USER_ROLE} for ${node_sa} to serviceAccount:${IAM_NAME}" | ||
| gcloud iam service-accounts add-iam-policy-binding "${node_sa}" \ | ||
| --member="serviceAccount:${IAM_NAME}" --condition=None \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need condition=None? What is the default of condition?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added The list of conditions might be specific to our project(?) but IMO we always want None for these bindings. I did not see that prompt when running |
||
| --role="${SA_USER_ROLE}" --project="${PROJECT}" | ||
| done | ||
| fi | ||
|
|
||
| # Authorize GCE to encrypt/decrypt using Cloud KMS encryption keys. | ||
| # https://cloud.google.com/compute/docs/disks/customer-managed-encryption#before_you_begin | ||
| if [ "${ENABLE_KMS}" = true ]; | ||
| then | ||
| gcloud services enable cloudkms.googleapis.com --project="${PROJECT}" | ||
| gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"service-${PROJECT_NUMBER}@compute-system.iam.gserviceaccount.com" --role "roles/cloudkms.cryptoKeyEncrypterDecrypter" | ||
| gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"service-${PROJECT_NUMBER}@compute-system.iam.gserviceaccount.com" --role "roles/cloudkms.cryptoKeyEncrypterDecrypter" --condition=None | ||
| fi | ||
|
|
||
| # Authorize SA to destroy Cloud KMS encryption keys. | ||
| if [ "${ENABLE_KMS_ADMIN}" = true ]; | ||
| then | ||
| gcloud services enable cloudkms.googleapis.com --project="${PROJECT}" | ||
| gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"${IAM_NAME}" --role "roles/cloudkms.admin" | ||
| gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"${IAM_NAME}" --role "roles/cloudkms.admin" --condition=None | ||
| fi | ||
|
|
||
| # Export key if needed | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do some input validation for NODE_SERVICE_ACCOUNTS? Like not empty etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
if use_scoped_sa_rolecheck already returns false at line 73 ifNODE_SERVICE_ACCOUNTSis an empty string:for example:
I considered checking for a comma in
NODE_SERVICE_ACCOUNTS, but it is valid to pass in a single service account with no comma delimiter, and this line still handles that case:The only other thing I can think to validate is whether it's a valid service account, which we won't know until calling the
gcloudcommand, which gives a useful error message:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is good enough with use_scoped_sa_role :)