feat(diigo-cache): production deployment with tailscale, systemd, and nix
diigo binds the session cookie to the IP it was issued to, so a Cloudflare deployment can't use a cookie minted in the browser. Run diigo-cache and webapp in one workerd on bldbox instead. The flake bundles each worker with wrangler's dry run, so the modules match what `wrangler deploy` ships, and generates the workerd config from the bundles. workerd comes from nixpkgs' wrangler, the only packaged one. A smoke check serves the home page and mints a webapp instance, which covers the WEBAPP binding and Durable Object storage. The service is a system unit with DynamicUser, so the webapp state holding the cookie isn't readable by processes running as the user. It listens on loopback behind tailscale serve: HTTPS, which PasswordCredential needs, at the tailnet name only. Operator docs in doc/; unsettled points recorded as IBIS issues. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9c3eaa9819
commit
857816166e
14 files changed
+580
-2
No files matched your search
@@ -0,0 +1,60 @@
|
||||
# diigo-cache and the webapp it reads diigo through, in one workerd.
|
||||
#
|
||||
# The listening socket has no address here: systemd hands it in
|
||||
# (-S http=3), so holding the socket is the authority to reach the cache.
|
||||
# State directories likewise come from the command line:
|
||||
# -d cache-state=<dir> -d webapp-state=<dir>.
|
||||
#
|
||||
# Durable Object unique keys follow miniflare's `<script>-<class>`, so a
|
||||
# `wrangler dev` state directory names its objects the same way.
|
||||
using Workerd = import "/workerd/workerd.capnp";
|
||||
|
||||
const config :Workerd.Config = (
|
||||
services = [
|
||||
(name = "diigo-cache", worker = .cache),
|
||||
(name = "webapp", worker = .webapp),
|
||||
(name = "nowhere", network = (allow = [])),
|
||||
(name = "cache-state", disk = (path = "cache-state", writable = true)),
|
||||
(name = "webapp-state", disk = (path = "webapp-state", writable = true)),
|
||||
],
|
||||
sockets = [
|
||||
(name = "http", http = (), service = "diigo-cache"),
|
||||
],
|
||||
# one JSON object per line, console.* included: journald keeps an
|
||||
# object in one entry rather than one entry per line of it
|
||||
structuredLogging = true,
|
||||
);
|
||||
|
||||
const cache :Workerd.Worker = (
|
||||
compatibilityDate = "@CACHE_DATE@",
|
||||
modules = [
|
||||
@CACHE_MODULES@
|
||||
],
|
||||
durableObjectNamespaces = [
|
||||
(className = "DiigoCache", uniqueKey = "diigo-cache-DiigoCache", enableSql = true),
|
||||
(className = "PublicCache", uniqueKey = "diigo-cache-PublicCache", enableSql = true),
|
||||
],
|
||||
durableObjectStorage = (localDisk = "cache-state"),
|
||||
bindings = [
|
||||
(name = "CACHE_DO", durableObjectNamespace = "DiigoCache"),
|
||||
(name = "PUBLIC_DO", durableObjectNamespace = "PublicCache"),
|
||||
(name = "WEBAPP", service = (name = "webapp", entrypoint = "WebAppFactory")),
|
||||
],
|
||||
# diigo is reached only through WEBAPP
|
||||
globalOutbound = "nowhere",
|
||||
);
|
||||
|
||||
const webapp :Workerd.Worker = (
|
||||
compatibilityDate = "@WEBAPP_DATE@",
|
||||
modules = [
|
||||
@WEBAPP_MODULES@
|
||||
],
|
||||
durableObjectNamespaces = [
|
||||
(className = "WebAppDo", uniqueKey = "webapp-WebAppDo", enableSql = true),
|
||||
],
|
||||
durableObjectStorage = (localDisk = "webapp-state"),
|
||||
bindings = [
|
||||
(name = "WEBAPP_DO", durableObjectNamespace = "WebAppDo"),
|
||||
],
|
||||
# globalOutbound: workerd's default "internet", public addresses only
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
[Unit]
|
||||
Description=diigo-cache and webapp (workerd)
|
||||
Requires=diigo-cache.socket
|
||||
|
||||
[Service]
|
||||
# the profile path survives upgrades; see doc/install.md
|
||||
ExecStart=/nix/var/nix/profiles/diigo-cache/bin/diigo-cache-serve -S http=3 -d cache-state=/var/lib/diigo-cache/cache -d webapp-state=/var/lib/diigo-cache/webapp
|
||||
DynamicUser=yes
|
||||
StateDirectory=diigo-cache diigo-cache/cache diigo-cache/webapp
|
||||
StateDirectoryMode=0700
|
||||
# webapp fetches diigo.com; nothing else needs the network
|
||||
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
|
||||
ProtectSystem=strict
|
||||
ProtectHome=yes
|
||||
PrivateTmp=yes
|
||||
PrivateDevices=yes
|
||||
ProtectKernelTunables=yes
|
||||
ProtectKernelModules=yes
|
||||
ProtectControlGroups=yes
|
||||
RestrictNamespaces=yes
|
||||
LockPersonality=yes
|
||||
CapabilityBoundingSet=
|
||||
SystemCallFilter=@system-service
|
||||
# V8 uses memory protection keys; without these, SIGSYS at startup
|
||||
SystemCallFilter=pkey_alloc pkey_mprotect pkey_free
|
||||
# not MemoryDenyWriteExecute: V8's JIT needs W+X
|
||||
@@ -0,0 +1,9 @@
|
||||
[Unit]
|
||||
Description=diigo-cache: loopback port behind tailscale serve
|
||||
|
||||
[Socket]
|
||||
# tailscale serve proxies https://bldbox.<tailnet>.ts.net/ here
|
||||
ListenStream=127.0.0.1:8790
|
||||
|
||||
[Install]
|
||||
WantedBy=sockets.target
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# Fill config.capnp.in from `wrangler deploy --dry-run` outdirs.
|
||||
# usage: gen-config.sh <dist> <template> <cache-date> <webapp-date> > config.capnp
|
||||
# <dist>/cache and <dist>/webapp each hold index.js plus the text modules
|
||||
# wrangler.jsonc's rules name (*.sql, capnweb's browser build); embed
|
||||
# paths are relative to the config, which sits beside them.
|
||||
set -euo pipefail
|
||||
dist=$1 template=$2 cache_date=$3 webapp_date=$4
|
||||
|
||||
modules() {
|
||||
local dir=$1
|
||||
if [[ ! -f $dist/$dir/index.js ]]; then
|
||||
echo "gen-config: $dist/$dir/index.js: missing" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo " (name = \"index.js\", esModule = embed \"$dir/index.js\"),"
|
||||
for f in "$dist/$dir"/*; do
|
||||
local name=${f##*/}
|
||||
case $name in
|
||||
index.js | README.md | *.map) ;;
|
||||
*.sql | *-index.js) echo " (name = \"$name\", text = embed \"$dir/$name\")," ;;
|
||||
*)
|
||||
echo "gen-config: $dir/$name: no module type rule" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
cache_modules=$(modules cache)
|
||||
webapp_modules=$(modules webapp)
|
||||
while IFS= read -r line; do
|
||||
case $line in
|
||||
@CACHE_MODULES@) echo "$cache_modules" ;;
|
||||
@WEBAPP_MODULES@) echo "$webapp_modules" ;;
|
||||
*)
|
||||
line=${line//@CACHE_DATE@/$cache_date}
|
||||
echo "${line//@WEBAPP_DATE@/$webapp_date}"
|
||||
;;
|
||||
esac
|
||||
done <"$template"
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
# Serve a built package under the service unit's syscall filter, as a
|
||||
# transient user unit, on a scratch port and state; check the home page.
|
||||
# The flake's smoke check can't: the nix sandbox has no systemd.
|
||||
# usage: probe-unit.sh <package out path>
|
||||
set -euo pipefail
|
||||
out=$1
|
||||
state=$(mktemp -d)
|
||||
trap 'systemctl --user stop diigo-cache-probe 2>/dev/null; rm -rf "$state"' EXIT
|
||||
mkdir "$state/cache" "$state/webapp"
|
||||
|
||||
props=()
|
||||
while IFS= read -r line; do props+=(-p "$line"); done \
|
||||
< <(grep '^SystemCallFilter=' "$out/lib/systemd/system/diigo-cache.service")
|
||||
|
||||
systemd-run --user -q --collect --unit=diigo-cache-probe "${props[@]}" \
|
||||
"$out/bin/diigo-cache-serve" -s http=127.0.0.1:18791 \
|
||||
-d cache-state="$state/cache" -d webapp-state="$state/webapp"
|
||||
for _ in $(seq 30); do
|
||||
if curl -sf -m 2 http://127.0.0.1:18791/ | grep -q _connect; then
|
||||
echo "probe: ok"
|
||||
exit 0
|
||||
fi
|
||||
sleep 0.3
|
||||
done
|
||||
echo "probe: no home page; see journalctl --user -u diigo-cache-probe and journalctl -k | grep syscall=" >&2
|
||||
exit 1
|
||||
Reference in new issue
Block a user