@serfab/cadre-host 1.12.0 → 1.14.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/README.md +332 -423
- package/dist/bin/host.d.ts +6 -3
- package/dist/bin/host.js +301 -505
- package/dist/bin/host.js.map +1 -1
- package/dist/bin/join-output.d.ts +56 -0
- package/dist/bin/join-output.js +92 -0
- package/dist/bin/join-output.js.map +1 -0
- package/dist/bin/nat-output.d.ts +49 -0
- package/dist/bin/nat-output.js +127 -0
- package/dist/bin/nat-output.js.map +1 -0
- package/dist/hosted/claim-details.d.ts +44 -0
- package/dist/hosted/claim-details.js +66 -0
- package/dist/hosted/claim-details.js.map +1 -0
- package/dist/hosted/hosted-node-service.d.ts +318 -0
- package/dist/hosted/hosted-node-service.js +666 -0
- package/dist/hosted/hosted-node-service.js.map +1 -0
- package/dist/hosted/hosted-node-store.d.ts +40 -0
- package/dist/hosted/hosted-node-store.js +126 -0
- package/dist/hosted/hosted-node-store.js.map +1 -0
- package/dist/{donation/donation-supervisor.d.ts → hosted/hosted-node-supervisor.d.ts} +70 -53
- package/dist/hosted/hosted-node-supervisor.js +396 -0
- package/dist/hosted/hosted-node-supervisor.js.map +1 -0
- package/dist/hosted/hosted-node-watcher.d.ts +83 -0
- package/dist/hosted/hosted-node-watcher.js +224 -0
- package/dist/hosted/hosted-node-watcher.js.map +1 -0
- package/dist/hosted/index.d.ts +16 -0
- package/dist/hosted/index.js +13 -0
- package/dist/hosted/index.js.map +1 -0
- package/dist/hosted/node-status.d.ts +35 -0
- package/dist/hosted/node-status.js +61 -0
- package/dist/hosted/node-status.js.map +1 -0
- package/dist/hosted/types.d.ts +134 -0
- package/dist/hosted/types.js +22 -0
- package/dist/hosted/types.js.map +1 -0
- package/dist/index.d.ts +12 -24
- package/dist/index.js +9 -19
- package/dist/index.js.map +1 -1
- package/dist/installer/config.d.ts +4 -34
- package/dist/installer/config.js +3 -18
- package/dist/installer/config.js.map +1 -1
- package/dist/installer/index.d.ts +7 -19
- package/dist/installer/index.js +14 -69
- package/dist/installer/index.js.map +1 -1
- package/dist/installer/paths.d.ts +0 -2
- package/dist/installer/paths.js +0 -4
- package/dist/installer/paths.js.map +1 -1
- package/dist/installer/wizard.d.ts +0 -7
- package/dist/installer/wizard.js +1 -8
- package/dist/installer/wizard.js.map +1 -1
- package/dist/nat/address-resolver.d.ts +18 -17
- package/dist/nat/address-resolver.js +79 -25
- package/dist/nat/address-resolver.js.map +1 -1
- package/dist/nat/address-watch.d.ts +37 -0
- package/dist/nat/address-watch.js +84 -0
- package/dist/nat/address-watch.js.map +1 -0
- package/dist/nat/ddns/duckdns.d.ts +2 -1
- package/dist/nat/ddns/duckdns.js +5 -1
- package/dist/nat/ddns/duckdns.js.map +1 -1
- package/dist/nat/ddns/index.d.ts +6 -0
- package/dist/nat/ddns/index.js.map +1 -1
- package/dist/nat/index.d.ts +12 -9
- package/dist/nat/index.js +6 -5
- package/dist/nat/index.js.map +1 -1
- package/dist/nat/nat-service.d.ts +203 -77
- package/dist/nat/nat-service.js +602 -237
- package/dist/nat/nat-service.js.map +1 -1
- package/dist/nat/nat-store.d.ts +12 -5
- package/dist/nat/nat-store.js +114 -30
- package/dist/nat/nat-store.js.map +1 -1
- package/dist/nat/port-mapper.d.ts +83 -79
- package/dist/nat/port-mapper.js +148 -189
- package/dist/nat/port-mapper.js.map +1 -1
- package/dist/nat/reachability.d.ts +31 -15
- package/dist/nat/reachability.js +97 -22
- package/dist/nat/reachability.js.map +1 -1
- package/dist/nat/types.d.ts +78 -27
- package/dist/nat/types.js +8 -5
- package/dist/nat/types.js.map +1 -1
- package/dist/orchestrator/host-process-orchestrator.d.ts +29 -71
- package/dist/orchestrator/host-process-orchestrator.js +78 -218
- package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
- package/dist/{installer/identity.d.ts → orchestrator/identity-file.d.ts} +5 -5
- package/dist/{installer/identity.js → orchestrator/identity-file.js} +6 -6
- package/dist/orchestrator/identity-file.js.map +1 -0
- package/dist/orchestrator/index.d.ts +2 -3
- package/dist/orchestrator/index.js +1 -1
- package/dist/orchestrator/index.js.map +1 -1
- package/dist/orchestrator/node-identity.d.ts +6 -6
- package/dist/orchestrator/node-identity.js +7 -7
- package/dist/orchestrator/node-identity.js.map +1 -1
- package/dist/orchestrator/port-allocator.d.ts +5 -6
- package/dist/orchestrator/port-allocator.js +9 -11
- package/dist/orchestrator/port-allocator.js.map +1 -1
- package/dist/orchestrator/state-store.d.ts +3 -5
- package/dist/orchestrator/state-store.js +2 -10
- package/dist/orchestrator/state-store.js.map +1 -1
- package/dist/orchestrator/types.d.ts +33 -22
- package/dist/orchestrator/types.js.map +1 -1
- package/dist/server/error-handler.d.ts +5 -25
- package/dist/server/error-handler.js +11 -74
- package/dist/server/error-handler.js.map +1 -1
- package/dist/server/events/types.d.ts +7 -11
- package/dist/server/events/types.js +2 -1
- package/dist/server/events/types.js.map +1 -1
- package/dist/server/index.d.ts +11 -37
- package/dist/server/index.js +30 -48
- package/dist/server/index.js.map +1 -1
- package/dist/server/routes/hosted-nodes.d.ts +28 -0
- package/dist/server/routes/hosted-nodes.js +68 -0
- package/dist/server/routes/hosted-nodes.js.map +1 -0
- package/dist/server/routes/nat.d.ts +4 -1
- package/dist/server/routes/nat.js +13 -2
- package/dist/server/routes/nat.js.map +1 -1
- package/dist/server/routes/nodes.d.ts +6 -20
- package/dist/server/routes/nodes.js +7 -86
- package/dist/server/routes/nodes.js.map +1 -1
- package/dist/server/routes/settings.d.ts +2 -7
- package/dist/server/routes/settings.js +3 -7
- package/dist/server/routes/settings.js.map +1 -1
- package/dist/server/routes/status.d.ts +2 -25
- package/dist/server/routes/status.js +1 -11
- package/dist/server/routes/status.js.map +1 -1
- package/dist/server/server.js +8 -8
- package/dist/server/settings-store.d.ts +2 -2
- package/dist/server/settings-store.js +2 -2
- package/dist/server/static.js +1 -2
- package/dist/server/static.js.map +1 -1
- package/dist/ui/assets/index-4FWuU8ye.js +19 -0
- package/dist/ui/assets/index-4FWuU8ye.js.map +1 -0
- package/dist/ui/assets/index-Di9Q4d01.css +1 -0
- package/dist/ui/index.html +2 -2
- package/dist/update/index.d.ts +2 -2
- package/dist/update/index.js +2 -2
- package/dist/update/store.d.ts +1 -1
- package/dist/update/store.js +1 -1
- package/dist/update/types.d.ts +1 -1
- package/dist/update/types.js +1 -1
- package/package.json +5 -6
- package/src/__tests__/cli-nat.smoke.test.ts +219 -212
- package/src/__tests__/cli.smoke.test.ts +1 -1
- package/src/__tests__/global-setup.ts +2 -3
- package/src/__tests__/orchestrator-claim-secret.test.ts +112 -0
- package/src/__tests__/orchestrator-handle-contract.ts +134 -134
- package/src/__tests__/orchestrator-node-identity.test.ts +182 -203
- package/src/__tests__/orchestrator-ports.test.ts +124 -143
- package/src/__tests__/orchestrator-push.test.ts +138 -146
- package/src/__tests__/orchestrator.test.ts +1019 -1063
- package/src/bin/host.ts +368 -553
- package/src/bin/join-output.ts +120 -0
- package/src/bin/nat-output.ts +177 -0
- package/src/{donation → hosted}/__tests__/fake-orchestrator.test.ts +173 -173
- package/src/{donation → hosted}/__tests__/fake-orchestrator.ts +265 -243
- package/src/hosted/__tests__/hosted-node-service.test.ts +609 -0
- package/src/hosted/__tests__/hosted-node-supervisor.test.ts +474 -0
- package/src/hosted/__tests__/test-invitation.ts +33 -0
- package/src/hosted/claim-details.ts +96 -0
- package/src/hosted/hosted-node-service.ts +795 -0
- package/src/hosted/hosted-node-store.ts +144 -0
- package/src/hosted/hosted-node-supervisor.ts +439 -0
- package/src/hosted/hosted-node-watcher.ts +250 -0
- package/src/hosted/index.ts +53 -0
- package/src/hosted/node-status.ts +71 -0
- package/src/hosted/types.ts +153 -0
- package/src/index.ts +72 -91
- package/src/installer/__tests__/config.test.ts +15 -40
- package/src/installer/__tests__/installer.smoke.test.ts +5 -86
- package/src/installer/__tests__/wizard.test.ts +56 -64
- package/src/installer/config.ts +135 -175
- package/src/installer/index.ts +15 -83
- package/src/installer/paths.ts +104 -109
- package/src/installer/wizard.ts +87 -103
- package/src/nat/__tests__/address-resolver.test.ts +107 -82
- package/src/nat/__tests__/nat-service.test.ts +433 -408
- package/src/nat/__tests__/nat-store.test.ts +127 -93
- package/src/nat/__tests__/port-mapper.test.ts +30 -191
- package/src/nat/__tests__/reachability.test.ts +96 -13
- package/src/nat/address-resolver.ts +94 -52
- package/src/nat/address-watch.ts +92 -0
- package/src/nat/ddns/duckdns.ts +5 -1
- package/src/nat/ddns/index.ts +6 -0
- package/src/nat/index.ts +87 -71
- package/src/nat/nat-service.ts +889 -501
- package/src/nat/nat-store.ts +237 -154
- package/src/nat/port-mapper.ts +241 -329
- package/src/nat/reachability.ts +134 -25
- package/src/nat/types.ts +209 -159
- package/src/{installer/__tests__/identity.test.ts → orchestrator/__tests__/identity-file.test.ts} +34 -34
- package/src/orchestrator/__tests__/node-identity.test.ts +62 -62
- package/src/orchestrator/host-process-orchestrator.ts +81 -243
- package/src/{installer/identity.ts → orchestrator/identity-file.ts} +56 -56
- package/src/orchestrator/index.ts +9 -10
- package/src/orchestrator/node-identity.ts +41 -41
- package/src/orchestrator/port-allocator.ts +71 -73
- package/src/orchestrator/state-store.ts +76 -90
- package/src/orchestrator/types.ts +34 -23
- package/src/server/__tests__/bus.test.ts +38 -38
- package/src/server/__tests__/error-handler.test.ts +81 -118
- package/src/server/__tests__/fakes.ts +40 -44
- package/src/server/__tests__/hosted-nodes-route.test.ts +160 -0
- package/src/server/__tests__/nodes-route.test.ts +114 -283
- package/src/server/__tests__/publishers.test.ts +193 -180
- package/src/server/__tests__/server.smoke.test.ts +134 -223
- package/src/server/__tests__/settings-route.test.ts +179 -195
- package/src/server/__tests__/sse-route.test.ts +119 -119
- package/src/server/__tests__/status-route.test.ts +82 -121
- package/src/server/error-handler.ts +112 -178
- package/src/server/events/types.ts +20 -24
- package/src/server/index.ts +223 -267
- package/src/server/routes/hosted-nodes.ts +83 -0
- package/src/server/routes/nat.ts +75 -63
- package/src/server/routes/nodes.ts +96 -188
- package/src/server/routes/settings.ts +115 -123
- package/src/server/routes/status.ts +102 -134
- package/src/server/server.ts +8 -8
- package/src/server/settings-store.ts +2 -2
- package/src/server/static.ts +1 -2
- package/src/update/index.ts +2 -2
- package/src/update/store.ts +1 -1
- package/src/update/types.ts +1 -1
- package/dist/auth/duration.d.ts +0 -1
- package/dist/auth/duration.js +0 -32
- package/dist/auth/duration.js.map +0 -1
- package/dist/auth/index.d.ts +0 -11
- package/dist/auth/index.js +0 -9
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/self-label.d.ts +0 -36
- package/dist/auth/self-label.js +0 -57
- package/dist/auth/self-label.js.map +0 -1
- package/dist/auth/trust-circle-store.d.ts +0 -26
- package/dist/auth/trust-circle-store.js +0 -129
- package/dist/auth/trust-circle-store.js.map +0 -1
- package/dist/auth/trust-circle.d.ts +0 -156
- package/dist/auth/trust-circle.js +0 -307
- package/dist/auth/trust-circle.js.map +0 -1
- package/dist/auth/types.d.ts +0 -84
- package/dist/auth/types.js +0 -24
- package/dist/auth/types.js.map +0 -1
- package/dist/donation/donation-service.d.ts +0 -389
- package/dist/donation/donation-service.js +0 -828
- package/dist/donation/donation-service.js.map +0 -1
- package/dist/donation/donation-store.d.ts +0 -51
- package/dist/donation/donation-store.js +0 -152
- package/dist/donation/donation-store.js.map +0 -1
- package/dist/donation/donation-supervisor.js +0 -364
- package/dist/donation/donation-supervisor.js.map +0 -1
- package/dist/donation/grant-service.d.ts +0 -80
- package/dist/donation/grant-service.js +0 -210
- package/dist/donation/grant-service.js.map +0 -1
- package/dist/donation/grant-store.d.ts +0 -38
- package/dist/donation/grant-store.js +0 -135
- package/dist/donation/grant-store.js.map +0 -1
- package/dist/donation/index.d.ts +0 -20
- package/dist/donation/index.js +0 -17
- package/dist/donation/index.js.map +0 -1
- package/dist/donation/types.d.ts +0 -224
- package/dist/donation/types.js +0 -40
- package/dist/donation/types.js.map +0 -1
- package/dist/installer/identity.js.map +0 -1
- package/dist/owner/index.d.ts +0 -11
- package/dist/owner/index.js +0 -11
- package/dist/owner/index.js.map +0 -1
- package/dist/owner/owner-node-client.d.ts +0 -110
- package/dist/owner/owner-node-client.js +0 -184
- package/dist/owner/owner-node-client.js.map +0 -1
- package/dist/server/routes/bootstrap-node-validation.d.ts +0 -115
- package/dist/server/routes/bootstrap-node-validation.js +0 -197
- package/dist/server/routes/bootstrap-node-validation.js.map +0 -1
- package/dist/server/routes/grants-admin.d.ts +0 -29
- package/dist/server/routes/grants-admin.js +0 -58
- package/dist/server/routes/grants-admin.js.map +0 -1
- package/dist/server/routes/grants.d.ts +0 -28
- package/dist/server/routes/grants.js +0 -165
- package/dist/server/routes/grants.js.map +0 -1
- package/dist/server/routes/provision-request-validation.d.ts +0 -45
- package/dist/server/routes/provision-request-validation.js +0 -97
- package/dist/server/routes/provision-request-validation.js.map +0 -1
- package/dist/server/routes/strands.d.ts +0 -20
- package/dist/server/routes/strands.js +0 -38
- package/dist/server/routes/strands.js.map +0 -1
- package/dist/server/routes/trust-circle.d.ts +0 -17
- package/dist/server/routes/trust-circle.js +0 -35
- package/dist/server/routes/trust-circle.js.map +0 -1
- package/dist/strands/index.d.ts +0 -7
- package/dist/strands/index.js +0 -6
- package/dist/strands/index.js.map +0 -1
- package/dist/strands/strand-service.d.ts +0 -50
- package/dist/strands/strand-service.js +0 -103
- package/dist/strands/strand-service.js.map +0 -1
- package/dist/strands/types.d.ts +0 -93
- package/dist/strands/types.js +0 -23
- package/dist/strands/types.js.map +0 -1
- package/dist/ui/assets/index-C6nELNfC.js +0 -23
- package/dist/ui/assets/index-C6nELNfC.js.map +0 -1
- package/dist/ui/assets/index-TtIYSeZO.css +0 -1
- package/src/__tests__/cli-invite.smoke.test.ts +0 -192
- package/src/__tests__/host-owner.smoke.test.ts +0 -123
- package/src/__tests__/orchestrator-owner.test.ts +0 -234
- package/src/__tests__/orchestrator-pin-keys.test.ts +0 -152
- package/src/auth/__tests__/duration.test.ts +0 -28
- package/src/auth/__tests__/self-label.test.ts +0 -85
- package/src/auth/__tests__/trust-circle-integration.test.ts +0 -152
- package/src/auth/__tests__/trust-circle-store.test.ts +0 -115
- package/src/auth/__tests__/trust-circle.test.ts +0 -445
- package/src/auth/duration.ts +0 -33
- package/src/auth/index.ts +0 -21
- package/src/auth/self-label.ts +0 -74
- package/src/auth/trust-circle-store.ts +0 -155
- package/src/auth/trust-circle.ts +0 -395
- package/src/auth/types.ts +0 -99
- package/src/donation/__tests__/donation-service.test.ts +0 -1193
- package/src/donation/__tests__/donation-supervisor.test.ts +0 -548
- package/src/donation/__tests__/grant-service.test.ts +0 -213
- package/src/donation/__tests__/grant-store.test.ts +0 -113
- package/src/donation/donation-service.ts +0 -1033
- package/src/donation/donation-store.ts +0 -176
- package/src/donation/donation-supervisor.ts +0 -411
- package/src/donation/grant-service.ts +0 -252
- package/src/donation/grant-store.ts +0 -152
- package/src/donation/index.ts +0 -65
- package/src/donation/types.ts +0 -260
- package/src/owner/__tests__/owner-node-client.test.ts +0 -194
- package/src/owner/index.ts +0 -12
- package/src/owner/owner-node-client.ts +0 -259
- package/src/server/__tests__/bootstrap-node-validation.test.ts +0 -138
- package/src/server/__tests__/grants-admin-route.test.ts +0 -171
- package/src/server/__tests__/grants-route.test.ts +0 -394
- package/src/server/__tests__/strands-route.test.ts +0 -234
- package/src/server/routes/bootstrap-node-validation.ts +0 -206
- package/src/server/routes/grants-admin.ts +0 -74
- package/src/server/routes/grants.ts +0 -193
- package/src/server/routes/provision-request-validation.ts +0 -105
- package/src/server/routes/strands.ts +0 -54
- package/src/server/routes/trust-circle.ts +0 -49
- package/src/strands/__tests__/strand-service.test.ts +0 -165
- package/src/strands/index.ts +0 -14
- package/src/strands/strand-service.ts +0 -135
- package/src/strands/types.ts +0 -102
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shape validation for the requester-supplied `bootstrapNodes` of `POST /grants`
|
|
3
|
-
* — the requester's control-network addresses the donated node is started with
|
|
4
|
-
* (written into the child's `cadre.json` as `controlNetwork.bootstrapNodes` by
|
|
5
|
-
* `orchestrator/host-process-orchestrator.ts`) and hands to `@libp2p/bootstrap`.
|
|
6
|
-
*
|
|
7
|
-
* Its own module rather than part of `grants.ts`: the per-entry rule is a duplicate
|
|
8
|
-
* of cadre-provider's copy (`packages/cadre-provider/src/server/bootstrap-node-validation.ts`)
|
|
9
|
-
* and must stay identical to it, so it is worth reading, testing and changing on
|
|
10
|
-
* its own, without a routing file around it — the same arrangement cadre-provider
|
|
11
|
-
* uses for its restatement of cadre-core's owner-key rule.
|
|
12
|
-
*
|
|
13
|
-
* ## An empty list is accepted — where this copy differs, on purpose
|
|
14
|
-
*
|
|
15
|
-
* The field may be absent or `[]`; cadre-provider's copy still requires at least one
|
|
16
|
-
* address. A lent node's requester is typically a phone, and a phone has no address
|
|
17
|
-
* to give — its node listens on nothing and can only dial out. Such a requester sends
|
|
18
|
-
* no addresses and dials the node itself, at the WebSocket address
|
|
19
|
-
* `GET /grants/:id/peer` reports. The node starts with no bootstrap peers and waits,
|
|
20
|
-
* and admits that first inbound connection because it holds no authorized members
|
|
21
|
-
* yet (`admitInboundControlConnection` / `authorizeInboundControlStream` in
|
|
22
|
-
* `cadre-core/src/cadre-node.ts`). A provider container has no such requester yet, so
|
|
23
|
-
* there an empty list is still the node-comes-up-alone failure described below
|
|
24
|
-
* (`backlog/feat-provider-drone-reachable-by-phone`).
|
|
25
|
-
*
|
|
26
|
-
* The difference is at the list level only: every entry that IS given must pass the
|
|
27
|
-
* rule below, which is identical in both packages.
|
|
28
|
-
*
|
|
29
|
-
* ## The rule, and why each clause is there
|
|
30
|
-
*
|
|
31
|
-
* An entry is usable exactly when it is a string, parses as a multiaddr, carries
|
|
32
|
-
* a `/p2p/<peerId>` component whose peer id decodes, and says where that peer is.
|
|
33
|
-
* Each clause corresponds to a distinct downstream failure the caller would
|
|
34
|
-
* otherwise never be told about — the value is forwarded verbatim into the spawned
|
|
35
|
-
* child, so every one of these surfaces where the caller cannot see it:
|
|
36
|
-
*
|
|
37
|
-
* - **Not parsable as a multiaddr → the child dies at boot.** `@libp2p/bootstrap`'s
|
|
38
|
-
* constructor maps `multiaddr()` over the whole list *before* filtering anything,
|
|
39
|
-
* and `multiaddr()` throws on a string that does not start with `/` (and on a
|
|
40
|
-
* non-string outright). libp2p construction throws and the node never starts.
|
|
41
|
-
* - **No `/p2p/<peerId>` → the node starts and never joins.** The quiet one.
|
|
42
|
-
* `/ip4/127.0.0.1/tcp/4001` parses fine, then `@libp2p/bootstrap` filters it out
|
|
43
|
-
* ("invalid bootstrap multiaddr without peer id", logged inside the child) and
|
|
44
|
-
* the node comes up with *zero* bootstrap peers: healthy-looking, permanently
|
|
45
|
-
* alone, never reaching the party it was created for. cadre-core's
|
|
46
|
-
* `getBootstrapPeerIds` (`packages/cadre-core/src/cadre-node.ts`) skips such an
|
|
47
|
-
* entry silently for the same reason.
|
|
48
|
-
* - **Malformed peer id → the child dies at boot.** `multiaddr()` does *not*
|
|
49
|
-
* validate the `/p2p/` value — parsing `/ip4/1.2.3.4/tcp/1/p2p/12D3KooReq` yields
|
|
50
|
-
* a `p2p` component whose value is the junk string unchanged — but
|
|
51
|
-
* `@libp2p/bootstrap` then calls `peerIdFromString` on it, unguarded, and it
|
|
52
|
-
* throws `Incorrect length`. A truncated copy-paste of a 52-character peer id is
|
|
53
|
-
* the likeliest human typo in this field.
|
|
54
|
-
* - **No location at all → the node starts and never joins.** `/p2p/<peerId>` (and
|
|
55
|
-
* `/p2p/<relay>/p2p-circuit/p2p/<target>`) parses and names a decodable peer, and
|
|
56
|
-
* `@libp2p/bootstrap` keeps it — but it says nothing about *where* the peer is, so
|
|
57
|
-
* libp2p finds no transport for it and the dial fails with no valid addresses.
|
|
58
|
-
* The node comes up with a bootstrap peer it can never reach, which looks the same
|
|
59
|
-
* from outside as having none. Only the total absence of a location is rejected:
|
|
60
|
-
* which transports the child can actually dial is the embedder's choice
|
|
61
|
-
* (`NetworkConfig.transports`), invisible from here, so a partial address like
|
|
62
|
-
* `/ip4/1.2.3.4/p2p/<peerId>` is deliberately left to the child.
|
|
63
|
-
*
|
|
64
|
-
* The peer id is read the way `@libp2p/bootstrap` reads it — the `p2p` components
|
|
65
|
-
* of `getComponents()` — rather than through the deprecated `getPeerId()`, so the
|
|
66
|
-
* two cannot disagree about which part of the address is the peer. Bootstrap uses
|
|
67
|
-
* only the *last* `p2p` component; this checks **every** one, so the relay half of
|
|
68
|
-
* a `…/p2p/<relay>/p2p-circuit/p2p/<target>` address must decode too. An address
|
|
69
|
-
* naming an undecodable relay is not dialable either, and bootstrap's own
|
|
70
|
-
* `P2P.matches` filter accepts any address carrying a `p2p` component, so nothing
|
|
71
|
-
* this accepts is dropped there.
|
|
72
|
-
*
|
|
73
|
-
* Requiring a multiaddr also closes a downstream ambiguity on the *other* side of
|
|
74
|
-
* this rule: cadre-provider joins its list with `,` into `CADRE_BOOTSTRAP_NODES`
|
|
75
|
-
* and cadre-cli's env loader splits it back on `,`, so an entry containing a comma
|
|
76
|
-
* would silently become two addresses there. This host writes JSON rather than an
|
|
77
|
-
* env var and so is not exposed to that, but the two per-entry rules are kept
|
|
78
|
-
* identical — see the note about manual upkeep below.
|
|
79
|
-
*
|
|
80
|
-
* Reachability is deliberately NOT checked: a well-formed address for a peer that
|
|
81
|
-
* happens to be down is indistinguishable here from one that is up, and no
|
|
82
|
-
* boundary check can tell them apart. This validates the *shape* the child must be
|
|
83
|
-
* able to parse, nothing more.
|
|
84
|
-
*
|
|
85
|
-
* Keeping the two per-entry rules in step is **manual** — neither package can see
|
|
86
|
-
* the other's rule, so no test can compare them. What the tests give instead is a
|
|
87
|
-
* tripwire on each side: `../__tests__/bootstrap-node-validation.test.ts` here and
|
|
88
|
-
* `packages/cadre-provider/src/server/__tests__/bootstrap-node-validation.test.ts`
|
|
89
|
-
* there each pin their own copy to the same per-entry accept/reject table (the
|
|
90
|
-
* empty-list row is the one that differs), so changing either rule fails that
|
|
91
|
-
* package's own suite — and the comment above the rule you just changed is what
|
|
92
|
-
* points at the other copy.
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
import debug from 'debug';
|
|
96
|
-
import { CODE_P2P, CODE_P2P_CIRCUIT, multiaddr, type Multiaddr } from '@multiformats/multiaddr';
|
|
97
|
-
import { peerIdFromString } from '@libp2p/peer-id';
|
|
98
|
-
|
|
99
|
-
const log = debug('cadre:host:bootstrap-nodes');
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Longest rejected address echoed back in an error message. Generous compared to
|
|
103
|
-
* the owner-key limit because a legitimate address is long: a plain
|
|
104
|
-
* `/ip4/…/tcp/…/p2p/<52 chars>` runs ~80 characters and a relayed
|
|
105
|
-
* `…/p2p-circuit/p2p/<52 chars>` about 150, so a smaller cap would truncate the
|
|
106
|
-
* very values an operator needs to read back.
|
|
107
|
-
*/
|
|
108
|
-
const REJECTED_ADDRESS_ECHO_LIMIT = 192;
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Render a rejected address for an error message, capped: `bootstrapNodes`
|
|
112
|
-
* arrives from a caller over the network, so an unbounded echo would let one junk
|
|
113
|
-
* string become a megabyte of log line.
|
|
114
|
-
*/
|
|
115
|
-
function describeRejectedAddress(value: string): string {
|
|
116
|
-
return value.length <= REJECTED_ADDRESS_ECHO_LIMIT
|
|
117
|
-
? value
|
|
118
|
-
: `${value.slice(0, REJECTED_ADDRESS_ECHO_LIMIT)}… (${value.length} chars)`;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** The message of a thrown parse/decode failure, whatever the thrower used. */
|
|
122
|
-
function reasonOf(error: unknown): string {
|
|
123
|
-
return error instanceof Error ? error.message : String(error);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Apply the rule above to one entry. Returns the trimmed value, so the create
|
|
128
|
-
* provision request carries exactly what was validated.
|
|
129
|
-
*/
|
|
130
|
-
function validateBootstrapNode(value: string): { node: string } | { error: string } {
|
|
131
|
-
const trimmed = value.trim();
|
|
132
|
-
if (trimmed.length === 0) {
|
|
133
|
-
return { error: 'bootstrapNodes entries must not be empty or whitespace-only' };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
let address: Multiaddr;
|
|
137
|
-
try {
|
|
138
|
-
address = multiaddr(trimmed);
|
|
139
|
-
} catch (error) {
|
|
140
|
-
log('rejecting bootstrapNodes entry: %s', reasonOf(error));
|
|
141
|
-
return {
|
|
142
|
-
error: `bootstrapNodes entries must be multiaddrs (could not parse "${describeRejectedAddress(trimmed)}": ${reasonOf(error)})`,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const components = address.getComponents();
|
|
147
|
-
const peerIds = components.filter(component => component.code === CODE_P2P);
|
|
148
|
-
if (peerIds.length === 0) {
|
|
149
|
-
return {
|
|
150
|
-
error: `bootstrapNodes entries must include a /p2p/<peerId> component ("${describeRejectedAddress(trimmed)}" names no peer, so the node would drop it and start with no bootstrap peers)`,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
if (!components.some(component => component.code !== CODE_P2P && component.code !== CODE_P2P_CIRCUIT)) {
|
|
155
|
-
return {
|
|
156
|
-
error: `bootstrapNodes entries must name where to reach the peer ("${describeRejectedAddress(trimmed)}" names only peer ids, so no transport can dial it and the node would start with no reachable bootstrap peers)`,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
for (const { value } of peerIds) {
|
|
161
|
-
try {
|
|
162
|
-
peerIdFromString(value ?? '');
|
|
163
|
-
} catch (error) {
|
|
164
|
-
log('rejecting bootstrapNodes peer id: %s', reasonOf(error));
|
|
165
|
-
return {
|
|
166
|
-
error: `bootstrapNodes entries must carry a decodable peer id ("${describeRejectedAddress(trimmed)}" names peer "${describeRejectedAddress(value ?? '')}", which does not decode: ${reasonOf(error)})`,
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return { node: trimmed };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Validate the optional `bootstrapNodes` field of a provision request: absent, or an
|
|
176
|
-
* array of dialable control-network addresses (see {@link validateBootstrapNode}).
|
|
177
|
-
* Absent and `[]` alike mean the requester dials the node itself — see "An empty list
|
|
178
|
-
* is accepted" in the module comment.
|
|
179
|
-
*
|
|
180
|
-
* Provisioning is the last point at which the requester can still fix a typo, so
|
|
181
|
-
* the shape is checked here: without it a bad address is answered 201 and then
|
|
182
|
-
* produces a donated node that either dies at boot or comes up permanently alone
|
|
183
|
-
* — the requester learning about it, if ever, from donation status rather than
|
|
184
|
-
* from a 400 naming the bad address.
|
|
185
|
-
*
|
|
186
|
-
* Every rejection names the offending entry: with several addresses in one
|
|
187
|
-
* request the message is the only thing that says WHICH one.
|
|
188
|
-
*
|
|
189
|
-
* @returns the trimmed addresses (`[]` when none were given), so the provision request carries exactly what was validated.
|
|
190
|
-
*/
|
|
191
|
-
export function validateBootstrapNodes(value: unknown): { nodes: string[] } | { error: string } {
|
|
192
|
-
if (value === undefined) {
|
|
193
|
-
return { nodes: [] };
|
|
194
|
-
}
|
|
195
|
-
if (!Array.isArray(value) || !value.every((entry): entry is string => typeof entry === 'string')) {
|
|
196
|
-
return { error: 'bootstrapNodes must be an array of strings' };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const nodes: string[] = [];
|
|
200
|
-
for (const entry of value) {
|
|
201
|
-
const checked = validateBootstrapNode(entry);
|
|
202
|
-
if ('error' in checked) return checked;
|
|
203
|
-
nodes.push(checked.node);
|
|
204
|
-
}
|
|
205
|
-
return { nodes };
|
|
206
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Donation grant **admin** HTTP routes — thin adapters around
|
|
3
|
-
* `GrantAdminHandlers` (`createGrantAdminHandlers(service, donations)`).
|
|
4
|
-
*
|
|
5
|
-
* Mount path: `/grants-admin` — the admin surface the `cadre-host grant` CLI
|
|
6
|
-
* targets. It is **loopback, no bearer**: same-machine admin, matching
|
|
7
|
-
* cadre-host's local-UI "no login" posture (see docs/cadre-host.md §
|
|
8
|
-
* Security posture). This is distinct from the grantee-facing `/grants`
|
|
9
|
-
* provisioning surface (`routes/grants.ts`), which carries the bearer gate.
|
|
10
|
-
*
|
|
11
|
-
* Clients: the `cadre-host grant` CLI and the local UI's Grants page. The list
|
|
12
|
-
* reports each grant's live-node count and the donations a revoke would end.
|
|
13
|
-
*
|
|
14
|
-
* Revoking a grant also terminates the nodes donated under it unless the
|
|
15
|
-
* caller passes `?keepNodes=true`; `DELETE /grants-admin/donations/:id` ends a
|
|
16
|
-
* single donated node. Those are the host's only teardown paths once a grant is
|
|
17
|
-
* revoked — the grantee's own `DELETE /grants/:id` is refused from then on.
|
|
18
|
-
*
|
|
19
|
-
* Each successful mutation publishes a `grants-changed` bus event, so an open
|
|
20
|
-
* Grants page refreshes when the CLI acts.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import type { FastifyInstance } from 'fastify';
|
|
24
|
-
|
|
25
|
-
import type { GrantAdminHandlers } from '../../donation/types.js';
|
|
26
|
-
import type { EventBus } from '../events/bus.js';
|
|
27
|
-
|
|
28
|
-
export interface GrantsAdminRoutesOptions {
|
|
29
|
-
handlers: GrantAdminHandlers;
|
|
30
|
-
events: EventBus;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function registerGrantsAdminRoutes(app: FastifyInstance, opts: GrantsAdminRoutesOptions): void {
|
|
34
|
-
const { handlers, events } = opts;
|
|
35
|
-
|
|
36
|
-
app.get('/grants-admin', async () => {
|
|
37
|
-
return handlers.listGrants();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
app.post('/grants-admin', async (request) => {
|
|
41
|
-
const body = (request.body ?? {}) as { label?: unknown; maxNodes?: unknown; ttlMs?: unknown };
|
|
42
|
-
const label = typeof body.label === 'string' ? body.label : '';
|
|
43
|
-
const args: { label: string; maxNodes?: number; ttlMs?: number } = { label };
|
|
44
|
-
if (typeof body.maxNodes === 'number') args.maxNodes = body.maxNodes;
|
|
45
|
-
if (typeof body.ttlMs === 'number') args.ttlMs = body.ttlMs;
|
|
46
|
-
const result = await handlers.postGrant(args);
|
|
47
|
-
events.publish({ type: 'grants-changed', kind: 'issued' });
|
|
48
|
-
return result;
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
app.delete<{ Params: { token: string }; Querystring: { keepNodes?: string } }>(
|
|
52
|
-
'/grants-admin/:token',
|
|
53
|
-
async (request) => {
|
|
54
|
-
const keepNodes = isTrueFlag(request.query.keepNodes);
|
|
55
|
-
const { terminated } = await handlers.deleteGrant(request.params.token, { keepNodes });
|
|
56
|
-
events.publish({ type: 'grants-changed', kind: 'revoked' });
|
|
57
|
-
return { ok: true, terminated };
|
|
58
|
-
},
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
// Two path segments past `/grants-admin`, so it never collides with `/:token`.
|
|
62
|
-
const { terminateDonation } = handlers;
|
|
63
|
-
if (terminateDonation) {
|
|
64
|
-
app.delete<{ Params: { id: string } }>('/grants-admin/donations/:id', async (request) => {
|
|
65
|
-
await terminateDonation(request.params.id);
|
|
66
|
-
events.publish({ type: 'grants-changed', kind: 'terminated' });
|
|
67
|
-
return { ok: true };
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function isTrueFlag(value: string | undefined): boolean {
|
|
73
|
-
return value === 'true' || value === '1';
|
|
74
|
-
}
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Grantee-facing donation routes — the HTTP surface an external cadre authority
|
|
3
|
-
* (a friend's phone) drives to have this host donate a node into *its* cadre.
|
|
4
|
-
*
|
|
5
|
-
* Mount path: `/grants`. Distinct from the loopback-only `/grants-admin` surface
|
|
6
|
-
* (issue/list/revoke, no bearer): this one is **bearer-gated** — every request
|
|
7
|
-
* carries `Authorization: Bearer <grant-token>`, and a grantee sees only the
|
|
8
|
-
* donations its own grant authorized (the single cross-grantee boundary,
|
|
9
|
-
* analogous to cadre-provider's per-customer ownership check).
|
|
10
|
-
*
|
|
11
|
-
* Reachability is out of scope here: in v1 this mounts on the loopback
|
|
12
|
-
* management server, same as the trust-circle / NAT surfaces. Physically exposing
|
|
13
|
-
* it to a remote phone is `backlog/feat-cadre-host-wan-grant-reachability`.
|
|
14
|
-
*
|
|
15
|
-
* Errors: handlers either send a `{ ok:false, error:{code,message} }` envelope
|
|
16
|
-
* inline (auth / body validation) or let the `DonationService` throw a
|
|
17
|
-
* `DonationError`, which `server/error-handler.ts` maps to the matching HTTP
|
|
18
|
-
* status and the same envelope.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
|
22
|
-
|
|
23
|
-
import type { DonationService } from '../../donation/donation-service.js';
|
|
24
|
-
import type { DonationView, GrantValidator } from '../../donation/types.js';
|
|
25
|
-
import { validateProvisionRequest } from './provision-request-validation.js';
|
|
26
|
-
|
|
27
|
-
export interface GrantsRoutesOptions {
|
|
28
|
-
donations: DonationService;
|
|
29
|
-
/** Validates the presented bearer for non-provisioning ops (provision revalidates internally). */
|
|
30
|
-
grants: GrantValidator;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Uniform error envelope (matches `error-handler.ts` and the provider API). */
|
|
34
|
-
function errorResponse(reply: FastifyReply, code: string, message: string, status: number) {
|
|
35
|
-
return reply.status(status).send({ ok: false, error: { code, message } });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Pull the `Bearer <token>` value out of the Authorization header, or undefined. */
|
|
39
|
-
function parseBearer(header: string | undefined): string | undefined {
|
|
40
|
-
if (!header) return undefined;
|
|
41
|
-
const match = /^Bearer\s+(.+)$/i.exec(header.trim());
|
|
42
|
-
return match ? match[1].trim() : undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Map a grant denial reason to an HTTP status for the bearer gate. */
|
|
46
|
-
function denyStatus(reason: string | undefined): number {
|
|
47
|
-
if (reason === 'expired' || reason === 'revoked') return 403;
|
|
48
|
-
return 401; // unknown_token / missing
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Authenticate the grant bearer for a non-provisioning op (identity + not
|
|
53
|
-
* expired/revoked; no quota). Returns the token, or null after sending the
|
|
54
|
-
* failure response. Provision does its own quota-aware validation inside
|
|
55
|
-
* `DonationService.provision`.
|
|
56
|
-
*/
|
|
57
|
-
function authenticate(
|
|
58
|
-
request: FastifyRequest,
|
|
59
|
-
reply: FastifyReply,
|
|
60
|
-
grants: GrantValidator,
|
|
61
|
-
): string | null {
|
|
62
|
-
const token = parseBearer(request.headers.authorization);
|
|
63
|
-
if (!token) {
|
|
64
|
-
errorResponse(reply, 'unauthorized', 'Missing bearer grant token', 401);
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
const validation = grants.validate(token);
|
|
68
|
-
if (!validation.ok) {
|
|
69
|
-
const status = denyStatus(validation.reason);
|
|
70
|
-
// Keep the envelope code aligned with the status (and with the POST path,
|
|
71
|
-
// which surfaces an unknown token as `unauthorized`): 401 → unauthorized,
|
|
72
|
-
// 403 → forbidden. A 401 labelled `forbidden` is self-contradictory.
|
|
73
|
-
const code = status === 403 ? 'forbidden' : 'unauthorized';
|
|
74
|
-
errorResponse(reply, code, `Grant is ${validation.reason ?? 'invalid'}`, status);
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
return token;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Resolve a donation the presented grant owns. Returns the redacted view, or
|
|
82
|
-
* null after sending a 404 — an unknown id and one owned by a *different* grant
|
|
83
|
-
* are deliberately indistinguishable (a grantee never learns another grantee's
|
|
84
|
-
* donations exist).
|
|
85
|
-
*/
|
|
86
|
-
function requireOwned(
|
|
87
|
-
donations: DonationService,
|
|
88
|
-
id: string,
|
|
89
|
-
token: string,
|
|
90
|
-
reply: FastifyReply,
|
|
91
|
-
): DonationView | null {
|
|
92
|
-
const donation = donations.get(id);
|
|
93
|
-
if (!donation || donation.grantToken !== token) {
|
|
94
|
-
errorResponse(reply, 'not_found', `No such donation: ${id}`, 404);
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
return donation;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function registerGrantsRoutes(app: FastifyInstance, opts: GrantsRoutesOptions): void {
|
|
101
|
-
const { donations, grants } = opts;
|
|
102
|
-
|
|
103
|
-
// POST /grants — provision a donated node. The bearer's identity/expiry/
|
|
104
|
-
// revocation AND quota are validated inside provision(), which throws a
|
|
105
|
-
// DonationError (unauthorized / forbidden / quota_exceeded) the error handler
|
|
106
|
-
// maps. We only parse the bearer and validate the request body here.
|
|
107
|
-
app.post('/grants', async (request, reply) => {
|
|
108
|
-
const token = parseBearer(request.headers.authorization);
|
|
109
|
-
if (!token) {
|
|
110
|
-
return errorResponse(reply, 'unauthorized', 'Missing bearer grant token', 401);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// One validator owns the whole body — see `provision-request-validation.ts`
|
|
114
|
-
// for why no per-field check lives here. Nothing is provisioned on a
|
|
115
|
-
// rejection: this runs before the service is touched.
|
|
116
|
-
const validated = validateProvisionRequest(request.body ?? {}, token);
|
|
117
|
-
if ('error' in validated) {
|
|
118
|
-
return errorResponse(reply, 'invalid_request', validated.error, 400);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const donation = await donations.provision(validated.request);
|
|
122
|
-
return reply.status(201).send({ ok: true, data: { donation } });
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// GET /grants — list only the donations this grant authorized.
|
|
126
|
-
app.get('/grants', async (request, reply) => {
|
|
127
|
-
const token = authenticate(request, reply, grants);
|
|
128
|
-
if (!token) return reply;
|
|
129
|
-
return reply.send({ ok: true, data: { donations: donations.list(token) } });
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// GET /grants/:id — one donation (redacted), owned by this grant.
|
|
133
|
-
app.get<{ Params: { id: string } }>('/grants/:id', async (request, reply) => {
|
|
134
|
-
const token = authenticate(request, reply, grants);
|
|
135
|
-
if (!token) return reply;
|
|
136
|
-
const donation = requireOwned(donations, request.params.id, token, reply);
|
|
137
|
-
if (!donation) return reply;
|
|
138
|
-
return reply.send({ ok: true, data: { donation } });
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
// GET /grants/:id/peer — live { peerId, multiaddrs } for the requester's addDrone.
|
|
142
|
-
app.get<{ Params: { id: string } }>('/grants/:id/peer', async (request, reply) => {
|
|
143
|
-
const token = authenticate(request, reply, grants);
|
|
144
|
-
if (!token) return reply;
|
|
145
|
-
if (!requireOwned(donations, request.params.id, token, reply)) return reply;
|
|
146
|
-
const peer = await donations.getPeer(request.params.id);
|
|
147
|
-
return reply.send({ ok: true, data: peer });
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
// PUT /grants/:id/seed — apply the requester's phone-signed seed.
|
|
151
|
-
app.put<{ Params: { id: string } }>('/grants/:id/seed', async (request, reply) => {
|
|
152
|
-
const token = authenticate(request, reply, grants);
|
|
153
|
-
if (!token) return reply;
|
|
154
|
-
if (!requireOwned(donations, request.params.id, token, reply)) return reply;
|
|
155
|
-
|
|
156
|
-
const body = (request.body ?? {}) as { seed?: unknown };
|
|
157
|
-
if (typeof body.seed !== 'string' || body.seed.length === 0) {
|
|
158
|
-
return errorResponse(reply, 'invalid_request', 'seed is required', 400);
|
|
159
|
-
}
|
|
160
|
-
const result = await donations.applySeed(request.params.id, body.seed);
|
|
161
|
-
// NOTE: exhaustive over today's three outcomes, and nothing forces it to stay
|
|
162
|
-
// that way — an unhandled fourth would fall out of the handler as `undefined`
|
|
163
|
-
// and hang the request rather than fail to compile. If `DonationSeedResult`
|
|
164
|
-
// ever grows a variant, add a `default:` that 500s here.
|
|
165
|
-
switch (result.outcome) {
|
|
166
|
-
case 'rejected':
|
|
167
|
-
return errorResponse(reply, 'seed_failed', result.error ?? 'Node rejected the seed', 502);
|
|
168
|
-
// The loan ended while the seed was in flight. The ending won, so the
|
|
169
|
-
// caller is told the record it seeded is no longer seedable (409) — or is
|
|
170
|
-
// gone entirely (404) — rather than a 200 that implies a live node.
|
|
171
|
-
case 'abandoned':
|
|
172
|
-
return result.status
|
|
173
|
-
? errorResponse(
|
|
174
|
-
reply,
|
|
175
|
-
'invalid_state',
|
|
176
|
-
`Donation ${request.params.id} ended (${result.status}) while the seed was in flight`,
|
|
177
|
-
409,
|
|
178
|
-
)
|
|
179
|
-
: errorResponse(reply, 'not_found', `No such donation: ${request.params.id}`, 404);
|
|
180
|
-
case 'seeded':
|
|
181
|
-
return reply.send({ ok: true, data: { peersAdded: result.peersAdded } });
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
// DELETE /grants/:id — terminate the donated node.
|
|
186
|
-
app.delete<{ Params: { id: string } }>('/grants/:id', async (request, reply) => {
|
|
187
|
-
const token = authenticate(request, reply, grants);
|
|
188
|
-
if (!token) return reply;
|
|
189
|
-
if (!requireOwned(donations, request.params.id, token, reply)) return reply;
|
|
190
|
-
await donations.terminate(request.params.id);
|
|
191
|
-
return reply.send({ ok: true });
|
|
192
|
-
});
|
|
193
|
-
}
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The whole shape check for `POST /grants` — one call that either yields the
|
|
3
|
-
* typed {@link DonationProvisionRequest} the `DonationService` is handed, or the
|
|
4
|
-
* message the route answers 400 with.
|
|
5
|
-
*
|
|
6
|
-
* **Why a validator rather than a few more `if`s in the route.** Every field in
|
|
7
|
-
* this body is forwarded into a child process the requester cannot see:
|
|
8
|
-
* `partyId`, `bootstrapNodes` and `profile` are written into the child's
|
|
9
|
-
* `cadre.json` (`orchestrator/host-process-orchestrator.ts`) and `ownerKeys`
|
|
10
|
-
* become its cold-start seed-trust anchors. A field the route forgets to check is
|
|
11
|
-
* not a 400 — it is a donated node that dies at boot, or comes up subtly wrong,
|
|
12
|
-
* with a 201 already sent. Routing the body through one function that *constructs*
|
|
13
|
-
* the request object means a field can only reach the spawn path by having been
|
|
14
|
-
* through here; adding a field to `DonationProvisionRequest` without validating it
|
|
15
|
-
* stops compiling rather than silently shipping.
|
|
16
|
-
*
|
|
17
|
-
* **Division of labour with `DonationService.provision`.** The Ed25519 rule for
|
|
18
|
-
* `ownerKeys` stays in the service, where it is applied by cadre-core's own
|
|
19
|
-
* `requireEd25519PublicKeyB64` — the service is also reached by the respawn path,
|
|
20
|
-
* which replays a persisted record rather than an HTTP body, so the crypto rule
|
|
21
|
-
* has to live there to cover both. What this validator owns for `ownerKeys` is the
|
|
22
|
-
* *container* shape (present, non-empty, all strings), which the service's rule
|
|
23
|
-
* assumes: a number reaching it surfaces as an internal `.trim is not a function`
|
|
24
|
-
* rather than as a message naming what is wrong with the request.
|
|
25
|
-
*
|
|
26
|
-
* The multiaddr rule has no such home — it is duplicated in cadre-provider — so it
|
|
27
|
-
* lives in `bootstrap-node-validation.ts` next to the comment saying so.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import type { DonationProvisionRequest } from '../../donation/donation-service.js';
|
|
31
|
-
import { validateBootstrapNodes } from './bootstrap-node-validation.js';
|
|
32
|
-
|
|
33
|
-
/** `partyId` becomes `controlNetwork.partyId` in the child's config. */
|
|
34
|
-
function validatePartyId(value: unknown): { partyId: string } | { error: string } {
|
|
35
|
-
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
36
|
-
return { error: 'partyId is required' };
|
|
37
|
-
}
|
|
38
|
-
return { partyId: value.trim() };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Container shape only — see the module comment: the per-key Ed25519 rule is
|
|
43
|
-
* `DonationService.provision`'s, so the respawn path gets it too.
|
|
44
|
-
*/
|
|
45
|
-
function validateOwnerKeys(value: unknown): { ownerKeys: string[] } | { error: string } {
|
|
46
|
-
if (!Array.isArray(value) || value.length === 0) {
|
|
47
|
-
return { error: 'ownerKeys is required' };
|
|
48
|
-
}
|
|
49
|
-
if (!value.every((key): key is string => typeof key === 'string')) {
|
|
50
|
-
return { error: 'ownerKeys must be an array of strings' };
|
|
51
|
-
}
|
|
52
|
-
return { ownerKeys: value };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Absent means `storage` — see {@link DonationProvisionRequest.profile}. */
|
|
56
|
-
function validateProfile(value: unknown): { profile?: DonationProvisionRequest['profile'] } | { error: string } {
|
|
57
|
-
if (value === undefined) return {};
|
|
58
|
-
if (value !== 'storage' && value !== 'transaction') {
|
|
59
|
-
return { error: 'profile must be "storage" or "transaction"' };
|
|
60
|
-
}
|
|
61
|
-
return { profile: value };
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Validate a `POST /grants` body and build the request `DonationService.provision`
|
|
66
|
-
* is handed.
|
|
67
|
-
*
|
|
68
|
-
* `grantToken` comes from the `Authorization` bearer rather than the body — a
|
|
69
|
-
* requester does not get to name which grant pays for this — so it is a
|
|
70
|
-
* parameter, not a validated field. Whether that grant is live and within quota is
|
|
71
|
-
* `provision`'s call, not this function's.
|
|
72
|
-
*
|
|
73
|
-
* @returns the typed provision request, or the message to answer `invalid_request` with.
|
|
74
|
-
*/
|
|
75
|
-
export function validateProvisionRequest(
|
|
76
|
-
body: unknown,
|
|
77
|
-
grantToken: string,
|
|
78
|
-
): { request: DonationProvisionRequest } | { error: string } {
|
|
79
|
-
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
|
80
|
-
return { error: 'request body must be a JSON object' };
|
|
81
|
-
}
|
|
82
|
-
const fields = body as Record<string, unknown>;
|
|
83
|
-
|
|
84
|
-
const partyId = validatePartyId(fields.partyId);
|
|
85
|
-
if ('error' in partyId) return partyId;
|
|
86
|
-
|
|
87
|
-
const bootstrap = validateBootstrapNodes(fields.bootstrapNodes);
|
|
88
|
-
if ('error' in bootstrap) return bootstrap;
|
|
89
|
-
|
|
90
|
-
const ownerKeys = validateOwnerKeys(fields.ownerKeys);
|
|
91
|
-
if ('error' in ownerKeys) return ownerKeys;
|
|
92
|
-
|
|
93
|
-
const profile = validateProfile(fields.profile);
|
|
94
|
-
if ('error' in profile) return profile;
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
request: {
|
|
98
|
-
grantToken,
|
|
99
|
-
partyId: partyId.partyId,
|
|
100
|
-
bootstrapNodes: bootstrap.nodes,
|
|
101
|
-
ownerKeys: ownerKeys.ownerKeys,
|
|
102
|
-
...(profile.profile ? { profile: profile.profile } : {}),
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Strand HTTP routes — thin adapters around `StrandHandlers`
|
|
3
|
-
* (`createStrandHandlers(service)`).
|
|
4
|
-
*
|
|
5
|
-
* Mount path: `/api/strands` — a UI-only surface with no CLI contract to
|
|
6
|
-
* preserve. (The `/auth/*` and `/nat/*` mounts sit outside `/api` only because
|
|
7
|
-
* the existing CLI already targets those URLs.) Envelope form therefore follows
|
|
8
|
-
* the other `/api/*` routes: `{ ok: true, data }`.
|
|
9
|
-
*
|
|
10
|
-
* A removal that actually issued a delete publishes `strands-changed` so other
|
|
11
|
-
* open tabs refresh. A call that found no row publishes nothing — nothing changed.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type { FastifyInstance } from 'fastify';
|
|
15
|
-
|
|
16
|
-
import type { StrandHandlers } from '../../strands/types.js';
|
|
17
|
-
import type { EventBus } from '../events/bus.js';
|
|
18
|
-
|
|
19
|
-
export interface StrandRoutesOptions {
|
|
20
|
-
handlers: StrandHandlers;
|
|
21
|
-
events: EventBus;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The only accepted `confirm` values — the same strict pair the admin channel
|
|
26
|
-
* accepts. Not a general truthiness parser: `yes`, `on` and an empty value all
|
|
27
|
-
* count as NOT confirmed, because a guessy parser here would turn a typo into a
|
|
28
|
-
* destroyed membership key.
|
|
29
|
-
*
|
|
30
|
-
* NOTE: deliberately duplicated from cadre-cli's `admin-server.ts` rather than
|
|
31
|
-
* imported — cadre-host depends on cadre-cli for its bin path only, never its
|
|
32
|
-
* source. Keep the two in step by hand; do not DRY them into an import.
|
|
33
|
-
*/
|
|
34
|
-
const CONFIRM_VALUES = new Set(['1', 'true']);
|
|
35
|
-
|
|
36
|
-
export function registerStrandRoutes(app: FastifyInstance, opts: StrandRoutesOptions): void {
|
|
37
|
-
const { handlers, events } = opts;
|
|
38
|
-
|
|
39
|
-
app.get('/api/strands', async () => {
|
|
40
|
-
return { ok: true, data: await handlers.listStrands() };
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
app.delete<{ Params: { id: string }; Querystring: { confirm?: string } }>(
|
|
44
|
-
'/api/strands/:id',
|
|
45
|
-
async (request) => {
|
|
46
|
-
const confirm = typeof request.query.confirm === 'string' && CONFIRM_VALUES.has(request.query.confirm);
|
|
47
|
-
const result = await handlers.removeStrand(request.params.id, { confirm });
|
|
48
|
-
if (result.removed) {
|
|
49
|
-
events.publish({ type: 'strands-changed', kind: 'removed' });
|
|
50
|
-
}
|
|
51
|
-
return { ok: true, data: result };
|
|
52
|
-
},
|
|
53
|
-
);
|
|
54
|
-
}
|