14
14
# GCE_PD_SA_NAME: Name of the service account to create
15
15
# GCE_PD_SA_DIR: Directory to save the service account key
16
16
# ENABLE_KMS: If true, it will enable Cloud KMS and configure IAM ACLs.
17
-
17
+ # CREATE_SA_KEY: (Optional) If true, creates a new service account key and
18
+ # exports it if creating a new service account
18
19
19
20
set -o nounset
20
21
set -o errexit
@@ -25,9 +26,15 @@ source "${PKGDIR}/deploy/common.sh"
25
26
26
27
ensure_var PROJECT
27
28
ensure_var GCE_PD_SA_NAME
28
- ensure_var GCE_PD_SA_DIR
29
29
ensure_var ENABLE_KMS
30
30
31
+ # Allow the user to pass CREATE_SA_KEY=false to skip the SA key creation
32
+ # Ensure the SA directory set, if we're creating the SA_KEY
33
+ CREATE_SA_KEY=" ${CREATE_SA_KEY:- true} "
34
+ if [ " ${CREATE_SA_KEY} " = true ]; then
35
+ ensure_var GCE_PD_SA_DIR
36
+ fi
37
+
31
38
# If the project id includes the org name in the format "org-name:project", the
32
39
# gCloud api will format the project part of the iam email domain as
33
40
# "project.org-name"
60
67
61
68
if [ " ${CREATE_SA} " = true ];
62
69
then
63
- # Delete Service Account Key
64
- if [ -f " ${GCE_PD_SA_DIR} /cloud-sa.json" ];
65
- then
66
- rm " ${GCE_PD_SA_DIR} /cloud-sa.json"
70
+ # Delete Service Account Key, if applicable
71
+ if [ " ${CREATE_SA_KEY} " = true ]; then
72
+ if [ -f " ${GCE_PD_SA_DIR} /cloud-sa.json" ];
73
+ then
74
+ rm " ${GCE_PD_SA_DIR} /cloud-sa.json"
75
+ fi
67
76
fi
77
+
68
78
# Delete ALL EXISTING Bindings
69
79
gcloud projects get-iam-policy " ${PROJECT} " --format json > " ${PKGDIR} /deploy/iam.json"
70
80
sed -i " /serviceAccount:${IAM_NAME} /d" " ${PKGDIR} /deploy/iam.json"
103
113
gcloud projects add-iam-policy-binding " ${PROJECT} " --member serviceAccount:" service-${PROJECT_NUMBER} @compute-system.iam.gserviceaccount.com" --role " roles/cloudkms.cryptoKeyEncrypterDecrypter"
104
114
fi
105
115
106
-
107
116
# Export key if needed
108
- if [ " ${CREATE_SA} " = true ];
117
+ if [ " ${CREATE_SA} " = true ] && [ " ${CREATE_SA_KEY} " = true ] ;
109
118
then
110
119
gcloud iam service-accounts keys create " ${GCE_PD_SA_DIR} /cloud-sa.json" --iam-account " ${IAM_NAME} " --project " ${PROJECT} "
111
120
fi
0 commit comments