agl 43.1.0 → 43.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/changelog.md +12 -0
- package/dist_serve/bundle.js +5329 -5116
- package/dist_serve/bundle.js.map +2 -2
- package/dist_serve/bundle.js.third-party-notices.json +30 -30
- package/dist_serve/bundle.js.third-party-notices.txt +30 -30
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/aglapi/aglapisurface.document.js +325 -1
- package/dist_ts/aglapi/interfaces.d.ts +3 -0
- package/dist_ts/aglapi/interfaces.js +2 -1
- package/dist_ts/classes.authmodels.js +17 -4
- package/dist_ts/classes.authstore.js +16 -2
- package/dist_ts/classes.cli.d.ts +4 -2
- package/dist_ts/classes.cli.js +6 -3
- package/dist_ts/classes.controller.d.ts +10 -0
- package/dist_ts/classes.controller.js +119 -4
- package/dist_ts/classes.controllermanagement.d.ts +14 -4
- package/dist_ts/classes.controllermanagement.js +20 -2
- package/dist_ts/classes.gatewayclient.d.ts +115 -0
- package/dist_ts/classes.gatewayclient.js +336 -0
- package/dist_ts/classes.gatewayclientstore.d.ts +97 -0
- package/dist_ts/classes.gatewayclientstore.js +261 -0
- package/dist_ts/classes.inferencerouter.d.ts +9 -0
- package/dist_ts/classes.inferencerouter.js +11 -1
- package/dist_ts/classes.inferenceservice.d.ts +9 -0
- package/dist_ts/classes.inferenceservice.js +27 -1
- package/dist_ts/classes.mcpclient.d.ts +4 -1
- package/dist_ts/classes.mcpclient.js +19 -1
- package/dist_ts/classes.mcpcontrollerhost.d.ts +3 -1
- package/dist_ts/classes.mcpcontrollerhost.js +9 -1
- package/dist_ts/classes.mcptools.d.ts +3 -3
- package/dist_ts/classes.mcptools.js +26 -1
- package/dist_ts/classes.mcpvalidation.js +7 -1
- package/dist_ts/classes.proxycontrol.d.ts +11 -0
- package/dist_ts/classes.proxycontrol.js +7 -1
- package/dist_ts/classes.proxycontrolclient.d.ts +17 -0
- package/dist_ts/classes.proxycontrolclient.js +35 -1
- package/dist_ts/classes.proxydaemon.js +8 -1
- package/dist_ts/classes.proxymonitor.d.ts +7 -0
- package/dist_ts/classes.proxymonitor.js +13 -1
- package/dist_ts/classes.trustedproxysecrets.d.ts +2 -0
- package/dist_ts/classes.trustedproxysecrets.js +5 -1
- package/dist_ts/functions.controllermanagementsurface.d.ts +4 -0
- package/dist_ts/functions.controllermanagementsurface.js +15 -1
- package/dist_ts/functions.gatewaycli.d.ts +24 -0
- package/dist_ts/functions.gatewaycli.js +395 -0
- package/dist_ts/functions.gatewayroute.d.ts +135 -0
- package/dist_ts/functions.gatewayroute.js +366 -0
- package/dist_ts/functions.inferencecli.js +30 -1
- package/dist_ts/functions.inferencecliarguments.d.ts +8 -2
- package/dist_ts/functions.inferencecliarguments.js +31 -3
- package/dist_ts/functions.proxycapacity.d.ts +50 -0
- package/dist_ts/functions.proxycapacity.js +142 -0
- package/dist_ts/functions.proxycapacitycli.d.ts +30 -0
- package/dist_ts/functions.proxycapacitycli.js +65 -0
- package/dist_ts/functions.remotecli.d.ts +30 -3
- package/dist_ts/functions.remotecli.js +44 -18
- package/dist_ts/interfaces.auth.d.ts +25 -4
- package/dist_ts/interfaces.auth.js +7 -1
- package/dist_ts/plugins.d.ts +2 -0
- package/dist_ts/plugins.js +4 -1
- package/dist_ts_authswitch/authority-placement.js +3 -17
- package/dist_ts_authswitch/authority-usagelimit.d.ts +29 -0
- package/dist_ts_authswitch/authority-usagelimit.js +66 -2
- package/dist_ts_authswitch/authority-usagewindows.d.ts +6 -0
- package/dist_ts_authswitch/authority-usagewindows.js +15 -1
- package/dist_ts_interfaces/gateway.d.ts +151 -0
- package/dist_ts_interfaces/gateway.js +66 -0
- package/dist_ts_interfaces/index.d.ts +2 -0
- package/dist_ts_interfaces/index.js +3 -1
- package/dist_ts_interfaces/management.d.ts +4 -4
- package/dist_ts_interfaces/management.js +3 -2
- package/dist_ts_interfaces/mcprequests.d.ts +14 -2
- package/dist_ts_interfaces/mcprequests.js +2 -1
- package/dist_ts_interfaces/proxycapacity.d.ts +99 -0
- package/dist_ts_interfaces/proxycapacity.js +74 -0
- package/package.json +4 -3
- package/readme.md +79 -5
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/aglapi/aglapisurface.document.ts +324 -0
- package/ts/aglapi/interfaces.ts +1 -0
- package/ts/classes.authmodels.ts +22 -3
- package/ts/classes.authstore.ts +15 -1
- package/ts/classes.cli.ts +7 -3
- package/ts/classes.controller.ts +134 -3
- package/ts/classes.controllermanagement.ts +40 -7
- package/ts/classes.gatewayclient.ts +401 -0
- package/ts/classes.gatewayclientstore.ts +272 -0
- package/ts/classes.inferencerouter.ts +12 -0
- package/ts/classes.inferenceservice.ts +27 -0
- package/ts/classes.mcpclient.ts +18 -0
- package/ts/classes.mcpcontrollerhost.ts +13 -0
- package/ts/classes.mcptools.ts +30 -2
- package/ts/classes.mcpvalidation.ts +5 -0
- package/ts/classes.proxycontrol.ts +16 -0
- package/ts/classes.proxycontrolclient.ts +30 -0
- package/ts/classes.proxydaemon.ts +8 -0
- package/ts/classes.proxymonitor.ts +12 -0
- package/ts/classes.trustedproxysecrets.ts +5 -0
- package/ts/functions.controllermanagementsurface.ts +15 -0
- package/ts/functions.gatewaycli.ts +436 -0
- package/ts/functions.gatewayroute.ts +429 -0
- package/ts/functions.inferencecli.ts +24 -0
- package/ts/functions.inferencecliarguments.ts +30 -4
- package/ts/functions.proxycapacity.ts +191 -0
- package/ts/functions.proxycapacitycli.ts +104 -0
- package/ts/functions.remotecli.ts +56 -20
- package/ts/interfaces.auth.ts +28 -4
- package/ts/plugins.ts +5 -0
- package/ts_authswitch/authority-placement.ts +2 -15
- package/ts_authswitch/authority-usagelimit.ts +84 -1
- package/ts_authswitch/authority-usagewindows.ts +13 -0
- package/ts_interfaces/gateway.ts +242 -0
- package/ts_interfaces/index.ts +2 -0
- package/ts_interfaces/management.ts +6 -4
- package/ts_interfaces/mcprequests.ts +18 -0
- package/ts_interfaces/proxycapacity.ts +162 -0
- package/ts_web/00_commitinfo_data.ts +1 -1
- package/ts_web/classes.browserpresentation.ts +13 -5
- package/ts_web/elements.accounts.ts +40 -113
- package/ts_web/elements.configurationpane.ts +24 -37
- package/ts_web/elements.devices.ts +4 -17
- package/ts_web/elements.flexlimitstrip.ts +21 -70
- package/ts_web/elements.harnesscontrollerapp.ts +91 -185
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import { defaultControllerPort } from './classes.config.js';
|
|
3
|
+
import { tryQueryControllerStatus } from './classes.cli.js';
|
|
4
|
+
import { requestControllerGatewaySync } from './classes.controllermanagement.js';
|
|
5
|
+
import {
|
|
6
|
+
ControllerGatewayClientStore,
|
|
7
|
+
controllerGatewayEnrolment,
|
|
8
|
+
type IControllerGatewayClientDocument,
|
|
9
|
+
type IControllerGatewayCredential,
|
|
10
|
+
type TControllerGatewayCredentialSpec,
|
|
11
|
+
} from './classes.gatewayclientstore.js';
|
|
12
|
+
import { TpmSecretCodec } from './classes.tpmsecretcodec.js';
|
|
13
|
+
import { configurationLocalCliActor } from './functions.configurationactor.js';
|
|
14
|
+
import {
|
|
15
|
+
deleteGatewayRoute,
|
|
16
|
+
deleteGatewayRoutes,
|
|
17
|
+
gatewayAppId,
|
|
18
|
+
gatewayClientIdFor,
|
|
19
|
+
gatewayClientProvisioningSpec,
|
|
20
|
+
GatewayClientTransport,
|
|
21
|
+
gatewayClientType,
|
|
22
|
+
gatewayProblemOf,
|
|
23
|
+
gatewayRouteKey,
|
|
24
|
+
gatewayRouteOwnership,
|
|
25
|
+
isLoopbackHost,
|
|
26
|
+
normalizeGatewayTargetHost,
|
|
27
|
+
normalizeGatewayUrl,
|
|
28
|
+
} from './functions.gatewayroute.js';
|
|
29
|
+
import {
|
|
30
|
+
audited,
|
|
31
|
+
readStandardInputSecret,
|
|
32
|
+
withStoppedController,
|
|
33
|
+
type IRemoteCliOptions,
|
|
34
|
+
} from './functions.remotecli.js';
|
|
35
|
+
import type { IControllerAuditGateway } from './interfaces.auth.js';
|
|
36
|
+
import type { IControllerGatewayStatus } from '../ts_interfaces/index.js';
|
|
37
|
+
|
|
38
|
+
type IReq_ProvisionGatewayClientCredential = plugins.servezoneInterfaces.requests.gateway.IReq_ProvisionGatewayClientCredential;
|
|
39
|
+
type IReq_FinalizeGatewayClientCredential = plugins.servezoneInterfaces.requests.gateway.IReq_FinalizeGatewayClientCredential;
|
|
40
|
+
|
|
41
|
+
export const gatewayCliHelp = `Usage: agl remote gateway enrol [--gateway-url URL] [--target-host HOST] [--port PORT] < ADMIN_TOKEN_FILE
|
|
42
|
+
agl remote gateway status [--json] [--port PORT]
|
|
43
|
+
agl remote gateway sync [--rotate] [--port PORT]
|
|
44
|
+
agl remote gateway remove [--abandon] [--port PORT]
|
|
45
|
+
The controller on PORT (default 4097) keeps its own route on a dcrouter gateway, as a gateway client of type custom:
|
|
46
|
+
https://HOST of its public origin, TLS ended at the gateway, forwarded with the trusted-proxy secret to HOST:PORT,
|
|
47
|
+
reachable from private networks only (source profile LOCAL NETWORK, directly at the hub and over its VPN, never through
|
|
48
|
+
RemoteIngress), with the gateway's own DNS record. The running controller syncs it in the background: at start, again
|
|
49
|
+
after six hours, and with backoff from 30 s to 15 min after a failure a later sync can fix. A gateway without
|
|
50
|
+
routes.requestHeaders (before dcrouter 34.5.0) gets no route and is waited for: asked again with that backoff, then
|
|
51
|
+
every six hours and at every sync. A failure only the operator can fix (a revoked or stale credential, a refused
|
|
52
|
+
route, a DNS conflict) stops the syncs until the next start or sync; status names it.
|
|
53
|
+
enrol runs against a stopped controller behind its gateway (agl remote set first). It reads a gateway admin token
|
|
54
|
+
from standard input, a pipe or a file, never a terminal, provisions the gateway client agl-<installation>-PORT with
|
|
55
|
+
exactly this route and its own credential, and keeps that credential sealed with the TPM; the admin token is used
|
|
56
|
+
once and never stored or printed: revoke it afterwards. --gateway-url is the gateway's API base URL (requests go to
|
|
57
|
+
URL/typedrequest): https, plain http only on loopback (127.0.0.0/8, ::1, localhost), for every request carries a
|
|
58
|
+
credential. --target-host is the address the gateway reaches this host by; both are required the first time and
|
|
59
|
+
kept for the next. Run it again to finish an enrolment whose confirmation was lost (no token needed then), or to
|
|
60
|
+
renew the credential.
|
|
61
|
+
status shows the enrolment and the last sync, from the running controller or, while it is stopped, from its
|
|
62
|
+
database. sync asks the running controller to sync now; --rotate first rotates the trusted-proxy secret, which the
|
|
63
|
+
previous secret stays valid for until a sync carried the new one to the route.
|
|
64
|
+
remove runs against a stopped controller: it deletes the route and its DNS record on the gateway and forgets the
|
|
65
|
+
credential. --abandon forgets the enrolment without reaching the gateway and prints the route left behind, for a
|
|
66
|
+
gateway admin to delete.
|
|
67
|
+
`;
|
|
68
|
+
|
|
69
|
+
export type TGatewayCliArguments =
|
|
70
|
+
| { action: 'enrol'; port: number; gatewayUrl?: string; targetHost?: string }
|
|
71
|
+
| { action: 'status'; port: number; json: boolean }
|
|
72
|
+
| { action: 'sync'; port: number; rotate: boolean }
|
|
73
|
+
| { action: 'remove'; port: number; abandon: boolean };
|
|
74
|
+
|
|
75
|
+
export const parseGatewayCliArguments = (argsArg: readonly string[]): TGatewayCliArguments => {
|
|
76
|
+
const rest = [...argsArg];
|
|
77
|
+
const action = rest.shift();
|
|
78
|
+
if (action !== 'enrol' && action !== 'status' && action !== 'sync' && action !== 'remove') throw new Error(gatewayCliHelp);
|
|
79
|
+
const flags: Record<string, readonly string[]> = {
|
|
80
|
+
enrol: ['--gateway-url', '--target-host'], status: ['--json'], sync: ['--rotate'], remove: ['--abandon'],
|
|
81
|
+
};
|
|
82
|
+
const booleanFlags = new Set(['--json', '--rotate', '--abandon']);
|
|
83
|
+
let port = defaultControllerPort;
|
|
84
|
+
const values = new Map<string, string>();
|
|
85
|
+
const switches = new Set<string>();
|
|
86
|
+
const seen = new Set<string>();
|
|
87
|
+
for (let index = 0; index < rest.length; index++) {
|
|
88
|
+
const token = rest[index]!;
|
|
89
|
+
const equals = token.startsWith('--') ? token.indexOf('=') : -1;
|
|
90
|
+
const flag = equals > 0 ? token.slice(0, equals) : token;
|
|
91
|
+
if (flag !== '--port' && !flags[action]!.includes(flag)) throw new Error(`Unknown remote gateway option: ${flag}.`);
|
|
92
|
+
if (seen.has(flag)) throw new Error(`Duplicate remote gateway option: ${flag}.`);
|
|
93
|
+
seen.add(flag);
|
|
94
|
+
if (booleanFlags.has(flag)) {
|
|
95
|
+
if (equals > 0) throw new Error(`${flag} takes no value.`);
|
|
96
|
+
switches.add(flag);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const value = equals > 0 ? token.slice(equals + 1) : rest[++index];
|
|
100
|
+
if (value === undefined || value === '' || (equals < 0 && value.startsWith('--'))) throw new Error(`${flag} requires a value.`);
|
|
101
|
+
if (flag === '--port') port = Number(value);
|
|
102
|
+
else values.set(flag, value);
|
|
103
|
+
}
|
|
104
|
+
if (!/^[1-9][0-9]{0,4}$/u.test(String(port)) || port > 65_535) throw new Error('--port must be an integer between 1 and 65535.');
|
|
105
|
+
if (action === 'enrol') {
|
|
106
|
+
const gatewayUrl = values.get('--gateway-url');
|
|
107
|
+
const targetHost = values.get('--target-host');
|
|
108
|
+
return { action, port, ...(gatewayUrl === undefined ? {} : { gatewayUrl }), ...(targetHost === undefined ? {} : { targetHost }) };
|
|
109
|
+
}
|
|
110
|
+
if (action === 'status') return { action, port, json: switches.has('--json') };
|
|
111
|
+
if (action === 'sync') return { action, port, rotate: switches.has('--rotate') };
|
|
112
|
+
return { action, port, abandon: switches.has('--abandon') };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** A gateway admin token is one line of visible ASCII; it is checked only for that, never printed. */
|
|
116
|
+
const gatewayAdminTokenPattern = /^[\x21-\x7e]{8,4096}$/u;
|
|
117
|
+
|
|
118
|
+
const readAdminToken = async (inputArg: NodeJS.ReadableStream & { isTTY?: boolean }, portArg: number): Promise<string> => {
|
|
119
|
+
const token = await readStandardInputSecret(
|
|
120
|
+
inputArg,
|
|
121
|
+
'agl remote gateway enrol reads the gateway admin token from a pipe or a file, never from a terminal: run it as '
|
|
122
|
+
+ `\`agl remote gateway enrol --port ${portArg} < ADMIN_TOKEN_FILE\`.`,
|
|
123
|
+
'a gateway admin token',
|
|
124
|
+
);
|
|
125
|
+
if (!gatewayAdminTokenPattern.test(token)) {
|
|
126
|
+
throw new Error('The standard input holds no gateway admin token: one line of visible characters.');
|
|
127
|
+
}
|
|
128
|
+
return token;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const auditGateway = (specArg: TControllerGatewayCredentialSpec, portArg: number): IControllerAuditGateway => ({
|
|
132
|
+
gatewayUrl: specArg.gatewayUrl,
|
|
133
|
+
gatewayClientId: specArg.gatewayClientId,
|
|
134
|
+
appId: specArg.appId,
|
|
135
|
+
hostname: specArg.hostname,
|
|
136
|
+
targetHost: specArg.targetHost,
|
|
137
|
+
targetPort: portArg,
|
|
138
|
+
routeKey: gatewayRouteKey(specArg),
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const sameSpec = (leftArg: TControllerGatewayCredentialSpec, rightArg: TControllerGatewayCredentialSpec): boolean => (
|
|
142
|
+
leftArg.gatewayUrl === rightArg.gatewayUrl && leftArg.gatewayClientId === rightArg.gatewayClientId
|
|
143
|
+
&& leftArg.appId === rightArg.appId && leftArg.hostname === rightArg.hostname && leftArg.targetHost === rightArg.targetHost
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
const sleep = (msArg: number): Promise<void> => new Promise((resolve) => { setTimeout(resolve, msArg); });
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Asks the gateway to confirm a stored candidate, again after each delay while it does not answer, and makes it the
|
|
150
|
+
* active credential once confirmed: dcrouter answers a confirmation it already made with the same success. A refusal
|
|
151
|
+
* (superseded, revoked) drops the candidate. One that never got an answer keeps it for the next `enrol`.
|
|
152
|
+
*/
|
|
153
|
+
const finalizeCandidate = async (
|
|
154
|
+
storeArg: ControllerGatewayClientStore,
|
|
155
|
+
candidateArg: IControllerGatewayCredential,
|
|
156
|
+
delaysMsArg: readonly number[],
|
|
157
|
+
): Promise<{ finalized: true } | { finalized: false; refusal: string }> => {
|
|
158
|
+
const token = await storeArg.unseal(candidateArg);
|
|
159
|
+
const transport = new GatewayClientTransport(candidateArg.gatewayUrl);
|
|
160
|
+
let unanswered = '';
|
|
161
|
+
for (let attempt = 0; attempt <= delaysMsArg.length; attempt++) {
|
|
162
|
+
if (attempt > 0) await sleep(delaysMsArg[attempt - 1]!);
|
|
163
|
+
let result: IReq_FinalizeGatewayClientCredential['response'];
|
|
164
|
+
try {
|
|
165
|
+
result = await transport.fire<IReq_FinalizeGatewayClientCredential>('finalizeGatewayClientCredential', {
|
|
166
|
+
apiToken: token,
|
|
167
|
+
tokenId: candidateArg.tokenId,
|
|
168
|
+
});
|
|
169
|
+
} catch (errorArg) {
|
|
170
|
+
unanswered = gatewayProblemOf(errorArg).message;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (result.success) {
|
|
174
|
+
if (result.gatewayClientId !== candidateArg.gatewayClientId || result.tokenId !== candidateArg.tokenId) {
|
|
175
|
+
throw new Error('The gateway confirmed another credential than the one it issued.');
|
|
176
|
+
}
|
|
177
|
+
await storeArg.promoteCandidate(candidateArg.tokenId);
|
|
178
|
+
return { finalized: true };
|
|
179
|
+
}
|
|
180
|
+
const message = String(result.message).slice(0, 512);
|
|
181
|
+
if (/not initialized/u.test(message)) {
|
|
182
|
+
unanswered = `The gateway is not ready: ${message}`;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
await storeArg.dropCandidate(candidateArg.tokenId);
|
|
186
|
+
return { finalized: false, refusal: message };
|
|
187
|
+
}
|
|
188
|
+
throw new Error(`The gateway did not confirm the enrolment. ${unanswered} The credential it issued is stored sealed: `
|
|
189
|
+
+ 'run `agl remote gateway enrol` again to finish it; that needs no admin token.');
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Deletes every route the credential's gateway client holds on its gateway, with its DNS record, and the route its
|
|
194
|
+
* enrolment names in case the gateway lists it no more; a failure names `--abandon`.
|
|
195
|
+
*/
|
|
196
|
+
const deleteEnrolmentRoutes = async (
|
|
197
|
+
storeArg: ControllerGatewayClientStore,
|
|
198
|
+
credentialArg: IControllerGatewayCredential,
|
|
199
|
+
portArg: number,
|
|
200
|
+
): Promise<string[]> => {
|
|
201
|
+
try {
|
|
202
|
+
const token = await storeArg.unseal(credentialArg);
|
|
203
|
+
const transport = new GatewayClientTransport(credentialArg.gatewayUrl);
|
|
204
|
+
const deleted = await deleteGatewayRoutes(transport, token, () => false);
|
|
205
|
+
const own = gatewayRouteKey(credentialArg);
|
|
206
|
+
if (!deleted.includes(own)) {
|
|
207
|
+
await deleteGatewayRoute(transport, token, gatewayRouteOwnership({ ...credentialArg, targetPort: portArg }));
|
|
208
|
+
}
|
|
209
|
+
return deleted.includes(own) ? deleted : [...deleted, own];
|
|
210
|
+
} catch (errorArg) {
|
|
211
|
+
throw new Error(`The route ${gatewayRouteKey(credentialArg)} on ${credentialArg.gatewayUrl} could not be deleted: `
|
|
212
|
+
+ `${gatewayProblemOf(errorArg).message} Retry, or forget the enrolment with \`agl remote gateway remove --abandon `
|
|
213
|
+
+ `--port ${portArg}\` and have a gateway admin delete the route.`, { cause: errorArg });
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const enrol = async (
|
|
218
|
+
parsedArg: Extract<TGatewayCliArguments, { action: 'enrol' }>,
|
|
219
|
+
optionsArg: IRemoteCliOptions,
|
|
220
|
+
): Promise<string> => withStoppedController(parsedArg.port, async (controllerArg) => {
|
|
221
|
+
const { config, controllerId, issuer } = controllerArg;
|
|
222
|
+
const port = parsedArg.port;
|
|
223
|
+
const origin = new URL(config.publicOrigin);
|
|
224
|
+
if (config.tlsMode !== 'proxy' || origin.protocol !== 'https:' || origin.port !== '') {
|
|
225
|
+
throw new Error(`${controllerId} is not behind its gateway: move it to its public origin with \`agl remote set `
|
|
226
|
+
+ `--public-origin https://HOST --gateway-source ADDRESS --port ${port}\` first.`);
|
|
227
|
+
}
|
|
228
|
+
const hostname = origin.hostname;
|
|
229
|
+
if (!plugins.servezoneInterfaces.data.isGatewayClientHostname(hostname)) {
|
|
230
|
+
throw new Error(`The public origin ${config.publicOrigin} has no hostname a gateway route can serve.`);
|
|
231
|
+
}
|
|
232
|
+
const store = new ControllerGatewayClientStore({
|
|
233
|
+
controllerId,
|
|
234
|
+
issuerId: issuer.issuerId,
|
|
235
|
+
codec: optionsArg.codec ?? new TpmSecretCodec(),
|
|
236
|
+
});
|
|
237
|
+
const record = await store.read();
|
|
238
|
+
const known = record?.candidate ?? record?.active;
|
|
239
|
+
// A kept URL is held to the same rule as a new one.
|
|
240
|
+
const keptUrl = parsedArg.gatewayUrl ?? known?.gatewayUrl;
|
|
241
|
+
const gatewayUrl = keptUrl === undefined ? undefined : normalizeGatewayUrl(keptUrl);
|
|
242
|
+
const targetHost = parsedArg.targetHost === undefined ? known?.targetHost : normalizeGatewayTargetHost(parsedArg.targetHost);
|
|
243
|
+
if (gatewayUrl === undefined || targetHost === undefined) {
|
|
244
|
+
throw new Error('The first agl remote gateway enrol needs --gateway-url and --target-host.');
|
|
245
|
+
}
|
|
246
|
+
if (!isLoopbackHost(targetHost) && !config.gatewaySources) {
|
|
247
|
+
throw new Error(`${controllerId} listens on loopback only, so the gateway cannot reach ${targetHost}:${port}: name `
|
|
248
|
+
+ `the gateway's source address with \`agl remote set --public-origin ${config.publicOrigin} --gateway-source ADDRESS `
|
|
249
|
+
+ `--port ${port}\` first.`);
|
|
250
|
+
}
|
|
251
|
+
const spec: TControllerGatewayCredentialSpec = {
|
|
252
|
+
gatewayUrl,
|
|
253
|
+
gatewayClientId: gatewayClientIdFor(issuer.issuerId, port),
|
|
254
|
+
appId: gatewayAppId(port),
|
|
255
|
+
hostname,
|
|
256
|
+
targetHost,
|
|
257
|
+
};
|
|
258
|
+
const delays = optionsArg.finalizeRetryDelaysMs ?? [1_000, 3_000];
|
|
259
|
+
const finished = await audited(controllerArg.authStore, 'remote.gateway.enrol', async () => {
|
|
260
|
+
// An enrolment whose confirmation was lost is finished with its own credential.
|
|
261
|
+
if (record?.candidate && sameSpec(record.candidate, spec)) {
|
|
262
|
+
if ((await finalizeCandidate(store, record.candidate, delays)).finalized) return 'finished' as const;
|
|
263
|
+
}
|
|
264
|
+
// The routes of a credential for another gateway or hostname would stay behind: they go first.
|
|
265
|
+
const active = (await store.read())?.active;
|
|
266
|
+
if (active && (active.gatewayUrl !== spec.gatewayUrl || active.hostname !== spec.hostname)) {
|
|
267
|
+
await deleteEnrolmentRoutes(store, active, port);
|
|
268
|
+
}
|
|
269
|
+
const adminToken = await readAdminToken(optionsArg.input ?? process.stdin, port);
|
|
270
|
+
const transport = new GatewayClientTransport(spec.gatewayUrl);
|
|
271
|
+
let provisioned: IReq_ProvisionGatewayClientCredential['response'];
|
|
272
|
+
try {
|
|
273
|
+
provisioned = await transport.fire<IReq_ProvisionGatewayClientCredential>('provisionGatewayClientCredential', {
|
|
274
|
+
apiToken: adminToken,
|
|
275
|
+
provisioning: gatewayClientProvisioningSpec({ ...spec, targetPort: port }),
|
|
276
|
+
credentialName: `AGL ${spec.appId} ${spec.hostname}`,
|
|
277
|
+
expiresInDays: null,
|
|
278
|
+
});
|
|
279
|
+
} catch (errorArg) {
|
|
280
|
+
throw new Error(`The gateway did not provision the client: ${gatewayProblemOf(errorArg).message}`, { cause: errorArg });
|
|
281
|
+
}
|
|
282
|
+
if (!provisioned.success) throw new Error(`The gateway refused to provision the client: ${String(provisioned.message).slice(0, 512)}`);
|
|
283
|
+
if (provisioned.gatewayClient.id !== spec.gatewayClientId || provisioned.gatewayClient.type !== gatewayClientType
|
|
284
|
+
|| provisioned.credential.state !== 'candidate') {
|
|
285
|
+
throw new Error('The gateway provisioned another client than the one asked for.');
|
|
286
|
+
}
|
|
287
|
+
// Sealed and stored before the confirmation is asked for: a lost answer is retried with the same credential.
|
|
288
|
+
const candidate = await store.storeCandidate(spec, provisioned.credential.tokenId, provisioned.credential.tokenValue);
|
|
289
|
+
const confirmed = await finalizeCandidate(store, candidate, delays);
|
|
290
|
+
if (!confirmed.finalized) throw new Error(`The gateway refused to confirm the credential it issued: ${confirmed.refusal}`);
|
|
291
|
+
return provisioned.action;
|
|
292
|
+
}, { gateway: auditGateway(spec, port) });
|
|
293
|
+
return [
|
|
294
|
+
`Enrolled ${controllerId} with the gateway at ${spec.gatewayUrl} as gateway client ${spec.gatewayClientId}`
|
|
295
|
+
+ `${finished === 'finished' ? ' (finished the enrolment it had begun)' : ` (${finished})`}.`,
|
|
296
|
+
`Its route: https://${hostname} → ${targetHost}:${port}, LOCAL NETWORK only (${gatewayRouteKey(spec)}).`,
|
|
297
|
+
'The credential is sealed in the controller\'s database; the admin token was not stored: revoke it now.',
|
|
298
|
+
`\`agl start --port ${port}\` syncs the route in the background; \`agl remote gateway status --port ${port}\` shows it.`,
|
|
299
|
+
].join('\n') + '\n';
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const remove = async (
|
|
303
|
+
parsedArg: Extract<TGatewayCliArguments, { action: 'remove' }>,
|
|
304
|
+
optionsArg: IRemoteCliOptions,
|
|
305
|
+
): Promise<string> => withStoppedController(parsedArg.port, async (controllerArg) => {
|
|
306
|
+
const { controllerId, issuer } = controllerArg;
|
|
307
|
+
const port = parsedArg.port;
|
|
308
|
+
const store = new ControllerGatewayClientStore({
|
|
309
|
+
controllerId,
|
|
310
|
+
issuerId: issuer.issuerId,
|
|
311
|
+
codec: optionsArg.codec ?? new TpmSecretCodec(),
|
|
312
|
+
});
|
|
313
|
+
const record = await store.read();
|
|
314
|
+
const credential = record?.active ?? record?.candidate;
|
|
315
|
+
if (!record || !credential) return `${controllerId} is not enrolled with a gateway; nothing to remove.\n`;
|
|
316
|
+
const details = { gateway: auditGateway(credential, port) };
|
|
317
|
+
const routeKeys = [...new Set([record.active, record.candidate]
|
|
318
|
+
.filter((entryArg): entryArg is IControllerGatewayCredential => entryArg !== undefined)
|
|
319
|
+
.map((entryArg) => gatewayRouteKey(entryArg)))];
|
|
320
|
+
if (parsedArg.abandon) {
|
|
321
|
+
await audited(controllerArg.authStore, 'remote.gateway.abandon', async () => { await store.forget(); }, details);
|
|
322
|
+
return [
|
|
323
|
+
`Forgot the gateway enrolment of ${controllerId} without reaching ${credential.gatewayUrl}.`,
|
|
324
|
+
`Left behind on the gateway: the route ${routeKeys.join(' and ')} with its DNS record, and the gateway client `
|
|
325
|
+
+ `${credential.gatewayClientId} with its credential.`,
|
|
326
|
+
`A gateway admin deletes them: in the gateway's Ops UI, delete the route under Routes and the gateway client `
|
|
327
|
+
+ `${credential.gatewayClientId} under Access → Gateway Clients (that revokes its credentials).`,
|
|
328
|
+
].join('\n') + '\n';
|
|
329
|
+
}
|
|
330
|
+
const deleted = await audited(controllerArg.authStore, 'remote.gateway.remove', async () => {
|
|
331
|
+
const routes = record.active ? await deleteEnrolmentRoutes(store, record.active, port) : [];
|
|
332
|
+
await store.forget();
|
|
333
|
+
return routes;
|
|
334
|
+
}, details);
|
|
335
|
+
return [
|
|
336
|
+
`Removed the gateway enrolment of ${controllerId}: ${deleted.length > 0
|
|
337
|
+
? `deleted ${deleted.join(', ')} with ${deleted.length === 1 ? 'its DNS record' : 'their DNS records'}`
|
|
338
|
+
: 'no route had been written'} and forgot the credential.`,
|
|
339
|
+
`The gateway client ${credential.gatewayClientId} stays on ${credential.gatewayUrl} without a route until a gateway admin `
|
|
340
|
+
+ 'deletes it (Access → Gateway Clients), which revokes its credential.',
|
|
341
|
+
].join('\n') + '\n';
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
/** The gateway status a stopped controller's database holds. */
|
|
345
|
+
const storedStatus = (recordArg: IControllerGatewayClientDocument | undefined, portArg: number): IControllerGatewayStatus => {
|
|
346
|
+
const credential = recordArg?.active ?? recordArg?.candidate;
|
|
347
|
+
if (!recordArg || !credential) return { state: 'not_enrolled' };
|
|
348
|
+
const lastSync = recordArg.lastSync;
|
|
349
|
+
return {
|
|
350
|
+
state: recordArg.candidate ? 'finalize_pending' : 'stopped',
|
|
351
|
+
enrolment: controllerGatewayEnrolment(credential, portArg),
|
|
352
|
+
...(lastSync?.syncedAt === undefined ? {} : { lastSyncedAt: lastSync.syncedAt }),
|
|
353
|
+
...(lastSync?.dns ? { dns: lastSync.dns } : {}),
|
|
354
|
+
...(lastSync?.sourceProfileVerified === undefined ? {} : { sourceProfileVerified: lastSync.sourceProfileVerified }),
|
|
355
|
+
...(lastSync?.problem ? { problem: lastSync.problem } : {}),
|
|
356
|
+
};
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const time = (valueArg: number): string => new Date(valueArg).toISOString();
|
|
360
|
+
|
|
361
|
+
export const formatGatewayStatus = (portArg: number, runningArg: boolean, statusArg: IControllerGatewayStatus): string => {
|
|
362
|
+
const lines = [`Gateway client of controller:${portArg} (${runningArg ? 'running' : 'stopped'}): ${statusArg.state.replace(/_/gu, ' ')}`];
|
|
363
|
+
const enrolment = statusArg.enrolment;
|
|
364
|
+
if (enrolment) {
|
|
365
|
+
lines.push(` gateway: ${enrolment.gatewayUrl} as ${enrolment.gatewayClientId}, enrolled ${time(enrolment.enrolledAt)}`);
|
|
366
|
+
lines.push(` route: https://${enrolment.hostname} → ${enrolment.target.host}:${enrolment.target.port}, LOCAL NETWORK only `
|
|
367
|
+
+ `(${gatewayRouteKey(enrolment)})`);
|
|
368
|
+
}
|
|
369
|
+
if (statusArg.lastSyncedAt !== undefined) {
|
|
370
|
+
lines.push(` last synced: ${time(statusArg.lastSyncedAt)}${statusArg.dns ? `, DNS ${statusArg.dns.status}`
|
|
371
|
+
+ `${statusArg.dns.owned ? '' : ' (not owned: records the gateway does not manage satisfy it)'}` : ''}`);
|
|
372
|
+
if (statusArg.sourceProfileVerified !== undefined) {
|
|
373
|
+
lines.push(` source profile: ${statusArg.sourceProfileVerified ? 'LOCAL NETWORK (verified)'
|
|
374
|
+
: 'profile unverified (the gateway does not name the profile it bound)'}`);
|
|
375
|
+
}
|
|
376
|
+
} else if (enrolment) {
|
|
377
|
+
lines.push(' last synced: never');
|
|
378
|
+
}
|
|
379
|
+
if (statusArg.problem) lines.push(` problem (${statusArg.problem.code}): ${statusArg.problem.message}`);
|
|
380
|
+
if (statusArg.nextSyncAt !== undefined) lines.push(` next sync: ${time(statusArg.nextSyncAt)}`);
|
|
381
|
+
if (statusArg.rotationPending) lines.push(' secret rotation: waiting for a sync that carries the new secret to the route');
|
|
382
|
+
return `${lines.join('\n')}\n`;
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
const status = async (
|
|
386
|
+
parsedArg: Extract<TGatewayCliArguments, { action: 'status' }>,
|
|
387
|
+
optionsArg: IRemoteCliOptions,
|
|
388
|
+
): Promise<string> => {
|
|
389
|
+
const running = await tryQueryControllerStatus(parsedArg.port);
|
|
390
|
+
let gateway: IControllerGatewayStatus;
|
|
391
|
+
if (running) {
|
|
392
|
+
if (!running.gateway) {
|
|
393
|
+
throw new Error(`The controller on port ${parsedArg.port} reports no gateway client: it runs an earlier release.`);
|
|
394
|
+
}
|
|
395
|
+
gateway = running.gateway;
|
|
396
|
+
} else {
|
|
397
|
+
gateway = await withStoppedController(parsedArg.port, async (controllerArg) => storedStatus(
|
|
398
|
+
await new ControllerGatewayClientStore({
|
|
399
|
+
controllerId: controllerArg.controllerId,
|
|
400
|
+
issuerId: controllerArg.issuer.issuerId,
|
|
401
|
+
codec: optionsArg.codec ?? new TpmSecretCodec(),
|
|
402
|
+
}).read(),
|
|
403
|
+
parsedArg.port,
|
|
404
|
+
));
|
|
405
|
+
}
|
|
406
|
+
return parsedArg.json
|
|
407
|
+
? `${JSON.stringify({ port: parsedArg.port, controller: running ? 'running' : 'stopped', gateway }, null, 2)}\n`
|
|
408
|
+
: formatGatewayStatus(parsedArg.port, running !== undefined, gateway);
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
const sync = async (parsedArg: Extract<TGatewayCliArguments, { action: 'sync' }>): Promise<{ text: string; synced: boolean }> => {
|
|
412
|
+
const running = await tryQueryControllerStatus(parsedArg.port);
|
|
413
|
+
if (!running) {
|
|
414
|
+
throw new Error(`No controller runs on port ${parsedArg.port}: \`agl start --port ${parsedArg.port}\` syncs its route as it starts.`);
|
|
415
|
+
}
|
|
416
|
+
if (!running.gateway) {
|
|
417
|
+
throw new Error(`The controller on port ${parsedArg.port} reports no gateway client: it runs an earlier release.`);
|
|
418
|
+
}
|
|
419
|
+
const gateway = await requestControllerGatewaySync(parsedArg.port, running, {
|
|
420
|
+
rotate: parsedArg.rotate,
|
|
421
|
+
localCli: await configurationLocalCliActor(),
|
|
422
|
+
});
|
|
423
|
+
return { text: formatGatewayStatus(parsedArg.port, true, gateway), synced: gateway.state === 'synced' };
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
export const runGatewayCli = async (argsArg: string[], optionsArg: IRemoteCliOptions = {}): Promise<void> => {
|
|
427
|
+
const write = optionsArg.write ?? ((textArg: string) => { process.stdout.write(textArg); });
|
|
428
|
+
if (argsArg.length === 0 || argsArg.includes('--help') || argsArg.includes('-h')) { write(gatewayCliHelp); return; }
|
|
429
|
+
const parsed = parseGatewayCliArguments(argsArg);
|
|
430
|
+
if (parsed.action === 'enrol') { write(await enrol(parsed, optionsArg)); return; }
|
|
431
|
+
if (parsed.action === 'remove') { write(await remove(parsed, optionsArg)); return; }
|
|
432
|
+
if (parsed.action === 'status') { write(await status(parsed, optionsArg)); return; }
|
|
433
|
+
const outcome = await sync(parsed);
|
|
434
|
+
write(outcome.text);
|
|
435
|
+
if (!outcome.synced) throw new Error('The gateway route was not synced; the problem above says why.');
|
|
436
|
+
};
|