@impz/testing 7.0.1 → 7.1.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 (2) hide show
  1. package/dist/index.js +164 -70
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -19370,7 +19370,7 @@ import * as z17 from "zod";
19370
19370
 
19371
19371
  // packages/api/src/schemas/api-call-schema.ts
19372
19372
  import * as z12 from "zod";
19373
- var ApiActorSchema = z12.enum(["token", "dashboard", "ssh", "tailnet", "oauth"]);
19373
+ var ApiActorSchema = z12.enum(["token", "dashboard", "ssh", "tailnet", "oauth", "service"]);
19374
19374
  var ApiCallSchema = z12.object({
19375
19375
  at: z12.date(),
19376
19376
  procedure: z12.string(),
@@ -21476,12 +21476,18 @@ function parseTailnetRules(text) {
21476
21476
  import * as z50 from "zod";
21477
21477
  var PublicMcpEnvSchema = z50.object({
21478
21478
  IMP_MCP_PUBLIC_URL: z50.string().optional(),
21479
- IMP_MCP_PUBLIC_PORT: z50.coerce.number().pipe(z50.int().min(1).max(65535)).default(7071)
21479
+ IMP_MCP_PUBLIC_PORT: z50.coerce.number().pipe(z50.int().min(1).max(65535)).default(7071),
21480
+ IMP_MCP_SERVICE_TOKEN_FILE: z50.string().min(1).optional(),
21481
+ IMP_MCP_SERVICE_CLIENTS_FILE: z50.string().min(1).optional()
21480
21482
  });
21481
21483
  var LOOPBACK_HOSTS = new Set(["127.0.0.1", "localhost", "[::1]"]);
21482
21484
  function parsePublicMcpConfig(env, others, slotPorts) {
21483
21485
  const text = env.IMP_MCP_PUBLIC_URL;
21486
+ const serviceFiles = parseServiceFiles(env);
21484
21487
  if (text === undefined) {
21488
+ if (serviceFiles !== null) {
21489
+ throw new Error("IMP_MCP_SERVICE_TOKEN_FILE and IMP_MCP_SERVICE_CLIENTS_FILE need IMP_MCP_PUBLIC_URL");
21490
+ }
21485
21491
  return null;
21486
21492
  }
21487
21493
  const origin = parseOrigin(text);
@@ -21493,7 +21499,21 @@ function parsePublicMcpConfig(env, others, slotPorts) {
21493
21499
  if (port >= slotPorts[0] && port <= slotPorts[1]) {
21494
21500
  throw new Error(`IMP_MCP_PUBLIC_PORT ${String(port)} is one of the imps' ports, ${String(slotPorts[0])} to ${String(slotPorts[1])}`);
21495
21501
  }
21496
- return { origin: origin.origin, host: origin.host, port };
21502
+ return { origin: origin.origin, host: origin.host, port, serviceFiles };
21503
+ }
21504
+ function parseServiceFiles(env) {
21505
+ const tokenFile = env.IMP_MCP_SERVICE_TOKEN_FILE;
21506
+ const clientsFile = env.IMP_MCP_SERVICE_CLIENTS_FILE;
21507
+ if (tokenFile === undefined && clientsFile === undefined) {
21508
+ return null;
21509
+ }
21510
+ if (tokenFile === undefined) {
21511
+ throw new Error("IMP_MCP_SERVICE_CLIENTS_FILE needs IMP_MCP_SERVICE_TOKEN_FILE");
21512
+ }
21513
+ if (clientsFile === undefined) {
21514
+ throw new Error("IMP_MCP_SERVICE_TOKEN_FILE needs IMP_MCP_SERVICE_CLIENTS_FILE");
21515
+ }
21516
+ return { tokenFile, clientsFile };
21497
21517
  }
21498
21518
  function parseOrigin(text) {
21499
21519
  let url;
@@ -22046,7 +22066,7 @@ import { join as join49 } from "path";
22046
22066
  // packages/daemon/package.json
22047
22067
  var package_default = {
22048
22068
  name: "@impz/daemon",
22049
- version: "7.0.1",
22069
+ version: "7.1.0",
22050
22070
  private: true,
22051
22071
  description: "impd: the imp host daemon",
22052
22072
  type: "module",
@@ -31563,7 +31583,7 @@ async function checkAccess(access, caller, input, readers) {
31563
31583
  reason: "scope"
31564
31584
  };
31565
31585
  }
31566
- if (access.on === "grant" && caller.kind === "oauth") {
31586
+ if (access.on === "grant" && (caller.kind === "oauth" || caller.kind === "service")) {
31567
31587
  return { message: `${formatCaller(caller)} may not grant or revoke secrets`, reason: null };
31568
31588
  }
31569
31589
  if (access.on === "imp" && access.noGrantable === true && caller.grantable.length > 0) {
@@ -54411,6 +54431,76 @@ function buildOAuthCaller(access, approver) {
54411
54431
  };
54412
54432
  }
54413
54433
 
54434
+ // packages/daemon/src/identity/oauth/build-service-caller.ts
54435
+ function buildServiceCaller(access, clients) {
54436
+ const clientName = access.clientName.slice("service:".length);
54437
+ const entry = Object.hasOwn(clients, clientName) ? clients[clientName] : undefined;
54438
+ if (entry === undefined || access.scopes.length === 0) {
54439
+ return null;
54440
+ }
54441
+ return {
54442
+ kind: "service",
54443
+ name: access.clientName,
54444
+ scope: pickGrantScope(access.scopes),
54445
+ imps: entry.imps,
54446
+ grantable: [],
54447
+ imageRefPrefix: null,
54448
+ tokenId: null,
54449
+ grantId: null,
54450
+ expiresAt: null,
54451
+ principal: access.clientName,
54452
+ display: `${access.clientName} via client credentials`
54453
+ };
54454
+ }
54455
+
54456
+ // packages/daemon/src/identity/oauth/read-service-files.ts
54457
+ import { readFile as readFile5 } from "fs/promises";
54458
+ import { parseServiceTokenFile } from "@zgeoff/mcp-auth";
54459
+ import * as z97 from "zod";
54460
+ var SERVICE_TOKEN_PREFIX = "impsvc_";
54461
+ var ServiceClientSchema = z97.strictObject({ imps: z97.array(GrantPatternSchema).min(1).max(32) });
54462
+ var ServiceClientsFileSchema = z97.strictObject({
54463
+ clients: z97.record(z97.string(), ServiceClientSchema)
54464
+ });
54465
+ function parseServiceClients(text) {
54466
+ let raw;
54467
+ try {
54468
+ raw = JSON.parse(text);
54469
+ } catch (error) {
54470
+ throw new Error("the file is not valid JSON", { cause: error });
54471
+ }
54472
+ const parsed = ServiceClientsFileSchema.safeParse(raw);
54473
+ if (!parsed.success) {
54474
+ throw new Error(parsed.error.issues.map((issue) => `${issue.path.map(String).join(".") || "the file"}: ${issue.message}`).join("; "));
54475
+ }
54476
+ return parsed.data.clients;
54477
+ }
54478
+ async function readServiceSettings(files) {
54479
+ const tokenText = await readText2("IMP_MCP_SERVICE_TOKEN_FILE", files.tokenFile);
54480
+ const clientsText = await readText2("IMP_MCP_SERVICE_CLIENTS_FILE", files.clientsFile);
54481
+ const tokens = parseServiceTokenFile(tokenText, {
54482
+ prefix: SERVICE_TOKEN_PREFIX,
54483
+ scopes: ScopeSchema.options
54484
+ });
54485
+ if (!tokens.ok) {
54486
+ throw new Error(`IMP_MCP_SERVICE_TOKEN_FILE ${files.tokenFile}: ${tokens.reason}`);
54487
+ }
54488
+ try {
54489
+ return { tokens: tokens.file, clients: parseServiceClients(clientsText) };
54490
+ } catch (error) {
54491
+ throw new Error(`IMP_MCP_SERVICE_CLIENTS_FILE ${files.clientsFile}: ${getErrorMessage(error)}`, { cause: error });
54492
+ }
54493
+ }
54494
+ async function readText2(variable, path) {
54495
+ try {
54496
+ return await readFile5(path, "utf8");
54497
+ } catch (error) {
54498
+ throw new Error(`${variable} ${path} cannot be read: ${getErrorMessage(error)}`, {
54499
+ cause: error
54500
+ });
54501
+ }
54502
+ }
54503
+
54414
54504
  // packages/daemon/src/identity/oauth/record-security-event.ts
54415
54505
  function recordSecurityEvent(event) {
54416
54506
  const fields = {
@@ -54444,7 +54534,10 @@ async function createImpdMcpAuth(deps) {
54444
54534
  }
54445
54535
  };
54446
54536
  const config = deps.config;
54447
- const opened = config === null ? { server: null, admin: await openMCPAuthAdmin(options) } : await openImpdServer(options, config, deps.tokens);
54537
+ const serviceFiles = config?.serviceFiles ?? null;
54538
+ const service = serviceFiles === null ? null : await readServiceSettings(serviceFiles);
54539
+ const opened = config === null ? { server: null, admin: await openMCPAuthAdmin(options) } : await openImpdServer(options, config, deps.tokens, service);
54540
+ const serviceClients = service?.clients ?? {};
54448
54541
  const server = opened.server;
54449
54542
  const admin = opened.admin;
54450
54543
  return {
@@ -54460,7 +54553,7 @@ async function createImpdMcpAuth(deps) {
54460
54553
  },
54461
54554
  readCaller: async (access) => {
54462
54555
  if (access.kind === "service") {
54463
- return null;
54556
+ return buildServiceCaller(access, serviceClients);
54464
54557
  }
54465
54558
  const grant = await deps.db.selectFrom("user").select("approvedBy").where("id", "=", access.grantID).executeTakeFirst();
54466
54559
  const approvedBy = grant?.approvedBy ?? null;
@@ -54475,13 +54568,14 @@ async function createImpdMcpAuth(deps) {
54475
54568
  }
54476
54569
  };
54477
54570
  }
54478
- async function openImpdServer(options, config, tokens) {
54571
+ async function openImpdServer(options, config, tokens, service) {
54479
54572
  const server = await openMCPAuth({
54480
54573
  ...options,
54481
54574
  origin: config.origin,
54482
54575
  productName: "imp",
54483
54576
  paths: DEFAULT_PATHS,
54484
54577
  grantLimits: buildImpGrantLimits({ findToken: tokens.findById }),
54578
+ ...service === null ? {} : { serviceTokens: { prefix: SERVICE_TOKEN_PREFIX, file: service.tokens } },
54485
54579
  security: { record: recordSecurityEvent }
54486
54580
  });
54487
54581
  return { server, admin: server };
@@ -54512,8 +54606,8 @@ async function loadImpdAccess(config, deps, isFileKey) {
54512
54606
  }
54513
54607
 
54514
54608
  // packages/daemon/src/network/https/dns/dns-token.ts
54515
- import { readFile as readFile5 } from "fs/promises";
54516
- function createDnsToken(source, now, readText = (path) => readFile5(path, "utf8")) {
54609
+ import { readFile as readFile6 } from "fs/promises";
54610
+ function createDnsToken(source, now, readText = (path) => readFile6(path, "utf8")) {
54517
54611
  if (source.kind === "value") {
54518
54612
  return { read: () => Promise.resolve(source.value), check: null };
54519
54613
  }
@@ -54580,8 +54674,8 @@ function loadOrCreateHostId(dataDir) {
54580
54674
 
54581
54675
  // packages/daemon/src/network/tailnet-names/oauth-credential.ts
54582
54676
  import { readFileSync as readFileSync30, statSync as statSync14 } from "fs";
54583
- import * as z97 from "zod";
54584
- var CredentialSchema = z97.object({ clientId: z97.string().min(1), clientSecret: z97.string().min(1) });
54677
+ import * as z98 from "zod";
54678
+ var CredentialSchema = z98.object({ clientId: z98.string().min(1), clientSecret: z98.string().min(1) });
54585
54679
  function readOAuthCredential(path) {
54586
54680
  const mode = statSync14(path).mode & 511;
54587
54681
  if ((mode & 63) !== 0) {
@@ -54602,17 +54696,17 @@ function readJson(path) {
54602
54696
  }
54603
54697
 
54604
54698
  // packages/daemon/src/host-net/net/tailscale-serve.ts
54605
- import * as z98 from "zod";
54606
- var HandlerSchema = z98.object({ Proxy: z98.string().optional() });
54607
- var WebSchema = z98.object({
54608
- Handlers: z98.record(z98.string(), HandlerSchema).nullable().optional()
54699
+ import * as z99 from "zod";
54700
+ var HandlerSchema = z99.object({ Proxy: z99.string().optional() });
54701
+ var WebSchema = z99.object({
54702
+ Handlers: z99.record(z99.string(), HandlerSchema).nullable().optional()
54609
54703
  });
54610
- var ServiceConfigSchema = z98.object({
54611
- Web: z98.record(z98.string(), WebSchema).nullable().optional()
54704
+ var ServiceConfigSchema = z99.object({
54705
+ Web: z99.record(z99.string(), WebSchema).nullable().optional()
54612
54706
  });
54613
- var ServeStatusSchema = z98.object({
54614
- TCP: z98.record(z98.string(), z98.unknown()).optional(),
54615
- Services: z98.record(z98.string(), ServiceConfigSchema).nullable().optional()
54707
+ var ServeStatusSchema = z99.object({
54708
+ TCP: z99.record(z99.string(), z99.unknown()).optional(),
54709
+ Services: z99.record(z99.string(), ServiceConfigSchema).nullable().optional()
54616
54710
  });
54617
54711
  function parseServeStatus(json) {
54618
54712
  try {
@@ -54663,17 +54757,17 @@ function createServiceServe(run = runChecked) {
54663
54757
  }
54664
54758
 
54665
54759
  // packages/daemon/src/network/tailnet-names/services-api.ts
54666
- import * as z99 from "zod";
54760
+ import * as z100 from "zod";
54667
54761
  var TAILSCALE_API = "https://api.tailscale.com/api/v2";
54668
54762
  var TOKEN_MARGIN_MS = 60000;
54669
- var ServiceSchema2 = z99.object({
54670
- name: z99.string(),
54671
- comment: z99.string().optional(),
54672
- ports: z99.array(z99.string()).readonly().optional(),
54673
- tags: z99.array(z99.string()).readonly().nullable().optional()
54763
+ var ServiceSchema2 = z100.object({
54764
+ name: z100.string(),
54765
+ comment: z100.string().optional(),
54766
+ ports: z100.array(z100.string()).readonly().optional(),
54767
+ tags: z100.array(z100.string()).readonly().nullable().optional()
54674
54768
  }).readonly();
54675
- var ServiceListSchema2 = z99.object({ vipServices: z99.array(ServiceSchema2).nullable().optional() });
54676
- var TokenSchema3 = z99.object({ access_token: z99.string(), expires_in: z99.number().positive() });
54769
+ var ServiceListSchema2 = z100.object({ vipServices: z100.array(ServiceSchema2).nullable().optional() });
54770
+ var TokenSchema3 = z100.object({ access_token: z100.string(), expires_in: z100.number().positive() });
54677
54771
 
54678
54772
  class TailscaleApiError extends Error {
54679
54773
  status;
@@ -54762,7 +54856,7 @@ function createServicesApi(options) {
54762
54856
  }
54763
54857
  };
54764
54858
  }
54765
- var ErrorSchema = z99.object({ message: z99.string() });
54859
+ var ErrorSchema = z100.object({ message: z100.string() });
54766
54860
  function readErrorText(text) {
54767
54861
  try {
54768
54862
  return ErrorSchema.parse(JSON.parse(text)).message;
@@ -55225,14 +55319,14 @@ function formatAcmeError(error) {
55225
55319
  // packages/daemon/src/network/https/acme/cert-store.ts
55226
55320
  import { existsSync as existsSync24, mkdirSync as mkdirSync25, readFileSync as readFileSync31, rmSync as rmSync30 } from "fs";
55227
55321
  import { join as join38 } from "path";
55228
- import * as z100 from "zod";
55229
- var AttemptStateSchema = z100.object({
55230
- failures: z100.int().nonnegative(),
55231
- lastAttemptAt: z100.number().nullable(),
55232
- lastError: z100.string().nullable()
55322
+ import * as z101 from "zod";
55323
+ var AttemptStateSchema = z101.object({
55324
+ failures: z101.int().nonnegative(),
55325
+ lastAttemptAt: z101.number().nullable(),
55326
+ lastError: z101.string().nullable()
55233
55327
  });
55234
- var AccountSchema = z100.object({ directoryUrl: z100.string(), url: z100.string(), keyPem: z100.string() });
55235
- var LegacyAccountSchema = z100.object({ directoryUrl: z100.string(), url: z100.string() });
55328
+ var AccountSchema = z101.object({ directoryUrl: z101.string(), url: z101.string(), keyPem: z101.string() });
55329
+ var LegacyAccountSchema = z101.object({ directoryUrl: z101.string(), url: z101.string() });
55236
55330
  var NO_ATTEMPTS = { failures: 0, lastAttemptAt: null, lastError: null };
55237
55331
  function createCertStore(dataDir) {
55238
55332
  const directory = join38(dataDir, "tls");
@@ -55370,7 +55464,7 @@ function createChalltestsrvProvider(apiUrl) {
55370
55464
  }
55371
55465
 
55372
55466
  // packages/daemon/src/network/https/dns/cloudflare-provider.ts
55373
- import * as z101 from "zod";
55467
+ import * as z102 from "zod";
55374
55468
 
55375
55469
  // packages/daemon/src/network/https/dns/wait-for-txt.ts
55376
55470
  import { Resolver as Resolver2 } from "dns/promises";
@@ -55432,30 +55526,30 @@ var CLOUDFLARE_API = "https://api.cloudflare.com/client/v4";
55432
55526
  var TXT_TTL_S = 60;
55433
55527
  var A_TTL_S = 300;
55434
55528
  var PAGE_SIZE = 500;
55435
- var ApiErrorSchema = z101.object({ code: z101.number().optional(), message: z101.string() });
55436
- var EnvelopeSchema = z101.object({
55437
- success: z101.boolean(),
55438
- errors: z101.array(ApiErrorSchema).default([]),
55439
- result: z101.unknown().optional(),
55440
- result_info: z101.object({ page: z101.number(), total_pages: z101.number() }).optional()
55529
+ var ApiErrorSchema = z102.object({ code: z102.number().optional(), message: z102.string() });
55530
+ var EnvelopeSchema = z102.object({
55531
+ success: z102.boolean(),
55532
+ errors: z102.array(ApiErrorSchema).default([]),
55533
+ result: z102.unknown().optional(),
55534
+ result_info: z102.object({ page: z102.number(), total_pages: z102.number() }).optional()
55441
55535
  });
55442
- var ZoneSchema = z101.object({
55443
- id: z101.string(),
55444
- name: z101.string(),
55445
- name_servers: z101.array(z101.string())
55536
+ var ZoneSchema = z102.object({
55537
+ id: z102.string(),
55538
+ name: z102.string(),
55539
+ name_servers: z102.array(z102.string())
55446
55540
  });
55447
- var RecordSchema = z101.object({
55448
- id: z101.string(),
55449
- name: z101.string().optional(),
55450
- type: z101.string().optional(),
55451
- content: z101.string(),
55452
- proxied: z101.boolean().optional(),
55453
- comment: z101.string().nullable().optional()
55541
+ var RecordSchema = z102.object({
55542
+ id: z102.string(),
55543
+ name: z102.string().optional(),
55544
+ type: z102.string().optional(),
55545
+ content: z102.string(),
55546
+ proxied: z102.boolean().optional(),
55547
+ comment: z102.string().nullable().optional()
55454
55548
  });
55455
55549
  var MANAGED_COMMENT = "managed by impd";
55456
55550
  var ADDRESS_TYPES = new Set(["A", "AAAA", "CNAME"]);
55457
- var ZoneListSchema = z101.array(ZoneSchema);
55458
- var RecordListSchema = z101.array(RecordSchema);
55551
+ var ZoneListSchema = z102.array(ZoneSchema);
55552
+ var RecordListSchema = z102.array(RecordSchema);
55459
55553
  function createCloudflareProvider(options) {
55460
55554
  const base = options.apiUrl ?? CLOUDFLARE_API;
55461
55555
  const send = options.fetch ?? fetch;
@@ -58077,8 +58171,8 @@ import {
58077
58171
  statSync as statSync15
58078
58172
  } from "fs";
58079
58173
  import { basename as basename5, dirname as dirname11, join as join40 } from "path";
58080
- import * as z102 from "zod";
58081
- var CopiesSchema = z102.record(z102.string(), z102.object({ ino: z102.string(), ctimeNs: z102.string(), size: z102.string() }));
58174
+ import * as z103 from "zod";
58175
+ var CopiesSchema = z103.record(z103.string(), z103.object({ ino: z103.string(), ctimeNs: z103.string(), size: z103.string() }));
58082
58176
  function createXfsBackups(ctx) {
58083
58177
  const backup = buildBackupPaths(ctx.dataDir);
58084
58178
  const freshCopies = new Set;
@@ -60822,19 +60916,19 @@ log tail`);
60822
60916
  // packages/daemon/src/test-utils/vm/create-stub-uffd-handler.ts
60823
60917
  import { existsSync as existsSync35, readFileSync as readFileSync40, writeFileSync as writeFileSync17 } from "fs";
60824
60918
  import { join as join50 } from "path";
60825
- import * as z103 from "zod";
60826
- var StartSchema = z103.object({
60827
- options: z103.object({
60828
- socket: z103.string().optional(),
60829
- uid: z103.string().optional(),
60830
- gid: z103.string().optional()
60919
+ import * as z104 from "zod";
60920
+ var StartSchema = z104.object({
60921
+ options: z104.object({
60922
+ socket: z104.string().optional(),
60923
+ uid: z104.string().optional(),
60924
+ gid: z104.string().optional()
60831
60925
  }),
60832
- served: z103.string()
60926
+ served: z104.string()
60833
60927
  });
60834
- var WarmRunSchema = z103.object({
60835
- mode: z103.enum(["--count-data", "--warm"]),
60836
- memFile: z103.string(),
60837
- pid: z103.int()
60928
+ var WarmRunSchema = z104.object({
60929
+ mode: z104.enum(["--count-data", "--warm"]),
60930
+ memFile: z104.string(),
60931
+ pid: z104.int()
60838
60932
  });
60839
60933
  function createStubUffdHandler(directory, mode = "serve") {
60840
60934
  const path = join50(directory, `uffd-handler-${mode}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@impz/testing",
3
- "version": "7.0.1",
3
+ "version": "7.1.0",
4
4
  "description": "A real impd on stand-ins, for Bun tests of code that drives impd through @impz/client",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -28,7 +28,7 @@
28
28
  "@orpc/server": "^1.16.1",
29
29
  "@peculiar/x509": "^2.2.0",
30
30
  "@zgeoff/kysely-bun-sqlite": "^0.3.2",
31
- "@zgeoff/mcp-auth": "^0.2.1",
31
+ "@zgeoff/mcp-auth": "^0.2.2",
32
32
  "@zgeoff/std": "^0.4.3",
33
33
  "dns-packet": "^5.6.1",
34
34
  "elysia": "^2.0.0-rc.3",
@@ -42,7 +42,7 @@
42
42
  "ts-pattern": "^5.9.2",
43
43
  "xstate": "^5.33.3",
44
44
  "zod": "^4.6.7",
45
- "@impz/client": "7.0.1"
45
+ "@impz/client": "7.1.0"
46
46
  },
47
47
  "engines": {
48
48
  "bun": ">=1.4.3"