agl 41.2.0 → 42.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/changelog.md +35 -0
  2. package/dist_serve/bundle.js +1996 -1999
  3. package/dist_serve/bundle.js.map +2 -2
  4. package/dist_serve/bundle.js.third-party-notices.json +3 -68
  5. package/dist_serve/bundle.js.third-party-notices.txt +4 -137
  6. package/dist_ts/00_commitinfo_data.js +1 -1
  7. package/dist_ts/aglapi/aglapisurface.document.js +4 -4
  8. package/dist_ts/classes.authmodels.d.ts +2 -8
  9. package/dist_ts/classes.authmodels.js +60 -23
  10. package/dist_ts/classes.authstore.d.ts +22 -0
  11. package/dist_ts/classes.authstore.js +152 -23
  12. package/dist_ts/classes.cli.d.ts +3 -6
  13. package/dist_ts/classes.cli.js +38 -51
  14. package/dist_ts/classes.config.d.ts +17 -3
  15. package/dist_ts/classes.config.js +55 -18
  16. package/dist_ts/classes.controller.d.ts +12 -12
  17. package/dist_ts/classes.controller.js +46 -117
  18. package/dist_ts/classes.controllermanagement.d.ts +42 -9
  19. package/dist_ts/classes.controllermanagement.js +72 -23
  20. package/dist_ts/classes.controllerservice.d.ts +3 -31
  21. package/dist_ts/classes.controllerservice.js +3 -47
  22. package/dist_ts/classes.mcpcontrollerhost.js +9 -3
  23. package/dist_ts/classes.processinspection.d.ts +8 -1
  24. package/dist_ts/classes.processinspection.js +92 -2
  25. package/dist_ts/classes.proxycontrol.js +6 -1
  26. package/dist_ts/classes.proxydaemon.d.ts +2 -2
  27. package/dist_ts/classes.proxydaemon.js +1 -1
  28. package/dist_ts/classes.proxystore.d.ts +2 -11
  29. package/dist_ts/classes.proxystore.js +3 -25
  30. package/dist_ts/classes.tpmsecretcodec.d.ts +13 -0
  31. package/dist_ts/classes.tpmsecretcodec.js +28 -0
  32. package/dist_ts/classes.trustedproxysecrets.d.ts +91 -0
  33. package/dist_ts/classes.trustedproxysecrets.js +338 -0
  34. package/dist_ts/classes.upgradecoordinator.d.ts +14 -1
  35. package/dist_ts/classes.upgradecoordinator.js +21 -3
  36. package/dist_ts/classes.upgradetransaction.d.ts +32 -0
  37. package/dist_ts/classes.upgradetransaction.js +101 -4
  38. package/dist_ts/functions.controlleradmission.d.ts +67 -0
  39. package/dist_ts/functions.controlleradmission.js +127 -0
  40. package/dist_ts/functions.remotecli.d.ts +27 -0
  41. package/dist_ts/functions.remotecli.js +236 -0
  42. package/dist_ts/functions.retiredenvironment.d.ts +11 -2
  43. package/dist_ts/functions.retiredenvironment.js +22 -5
  44. package/dist_ts/index.d.ts +1 -0
  45. package/dist_ts/index.js +2 -1
  46. package/dist_ts/interfaces.auth.d.ts +29 -4
  47. package/dist_ts/interfaces.auth.js +5 -1
  48. package/dist_ts/interfaces.config.d.ts +14 -0
  49. package/dist_ts/interfaces.config.js +9 -2
  50. package/dist_ts/interfaces.lifecycle.d.ts +3 -0
  51. package/dist_ts_authswitch/classes.authoritydaemon.js +6 -1
  52. package/dist_ts_interfaces/interfaces.d.ts +2 -2
  53. package/dist_ts_interfaces/interfaces.js +1 -1
  54. package/dist_ts_interfaces/management.d.ts +2 -2
  55. package/dist_ts_interfaces/management.js +2 -2
  56. package/dist_ts_interfaces/requests.d.ts +1 -2
  57. package/dist_ts_interfaces/requests.js +1 -1
  58. package/dist_ts_interfaces/requests.upgrade.d.ts +0 -12
  59. package/package.json +12 -11
  60. package/readme.md +84 -24
  61. package/ts/00_commitinfo_data.ts +1 -1
  62. package/ts/aglapi/aglapisurface.document.ts +3 -3
  63. package/ts/classes.authmodels.ts +68 -25
  64. package/ts/classes.authstore.ts +151 -24
  65. package/ts/classes.cli.ts +38 -61
  66. package/ts/classes.config.ts +70 -21
  67. package/ts/classes.controller.ts +50 -163
  68. package/ts/classes.controllermanagement.ts +123 -49
  69. package/ts/classes.controllerservice.ts +3 -56
  70. package/ts/classes.mcpcontrollerhost.ts +10 -5
  71. package/ts/classes.processinspection.ts +84 -3
  72. package/ts/classes.proxycontrol.ts +2 -0
  73. package/ts/classes.proxydaemon.ts +3 -2
  74. package/ts/classes.proxystore.ts +4 -25
  75. package/ts/classes.tpmsecretcodec.ts +26 -0
  76. package/ts/classes.trustedproxysecrets.ts +317 -0
  77. package/ts/classes.upgradecoordinator.ts +33 -3
  78. package/ts/classes.upgradetransaction.ts +130 -1
  79. package/ts/functions.controlleradmission.ts +168 -0
  80. package/ts/functions.remotecli.ts +264 -0
  81. package/ts/functions.retiredenvironment.ts +25 -3
  82. package/ts/index.ts +1 -0
  83. package/ts/interfaces.auth.ts +36 -3
  84. package/ts/interfaces.config.ts +15 -0
  85. package/ts/interfaces.lifecycle.ts +3 -0
  86. package/ts_authswitch/classes.authoritydaemon.ts +2 -0
  87. package/ts_interfaces/interfaces.ts +2 -2
  88. package/ts_interfaces/management.ts +2 -2
  89. package/ts_interfaces/requests.ts +0 -10
  90. package/ts_interfaces/requests.upgrade.ts +0 -16
  91. package/ts_web/00_commitinfo_data.ts +1 -1
@@ -1,8 +1,7 @@
1
1
  import * as plugins from './plugins.js';
2
2
  import { controllerServiceUnitName } from '../ts_interfaces/index.js';
3
3
  import { resolveAGLHomePaths } from './classes.aglhome.js';
4
- import { trustedProxySecretEnvironmentVariable } from './classes.config.js';
5
- import type { IControllerRuntimeOverrides, TControllerTlsMode } from './interfaces.config.js';
4
+ import type { IControllerRuntimeOverrides } from './interfaces.config.js';
6
5
  import { prepareControllerLogFile, resolveControllerLogFilePath } from './functions.controllerlog.js';
7
6
 
8
7
  /**
@@ -15,50 +14,6 @@ export const controllerServiceStandDownExitStatus = 78;
15
14
  /** The oldest systemd whose manager reports the loaded `ExecStart` and environment a start is matched against. */
16
15
  export const controllerServiceManagerMajor = 254;
17
16
 
18
- /**
19
- * The unit cannot run a controller behind a TLS proxy: that controller needs `AGL_TRUSTED_PROXY_SECRET`, and the
20
- * unit carries no secret (its file is readable by every local user). `remedy` says how to run it detached instead.
21
- */
22
- export class ControllerServiceTlsProxyRefusal extends Error {
23
- constructor(remedyArg: string) {
24
- super(`${controllerServiceUnitName} cannot run AGL behind a TLS proxy: the unit cannot carry `
25
- + `${trustedProxySecretEnvironmentVariable}, which that mode needs. ${remedyArg}`);
26
- this.name = 'ControllerServiceTlsProxyRefusal';
27
- }
28
- }
29
-
30
- /**
31
- * Whether a failed start was refused because the unit cannot run this controller: the refusal itself, or one inside
32
- * the `AggregateError` a start throws when releasing its resources failed as well. Such a start fails the same way
33
- * every time, so the unit stands down rather than being restarted.
34
- */
35
- export const isControllerServiceTlsProxyRefusal = (errorArg: unknown): boolean =>
36
- errorArg instanceof ControllerServiceTlsProxyRefusal
37
- || (errorArg instanceof AggregateError && errorArg.errors.some(isControllerServiceTlsProxyRefusal));
38
-
39
- /** What to do when a controller behind a TLS proxy meets an installed unit: leave the unit and run detached. */
40
- export const controllerServiceDetachedRemedy = 'Run it detached: `agl service uninstall`, then `agl start`.';
41
- /** What to do when `agl service install` refuses such a controller: nothing was written, so run it detached. */
42
- export const controllerServiceInstallRemedy = 'Run it detached with `agl start`.';
43
-
44
- /**
45
- * Refuses a controller the unit could not run: one whose stored runtime configuration is in TLS-proxy mode, or
46
- * startup options that ask for that mode (`--behind-tls-proxy`, or an HTTPS `--public-origin`, which only that
47
- * mode serves).
48
- */
49
- export const assertControllerServiceTlsMode = (optionsArg: {
50
- requested?: Pick<IControllerRuntimeOverrides, 'behindTlsProxy' | 'publicOrigin'>;
51
- stored?: TControllerTlsMode;
52
- remedy: string;
53
- }): void => {
54
- const origin = optionsArg.requested?.publicOrigin;
55
- if (
56
- optionsArg.stored === 'proxy'
57
- || optionsArg.requested?.behindTlsProxy === true
58
- || (origin !== undefined && URL.canParse(origin) && new URL(origin).protocol === 'https:')
59
- ) throw new ControllerServiceTlsProxyRefusal(optionsArg.remedy);
60
- };
61
-
62
17
  /** The startup options the unit's `ExecStart` carries; `--port` aside, every one is optional. */
63
18
  export type IControllerServiceOverrides = Omit<IControllerRuntimeOverrides, 'controllerPort' | 'initialProjectDirectory'>;
64
19
 
@@ -163,7 +118,6 @@ export class ControllerServiceEnvironmentRefusal extends Error {
163
118
  */
164
119
  export const controllerServiceEnvironment = (environmentArg: NodeJS.ProcessEnv): Record<string, string> => {
165
120
  const secrets: string[] = [];
166
- if (environmentArg[trustedProxySecretEnvironmentVariable]) secrets.push(trustedProxySecretEnvironmentVariable);
167
121
  for (const name of urlVariables) {
168
122
  const value = environmentArg[name];
169
123
  if (value && urlCarriesCredential(value)) secrets.push(name);
@@ -287,7 +241,6 @@ export class AglControllerService {
287
241
  if (!Number.isInteger(optionsArg.port) || optionsArg.port < 1 || optionsArg.port > 65_535) {
288
242
  throw new Error('The controller port must be 1 to 65535.');
289
243
  }
290
- assertControllerServiceTlsMode({ requested: optionsArg.overrides, remedy: controllerServiceInstallRemedy });
291
244
  return new plugins.smartdaemon.SystemdServiceDefinition({
292
245
  unitName: controllerServiceUnitName,
293
246
  scope: 'user',
@@ -404,9 +357,8 @@ export type TControllerStartRoute =
404
357
  /**
405
358
  * The unit runs the controller only for the AGL home and port it was installed for. A start for that home and
406
359
  * port with startup options or environment the unit does not carry is refused rather than run with settings
407
- * other than the ones asked for; every other home and port starts detached. A start asking for the TLS-proxy mode,
408
- * or from a shell holding its secret, is refused: the unit cannot carry that secret. An upgrade's restart passes
409
- * no `environment`: it restarts the controller as installed, from inside the unit's own environment.
360
+ * other than the ones asked for; every other home and port starts detached. An upgrade's restart passes no
361
+ * `environment`: it restarts the controller as installed, from inside the unit's own environment.
410
362
  */
411
363
  export const resolveControllerStartRoute = async (optionsArg: {
412
364
  service: AglControllerService;
@@ -426,11 +378,6 @@ export const resolveControllerStartRoute = async (optionsArg: {
426
378
  if (installed.aglHome !== optionsArg.aglHome || installed.execStart.port !== optionsArg.port) {
427
379
  return { kind: 'detached' };
428
380
  }
429
- assertControllerServiceTlsMode({ requested: optionsArg.requested, remedy: controllerServiceDetachedRemedy });
430
- if (optionsArg.environment?.[trustedProxySecretEnvironmentVariable]) {
431
- throw new ControllerServiceTlsProxyRefusal('To start the unit, unset it in this shell; to run behind a TLS proxy, '
432
- + 'run it detached: `agl service uninstall`, then `agl start`.');
433
- }
434
381
  const differences: string[] = [];
435
382
  for (const key of Object.keys(overrideFlags) as Array<keyof IControllerServiceOverrides>) {
436
383
  const requested = optionsArg.requested[key];
@@ -189,7 +189,7 @@ class ControllerMcpBoundedTypedRouter extends plugins.typedrequest.TypedRouter {
189
189
  >(
190
190
  typedRequestArg: T,
191
191
  optionsArg: plugins.typedrequest.ITypedRouterRouteOptions = {},
192
- ): Promise<T> {
192
+ ): Promise<T | null> {
193
193
  // The envelope is the only place the caller credential exists; typed handlers never see it.
194
194
  // Resolving here once and running the handler inside the caller context keeps authorization
195
195
  // out of every handler signature and needs no per-request bookkeeping to clean up.
@@ -201,7 +201,7 @@ class ControllerMcpBoundedTypedRouter extends plugins.typedrequest.TypedRouter {
201
201
  : { caller: this.resolveCaller(wireCaller?.credential), release: () => undefined };
202
202
  const caller = lease.caller.subject.kind === 'worker_launch' && !typedRequestArg.method.startsWith('agl.mcp.ticket.')
203
203
  ? anonymousControllerMcpCaller : lease.caller;
204
- let response: T;
204
+ let response: T | null;
205
205
  try {
206
206
  response = await runWithControllerMcpCaller(caller, async () => super.routeAndAddResponse(typedRequestArg, optionsArg));
207
207
  } finally {
@@ -221,17 +221,22 @@ class ControllerMcpBoundedTypedRouter extends plugins.typedrequest.TypedRouter {
221
221
  callerArg: IControllerMcpCaller,
222
222
  typedRequestArg: T,
223
223
  optionsArg: plugins.typedrequest.ITypedRouterRouteOptions,
224
- ): Promise<T> {
224
+ ): Promise<T | null> {
225
225
  return this.bounded(await runWithControllerMcpCaller(
226
226
  callerArg, async () => super.routeAndAddResponse(typedRequestArg, optionsArg),
227
227
  ));
228
228
  }
229
229
 
230
230
  /** What every answer leaves out and how large it may be. */
231
- private bounded<T extends plugins.typedrequestInterfaces.ITypedRequest>(response: T): T {
232
- if (!response) return response;
231
+ private bounded<T extends plugins.typedrequestInterfaces.ITypedRequest>(response: T | null): T | null {
232
+ // Nothing to answer: the router refused the envelope, and the transport refuses it in its own protocol.
233
+ if (response === null) return null;
233
234
  // A credential is never reflected back: the response carries exactly its documented keys.
234
235
  delete (response as unknown as Record<string, unknown>).caller;
236
+ // The private envelope's `response` is always an object, beside an `error` too: every `agl mcp` client sends `{}`
237
+ // and refuses anything else, an installed older one included. TypedRequest 9 answers `null` where TypedRequest 8
238
+ // returned the `{}` the client sent (an error, a handler that returns nothing), so the host restores it here.
239
+ if (response.response === null) (response as plugins.typedrequestInterfaces.ITypedRequest).response = {};
235
240
  if (typeof response.method === 'string' && (response.method.startsWith('agl.mcp.configuration.')
236
241
  || response.method.startsWith('agl.mcp.ticket.') || response.method.startsWith('agl.mcp.fleet.'))) {
237
242
  // TypedRequest normally echoes the request. The owner capability must not travel back in
@@ -20,7 +20,7 @@ export interface IControllerProcessCandidate {
20
20
  }
21
21
 
22
22
  export interface IControllerDataWriterProcessCandidate {
23
- kind: 'controller' | 'temp-password' | 'config' | 'flex-child';
23
+ kind: 'controller' | 'temp-password' | 'config' | 'remote' | 'flex-child';
24
24
  identity: IControllerProcessIdentity;
25
25
  /** Absolute path of the package entry file the process runs (`cli.js` or the Flex child). */
26
26
  entryPath: string;
@@ -461,7 +461,7 @@ const findInstalledControllerPackageFileArgument = (
461
461
  /** The installed `cli.js` path of a process running the given command, if any. */
462
462
  const findInstalledControllerCommandEntryPath = (
463
463
  identityArg: IControllerProcessIdentity,
464
- commandArg: '__serve' | 'foreground' | 'temp-password' | 'config',
464
+ commandArg: '__serve' | 'foreground' | 'temp-password' | 'config' | 'remote',
465
465
  ): string | undefined => {
466
466
  const cli = findInstalledControllerPackageFileArgument(identityArg.arguments, 'cli.js');
467
467
  return cli && identityArg.arguments[cli.index + 1] === commandArg ? cli.path : undefined;
@@ -595,6 +595,87 @@ export const listControllerProcessesForCli = async (
595
595
  return candidates.sort((leftArg, rightArg) => leftArg.identity.pid - rightArg.identity.pid);
596
596
  };
597
597
 
598
+ /**
599
+ * The listening sockets (their inodes) that take a connection to `127.0.0.1:<port>` on the addresses a controller
600
+ * binds: `127.0.0.1` in local mode and `0.0.0.0` behind a TLS proxy, as every release before the loopback management
601
+ * endpoint does.
602
+ */
603
+ const readLinuxControllerListenerInodes = async (portArg: number): Promise<Set<string>> => {
604
+ const port = portArg.toString(16).toUpperCase().padStart(4, '0');
605
+ const endpoints = new Set([`0100007F:${port}`, `00000000:${port}`]);
606
+ const inodes = new Set<string>();
607
+ for (const line of (await plugins.fs.promises.readFile('/proc/net/tcp', 'utf8')).split('\n').slice(1)) {
608
+ const fields = line.trim().split(/\s+/u);
609
+ if (fields.length >= 10 && endpoints.has(fields[1]) && fields[3] === '0A') inodes.add(fields[9]);
610
+ }
611
+ return inodes;
612
+ };
613
+
614
+ /** Whether the Linux process holds one of the sockets `inodesArg` names; one it cannot inspect holds none. */
615
+ const linuxProcessHoldsSocket = async (pidArg: number, inodesArg: ReadonlySet<string>): Promise<boolean> => {
616
+ const directory = `/proc/${pidArg}/fd`;
617
+ let names: string[];
618
+ try {
619
+ names = await plugins.fs.promises.readdir(directory);
620
+ } catch (errorArg) {
621
+ if (['ENOENT', 'ESRCH', 'EACCES', 'EPERM'].includes((errorArg as NodeJS.ErrnoException).code ?? '')) return false;
622
+ throw errorArg;
623
+ }
624
+ for (const name of names) {
625
+ let target: string;
626
+ try {
627
+ target = await plugins.fs.promises.readlink(plugins.path.join(directory, name));
628
+ } catch (errorArg) {
629
+ const code = (errorArg as NodeJS.ErrnoException).code ?? '';
630
+ if (code === 'ENOENT') continue;
631
+ if (['ESRCH', 'EACCES', 'EPERM'].includes(code)) return false;
632
+ throw errorArg;
633
+ }
634
+ const socket = /^socket:\[(\d+)\]$/u.exec(target);
635
+ if (socket && inodesArg.has(socket[1])) return true;
636
+ }
637
+ return false;
638
+ };
639
+
640
+ /** Whether the macOS process listens on `127.0.0.1:<port>` or on every address of that port. */
641
+ const macOsProcessHoldsControllerListener = async (pidArg: number, portArg: number): Promise<boolean> => {
642
+ const output = await execFile('/usr/sbin/lsof', [
643
+ '-nP', '-a', '-p', String(pidArg), `-iTCP:${portArg}`, '-sTCP:LISTEN', '-Fpn',
644
+ ], { emptyExitCode: 1, timeoutMs: 1_500, maxBufferBytes: 64 * 1024 });
645
+ const lines = output.split(/\r?\n/u);
646
+ return lines.includes(`p${pidArg}`)
647
+ && lines.some((lineArg) => lineArg === `n127.0.0.1:${portArg}` || lineArg === `n*:${portArg}`);
648
+ };
649
+
650
+ /**
651
+ * Whether the listener on `127.0.0.1:<port>` is an AGL controller of an installed package: a process running that
652
+ * package's `cli.js` (`node_modules/agl` or `node_modules/@modelprofile.com/harness-controller`) with the controller
653
+ * command (`__serve`, also as the service unit runs it, or `foreground`) for this port holds the port's listening
654
+ * socket. Linux reads the kernel's socket table, macOS asks `lsof`; elsewhere nothing confirms it.
655
+ */
656
+ export const installedControllerHoldsPort = async (portArg: number): Promise<boolean> => {
657
+ if (process.platform !== 'linux' && process.platform !== 'darwin') return false;
658
+ const candidates: number[] = [];
659
+ await visitProcessIdentities(async (identity) => {
660
+ const runsController = findInstalledControllerCommandEntryPath(identity, '__serve') !== undefined
661
+ || findInstalledControllerCommandEntryPath(identity, 'foreground') !== undefined;
662
+ if (runsController && readEffectiveArgvPort(identity.arguments) === portArg) candidates.push(identity.pid);
663
+ });
664
+ if (candidates.length === 0) return false;
665
+ if (process.platform === 'darwin') {
666
+ for (const pid of candidates) {
667
+ if (await macOsProcessHoldsControllerListener(pid, portArg)) return true;
668
+ }
669
+ return false;
670
+ }
671
+ const inodes = await readLinuxControllerListenerInodes(portArg);
672
+ if (inodes.size === 0) return false;
673
+ for (const pid of candidates) {
674
+ if (await linuxProcessHoldsSocket(pid, inodes)) return true;
675
+ }
676
+ return false;
677
+ };
678
+
598
679
  export const listControllerDataWriterProcessesForCli = async (
599
680
  cliPathArg: string,
600
681
  optionsArg: { includeInstalledPackagePaths?: boolean } = {},
@@ -631,7 +712,7 @@ export const listControllerDataWriterProcessesForCliPaths = async (
631
712
  ?? findInstalledControllerCommandEntryPath(identityArg, 'foreground');
632
713
  if (installedEntryPath) return { kind: 'controller', entryPath: installedEntryPath };
633
714
  }
634
- for (const command of ['temp-password', 'config'] as const) {
715
+ for (const command of ['temp-password', 'config', 'remote'] as const) {
635
716
  for (const cliPath of cliPaths) {
636
717
  if (await processIdentityHasCliCommand(identityArg, cliPath, command)) {
637
718
  return { kind: command, entryPath: cliPath };
@@ -421,6 +421,8 @@ export class AglProxyControlServer {
421
421
  void this.router.routeAndAddResponse(request, { trustedLocalData: { proxyConnection: connection },
422
422
  trustedAbortSignal: abort.signal })
423
423
  .then((responseArg) => {
424
+ // Nothing to answer (the router refused the envelope): refuse it by closing, never with a bare null.
425
+ if (responseArg === null) { socketArg.destroy(); return; }
424
426
  if (!socketArg.writable) return;
425
427
  const raw = JSON.stringify(responseArg) + '\n';
426
428
  if (Buffer.byteLength(raw) > proxyControlMaximumFrameBytes) { socketArg.destroy(); return; }
@@ -10,7 +10,8 @@ import { aglProxyConfinedClient, aglProxyConfinedOrigin, proxyClientNamePattern,
10
10
  import { AglProxyControlRefusal, AglProxyControlServer, type IAglProxyConfinedGrant, type IAglProxyConnection,
11
11
  type IAglProxyStatus, type IReq_AglProxyCatalog, type IReq_AglProxyConfined, type IReq_AglProxyReclaim,
12
12
  type TAglProxyLeaseCatalog } from './classes.proxycontrol.js';
13
- import { ProxyStore, type IProxySecretCodec } from './classes.proxystore.js';
13
+ import { ProxyStore } from './classes.proxystore.js';
14
+ import type { ITpmSecretCodec } from './classes.tpmsecretcodec.js';
14
15
  import type { InferenceUsageStore } from './classes.inferenceusage.js';
15
16
  import { prepareProxyCredentialDirectory, proxyCredentialFile, removeProxyCredentialFile,
16
17
  writeProxyCredentialFile } from './functions.proxycredentialfile.js';
@@ -23,7 +24,7 @@ export interface IAglProxyDaemonOptions {
23
24
  credentialDirectory: string;
24
25
  authorityPaths: IAuthoritySocketPaths;
25
26
  port: number;
26
- codec?: IProxySecretCodec;
27
+ codec?: ITpmSecretCodec;
27
28
  providerFetch?: typeof fetch;
28
29
  limits?: IInferenceGatewayLimits;
29
30
  /** How long a lease kept across a restart waits for its holder; `proxyLeaseReclaimWindowMs` unless a test shortens it. */
@@ -1,5 +1,6 @@
1
1
  import * as plugins from './plugins.js';
2
2
  import { EmbeddedControllerDatabase } from './classes.embeddeddb.js';
3
+ import { TpmSecretCodec, type ITpmSecretCodec } from './classes.tpmsecretcodec.js';
3
4
  import { InferenceAffinityModel, InferenceAffinityStore } from './classes.inferenceaffinity.js';
4
5
  import { InferenceSessionModel } from './classes.inferencesession.js';
5
6
  import { InferencePlacementModel, InferencePlacementStore } from './classes.inferenceplacement.js';
@@ -22,28 +23,6 @@ const maximumProxyClients = 32;
22
23
  /** The gateway admits 128 credentials in all; no more leases than that are ever kept. */
23
24
  const maximumProxyLeases = 128;
24
25
 
25
- /** Seals a client credential for this user and machine; only its ciphertext is stored. */
26
- export interface IProxySecretCodec {
27
- seal(nameArg: string, plaintextArg: Uint8Array): Promise<string>;
28
- unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array>;
29
- }
30
-
31
- /** TPM2-sealed credentials through `systemd-creds`, bound to their name. */
32
- export class ProxyTpmSecretCodec implements IProxySecretCodec {
33
- public async seal(nameArg: string, plaintextArg: Uint8Array): Promise<string> {
34
- const ciphertext = await plugins.smartsecret.sealSmartSecretTpm2Credential(nameArg, plaintextArg);
35
- try { return Buffer.from(ciphertext).toString('base64url'); }
36
- finally { ciphertext.fill(0); }
37
- }
38
-
39
- public async unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array> {
40
- if (!/^[A-Za-z0-9_-]{1,131072}$/.test(ciphertextArg)) throw new Error('Invalid sealed proxy credential.');
41
- const bytes = Buffer.from(ciphertextArg, 'base64url');
42
- try { return await plugins.smartsecret.unsealSmartSecretTpm2Credential(nameArg, bytes); }
43
- finally { bytes.fill(0); }
44
- }
45
- }
46
-
47
26
  export interface IProxyIdentityDocument {
48
27
  id: string;
49
28
  /** Names this proxy's authority bindings; independent of its port, so a restart reclaims exactly its own. */
@@ -215,7 +194,7 @@ let activeOwner: object | undefined;
215
194
  export interface IProxyStoreOptions {
216
195
  /** The proxy's own NoSQLDB storage root; no other process opens it. */
217
196
  dataDirectory: string;
218
- codec?: IProxySecretCodec;
197
+ codec?: ITpmSecretCodec;
219
198
  }
220
199
 
221
200
  export interface IProxyIdentity {
@@ -250,7 +229,7 @@ export interface IProxyLease {
250
229
  * is refused, so stopping the proxy never stops an engine someone else uses.
251
230
  */
252
231
  export class ProxyStore {
253
- private readonly codec: IProxySecretCodec;
232
+ private readonly codec: ITpmSecretCodec;
254
233
  private readonly owner = {};
255
234
  private embedded?: EmbeddedControllerDatabase;
256
235
  private database?: plugins.nosqldb.SmartdataDb;
@@ -258,7 +237,7 @@ export class ProxyStore {
258
237
 
259
238
  constructor(private readonly options: IProxyStoreOptions) {
260
239
  if (!plugins.path.isAbsolute(options.dataDirectory)) throw new Error('The proxy data directory must be absolute.');
261
- this.codec = options.codec ?? new ProxyTpmSecretCodec();
240
+ this.codec = options.codec ?? new TpmSecretCodec();
262
241
  }
263
242
 
264
243
  public async open(): Promise<IProxyIdentity> {
@@ -0,0 +1,26 @@
1
+ import * as plugins from './plugins.js';
2
+
3
+ /** Seals a secret for this user and machine under a name it is bound to; only its ciphertext is stored. */
4
+ export interface ITpmSecretCodec {
5
+ seal(nameArg: string, plaintextArg: Uint8Array): Promise<string>;
6
+ unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array>;
7
+ }
8
+
9
+ /**
10
+ * TPM2-sealed secrets through `systemd-creds`, bound to their name: the inference proxy's client credentials and
11
+ * signature key, and the controller's trusted-proxy secret.
12
+ */
13
+ export class TpmSecretCodec implements ITpmSecretCodec {
14
+ public async seal(nameArg: string, plaintextArg: Uint8Array): Promise<string> {
15
+ const ciphertext = await plugins.smartsecret.sealSmartSecretTpm2Credential(nameArg, plaintextArg);
16
+ try { return Buffer.from(ciphertext).toString('base64url'); }
17
+ finally { ciphertext.fill(0); }
18
+ }
19
+
20
+ public async unseal(nameArg: string, ciphertextArg: string): Promise<Uint8Array> {
21
+ if (!/^[A-Za-z0-9_-]{1,131072}$/.test(ciphertextArg)) throw new Error('Invalid sealed secret.');
22
+ const bytes = Buffer.from(ciphertextArg, 'base64url');
23
+ try { return await plugins.smartsecret.unsealSmartSecretTpm2Credential(nameArg, bytes); }
24
+ finally { bytes.fill(0); }
25
+ }
26
+ }