Skip to content

Instantly share code, notes, and snippets.

@marcinantkiewicz
Created November 10, 2023 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcinantkiewicz/d890c9a24ac2c0f68162c901562511d0 to your computer and use it in GitHub Desktop.
Save marcinantkiewicz/d890c9a24ac2c0f68162c901562511d0 to your computer and use it in GitHub Desktop.
read cloudbuild manifest, find secrets, pull them from Secrets Manager, and display in a way where they can be saved in env.
#! /usr/bin/env sh
set -o pipefail
FILEPATH=$1; shift;
function pull_secrets {
MANIFEST=$1; shift;
SECRETS=$(cat "${MANIFEST}" | jq -r '.availableSecrets.secretManager[] | .env + "=" + .versionName');
PROJECT_ID=$(gcloud projects list --filter $(gcloud config get project) --format="value(PROJECT_NUMBER)")
for SECRET in ${SECRETS[@]}; do
SECRET_ENV=$(echo "$SECRET" | cut -d '=' -f 1);
SECRET_PATH=$(echo "$SECRET" | cut -d '=' -f 2);
SECRET_NAME=$(echo "$SECRET_PATH" | cut -d '/' -f 4);
echo "export ${SECRET_ENV}=\"$(gcloud secrets versions access latest --secret=$SECRET_NAME --project=$PROJECT_ID)\"";
done
}
set -e
test -r "$FILEPATH" -a -f "$FILEPATH" || \
(>&2 echo "Error: file \"$FILEPATH\" not found or unreadable"; exit 255);
pull_secrets "$FILEPATH";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment