Skip to content

Instantly share code, notes, and snippets.

@snorremd
Last active February 25, 2019 15:18
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 snorremd/858c59ffcd65d0ec93c979e575a1bf6f to your computer and use it in GitHub Desktop.
Save snorremd/858c59ffcd65d0ec93c979e575a1bf6f to your computer and use it in GitHub Desktop.
Script to grab credentials from gpg encrypted file and run clj with them
#!/usr/bin/env bash
# cljgp is a script for decrypting a credentials file with environment
# variables and set them on the clj process. Combine this with the
# ~/.m2/settings.xml file and tell maven to use your environment vars:
# https://maven.apache.org/settings.html.
#
# Credentials file should contain:
# MY_USER=someuser
# MY_PASS=somepass
#
# Settings.xml file should then contain:
# <username>${env.MY_USER}</username>
# <password>${env.MY_PASS}</password>
ENCRYPTED_CREDENTIALS=${HOME}/.clojure/credentials.gpg
neededutils=( gpg clj rlwrap )
for UTIL in ${neededutils[@]} ; do
if [ ! -x "`which $UTIL`" ] ; then
echo "$0 Failure: Could not find $UTIL in \$PATH"
exit 2
fi
done
# Check if credential file exists
if [ ! -e ${ENCRYPTED_CREDENTIALS} ]; then
echo "Missing credential file at ${ENCRYPTED_CREDENTIALS}"
exit 1
fi
# Decrypt credentials file and source it
CREDENTIALS=$(gpg -d ${ENCRYPTED_CREDENTIALS} 2> /dev/null)
if [ $? -ne 0 ]; then
echo "Could not decrypt credential file at ${ENCRYPTED_CREDENTIALS}"
exit 1
fi
echo "Running clj with environment variables from ${ENCRYPTED_CREDENTIALS}"
env $(echo $CREDENTIALS | grep -v "^#" | xargs) clj $@
@snorremd
Copy link
Author

snorremd commented Feb 25, 2019

Edit: Add this as a shell script in your ~/bin/ folder under some name like cljpg and make it executable.
Add an alias from clj to cljpg if you want to shadow the original clj command.

Original comment: Put this in your ~/.bash_aliases file, configure the ~/.m2/settings.xml file, and you are good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment