|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +## |
| 4 | +# @author Jay Taylor [@jtaylor] |
| 5 | +# |
| 6 | +# @date 2012-04-09 |
| 7 | +# |
| 8 | +# @description Initializes openssl encryption filter into the .git/config file |
| 9 | +# of a cloned git repository. |
| 10 | +# |
| 11 | + |
| 12 | + |
| 13 | +localGitConfigFile='.git/config' |
| 14 | + |
| 15 | + |
| 16 | +################################################################################ |
| 17 | + |
| 18 | +# Ensure that we are running in the root of a git repository. |
| 19 | +if ! [ -r "$localGitConfigFile" ]; then |
| 20 | + echo 'fatal: this script can only be run in the root of a git repository' 1>&2 |
| 21 | + echo 'check your current directory (by running `pwd`), correct any issues you find, and then try again' 1>&2 |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | + |
| 26 | +# Define filter scripts and other static executable/reference file contents. |
| 27 | +# NB: The semi-colons at the end of each line for the first 3 entries here are |
| 28 | +# due to the use of `eval` below. |
| 29 | +clean_filter_openssl='#!/usr/bin/env bash; |
| 30 | +; |
| 31 | +SALT_FIXED={{SALT}}; |
| 32 | +#A1F1F8129C4FEBAB3513C174 # 24 or less hex characters; |
| 33 | +PASS_FIXED={{PASSWORD}}; |
| 34 | +; |
| 35 | +openssl enc -base64 -aes-256-ecb -S $SALT_FIXED -k $PASS_FIXED' |
| 36 | + |
| 37 | +smudge_filter_openssl='#!/usr/bin/env bash; |
| 38 | +; |
| 39 | +# No salt is needed for decryption.; |
| 40 | +PASS_FIXED={{PASSWORD}}; |
| 41 | +; |
| 42 | +# If decryption fails, use `cat` instead.; |
| 43 | +# Error messages are redirected to /dev/null.; |
| 44 | +openssl enc -d -base64 -aes-256-ecb -k $PASS_FIXED 2> /dev/null || cat' |
| 45 | + |
| 46 | +diff_filter_openssl='#!/usr/bin/env bash; |
| 47 | +; |
| 48 | +# No salt is needed for decryption.; |
| 49 | +PASS_FIXED={{PASSWORD}}; |
| 50 | +; |
| 51 | +# Error messages are redirected to /dev/null.; |
| 52 | +openssl enc -d -base64 -aes-256-ecb -k $PASS_FIXED -in "$1" 2> /dev/null || cat "$1"' |
| 53 | + |
| 54 | +gitattributes='*.md filter=openssl diff=openssl |
| 55 | +sensitive.txt filter=openssl diff=openssl |
| 56 | +[merge] |
| 57 | + renormalize = true' |
| 58 | + |
| 59 | +gitconfig='[filter "openssl"] |
| 60 | + smudge = ~/.gitencrypt/smudge_filter_openssl |
| 61 | + clean = ~/.gitencrypt/clean_filter_openssl |
| 62 | +[diff "openssl"] |
| 63 | + textconv = ~/.gitencrypt/diff_filter_openssl' |
| 64 | + |
| 65 | + |
| 66 | +# Initialize .gitencrypt directory in the users $HOME if not already there. |
| 67 | + |
| 68 | +if ! [ -d "$HOME/.gitencrypt" ]; then |
| 69 | + echo 'info: initializing ~/.gitencrypt' |
| 70 | + |
| 71 | + # Prompt user for salt and password. |
| 72 | + while [ -z "$salt" ]; do |
| 73 | + echo 'Enter the salt phrase (16 hexadecimal characters):' |
| 74 | + read salt |
| 75 | + done |
| 76 | + |
| 77 | + while [ -z "$password" ]; do |
| 78 | + echo 'Enter the encryption pass-phrase:' |
| 79 | + read password |
| 80 | + done |
| 81 | + |
| 82 | + mkdir "$HOME/.gitencrypt" |
| 83 | + |
| 84 | + for filter in clean_filter_openssl smudge_filter_openssl diff_filter_openssl; do |
| 85 | + echo "info: generating filter script '$filter'" |
| 86 | + filterScriptPath="$HOME/.gitencrypt/$filter" |
| 87 | + |
| 88 | + # This ugliness is due to `eval` not handling newlines very nicely. |
| 89 | + # @see http://stackoverflow.com/a/3524860/293064 for more eval details. |
| 90 | + echo -e $(eval "echo \$$filter") | tr ';' '\n' | sed "s/{{SALT}}/$salt/g |
| 91 | + s/{{PASSWORD}}/$password/g |
| 92 | + s/^ *\(.*\) *$/\1/g" > "$filterScriptPath" |
| 93 | + |
| 94 | + chmod a+x "$filterScriptPath" |
| 95 | + done |
| 96 | +fi |
| 97 | + |
| 98 | + |
| 99 | +# Initialize .gitattributes file if it doesn't exist. |
| 100 | + |
| 101 | +if ! [ -e '.gitattributes' ]; then |
| 102 | + echo "info: initializing file '.gitattributes'" |
| 103 | + echo -n $gitattributes > .gitattributes |
| 104 | +fi |
| 105 | + |
| 106 | + |
| 107 | +# Initialize the .git/conf file for this repository clone if not already. |
| 108 | + |
| 109 | +checkForPreExistingConf=$(grep '^\[\(filter\|diff\) "openssl"]$' "$localGitConfigFile") |
| 110 | + |
| 111 | +if [ -n "$checkForPreExistingConf" ]; then |
| 112 | + echo 'info: openssl filter/diff already configured for this clone' |
| 113 | +else |
| 114 | + cat <<EOF >> "$localGitConfigFile" |
| 115 | +$gitconfig |
| 116 | +EOF |
| 117 | + echo 'info: openssl filter/diff successfuly applied to this clone' |
| 118 | +fi |
| 119 | + |
| 120 | + |
| 121 | +# Reset the HEAD to re-check out all of the files [with the encryption filters.] |
| 122 | + |
| 123 | +echo 'info: re-checking out all of the files to ensure that the encryption filters are applied' |
| 124 | +git reset --hard HEAD |
| 125 | + |
0 commit comments