Files
MemRelay/scripts/validate_bitwarden_cli.sh

56 lines
1.5 KiB
Bash

#!/bin/sh
set -eu
: "${BW_SERVER:?BW_SERVER is required}"
: "${BW_EMAIL:?BW_EMAIL is required}"
: "${BW_PASSWORD:?BW_PASSWORD is required}"
if [ "${BW_OFFLINE_ONLY:-0}" = "1" ]; then
BW_SESSION="$(bw unlock "$BW_PASSWORD" --raw)"
export BW_SESSION
items="$(bw list items)"
count="$(printf '%s' "$items" | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>process.stdout.write(String(JSON.parse(d).length)))')"
if [ "$count" -lt 1 ]; then
echo "Offline cache is empty" >&2
exit 1
fi
echo "Bitwarden CLI offline validation passed"
exit 0
fi
bw logout >/dev/null 2>&1 || true
bw config server "$BW_SERVER" >/dev/null
BW_SESSION="$(bw login "$BW_EMAIL" "$BW_PASSWORD" --raw)"
export BW_SESSION
bw sync >/dev/null
item_json='{"type":1,"name":"MemRelay Stage Zero","login":{"username":"stage0","password":"test-secret","totp":"JBSWY3DPEHPK3PXP"}}'
encoded="$(printf '%s' "$item_json" | bw encode)"
created="$(bw create item "$encoded")"
item_id="$(printf '%s' "$created" | node -e 'let d="";process.stdin.on("data",c=>d+=c).on("end",()=>process.stdout.write(JSON.parse(d).id))')"
totp="$(bw get totp "$item_id")"
case "$totp" in
??????) ;;
*) echo "Unexpected TOTP response" >&2; exit 1 ;;
esac
pids=""
for _index in 1 2 3 4 5; do
bw get item "$item_id" >/dev/null &
pids="$pids $!"
done
for pid in $pids; do
wait "$pid"
done
bw lock >/dev/null
BW_SESSION="$(bw unlock "$BW_PASSWORD" --raw)"
export BW_SESSION
bw get item "$item_id" >/dev/null
bw sync >/dev/null
echo "Bitwarden CLI online validation passed"