Skip to content

Instantly share code, notes, and snippets.

@TomK
Last active November 24, 2016 13:26
Show Gist options
  • Save TomK/f624e47fe3d8e98e79ee7dabd5588f94 to your computer and use it in GitHub Desktop.
Save TomK/f624e47fe3d8e98e79ee7dabd5588f94 to your computer and use it in GitHub Desktop.
update an ssl certificate on google cloud (using a/b suffixes)
#!/bin/bash
set -e
CERT_PREFIX="$1"
CERT_PATH="$2"
CERT_KEY="$3"
if [[ $CERT_PREFIX == "" || $CERT_PATH == "" || $CERT_KEY == "" ]]; then
echo "Usage: update-ssl.sh <cert prefix> <cert path> <cert key>"
exit 1
fi
# detect existing certificate name
OLD_CERT=$( gcloud compute target-https-proxies list | grep "web-target-proxy-$CERT_PREFIX" | awk '{print $2}' )
OLD_SUFFIX=$( echo -n "$OLD_CERT" | tail -c 1 )
if [ "$OLD_SUFFIX" == "a" ]; then
NEW_SUFFIX="b"
else
NEW_SUFFIX="a"
fi
# create new certificate
gcloud compute ssl-certificates create "$CERT_PREFIX-$NEW_SUFFIX" --certificate "$CERT_PATH" --private-key "$CERT_KEY"
# update https proxy
gcloud compute target-https-proxies update "web-target-proxy-$CERT_PREFIX" --ssl-certificate "$CERT_PREFIX-$NEW_SUFFIX"
# delete old certificate
gcloud compute ssl-certificates delete "$OLD_CERT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment