Commit 27dd607

Eric Bower  ·  2026-08-14 23:20:19 -0400 EDT
parent df43346
refactor(pa-rekey): now just adds a git commit instead of automatically wiping
1 files changed,  +30, -25
+30, -25
 1@@ -2,7 +2,8 @@
 2 #
 3 # re-encrypt passwords with existing recipients and identities
 4 #
 5-# Override recipients file: export PA_RECIPIENTS=~/.local/share/pa/recipients
 6+# This will allow users to add or remove access to their passwords by updating
 7+# the recipients file.
 8 
 9 die() {
10     printf '%s: %s.\n' "$(basename "$0")" "$1" >&2
11@@ -16,42 +17,46 @@ age=$(command -v age || command -v rage) ||
12 umask 077
13 
14 : "${PA_DIR:=${XDG_DATA_HOME:-$HOME/.local/share}/pa}"
15+: "${PA_IDENTITIES:=$PA_DIR/identities}"
16 : "${PA_RECIPIENTS:=$PA_DIR/recipients}"
17 
18+[ -f "$PA_IDENTITIES" ] ||
19+    die "identities file '$PA_IDENTITIES' doesn't exist"
20+
21 [ -f "$PA_RECIPIENTS" ] ||
22     die "recipients file '$PA_RECIPIENTS' doesn't exist"
23 
24-realstore=$(realpath "$PA_DIR/passwords") ||
25-    die "couldn't get path to password directory"
26-
27-tmpdir=$PA_DIR/tmp
28+passdir=$PA_DIR/passwords
29+[ -d "$passdir" ] ||
30+    die "password directory '$passdir' doesn't exist"
31 
32-mkdir "$tmpdir" ||
33-    die "couldn't create temporary directory"
34+cd "$passdir" ||
35+    die "couldn't change to password directory"
36 
37-trap 'rm -rf "$tmpdir"; exit' EXIT
38-trap 'rm -rf "$tmpdir"; trap - INT; kill -s INT 0' INT
39+# Ensure that debug mode is never enabled to
40+# prevent the password from leaking.
41+set +x
42 
43-cp -Rp "$realstore" "$tmpdir/passwords" ||
44-    die "couldn't copy password directory"
45-
46-# Remove git repository for forward secrecy.
47-rm -rf "$tmpdir/passwords/.git"
48+printf 'Using identities: %s\n' "$PA_IDENTITIES"
49+printf 'Using recipients: %s\n' "$PA_RECIPIENTS"
50+printf 'Re-encrypting passwords in %s...\n' "$passdir"
51 
52 pa l | while IFS= read -r name; do
53     [ -n "$name" ] || continue
54-    pa s "$name" |
55-        $age -R "$PA_RECIPIENTS" -o "$tmpdir/passwords/$name.age" ||
56+    printf '  re-encrypting %s\n' "$name"
57+    pass=$($age --decrypt -i "$PA_IDENTITIES" "./$name.age") ||
58+        die "couldn't decrypt $name.age"
59+    printf '%s\n' "$pass" | $age --encrypt -R "$PA_RECIPIENTS" -o "./$name.age" ||
60         die "couldn't encrypt $name.age"
61-done
62-
63-trap - INT EXIT
64+done || exit 1
65 
66-rm -rf "$realstore" ||
67-    die "couldn't remove password directory"
68+printf 'Staging and committing changes to git...\n'
69+git add -A || die "couldn't stage files in git"
70+git commit -qm "pa-rekey run" || die "couldn't git commit"
71+printf 'Created commit: "pa-rekey run"\n'
72 
73-mv "$tmpdir/passwords" "$realstore"
74-rmdir "$tmpdir" 2>/dev/null || :
75+printf 'Done.\n'
76 
77-# Recreate git repository if needed.
78-pa l >/dev/null
79+printf '\n===\n'
80+printf 'NOTICE: If you are removing keys you might want to wipe git history so those passwords are not still accessible.\n'
81+printf '===\n'