Skip to content

Add CREATE_SA_KEY flag to deploy/setup-project.sh #879

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions deploy/setup-project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# GCE_PD_SA_NAME: Name of the service account to create
# GCE_PD_SA_DIR: Directory to save the service account key
# ENABLE_KMS: If true, it will enable Cloud KMS and configure IAM ACLs.

# CREATE_SA_KEY: (Optional) If true, creates a new service account key and
# exports it if creating a new service account

set -o nounset
set -o errexit
Expand All @@ -25,9 +26,15 @@ source "${PKGDIR}/deploy/common.sh"

ensure_var PROJECT
ensure_var GCE_PD_SA_NAME
ensure_var GCE_PD_SA_DIR
ensure_var ENABLE_KMS

# Allow the user to pass CREATE_SA_KEY=false to skip the SA key creation
# Ensure the SA directory set, if we're creating the SA_KEY
CREATE_SA_KEY="${CREATE_SA_KEY:-true}"
if [ "${CREATE_SA_KEY}" = true ]; then
ensure_var GCE_PD_SA_DIR
fi

# If the project id includes the org name in the format "org-name:project", the
# gCloud api will format the project part of the iam email domain as
# "project.org-name"
Expand Down Expand Up @@ -60,11 +67,14 @@ fi

if [ "${CREATE_SA}" = true ];
then
# Delete Service Account Key
if [ -f "${GCE_PD_SA_DIR}/cloud-sa.json" ];
then
rm "${GCE_PD_SA_DIR}/cloud-sa.json"
# Delete Service Account Key, if applicable
if [ "${CREATE_SA_KEY}" = true ]; then
if [ -f "${GCE_PD_SA_DIR}/cloud-sa.json" ];
then
rm "${GCE_PD_SA_DIR}/cloud-sa.json"
fi
fi

# Delete ALL EXISTING Bindings
gcloud projects get-iam-policy "${PROJECT}" --format json > "${PKGDIR}/deploy/iam.json"
sed -i "/serviceAccount:${IAM_NAME}/d" "${PKGDIR}/deploy/iam.json"
Expand Down Expand Up @@ -103,9 +113,8 @@ then
gcloud projects add-iam-policy-binding "${PROJECT}" --member serviceAccount:"service-${PROJECT_NUMBER}@compute-system.iam.gserviceaccount.com" --role "roles/cloudkms.cryptoKeyEncrypterDecrypter"
fi


# Export key if needed
if [ "${CREATE_SA}" = true ];
if [ "${CREATE_SA}" = true ] && [ "${CREATE_SA_KEY}" = true ];
then
gcloud iam service-accounts keys create "${GCE_PD_SA_DIR}/cloud-sa.json" --iam-account "${IAM_NAME}" --project "${PROJECT}"
fi