github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/test/gpg/gen_gopass_data.sh (about) 1 #!/bin/bash 2 set -uo pipefail 3 4 # variables and commands 5 ME=$(realpath "$0") 6 WD=$(dirname "$ME") 7 GPG=$(command -v gpg) 8 if [ -z "$GPG" ]; then 9 echo "gpg not found" 10 exit 1 11 fi 12 13 GPG_PRESET="$(ls /usr/lib/gnupg*/gpg-preset-passphrase*)" 14 if [ -z "$GPG_PRESET" ]; then 15 GPG_PRESET=$(command -v gpg-preset-passphrase) 16 fi 17 if [ ! -x "$GPG_PRESET" ]; then 18 echo "gpg-preset-passphrase not found" 19 exit 1 20 fi 21 GOPASS=$(command -v gopass) 22 if [ -z "$GOPASS" ]; then 23 go install github.com/gopasspw/gopass@latest 24 GOPASS=$(command -v gopass) 25 fi 26 27 set -e 28 29 30 TESTDIR="$WD/.." 31 KEYDIR="$TESTDIR/gpg" 32 cd "$WD" 33 APP="test" 34 export GNUPGHOME=$TESTDIR/testdata/.gnupg 35 #export GPG_TTY=/dev/tty 36 if [ -d "$GNUPGHOME" ]; then 37 rm -rf "$GNUPGHOME" 38 fi 39 40 41 PWFILE="$KEYDIR/$APP.gpgpw" 42 IDFILE="$KEYDIR/$APP.gpgid" 43 PRIVFILE="$KEYDIR/$APP.gpg.key" 44 PUBFILE="$KEYDIR/$APP.asc" 45 46 # generate key 47 $GPG --batch --gen-key "$APP.keygen" 48 grep "^Passphrase:" $APP.keygen|sed -e 's/^Passphrase:\s*//g' |tr -d "\r\n">"$PWFILE" 49 EMAIL=$(grep "^Name-Email:" $APP.keygen|sed -e 's/^Name-Email:\s*//g') 50 51 # prepare agent 52 echo "default-cache-ttl 46000 53 allow-preset-passphrase 54 " >"$GNUPGHOME/gpg-agent.conf" 55 gpg-connect-agent reloadagent /bye 56 57 58 $GPG --list-secret-keys 59 60 GPG_PASSPHRASE=$(< $PWFILE ) 61 KEYID="$(gpg --list-secret-keys |grep "$EMAIL" -B 1|grep '^ '|perl -pe 's/.*\s+(\w+)$/\1/g;')" 62 KEYGRIP="$(gpg --list-secret-keys --with-keygrip|grep "$EMAIL" -B 1| grep -i 'Keygrip'|perl -pe 's/.*=\s+(\w+)$/\1/g;')" 63 64 echo -n "$KEYID" >$IDFILE 65 $GPG --export --armor "$KEYID" >"$PUBFILE" 66 echo "" 67 $GPG --pinentry-mode loopback --batch --export-secret-key --armor --passphrase "$GPG_PASSPHRASE" "$KEYID" >"$PRIVFILE" 68 "$GPG_PRESET" --preset "$KEYGRIP" <<<"$GPG_PASSPHRASE" 69 70 71 STORE="pwlib-store" 72 STOREPATH="$TESTDIR/$STORE" 73 74 # cleanup 75 if [ -d "$STOREPATH" ]; then 76 rm -rf "$STOREPATH" 77 fi 78 79 # setup gopass if needed 80 if [ ! -d "$HOME/.config/gopass" ]; then 81 $GOPASS setup --email="$EMAIL" --remote "" --crypto gpgcli --storage fs 82 fi 83 # init store 84 85 $GOPASS mounts rm "$STORE" 86 rm -rf "$STOREPATH" 87 $GOPASS init --path "$STOREPATH" --storage fs --store "$STORE" --crypto gpgcli "$KEYID" 88 89 $GOPASS insert "$STORE/passphrase" <"$PWFILE" 90 $GOPASS insert "$STORE/$APP/test1" <<<"123456" 91 $GOPASS generate "$STORE/$APP/test2" 16 92 # test reading 93 # $GOPASS cat "$STORE/$APP/test2"