Skip to content

Instantly share code, notes, and snippets.

@brandur

brandur/gpgup.sh Secret

Last active August 29, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandur/a68fb37c4059c281fa6b to your computer and use it in GitHub Desktop.
Save brandur/a68fb37c4059c281fa6b to your computer and use it in GitHub Desktop.
gpgup
#!/bin/sh
set -e
if [ -z "$GPGUP_TEMP" ]; then
echo 'gpgup: please set $GPGUP_TEMP'
exit 1
fi
GPGUP_ENCRYPTED_PATH=$GPGUP_PATH
# spin until we can find a suitable temporary file name
GPGUP_PATH=$GPGUP_TEMP/gpgup-$RANDOM
while [ -f "$GPGUP_PATH" ]; do
GPGUP_PATH=$GPGUP_TEMP/gpgup-$RANDOM
done
# make sure that the cleartext file has restricted access
touch $GPGUP_PATH
chmod 600 $GPGUP_PATH
# decrypt
gpg --batch -q -d $GPGUP_ENCRYPTED_PATH > $GPGUP_PATH
# don't fail on bad exits from here on out to maximize the chances that our
# "rm" of the decrypted path gets run even if everything else fails
set +e
eval "$@"
# optionally encrypt after exit (don't do this by default for safety/speed)
if [ "$GPGUP_WRITE" == "true" ]; then
>&2 echo "gpgup: writing $GPGUP_ENCRYPTED_PATH"
gpg --batch --yes -q --output $GPGUP_ENCRYPTED_PATH -e $GPGUP_PATH
fi
# clean up the temporary path
rm -f $GPGUP_PATH
@brandur
Copy link
Author

brandur commented Nov 19, 2014

Example usage:

__curl() { GPGUP_PATH=$HOME/.netrc.gpg gpgup 'curl --netrc-file $GPGUP_PATH' $@ }
alias curl=__curl

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