diff --git a/README.md b/README.md index 556e0cf..e50ddd5 100644 --- a/README.md +++ b/README.md @@ -127,8 +127,10 @@ With `bw` unlocked on the intended vault, run this on the source machine: dot secrets push-secretctl ``` -This saves all entries in the secretctl store as one encrypted secure note -named `dotfiles/secretctl`. Repeating the command replaces that snapshot. +This saves all entries as encrypted secure-note parts to stay below +Bitwarden’s Notes limit. The manifest points to the newest complete +snapshot. Older parts stay in the vault. Restore also accepts the original +single-note backup format. On the destination machine, unlock the same vault and restore the snapshot: ```bash diff --git a/bin/dot b/bin/dot index 6322cfd..9f9fd63 100755 --- a/bin/dot +++ b/bin/dot @@ -409,7 +409,7 @@ bw_put_note() { # name, notes-file bw get template item | jq --arg name "$name" --rawfile n "$file" \ '.type = 2 | .secureNote = {type: 0} | .name = $name | .notes = $n | .login = null' \ | bw encode | bw create item >/dev/null - fi + fi || return 1 ok "bitwarden: $name" } @@ -542,27 +542,40 @@ secrets_render() { secrets_push_secretctl() ( [ "$#" -le 1 ] || die "usage: dot secrets push-secretctl [SOURCE]" have secretctl || die "secretctl is required" - local src="${1:-${SECRETCTL_FILE:-$HOME/secrets.txt}}" keys key stage jq_bin + local src="${1:-${SECRETCTL_FILE:-$HOME/secrets.txt}}" keys key stage jq_bin part generation checksum + local -a parts keys="$(secretctl --file "$src" list)" || die "Could not list secrets" [ -n "$keys" ] || die "No secrets to upload" jq_bin="$(command -v jq)" || die "jq is required" bw_ready bw sync >/dev/null umask 077 - stage="$(mktemp)" - trap 'rm -f -- "$stage"' EXIT + stage="$(mktemp -d)" + trap 'rm -rf -- "$stage"' EXIT + generation="${stage##*/}" while IFS= read -r key; do secretctl --file "$src" get "$key" -- "$jq_bin" -jn --arg key "$key" 'env[$key]' | - secretctl --file "$stage" add "$key" --allow-empty >/dev/null || + secretctl --file "$stage/store" add "$key" --allow-empty >/dev/null || die "Could not export secretctl entry $key" done <<<"$keys" - bw_put_note "$BW_PREFIX/secretctl" "$stage" + # Leave room for encryption overhead and split only ASCII base64 text. + base64 -w0 "$stage/store" | split -b 6000 -d -a 6 - "$stage/part." + parts=("$stage"/part.*) + for part in "${parts[@]}"; do + bw_put_note "$BW_PREFIX/secretctl/$generation/${part##*.}" "$part" || + die "Could not upload snapshot part" + done + checksum="$(sha256sum <"$stage/store")" + jq -n --arg generation "$generation" --argjson parts "${#parts[@]}" \ + --arg sha256 "${checksum%% *}" '{version:1,generation:$generation,parts:$parts,sha256:$sha256}' >"$stage/manifest" + # Publish the manifest last so a partial upload leaves the prior backup usable. + bw_put_note "$BW_PREFIX/secretctl/manifest" "$stage/manifest" ) secrets_pull_secretctl() ( [ "$#" -le 1 ] || die "usage: dot secrets pull-secretctl [DESTINATION]" have secretctl || die "secretctl is required" - local dest="${1:-${SECRETCTL_FILE:-$HOME/secrets.txt}}" stage + local dest="${1:-${SECRETCTL_FILE:-$HOME/secrets.txt}}" stage manifest generation count checksum actual index part if [ -e "$dest" ] || [ -L "$dest" ]; then die "$dest already exists; choose a new destination" fi @@ -571,9 +584,31 @@ secrets_pull_secretctl() ( umask 077 stage="$(mktemp -- "$(dirname -- "$dest")/.secretctl-import.XXXXXXXX")" trap 'rm -f -- "$stage"' EXIT - bw_find "$BW_PREFIX/secretctl" | - jq -je 'select(type == "object" and .type == 2) | .notes | select(type == "string" and length > 0)' >"$stage" || - die "No valid secretctl secure note in this vault" + manifest="$(bw_find "$BW_PREFIX/secretctl/manifest")" || die "Could not look up the snapshot manifest" + if [ -z "$manifest" ]; then + bw_find "$BW_PREFIX/secretctl" | + jq -je 'select(.type == 2) | .notes | select(type == "string" and length > 0)' >"$stage" || + die "No valid secretctl backup in this vault" + else + manifest="$(jq -er 'select(.type == 2) | .notes | fromjson | + select(.version == 1 and (.generation | test("^tmp[.][a-zA-Z0-9]+$")) and + (.parts | type == "number" and . >= 1 and . <= 1000000 and floor == .) and + (.sha256 | test("^[a-f0-9]{64}$")))' <<<"$manifest")" || + die "Invalid secretctl snapshot manifest" + generation="$(jq -r .generation <<<"$manifest")" + count="$(jq -r .parts <<<"$manifest")" + checksum="$(jq -r .sha256 <<<"$manifest")" + { + for ((index = 0; index < count; index++)); do + printf -v part '%06d' "$index" + bw_find "$BW_PREFIX/secretctl/$generation/$part" | + jq -je 'select(.type == 2) | .notes | select(test("^[A-Za-z0-9+/=]+$"))' || + exit 1 + done + } | base64 -d >"$stage" || die "Snapshot is missing a part or is corrupt" + actual="$(sha256sum <"$stage")" + [ "${actual%% *}" = "$checksum" ] || die "Snapshot integrity check failed" + fi secretctl --file "$stage" check >/dev/null 2>&1 || die "Vault snapshot failed secretctl validation" ln -T -- "$stage" "$dest" || die "Could not create $dest without overwriting" ok "Secretctl file restored at $dest (0600)"