@attest-it/cli 0.9.0 → 0.9.1

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.
@@ -1,15 +1,15 @@
1
1
  #!/usr/bin/env node
2
- import { Command } from 'commander';
3
2
  import * as fs from 'fs';
4
- import { readFileSync } from 'fs';
3
+ import { readFileSync, existsSync, realpathSync } from 'fs';
5
4
  import * as path from 'path';
6
5
  import { join, dirname } from 'path';
6
+ import { spawn, spawnSync } from 'child_process';
7
7
  import { fileURLToPath } from 'url';
8
+ import { Command } from 'commander';
8
9
  import { detectTheme } from 'chromaterm';
9
10
  import { input, select, confirm, checkbox } from '@inquirer/prompts';
10
11
  import { loadConfig, toAttestItConfig, readSealsSync, computeFingerprintSync, verifyGateSeal, verifyAllSeals, computeFingerprint, createAttestation, readAttestations, upsertAttestation, KeyProviderRegistry, getDefaultPrivateKeyPath, FilesystemKeyProvider, writeSignedAttestations, loadLocalConfigSync, getActiveIdentity, isAuthorizedSigner, createSeal, writeSealsSync, getGate, loadLocalConfig, OnePasswordKeyProvider, MacOSKeychainKeyProvider, YubiKeyProvider, getAttestItConfigDir, generateEd25519KeyPair, saveLocalConfig, savePublicKey, findConfigPath, loadPreferences, savePreferences, findAttestation, setAttestItHomeDir } from '@attest-it/core';
11
12
  import tabtab2 from '@pnpm/tabtab';
12
- import { spawn } from 'child_process';
13
13
  import * as os from 'os';
14
14
  import { parse } from 'shell-quote';
15
15
  import * as React7 from 'react';
@@ -18,6 +18,68 @@ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
18
18
  import { mkdir, writeFile, unlink, readFile } from 'fs/promises';
19
19
  import { stringify } from 'yaml';
20
20
 
21
+ var __filename$1 = fileURLToPath(import.meta.url);
22
+ function getExecutableExtensions() {
23
+ if (process.platform === "win32") {
24
+ return [".cmd", ".ps1", ""];
25
+ }
26
+ return [""];
27
+ }
28
+ function findLocalCli() {
29
+ let dir = process.cwd();
30
+ const extensions = getExecutableExtensions();
31
+ while (dir !== dirname(dir)) {
32
+ const binDir = join(dir, "node_modules", ".bin");
33
+ for (const ext of extensions) {
34
+ const localBin = join(binDir, `attest-it${ext}`);
35
+ if (existsSync(localBin)) {
36
+ return localBin;
37
+ }
38
+ }
39
+ dir = dirname(dir);
40
+ }
41
+ return null;
42
+ }
43
+ function getPackageRoot(filePath) {
44
+ const normalizedPath = filePath.replace(/\\/g, "/");
45
+ const regex = /(.*node_modules\/@attest-it\/cli)/;
46
+ const match = regex.exec(normalizedPath);
47
+ if (!match?.[1]) {
48
+ return null;
49
+ }
50
+ return filePath.slice(0, match[1].length);
51
+ }
52
+ function isSameAsCurrentCli(localCliPath) {
53
+ try {
54
+ const localReal = realpathSync(localCliPath);
55
+ const currentReal = realpathSync(__filename$1);
56
+ if (localReal === currentReal) {
57
+ return true;
58
+ }
59
+ const localPkgRoot = getPackageRoot(localReal);
60
+ const currentPkgRoot = getPackageRoot(currentReal);
61
+ return localPkgRoot !== null && localPkgRoot === currentPkgRoot;
62
+ } catch (err) {
63
+ console.warn(
64
+ `Warning: Could not resolve CLI paths for comparison: ${err instanceof Error ? err.message : String(err)}`
65
+ );
66
+ return false;
67
+ }
68
+ }
69
+ function tryDelegateToLocal() {
70
+ if (process.env.ATTEST_IT_SKIP_LOCAL_RESOLUTION === "1") {
71
+ return false;
72
+ }
73
+ const localCli = findLocalCli();
74
+ if (!localCli || isSameAsCurrentCli(localCli)) {
75
+ return false;
76
+ }
77
+ const result = spawnSync(localCli, process.argv.slice(2), {
78
+ stdio: "inherit",
79
+ env: { ...process.env, ATTEST_IT_SKIP_LOCAL_RESOLUTION: "1" }
80
+ });
81
+ process.exit(result.status ?? 1);
82
+ }
21
83
  var globalOptions = {};
22
84
  var theme;
23
85
  async function initTheme() {
@@ -259,8 +321,8 @@ function getPackageVersion() {
259
321
  if (cachedVersion !== void 0) {
260
322
  return cachedVersion;
261
323
  }
262
- const __filename = fileURLToPath(import.meta.url);
263
- const __dirname = dirname(__filename);
324
+ const __filename2 = fileURLToPath(import.meta.url);
325
+ const __dirname = dirname(__filename2);
264
326
  const possiblePaths = [join(__dirname, "../package.json"), join(__dirname, "../../package.json")];
265
327
  for (const packageJsonPath of possiblePaths) {
266
328
  try {
@@ -286,8 +348,8 @@ var initCommand = new Command("init").description("Initialize attest-it configur
286
348
  await runInit(options);
287
349
  });
288
350
  function loadConfigTemplate() {
289
- const __filename = fileURLToPath(import.meta.url);
290
- const __dirname = dirname(__filename);
351
+ const __filename2 = fileURLToPath(import.meta.url);
352
+ const __dirname = dirname(__filename2);
291
353
  const possiblePaths = [
292
354
  join(__dirname, "../../templates/config.yaml"),
293
355
  join(__dirname, "../templates/config.yaml")
@@ -2273,10 +2335,19 @@ async function runCreate() {
2273
2335
  default: ""
2274
2336
  });
2275
2337
  info("Checking available key storage providers...");
2338
+ info(
2339
+ "You may see authentication prompts from 1Password, macOS Keychain, or other security tools."
2340
+ );
2276
2341
  const opAvailable = await OnePasswordKeyProvider.isInstalled();
2342
+ verbose(` 1Password CLI (op): ${opAvailable ? "found" : "not found"}`);
2277
2343
  const keychainAvailable = MacOSKeychainKeyProvider.isAvailable();
2344
+ verbose(` macOS Keychain: ${keychainAvailable ? "available" : "not available (not macOS)"}`);
2278
2345
  const yubikeyInstalled = await YubiKeyProvider.isInstalled();
2346
+ verbose(` YubiKey CLI (ykman): ${yubikeyInstalled ? "found" : "not found"}`);
2279
2347
  const yubikeyConnected = yubikeyInstalled ? await YubiKeyProvider.isConnected() : false;
2348
+ if (yubikeyInstalled) {
2349
+ verbose(` YubiKey device: ${yubikeyConnected ? "connected" : "not connected"}`);
2350
+ }
2280
2351
  const configDir = getAttestItConfigDir();
2281
2352
  const storageChoices = [
2282
2353
  { name: `File system (${join(configDir, "keys")})`, value: "file" }
@@ -2290,24 +2361,24 @@ async function runCreate() {
2290
2361
  if (yubikeyInstalled) {
2291
2362
  const yubikeyLabel = yubikeyConnected ? "YubiKey (encrypted with challenge-response)" : "YubiKey (not connected - insert YubiKey first)";
2292
2363
  storageChoices.push({ name: yubikeyLabel, value: "yubikey" });
2364
+ } else {
2365
+ storageChoices.push({
2366
+ name: theme3.muted("YubiKey (install ykman CLI to enable)"),
2367
+ value: "yubikey-disabled",
2368
+ // @ts-expect-error -- @inquirer/prompts supports disabled property but types may not reflect it
2369
+ disabled: true
2370
+ });
2293
2371
  }
2294
2372
  const keyStorageType = await select({
2295
2373
  message: "Where should the private key be stored?",
2296
2374
  choices: storageChoices
2297
2375
  });
2298
- log("");
2299
- log("Generating Ed25519 keypair...");
2300
- const keyPair = generateEd25519KeyPair();
2301
- let privateKeyRef;
2302
- let keyStorageDescription;
2376
+ let storageConfig;
2303
2377
  switch (keyStorageType) {
2304
2378
  case "file": {
2305
2379
  const keysDir = join(getAttestItConfigDir(), "keys");
2306
- await mkdir(keysDir, { recursive: true });
2307
2380
  const keyPath = join(keysDir, `${slug}.pem`);
2308
- await writeFile(keyPath, keyPair.privateKey, { mode: 384 });
2309
- privateKeyRef = { type: "file", path: keyPath };
2310
- keyStorageDescription = keyPath;
2381
+ storageConfig = { type: "file", keyPath };
2311
2382
  break;
2312
2383
  }
2313
2384
  case "keychain": {
@@ -2315,6 +2386,9 @@ async function runCreate() {
2315
2386
  error("macOS Keychain is not available on this system");
2316
2387
  process.exit(ExitCode.CONFIG_ERROR);
2317
2388
  }
2389
+ log("");
2390
+ info("Accessing macOS Keychain to list available keychains...");
2391
+ info("You may be prompted to allow access or enter your password.");
2318
2392
  const keychains = await MacOSKeychainKeyProvider.listKeychains();
2319
2393
  if (keychains.length === 0) {
2320
2394
  throw new Error("No keychains found on this system");
@@ -2350,38 +2424,15 @@ async function runCreate() {
2350
2424
  return true;
2351
2425
  }
2352
2426
  });
2353
- const { execFile } = await import('child_process');
2354
- const { promisify } = await import('util');
2355
- const execFileAsync = promisify(execFile);
2356
- const encodedKey = Buffer.from(keyPair.privateKey).toString("base64");
2357
- try {
2358
- const addArgs = [
2359
- "add-generic-password",
2360
- "-a",
2361
- "attest-it",
2362
- "-s",
2363
- keychainItemName,
2364
- "-w",
2365
- encodedKey,
2366
- "-U",
2367
- selectedKeychain.path
2368
- ];
2369
- await execFileAsync("security", addArgs);
2370
- } catch (err) {
2371
- throw new Error(
2372
- `Failed to store key in macOS Keychain: ${err instanceof Error ? err.message : String(err)}`
2373
- );
2374
- }
2375
- privateKeyRef = {
2376
- type: "keychain",
2377
- service: keychainItemName,
2378
- account: "attest-it",
2379
- keychain: selectedKeychain.path
2380
- };
2381
- keyStorageDescription = `macOS Keychain: ${selectedKeychain.name}/${keychainItemName}`;
2427
+ storageConfig = { type: "keychain", selectedKeychain, keychainItemName };
2382
2428
  break;
2383
2429
  }
2384
2430
  case "1password": {
2431
+ log("");
2432
+ info("Accessing 1Password to list your accounts and vaults...");
2433
+ info(
2434
+ "You may see biometric prompts or be asked to unlock 1Password for each configured account."
2435
+ );
2385
2436
  const accounts = await OnePasswordKeyProvider.listAccounts();
2386
2437
  if (accounts.length === 0) {
2387
2438
  throw new Error(
@@ -2402,7 +2453,7 @@ async function runCreate() {
2402
2453
  "--format=json"
2403
2454
  ]);
2404
2455
  const details = JSON.parse(stdout);
2405
- const name2 = details !== null && typeof details === "object" && "name" in details && typeof details.name === "string" ? details.name : acc.url;
2456
+ const name2 = details !== null && typeof details === "object" && "name" in details && typeof details.name === "string" ? details.name : "[Could not read account name]";
2406
2457
  return {
2407
2458
  url: acc.url,
2408
2459
  email: acc.email,
@@ -2412,7 +2463,7 @@ async function runCreate() {
2412
2463
  return {
2413
2464
  url: acc.url,
2414
2465
  email: acc.email,
2415
- name: acc.url
2466
+ name: "[Could not read account name]"
2416
2467
  };
2417
2468
  }
2418
2469
  })
@@ -2454,43 +2505,21 @@ async function runCreate() {
2454
2505
  return true;
2455
2506
  }
2456
2507
  });
2457
- const { tmpdir } = await import('os');
2458
- const tempDir = join(tmpdir(), `attest-it-${String(Date.now())}`);
2459
- await mkdir(tempDir, { recursive: true });
2460
- const tempPrivatePath = join(tempDir, "private.pem");
2461
- try {
2462
- await writeFile(tempPrivatePath, keyPair.privateKey, { mode: 384 });
2463
- const { execFile: execFile2 } = await import('child_process');
2464
- const { promisify: promisify2 } = await import('util');
2465
- const execFileAsync2 = promisify2(execFile2);
2466
- const opArgs = [
2467
- "document",
2468
- "create",
2469
- tempPrivatePath,
2470
- "--title",
2471
- item,
2472
- "--vault",
2473
- selectedVault
2474
- ];
2475
- if (selectedAccount) {
2476
- opArgs.push("--account", selectedAccount);
2477
- }
2478
- await execFileAsync2("op", opArgs);
2479
- } finally {
2480
- const { rm } = await import('fs/promises');
2481
- await rm(tempDir, { recursive: true, force: true }).catch(() => {
2482
- });
2483
- }
2484
- privateKeyRef = {
2508
+ const selectedAccountDetails = accountDetails.find((acc) => acc.url === selectedAccount);
2509
+ const accountDisplayName = selectedAccountDetails?.name ?? selectedAccount;
2510
+ storageConfig = {
2485
2511
  type: "1password",
2486
- vault: selectedVault,
2487
- item,
2488
- ...selectedAccount && { account: selectedAccount }
2512
+ selectedAccount,
2513
+ accountDisplayName,
2514
+ selectedVault,
2515
+ item
2489
2516
  };
2490
- keyStorageDescription = `1Password (${selectedVault}/${item})`;
2491
2517
  break;
2492
2518
  }
2493
2519
  case "yubikey": {
2520
+ log("");
2521
+ info("Accessing YubiKey to detect connected devices...");
2522
+ info("Your private key will be encrypted using HMAC challenge-response from the YubiKey.");
2494
2523
  if (!await YubiKeyProvider.isConnected()) {
2495
2524
  error("No YubiKey detected. Please insert your YubiKey and try again.");
2496
2525
  process.exit(ExitCode.CONFIG_ERROR);
@@ -2542,25 +2571,115 @@ async function runCreate() {
2542
2571
  }
2543
2572
  });
2544
2573
  const keysDir = join(getAttestItConfigDir(), "keys");
2545
- await mkdir(keysDir, { recursive: true });
2546
2574
  const encryptedKeyPath = join(keysDir, encryptedKeyName);
2575
+ storageConfig = { type: "yubikey", selectedSerial, slot, encryptedKeyPath };
2576
+ break;
2577
+ }
2578
+ default:
2579
+ throw new Error(`Unknown key storage type: ${keyStorageType}`);
2580
+ }
2581
+ log("");
2582
+ log("Generating Ed25519 keypair...");
2583
+ const keyPair = generateEd25519KeyPair();
2584
+ let privateKeyRef;
2585
+ let keyStorageDescription;
2586
+ switch (storageConfig.type) {
2587
+ case "file": {
2588
+ log("");
2589
+ info("Creating private key file on disk...");
2590
+ const keysDir = join(getAttestItConfigDir(), "keys");
2591
+ await mkdir(keysDir, { recursive: true });
2592
+ await writeFile(storageConfig.keyPath, keyPair.privateKey, { mode: 384 });
2593
+ privateKeyRef = { type: "file", path: storageConfig.keyPath };
2594
+ keyStorageDescription = storageConfig.keyPath;
2595
+ break;
2596
+ }
2597
+ case "keychain": {
2598
+ const { execFile } = await import('child_process');
2599
+ const { promisify } = await import('util');
2600
+ const execFileAsync = promisify(execFile);
2601
+ const encodedKey = Buffer.from(keyPair.privateKey).toString("base64");
2602
+ try {
2603
+ const addArgs = [
2604
+ "add-generic-password",
2605
+ "-a",
2606
+ "attest-it",
2607
+ "-s",
2608
+ storageConfig.keychainItemName,
2609
+ "-w",
2610
+ encodedKey,
2611
+ "-U",
2612
+ storageConfig.selectedKeychain.path
2613
+ ];
2614
+ await execFileAsync("security", addArgs);
2615
+ } catch (err) {
2616
+ throw new Error(
2617
+ `Failed to store key in macOS Keychain: ${err instanceof Error ? err.message : String(err)}`
2618
+ );
2619
+ }
2620
+ privateKeyRef = {
2621
+ type: "keychain",
2622
+ service: storageConfig.keychainItemName,
2623
+ account: "attest-it",
2624
+ keychain: storageConfig.selectedKeychain.path
2625
+ };
2626
+ keyStorageDescription = `macOS Keychain: ${storageConfig.selectedKeychain.name}/${storageConfig.keychainItemName}`;
2627
+ break;
2628
+ }
2629
+ case "1password": {
2630
+ const { tmpdir } = await import('os');
2631
+ const tempDir = join(tmpdir(), `attest-it-${String(Date.now())}`);
2632
+ await mkdir(tempDir, { recursive: true });
2633
+ const tempPrivatePath = join(tempDir, "private.pem");
2634
+ try {
2635
+ await writeFile(tempPrivatePath, keyPair.privateKey, { mode: 384 });
2636
+ const { execFile } = await import('child_process');
2637
+ const { promisify } = await import('util');
2638
+ const execFileAsync = promisify(execFile);
2639
+ const opArgs = [
2640
+ "document",
2641
+ "create",
2642
+ tempPrivatePath,
2643
+ "--title",
2644
+ storageConfig.item,
2645
+ "--vault",
2646
+ storageConfig.selectedVault,
2647
+ "--account",
2648
+ storageConfig.selectedAccount
2649
+ ];
2650
+ await execFileAsync("op", opArgs);
2651
+ } finally {
2652
+ const { rm } = await import('fs/promises');
2653
+ await rm(tempDir, { recursive: true, force: true }).catch(() => {
2654
+ });
2655
+ }
2656
+ privateKeyRef = {
2657
+ type: "1password",
2658
+ vault: storageConfig.selectedVault,
2659
+ item: storageConfig.item,
2660
+ account: storageConfig.selectedAccount
2661
+ };
2662
+ keyStorageDescription = `1Password (${storageConfig.accountDisplayName}/${storageConfig.selectedVault}/${storageConfig.item})`;
2663
+ break;
2664
+ }
2665
+ case "yubikey": {
2666
+ const keysDir = join(getAttestItConfigDir(), "keys");
2667
+ await mkdir(keysDir, { recursive: true });
2547
2668
  const result = await YubiKeyProvider.encryptPrivateKey({
2548
2669
  privateKey: keyPair.privateKey,
2549
- encryptedKeyPath,
2550
- slot,
2551
- serial: selectedSerial
2670
+ encryptedKeyPath: storageConfig.encryptedKeyPath,
2671
+ slot: storageConfig.slot,
2672
+ serial: storageConfig.selectedSerial
2552
2673
  });
2553
2674
  privateKeyRef = {
2554
2675
  type: "yubikey",
2555
2676
  encryptedKeyPath: result.encryptedKeyPath,
2556
- slot,
2557
- serial: selectedSerial
2677
+ slot: storageConfig.slot,
2678
+ serial: storageConfig.selectedSerial
2558
2679
  };
2559
2680
  keyStorageDescription = result.storageDescription;
2560
2681
  break;
2561
2682
  }
2562
- default:
2563
- throw new Error(`Unknown key storage type: ${keyStorageType}`);
2564
2683
  }
2565
2684
  const identity = {
2566
2685
  name,
@@ -3696,6 +3815,7 @@ async function run() {
3696
3815
  }
3697
3816
 
3698
3817
  // bin/attest-it.ts
3818
+ tryDelegateToLocal();
3699
3819
  void run();
3700
3820
  //# sourceMappingURL=attest-it.js.map
3701
3821
  //# sourceMappingURL=attest-it.js.map