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:
dckcandClaude Opus 5.5 committed 2026-10-14 23:12:47 -05:00
1 parent 9c3eaa9819
commit 857816166e
14 files changed
+580 -2

No files matched your search

+19
View File
@@ -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`.
+81
View File
@@ -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 <version>`. 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
```
+121
View File
@@ -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/<username>; 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 `<script>-<class>`,
so the per-object files may carry over; untested
:~+ 8a. start fresh on bldbox
+ dev cookies were minted for wherever dev ran (see 1)
: 8b. copy .wrangler/state/v3/do/<script>-<class>/ into the state dirs
```
+48
View File
@@ -0,0 +1,48 @@
# Day to day
## Create a cache
As in [the README](../README.md#getting-started), at `https://bldbox.tail09e44.ts.net/` instead of localhost: mint, wire, save the ids, fill. Copy the cookie from a browser running on bldbox: diigo accepts it only from the IP it was issued to (see [issue 1](open-issues.md#-1-how-does-diigo-see-the-cookie-come-from-the-ip-it-was-issued-to)).
The public page is then `https://bldbox.tail09e44.ts.net/public/<diigo username>?days=7`, on the tailnet only.
## Logs
```sh
journalctl -u diigo-cache.service -f -o cat |
jq -Rr 'fromjson? | "\(.timestamp / 1000 | todate) \(.level) \(.message | gsub("\\s*\\n\\s*"; " "))"'
```
`fromjson?` skips lines that aren't JSON: systemd's own ("Started …") and entries from before `structuredLogging`.
workerd runs with `structuredLogging`: each entry is one line of JSON, `{timestamp, level, message}`. `message` is the logged object as `console.log` prints it, not JSON: on Cloudflare, Workers Logs indexes the fields of a logged object, so the workers log objects rather than strings.
workerd logs the workers' `console.*` output: `{event: "cache", kind, page, outcome}` per page read, `{event: "fill", state, …}` per fill step, `{event: "mint"}`. Ids show only as fingerprints; the cookie never appears.
## State
With `DynamicUser=yes`, systemd keeps the state in `/var/lib/private/diigo-cache/` (`/var/lib/diigo-cache` is a symlink to it), mode 0700, owned by a uid that exists only while the service runs:
| Dir | Holds | Secret? |
| :--- | :--- | :--- |
| `cache/diigo-cache-DiigoCache/` | private caches: every bookmark | yes |
| `cache/diigo-cache-PublicCache/` | public caches and the username index | no |
| `webapp/webapp-WebAppDo/` | webapp instances: the diigo cookie | yes, a credential |
Each Durable Object is one `<id hash>.sqlite`. Read one with the service stopped, or copy it first: `sudo sqlite3 -readonly <file> .tables`.
## Backups
The private caches are refillable from diigo; the cookie can be re-minted. What's lost with the state is the binding from the ids people saved to these objects. Backups hold the cookie, so treat them as secret.
```sh
sudo systemctl stop diigo-cache.socket diigo-cache.service # the socket would restart it
sudo tar -C /var/lib/private -czf diigo-cache-$(date +%F).tgz diigo-cache
sudo systemctl start diigo-cache.socket
```
No backup is scheduled (see [open issue 6](open-issues.md#-6-how-is-state-backed-up)).
## The cookie expires
When fills log `{event: "fill", state: "failed"}` with a diigo auth error, mint a fresh webapp instance and wire a new cache. A wired cache refuses a different webapp id (`setup` in [cache.ts](../src/cache.ts)) (see [open issue 7](open-issues.md#-7-how-does-a-cache-get-a-fresh-cookie)).