← Pablo Murad

SSH, dotfiles and secrets, part 2: a pocket vault on an Orange Pi

Part two: an Orange Pi Zero2 turned into a pocket vault. The Armbian image is prepared in WSL so it boots ready, passage stores secrets it can't read, restic backs it up to the NAS and Caddy serves File Browser with real HTTPS on the tailnet.

A small blue Orange Pi single-board computer with two copper heatsinks on its chips, a row of pin headers along the edge and a thin antenna cable attached.
Contents

A note before you start: this is the second half of the longer, better explained version of my last SSH post, How I organize SSH. Part 1 covers the PC.

Part 1 ended with everything on the PC. This part is about the vault: an Orange Pi Zero2 with 1 GB of RAM running Armbian (Debian 13, the minimal image). It stores secrets without being able to read them, pushes them to two Git hosts and backs itself up to the NAS.

pc PC private age key vault Orange Pi public key only pc->vault passage insert over ssh vault->pc scp the .age, decrypt on the PC forgejo Forgejo private repo vault->forgejo git push github GitHub private repo vault->github git push nas NAS restic vault->nas daily backup
Source
digraph {
rankdir=LR
fontname="Helvetica"
margin="0.4,0.12"
node [shape=box, style=rounded, fontname="Helvetica", fontsize=11]
edge [fontname="Helvetica", fontsize=9, arrowsize=0.6]
pc [label="PC\nprivate age key"]
vault [label="Orange Pi\npublic key only", color="#d93a2b", fontcolor="#d93a2b"]
forgejo [label="Forgejo\nprivate repo"]
github [label="GitHub\nprivate repo"]
nas [label="NAS\nrestic"]
pc -> vault [label="passage insert\nover ssh"]
vault -> forgejo [label="git push"]
vault -> github [label="git push"]
vault -> nas [label="daily backup"]
vault -> pc [label="scp the .age,\ndecrypt on the PC"]
}

An image that's ready before it boots #

Instead of turning the board on and setting it up by hand, I prepared the image on the PC before writing it to the card. The first boot already comes up with my user, key-only login, the firewall, Tailscale and Wi-Fi. That works because the Linux inside WSL can mount the image and run its ARM programs through qemu.1

Opening the image and getting inside #

#!/bin/bash
# mount.sh: in WSL (Debian), as root
# apt install qemu-user-static binfmt-support xz-utils parted e2fsprogs
set -euo pipefail
IMG=vault.img
xz -dc Armbian_*_minimal.img.xz > "$IMG"
truncate -s 4G "$IMG" # room for the new packages
L=$(losetup -f --show -P "$IMG")
parted -s "$L" resizepart 1 100%
e2fsck -fy "${L}p1" >/dev/null || true
resize2fs "${L}p1"

R=/mnt/img; mkdir -p $R; mount "${L}p1" $R
mv $R/etc/resolv.conf $R/etc/resolv.conf.orig # WSL's DNS inside the image
cp -L /etc/resolv.conf $R/etc/resolv.conf
for d in dev dev/pts proc sys; do mount --bind /$d $R/$d; done

cp personalize.sh $R/tmp/
chroot $R /bin/bash /tmp/personalize.sh

rm $R/tmp/personalize.sh $R/etc/resolv.conf
mv $R/etc/resolv.conf.orig $R/etc/resolv.conf
for d in sys proc dev/pts dev; do umount -l $R/$d; done
sync
# processes the chroot left behind (gpg-agent, dirmngr) hold the mount for a few seconds
for i in $(seq 1 20); do umount $R 2>/dev/null && break; sleep 3; done
losetup -d "$L"

Run the whole thing as a single command. The WSL distro shuts itself down when it sits idle, and it takes the mount with it halfway through.

What goes inside #

personalize.sh runs inside the image, emulated. This is a trimmed version. The sudo setup and the firewall rules are yours to fill in:

#!/bin/bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive LC_ALL=C
U=you
KEY='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleExampleExampleExampleExample0 you@example.com'

apt-get update -qq
apt-get install -y -qq curl git age restic ufw caddy watchdog >/dev/null
# Tailscale's own repository (trixie is Debian 13)
curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.noarmor.gpg -o /usr/share/keyrings/tailscale-archive-keyring.gpg
curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.tailscale-keyring.list -o /etc/apt/sources.list.d/tailscale.list
apt-get update -qq && apt-get install -y -qq tailscale >/dev/null

echo vault > /etc/hostname

# a user that logs in with a key only; root locked, no password logins
useradd -m -s /bin/bash -G sudo $U
install -d -m 700 -o $U -g $U /home/$U/.ssh
echo "$KEY" > /home/$U/.ssh/authorized_keys
chown $U:$U /home/$U/.ssh/authorized_keys; chmod 600 /home/$U/.ssh/authorized_keys
passwd -l root >/dev/null
printf 'PasswordAuthentication no\nPermitRootLogin no\n' > /etc/ssh/sshd_config.d/10-vault.conf
# (sudo for $U goes here, the way you like it)
rm -f /root/.not_logged_in_yet # skips Armbian's first-login wizard

# hardware watchdog: if the board hangs, it reboots itself
mkdir -p /etc/systemd/system.conf.d
printf '[Manager]\nRuntimeWatchdogSec=15\n' > /etc/systemd/system.conf.d/watchdog.conf

# first boot: join the tailnet, destroy the auth key, turn on the firewall
cat > /usr/local/sbin/first-boot <<'EOF'
#!/bin/bash
. /etc/vault/tailscale.env 2>/dev/null || true
if [ -n "${TS_AUTHKEY:-}" ]; then
tailscale up --authkey="$TS_AUTHKEY" --ssh --hostname=vault --advertise-tags=tag:server
fi
shred -u /etc/vault/tailscale.env 2>/dev/null || true
ufw default deny incoming
# (allow rules for your home network and the tailnet go here)
ufw --force enable
systemctl disable first-boot.service
EOF
chmod 700 /usr/local/sbin/first-boot
cat > /etc/systemd/system/first-boot.service <<'EOF'
[Unit]
Description=First boot setup
Wants=network-online.target
After=network-online.target tailscaled.service
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/first-boot
[Install]
WantedBy=multi-user.target
EOF
systemctl enable first-boot.service tailscaled.service >/dev/null
apt-get clean

The firewall ends up accepting connections only from the home network and the tailnet. The Tailscale auth key is single use and tagged, and it goes into /etc/vault/tailscale.env from outside the script, only while the image is being built. The first boot uses it once and shreds it. No secret lives in the repository.

A Wi-Fi fallback #

The cable comes first, and Wi-Fi takes over if the cable goes away. netplan handles that with route metrics, and one detail held me up: the metric has to be the same for IPv4 and IPv6, or netplan rejects the file. The blog squeezes indentation, so here it is in YAML's one-line form, which netplan reads just the same:

# /etc/netplan/20-wifi.yaml (chmod 600)
network: {version: 2, renderer: networkd, wifis: {wlan0: {dhcp4: true, dhcp6: true, dhcp4-overrides: {route-metric: 2048}, dhcp6-overrides: {route-metric: 2048}, regulatory-domain: BR, access-points: {"HomeNetwork": {auth: {key-management: psk, password: "wifi-password"}}}}}}

Changed the Wi-Fi password with the board running? netplan apply alone doesn't restart wpa_supplicant:

sudo netplan apply
sudo systemctl restart netplan-wpa-wlan0.service
sudo networkctl reconfigure wlan0

Writing the card #

wsl --mount doesn't take USB card readers, so the writing happens on Windows, with balenaEtcher or Rufus writing vault.img directly. Before the open image is deleted, it goes to the cloud encrypted with age (the commands are in part 1), and the unencrypted copy goes away with shred -u.

On the first boot, the vault shows up on the tailnet and ssh vault gets in with no key.

passage: a vault that can only encrypt #

passage is the classic pass with age in place of GPG. Each secret is an .age file in a folder, and the folder is a Git repository. On the vault, I set it up to encrypt only: it knows my public key and writes secrets, but it can't read any of them, because the private key stays on the PC.

sudo git clone --depth 1 https://github.com/FiloSottile/passage /opt/passage
sudo ln -s /opt/passage/src/password-store.sh /usr/local/bin/passage

# only the PUBLIC key comes to the vault
mkdir -p ~/.passage/store
echo 'age1p3yuskta72q72kdk6w5uahfr5m8yq9uxkpa3pe7583hum62k5g6spmwp5t' > ~/.passage/store/.age-recipients

# passage wants an identities file even just to encrypt; here it's only a note
printf '# the private key never lives on this machine\n' > ~/.passage/identities
chmod 600 ~/.passage/identities

git -C ~/.passage/store init -b main

With .age-recipients at the root of the store, every insert encrypts to that public key. Reading, on the other hand, fails right there on the vault, and opens fine on the PC:

Two terminal panels. In the top panel, labeled vault, the store holds only an age public key in its recipients file and the identities file is just a comment saying the private key never lives on that machine. passage generates and inserts two secrets and lists them as a small tree, but passage show fails because no secret key is found. In the bottom panel, labeled pc, age decrypts the same file with the passphrase-protected key and prints the recovery codes.

insert -m hands the input straight to age, so a binary file (a license .key, a PDF) goes in intact.

Two remotes, one push #

The vault's repository has two push destinations, both private: my Forgejo and GitHub. One git push sends to both. The vault uses its own SSH key, added as a deploy key with write access to those two repositories and nothing else. It's the opposite of the dotfiles mirror in part 1, because here the device does the pushing.

$ cd ~/.passage/store
$ git remote add origin git@git.example.com:you/vault.git
$ git remote set-url --add --push origin git@git.example.com:you/vault.git
$ git remote set-url --add --push origin git@github.com:you/vault.git
$ git remote -v
origin git@git.example.com:you/vault.git (fetch)
origin git@git.example.com:you/vault.git (push)
origin git@github.com:you/vault.git (push)
Source
$ cd ~/.passage/store
$ git remote add origin git@git.example.com:you/vault.git
$ git remote set-url --add --push origin git@git.example.com:you/vault.git
$ git remote set-url --add --push origin git@github.com:you/vault.git
$ git remote -v
origin git@git.example.com:you/vault.git (fetch)
origin git@git.example.com:you/vault.git (push)
origin git@github.com:you/vault.git (push)

passage makes a commit on every change, and a hook pushes it. flock puts the pushes in a queue. Without it, an insert followed by an rm fired two pushes at the same time, and Forgejo ended up one commit behind.

#!/bin/sh
# ~/.passage/store/.git/hooks/post-commit
( sleep 3; flock /tmp/vault-push.lock git push -q origin main >/dev/null 2>&1 ) &

Reading on the PC #

Since the vault can't open anything, reading happens on the PC: the script pulls the .age file over SSH and decrypts it with the local identity.

# read-secret.ps1
param([Parameter(Mandatory)][string]$Name, [string]$Save)
$id = "$HOME\keys\main.age"
$tmp = New-TemporaryFile
try {
scp -q "vault:.passage/store/$Name.age" $tmp.FullName
if ($Save) { age -d -i $id -o $Save $tmp.FullName } # binary files
else { age -d -i $id $tmp.FullName }
} finally { Remove-Item $tmp.FullName -Force }
.\read-secret.ps1 accounts/new-account
.\read-secret.ps1 licenses/program.key -Save $HOME\Desktop\program.key

Backups to the NAS #

The vault backs itself up every day to the NAS at home, over SFTP, encrypted by restic. On a Synology with DSM 7, the SSH service and the user home service both need to be on. The detail that cost me one try: DSM's SFTP shows the user's folder as /home, so the repository path starts with /home/... and not with the real path on the volume.

# /etc/vault/restic.env (owned by root, chmod 600)
RESTIC_REPOSITORY='sftp:you@nas:/home/backups/vault'
RESTIC_PASSWORD='a-long-generated-password'
#!/bin/bash
# /usr/local/sbin/vault-backup
set -uo pipefail
set -a; . /etc/vault/restic.env; set +a

restic cat config >/dev/null 2>&1 || restic init
restic backup --one-file-system --exclude-caches /home/you /etc /var/lib/filebrowser \
&& restic forget --prune <your retention flags>

A systemd timer runs it, with a .service next to it that calls the script:

# /etc/systemd/system/vault-backup.timer
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target

Then systemctl enable --now vault-backup.timer. The restic password goes into 1Password too, because without it the backup is worth nothing.

A terminal on the machine named vault. restic snapshots shows the latest snapshot, taken on November 26 at 4:33 in the morning, with the path of the passage folder. restic ls lists the contents of the store inside that snapshot: the recipients file, the git folder, and the accounts and services folders.

Real HTTPS on the tailnet #

To handle files from a browser, I use File Browser behind Caddy. Caddy gets the *.ts.net certificate from tailscaled on its own, as long as it has permission. On the home network it uses its own internal CA.

echo 'TS_PERMIT_CERT_UID=caddy' | sudo tee -a /etc/default/tailscaled
sudo systemctl restart tailscaled

That restart drops every session that came in through Tailscale SSH, yours included. Run it while logged in through the local network address.

# /etc/caddy/Caddyfile
vault.example-tailnet.ts.net {
reverse_proxy 127.0.0.1:8080
}

vault.local {
tls internal
reverse_proxy 127.0.0.1:8080
}
DB=/var/lib/filebrowser/filebrowser.db
sudo install -d -o you -g you /var/lib/filebrowser /home/you/files
sudo -u you filebrowser -d $DB config init -a 127.0.0.1 -p 8080 -r /home/you/files
sudo -u you filebrowser -d $DB users add you 'a-strong-password' --perm.admin

File Browser has to be stopped for those commands, because it keeps the database locked while it runs.

One more word on Termius #

There's a third reason I left Termius that didn't make it into the earlier posts: in Brazil it's expensive, and that weighed in too. Even so, it was a pleasant app for the two years I used it, and I recommend it to anyone who wants to give it a try. I'm not affiliated with Termius and I get nothing for saying this.

Notes

  1. The binfmt_misc feature of the Linux kernel hands any ARM program to qemu-user-static automatically, so chroot into an ARM image works as if the PC were the board. ↩