Merge branch 't3/implement-auth-deploy'

Integrate auth, config, watcher and terminal wiring in buildApp: auth guards every API route by default, notebook watcher events feed the /api/events hub, the terminal runs in the configured workspace, and SHELLBOOK_DATA_DIR is the single data directory variable. The systemd unit uses KillMode=process so tmux sessions survive service restarts.
This commit is contained in:
Florian Schmidt
2026-11-16 00:11:06 +01:00
24 changed files with 1232 additions and 21 deletions
+1
View File
@@ -0,0 +1 @@
*.sh text eol=lf
+63
View File
@@ -0,0 +1,63 @@
# Deploy ShellBook (M5 / #6)
Access is root-equivalent across managed hosts. Restrict CT port 8080 to the
Traefik host with the Proxmox firewall; use HTTPS and limit the router to your
LAN/Tailscale addresses. No password is set initially: API access fails closed.
1. Copy `deploy/` to the Proxmox VE 9 host. As root run
`bash deploy/proxmox/create-ct.sh`. Defaults: unprivileged Debian 13 CT 501,
2 cores, 2 GiB RAM, 16 GiB disk, DHCP on vmbr0, onboot, nesting and TUN.
Overrides: `CTID`, `CT_STORAGE`, `TEMPLATE_STORAGE`, `CT_BRIDGE`, `CT_IP`
(Proxmox ip syntax), `CT_HOSTNAME`, `CT_CORES`, `CT_MEMORY`, `CT_DISK_GB`.
Existing CTs must be unprivileged Debian 13 of the host architecture; they
may reboot when TUN passthrough is added. No raw config appending is used.
2. Optionally supply `SHELLBOOK_REPO`, `SHELLBOOK_REF`, `SHELLBOOK_HOST`,
`SHELLBOOK_PORT`, `SHELLBOOK_TRUSTED_PROXIES` (comma-separated proxy IPs),
and `TS_AUTHKEY` in the host environment. Auth keys are transferred in a
mode-0600 temporary file and removed, never stored in the service env.
3. In the CT run the printed password command. It also accepts one password
line on stdin; do not pass passwords as arguments. Source-checkout command:
`SHELLBOOK_DATA_DIR=/var/lib/shellbook pnpm --filter @shellbook/server set-password`.
4. Run `claude` as shellbook and use `/login`. Authorize
`/home/shellbook/.ssh/id_ed25519.pub` on managed hosts. The service user has
no sudo access. Initialize notebooks manually if templates were absent.
5. Install the Traefik example in its file-provider directory and replace the
hostname, CT address, TLS resolver and allowed networks. Set the actual
Traefik peer IP in `/etc/shellbook/env` as `SHELLBOOK_TRUSTED_PROXIES`, then
`systemctl restart shellbook`. Only configured proxies can influence HTTPS
detection (and thus Secure cookies). TLS terminates at Traefik.
Standalone CT installation: run `bash deploy/install.sh` as root on Debian 13.
The installer can be rerun; it preserves env, password, workspace, SSH key and
session signing secret. It builds the checkout but does not pull on rerun.
Updates: `bash /opt/shellbook/deploy/update.sh` (root). Failure before restart
leaves the running service in place. Check `journalctl -u shellbook`.
Configuration is `/etc/shellbook/env`; data is `/var/lib/shellbook` (back it up
along with workspace and service-user home). `SHELLBOOK_SESSION_SECONDS` defaults
to 2592000 (30 days). Cookies survive restart. Logout clears the browser cookie;
password changes do not revoke existing cookies. For emergency revocation stop
the service, delete `session-secret` from the data directory and start it again.
Forward auth is off by default. To enable it set
`SHELLBOOK_FORWARD_AUTH_HEADER=Remote-User` and trusted proxy IPs. The header is
accepted only from an exact direct peer IP on that list (IPv4-mapped IPv6 is
normalized). Traefik must strip incoming identity headers and set its own after
authentication; block direct CT access. Built-in password login remains available.
WebSocket clients must send the same session cookie; cross-origin API requests
are rejected. Custom browser clients must use the public Host header.
Build choices: esbuild is a well-known TypeScript bundler; it bundles the source
`@shellbook/core` via an explicit alias, while npm dependencies (including native
node-pty) stay external. Production therefore retains installed node_modules.
`@fastify/static` supplies Fastify-compatible static serving and safe file paths;
the static directory is served only when index.html exists, with SPA fallback
outside `/api`. Node 22 comes from nodejs.org's latest-v22.x binary release,
verified with its SHA-256 manifest downloaded over HTTPS. This avoids third-party
apt signing-key compatibility issues on Debian 13. Rerun the installer to update
Node 22 security releases; unattended upgrades cover Debian packages only.
Build tools support node-pty compilation. Claude uses its native installer.
Local deployment regression: `bash deploy/test/template-picker.test.sh`.
The scripts require bash, use LF endings, and avoid early-consuming pipelines
under pipefail. Proxmox, systemd, Tailscale and SSH require deployment validation.
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $EUID == 0 ]] || { echo 'Run as root inside Debian 13' >&2; exit 1; }
# shellcheck source=/dev/null
source /etc/os-release
[[ $ID == debian && $VERSION_ID == 13 ]] || { echo 'Debian 13 required' >&2; exit 1; }
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y ca-certificates git curl jq tmux build-essential python3 unattended-upgrades openssh-client xz-utils
# Official Node binaries, verified against the SHA-256 manifest over HTTPS.
case $(dpkg --print-architecture) in
amd64) node_arch=x64 ;;
arm64) node_arch=arm64 ;;
*) echo 'Unsupported Node binary architecture' >&2; exit 1 ;;
esac
node_temp=$(mktemp -d)
trap 'rm -rf -- "$node_temp"' EXIT
curl -fsSL https://nodejs.org/dist/latest-v22.x/SHASUMS256.txt -o "$node_temp/SHASUMS256.txt"
node_file=$(awk -v arch="$node_arch" '$2 ~ ("^node-v22\\.[0-9]+\\.[0-9]+-linux-" arch "\\.tar\\.xz$") {print $2}' "$node_temp/SHASUMS256.txt")
[[ -n $node_file && $node_file != *$'\n'* ]] || { echo 'No unique Node 22 binary found' >&2; exit 1; }
curl -fsSL "https://nodejs.org/dist/latest-v22.x/$node_file" -o "$node_temp/$node_file"
awk -v file="$node_file" '$2 == file' "$node_temp/SHASUMS256.txt" > "$node_temp/checksum"
(cd "$node_temp"; sha256sum -c checksum)
mkdir "$node_temp/unpacked"
tar -xJf "$node_temp/$node_file" -C "$node_temp/unpacked" --strip-components=1
# Replace the executable rather than truncating one a running service uses.
install -m 0755 "$node_temp/unpacked/bin/node" /usr/local/bin/node
rm "$node_temp/unpacked/bin/node"
cp -a "$node_temp/unpacked/." /usr/local/
rm -rf -- "$node_temp"
trap - EXIT
[[ $(node --version) == v22.* ]] || { echo 'Node 22 required' >&2; exit 1; }
# Pin a current Corepack rather than relying on the binary bundled with Node.
npm install -g [email protected]
corepack enable
corepack prepare [email protected] --activate
if ! id shellbook >/dev/null 2>&1; then useradd --create-home --shell /bin/bash shellbook; fi
install -d -m 0700 -o shellbook -g shellbook /var/lib/shellbook /home/shellbook/.ssh
repo=${SHELLBOOK_REPO:-https://git.finalfactory.de/FinalFactory/ShellBook.git}
if [[ ! -e /opt/shellbook ]]; then
install -d -o shellbook -g shellbook /opt/shellbook
runuser -u shellbook -- git clone "$repo" /opt/shellbook
fi
if [[ -n ${SHELLBOOK_REF:-} ]]; then runuser -u shellbook -- git -C /opt/shellbook checkout "$SHELLBOOK_REF"; fi
runuser -u shellbook -- bash -c 'cd /opt/shellbook; pnpm install --frozen-lockfile; pnpm build'
workspace=/home/shellbook/workspace
install -d -o shellbook -g shellbook "$workspace"
if [[ -z $(find "$workspace" -mindepth 1 -maxdepth 1 -print -quit) ]]; then
if [[ -d /opt/shellbook/templates/workspace ]]; then cp -a /opt/shellbook/templates/workspace/. "$workspace/"; fi
chown -R shellbook:shellbook "$workspace"
runuser -u shellbook -- git -C "$workspace" init
fi
if [[ ! -f /home/shellbook/.ssh/id_ed25519 ]]; then
runuser -u shellbook -- ssh-keygen -q -t ed25519 -N '' -f /home/shellbook/.ssh/id_ed25519 -C shellbook
fi
if [[ ! -x /home/shellbook/.local/bin/claude ]]; then
runuser -u shellbook -- env HOME=/home/shellbook bash -o pipefail -c 'curl -fsSL https://claude.ai/install.sh | bash'
fi
install -d -m 0750 /etc/shellbook
if [[ ! -f /etc/shellbook/env ]]; then
host=${SHELLBOOK_HOST:-0.0.0.0}
port=${SHELLBOOK_PORT:-8080}
proxies=${SHELLBOOK_TRUSTED_PROXIES:-}
[[ $host =~ ^[a-zA-Z0-9.:_-]+$ && $port =~ ^[0-9]+$ && $proxies =~ ^[a-zA-Z0-9.,:/_-]*$ ]] || { echo 'Invalid listen/proxy settings' >&2; exit 1; }
cat > /etc/shellbook/env <<EOF
SHELLBOOK_HOST=$host
SHELLBOOK_PORT=$port
SHELLBOOK_DATA_DIR=/var/lib/shellbook
SHELLBOOK_WORKSPACE=/home/shellbook/workspace
SHELLBOOK_SSH_KEY=/home/shellbook/.ssh/id_ed25519
SHELLBOOK_STATIC_DIR=/opt/shellbook/apps/web/dist
SHELLBOOK_TRUSTED_PROXIES=$proxies
PATH=/home/shellbook/.local/bin:/usr/local/bin:/usr/bin:/bin
EOF
fi
chmod 600 /etc/shellbook/env
cat > /etc/systemd/system/shellbook.service <<'EOF'
[Unit]
Description=ShellBook runbook server
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=shellbook
Group=shellbook
WorkingDirectory=/opt/shellbook
EnvironmentFile=/etc/shellbook/env
ExecStart=/usr/local/bin/node /opt/shellbook/apps/server/dist/main.js
Restart=on-failure
# tmux servers started by the terminal panel must survive service restarts (spec §6).
KillMode=process
RestartSec=5
UMask=0077
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
# Home and /var/lib remain writable for workspace, SSH and Claude/tmux state.
# Avoid namespace/device hardening that breaks unprivileged LXC or PTYs.
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/apt/apt.conf.d/20auto-upgrades <<'EOF'
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
EOF
if [[ -n ${TS_AUTHKEY:-} ]]; then
curl -fsSL https://tailscale.com/install.sh | sh
key_file=$(mktemp)
trap 'rm -f "$key_file"' EXIT
chmod 600 "$key_file"
printf '%s' "$TS_AUTHKEY" > "$key_file"
tailscale up --auth-key="file:$key_file"
rm -f "$key_file"
fi
systemctl daemon-reload
systemctl enable shellbook
systemctl restart shellbook
cat <<'EOF'
Next steps:
1. Set the password (hidden prompt):
runuser -u shellbook -- env SHELLBOOK_DATA_DIR=/var/lib/shellbook node /opt/shellbook/apps/server/dist/cli/set-password.js
2. Log into Claude as the service user:
runuser -l shellbook -c '/home/shellbook/.local/bin/claude'
Then use /login.
3. Authorize this public SSH key on managed servers:
EOF
cat /home/shellbook/.ssh/id_ed25519.pub
echo '4. Configure Traefik, trusted proxy IPs and the LAN/Tailscale firewall; see deploy/README.md.'
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $EUID == 0 ]] || { echo 'Run on the Proxmox host as root' >&2; exit 1; }
script_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=deploy/proxmox/template-picker.sh
source "$script_dir/template-picker.sh"
ctid=${CTID:-501}
[[ $ctid =~ ^[0-9]+$ ]] || { echo 'Invalid CTID' >&2; exit 1; }
arch=$(dpkg --print-architecture)
if pct config "$ctid" >/dev/null 2>&1; then
config=$(pct config "$ctid")
[[ $config == *'ostype: debian'* && $config == *"arch: $arch"* && $config == *'unprivileged: 1'* ]] || {
echo 'Refusing to touch a non-Debian, wrong-architecture, or privileged CT' >&2; exit 1;
}
pct start "$ctid" 2>/dev/null || [[ $(pct status "$ctid") == *running* ]]
# These variables must expand inside the CT, not on the host.
# shellcheck disable=SC2016
pct exec "$ctid" -- sh -c '. /etc/os-release; test "$ID" = debian && test "$VERSION_ID" = 13' || {
echo 'Refusing to touch a CT that is not Debian 13' >&2; exit 1;
}
container_arch=$(pct exec "$ctid" -- dpkg --print-architecture)
[[ $container_arch == "$arch" ]] || { echo 'Container architecture mismatch' >&2; exit 1; }
else
pveam update
available=$(pveam available --section system)
template=$(pick_template "$arch" <<< "$available")
[[ -n $template ]] || { echo "No Debian 13 template for $arch" >&2; exit 1; }
pveam download "${TEMPLATE_STORAGE:-local}" "$template"
pct create "$ctid" "${TEMPLATE_STORAGE:-local}:vztmpl/$template" \
--hostname "${CT_HOSTNAME:-shellbook}" --ostype debian --arch "$arch" \
--unprivileged 1 --features nesting=1 --onboot 1 \
--cores "${CT_CORES:-2}" --memory "${CT_MEMORY:-2048}" --swap 512 \
--rootfs "${CT_STORAGE:-local-lvm}:${CT_DISK_GB:-16}" \
--net0 "name=eth0,bridge=${CT_BRIDGE:-vmbr0},ip=${CT_IP:-dhcp}"
fi
# pct manages the active config correctly even when snapshot sections exist.
config=$(pct config "$ctid")
needs_restart=0
features=$(awk -F': ' '$1 == "features" {print $2}' <<< "$config")
if [[ $features != *nesting=1* ]]; then
features=${features//nesting=0/nesting=1}
[[ $features == *nesting=1* ]] || features="${features:+$features,}nesting=1"
pct set "$ctid" --features "$features"
needs_restart=1
fi
pct set "$ctid" --onboot 1
if [[ $config != *'path=/dev/net/tun'* && $config != *'dev0: /dev/net/tun'* ]]; then
[[ $config != *'dev0:'* ]] || { echo 'dev0 already occupied; configure /dev/net/tun passthrough manually' >&2; exit 1; }
pct set "$ctid" --dev0 /dev/net/tun
needs_restart=1
fi
if [[ $needs_restart == 1 && $(pct status "$ctid") == *running* ]]; then pct shutdown "$ctid" --timeout 60; fi
if [[ $(pct status "$ctid") != *running* ]]; then pct start "$ctid"; fi
# Wait for init/network after creation or reboot.
for ((attempt=0; attempt<60; attempt++)); do
if pct exec "$ctid" -- test -d /run/systemd/system; then break; fi
sleep 1
done
pct push "$ctid" "$script_dir/../install.sh" /root/shellbook-install.sh --perms 0700
# Transfer optional settings without exposing TS_AUTHKEY in process arguments.
settings=$(mktemp)
trap 'rm -f "$settings"' EXIT
chmod 600 "$settings"
for key in SHELLBOOK_REPO SHELLBOOK_REF SHELLBOOK_HOST SHELLBOOK_PORT SHELLBOOK_TRUSTED_PROXIES TS_AUTHKEY; do
if [[ -v $key ]]; then printf 'export %s=%q\n' "$key" "${!key}" >> "$settings"; fi
done
pct push "$ctid" "$settings" /root/shellbook-install.env --perms 0600
pct exec "$ctid" -- bash -c 'trap "rm -f /root/shellbook-install.env" EXIT; source /root/shellbook-install.env; bash /root/shellbook-install.sh'
+8
View File
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
# Input: pveam available output. Filter architecture BEFORE sorting versions.
pick_template() {
local arch=$1
[[ $arch =~ ^[a-z0-9]+$ ]] || return 1
awk -v arch="$arch" '$NF ~ ("^debian-13-standard_.*_" arch "\\.tar\\.(zst|xz|gz)$") { print $NF }' |
sort -V | tail -n 1
}
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source=deploy/proxmox/template-picker.sh
source "$(dirname "${BASH_SOURCE[0]}")/../proxmox/template-picker.sh"
fixtures='system debian-13-standard_13.0-1_amd64.tar.zst
system debian-13-standard_13.0-9_arm64.tar.zst
system debian-12-standard_12.9-1_amd64.tar.zst
system debian-13-standard_13.0-2_amd64.tar.zst
system debian-13-standard_13.0-10_amd64.tar.zst'
[[ $(pick_template amd64 <<< "$fixtures") == debian-13-standard_13.0-10_amd64.tar.zst ]]
[[ $(pick_template arm64 <<< "$fixtures") == debian-13-standard_13.0-9_arm64.tar.zst ]]
[[ -z $(pick_template riscv64 <<< "$fixtures") ]]
[[ -z $(pick_template amd64 <<< 'system debian-13-standard_13.0-99_arm64.tar.zst') ]]
if pick_template '.*' <<< "$fixtures"; then exit 1; fi
printf '5 template picker checks passed\n'
+21
View File
@@ -0,0 +1,21 @@
# Traefik v3 file-provider example. Replace host, CT IP and allowed networks.
http:
routers:
shellbook:
rule: Host(`shellbook.example.net`)
entryPoints: [websecure]
tls: {} # Configure the certificate resolver on the entrypoint or here.
service: shellbook
middlewares: [shellbook-networks]
services:
shellbook:
loadBalancer:
servers:
- url: http://192.168.1.50:8080
# WebSocket upgrades work without extra headers/middleware.
middlewares:
shellbook-networks:
ipAllowList:
sourceRange:
- 192.168.1.0/24
- 100.64.0.0/10
+6
View File
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -euo pipefail
[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; }
runuser -u shellbook -- bash -c 'cd /opt/shellbook; git pull --ff-only; pnpm install --frozen-lockfile; pnpm build'
systemctl restart shellbook
systemctl --no-pager status shellbook