From 772070519951dac6f8a95fb77b3624a0dee4382d Mon Sep 17 00:00:00 2001 From: James Fraser Date: Fri, 15 Apr 2016 16:06:40 +1000 Subject: [PATCH 1/5] added first pass to fork --- plugins/history-sync/README.md | 23 ++++ plugins/history-sync/history-sync.plugin.zsh | 105 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 plugins/history-sync/README.md create mode 100644 plugins/history-sync/history-sync.plugin.zsh diff --git a/plugins/history-sync/README.md b/plugins/history-sync/README.md new file mode 100644 index 000000000..d79a4dfbf --- /dev/null +++ b/plugins/history-sync/README.md @@ -0,0 +1,23 @@ +## An oh-my-zsh plugin for gpg encrypted, internet synchronized zsh history using git. +
+That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the internet, this plugin will help you. You could even automate it using something like cron for a daily or even hourly sync. + +### How do I use it? + +1. Create a git repo for housing your encrypted zsh_history file (local or remote) + - Mine is $HOME/.zsh_history_proj +2. Activate history-sync plugin in your .zshrc + - git clone git@github.com:wulfgarpro/history-sync.git $HOME/.oh-my-zsh/plugins/. +3. Export environment variables (or use defaults found in history-sync.plugin.zsh) +
These are: + - ZSH_HISTORY_FILE + - ZSH_HISTORY_PROJ + - ZSH_HISTORY_FILE_ENC + - GIT_COMMIT_MSG +4. Ensure your gpg/pgp setup is complete and you have a public/private key pair +
i.e.: + - gpg --list-keys +5. Run zhpl alias to pull +6. Run zhps alias to push +7. Run zhsync to pull/push + diff --git a/plugins/history-sync/history-sync.plugin.zsh b/plugins/history-sync/history-sync.plugin.zsh new file mode 100644 index 000000000..1a1d103d7 --- /dev/null +++ b/plugins/history-sync/history-sync.plugin.zsh @@ -0,0 +1,105 @@ +### +# James Fraser +# +### + +autoload -U colors +colors + +ZSH_HISTORY_FILE=$HOME/.zsh_history +ZSH_HISTORY_PROJ=$HOME/.zsh_history_proj +ZSH_HISTORY_FILE_ENC=$ZSH_HISTORY_PROJ/zsh_history +GIT_COMMIT_MSG="latest $(date)" + +function print_git_error_msg() { + echo "$bold_color$fg[red]Fix your git repo...${reset_color}"; +} + +function print_gpg_encrypt_error_msg() { + echo "$bold_color$fg[red]GPG failed to encrypt history file... exiting.${reset_color}"; +} + +function print_gpg_decrypt_error_msg() { + echo "$bold_color$fg[red]GPG failed to decrypt history file... exiting.${reset_color}"; +} + +# Pull current master and merge with .zsh_history +function history_sync_pull() { + cp -a $HOME/{.zsh_history,.zsh_history.backup} + DIR=$CWD + cd $ZSH_HISTORY_PROJ && git pull + if [[ $? != 0 ]]; then + print_git_error_msg + cd $DIR + return + fi + + # Decrypt + gpg --output zsh_history_decrypted --decrypt zsh_history + if [[ $? != 0 ]]; then + print_gpg_decrypt_error_msg + cd $DIR + return + fi + + # Merge using sort unique + cat $HOME/.zsh_history zsh_history_decrypted | sort -u > $HOME/.zsh_history + rm zsh_history_decrypted + cd $DIR +} + +# Push current history to master +function history_sync_push() { + echo -n "Please enter GPG recipient name: " + read name + +# Encrypt + if [[ -n $name ]]; then + gpg -v -r $NAME --encrypt --sign --armor --output $ZSH_HISTORY_FILE_ENC $ZSH_HISTORY_FILE + if [[ $? != 0 ]]; then + print_gpg_encrypt_error_msg + return + fi + + echo -n "$bold_color$fg[yellow]Do you want to commit current local history file? ${reset_color}" + read commit + if [[ -n $commit ]]; then + case $commit in + [Yy]* ) + DIR=$CWD + cd $ZSH_HISTORY_PROJ && git add * && git commit -am $GIT_COMMIT_MSG + echo -n "$bold_color$fg[yellow]Do you want to push to remote? ${reset_color}" + read push + if [[ -n $push ]]; then + case $push in + [Yy]* ) + git push + if [[ $? != 0 ]]; then + print_git_error_msg + cd $DIR + return + fi + cd $DIR + ;; + esac + fi + + if [[ $? != 0 ]]; then + print_git_error_msg + cd $DIR + return + fi + ;; + [Nn]* ) + ;; + * ) + ;; + esac + fi + fi +} + +alias zhpl=history_sync_pull +alias zhps=history_sync_push +alias zhsync="history_sync_pull && history_sync_push" + From b98c7f341ef177479a970e5f5865b5ca0118ea50 Mon Sep 17 00:00:00 2001 From: James Fraser Date: Tue, 19 Apr 2016 20:02:09 +1000 Subject: [PATCH 2/5] added functionality to support multi-recipient pgp encryption --- plugins/history-sync/README.md | 4 +- plugins/history-sync/history-sync.plugin.zsh | 62 +++++++++++++++----- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/plugins/history-sync/README.md b/plugins/history-sync/README.md index d79a4dfbf..f6a5b1855 100644 --- a/plugins/history-sync/README.md +++ b/plugins/history-sync/README.md @@ -1,6 +1,6 @@ -## An oh-my-zsh plugin for gpg encrypted, internet synchronized zsh history using git. +## An oh-my-zsh plugin for gpg encrypted, internet synchronized zsh history using git
-That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the internet, this plugin will help you. You could even automate it using something like cron for a daily or even hourly sync. +That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the Internet, this plugin will help you. You could even automate it using something like cron for a daily or even hourly sync. ### How do I use it? diff --git a/plugins/history-sync/history-sync.plugin.zsh b/plugins/history-sync/history-sync.plugin.zsh index 1a1d103d7..005348914 100644 --- a/plugins/history-sync/history-sync.plugin.zsh +++ b/plugins/history-sync/history-sync.plugin.zsh @@ -12,21 +12,27 @@ ZSH_HISTORY_FILE_ENC=$ZSH_HISTORY_PROJ/zsh_history GIT_COMMIT_MSG="latest $(date)" function print_git_error_msg() { - echo "$bold_color$fg[red]Fix your git repo...${reset_color}"; +echo "$bold_color$fg[red]Fix your git repo...${reset_color}"; } function print_gpg_encrypt_error_msg() { - echo "$bold_color$fg[red]GPG failed to encrypt history file... exiting.${reset_color}"; +echo "$bold_color$fg[red]GPG failed to encrypt history file... exiting.${reset_color}"; } function print_gpg_decrypt_error_msg() { - echo "$bold_color$fg[red]GPG failed to decrypt history file... exiting.${reset_color}"; +echo "$bold_color$fg[red]GPG failed to decrypt history file... exiting.${reset_color}"; } -# Pull current master and merge with .zsh_history +function usage() { +echo "$bold_color$fg[red]Usage: $0 [-r -r ...]${reset_color}" 1>&2; return; +} + +# Pull current master, decrypt, and merge with .zsh_history function history_sync_pull() { + # Backup cp -a $HOME/{.zsh_history,.zsh_history.backup} DIR=$CWD + # Pull cd $ZSH_HISTORY_PROJ && git pull if [[ $? != 0 ]]; then print_git_error_msg @@ -42,20 +48,45 @@ function history_sync_pull() { return fi - # Merge using sort unique + # Merge cat $HOME/.zsh_history zsh_history_decrypted | sort -u > $HOME/.zsh_history rm zsh_history_decrypted cd $DIR } -# Push current history to master +# Encrypt and push current history to master function history_sync_push() { - echo -n "Please enter GPG recipient name: " - read name + # Get option recipients + local recipients=() + while getopts -r: opt; do + case "$opt" in + r) + recipients+="$OPTARG" + ;; + *) + usage + return + ;; + esac + done -# Encrypt - if [[ -n $name ]]; then - gpg -v -r $NAME --encrypt --sign --armor --output $ZSH_HISTORY_FILE_ENC $ZSH_HISTORY_FILE + echo $recipients + + # Encrypt + if ! [[ ${#recipients[@]} > 0 ]]; then + echo -n "Please enter GPG recipient name: " + read name + recipients+=$name + fi + + ENCRYPT_CMD="gpg -v " + for r in $recipients; do + ENCRYPT_CMD+="-r \"$r\" " + done + + if [[ $ENCRYPT_CMD =~ '.(-r).+.' ]]; then + ENCRYPT_CMD+="--encrypt --sign --armor --output $ZSH_HISTORY_FILE_ENC $ZSH_HISTORY_FILE" + eval ${ENCRYPT_CMD} if [[ $? != 0 ]]; then print_gpg_encrypt_error_msg return @@ -80,7 +111,7 @@ function history_sync_push() { return fi cd $DIR - ;; + ;; esac fi @@ -89,11 +120,11 @@ function history_sync_push() { cd $DIR return fi - ;; + ;; [Nn]* ) - ;; + ;; * ) - ;; + ;; esac fi fi @@ -102,4 +133,3 @@ function history_sync_push() { alias zhpl=history_sync_pull alias zhps=history_sync_push alias zhsync="history_sync_pull && history_sync_push" - From c39a995c1e0abb4b8bdc486ea865c5d342a7c578 Mon Sep 17 00:00:00 2001 From: James Fraser Date: Tue, 19 Apr 2016 21:03:55 +1000 Subject: [PATCH 3/5] updated README --- plugins/history-sync/README.md | 41 ++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/plugins/history-sync/README.md b/plugins/history-sync/README.md index f6a5b1855..62c5e825c 100644 --- a/plugins/history-sync/README.md +++ b/plugins/history-sync/README.md @@ -1,23 +1,30 @@ -## An oh-my-zsh plugin for gpg encrypted, internet synchronized zsh history using git +## An Oh My Zsh plugin for GPG encrypted, Internet synchronized Zsh history using Git
-That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the Internet, this plugin will help you. You could even automate it using something like cron for a daily or even hourly sync. +That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the Internet, this plugin will help you. ** Be creative with this - I dare you. ** + +### What tooling do I need? +##### 1. GnuPG +history-sync uses GPG to encrypt/decrypt your zsh_history. The [GnuPG documentation](https://www.gnupg.org/documentation/manuals.html) is very good. + +##### 2. Git +history-sync uses Git to push/pull your zsh_history to/from a remote repository.
### How do I use it? - -1. Create a git repo for housing your encrypted zsh_history file (local or remote) +1. Create a Git repo for housing your encrypted zsh_history file. This repo needs to be accessible from all client shells you'd like to synchronize - Mine is $HOME/.zsh_history_proj 2. Activate history-sync plugin in your .zshrc - - git clone git@github.com:wulfgarpro/history-sync.git $HOME/.oh-my-zsh/plugins/. -3. Export environment variables (or use defaults found in history-sync.plugin.zsh) + - `git clone git@github.com:wulfgarpro/history-sync.git $HOME/.oh-my-zsh/plugins/.` +3. Export environment variables (or use defaults found in the plugin file history-sync.plugin.zsh)
These are: - - ZSH_HISTORY_FILE - - ZSH_HISTORY_PROJ - - ZSH_HISTORY_FILE_ENC - - GIT_COMMIT_MSG -4. Ensure your gpg/pgp setup is complete and you have a public/private key pair -
i.e.: - - gpg --list-keys -5. Run zhpl alias to pull -6. Run zhps alias to push -7. Run zhsync to pull/push - + - ** ZSH_HISTORY_FILE **
+ Your zsh_history file location + - ** ZSH_HISTORY_PROJ **
+ Your Git project for housing your zsh_history file + - ** ZSH_HISTORY_FILE_ENC **
+ Your encrypted zsh_history file location + - ** GIT_COMMIT_MSG **
+ Your default message when pushing to $ZSH_HISTORY_PROJ +4. Ensure your GPG setup is complete and you have a public/private key pair for encrypting/decrypting: `man gpg` +5. Run `zhpl` to pull +6. Run `zhps -r 876T3F78 -r 998A637B -r ...` to encrypt and push +7. Run `zhsync` to pull/push From 07a58cb095372d5ec54d80358728f0351e07b2e6 Mon Sep 17 00:00:00 2001 From: James Fraser Date: Tue, 19 Apr 2016 21:08:06 +1000 Subject: [PATCH 4/5] fixed bolding formatting in markdown --- plugins/history-sync/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/history-sync/README.md b/plugins/history-sync/README.md index 62c5e825c..d4da2ef67 100644 --- a/plugins/history-sync/README.md +++ b/plugins/history-sync/README.md @@ -1,6 +1,6 @@ ## An Oh My Zsh plugin for GPG encrypted, Internet synchronized Zsh history using Git
-That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the Internet, this plugin will help you. ** Be creative with this - I dare you. ** +That is, if you'd like an easy way to securely synchronise your zsh_history across many computers connected to the Internet, this plugin will help you. **Be creative with this - I dare you.** ### What tooling do I need? ##### 1. GnuPG @@ -16,13 +16,13 @@ history-sync uses Git to push/pull your zsh_history to/from a remote repository. - `git clone git@github.com:wulfgarpro/history-sync.git $HOME/.oh-my-zsh/plugins/.` 3. Export environment variables (or use defaults found in the plugin file history-sync.plugin.zsh)
These are: - - ** ZSH_HISTORY_FILE **
+ - **ZSH_HISTORY_FILE**
Your zsh_history file location - - ** ZSH_HISTORY_PROJ **
+ - **ZSH_HISTORY_PROJ**
Your Git project for housing your zsh_history file - - ** ZSH_HISTORY_FILE_ENC **
+ - **ZSH_HISTORY_FILE_ENC**
Your encrypted zsh_history file location - - ** GIT_COMMIT_MSG **
+ - **GIT_COMMIT_MSG**
Your default message when pushing to $ZSH_HISTORY_PROJ 4. Ensure your GPG setup is complete and you have a public/private key pair for encrypting/decrypting: `man gpg` 5. Run `zhpl` to pull From e86dfef1c5ede2bff955336dc8514fcfdabe103c Mon Sep 17 00:00:00 2001 From: James Fraser Date: Tue, 19 Apr 2016 21:10:13 +1000 Subject: [PATCH 5/5] removed debug echo --- plugins/history-sync/history-sync.plugin.zsh | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/history-sync/history-sync.plugin.zsh b/plugins/history-sync/history-sync.plugin.zsh index 005348914..625d6830f 100644 --- a/plugins/history-sync/history-sync.plugin.zsh +++ b/plugins/history-sync/history-sync.plugin.zsh @@ -70,8 +70,6 @@ function history_sync_push() { esac done - echo $recipients - # Encrypt if ! [[ ${#recipients[@]} > 0 ]]; then echo -n "Please enter GPG recipient name: "