@serve.zone/interfaces 32.74.0 → 32.76.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/changelog.md +26 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/data/controller.d.ts +70 -0
- package/dist_ts/data/controller.js +44 -0
- package/dist_ts/data/gateway.d.ts +64 -1
- package/dist_ts/data/gateway.js +87 -2
- package/dist_ts/data/gatewayconnector.d.ts +278 -0
- package/dist_ts/data/gatewayconnector.js +418 -0
- package/dist_ts/data/index.d.ts +2 -0
- package/dist_ts/data/index.js +3 -1
- package/dist_ts/data/location.d.ts +3 -1
- package/dist_ts/data/location.js +7 -5
- package/dist_ts/private/controllercontract.d.ts +8 -0
- package/dist_ts/private/controllercontract.js +50 -0
- package/dist_ts/requests/baremetal.d.ts +2 -1
- package/dist_ts/requests/cluster.d.ts +5 -3
- package/dist_ts/requests/gateway.d.ts +9 -5
- package/dist_ts/requests/gatewayconnector.d.ts +133 -0
- package/dist_ts/requests/gatewayconnector.js +2 -0
- package/dist_ts/requests/index.d.ts +2 -1
- package/dist_ts/requests/index.js +3 -2
- package/dist_ts/requests/node.d.ts +4 -1
- package/dist_ts/runtime.corestore.database.d.ts +23 -0
- package/dist_ts/runtime.corestore.database.js +41 -0
- package/dist_ts/runtime.d.ts +1 -0
- package/dist_ts/runtime.js +2 -1
- package/package.json +2 -2
- package/readme.md +129 -6
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/data/controller.ts +106 -0
- package/ts/data/gateway.ts +137 -2
- package/ts/data/gatewayconnector.ts +714 -0
- package/ts/data/index.ts +2 -0
- package/ts/data/location.ts +6 -4
- package/ts/private/controllercontract.ts +59 -0
- package/ts/requests/baremetal.ts +2 -1
- package/ts/requests/cluster.ts +5 -3
- package/ts/requests/gateway.ts +9 -5
- package/ts/requests/gatewayconnector.ts +168 -0
- package/ts/requests/index.ts +2 -0
- package/ts/requests/node.ts +4 -1
- package/ts/runtime.corestore.database.ts +64 -0
- package/ts/runtime.ts +1 -0
package/readme.md
CHANGED
|
@@ -3805,7 +3805,8 @@ create that names no location for a cluster without nodes, and `getLocations` ma
|
|
|
3805
3805
|
- The member kinds are the stored kinds `data.locationMemberKinds` (`edge`, `gateway`, `node`,
|
|
3806
3806
|
`oneboxHost`); `getLocations` answers which of them the Cloudly records
|
|
3807
3807
|
(`recordedMemberKinds`). A cluster is no member and no part of the digest: `getClusters` with a
|
|
3808
|
-
`locationScope` lists the clusters standing in a location.
|
|
3808
|
+
`locationScope` lists the clusters standing in a location. A cluster whose nodes span locations
|
|
3809
|
+
is in no scoped answer.
|
|
3809
3810
|
- `data.initialLocationId` (`location-initial`) is fixed; Cloudly generates every other location id
|
|
3810
3811
|
with a generator that can never produce it.
|
|
3811
3812
|
|
|
@@ -3820,15 +3821,19 @@ canonical JSON of the sorted `[kind, id]` pairs and the recorded kinds
|
|
|
3820
3821
|
The requests (`requests.location`) are administrator-only, admitted by no delegated-admin scope:
|
|
3821
3822
|
`createLocation`, `renameLocation`, `getLocations`, `setClusterLocation` (answers
|
|
3822
3823
|
`data.setClusterLocationOutcomes`: `moved`, `unchanged`, or `cluster-empty` for a cluster without
|
|
3823
|
-
nodes) and `confirmLocationMembership`. Every refusal is one of `data.locationRefusals
|
|
3824
|
+
nodes) and `confirmLocationMembership`. Every refusal is one of `data.locationRefusals` and
|
|
3825
|
+
travels by name as a `TypedResponseError`, except at a jump-code claim or node enrollment: there
|
|
3826
|
+
Cloudly logs the refusal by name and the claimant receives that path's single refusal sentence.
|
|
3824
3827
|
`createCluster` and `createNodeJumpCommand` take an optional `locationId`; for a cluster with nodes
|
|
3825
3828
|
a named location must be the cluster's (`location-mismatch`). A `createCluster` that names a
|
|
3826
|
-
location but creates no node (
|
|
3829
|
+
location but creates no node (a setup mode that provisions no node: today every mode but
|
|
3830
|
+
`hetzner`) is refused `location-needs-node`,
|
|
3827
3831
|
checked before `location-not-found`: name the location on `createNodeJumpCommand` instead.
|
|
3828
3832
|
`getClusters`, `getNodes` and `getBaremetalServers` take an optional `locationScope` and echo it as `appliedLocationScope`: a
|
|
3829
|
-
response without the echo comes from a Cloudly that ignored the scope and is not scoped.
|
|
3833
|
+
response without the echo comes from a Cloudly that ignored the scope and is not scoped. Each is
|
|
3834
|
+
refused `location-not-found` when no location has that id.
|
|
3830
3835
|
|
|
3831
|
-
Cloudly's
|
|
3836
|
+
Cloudly's `initial-location` data step writes one transaction per cluster (the cluster document and
|
|
3832
3837
|
all its node documents, setting `locationId` only where it is missing, so a rerun is safe) and one
|
|
3833
3838
|
transaction per node whose `clusterId` names no existing cluster. It never calls
|
|
3834
3839
|
`data.getClusterLocationId`, which throws for node copies that disagree.
|
|
@@ -4172,7 +4177,37 @@ A delivery state of `pushServiceAccepted` means only that the remote push servic
|
|
|
4172
4177
|
|
|
4173
4178
|
### Gateway Client Lifecycle and DNS
|
|
4174
4179
|
|
|
4175
|
-
`syncGatewayClientRoute` accepts an optional `dnsMode`. Omission means `skip` for
|
|
4180
|
+
`syncGatewayClientRoute` accepts an optional `dnsMode`. Omission keeps the route's stored mode and means `skip` for a new route. `observe` reports DNS without changing it. `reconcile` creates, updates and deletes only the exact-host A or AAAA record the route's gateway DNS ownership holds; it never adopts, replaces or deletes a record without that ownership (`data.TGatewayRouteDnsMode`), so a differing manual record is a non-retryable `record-conflict` and matching manual records are `manual-satisfied`. Its optional `dns` result contains a closed status, retryability, the desired A/AAAA target, `checkedAt`, and `authoritativeVerifiedAt` once the provider or authoritative server confirms the state. Consumers can carry that evidence while retrying public propagation instead of treating an immediate recursive lookup miss as permanent.
|
|
4181
|
+
|
|
4182
|
+
`sourceProfileRef` names the gateway source profile a route binds to, by id or name. Omission binds the route to the gateway's `STANDARD` profile on every sync, an update included, not to the profile stored before; a route meant for private networks only sends its profile explicitly, for example `'LOCAL NETWORK'`. A route read (`data.IGatewayClientRoute`) returns the bound profile's id in `sourceProfileRef` and its name in `sourceProfileName`, so a client compares the binding by the name it synced; `sourceProfileName` is optional, and a gateway built before it omits it.
|
|
4183
|
+
|
|
4184
|
+
`route.action.requestHeaders` sets request header fields on everything the gateway forwards to the route's targets, requests and WebSocket upgrades alike, replacing every copy of the same name the client sent; a gateway client hands its backend a trusted-proxy secret this way. Names are lowercase hyphen-separated words, values visible ASCII, at most 16 fields (`data.gatewayRouteRequestHeadersContract`, `data.validateGatewayRouteRequestHeaders`), and the fields a gateway owns on the backend leg (`host`, hop-by-hop and framing fields, `forwarded`, `via`, `x-real-ip`, `x-forwarded-*`, `proxy-*`, `sec-websocket-*`) are refused. The member is write-only: each sync states the complete set, and a sync without it removes the fields; `getGatewayClientRoutes` never returns the values and lists the names in `requestHeaderNames`. A gateway built before it refuses the whole route, because gateways read routes by their exact keys, so a client sends it only to a gateway whose `getGatewayCapabilities` answers `routes.requestHeaders: true`.
|
|
4185
|
+
|
|
4186
|
+
```typescript
|
|
4187
|
+
import { data, requests } from '@serve.zone/interfaces';
|
|
4188
|
+
|
|
4189
|
+
const sync: requests.gateway.IReq_SyncGatewayClientRoute['request'] = {
|
|
4190
|
+
apiToken: 'gateway-client-token',
|
|
4191
|
+
ownership: { appId: 'controller', hostname: 'app.example.com' },
|
|
4192
|
+
route: {
|
|
4193
|
+
name: 'controller',
|
|
4194
|
+
match: { ports: [443], domains: ['app.example.com'] },
|
|
4195
|
+
action: {
|
|
4196
|
+
type: 'forward',
|
|
4197
|
+
targets: [{ host: '192.0.2.10', port: 4097 }],
|
|
4198
|
+
tls: { mode: 'terminate', certificate: 'auto' },
|
|
4199
|
+
websocket: { enabled: true },
|
|
4200
|
+
requestHeaders: { 'x-trusted-proxy-secret': 'sealed-secret-value' },
|
|
4201
|
+
},
|
|
4202
|
+
ingress: { directHub: true, smartVpn: true },
|
|
4203
|
+
remoteIngress: { enabled: false },
|
|
4204
|
+
},
|
|
4205
|
+
sourceProfileRef: 'LOCAL NETWORK',
|
|
4206
|
+
dnsMode: 'reconcile',
|
|
4207
|
+
dnsProxied: false,
|
|
4208
|
+
};
|
|
4209
|
+
const refusals = data.validateGatewayRouteRequestHeaders(sync.route!.action.requestHeaders);
|
|
4210
|
+
```
|
|
4176
4211
|
|
|
4177
4212
|
Use `requests.gateway.IReq_ProvisionGatewayClientCredential` to replace an admin/bootstrap token with a first-class gateway-client credential. The admin-authenticated request idempotently upserts a `data.IGatewayClient`, durably creates a new bound credential, returns its raw value once, and then revokes older credentials bound to that client. It never revokes the bootstrap/admin credential. A successful response is discriminated with `success: true` and always includes the action, durable client, one-time credential, and revocation count.
|
|
4178
4213
|
|
|
@@ -4297,6 +4332,73 @@ const request: requests.gateway.IReq_SyncGatewayClientDnsRecord['request'] = {
|
|
|
4297
4332
|
};
|
|
4298
4333
|
```
|
|
4299
4334
|
|
|
4335
|
+
### Gateway Connector
|
|
4336
|
+
|
|
4337
|
+
A gateway (dcrouter) dials its controller (Cloudly) on the controller's typedsocket server and is
|
|
4338
|
+
never dialled once adopted. It authenticates with `registerCloudlyClientSession` as the machine
|
|
4339
|
+
identity it received at adoption, then says hello with `registerGatewaySession`; the gateway is the
|
|
4340
|
+
one that identity names and is never a request field. The controller records its gateways as
|
|
4341
|
+
`data.IGateway`: every gateway stands in a location, and its `connection` is either `legacy` (the
|
|
4342
|
+
dcrouter the controller still dials at a URL as a gateway client) or `adopted` (machine user and
|
|
4343
|
+
credential generation, no URL).
|
|
4344
|
+
|
|
4345
|
+
- Adoption: an administrator makes a one-time code (`createGatewayAdoptionCode`, for a new gateway
|
|
4346
|
+
in an existing location or for an existing gateway record) and runs the answered `command` on
|
|
4347
|
+
the gateway's host. The gateway redeems it with `adoptGateway` and its `data.IGatewayAdoptionFacts`;
|
|
4348
|
+
the controller answers the gateway id, its `data.IControllerPeerSet` and the credential, once.
|
|
4349
|
+
Adopting again raises the credential generation. Keep both bodies out of logs.
|
|
4350
|
+
- Peers: a gateway stores the peer set (`controllerId`, `epoch`, `activeOrigin`, `shadowOrigins`) and
|
|
4351
|
+
refuses any other controller and any older epoch. A takeover and a database restore raise the
|
|
4352
|
+
epoch. Later controller contracts extend `data.IControllerPeerSet` and never define a second one.
|
|
4353
|
+
- Desired state: the controller pushes the whole `data.IGatewayDesiredState` (`applyDesiredState`):
|
|
4354
|
+
`{ gatewayId, controllerId, epoch, revision, areas }`, one whole table per controller-owned area.
|
|
4355
|
+
Documents order by `(epoch, revision)` (`data.compareControllerRevisionOrder`): newer applies,
|
|
4356
|
+
equal replays the stored report, older is refused `stale`. A gateway validates every table first,
|
|
4357
|
+
skips an area whose digest it already serves, applies the rest in
|
|
4358
|
+
`data.gatewayConfigAreaApplyOrder` (`security`, `dns`, `routes`, `mail`, `edges`) and stops at the
|
|
4359
|
+
first failure; `data.IGatewayApplyReport.appliedRevision` advances only when the whole document
|
|
4360
|
+
applied. Refusals are `data.gatewayApplyRefusals`.
|
|
4361
|
+
- Area content is untyped here (`content: unknown`); each area's content type and validator ship in
|
|
4362
|
+
the release that moves that area. Its digest is `data.computeGatewayAreaTableDigest(area, content)`,
|
|
4363
|
+
`sha256:<hex>` of the strict canonical JSON of the domain `serve.zone/gateway-area-table`, the area
|
|
4364
|
+
and the content, which both sides compute.
|
|
4365
|
+
- Takeover: an area has one owner per gateway, `gateway` or `controller`, and moves to the controller
|
|
4366
|
+
once. The controller reads the local table (`exportGatewayArea`), writes an import revision and
|
|
4367
|
+
pushes the table with `takeover.localDigest`; a gateway whose local table changed since refuses
|
|
4368
|
+
`local-changed-since-export`.
|
|
4369
|
+
- Status: a gateway pushes its `data.IGatewayStatusDocument` (`pushGatewayStatus`) after its hello,
|
|
4370
|
+
after every apply and on change, at most once per
|
|
4371
|
+
`data.gatewayConnectorContract.minimumStatusPushIntervalMs` (5 s). `getGateways` lists the gateways
|
|
4372
|
+
with their connection state and last status, and takes the optional `locationScope`.
|
|
4373
|
+
`listeningPorts` (`data.IGatewayListeningPorts`) reports every public socket the gateway has open,
|
|
4374
|
+
not only its SmartProxy routes, as `{ tcp, udp }`: two lists of ranges the way routes state them
|
|
4375
|
+
(`data.IGatewayPortRange`, `{ from, to }`, inclusive), so a route on 10000–20000 is one entry. A
|
|
4376
|
+
route's `match.transport` `udp` counts under udp, `all` under both, `tcp` or none under tcp; QUIC
|
|
4377
|
+
and HTTP/3 count under udp. A gateway merges each transport's ports on its own into ranges sorted by
|
|
4378
|
+
`from` that neither overlap nor touch, at most
|
|
4379
|
+
`data.gatewayConnectorContract.maximumListeningPortRanges` (1024) per transport; one port may
|
|
4380
|
+
appear in both lists. A gateway lowercases its zone names before reporting them in `dnsZones`.
|
|
4381
|
+
- Validators name every refusal and never run an accessor: `data.validateControllerPeerSet`,
|
|
4382
|
+
`data.validateGatewayAdoptionFacts`, `data.validateRegisterGatewaySessionRequest`,
|
|
4383
|
+
`data.validateGatewayDesiredState` (envelope only), `data.validateGatewayApplyReport`,
|
|
4384
|
+
`data.validateGatewayStatusDocument` and `data.validateGatewayAreaExport`.
|
|
4385
|
+
|
|
4386
|
+
```typescript
|
|
4387
|
+
import { data, requests } from '@serve.zone/interfaces';
|
|
4388
|
+
|
|
4389
|
+
const content = { routes: [] };
|
|
4390
|
+
const desired: requests.gatewayconnector.IReq_Cloudly_Gateway_ApplyDesiredState['request'] = {
|
|
4391
|
+
desired: {
|
|
4392
|
+
gatewayId: 'gateway-fra-1',
|
|
4393
|
+
controllerId: 'controller-fra',
|
|
4394
|
+
epoch: 7,
|
|
4395
|
+
revision: 42,
|
|
4396
|
+
areas: [{ area: 'routes', digest: await data.computeGatewayAreaTableDigest('routes', content), content, takeover: null }],
|
|
4397
|
+
},
|
|
4398
|
+
};
|
|
4399
|
+
data.validateGatewayDesiredState(desired.desired); // []
|
|
4400
|
+
```
|
|
4401
|
+
|
|
4300
4402
|
### CoreMail contracts
|
|
4301
4403
|
|
|
4302
4404
|
`requests.coremail` is the shared contract boundary for authenticated workload
|
|
@@ -4705,6 +4807,27 @@ bytes and digests exactly.
|
|
|
4705
4807
|
payload, one-byte-larger framed snapshot-original, 128 MiB verified closure
|
|
4706
4808
|
plaintext, and 256 MiB closure boundaries used by Corestore.
|
|
4707
4809
|
|
|
4810
|
+
### Corestore Database Helpers
|
|
4811
|
+
|
|
4812
|
+
The Corestore database helpers its readers share live in the Node-only `/runtime` export, so a
|
|
4813
|
+
reader such as Onebox needs no `@serve.zone/corestore` dependency:
|
|
4814
|
+
|
|
4815
|
+
- `ICorestoreDatabaseAllocationReference`, read with `normalizeCorestoreDatabaseAllocationReference()`
|
|
4816
|
+
(see Corestore Runtime Credentials), and `corestoreDatabaseAllocationReferencesEqual(left, right)`,
|
|
4817
|
+
true when every member of two normalized references is equal.
|
|
4818
|
+
- `getCorestoreDatabaseIdentity(serviceId, serviceName?)` answers the `ICorestoreDatabaseIdentity`
|
|
4819
|
+
(`databaseName`, `username`) Corestore derives for a service: `sz_<base>_<hash>` and
|
|
4820
|
+
`u_<base>_<hash>`, with `<hash>` the first 12 hex digits of the SHA-1 of the service id and
|
|
4821
|
+
`<base>` the sanitized service name (the id when the name is absent or empty), at most 36
|
|
4822
|
+
characters. Every created database carries these names, so the derivation never changes.
|
|
4823
|
+
|
|
4824
|
+
```typescript
|
|
4825
|
+
import * as runtime from '@serve.zone/interfaces/runtime';
|
|
4826
|
+
|
|
4827
|
+
runtime.getCorestoreDatabaseIdentity('onebox-service-1');
|
|
4828
|
+
// { databaseName: 'sz_onebox_service_1_aa2bf033ccdf', username: 'u_onebox_service_1_aa2bf033ccdf' }
|
|
4829
|
+
```
|
|
4830
|
+
|
|
4708
4831
|
### Corestore Control Token Administration
|
|
4709
4832
|
|
|
4710
4833
|
The elected relay of every cluster calls its nodes' Corestore control API with one control token
|
package/ts/00_commitinfo_data.ts
CHANGED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {
|
|
2
|
+
validateControllerCounter,
|
|
3
|
+
validateControllerIdentifier,
|
|
4
|
+
validateControllerOrigin,
|
|
5
|
+
} from '../private/controllercontract.js';
|
|
6
|
+
import { readDataElements, readExactDataRecord } from '../private/dataproperties.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The controller as the components that dial it know it: which controller they belong to, which
|
|
10
|
+
* epoch it is in and where to reach it.
|
|
11
|
+
*
|
|
12
|
+
* A dialling component (a gateway today) stores this when it is adopted and replaces it with every
|
|
13
|
+
* answer the controller gives it. It refuses any other `controllerId` and any older `epoch`: a
|
|
14
|
+
* takeover and a database restore both raise the epoch, so the controller that answers with the
|
|
15
|
+
* highest one is the one whose revisions count. Later controller contracts extend this shape and
|
|
16
|
+
* never define a second one.
|
|
17
|
+
*/
|
|
18
|
+
export interface IControllerPeerSet {
|
|
19
|
+
/** Fixed for the controller's life; stored at adoption. */
|
|
20
|
+
controllerId: string;
|
|
21
|
+
/** A positive safe integer; never lowered while the controller lives. */
|
|
22
|
+
epoch: number;
|
|
23
|
+
/** The canonical `https` origin of the active controller, dialled first. */
|
|
24
|
+
activeOrigin: string;
|
|
25
|
+
/** The canonical `https` origins of the shadow controllers, dialled in this order after the active one. */
|
|
26
|
+
shadowOrigins: string[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const controllerPeerSetContract = Object.freeze({
|
|
30
|
+
maximumOriginLength: 2048,
|
|
31
|
+
maximumShadowOrigins: 16,
|
|
32
|
+
} as const);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Negative, zero or positive as the left controller position is older than, the same as or newer
|
|
36
|
+
* than the right one: the epoch decides first and the revision only within one epoch, so a higher
|
|
37
|
+
* epoch with a lower revision (after a takeover or a restore) is still newer.
|
|
38
|
+
*/
|
|
39
|
+
export const compareControllerRevisionOrder = (
|
|
40
|
+
leftArg: Readonly<{ epoch: number; revision: number }>,
|
|
41
|
+
rightArg: Readonly<{ epoch: number; revision: number }>,
|
|
42
|
+
): number => Math.sign(leftArg.epoch - rightArg.epoch) || Math.sign(leftArg.revision - rightArg.revision);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* One record of the controller-wide revision sequence, written in the same transaction as the
|
|
46
|
+
* change it records. A secret appears in `changes` by its ids and versions only, never by value.
|
|
47
|
+
*/
|
|
48
|
+
export interface IControllerRevision {
|
|
49
|
+
/** A positive safe integer from the one controller-wide sequence. */
|
|
50
|
+
revision: number;
|
|
51
|
+
/** The controller epoch the revision was written in. */
|
|
52
|
+
epoch: number;
|
|
53
|
+
/** Unix milliseconds on the controller's clock. */
|
|
54
|
+
at: number;
|
|
55
|
+
actor: {
|
|
56
|
+
kind: 'person' | 'machine' | 'system';
|
|
57
|
+
/** The acting identity's subject; `null` for the controller itself (`system`). */
|
|
58
|
+
sub: string | null;
|
|
59
|
+
displayName: string;
|
|
60
|
+
};
|
|
61
|
+
kind: 'change' | 'adoption' | 'import' | 'revert';
|
|
62
|
+
changes: {
|
|
63
|
+
objectKind: string;
|
|
64
|
+
objectId: string;
|
|
65
|
+
/** The object before the change; `null` when the change created it. */
|
|
66
|
+
before: unknown;
|
|
67
|
+
/** The object after the change; `null` when the change deleted it. */
|
|
68
|
+
after: unknown;
|
|
69
|
+
}[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Validates one controller peer set and names every reason it is refused.
|
|
74
|
+
*
|
|
75
|
+
* Shape only: whether the controller id is the one a gateway stored, and whether the epoch is not
|
|
76
|
+
* older than its stored one, is the dialling component's decision against its own state.
|
|
77
|
+
*/
|
|
78
|
+
export const validateControllerPeerSet = (
|
|
79
|
+
valueArg: unknown,
|
|
80
|
+
pathArg = 'controller peer set',
|
|
81
|
+
): string[] => {
|
|
82
|
+
const record = readExactDataRecord(valueArg, ['controllerId', 'epoch', 'activeOrigin', 'shadowOrigins']);
|
|
83
|
+
if (record === null) {
|
|
84
|
+
return [`${pathArg} must use its exact schema`];
|
|
85
|
+
}
|
|
86
|
+
const { maximumOriginLength, maximumShadowOrigins } = controllerPeerSetContract;
|
|
87
|
+
const errors = [
|
|
88
|
+
...validateControllerIdentifier(record.controllerId, `${pathArg} controllerId`),
|
|
89
|
+
...validateControllerCounter(record.epoch, `${pathArg} epoch`),
|
|
90
|
+
...validateControllerOrigin(record.activeOrigin, `${pathArg} activeOrigin`, maximumOriginLength),
|
|
91
|
+
];
|
|
92
|
+
const shadowOrigins = readDataElements(record.shadowOrigins, maximumShadowOrigins);
|
|
93
|
+
if (shadowOrigins === null) {
|
|
94
|
+
errors.push(`${pathArg} shadowOrigins must be a dense list of at most ${maximumShadowOrigins} origins`);
|
|
95
|
+
return errors;
|
|
96
|
+
}
|
|
97
|
+
shadowOrigins.forEach((originArg, indexArg) => {
|
|
98
|
+
errors.push(...validateControllerOrigin(originArg, `${pathArg} shadowOrigins[${indexArg}]`,
|
|
99
|
+
maximumOriginLength));
|
|
100
|
+
});
|
|
101
|
+
const origins = [record.activeOrigin, ...shadowOrigins];
|
|
102
|
+
if (new Set(origins).size !== origins.length) {
|
|
103
|
+
errors.push(`${pathArg} must name each origin once`);
|
|
104
|
+
}
|
|
105
|
+
return errors;
|
|
106
|
+
};
|
package/ts/data/gateway.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TDnsRecordType } from './dns.js';
|
|
2
|
-
import { hasExactKeys, readDataElements, readDataProperties } from '../private/dataproperties.js';
|
|
2
|
+
import { hasExactKeys, isPlainRecord, readDataElements, readDataProperties } from '../private/dataproperties.js';
|
|
3
3
|
import { isPlausibleUnicastIpv4 } from '../private/ipv4.js';
|
|
4
4
|
import {
|
|
5
5
|
ipPrefixContains,
|
|
@@ -31,7 +31,9 @@ export type TManagedRouteKind = 'letsencrypt-http01-forward';
|
|
|
31
31
|
* hostname while another enabled route on the gateway serves it, the owned
|
|
32
32
|
* record is released to manual ownership instead of deleted.
|
|
33
33
|
* - 'observe' reports DNS state without mutating it.
|
|
34
|
-
* - 'skip' bypasses DNS inspection and mutation
|
|
34
|
+
* - 'skip' bypasses DNS inspection and mutation.
|
|
35
|
+
*
|
|
36
|
+
* An omitted mode keeps the route's stored mode, and is 'skip' for a new route.
|
|
35
37
|
*/
|
|
36
38
|
export type TGatewayRouteDnsMode = 'reconcile' | 'observe' | 'skip';
|
|
37
39
|
|
|
@@ -134,6 +136,11 @@ export interface IGatewayCapabilities {
|
|
|
134
136
|
read: boolean;
|
|
135
137
|
write: boolean;
|
|
136
138
|
idempotentSync: boolean;
|
|
139
|
+
/**
|
|
140
|
+
* `syncGatewayClientRoute` accepts `IGatewayRouteAction.requestHeaders`. Absent means
|
|
141
|
+
* unsupported: such a gateway reads a route by its exact keys and refuses the whole route.
|
|
142
|
+
*/
|
|
143
|
+
requestHeaders?: boolean;
|
|
137
144
|
};
|
|
138
145
|
domains: {
|
|
139
146
|
read: boolean;
|
|
@@ -444,6 +451,21 @@ export interface IGatewayRouteAction {
|
|
|
444
451
|
websocket?: {
|
|
445
452
|
enabled: boolean;
|
|
446
453
|
};
|
|
454
|
+
/**
|
|
455
|
+
* Request header fields the gateway sets on every request and WebSocket upgrade it forwards to
|
|
456
|
+
* the targets, by name. Each replaces every copy of its name the client sent, whatever its
|
|
457
|
+
* casing, so a client can neither forge nor duplicate one: this is how a gateway client hands
|
|
458
|
+
* its backend a trusted-proxy secret. Names and values follow `gatewayRouteRequestHeadersContract`
|
|
459
|
+
* and `validateGatewayRouteRequestHeaders`.
|
|
460
|
+
*
|
|
461
|
+
* Write-only. A sync states the complete set: a route synced without it sets no header, also
|
|
462
|
+
* when an earlier sync set some. No read returns the values; `IGatewayClientRoute` names the
|
|
463
|
+
* fields in `requestHeaderNames`, and its `route.action` never carries this member.
|
|
464
|
+
*
|
|
465
|
+
* Gate it on the gateway: one built before this member refuses a route that carries it, so send
|
|
466
|
+
* it only to a gateway whose `getGatewayCapabilities` answers `routes.requestHeaders: true`.
|
|
467
|
+
*/
|
|
468
|
+
requestHeaders?: Record<string, string>;
|
|
447
469
|
}
|
|
448
470
|
|
|
449
471
|
/**
|
|
@@ -490,8 +512,22 @@ export interface IGatewayClientRoute {
|
|
|
490
512
|
name: string;
|
|
491
513
|
enabled: boolean;
|
|
492
514
|
ownership: IGatewayClientOwnership;
|
|
515
|
+
/** The route as synced, without `action.requestHeaders`, which is write-only. */
|
|
493
516
|
route: IGatewayRouteConfig;
|
|
517
|
+
/**
|
|
518
|
+
* Names of the request header fields the route sets (`action.requestHeaders`), sorted; their
|
|
519
|
+
* values are never returned. Absent when the route sets none.
|
|
520
|
+
*/
|
|
521
|
+
requestHeaderNames?: string[];
|
|
522
|
+
/** Id of the gateway source profile the route is bound to. */
|
|
494
523
|
sourceProfileRef?: string;
|
|
524
|
+
/**
|
|
525
|
+
* Name of that source profile, as the gateway recorded it for the route, so a client can read the
|
|
526
|
+
* binding back by the name it synced (`syncGatewayClientRoute` resolves `sourceProfileRef` by id
|
|
527
|
+
* or name). Optional: a gateway built before this member omits it, so absence says nothing about
|
|
528
|
+
* the binding.
|
|
529
|
+
*/
|
|
530
|
+
sourceProfileName?: string;
|
|
495
531
|
/** Persisted DNS handling mode for this managed route. */
|
|
496
532
|
dnsMode?: TGatewayRouteDnsMode;
|
|
497
533
|
/** Explicit provider proxy intent for the managed address record. */
|
|
@@ -788,3 +824,102 @@ export const isGatewayClientDnsRecordAllowed = (
|
|
|
788
824
|
return prefix !== null && ipPrefixContains(prefix, address);
|
|
789
825
|
});
|
|
790
826
|
};
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Bounds and reserved names of `IGatewayRouteAction.requestHeaders`.
|
|
830
|
+
*
|
|
831
|
+
* A name is lowercase letters and digits in hyphen-separated words, so a field has one spelling and
|
|
832
|
+
* no two entries of one map name the same field. A value is visible ASCII and spaces, without a
|
|
833
|
+
* leading or trailing space. Reserved are the fields a gateway owns on the backend leg: hop-by-hop
|
|
834
|
+
* and message framing fields, the request target (`host`), the forwarding chain (`forwarded`,
|
|
835
|
+
* `via`, `x-real-ip`, `x-forwarded-*`), proxy credentials (`proxy-*`) and the WebSocket handshake
|
|
836
|
+
* (`sec-websocket-*`).
|
|
837
|
+
*/
|
|
838
|
+
export const gatewayRouteRequestHeadersContract = Object.freeze({
|
|
839
|
+
maxHeaders: 16,
|
|
840
|
+
maxNameLength: 64,
|
|
841
|
+
maxValueLength: 1024,
|
|
842
|
+
/** Bound of the summed lengths of every name and value of one map. */
|
|
843
|
+
maxTotalLength: 4096,
|
|
844
|
+
reservedNames: Object.freeze([
|
|
845
|
+
'connection',
|
|
846
|
+
'content-length',
|
|
847
|
+
'expect',
|
|
848
|
+
'forwarded',
|
|
849
|
+
'host',
|
|
850
|
+
'http2-settings',
|
|
851
|
+
'keep-alive',
|
|
852
|
+
'te',
|
|
853
|
+
'trailer',
|
|
854
|
+
'transfer-encoding',
|
|
855
|
+
'upgrade',
|
|
856
|
+
'via',
|
|
857
|
+
'x-real-ip',
|
|
858
|
+
]) as readonly string[],
|
|
859
|
+
reservedNamePrefixes: Object.freeze(['proxy-', 'sec-websocket-', 'x-forwarded-']) as readonly string[],
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
const gatewayRouteRequestHeaderName = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
863
|
+
const gatewayRouteRequestHeaderValue = /^[\x21-\x7e](?:[\x20-\x7e]*[\x21-\x7e])?$/;
|
|
864
|
+
|
|
865
|
+
/** True when the name follows the request header name syntax, reserved or not. */
|
|
866
|
+
const isGatewayRouteRequestHeaderNameSyntax = (valueArg: string): boolean => (
|
|
867
|
+
valueArg.length <= gatewayRouteRequestHeadersContract.maxNameLength
|
|
868
|
+
&& gatewayRouteRequestHeaderName.test(valueArg)
|
|
869
|
+
);
|
|
870
|
+
|
|
871
|
+
/** True when the name is one a gateway owns on the backend leg. */
|
|
872
|
+
const isGatewayRouteRequestHeaderNameReserved = (valueArg: string): boolean => (
|
|
873
|
+
gatewayRouteRequestHeadersContract.reservedNames.includes(valueArg)
|
|
874
|
+
|| gatewayRouteRequestHeadersContract.reservedNamePrefixes.some((prefixArg) => valueArg.startsWith(prefixArg))
|
|
875
|
+
);
|
|
876
|
+
|
|
877
|
+
/** True for a request header name a route may set: canonical lowercase and not reserved. */
|
|
878
|
+
export const isGatewayRouteRequestHeaderName = (valueArg: unknown): valueArg is string => (
|
|
879
|
+
typeof valueArg === 'string'
|
|
880
|
+
&& isGatewayRouteRequestHeaderNameSyntax(valueArg)
|
|
881
|
+
&& !isGatewayRouteRequestHeaderNameReserved(valueArg)
|
|
882
|
+
);
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Validates a route's `requestHeaders` and names every reason it is refused.
|
|
886
|
+
*
|
|
887
|
+
* The reasons name the fields, never a value: a value is commonly a secret.
|
|
888
|
+
*/
|
|
889
|
+
export const validateGatewayRouteRequestHeaders = (
|
|
890
|
+
valueArg: unknown,
|
|
891
|
+
pathArg = 'requestHeaders',
|
|
892
|
+
): string[] => {
|
|
893
|
+
const { maxHeaders, maxNameLength, maxValueLength, maxTotalLength } = gatewayRouteRequestHeadersContract;
|
|
894
|
+
// The bound is judged before any property is copied, as `readDataElements` does for lists.
|
|
895
|
+
const headers = isPlainRecord(valueArg) && Reflect.ownKeys(valueArg).length <= maxHeaders
|
|
896
|
+
? readDataProperties(valueArg)
|
|
897
|
+
: null;
|
|
898
|
+
if (headers === null || Object.keys(headers).length === 0) {
|
|
899
|
+
return [`${pathArg} must map 1 to ${maxHeaders} header names to string values; omit it to set none`];
|
|
900
|
+
}
|
|
901
|
+
const errors: string[] = [];
|
|
902
|
+
let totalLength = 0;
|
|
903
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
904
|
+
// Quoted with escapes and cut to the longest valid name, so a refused name prints as one line.
|
|
905
|
+
const label = name.length <= maxNameLength
|
|
906
|
+
? JSON.stringify(name)
|
|
907
|
+
: `${JSON.stringify(name.slice(0, maxNameLength))}…`;
|
|
908
|
+
if (!isGatewayRouteRequestHeaderNameSyntax(name)) {
|
|
909
|
+
errors.push(`${pathArg} name ${label} must be 1 to ${maxNameLength} characters of lowercase `
|
|
910
|
+
+ 'letters and digits in hyphen-separated words');
|
|
911
|
+
} else if (isGatewayRouteRequestHeaderNameReserved(name)) {
|
|
912
|
+
errors.push(`${pathArg} must not set ${label}: the gateway owns it on the backend leg`);
|
|
913
|
+
}
|
|
914
|
+
if (typeof value !== 'string' || value.length > maxValueLength
|
|
915
|
+
|| !gatewayRouteRequestHeaderValue.test(value)) {
|
|
916
|
+
errors.push(`${pathArg} value of ${label} must be 1 to ${maxValueLength} characters of visible `
|
|
917
|
+
+ 'ASCII and spaces, without a leading or trailing space');
|
|
918
|
+
}
|
|
919
|
+
totalLength += name.length + (typeof value === 'string' ? value.length : 0);
|
|
920
|
+
}
|
|
921
|
+
if (totalLength > maxTotalLength) {
|
|
922
|
+
errors.push(`${pathArg} names and values must total at most ${maxTotalLength} characters`);
|
|
923
|
+
}
|
|
924
|
+
return errors;
|
|
925
|
+
};
|