-
Notifications
You must be signed in to change notification settings - Fork 21
overhaul multi-device recipients synchronization #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arcxio
wants to merge
2
commits into
main
Choose a base branch
from
recipients_sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,64 +1,65 @@ | ||
| #!/bin/sh | ||
| # | ||
| # rotate keys and reencrypt passwords | ||
| # | ||
| # Reuse identities file: export PA_IDENTITIES=~/.local/share/pa/identities | ||
| # Reuse recipients file: export PA_RECIPIENTS=~/.local/share/pa/recipients | ||
|
|
||
| die() { | ||
| printf '%s: %s.\n' "$(basename "$0")" "$1" >&2 | ||
| exit 1 | ||
| } | ||
| set -e | ||
|
|
||
| age=$(command -v age || command -v rage) || | ||
| die "age not found, install per https://age-encryption.org" | ||
| # Complete any pending | ||
| # recipients synchronization. | ||
| pa l >/dev/null | ||
|
|
||
| age_keygen=$(command -v age-keygen || command -v rage-keygen) || | ||
| die "age-keygen not found, install per https://age-encryption.org" | ||
| : "${PA_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/pa}" | ||
|
|
||
| # Restrict permissions of any new files to only the current user. | ||
| umask 077 | ||
| suffix=$(LC_ALL=C tr -dc A-Za-z0-9 </dev/urandom | | ||
| dd ibs=1 obs=1 count=10 2>/dev/null) | ||
|
|
||
| : "${PA_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/pa}" | ||
| [ "$suffix" ] | ||
|
|
||
| tmp=$PA_DIR/rekey.$suffix | ||
|
|
||
| realstore=$(realpath "$PA_DIR/passwords") || | ||
| die "couldn't get path to password directory" | ||
| mkdir "$tmp" | ||
|
|
||
| tmpdir=$PA_DIR/tmp | ||
| trap 'rm -f "$tmp/identities" "$tmp/recipients" "$tmp/passwords/.recipients" | ||
| rmdir "$tmp/passwords" "$tmp"' EXIT | ||
|
|
||
| mkdir "$tmpdir" || | ||
| die "couldn't create temporary directory" | ||
| cp -p "$PA_DIR/identities" "$tmp/identities" | ||
|
|
||
| trap 'rm -rf "$tmpdir"; exit' EXIT | ||
| trap 'rm -rf "$tmpdir"; trap - INT; kill -s INT 0' INT | ||
| # Generate recipients for current identities. | ||
| PA_DIR=$tmp PA_NOGIT='' pa l >/dev/null | ||
|
|
||
| cp -Rp "$realstore" "$tmpdir/passwords" || | ||
| die "couldn't copy password directory" | ||
| # Filter out recipients corresponding to current identities. | ||
| { grep -Fxvf "$tmp/passwords/.recipients" "$PA_DIR/recipients" || | ||
| :; } >"$tmp/recipients" | ||
|
|
||
| # Remove git repository for forward secrecy. | ||
| rm -rf "$tmpdir/passwords/.git" | ||
| rm -f "$tmp/identities" "$tmp/passwords/.recipients" | ||
|
|
||
| [ "$PA_IDENTITIES" ] && cp "$PA_IDENTITIES" "$tmpdir/identities" | ||
| [ "$PA_RECIPIENTS" ] && cp "$PA_RECIPIENTS" "$tmpdir/recipients" | ||
| # Generate a brand new key pair. | ||
| PA_DIR=$tmp PA_NOGIT='' pa l >/dev/null | ||
|
|
||
| $age_keygen >>"$tmpdir/identities" 2>/dev/null | ||
| $age_keygen -y "$tmpdir/identities" >>"$tmpdir/recipients" 2>/dev/null | ||
| printf 'add a new generated recipient? [y/N]: ' >&2 | ||
|
|
||
| pa l | while read -r name; do | ||
| pa s "$name" | | ||
| $age -R "$tmpdir/recipients" -o "$tmpdir/passwords/$name.age" || | ||
| die "couldn't encrypt $name.age" | ||
| done | ||
| [ -t 0 ] && trap 'stty echo icanon; trap - INT; kill -s INT 0' INT | ||
|
|
||
| trap - INT EXIT | ||
| [ -t 0 ] && stty -echo -icanon | ||
|
|
||
| rm -rf "$realstore" || | ||
| die "couldn't remove password directory" | ||
| answer=$(dd ibs=1 count=1 2>/dev/null) | ||
|
|
||
| mv "$tmpdir/passwords" "$realstore" | ||
| mv "$tmpdir/identities" "$(realpath "$PA_DIR/identities")" | ||
| mv "$tmpdir/recipients" "$(realpath "$PA_DIR/recipients")" | ||
| rmdir "$tmpdir" | ||
| [ -t 0 ] && stty echo icanon | ||
|
|
||
| # Recreate git repository if needed. | ||
| printf '%s\n' "${answer:-N}" >&2 | ||
|
|
||
| case $answer in [yY]) ;; *) exit 1 ;; esac | ||
|
|
||
| # Ensure files can be decrypted with both | ||
| # identities if store is in a partial state. | ||
| cat "$tmp/identities" >>"$PA_DIR/identities" | ||
|
|
||
| # Recipients are now ready to use. | ||
| mv "$tmp/recipients" "$PA_DIR/recipients" | ||
|
|
||
| # Reencrypt all passwords | ||
| # for new recipients. | ||
| pa l >/dev/null | ||
|
|
||
| # Old identity is now safe to remove. | ||
| mv "$tmp/identities" "$PA_DIR/identities" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically age-keygen is optional now as it can be substituted with plugins below, but since it's expected to be delivered with age I don't think it matters (and it's always used as a default fallback)