pinnace 0.0.0 → 0.2.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.
- package/LICENSE +661 -0
- package/README.md +164 -0
- package/dist/car/car-build.d.ts +26 -0
- package/dist/car/car-build.d.ts.map +1 -0
- package/dist/car/car-build.js +113 -0
- package/dist/car/car-build.js.map +1 -0
- package/dist/ci/ci-emit.d.ts +116 -0
- package/dist/ci/ci-emit.d.ts.map +1 -0
- package/dist/ci/ci-emit.js +190 -0
- package/dist/ci/ci-emit.js.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +17 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/run.d.ts +94 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +731 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/config/config-resolution.d.ts +186 -0
- package/dist/config/config-resolution.d.ts.map +1 -0
- package/dist/config/config-resolution.js +137 -0
- package/dist/config/config-resolution.js.map +1 -0
- package/dist/deploy/deploy.d.ts +121 -0
- package/dist/deploy/deploy.d.ts.map +1 -0
- package/dist/deploy/deploy.js +150 -0
- package/dist/deploy/deploy.js.map +1 -0
- package/dist/derive/ipns-key-derivation.d.ts +37 -0
- package/dist/derive/ipns-key-derivation.d.ts.map +1 -0
- package/dist/derive/ipns-key-derivation.js +130 -0
- package/dist/derive/ipns-key-derivation.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/node/node-commands.d.ts +115 -0
- package/dist/node/node-commands.d.ts.map +1 -0
- package/dist/node/node-commands.js +231 -0
- package/dist/node/node-commands.js.map +1 -0
- package/dist/provision/cloud-init.d.ts +162 -0
- package/dist/provision/cloud-init.d.ts.map +1 -0
- package/dist/provision/cloud-init.js +460 -0
- package/dist/provision/cloud-init.js.map +1 -0
- package/dist/publisher/key-import.d.ts +105 -0
- package/dist/publisher/key-import.d.ts.map +1 -0
- package/dist/publisher/key-import.js +76 -0
- package/dist/publisher/key-import.js.map +1 -0
- package/dist/publisher/record-sequence.d.ts +90 -0
- package/dist/publisher/record-sequence.d.ts.map +1 -0
- package/dist/publisher/record-sequence.js +250 -0
- package/dist/publisher/record-sequence.js.map +1 -0
- package/dist/rpc/kubo-rpc-client.d.ts +154 -0
- package/dist/rpc/kubo-rpc-client.d.ts.map +1 -0
- package/dist/rpc/kubo-rpc-client.js +207 -0
- package/dist/rpc/kubo-rpc-client.js.map +1 -0
- package/dist/rpc/mock-kubo.d.ts +87 -0
- package/dist/rpc/mock-kubo.d.ts.map +1 -0
- package/dist/rpc/mock-kubo.js +129 -0
- package/dist/rpc/mock-kubo.js.map +1 -0
- package/dist/site/site-management.d.ts +126 -0
- package/dist/site/site-management.d.ts.map +1 -0
- package/dist/site/site-management.js +123 -0
- package/dist/site/site-management.js.map +1 -0
- package/dist/status/status-report.d.ts +145 -0
- package/dist/status/status-report.d.ts.map +1 -0
- package/dist/status/status-report.js +192 -0
- package/dist/status/status-report.js.map +1 -0
- package/package.json +45 -2
- package/src/car/car-build.ts +135 -0
- package/src/ci/ci-emit.ts +284 -0
- package/src/cli/bin.ts +20 -0
- package/src/cli/run.ts +970 -0
- package/src/config/config-resolution.ts +264 -0
- package/src/deploy/deploy.ts +250 -0
- package/src/derive/ipns-key-derivation.ts +173 -0
- package/src/index.ts +146 -0
- package/src/node/node-commands.ts +395 -0
- package/src/provision/cloud-init.ts +646 -0
- package/src/publisher/key-import.ts +141 -0
- package/src/publisher/record-sequence.ts +336 -0
- package/src/rpc/kubo-rpc-client.ts +281 -0
- package/src/rpc/mock-kubo.ts +194 -0
- package/src/site/site-management.ts +241 -0
- package/src/status/status-report.ts +291 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config resolution for pinnace.
|
|
3
|
+
*
|
|
4
|
+
* Every setting resolves with precedence **CLI arg > env (via `ldenv`) >
|
|
5
|
+
* `pinnace.json`** (CONTEXT.md "config resolution"). This module owns the typed
|
|
6
|
+
* `pinnace.json` schema and the resolver.
|
|
7
|
+
*
|
|
8
|
+
* SECURITY INVARIANT (hard): the **master secret is env-only**. The resolver
|
|
9
|
+
* has NO file path for the master — a `master` field placed in `pinnace.json`
|
|
10
|
+
* is structurally impossible to surface here. Reading the master goes through
|
|
11
|
+
* {@link resolveMasterSecret}, which consults ONLY env. A compromised node or a
|
|
12
|
+
* leaked config file therefore cannot leak the master.
|
|
13
|
+
*
|
|
14
|
+
* `ldenv` is the env layer in production; to keep tests hermetic (and to keep
|
|
15
|
+
* the operator's real environment untouched), the resolver takes an explicit
|
|
16
|
+
* `env` record rather than reading `process.env` directly. A thin production
|
|
17
|
+
* caller passes `ldenv`-loaded values in.
|
|
18
|
+
*
|
|
19
|
+
* SECOND ENV-ONLY SECRET (the bearer TOKEN). A node's bearer `token` is the
|
|
20
|
+
* SAME CLASS as the master: a secret that must NEVER live in `pinnace.json`.
|
|
21
|
+
* So {@link HostConfig} has NO `token` field at all (structurally impossible to
|
|
22
|
+
* leak one via config, exactly like the master), and the token is resolved
|
|
23
|
+
* ONLY from `CLI > env(PINNACE_HOST_<NAME>_TOKEN)` by {@link resolveHostToken}.
|
|
24
|
+
* A host with no resolvable token is a LOUD, specific error naming the missing
|
|
25
|
+
* env var (never a silent `""` that turns into a downstream 401).
|
|
26
|
+
*
|
|
27
|
+
* EAGER-VS-LAZY (decided): the token failure is LAZY, not eager. `resolveConfig`
|
|
28
|
+
* does NOT demand a token for every configured host up front (a host no
|
|
29
|
+
* operation touches must not block unrelated work); instead a caller resolves a
|
|
30
|
+
* host's token via {@link resolveHostToken} at the moment it actually builds
|
|
31
|
+
* that host's client (deploy target / status client), so only hosts an
|
|
32
|
+
* operation USES must have a token set. See the `## Decisions` block in the
|
|
33
|
+
* done record.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
/** A node/host role. */
|
|
37
|
+
export type HostRole = 'publisher' | 'replica';
|
|
38
|
+
|
|
39
|
+
/** A per-site publish mode. */
|
|
40
|
+
export type SiteMode = 'ipfs' | 'ipns';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A host/node entry as it appears in `pinnace.json`.
|
|
44
|
+
*
|
|
45
|
+
* NOTE there is DELIBERATELY no `token` field: the bearer token is env-only by
|
|
46
|
+
* construction (see the module doc), resolved via {@link resolveHostToken}. A
|
|
47
|
+
* stray `token` in a file object is ignored, exactly as a stray `master` is.
|
|
48
|
+
*/
|
|
49
|
+
export interface HostConfig {
|
|
50
|
+
/** Stable host name (used to key env/CLI token + endpoint overrides). */
|
|
51
|
+
name: string;
|
|
52
|
+
/** The node's Kubo RPC endpoint. */
|
|
53
|
+
endpoint: string;
|
|
54
|
+
/** publisher (holds a key, signs) or replica (keyless, re-announces). */
|
|
55
|
+
role: HostRole;
|
|
56
|
+
/** For replicas: where to fetch the publisher's exported record. */
|
|
57
|
+
publisherEndpoint?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A site entry as it appears in `pinnace.json`.
|
|
62
|
+
*
|
|
63
|
+
* A site is identified by ONE value: {@link SiteConfig.id}. It is BOTH the MFS
|
|
64
|
+
* entry (`/sites/<id>`) AND the KDF input fed to the frozen derivation (see
|
|
65
|
+
* `../derive/ipns-key-derivation.ts` / ADR-0001). There is no separate `name`
|
|
66
|
+
* and no separate `keyId` — one `id` is the whole identity surface.
|
|
67
|
+
*/
|
|
68
|
+
export interface SiteConfig {
|
|
69
|
+
/**
|
|
70
|
+
* The site's SINGLE identifier: both its MFS entry (`/sites/<id>`) and the KDF
|
|
71
|
+
* input for its per-site IPNS key. Frozen once a name is live (changing it
|
|
72
|
+
* moves the derived id, per ADR-0001). NOT the ENS name.
|
|
73
|
+
*/
|
|
74
|
+
id: string;
|
|
75
|
+
/** ipfs (land+pin+MFS) or ipns (also publish/refresh). */
|
|
76
|
+
mode: SiteMode;
|
|
77
|
+
/** The site source directory to build a CAR from. */
|
|
78
|
+
sourceDir: string;
|
|
79
|
+
/**
|
|
80
|
+
* OPTIONAL eth.limo-warming hint: when set, the site is ALSO warmed via
|
|
81
|
+
* `https://<ensName>.limo`. It is NOT part of identity and NEVER an input to
|
|
82
|
+
* the key derivation — purely an opt-in warming lever.
|
|
83
|
+
*/
|
|
84
|
+
ensName?: string;
|
|
85
|
+
/**
|
|
86
|
+
* An optional externally-owned key (the escape hatch): when set, this site's
|
|
87
|
+
* IPNS key is NOT derived from the master but supplied here.
|
|
88
|
+
*/
|
|
89
|
+
externalKey?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The typed `pinnace.json` shape. Note there is DELIBERATELY no `master` field
|
|
94
|
+
* in the resolved config — see the module doc. If a raw file object carries a
|
|
95
|
+
* stray `master`, it is ignored (never copied into the resolved config).
|
|
96
|
+
*/
|
|
97
|
+
export interface PinnaceConfigFile {
|
|
98
|
+
hosts?: HostConfig[];
|
|
99
|
+
sites?: SiteConfig[];
|
|
100
|
+
/** Public gateways to warm (dweb.link, eth.limo, ...). */
|
|
101
|
+
gateways?: string[];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The fully resolved config the rest of pinnace consumes. */
|
|
105
|
+
export interface ResolvedConfig {
|
|
106
|
+
hosts: HostConfig[];
|
|
107
|
+
sites: SiteConfig[];
|
|
108
|
+
gateways: string[];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** An env record (name → value). In production these come from `ldenv`. */
|
|
112
|
+
export type EnvRecord = Record<string, string | undefined>;
|
|
113
|
+
|
|
114
|
+
/** CLI overrides. Keyed by host name for per-host values. */
|
|
115
|
+
export interface CliOverrides {
|
|
116
|
+
/** hostName → token override. */
|
|
117
|
+
hostToken?: Record<string, string>;
|
|
118
|
+
/** hostName → endpoint override. */
|
|
119
|
+
hostEndpoint?: Record<string, string>;
|
|
120
|
+
/** Gateways override (replaces the file/env list). */
|
|
121
|
+
gateways?: string[];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Inputs to {@link resolveConfig}. */
|
|
125
|
+
export interface ResolveConfigInput {
|
|
126
|
+
/** The parsed `pinnace.json` object (lowest precedence). */
|
|
127
|
+
file: PinnaceConfigFile;
|
|
128
|
+
/** The env layer (middle precedence). */
|
|
129
|
+
env: EnvRecord;
|
|
130
|
+
/** CLI args (highest precedence). */
|
|
131
|
+
cli: CliOverrides;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Uppercase + non-alphanumeric → `_`, for building env var names. */
|
|
135
|
+
function envKey(...parts: string[]): string {
|
|
136
|
+
return parts
|
|
137
|
+
.map((p) => p.toUpperCase().replace(/[^A-Z0-9]+/g, '_'))
|
|
138
|
+
.join('_');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Resolve the full pinnace config with precedence CLI arg > env > file.
|
|
143
|
+
*
|
|
144
|
+
* A stray `master` field on the file object is NEVER read here — the master is
|
|
145
|
+
* env-only via {@link resolveMasterSecret}.
|
|
146
|
+
*/
|
|
147
|
+
export function resolveConfig(input: ResolveConfigInput): ResolvedConfig {
|
|
148
|
+
const {file, env, cli} = input;
|
|
149
|
+
|
|
150
|
+
const hosts: HostConfig[] = (file.hosts ?? []).map((h) => {
|
|
151
|
+
const endpoint =
|
|
152
|
+
cli.hostEndpoint?.[h.name] ??
|
|
153
|
+
env[envKey('PINNACE_HOST', h.name, 'ENDPOINT')] ??
|
|
154
|
+
h.endpoint;
|
|
155
|
+
// The token is NOT resolved here — it is env-only and resolved lazily, per
|
|
156
|
+
// host, at the moment an operation builds that host's client (see
|
|
157
|
+
// resolveHostToken + the module doc). Copy only the non-secret fields.
|
|
158
|
+
return {
|
|
159
|
+
name: h.name,
|
|
160
|
+
endpoint,
|
|
161
|
+
role: h.role,
|
|
162
|
+
...(h.publisherEndpoint !== undefined
|
|
163
|
+
? {publisherEndpoint: h.publisherEndpoint}
|
|
164
|
+
: {}),
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const gateways =
|
|
169
|
+
cli.gateways ?? splitList(env['PINNACE_GATEWAYS']) ?? file.gateways ?? [];
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
hosts,
|
|
173
|
+
sites: file.sites ?? [],
|
|
174
|
+
gateways,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The env var name a host's bearer token is read from: `PINNACE_HOST_<NAME>_TOKEN`
|
|
180
|
+
* (uppercased, non-alphanumerics collapsed to `_`). Exposed so error messages
|
|
181
|
+
* and callers name the EXACT var an operator must set.
|
|
182
|
+
*/
|
|
183
|
+
export function hostTokenEnvVar(hostName: string): string {
|
|
184
|
+
return envKey('PINNACE_HOST', hostName, 'TOKEN');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A loud, specific failure when a host has no resolvable bearer token. Names the
|
|
189
|
+
* exact missing env var so the operator knows precisely what to set (never a
|
|
190
|
+
* silent empty token / downstream 401).
|
|
191
|
+
*/
|
|
192
|
+
export class MissingHostTokenError extends Error {
|
|
193
|
+
constructor(
|
|
194
|
+
/** The host whose token could not be resolved. */
|
|
195
|
+
readonly hostName: string,
|
|
196
|
+
/** The exact env var that would supply it. */
|
|
197
|
+
readonly envVar: string,
|
|
198
|
+
) {
|
|
199
|
+
super(
|
|
200
|
+
`host '${hostName}' has no token; set ${envVar} ` +
|
|
201
|
+
`(the token is env-only, never read from pinnace.json)`,
|
|
202
|
+
);
|
|
203
|
+
this.name = 'MissingHostTokenError';
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Inputs to {@link resolveHostToken}. */
|
|
208
|
+
export interface ResolveHostTokenInput {
|
|
209
|
+
/** The host whose token to resolve (its `name` keys the env/CLI override). */
|
|
210
|
+
hostName: string;
|
|
211
|
+
/** The env layer (the ONLY file-less source of the token). */
|
|
212
|
+
env: EnvRecord;
|
|
213
|
+
/** CLI overrides (highest precedence): `hostToken[hostName]`. */
|
|
214
|
+
cli?: CliOverrides;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Resolve ONE host's bearer token with precedence `CLI > env` (there is
|
|
219
|
+
* DELIBERATELY no file layer — the token is env-only, like the master). Throws
|
|
220
|
+
* {@link MissingHostTokenError} (naming the exact env var) when neither layer
|
|
221
|
+
* supplies it, so a missing token is a LOUD config error, never a silent `""`.
|
|
222
|
+
*
|
|
223
|
+
* Callers invoke this LAZILY — only for hosts an operation actually uses (see
|
|
224
|
+
* the eager-vs-lazy decision in the module doc) — so a configured-but-unused
|
|
225
|
+
* host never blocks unrelated work.
|
|
226
|
+
*/
|
|
227
|
+
export function resolveHostToken(input: ResolveHostTokenInput): string {
|
|
228
|
+
const envVar = hostTokenEnvVar(input.hostName);
|
|
229
|
+
const token = input.cli?.hostToken?.[input.hostName] ?? input.env[envVar];
|
|
230
|
+
if (token === undefined || token === '') {
|
|
231
|
+
throw new MissingHostTokenError(input.hostName, envVar);
|
|
232
|
+
}
|
|
233
|
+
return token;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Split a comma-separated env list, or return undefined if unset/empty. */
|
|
237
|
+
function splitList(value: string | undefined): string[] | undefined {
|
|
238
|
+
if (!value) return undefined;
|
|
239
|
+
const list = value
|
|
240
|
+
.split(',')
|
|
241
|
+
.map((s) => s.trim())
|
|
242
|
+
.filter(Boolean);
|
|
243
|
+
return list.length > 0 ? list : undefined;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Inputs to {@link resolveMasterSecret}. */
|
|
247
|
+
export interface ResolveMasterInput {
|
|
248
|
+
/** The env layer. This is the ONLY source of the master. */
|
|
249
|
+
env: EnvRecord;
|
|
250
|
+
/** The env var name to read (default `PINNACE_MASTER`). */
|
|
251
|
+
envVar?: string;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Resolve the master secret. It is read ONLY from env (there is deliberately no
|
|
256
|
+
* `file` parameter): the master must never come from `pinnace.json`. Returns
|
|
257
|
+
* undefined when the env var is unset.
|
|
258
|
+
*/
|
|
259
|
+
export function resolveMasterSecret(
|
|
260
|
+
input: ResolveMasterInput,
|
|
261
|
+
): string | undefined {
|
|
262
|
+
const envVar = input.envVar ?? 'PINNACE_MASTER';
|
|
263
|
+
return input.env[envVar];
|
|
264
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-agnostic **deploy** — build one CAR and land the SAME CID on every node.
|
|
3
|
+
*
|
|
4
|
+
* This is the core operation user stories 4, 6, 7 describe: build a site's
|
|
5
|
+
* **CAR** ONCE (via the `car-build` seam), then fan out across every configured
|
|
6
|
+
* **node** (each with its OWN bearer token) so all nodes serve the IDENTICAL
|
|
7
|
+
* **CID** with no single point of failure. Per node the flow is: import + pin
|
|
8
|
+
* the CAR (`dag/import?pin-roots=true`), then place it in MFS at `/sites/<id>`
|
|
9
|
+
* (mkdir parents / rm old / cp `/ipfs/<cid>`) so `gateway warming`, IPNS
|
|
10
|
+
* republish, and `status` auto-discover it. Deploy speaks ONLY the Kubo RPC seam
|
|
11
|
+
* ({@link KuboRpcClient}); it is not host-specific.
|
|
12
|
+
*
|
|
13
|
+
* Behaviour ported (not copied) from the reference prototype
|
|
14
|
+
* `~/searches/ipfs-hetzner/deploy-car.mjs`: the multi-target fan-out, the MFS
|
|
15
|
+
* placement, the per-`mode` branch, and the `Promise.allSettled` partial-failure
|
|
16
|
+
* semantics (a non-empty subset of nodes succeeding is still an overall success:
|
|
17
|
+
* "the site is up on the rest").
|
|
18
|
+
*
|
|
19
|
+
* PER-SITE `mode` BRANCH (CONTEXT.md `mode`; spec's "Per-site mode branch"):
|
|
20
|
+
* - `ipfs` mode: land + pin + MFS ONLY (no key, no publish). ENS uses
|
|
21
|
+
* `ipfs://<cid>`, updated per deploy.
|
|
22
|
+
* - `ipns` mode: everything above PLUS the publish path (`key/list` then
|
|
23
|
+
* `name/publish`) and ONLY on a PUBLISHER target. A `replica` (or a target
|
|
24
|
+
* with {@link DeployTarget.publish} disabled) NEVER signs — it re-announces
|
|
25
|
+
* the publisher's record via the on-box `mirror` verb instead. This mirrors
|
|
26
|
+
* the prototype's `doPublish = mode === "ipns" && PUBLISH_IPNS` gate, with
|
|
27
|
+
* role standing in for the prototype's `PUBLISH_IPNS=0` replica flag.
|
|
28
|
+
*
|
|
29
|
+
* SEAM BOUNDARY (see the sibling tasks): this module wires the per-mode call
|
|
30
|
+
* SEQUENCE only. The full publisher/replica record export+mirror lives in
|
|
31
|
+
* `publisher-replica-model`, and importing the derived key into the publisher's
|
|
32
|
+
* keystore lives in `key-import-publisher`. So deploy here does NOT `key/gen`
|
|
33
|
+
* or `key/import` — it assumes the publisher already holds the key (looked up
|
|
34
|
+
* via `key/list`) and issues `name/publish`. If a publisher has no matching key
|
|
35
|
+
* yet, the publish is reported as skipped rather than silently generating one
|
|
36
|
+
* (key provisioning is that other task's job, not deploy's).
|
|
37
|
+
*/
|
|
38
|
+
import {KuboRpcClient, type FetchLike} from '../rpc/kubo-rpc-client.js';
|
|
39
|
+
import {buildCar, type BuiltCar} from '../car/car-build.js';
|
|
40
|
+
import {placeInMfs} from '../site/site-management.js';
|
|
41
|
+
import type {HostRole, SiteMode} from '../config/config-resolution.js';
|
|
42
|
+
|
|
43
|
+
/** The MFS directory sites live under (matches site-management). */
|
|
44
|
+
const DEFAULT_SITES_DIR = '/sites';
|
|
45
|
+
|
|
46
|
+
/** Reference record values (ported from the prototype / on-box republish). */
|
|
47
|
+
const RECORD_LIFETIME = '72h';
|
|
48
|
+
const RECORD_TTL = '1h';
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* One deploy target: a node's RPC endpoint + its OWN token + its role. In
|
|
52
|
+
* `ipns` mode only a `publisher` (with publish not disabled) signs the record;
|
|
53
|
+
* a `replica` lands+pins+MFS but never publishes.
|
|
54
|
+
*/
|
|
55
|
+
export interface DeployTarget {
|
|
56
|
+
/** The node's Kubo RPC base URL. */
|
|
57
|
+
baseUrl: string;
|
|
58
|
+
/** The node's bearer token (each target has its OWN). */
|
|
59
|
+
token: string;
|
|
60
|
+
/** publisher (may sign in ipns mode) or replica (never signs). */
|
|
61
|
+
role: HostRole;
|
|
62
|
+
/**
|
|
63
|
+
* Explicit publish switch. Defaults to true for a publisher. Set false to
|
|
64
|
+
* land+pin+MFS on a publisher WITHOUT signing (the prototype's
|
|
65
|
+
* `PUBLISH_IPNS=0`). A `replica` never publishes regardless of this flag.
|
|
66
|
+
*/
|
|
67
|
+
publish?: boolean;
|
|
68
|
+
/** Injectable fetch (tests pass a MockKuboApi); defaults to global fetch. */
|
|
69
|
+
fetchImpl?: FetchLike;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Inputs to {@link deploy}. Supply EITHER a `sourceDir` OR a prebuilt `car`. */
|
|
73
|
+
export interface DeployInput {
|
|
74
|
+
/** The site source directory to build a CAR from (mutually exclusive with `car`). */
|
|
75
|
+
sourceDir?: string;
|
|
76
|
+
/** A prebuilt CAR to deploy as-is (mutually exclusive with `sourceDir`). */
|
|
77
|
+
car?: BuiltCar;
|
|
78
|
+
/** The site's single `id`: its MFS entry `/sites/<id>` and, in ipns mode, its key name. */
|
|
79
|
+
id: string;
|
|
80
|
+
/** ipfs (land+pin+MFS) or ipns (also key/list + name/publish on publishers). */
|
|
81
|
+
mode: SiteMode;
|
|
82
|
+
/** The nodes to deploy to (each with its own token); the CAR lands on all. */
|
|
83
|
+
targets: DeployTarget[];
|
|
84
|
+
/** The MFS directory sites live under (default `/sites`). */
|
|
85
|
+
sitesDir?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A per-target success record. */
|
|
89
|
+
export interface DeployNodeOk {
|
|
90
|
+
/** The node's base URL. */
|
|
91
|
+
baseUrl: string;
|
|
92
|
+
/** The CID landed on this node (identical across all successful nodes). */
|
|
93
|
+
cid: string;
|
|
94
|
+
/** The IPNS id published on this node, if it signed; undefined otherwise. */
|
|
95
|
+
ipns?: string;
|
|
96
|
+
/** Whether this node signed+published an IPNS record. */
|
|
97
|
+
published: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A per-target failure record. */
|
|
101
|
+
export interface DeployNodeFailure {
|
|
102
|
+
/** The node's base URL. */
|
|
103
|
+
baseUrl: string;
|
|
104
|
+
/** The error that failed this node's deploy (its content is up elsewhere). */
|
|
105
|
+
error: Error;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The overall deploy result: the CID, and per-node success/failure. */
|
|
109
|
+
export interface DeployResult {
|
|
110
|
+
/** The authoritative site CID (identical on every node). */
|
|
111
|
+
cid: string;
|
|
112
|
+
/** The site's mode. */
|
|
113
|
+
mode: SiteMode;
|
|
114
|
+
/** Nodes the deploy landed on (import + MFS, plus publish where applicable). */
|
|
115
|
+
ok: DeployNodeOk[];
|
|
116
|
+
/** Nodes whose deploy failed (reported, not thrown — the rest are up). */
|
|
117
|
+
failed: DeployNodeFailure[];
|
|
118
|
+
/** True when at least one node succeeded (some-nodes-up is still success). */
|
|
119
|
+
success: boolean;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Deploy a site: build the CAR ONCE, then import the SAME CAR into every target
|
|
124
|
+
* (each with its own token), pin it, place it in MFS, and (in `ipns` mode, on
|
|
125
|
+
* publisher targets) publish the IPNS record. Fans out with
|
|
126
|
+
* `Promise.allSettled`: a node that fails is reported in {@link DeployResult.failed}
|
|
127
|
+
* and does not sink the others; a non-empty success subset is still an overall
|
|
128
|
+
* success ({@link DeployResult.success}). Throws only if NO node succeeds is
|
|
129
|
+
* NOT the contract here — deploy always RESOLVES with the per-node breakdown;
|
|
130
|
+
* callers inspect `success`.
|
|
131
|
+
*
|
|
132
|
+
* @throws if neither `sourceDir` nor `car` is supplied, or both are.
|
|
133
|
+
*/
|
|
134
|
+
export async function deploy(input: DeployInput): Promise<DeployResult> {
|
|
135
|
+
const {sourceDir, car, id, mode, targets} = input;
|
|
136
|
+
if ((sourceDir === undefined) === (car === undefined)) {
|
|
137
|
+
throw new Error('deploy requires exactly one of `sourceDir` or `car`');
|
|
138
|
+
}
|
|
139
|
+
const sitesDir = input.sitesDir ?? DEFAULT_SITES_DIR;
|
|
140
|
+
|
|
141
|
+
// Build the CAR ONCE — the same bytes (and thus the same CID) land on every
|
|
142
|
+
// node (redundancy, no single point of failure).
|
|
143
|
+
const built: BuiltCar = car ?? (await buildCar(sourceDir as string));
|
|
144
|
+
|
|
145
|
+
// Fan out. allSettled so one node's failure never sinks the others.
|
|
146
|
+
const settled = await Promise.allSettled(
|
|
147
|
+
targets.map((target) => deployToNode(target, built, id, mode, sitesDir)),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
const ok: DeployNodeOk[] = [];
|
|
151
|
+
const failed: DeployNodeFailure[] = [];
|
|
152
|
+
settled.forEach((outcome, i) => {
|
|
153
|
+
const baseUrl = targets[i].baseUrl;
|
|
154
|
+
if (outcome.status === 'fulfilled') {
|
|
155
|
+
ok.push(outcome.value);
|
|
156
|
+
} else {
|
|
157
|
+
failed.push({baseUrl, error: asError(outcome.reason)});
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
cid: built.rootCid,
|
|
163
|
+
mode,
|
|
164
|
+
ok,
|
|
165
|
+
failed,
|
|
166
|
+
success: ok.length > 0,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Deploy the (already-built) CAR to ONE node: import + pin, place in MFS, then
|
|
172
|
+
* (in ipns mode on a publishing publisher) key/list + name/publish. Rejects on
|
|
173
|
+
* any RPC failure so the caller's allSettled records it as a per-node failure.
|
|
174
|
+
*/
|
|
175
|
+
async function deployToNode(
|
|
176
|
+
target: DeployTarget,
|
|
177
|
+
built: BuiltCar,
|
|
178
|
+
id: string,
|
|
179
|
+
mode: SiteMode,
|
|
180
|
+
sitesDir: string,
|
|
181
|
+
): Promise<DeployNodeOk> {
|
|
182
|
+
const client = new KuboRpcClient({
|
|
183
|
+
baseUrl: target.baseUrl,
|
|
184
|
+
token: target.token,
|
|
185
|
+
fetchImpl: target.fetchImpl,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// 1. Import + pin the CAR (same CID as every other node).
|
|
189
|
+
await client.dagImport(built.carBytes);
|
|
190
|
+
|
|
191
|
+
// 2. Place it in MFS /sites/<id> (mkdir parents / rm old / cp /ipfs/<cid>).
|
|
192
|
+
// Reuses the single implementation of that sequence (site-management).
|
|
193
|
+
await placeInMfs(client, sitesDir, id, built.rootCid);
|
|
194
|
+
|
|
195
|
+
// 3. Mode branch: ipns mode ADDS publish, and ONLY on a publishing publisher.
|
|
196
|
+
if (mode === 'ipns' && shouldPublish(target)) {
|
|
197
|
+
const ipns = await publish(client, id, built.rootCid);
|
|
198
|
+
return {baseUrl: target.baseUrl, cid: built.rootCid, ipns, published: true};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return {baseUrl: target.baseUrl, cid: built.rootCid, published: false};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Whether this target signs the IPNS record: a `publisher` whose publish switch
|
|
206
|
+
* is not explicitly disabled. A `replica` NEVER publishes (it re-announces the
|
|
207
|
+
* publisher's signed record via the on-box `mirror` verb instead).
|
|
208
|
+
*/
|
|
209
|
+
function shouldPublish(target: DeployTarget): boolean {
|
|
210
|
+
return target.role === 'publisher' && target.publish !== false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The publish path (ipns mode, publisher only): `key/list` to resolve the site
|
|
215
|
+
* key's IPNS id, then `name/publish` to sign+refresh the record for
|
|
216
|
+
* `/ipfs/<cid>`. The keystore key name is the site's single `id` (the same
|
|
217
|
+
* value key-import imports under — one identifier, so the lookup cannot miss by
|
|
218
|
+
* a name/keyId split). Does NOT `key/gen`/`key/import`: the key is provisioned
|
|
219
|
+
* by the sibling `key-import-publisher` task; here we assume it exists. If it
|
|
220
|
+
* does not yet, we skip the publish (returning undefined) rather than silently
|
|
221
|
+
* generating a key deploy has no business owning.
|
|
222
|
+
*/
|
|
223
|
+
async function publish(
|
|
224
|
+
client: KuboRpcClient,
|
|
225
|
+
id: string,
|
|
226
|
+
cid: string,
|
|
227
|
+
): Promise<string | undefined> {
|
|
228
|
+
const keys = await client.keyList<{
|
|
229
|
+
Keys?: Array<{Name?: string; Id?: string}> | null;
|
|
230
|
+
}>();
|
|
231
|
+
const ipns = (keys.Keys ?? []).find((k) => k?.Name === id)?.Id;
|
|
232
|
+
if (!ipns) {
|
|
233
|
+
// The publisher has no key for this site yet (key-import-publisher owns
|
|
234
|
+
// provisioning it). Land the content but do not sign.
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
await client.namePublish({
|
|
238
|
+
cidPath: `/ipfs/${cid}`,
|
|
239
|
+
key: id,
|
|
240
|
+
lifetime: RECORD_LIFETIME,
|
|
241
|
+
ttl: RECORD_TTL,
|
|
242
|
+
allowOffline: true,
|
|
243
|
+
});
|
|
244
|
+
return ipns;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Coerce an unknown rejection reason into an Error. */
|
|
248
|
+
function asError(reason: unknown): Error {
|
|
249
|
+
return reason instanceof Error ? reason : new Error(String(reason));
|
|
250
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Master-key -> per-site IPNS key derivation. **THIS IS A FROZEN CONTRACT.**
|
|
3
|
+
*
|
|
4
|
+
* Every `ipns`-mode site's key is derived deterministically from one operator
|
|
5
|
+
* master secret, so names are recoverable from the master alone and
|
|
6
|
+
* provisioning is stateless (CONTEXT.md `master key`, `keyId`). The scheme,
|
|
7
|
+
* pinned FOREVER by docs/adr/0001-frozen-ipns-key-derivation.md:
|
|
8
|
+
*
|
|
9
|
+
* seed = HKDF-SHA256(ikm = master, salt = "", info = "pinnace:ipns:v1:" + keyId, length = 32)
|
|
10
|
+
* -> the 32 bytes ARE the ed25519 private seed
|
|
11
|
+
* -> its public key IS the IPNS name, rendered as a CIDv1 `k51...` id.
|
|
12
|
+
*
|
|
13
|
+
* Frozen invariants (changing ANY of these moves every live name irreversibly):
|
|
14
|
+
* - the KDF is HKDF-SHA256;
|
|
15
|
+
* - the `info` prefix is exactly `pinnace:ipns:v1:` (the version `v1` lives in
|
|
16
|
+
* the `info` string, NOT in a separate parameter — a v2 scheme would use a
|
|
17
|
+
* new prefix and never re-derive existing names);
|
|
18
|
+
* - `keyId` is the SOLE per-site input, appended verbatim (UTF-8) to the info
|
|
19
|
+
* prefix. The ENS name is mutable and MUST NEVER enter derivation, so this
|
|
20
|
+
* surface has no `ensName` parameter at all;
|
|
21
|
+
* - the HKDF `salt` is the RFC 5869 default (empty / zero-length). The spec
|
|
22
|
+
* pins only ikm/info/length; an empty salt is the standard "no salt" case
|
|
23
|
+
* and is pinned here so the contract is total (see ADR-0001);
|
|
24
|
+
* - the 32 HKDF output bytes are used directly as the ed25519 seed (RFC 8032
|
|
25
|
+
* private key), NOT hashed or expanded again.
|
|
26
|
+
*
|
|
27
|
+
* Key IMPORT into a node keystore and the "no client signing" boundary are a
|
|
28
|
+
* SEPARATE concern (the `key-import-publisher` task): this module derives the
|
|
29
|
+
* key + id only, with no node, network, or deploy.
|
|
30
|
+
*/
|
|
31
|
+
import {createPrivateKey, createPublicKey, hkdfSync} from 'node:crypto';
|
|
32
|
+
|
|
33
|
+
/** The frozen `info`-string prefix. The version (`v1`) is encoded HERE. */
|
|
34
|
+
export const IPNS_INFO_PREFIX = 'pinnace:ipns:v1:';
|
|
35
|
+
|
|
36
|
+
/** The master secret: a UTF-8 string or raw bytes. Never read from a node/file. */
|
|
37
|
+
export type Master = string | Uint8Array;
|
|
38
|
+
|
|
39
|
+
/** Inputs to the derivation: the master secret and the frozen per-site keyId. */
|
|
40
|
+
export interface DeriveIpnsInput {
|
|
41
|
+
/** The operator master secret (env-only in production; see config module). */
|
|
42
|
+
master: Master;
|
|
43
|
+
/**
|
|
44
|
+
* The site's frozen, internal key identity — the SOLE per-site KDF input.
|
|
45
|
+
* NOT the ENS name (which is mutable and never enters derivation).
|
|
46
|
+
*/
|
|
47
|
+
keyId: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A fully derived per-site IPNS key. */
|
|
51
|
+
export interface DerivedIpnsKey {
|
|
52
|
+
/** The 32-byte ed25519 private seed (the raw HKDF output). */
|
|
53
|
+
readonly seed: Uint8Array;
|
|
54
|
+
/** The 32-byte raw ed25519 public key. */
|
|
55
|
+
readonly publicKey: Uint8Array;
|
|
56
|
+
/** The IPNS name: a CIDv1 (libp2p-key, base36) `k51...` id. */
|
|
57
|
+
readonly ipnsId: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** PKCS#8 DER prefix wrapping a raw 32-byte ed25519 seed (RFC 8410). */
|
|
61
|
+
const ED25519_PKCS8_PREFIX = Buffer.from(
|
|
62
|
+
'302e020100300506032b657004220420',
|
|
63
|
+
'hex',
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
/** base36 lowercase alphabet (multibase `k`), per the multiformats spec. */
|
|
67
|
+
const BASE36_ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
|
|
68
|
+
|
|
69
|
+
function toBytes(master: Master): Uint8Array {
|
|
70
|
+
return typeof master === 'string' ? new TextEncoder().encode(master) : master;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* HKDF-SHA256(master, salt = "", info = IPNS_INFO_PREFIX + keyId, 32).
|
|
75
|
+
* Returns the 32-byte ed25519 seed. Frozen — see the module doc.
|
|
76
|
+
*/
|
|
77
|
+
function deriveSeed(master: Master, keyId: string): Uint8Array {
|
|
78
|
+
const info = new TextEncoder().encode(IPNS_INFO_PREFIX + keyId);
|
|
79
|
+
const seed = hkdfSync(
|
|
80
|
+
'sha256',
|
|
81
|
+
toBytes(master),
|
|
82
|
+
new Uint8Array(0), // RFC 5869 default (empty) salt — pinned by ADR-0001.
|
|
83
|
+
info,
|
|
84
|
+
32,
|
|
85
|
+
);
|
|
86
|
+
return new Uint8Array(seed);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Derive the raw 32-byte ed25519 public key from a 32-byte private seed. */
|
|
90
|
+
function ed25519PublicKeyFromSeed(seed: Uint8Array): Uint8Array {
|
|
91
|
+
const pkcs8 = Buffer.concat([ED25519_PKCS8_PREFIX, Buffer.from(seed)]);
|
|
92
|
+
const priv = createPrivateKey({key: pkcs8, format: 'der', type: 'pkcs8'});
|
|
93
|
+
const spki = createPublicKey(priv).export({format: 'der', type: 'spki'});
|
|
94
|
+
// An ed25519 SPKI DER is a fixed 12-byte header + the 32 raw public bytes.
|
|
95
|
+
return new Uint8Array(spki.subarray(spki.length - 32));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Encode bytes as multibase-less base36 (lowercase), preserving leading zeros. */
|
|
99
|
+
function base36Encode(bytes: Uint8Array): string {
|
|
100
|
+
const digits = [0];
|
|
101
|
+
for (const byte of bytes) {
|
|
102
|
+
let carry = byte;
|
|
103
|
+
for (let i = 0; i < digits.length; i++) {
|
|
104
|
+
const value = digits[i] * 256 + carry;
|
|
105
|
+
digits[i] = value % 36;
|
|
106
|
+
carry = (value / 36) | 0;
|
|
107
|
+
}
|
|
108
|
+
while (carry > 0) {
|
|
109
|
+
digits.push(carry % 36);
|
|
110
|
+
carry = (carry / 36) | 0;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let out = '';
|
|
114
|
+
for (let i = 0; i < bytes.length && bytes[i] === 0; i++) out += '0';
|
|
115
|
+
for (let i = digits.length - 1; i >= 0; i--)
|
|
116
|
+
out += BASE36_ALPHABET[digits[i]];
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Encode a raw 32-byte ed25519 public key as an IPNS name (`k51...`).
|
|
122
|
+
*
|
|
123
|
+
* The name is a CIDv1 with the libp2p-key multicodec, whose multihash inlines
|
|
124
|
+
* the libp2p PublicKey protobuf via the identity hash (the default for
|
|
125
|
+
* ed25519), rendered in case-insensitive base36 with the `k` multibase prefix:
|
|
126
|
+
*
|
|
127
|
+
* 0x01 (CIDv1) 0x72 (libp2p-key)
|
|
128
|
+
* 0x00 (identity mh) 0x24 (len 36)
|
|
129
|
+
* 0x08 0x01 (protobuf field1 Type = Ed25519) 0x12 0x20 (field2 Data, len 32)
|
|
130
|
+
* <32 raw public key bytes>
|
|
131
|
+
*
|
|
132
|
+
* @see https://specs.ipfs.tech/ipns/ipns-record/
|
|
133
|
+
*/
|
|
134
|
+
function ipnsIdFromPublicKey(publicKey: Uint8Array): string {
|
|
135
|
+
const pbHeader = Uint8Array.from([0x08, 0x01, 0x12, 0x20]);
|
|
136
|
+
const protobuf = new Uint8Array(pbHeader.length + publicKey.length);
|
|
137
|
+
protobuf.set(pbHeader, 0);
|
|
138
|
+
protobuf.set(publicKey, pbHeader.length);
|
|
139
|
+
|
|
140
|
+
const multihash = new Uint8Array(2 + protobuf.length);
|
|
141
|
+
multihash[0] = 0x00; // identity multihash function
|
|
142
|
+
multihash[1] = protobuf.length; // digest length (36)
|
|
143
|
+
multihash.set(protobuf, 2);
|
|
144
|
+
|
|
145
|
+
const cid = new Uint8Array(2 + multihash.length);
|
|
146
|
+
cid[0] = 0x01; // CIDv1
|
|
147
|
+
cid[1] = 0x72; // libp2p-key multicodec
|
|
148
|
+
cid.set(multihash, 2);
|
|
149
|
+
|
|
150
|
+
return 'k' + base36Encode(cid); // multibase base36 (lowercase) prefix.
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Derive the full per-site IPNS key (seed + public key + `k51...` id) from the
|
|
155
|
+
* master secret and the frozen `keyId`. Pure and deterministic — no node,
|
|
156
|
+
* network, or deploy. This is the FROZEN CONTRACT; see the module doc.
|
|
157
|
+
*/
|
|
158
|
+
export function deriveIpnsKey(input: DeriveIpnsInput): DerivedIpnsKey {
|
|
159
|
+
const seed = deriveSeed(input.master, input.keyId);
|
|
160
|
+
const publicKey = ed25519PublicKeyFromSeed(seed);
|
|
161
|
+
const ipnsId = ipnsIdFromPublicKey(publicKey);
|
|
162
|
+
return {seed, publicKey, ipnsId};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Derive-and-print path (user story 22): return a site's `k51...` IPNS id from
|
|
167
|
+
* the master + keyId with NO deploy and NO network, so an operator can set the
|
|
168
|
+
* ENS contenthash to `ipns://<id>` before the first deploy. The id depends ONLY
|
|
169
|
+
* on (master, keyId) — never the ENS name.
|
|
170
|
+
*/
|
|
171
|
+
export function deriveIpnsId(input: DeriveIpnsInput): string {
|
|
172
|
+
return deriveIpnsKey(input).ipnsId;
|
|
173
|
+
}
|