pinnace 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +15 -0
  3. package/dist/car/car-build.d.ts +26 -0
  4. package/dist/car/car-build.d.ts.map +1 -0
  5. package/dist/car/car-build.js +113 -0
  6. package/dist/car/car-build.js.map +1 -0
  7. package/dist/ci/ci-emit.d.ts +116 -0
  8. package/dist/ci/ci-emit.d.ts.map +1 -0
  9. package/dist/ci/ci-emit.js +190 -0
  10. package/dist/ci/ci-emit.js.map +1 -0
  11. package/dist/cli/bin.d.ts +3 -0
  12. package/dist/cli/bin.d.ts.map +1 -0
  13. package/dist/cli/bin.js +17 -0
  14. package/dist/cli/bin.js.map +1 -0
  15. package/dist/cli/run.d.ts +78 -0
  16. package/dist/cli/run.d.ts.map +1 -0
  17. package/dist/cli/run.js +506 -0
  18. package/dist/cli/run.js.map +1 -0
  19. package/dist/config/config-resolution.d.ts +186 -0
  20. package/dist/config/config-resolution.d.ts.map +1 -0
  21. package/dist/config/config-resolution.js +137 -0
  22. package/dist/config/config-resolution.js.map +1 -0
  23. package/dist/deploy/deploy.d.ts +121 -0
  24. package/dist/deploy/deploy.d.ts.map +1 -0
  25. package/dist/deploy/deploy.js +150 -0
  26. package/dist/deploy/deploy.js.map +1 -0
  27. package/dist/derive/ipns-key-derivation.d.ts +37 -0
  28. package/dist/derive/ipns-key-derivation.d.ts.map +1 -0
  29. package/dist/derive/ipns-key-derivation.js +130 -0
  30. package/dist/derive/ipns-key-derivation.js.map +1 -0
  31. package/dist/index.d.ts +27 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +29 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/node/node-commands.d.ts +115 -0
  36. package/dist/node/node-commands.d.ts.map +1 -0
  37. package/dist/node/node-commands.js +227 -0
  38. package/dist/node/node-commands.js.map +1 -0
  39. package/dist/provision/cloud-init.d.ts +137 -0
  40. package/dist/provision/cloud-init.d.ts.map +1 -0
  41. package/dist/provision/cloud-init.js +390 -0
  42. package/dist/provision/cloud-init.js.map +1 -0
  43. package/dist/publisher/key-import.d.ts +105 -0
  44. package/dist/publisher/key-import.d.ts.map +1 -0
  45. package/dist/publisher/key-import.js +76 -0
  46. package/dist/publisher/key-import.js.map +1 -0
  47. package/dist/publisher/record-sequence.d.ts +90 -0
  48. package/dist/publisher/record-sequence.d.ts.map +1 -0
  49. package/dist/publisher/record-sequence.js +250 -0
  50. package/dist/publisher/record-sequence.js.map +1 -0
  51. package/dist/rpc/kubo-rpc-client.d.ts +154 -0
  52. package/dist/rpc/kubo-rpc-client.d.ts.map +1 -0
  53. package/dist/rpc/kubo-rpc-client.js +207 -0
  54. package/dist/rpc/kubo-rpc-client.js.map +1 -0
  55. package/dist/rpc/mock-kubo.d.ts +87 -0
  56. package/dist/rpc/mock-kubo.d.ts.map +1 -0
  57. package/dist/rpc/mock-kubo.js +129 -0
  58. package/dist/rpc/mock-kubo.js.map +1 -0
  59. package/dist/site/site-management.d.ts +126 -0
  60. package/dist/site/site-management.d.ts.map +1 -0
  61. package/dist/site/site-management.js +123 -0
  62. package/dist/site/site-management.js.map +1 -0
  63. package/dist/status/status-report.d.ts +145 -0
  64. package/dist/status/status-report.d.ts.map +1 -0
  65. package/dist/status/status-report.js +192 -0
  66. package/dist/status/status-report.js.map +1 -0
  67. package/package.json +45 -2
  68. package/src/car/car-build.ts +135 -0
  69. package/src/ci/ci-emit.ts +284 -0
  70. package/src/cli/bin.ts +20 -0
  71. package/src/cli/run.ts +654 -0
  72. package/src/config/config-resolution.ts +264 -0
  73. package/src/deploy/deploy.ts +250 -0
  74. package/src/derive/ipns-key-derivation.ts +173 -0
  75. package/src/index.ts +146 -0
  76. package/src/node/node-commands.ts +391 -0
  77. package/src/provision/cloud-init.ts +551 -0
  78. package/src/publisher/key-import.ts +141 -0
  79. package/src/publisher/record-sequence.ts +336 -0
  80. package/src/rpc/kubo-rpc-client.ts +281 -0
  81. package/src/rpc/mock-kubo.ts +194 -0
  82. package/src/site/site-management.ts +241 -0
  83. package/src/status/status-report.ts +291 -0
@@ -0,0 +1,551 @@
1
+ /**
2
+ * The **cloud-init generator** behind the `HostProvider` seam (CONTEXT.md `host
3
+ * provider seam`). `provision` asks the core for a ready-to-paste cloud-init:
4
+ * the host-specific YAML that stands up a hardened Kubo node. Hetzner is the
5
+ * FIRST (and, in v1, only) implementation; other hosts are Out of Scope but
6
+ * slot in behind this same seam later (spec "Out of Scope"; user stories 1, 21).
7
+ *
8
+ * This module is PURE: {@link provision} takes a plain host/site/role config
9
+ * and returns the cloud-init file (path + contents) as data. It does NOT touch
10
+ * the filesystem, the network, or SSH. The CLI wrapper (`provision` command)
11
+ * is what hands the YAML to the operator. Keeping the emit pure lets it be
12
+ * snapshot-tested as a string with no fixtures (test-first policy) and reused
13
+ * as a TypeScript API (CONTEXT.md `core vs cli`).
14
+ *
15
+ * SEAM DISCIPLINE (user story 21): only host-specific provisioning lives behind
16
+ * `HostProvider`. Deploy/publish/status speak ONLY Kubo RPC and must not import
17
+ * this module. Adding a host later touches nothing but this file + the
18
+ * {@link HOST_PROVIDERS} registry. (The dependency direction is one-way: this
19
+ * module may reference the shared `HostRole` type, but nothing in deploy/
20
+ * publish/status references this module.)
21
+ *
22
+ * KEY CHANGE FROM THE REFERENCE (docs/adr/0002-on-box-agent-boundary.md): the
23
+ * emitted cloud-init does NOT bake the reference's four bash scripts
24
+ * (`ipfs-warm.sh`, `ipfs-ipns-publish.sh`, `ipfs-ipns-mirror.sh`,
25
+ * `ipfs-status.sh`). Instead it INSTALLS the `pinnace` binary on the box and
26
+ * schedules the on-box subcommands (`pinnace node republish|mirror|warm|
27
+ * status`) on role-gated systemd timers. One codebase runs both as the
28
+ * operator's client and as the box's periodic agent, so the record/warm/mirror/
29
+ * status logic has a single implementation (no bash/TS drift). Kubo still owns
30
+ * pinning (`dag/import --pin-roots`) + provider-record freshness
31
+ * (`Provide.Interval`); the pinnace timers own ONLY IPNS republish/export,
32
+ * replica mirror/fallback, gateway warm, and status. The reference bash is used
33
+ * here as the BEHAVIOURAL SPEC of what each timer must do, not as code to emit.
34
+ *
35
+ * INVARIANTS ENCODED (ported, not copied, from `~/searches/ipfs-hetzner/`):
36
+ * - Kubo as a hardened systemd unit (dedicated `ipfs` user, sandboxing).
37
+ * - ufw opens 4001 TCP+UDP (swarm, so gateways can dial) + 80/443 (Caddy/ACME);
38
+ * the RPC API (5001) and gateway (8080) are bound to localhost and NEVER
39
+ * opened in the firewall.
40
+ * - discoverability: AcceleratedDHTClient + `Routing.Type auto` + reprovide
41
+ * (interval well under the record-expiry window, strategy `all`).
42
+ * - Caddy HTTPS reverse proxy for the RPC API (auto TLS), which forwards the
43
+ * operator's bearer header straight through to the token-guarded Kubo API.
44
+ */
45
+ import type {HostRole} from '../config/config-resolution.js';
46
+
47
+ /** The hosts pinnace can provision for. v1 = Hetzner ONLY. */
48
+ export type HostName = 'hetzner';
49
+
50
+ /** The hosts, in a stable order (help text / iteration / validation). */
51
+ export const HOST_PROVIDERS: readonly HostName[] = ['hetzner'];
52
+
53
+ /**
54
+ * The per-box inputs the generator injects into the cloud-init (the values
55
+ * `make-cloud-init.sh` used to `sed` in). Everything that varies per box lives
56
+ * here; the hardened invariants are baked into the template.
57
+ */
58
+ export interface ProvisionInput {
59
+ /** Which host to provision for (v1: `hetzner`). */
60
+ host: HostName;
61
+ /** DNS A-record pointed at this box, used for the HTTPS RPC API vhost. */
62
+ apiDomain: string;
63
+ /** Optional read-only status dashboard vhost (also serves exported records). */
64
+ dashboardDomain?: string;
65
+ /** Email for Let's Encrypt (ACME) certificate issuance. */
66
+ acmeEmail: string;
67
+ /**
68
+ * The long random bearer token guarding the RPC API. Kubo enforces it via
69
+ * `API.Authorizations` even on localhost; Caddy forwards the header through.
70
+ */
71
+ bearerToken: string;
72
+ /**
73
+ * This box's role. `publisher` holds IPNS keys and signs/exports records;
74
+ * `replica` is keyless and mirrors the publisher's records. Written to the
75
+ * env file as `NODE_ROLE`, which the `pinnace node` timers self-gate on
76
+ * (republish only on publisher, mirror only on replica), so scheduling all
77
+ * timers on every box is safe.
78
+ */
79
+ role: HostRole;
80
+ /**
81
+ * REPLICA ONLY: base URL to fetch the publisher's exported signed records
82
+ * from (the publisher's dashboard vhost). Ignored for publishers.
83
+ */
84
+ publisherEndpoint?: string;
85
+ /**
86
+ * Browser origins allowed to call the RPC API (CORS). Space-separated in the
87
+ * env file. Defaults to the dashboard domain's URL when a dashboard is set.
88
+ */
89
+ corsOrigins?: string;
90
+ /**
91
+ * Public gateways to warm through, each a template containing `{cid}`.
92
+ * Defaults to a sensible dweb.link/ipfs.io set. `.eth` MFS names are ALSO
93
+ * warmed via eth.limo automatically by `pinnace node warm`.
94
+ */
95
+ gateways?: string[];
96
+ /** The Kubo version to install. Defaults to a pinned known-good release. */
97
+ kuboVersion?: string;
98
+ /** The MFS directory sites live under. Defaults to `/sites`. */
99
+ sitesDir?: string;
100
+ }
101
+
102
+ /** A single emitted file: where it goes + what it contains. */
103
+ export interface EmittedFile {
104
+ /** The path to write the file at (relative; the operator pastes/uploads it). */
105
+ path: string;
106
+ /** The full file contents. */
107
+ contents: string;
108
+ }
109
+
110
+ /** What a host provider returns: the ready-to-paste cloud-init file. */
111
+ export interface ProvisionResult {
112
+ /** Which host this was provisioned for (echoed for the caller). */
113
+ host: HostName;
114
+ /** The cloud-init file (path + contents). */
115
+ cloudInit: EmittedFile;
116
+ }
117
+
118
+ /**
119
+ * The `HostProvider` seam: one method that generates a host's provisioning
120
+ * artifact (cloud-init) for a given input. v1 has a single implementation
121
+ * ({@link hetznerHostProvider}); adding DigitalOcean / others later means
122
+ * adding another provider here and an entry in {@link HOST_PROVIDERS}; deploy/
123
+ * publish/status logic is untouched (spec user story 21).
124
+ */
125
+ export interface HostProvider {
126
+ /** The host this provider provisions for. */
127
+ readonly host: HostName;
128
+ /** Generate the cloud-init (+ any host artifacts) for the given input. */
129
+ provision(input: ProvisionInput): ProvisionResult;
130
+ }
131
+
132
+ /** Defaults kept in one place so the template + docs never drift. */
133
+ const DEFAULT_KUBO_VERSION = 'v0.38.1';
134
+ const DEFAULT_SITES_DIR = '/sites';
135
+ const DEFAULT_GATEWAYS: readonly string[] = [
136
+ 'https://{cid}.ipfs.dweb.link/',
137
+ 'https://{cid}.ipfs.cf-ipfs.com/',
138
+ 'https://ipfs.io/ipfs/{cid}',
139
+ ];
140
+
141
+ /**
142
+ * One `pinnace node` verb scheduled on a systemd timer. The verb self-gates on
143
+ * `NODE_ROLE` (republish -> publisher, mirror -> replica) or is role-agnostic
144
+ * (warm/status), so scheduling ALL of them on EVERY box is safe: the
145
+ * wrong-role verb is a clean no-op (ADR-0002).
146
+ */
147
+ interface TimerSpec {
148
+ /** The `pinnace node <verb>` this timer runs. */
149
+ verb: 'republish' | 'mirror' | 'warm' | 'status';
150
+ /** Human description for the unit files. */
151
+ description: string;
152
+ /** `OnBootSec` for the timer (staggered so timers don't all fire at once). */
153
+ onBootSec: string;
154
+ /** `OnUnitActiveSec`, the recurring cadence. */
155
+ onUnitActiveSec: string;
156
+ }
157
+
158
+ /**
159
+ * The four timers, ported from the reference bash-unit cadences but pointed at
160
+ * `pinnace node <verb>` instead of the bash scripts (ADR-0002). Order is stable
161
+ * (deterministic output). Cadences mirror the reference: IPNS republish/mirror
162
+ * well under record expiry, warm/status more frequent.
163
+ */
164
+ const TIMERS: readonly TimerSpec[] = [
165
+ {
166
+ verb: 'republish',
167
+ description: 'Republish + export IPNS records (publisher role)',
168
+ onBootSec: '8min',
169
+ onUnitActiveSec: '6h',
170
+ },
171
+ {
172
+ verb: 'mirror',
173
+ description: 'Mirror publisher IPNS records (replica role)',
174
+ onBootSec: '9min',
175
+ onUnitActiveSec: '3h',
176
+ },
177
+ {
178
+ verb: 'warm',
179
+ description: 'Warm public IPFS gateway caches for our CIDs',
180
+ onBootSec: '5min',
181
+ onUnitActiveSec: '30min',
182
+ },
183
+ {
184
+ verb: 'status',
185
+ description: 'Regenerate the per-site status JSON for the dashboard',
186
+ onBootSec: '6min',
187
+ onUnitActiveSec: '15min',
188
+ },
189
+ ];
190
+
191
+ /** Render the `pinnace-<verb>` systemd service + timer unit pair. */
192
+ function renderTimerUnits(t: TimerSpec): string {
193
+ return ` - path: /etc/systemd/system/pinnace-${t.verb}.service
194
+ permissions: "0644"
195
+ owner: root:root
196
+ content: |
197
+ [Unit]
198
+ Description=${t.description}
199
+ After=ipfs.service
200
+
201
+ [Service]
202
+ Type=oneshot
203
+ User=ipfs
204
+ Group=ipfs
205
+ EnvironmentFile=/etc/pinnace-node.env
206
+ Environment=IPFS_PATH=/var/lib/ipfs/.ipfs
207
+ ExecStart=/usr/local/bin/pinnace node ${t.verb}
208
+
209
+ - path: /etc/systemd/system/pinnace-${t.verb}.timer
210
+ permissions: "0644"
211
+ owner: root:root
212
+ content: |
213
+ [Unit]
214
+ Description=Schedule pinnace node ${t.verb}
215
+
216
+ [Timer]
217
+ OnBootSec=${t.onBootSec}
218
+ OnUnitActiveSec=${t.onUnitActiveSec}
219
+ Persistent=true
220
+
221
+ [Install]
222
+ WantedBy=timers.target
223
+ `;
224
+ }
225
+
226
+ /**
227
+ * Render the full Hetzner cloud-init. Deterministic: same input -> byte-
228
+ * identical output (snapshot-locked). Encodes the hardened-node invariants
229
+ * (ufw 4001 TCP+UDP + 443, never 5001 raw; localhost-bound API/gateway;
230
+ * AcceleratedDHTClient + reprovide + `Routing.Type auto`; hardened systemd
231
+ * Kubo unit; Caddy HTTPS + bearer proxy) and schedules the `pinnace node`
232
+ * timers (NOT bash).
233
+ */
234
+ function renderHetznerCloudInit(input: ProvisionInput): string {
235
+ const dashboardDomain = input.dashboardDomain ?? '';
236
+ const corsOrigins =
237
+ input.corsOrigins ?? (dashboardDomain ? `https://${dashboardDomain}` : '');
238
+ const publisherEndpoint =
239
+ input.role === 'replica' ? (input.publisherEndpoint ?? '') : '';
240
+ const gateways = input.gateways ?? DEFAULT_GATEWAYS;
241
+ const kuboVersion = input.kuboVersion ?? DEFAULT_KUBO_VERSION;
242
+ const sitesDir = input.sitesDir ?? DEFAULT_SITES_DIR;
243
+ const warmGateways = gateways.join(' ');
244
+
245
+ const timerUnits = TIMERS.map(renderTimerUnits).join('\n');
246
+ const enableTimers = TIMERS.map(
247
+ (t) => ` - systemctl enable --now pinnace-${t.verb}.timer`,
248
+ ).join('\n');
249
+
250
+ return `#cloud-config
251
+ # =============================================================================
252
+ # ${input.host} Cloud -> self-hosted IPFS (Kubo) node for a static website.
253
+ #
254
+ # Generated by \`pinnace provision --host ${input.host}\`. This is the programmatic
255
+ # successor to the shell \`sed\`-template prototype (superseded).
256
+ #
257
+ # What this gives you:
258
+ # - Kubo running as a hardened systemd service (user: ipfs)
259
+ # - Swarm port 4001 (TCP+UDP) open so public gateways can dial you
260
+ # - RPC API (5001) bound to localhost, exposed ONLY via Caddy HTTPS + bearer
261
+ # - AcceleratedDHTClient + reprovide tuned so gateways can DISCOVER your node
262
+ # - The \`pinnace\` binary installed + its on-box subcommands
263
+ # (\`pinnace node republish|mirror|warm|status\`) scheduled on role-gated
264
+ # systemd timers. Kubo owns pinning + reprovide; pinnace owns IPNS
265
+ # republish/export, replica mirror/fallback, gateway warm, and status.
266
+ # (See docs/adr/0002-on-box-agent-boundary.md.)
267
+ # =============================================================================
268
+
269
+ package_update: true
270
+ package_upgrade: true
271
+
272
+ packages:
273
+ - curl
274
+ - ca-certificates
275
+ - ufw
276
+ - jq
277
+ - debian-keyring
278
+ - debian-archive-keyring
279
+ - apt-transport-https
280
+
281
+ # ---------------------------------------------------------------------------
282
+ # On-box environment consumed by the pinnace node timers + the setup scripts.
283
+ # ---------------------------------------------------------------------------
284
+ write_files:
285
+ - path: /etc/pinnace-node.env
286
+ permissions: "0600"
287
+ owner: root:root
288
+ content: |
289
+ API_DOMAIN="${input.apiDomain}"
290
+ DASH_DOMAIN="${dashboardDomain}"
291
+ API_CORS_ORIGINS="${corsOrigins}"
292
+ ACME_EMAIL="${input.acmeEmail}"
293
+ RPC_BEARER_TOKEN="${input.bearerToken}"
294
+
295
+ # Gateways to warm through. {cid} is replaced with each site's current CID
296
+ # by \`pinnace node warm\`, which AUTO-DISCOVERS sites from MFS (\${SITES_DIR}).
297
+ # Any MFS entry whose name ends in .eth is ALSO warmed via eth.limo.
298
+ WARM_GATEWAYS="${warmGateways}"
299
+
300
+ # MFS directory that holds your sites (one entry per site).
301
+ SITES_DIR="${sitesDir}"
302
+
303
+ # Role: "publisher" holds IPNS keys and signs/exports records; "replica"
304
+ # holds NO key and MIRRORS the publisher's signed records. The pinnace node
305
+ # timers self-gate on this, so scheduling all timers on every box is safe.
306
+ NODE_ROLE="${input.role}"
307
+
308
+ # REPLICA ONLY: where to fetch the publisher's exported signed records.
309
+ PUBLISHER_ENDPOINT="${publisherEndpoint}"
310
+
311
+ KUBO_VERSION="${kuboVersion}"
312
+
313
+ # -- Kubo installer / initializer -----------------------------------------
314
+ - path: /usr/local/sbin/ipfs-setup.sh
315
+ permissions: "0755"
316
+ owner: root:root
317
+ content: |
318
+ #!/usr/bin/env bash
319
+ set -euo pipefail
320
+ source /etc/pinnace-node.env
321
+
322
+ ARCH="$(dpkg --print-architecture)" # amd64 / arm64
323
+ TARBALL="kubo_\${KUBO_VERSION}_linux-\${ARCH}.tar.gz"
324
+ URL="https://dist.ipfs.tech/kubo/\${KUBO_VERSION}/\${TARBALL}"
325
+
326
+ cd /tmp
327
+ curl -fsSL "$URL" -o "$TARBALL"
328
+ tar -xzf "$TARBALL"
329
+ bash kubo/install.sh
330
+ ipfs --version
331
+
332
+ if ! id ipfs >/dev/null 2>&1; then
333
+ useradd --system --create-home --home-dir /var/lib/ipfs --shell /usr/sbin/nologin ipfs
334
+ fi
335
+ install -d -o ipfs -g ipfs /var/lib/ipfs
336
+
337
+ export IPFS_PATH=/var/lib/ipfs/.ipfs
338
+ if [ ! -f "$IPFS_PATH/config" ]; then
339
+ # 'server' profile disables local-network announce (good for a datacenter box)
340
+ sudo -u ipfs env IPFS_PATH="$IPFS_PATH" ipfs init --profile server
341
+ fi
342
+
343
+ cfg() { sudo -u ipfs env IPFS_PATH="$IPFS_PATH" ipfs config "$@"; }
344
+
345
+ # --- Reachability: bind API + gateway to localhost only (NEVER public) ---
346
+ cfg Addresses.API "/ip4/127.0.0.1/tcp/5001"
347
+ cfg Addresses.Gateway "/ip4/127.0.0.1/tcp/8080"
348
+
349
+ # --- Discoverability: keep provider records fresh so gateways find us ---
350
+ cfg --json Routing.AcceleratedDHTClient true
351
+ cfg Routing.Type "auto"
352
+ # Re-announce everything we serve; interval well under the record expiry.
353
+ # (Kubo 0.38 uses Provide.*; the pre-0.38 keys FATAL the daemon at boot.)
354
+ cfg Provide.Interval "12h"
355
+ cfg Provide.Strategy "all"
356
+
357
+ # --- Resource hygiene for a small box ---
358
+ cfg Datastore.StorageMax "40GB"
359
+ cfg --json Swarm.ConnMgr.HighWater 200
360
+ cfg --json Swarm.ConnMgr.LowWater 100
361
+
362
+ # --- RPC API auth: require a bearer token even on localhost ---
363
+ cfg --json API.Authorizations "{
364
+ \\"uploader\\": {
365
+ \\"AuthSecret\\": \\"bearer:\${RPC_BEARER_TOKEN}\\",
366
+ \\"AllowedPaths\\": [\\"/api/v0\\"]
367
+ }
368
+ }"
369
+
370
+ cfg --json API.HTTPHeaders.Access-Control-Allow-Origin "[\\"*\\"]"
371
+ cfg --json API.HTTPHeaders.Access-Control-Allow-Methods "[\\"POST\\"]"
372
+ cfg --json API.HTTPHeaders.Access-Control-Allow-Headers "[\\"Authorization\\"]"
373
+
374
+ chown -R ipfs:ipfs /var/lib/ipfs
375
+
376
+ # -- systemd unit for the daemon (hardened) -------------------------------
377
+ - path: /etc/systemd/system/ipfs.service
378
+ permissions: "0644"
379
+ owner: root:root
380
+ content: |
381
+ [Unit]
382
+ Description=IPFS Kubo daemon
383
+ After=network-online.target
384
+ Wants=network-online.target
385
+
386
+ [Service]
387
+ User=ipfs
388
+ Group=ipfs
389
+ Environment=IPFS_PATH=/var/lib/ipfs/.ipfs
390
+ ExecStart=/usr/local/bin/ipfs daemon --migrate=true --enable-gc
391
+ Restart=on-failure
392
+ RestartSec=5
393
+ LimitNOFILE=65536
394
+ # hardening
395
+ NoNewPrivileges=true
396
+ ProtectSystem=strict
397
+ ProtectHome=true
398
+ ReadWritePaths=/var/lib/ipfs
399
+ PrivateTmp=true
400
+
401
+ [Install]
402
+ WantedBy=multi-user.target
403
+
404
+ # -- pinnace installer: the box runs the SAME binary as the client --------
405
+ - path: /usr/local/sbin/pinnace-setup.sh
406
+ permissions: "0755"
407
+ owner: root:root
408
+ content: |
409
+ #!/usr/bin/env bash
410
+ set -euo pipefail
411
+ # Install Node.js (for npm) then the pinnace binary globally. The on-box
412
+ # timers invoke \`pinnace node <verb>\`: one codebase, client + on-box.
413
+ if ! command -v npm >/dev/null 2>&1; then
414
+ curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
415
+ apt-get install -y nodejs
416
+ fi
417
+ npm install -g pinnace
418
+ pinnace version
419
+
420
+ # -- Caddy reverse proxy for the HTTPS API (auto TLS) ---------------------
421
+ - path: /usr/local/sbin/write-caddyfile.sh
422
+ permissions: "0755"
423
+ owner: root:root
424
+ content: |
425
+ #!/usr/bin/env bash
426
+ set -euo pipefail
427
+ source /etc/pinnace-node.env
428
+ if [ -z "\${API_DOMAIN}" ]; then
429
+ echo "API_DOMAIN not set; skipping Caddy config (API stays localhost-only)."
430
+ exit 0
431
+ fi
432
+ cat > /etc/caddy/Caddyfile <<EOF
433
+ {
434
+ email \${ACME_EMAIL}
435
+ }
436
+
437
+ \${API_DOMAIN} {
438
+ @cors_preflight method OPTIONS
439
+ header {
440
+ Access-Control-Allow-Origin "\${API_CORS_ORIGINS}"
441
+ Access-Control-Allow-Methods "POST, OPTIONS"
442
+ Access-Control-Allow-Headers "Authorization, Content-Type"
443
+ Vary Origin
444
+ }
445
+ respond @cors_preflight 204
446
+
447
+ # Only the Kubo RPC API. Kubo enforces the bearer token via
448
+ # API.Authorizations; Caddy just forwards the header.
449
+ reverse_proxy 127.0.0.1:5001
450
+ }
451
+ EOF
452
+
453
+ if [ -n "\${DASH_DOMAIN}" ]; then
454
+ cat >> /etc/caddy/Caddyfile <<EOF
455
+
456
+ \${DASH_DOMAIN} {
457
+ root * /var/www/ipfs-dash
458
+ file_server
459
+ }
460
+ EOF
461
+ fi
462
+ systemctl restart caddy
463
+
464
+ ${timerUnits}
465
+ # ---------------------------------------------------------------------------
466
+ # Boot sequence
467
+ # ---------------------------------------------------------------------------
468
+ runcmd:
469
+ # Install Caddy (official repo)
470
+ - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
471
+ - curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
472
+ - apt-get update
473
+ - apt-get install -y caddy
474
+
475
+ # Firewall: SSH + IPFS swarm + HTTP/HTTPS (for the API). NOT 5001 raw, NOT 8080.
476
+ - ufw default deny incoming
477
+ - ufw default allow outgoing
478
+ - ufw allow 22/tcp
479
+ - ufw allow 4001/tcp
480
+ - ufw allow 4001/udp
481
+ - ufw allow 80/tcp
482
+ - ufw allow 443/tcp
483
+ - ufw --force enable
484
+
485
+ # Install + start Kubo
486
+ - /usr/local/sbin/ipfs-setup.sh
487
+ - systemctl daemon-reload
488
+ - systemctl enable --now ipfs.service
489
+
490
+ # Configure the HTTPS API proxy (no-op if API_DOMAIN unset)
491
+ - /usr/local/sbin/write-caddyfile.sh
492
+
493
+ # Install the pinnace binary (the box runs the same CLI as the client).
494
+ - /usr/local/sbin/pinnace-setup.sh
495
+
496
+ # Enable the pinnace node timers. republish self-gates to publisher, mirror to
497
+ # replica, so enabling all of them on every box is safe (ADR-0002).
498
+ - install -d -o ipfs -g ipfs /var/www/ipfs-dash
499
+ ${enableTimers}
500
+
501
+ # =============================================================================
502
+ # NOTES
503
+ # -----
504
+ # 1. DNS: point an A record (API_DOMAIN) at this box's IPv4 BEFORE first boot,
505
+ # or re-run /usr/local/sbin/write-caddyfile.sh after DNS propagates.
506
+ # 2. Verify discoverability from OUTSIDE the box after ~15 min:
507
+ # https://delegated-ipfs.dev/routing/v1/providers/<CID>
508
+ # Your PeerID should appear in the providers list.
509
+ # =============================================================================
510
+ `;
511
+ }
512
+
513
+ /**
514
+ * The Hetzner provider: the first implementation of {@link HostProvider}.
515
+ * Emits the hardened-node cloud-init to a conventional filename.
516
+ */
517
+ export const hetznerHostProvider: HostProvider = {
518
+ host: 'hetzner',
519
+ provision(input: ProvisionInput): ProvisionResult {
520
+ return {
521
+ host: 'hetzner',
522
+ cloudInit: {
523
+ path: 'cloud-init.yaml',
524
+ contents: renderHetznerCloudInit(input),
525
+ },
526
+ };
527
+ },
528
+ };
529
+
530
+ /** The provider registry (host -> provider). v1 has a single entry. */
531
+ const PROVIDERS: Record<HostName, HostProvider> = {
532
+ hetzner: hetznerHostProvider,
533
+ };
534
+
535
+ /**
536
+ * Generate the provisioning cloud-init for the requested host: dispatches to
537
+ * the matching {@link HostProvider} and returns its cloud-init file. Throws
538
+ * LOUDLY on an unknown/unimplemented host (the seam exists so callers can add
539
+ * hosts later, but v1 only ships `hetzner`): never a silent no-op.
540
+ */
541
+ export function provision(input: ProvisionInput): ProvisionResult {
542
+ const provider = PROVIDERS[input.host];
543
+ if (!provider) {
544
+ throw new Error(
545
+ `unsupported host '${input.host}'; v1 provisions only ${HOST_PROVIDERS.join(
546
+ ', ',
547
+ )} (other hosts are Out of Scope but the HostProvider seam exists)`,
548
+ );
549
+ }
550
+ return provider.provision(input);
551
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Import a derived per-site IPNS key into the **publisher** node's keystore.
3
+ *
4
+ * This module wires the frozen master-key -> per-site key derivation (see
5
+ * `../derive/ipns-key-derivation.ts`, {@link DerivedIpnsKey}) into a running
6
+ * publisher node: it serializes the ed25519 keypair into the libp2p-protobuf
7
+ * form Kubo's `ipfs key import` (`key/import`) expects, then POSTs those bytes
8
+ * to the publisher's keystore via the {@link KuboRpcClient} seam. The client
9
+ * only ever supplies key MATERIAL.
10
+ *
11
+ * THE LOAD-BEARING INVARIANT (docs/adr/0003-*): deriving a key client-side is
12
+ * NOT client-side record SIGNING. A future reader will look at client-side key
13
+ * derivation + import and reasonably wonder "isn't this the fully-keyless C-1
14
+ * client-signing model the spec excludes?" — it is NOT. This module hands the
15
+ * node key MATERIAL; the NODE then signs the IPNS record and owns sequence
16
+ * numbers / validity via `name/publish` (owned by the on-box `republish` verb,
17
+ * `../node/node-commands.ts`). No signing primitive runs here; the only RPC
18
+ * this module issues is `key/import`. Spec Out of Scope excludes the C-1
19
+ * fully-keyless client-signing model; ADR-0003 records that invariant durably.
20
+ *
21
+ * PUBLISHER-ONLY (CONTEXT.md `publisher`, `replica`): exactly one node per
22
+ * shared IPNS name holds the key; replicas are KEYLESS and only re-announce the
23
+ * publisher's signed record. So this module REFUSES to import onto a `replica`
24
+ * ({@link KeyImportRoleError}) rather than silently proceeding — a wrong-role
25
+ * import is a caller error, not a no-op, because it would put a signing key on a
26
+ * box that must never hold one. (Design note: this refusal is a new, loud ERROR
27
+ * gated on the existing `HostRole` concept; it does not invent a role. See the
28
+ * done record's Decisions block.)
29
+ *
30
+ * SCOPE: ONLY key import into the publisher. Deriving the key is the
31
+ * `ipns-key-derivation` task; the publish/refresh timers and the record
32
+ * export/mirror are the `publisher-replica-model` task. This module does
33
+ * neither — it lands the key and stops.
34
+ */
35
+ import type {DerivedIpnsKey} from '../derive/ipns-key-derivation.js';
36
+ import type {HostRole} from '../config/config-resolution.js';
37
+ import type {KuboRpcClient} from '../rpc/kubo-rpc-client.js';
38
+
39
+ /**
40
+ * The frozen libp2p-protobuf `PrivateKey` prefix for an ed25519 key with a
41
+ * 64-byte `Data`:
42
+ *
43
+ * 0x08 0x01 — field 1 (Type), varint, KeyType Ed25519 = 1
44
+ * 0x12 0x40 — field 2 (Data), length-delimited, length = 64
45
+ *
46
+ * followed by the 64-byte ed25519 raw key (32-byte seed || 32-byte public key,
47
+ * the Go `ed25519.PrivateKey` layout libp2p `Raw()` emits). This is the
48
+ * `libp2p-protobuf-cleartext` format `ipfs key import` defaults to.
49
+ *
50
+ * @see https://github.com/libp2p/go-libp2p/blob/master/core/crypto/pb/crypto.proto
51
+ * @see https://github.com/libp2p/go-libp2p/blob/master/core/crypto/ed25519.go
52
+ */
53
+ export const LIBP2P_ED25519_PRIVATE_KEY_PREFIX = Uint8Array.from([
54
+ 0x08, 0x01, 0x12, 0x40,
55
+ ]);
56
+
57
+ /**
58
+ * Serialize a {@link DerivedIpnsKey} into the libp2p-protobuf-cleartext
59
+ * `PrivateKey` bytes `ipfs key import` expects.
60
+ *
61
+ * The libp2p ed25519 private key `Data` is the 64-byte concatenation of the
62
+ * 32-byte private seed and the 32-byte public key (so the public key need not
63
+ * be recomputed on load); we wrap it in the `PrivateKey { Type=Ed25519, Data }`
64
+ * protobuf via {@link LIBP2P_ED25519_PRIVATE_KEY_PREFIX}. Pure and offline: no
65
+ * node, network, or signing — this only reshapes bytes we already derived.
66
+ */
67
+ export function serializeIpnsKeyForImport(derived: DerivedIpnsKey): Uint8Array {
68
+ if (derived.seed.length !== 32) {
69
+ throw new Error(
70
+ `expected a 32-byte ed25519 seed, got ${derived.seed.length}`,
71
+ );
72
+ }
73
+ if (derived.publicKey.length !== 32) {
74
+ throw new Error(
75
+ `expected a 32-byte ed25519 public key, got ${derived.publicKey.length}`,
76
+ );
77
+ }
78
+ const prefix = LIBP2P_ED25519_PRIVATE_KEY_PREFIX;
79
+ const out = new Uint8Array(prefix.length + 64);
80
+ out.set(prefix, 0);
81
+ out.set(derived.seed, prefix.length); // Data[0..32): the private seed.
82
+ out.set(derived.publicKey, prefix.length + 32); // Data[32..64): the pubkey.
83
+ return out;
84
+ }
85
+
86
+ /** A loud refusal to import a key onto a non-publisher node (a replica). */
87
+ export class KeyImportRoleError extends Error {
88
+ constructor(
89
+ /** The role the caller passed (must be `publisher` to import). */
90
+ readonly role: HostRole,
91
+ /** The key name the import was attempted under. */
92
+ readonly keyName: string,
93
+ ) {
94
+ super(
95
+ `refusing key/import '${keyName}' onto a ${role}: only the publisher ` +
96
+ `holds the IPNS key; replicas are keyless and re-announce the ` +
97
+ `publisher's signed record`,
98
+ );
99
+ this.name = 'KeyImportRoleError';
100
+ }
101
+ }
102
+
103
+ /** Inputs to {@link importIpnsKeyIntoPublisher}. */
104
+ export interface ImportIpnsKeyInput {
105
+ /** The Kubo RPC client for the PUBLISHER node (per-node, bearer-guarded). */
106
+ client: KuboRpcClient;
107
+ /** The target node's role; MUST be `publisher` or the import is refused. */
108
+ role: HostRole;
109
+ /** The keystore key name to import under (the site name / `key/list` Name). */
110
+ keyName: string;
111
+ /** The derived per-site key (seed + public key) from `ipns-key-derivation`. */
112
+ derived: DerivedIpnsKey;
113
+ }
114
+
115
+ /**
116
+ * The Kubo `key/import` JSON response (`Name` + IPNS `Id`), as far as we read.
117
+ */
118
+ export interface KeyImportResult {
119
+ Name?: string;
120
+ Id?: string;
121
+ }
122
+
123
+ /**
124
+ * Serialize the derived key and import it into the PUBLISHER node's keystore
125
+ * via Kubo `key/import`. REFUSES ({@link KeyImportRoleError}) on any non-
126
+ * publisher role, touching the node not at all, so a replica stays keyless.
127
+ *
128
+ * The client supplies key MATERIAL only. The node signs IPNS records itself via
129
+ * `name/publish` (a separate concern the on-box `republish` verb owns); nothing
130
+ * here signs, and the only RPC issued is `key/import`. See the module doc +
131
+ * ADR-0003 for the "no client-side record signing" invariant.
132
+ */
133
+ export async function importIpnsKeyIntoPublisher(
134
+ input: ImportIpnsKeyInput,
135
+ ): Promise<KeyImportResult> {
136
+ if (input.role !== 'publisher') {
137
+ throw new KeyImportRoleError(input.role, input.keyName);
138
+ }
139
+ const keyBytes = serializeIpnsKeyForImport(input.derived);
140
+ return await input.client.keyImport<KeyImportResult>(input.keyName, keyBytes);
141
+ }