From 857816166ecd696a73536898ad0033e6226832de Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 14 Oct 2026 23:02:50 -0500 Subject: [PATCH] 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) --- .gitignore | 1 + diigo-cache/CONTRIBUTING.md | 2 +- diigo-cache/README.md | 2 +- diigo-cache/doc/README.md | 19 +++++ diigo-cache/doc/install.md | 81 +++++++++++++++++++ diigo-cache/doc/open-issues.md | 121 ++++++++++++++++++++++++++++ diigo-cache/doc/operate.md | 48 +++++++++++ diigo-cache/flake.lock | 27 +++++++ diigo-cache/flake.nix | 118 +++++++++++++++++++++++++++ diigo-cache/nix/config.capnp.in | 60 ++++++++++++++ diigo-cache/nix/diigo-cache.service | 26 ++++++ diigo-cache/nix/diigo-cache.socket | 9 +++ diigo-cache/nix/gen-config.sh | 41 ++++++++++ diigo-cache/nix/probe-unit.sh | 27 +++++++ 14 files changed, 580 insertions(+), 2 deletions(-) create mode 100644 diigo-cache/doc/README.md create mode 100644 diigo-cache/doc/install.md create mode 100644 diigo-cache/doc/open-issues.md create mode 100644 diigo-cache/doc/operate.md create mode 100644 diigo-cache/flake.lock create mode 100644 diigo-cache/flake.nix create mode 100644 diigo-cache/nix/config.capnp.in create mode 100644 diigo-cache/nix/diigo-cache.service create mode 100644 diigo-cache/nix/diigo-cache.socket create mode 100644 diigo-cache/nix/gen-config.sh create mode 100755 diigo-cache/nix/probe-unit.sh diff --git a/.gitignore b/.gitignore index d5c5b73..69b905d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ dist/ worker-configuration.d.ts emdash-env.d.ts# diigo exports: personal data export/ +result diff --git a/diigo-cache/CONTRIBUTING.md b/diigo-cache/CONTRIBUTING.md index 81d955d..c322fd7 100644 --- a/diigo-cache/CONTRIBUTING.md +++ b/diigo-cache/CONTRIBUTING.md @@ -69,4 +69,4 @@ Check a filled cache against a diigo RSS export: `node scripts/compare-export.ts ## Deploy -Not deployed yet; the order matters (goal 5 in [the capsite checklist](../capsite-checklist.md)): webapp first (`cd ../webapp && npx wrangler deploy`), then diigo-cache, after rate-limiting `/api` (above). +On bldbox, tailnet only, via [the flake](flake.nix): see [the operator docs](doc/README.md). Cloudflare is out while diigo binds the cookie to the IP it was issued to ([open issue 1](doc/open-issues.md#-1-how-does-diigo-see-the-cookie-come-from-the-ip-it-was-issued-to)). diff --git a/diigo-cache/README.md b/diigo-cache/README.md index 8a6b1b7..4620ad7 100644 --- a/diigo-cache/README.md +++ b/diigo-cache/README.md @@ -23,4 +23,4 @@ Then, in the browser: - **Ids are capabilities:** the cache id is a bearer key you keep in your password manager. It rides URL fragments and POST bodies, never a path or query string, so it stays out of logs and history. - **Cheap to read:** pages come from Durable Object SQLite storage, and a fill job in the background fetches each diigo page once. -Design, tests, and deploying: [CONTRIBUTING](CONTRIBUTING.md). +Design and tests: [CONTRIBUTING](CONTRIBUTING.md). Running it on bldbox: [the operator docs](doc/README.md). diff --git a/diigo-cache/doc/README.md b/diigo-cache/doc/README.md new file mode 100644 index 0000000..105f7c9 --- /dev/null +++ b/diigo-cache/doc/README.md @@ -0,0 +1,19 @@ +# Operating diigo-cache on bldbox + +diigo-cache and [webapp](../../webapp/README.md) run together in one workerd on bldbox, reachable only over the tailnet at `https://bldbox.tail09e44.ts.net/`. Cloudflare doesn't work: diigo binds the session cookie to the IP address it was issued to, and Cloudflare's egress IPs aren't that address (see [open issue 1](open-issues.md#-1-how-does-diigo-see-the-cookie-come-from-the-ip-it-was-issued-to)). + +``` +browser on the tailnet + → tailscale serve (HTTPS, bldbox.tail09e44.ts.net:443) + → diigo-cache.socket (127.0.0.1:8790) + → diigo-cache.service: workerd, DynamicUser + diigo-cache worker → state in /var/lib/diigo-cache/cache + webapp worker → state in /var/lib/diigo-cache/webapp (holds the cookie) + → diigo.com +``` + +- [Install, upgrade, roll back](install.md) +- [Day to day: create a cache, logs, backups](operate.md) +- [Open issues](open-issues.md), in IBIS style + +The build is [the flake](../flake.nix): wrangler bundles each worker as `wrangler deploy` would, [gen-config.sh](../nix/gen-config.sh) fills [the workerd config](../nix/config.capnp.in), and the [socket](../nix/diigo-cache.socket) and [service](../nix/diigo-cache.service) units ship in the package. workerd comes from nixpkgs' `wrangler`, which bundles a patched one; nixpkgs has no standalone `workerd`. diff --git a/diigo-cache/doc/install.md b/diigo-cache/doc/install.md new file mode 100644 index 0000000..2caca5a --- /dev/null +++ b/diigo-cache/doc/install.md @@ -0,0 +1,81 @@ +# Install, upgrade, roll back + +Everything below runs on bldbox. Neither unit has been run under the system manager yet; the flake's smoke check runs the same build under workerd directly. + +## Once: tailnet HTTPS + +In the [Tailscale admin console DNS page](https://login.tailscale.com/admin/dns), enable MagicDNS (on) and **HTTPS Certificates** (off as of 2026-10-14: `tailscale status --json | jq .CertDomains` is `null`). HTTPS matters: the "save to password manager" button uses `PasswordCredential`, which browsers offer only in a secure context. + +## Build and check + +```sh +cd ~/oros-bots/capsite/diigo-cache +nix flake check # builds, then serves the home page and mints a webapp instance +out=$(nix build --no-link --print-out-paths .#) +nix/probe-unit.sh "$out" # serves it under the unit's syscall filter: "probe: ok" +``` + +The probe catches what the smoke check can't: the nix sandbox has no systemd, so it doesn't apply the unit's `SystemCallFilter`. The flake only sees files git tracks: `git add` new files before building. + +## Install + +The service runs whatever `/nix/var/nix/profiles/diigo-cache` points at, so the profile is also the GC root and the rollback history. + +nix-env is spelled out in full because sudo's `secure_path` leaves out nix's profile. + +```sh +sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --set "$out" +sudo systemctl link /nix/var/nix/profiles/diigo-cache/lib/systemd/system/diigo-cache.{socket,service} +sudo systemctl enable --now diigo-cache.socket +sudo tailscale serve --bg --https=443 http://127.0.0.1:8790 +``` + +`tailscale serve --bg` persists across reboots in tailscaled's state; `tailscale serve status` shows it. + +## Verify + +```sh +curl -sf https://bldbox.tail09e44.ts.net/ | grep -c _connect # 1+; first request starts workerd +systemctl status diigo-cache.service +``` + +On a 502, make sure `tailscale serve` finds something on 8790: `curl -sI http://127.0.0.1:8790/`. (The cache also answers 502 when it can't load a page from diigo; its body then says `could not load page`.) If workerd crashes on start, systemd stops retrying, then fails the socket too (`Result: service-start-limit-hit` in `systemctl status diigo-cache.socket`). After fixing the cause: + +```sh +sudo systemctl reset-failed diigo-cache.socket diigo-cache.service +sudo systemctl start diigo-cache.socket +``` + +From another tailnet device, open `https://bldbox.tail09e44.ts.net/`. From off the tailnet, the name doesn't resolve and `100.89.146.23` doesn't route. + +## Upgrade + +```sh +out=$(nix build --no-link --print-out-paths .#) +nix/probe-unit.sh "$out" +sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --set "$out" +sudo systemctl daemon-reload # in case the units changed +sudo systemctl restart diigo-cache.service +``` + +Durable Object state outlives the code: an upgrade whose schema change existing state can't read is a breaking change (see [capsite AGENTS.md](../../AGENTS.md#durable-state-be-loud-and-conservative-about-breaking-changes)). Back up first ([operate](operate.md#backups)). + +## Roll back + +```sh +sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --list-generations +sudo /nix/var/nix/profiles/default/bin/nix-env -p /nix/var/nix/profiles/diigo-cache --rollback +sudo systemctl restart diigo-cache.service +``` + +## Update nixpkgs + +`nix flake update` moves wrangler, and with it workerd. The flake names workerd's version (`version` in [flake.nix](../flake.nix)); if nixpkgs' wrangler bundles a different one, the build fails with `no workerd `. Set it to the bundled version, which must be no older than the workers' `compatibility_date`. + +## Remove + +```sh +sudo tailscale serve --https=443 off +sudo systemctl disable --now diigo-cache.socket diigo-cache.service +sudo rm /nix/var/nix/profiles/diigo-cache* # state in /var/lib/private/diigo-cache stays +``` diff --git a/diigo-cache/doc/open-issues.md b/diigo-cache/doc/open-issues.md new file mode 100644 index 0000000..33a14b3 --- /dev/null +++ b/diigo-cache/doc/open-issues.md @@ -0,0 +1,121 @@ +# Open issues + +In [IBIS](https://github.com/dckc/vattr97/wiki/IBIS) notation: `?` issue, `?!` resolved, `?~` open with a fallback; `:` position, `:+` accepted, `:-` rejected, `:~+` fallback; `+` pro, `-` con; `!` to-do; `.` comment. Append; don't renumber. + +### ?! 1. How does diigo see the cookie come from the IP it was issued to? + +``` +?! 1. How does diigo see the cookie come from the IP it was issued to? + . observed: minted from the browser, the cookie failed from Cloudflare + . unverified that IP is the only binding; the mint sends user-agent + "Mozilla/5.0", not the browser's, and that works in dev + : 1a. log in to diigo from a browser that shares bldbox's public IP + (same LAN, same NAT) + + no setup if that's where the browser already is + - breaks when the browser roams (phone, laptop away from home) + ! compare: curl https://ifconfig.me on bldbox and in the browser + : 1b. advertise bldbox as a tailnet exit node; route the browser through + it while logging in to diigo and copying the cookie + + bldbox's IP by construction, from anywhere on the tailnet + - bldbox isn't an exit node (ExitNodeOption false, 2026-10-14); + needs `tailscale set --advertise-exit-node` and admin approval + :+ 1c. run the browser on bldbox + + worked 2026-10-14: a cookie copied from Brave's devtools on bldbox + minted, wired public, and filled 168 pages + :- 1d. deploy to Cloudflare + - its egress IPs aren't the browser's (the failure that prompted this) +``` + +### ?~ 2. How is diigo-cache reachable only from the tailnet? + +``` +?~ 2. How is diigo-cache reachable only from the tailnet? + :~+ 2a. workerd on 127.0.0.1:8790; tailscale serve proxies + https://bldbox.tail09e44.ts.net/ to it + + HTTPS with a real cert: PasswordCredential needs a secure context + + tailscaled owns the tailnet address; no start-order dependency + - needs HTTPS certificates on in the tailnet (off as of 2026-10-14) + ! enable in the admin console (install.md) + - every local user and process on bldbox reaches 127.0.0.1:8790 (see 3) + + the cache id is still needed for anything private + : 2b. listen on 100.89.146.23 directly (ListenStream, FreeBind=yes) + - plain HTTP: the password-manager button fails + : 2c. tailscale serve to a unix socket only it can open + + no loopback port + - unverified that tailscale 1.90.9 serve takes a unix socket target + ? 2d. Which tailnet devices may reach it? + . today: every device on the tailnet (m5x, penguin, pixel-7, tiny-jwc) + : 2d-i. restrict to some devices with a tailnet ACL grant on bldbox:443 +``` + +### ?~ 3. What account does the service run as? + +``` +?~ 3. What account does the service run as? + :~+ 3a. a system unit with DynamicUser=yes + + the cookie's state is 0700 to a uid nothing else runs as; agents + and bots running as connolly can't read it + + full sandboxing (systemd-analyze security: 2.2 OK) + - install and upgrade need sudo + : 3b. a user unit under connolly, with lingering + + no sudo for upgrade; a ~/.local profile + - the cookie sits in connolly's files, readable by every process + connolly runs, Discord and opencode bots included + - Ubuntu's AppArmor userns restriction weakens user-manager sandboxing + - tailscale serve needs root (or `tailscale set --operator`) anyway +``` + +### ? 4. Does /api need a rate limit on the tailnet? + +``` +? 4. Does /api need a rate limit on the tailnet? + . CONTRIBUTING asks for one "before exposing a deployment" + :~+ 4a. not while only tailnet devices reach it (see 2) + : 4b. a rate limit in front of workerd before any funnel (see 5) +``` + +### ? 5. How does the weekly roundup read the public page? + +``` +? 5. How does the weekly roundup read the public page? + . the madmode blog build needs /public/; where does it run? + : 5a. build on a tailnet device + : 5b. join CI to the tailnet (Tailscale's GitHub Action) + : 5c. tailscale funnel just /public/ to the internet + + public pages are public by design + - exposes a guessable URL; needs 4b +``` + +### ? 6. How is state backed up? + +``` +? 6. How is state backed up? + :~+ 6a. by hand, service stopped (operate.md) + : 6b. a systemd timer + - backups hold the cookie: where do they go, encrypted how? + : 6c. none: the cache is a cache + - the ids people saved point at objects that would be gone +``` + +### ? 7. How does a cache get a fresh cookie? + +``` +? 7. How does a cache get a fresh cookie? + . a wired cache refuses a different webapp id + :~+ 7a. mint a fresh webapp instance and wire a new cache + - a new cache id to save + + wiring public re-registers the username, so the public URL stays + : 7b. let webapp replace the cookie in an existing instance + + cache ids stay put +``` + +### ? 8. Should `wrangler dev` state move to bldbox? + +``` +? 8. Should `wrangler dev` state move to bldbox? + . Durable Object unique keys follow miniflare's `