gitenc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. # /home/your_username/.gitenc
  3. GITENC_CONF="$HOME/.gitenc"
  4. function toencrypt() {
  5. if [[ $1 != *.gpg ]] &&
  6. ([[ $1 == *connection* ]] ||
  7. [[ $1 == *.conf ]] ||
  8. [[ $1 == *.cnf ]] ||
  9. [[ $1 =~ .*sql* ]] ||
  10. [[ $1 == *.db ]] ||
  11. [[ $1 == *.bin ]] ||
  12. [[ $1 =~ .*config* ]]) &&
  13. [ "$(find "$1" -mmin -1440 -exec echo "true" \;)" == 'true' ];
  14. then
  15. # sensitive data filename match; encrypt (if modified within the past 24hrs)
  16. lockdown "$1"
  17. # add the encrypted file to git
  18. git add "$1".gpg
  19. # remove the original from git tracking
  20. git reset -- "$1"
  21. # append the original to .gitignore (if its not already there)
  22. if [ "$(grep "$1" .gitignore)" == "" ];
  23. then
  24. echo "$1" >> .gitignore
  25. git add .gitignore
  26. fi
  27. else
  28. # normal file, git away
  29. git add "$1"
  30. fi
  31. }
  32. function lockdown() {
  33. # if a previously encrypted file exists, remove it
  34. rm -f "$1".gpg
  35. # determine whether or not this system uses gpg or gpg2 package
  36. if [ -f /usr/bin/gpg ] || [ -f /bin/gpg ];
  37. then
  38. GPG_LOC='gpg'
  39. elif [ -f /usr/bin/gpg2 ] || [ -f /bin/gpg2 ];
  40. then
  41. GPG_LOC='gpg2'
  42. fi
  43. # to alter the cipher used, append your selection below
  44. "$GPG_LOC" --batch -c --passphrase-file "$GITENC_CONF" "$1"
  45. }
  46. # a gitenc add argument is passed, validate to see if encryption is needed
  47. if [ "$1" == "add" ];
  48. then
  49. # preserve original git add functionalities and stage everything by default
  50. shift
  51. git add "$@"
  52. for filename in $(git diff --cached --name-only);
  53. do
  54. # only encrypt files modified within the last 24 hours
  55. toencrypt "$filename"
  56. done
  57. ### setup ###
  58. elif [ "$1" == 'setup' ];
  59. then
  60. function pw_validate() {
  61. echo "Enter your preferred password to use for the GPG encryption:"
  62. read -rs PW_PREF
  63. if [ "$PW_PREF" == "" ];
  64. then
  65. echo "Password must not be blank, try again:"
  66. fi
  67. echo "Please re-enter the password, to confirm:"
  68. read -rs PW_CONFIRM
  69. if [ "$PW_PREF" != "$PW_CONFIRM" ];
  70. then
  71. echo "Passwords do not match. Try again."
  72. pw_validate
  73. fi
  74. }
  75. function createconfig() {
  76. echo "$1" > "$GITENC_CONF"
  77. echo -e "GPG password saved to $HOME/.gitenc!\nYou can now auto-encrypt your config by running:\n\tgitenc add filename\n\t\tor\n\tgitenc add .\n\n"
  78. echo -e "To use Gitenc without requiring an absolute path on every command, create a symlink:\n\tsudo ln -s $HOME/gitenc/gitenc /bin/gitenc"
  79. exit 0
  80. }
  81. pw_validate
  82. # if the directory for config doesn't already exist, create it
  83. if [ "$PW_PREF" != "" ] && [ ! -f "$GITENC_CONF" ];
  84. then
  85. touch "$GITENC_CONF"
  86. # if a .gitingore doesn't exist, it'll need to be added
  87. if [ ! -f .gitignore ];
  88. then
  89. touch .gitignore
  90. fi
  91. else
  92. echo "Unknown error occured, please submit a bug report: https://notabug.org/angela/gitenc"
  93. exit 1
  94. fi
  95. # assuming.. if we made it this far, there was a successful config creation
  96. createconfig "$PW_PREF"
  97. elif [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "help" ];
  98. then
  99. echo -e "\n\t\tGITENC USAGE\n\n"
  100. echo -e "gitenc add filename\nParses the individual added filename for common sensitive filenames (ie. widget.conf or connection.py)\n"
  101. echo -e "gitenc add . or -A or --all\nParses group filenames for common sensitive filenames (ie. widget.conf or connection.py)\n"
  102. echo -e "\nExample Cron Usage:\n00 02 * * * cd /home/user/public_html && gitenc add . && git commit -m "Adding new changes" && git push && cd"
  103. echo -e "\nDecrypt an Encrypted File to the CLI:\ngpg --decrypt filename.gpg"
  104. echo -e "\nDecrypt an Encrypted File and Make a Physical Copy:\ngpg --output filename.conf --decrypt filename.gpg\n \
  105. Note: Some systems run gpg as gpg2 - so if running gpg via the command-line interface, try gpg2"
  106. echo -e "\n\t\tUNINSTALL\n\n \
  107. - Remove/delete $HOME/gitenc directory\n \
  108. - Remove/delete $GITENC_CONF file\n \
  109. - If a symlink was set, remove /bin/gitenc\n"
  110. echo -e "\n\t\tABOUT\n\n"
  111. echo -e "Developed by Angela D\n \
  112. - https://github.com/angela-d\n \
  113. - https://notabug.org/angela\n
  114. Code improvements and additional suggestions by alfunx"
  115. else
  116. # gitenc is not a replacement for git
  117. echo -e "Command not recognized; you only need to run gitenc in place of 'git add'.. it serves no other purpose.\nRun 'git yourcommand', instead."
  118. fi