@agledger/cli 1.4.1 → 2.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.
@@ -1,3 +1,4 @@
1
+ import { ApiClient } from '../api-client.js';
1
2
  import { BaseCommand } from '../base.js';
2
3
  /**
3
4
  * Cold-start discovery for agents and humans. Mirrors the MCP server's
@@ -15,8 +16,11 @@ export default class Discover extends BaseCommand {
15
16
  // "Call this first" has to be true without a key: /health answers
16
17
  // unauthenticated, and /v1/auth/me simply reports no identity.
17
18
  const client = this.createApiClient(flags, { allowAnonymous: true });
19
+ // /health goes without a credential, so a token source or IdP that is down
20
+ // does not also hide whether the Server is up.
21
+ const anonymous = new ApiClient(client.baseUrl, null, this.config.version);
18
22
  const [health, identity] = await Promise.allSettled([
19
- client.request('GET', '/health'),
23
+ anonymous.request('GET', '/health'),
20
24
  client.request('GET', '/v1/auth/me'),
21
25
  ]);
22
26
  const result = {};
@@ -14,6 +14,7 @@ export default class Docs extends BaseCommand {
14
14
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
15
15
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
16
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
17
18
  full: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
19
  };
19
20
  run(): Promise<void>;
@@ -26,7 +26,13 @@ export default class Docs extends BaseCommand {
26
26
  // able to read the narrative before it has a key.
27
27
  const client = this.createApiClient(flags, { allowAnonymous: true });
28
28
  const path = flags.full ? '/llms-full.txt' : '/llms.txt';
29
- const res = await client.request('GET', path);
29
+ let res;
30
+ try {
31
+ res = await client.request('GET', path);
32
+ }
33
+ catch (err) {
34
+ this.handleError(err);
35
+ }
30
36
  const text = res.body && typeof res.body === 'object' && '_raw' in res.body
31
37
  ? String(res.body._raw)
32
38
  : typeof res.body === 'string'
@@ -11,6 +11,7 @@ export default class HelpJson extends BaseCommand {
11
11
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
12
12
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
13
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
14
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
14
15
  };
15
16
  run(): Promise<void>;
16
17
  }
@@ -15,6 +15,7 @@ export default class ListCommands extends BaseCommand {
15
15
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
16
16
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
17
17
  profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
18
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
19
  };
19
20
  run(): Promise<void>;
20
21
  }
@@ -1,17 +1,30 @@
1
1
  import { BaseCommand } from '../base.js';
2
2
  /**
3
- * Verify the provided key against the API, then persist it to ~/.agledger/config.json
4
- * (0600, dir 0700). Supports named profiles so one machine can hold keys for multiple
5
- * instances/environments.
3
+ * Verify a credential against the API, then persist it to
4
+ * ~/.agledger/config.json (0600, dir 0700). Supports named profiles so one
5
+ * machine can hold credentials for several Servers.
6
+ *
7
+ * `--oidc` stores an OIDC token SOURCE (a command or a file path), never a
8
+ * token, cert or key. Every later command runs the source again and exchanges
9
+ * the token for a fresh cert held in memory for that one invocation.
6
10
  */
7
11
  export default class Login extends BaseCommand {
8
12
  static description: string;
13
+ static examples: string[];
14
+ protected readonly createsProfile = true;
9
15
  static flags: {
10
16
  json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
17
  quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
12
18
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
13
19
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
20
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
14
21
  profile: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
22
+ oidc: import("@oclif/core/interfaces").BooleanFlag<boolean>;
23
+ 'oidc-token-cmd': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
24
+ 'oidc-token-file': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
25
+ 'oidc-agent-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
15
26
  };
16
27
  run(): Promise<void>;
28
+ private loginOidc;
29
+ private save;
17
30
  }
@@ -1,36 +1,147 @@
1
+ import { resolve } from 'node:path';
1
2
  import { Flags } from '@oclif/core';
2
- import { BaseCommand, ErrorCode, ExitCode } from '../base.js';
3
+ import { ApiClient } from '../api-client.js';
4
+ import { BaseCommand, ErrorCode, ExitCode, argvHasFlag } from '../base.js';
5
+ import { OIDC_ENV, fetchOidcToken } from '../oidc.js';
3
6
  import { readConfig, writeConfig } from '../util/config.js';
4
7
  /**
5
- * Verify the provided key against the API, then persist it to ~/.agledger/config.json
6
- * (0600, dir 0700). Supports named profiles so one machine can hold keys for multiple
7
- * instances/environments.
8
+ * Verify a credential against the API, then persist it to
9
+ * ~/.agledger/config.json (0600, dir 0700). Supports named profiles so one
10
+ * machine can hold credentials for several Servers.
11
+ *
12
+ * `--oidc` stores an OIDC token SOURCE (a command or a file path), never a
13
+ * token, cert or key. Every later command runs the source again and exchanges
14
+ * the token for a fresh cert held in memory for that one invocation.
8
15
  */
9
16
  export default class Login extends BaseCommand {
10
- static description = 'Verify an API key and store it in ~/.agledger/config.json (0600)';
17
+ static description = 'Verify an API key, or an OIDC token source with --oidc, and store it in ~/.agledger/config.json (0600)';
18
+ static examples = [
19
+ '<%= config.bin %> login --api-url https://agledger.internal --api-key agl_agt_... --profile prod',
20
+ "<%= config.bin %> login --oidc --api-url https://agledger.internal --oidc-token-cmd 'gcloud auth print-identity-token --audiences=agledger'",
21
+ '<%= config.bin %> login --oidc --api-url https://agledger.internal --oidc-token-file /var/run/secrets/tokens/agledger',
22
+ ];
23
+ // --profile names the profile this login writes, so it need not exist yet.
24
+ createsProfile = true;
11
25
  static flags = {
12
26
  ...BaseCommand.baseFlags,
13
27
  profile: Flags.string({ description: 'Profile name', default: 'default' }),
28
+ oidc: Flags.boolean({
29
+ description: 'Store an OIDC token source instead of an API key. A token command is verified by one cert exchange and a GET /v1/auth/me before it is saved. A token file is only checked to hold a JWT: the Server exchanges each token once, so exchanging it here would leave the next command a spent token until the file rotates.',
30
+ default: false,
31
+ }),
32
+ 'oidc-token-cmd': Flags.string({
33
+ description: 'With --oidc: a shell command whose stdout is an OIDC JWT, run on every cert exchange.',
34
+ env: OIDC_ENV.TOKEN_CMD,
35
+ }),
36
+ 'oidc-token-file': Flags.string({
37
+ description: 'With --oidc: a file holding an OIDC JWT, read on every cert exchange (a projected token in a pod).',
38
+ env: OIDC_ENV.TOKEN_FILE,
39
+ }),
40
+ 'oidc-agent-id': Flags.string({
41
+ description: 'With --oidc: an optional assertion of the agent id the token binds to. The token decides the agent, never this ' +
42
+ 'value: when it names a different agent, or the token binds none, the exchange fails 403 ' +
43
+ 'CERT_AGENT_BINDING_MISMATCH instead of issuing a cert. To bind an agent, an administrator sets oidcIss/oidcSub ' +
44
+ 'on it (PATCH /v1/agents/{id}) or maps an agent_id claim on the trusted issuer (claimMapping.agent_id).',
45
+ env: OIDC_ENV.AGENT_ID,
46
+ }),
14
47
  };
15
48
  async run() {
16
49
  const { flags } = await this.parse(Login);
50
+ if (flags.oidc) {
51
+ await this.loginOidc(flags);
52
+ return;
53
+ }
17
54
  const apiKey = flags['api-key'];
18
55
  if (!apiKey) {
19
- this.failWith(ErrorCode.AUTH_REQUIRED, 'Provide --api-key or set AGLEDGER_API_KEY.', ExitCode.AUTH_ERROR);
56
+ this.failWith(ErrorCode.AUTH_REQUIRED, 'Provide --api-key or set AGLEDGER_API_KEY.', ExitCode.AUTH_ERROR, 'To store an OIDC token source instead of a key, run `agledger login --oidc`.');
20
57
  }
21
58
  try {
22
59
  const response = await this.callApi(flags, 'GET', '/v1/auth/me');
23
60
  if (!response.ok) {
24
61
  this.handleApiError(response);
25
62
  }
26
- const config = readConfig();
27
- config.profiles[flags.profile] = { apiKey: apiKey, apiUrl: flags['api-url'] || undefined };
28
- config.activeProfile = flags.profile;
29
- writeConfig(config);
63
+ this.save(flags.profile, { apiKey: apiKey, apiUrl: flags['api-url'] || undefined });
30
64
  this.output({ authenticated: true, profile: flags.profile, account: response.body });
31
65
  }
32
66
  catch (err) {
33
67
  this.handleError(err);
34
68
  }
35
69
  }
70
+ async loginOidc(flags) {
71
+ if (flags['api-key']) {
72
+ this.failWith(ErrorCode.MISSING_INPUT, '--oidc stores an OIDC token source, but an API key was also supplied.', ExitCode.USAGE_ERROR, 'Drop --api-key and unset AGLEDGER_API_KEY, or run `agledger login` without --oidc to store the key.');
73
+ }
74
+ const command = flags['oidc-token-cmd'] || undefined;
75
+ // Stored absolute: later commands run from other directories.
76
+ const file = flags['oidc-token-file'] ? resolve(flags['oidc-token-file']) : undefined;
77
+ if (!command && !file) {
78
+ this.failWith(ErrorCode.AUTH_REQUIRED, '--oidc needs a token source.', ExitCode.AUTH_ERROR, 'Pass --oidc-token-cmd <command> (or set AGLEDGER_OIDC_TOKEN_CMD), or --oidc-token-file <path> (or set AGLEDGER_OIDC_TOKEN_FILE).');
79
+ }
80
+ const apiUrl = flags['api-url'] || undefined;
81
+ if (!apiUrl) {
82
+ this.failWith(ErrorCode.CONFIG_ERROR, 'No API URL configured. AGLedger is self-hosted, so there is no default server to call.', ExitCode.USAGE_ERROR, 'Pass --api-url <url> or set AGLEDGER_API_URL.');
83
+ }
84
+ this.requireUsableApiUrl(apiUrl, argvHasFlag('--api-url') ? '--api-url' : 'AGLEDGER_API_URL');
85
+ // The command outranks the file, as it does at call time.
86
+ const source = command
87
+ ? { kind: 'command', command, origin: argvHasFlag('--oidc-token-cmd') ? '--oidc-token-cmd' : OIDC_ENV.TOKEN_CMD }
88
+ : { kind: 'file', path: file, origin: argvHasFlag('--oidc-token-file') ? '--oidc-token-file' : OIDC_ENV.TOKEN_FILE };
89
+ const agentId = flags['oidc-agent-id'] || undefined;
90
+ const stored = {
91
+ apiUrl,
92
+ oidc: {
93
+ ...(command ? { tokenCommand: command } : { tokenFile: file }),
94
+ ...(agentId ? { agentId } : {}),
95
+ },
96
+ };
97
+ if (source.kind === 'file') {
98
+ // No exchange: the file returns the same token until it rotates, and the
99
+ // Server exchanges a token id once, so a check here would spend the token
100
+ // the next command needs. Confirm it holds a JWT and save.
101
+ try {
102
+ const { sub } = await fetchOidcToken(source);
103
+ this.save(flags.profile, stored);
104
+ this.output({
105
+ saved: true,
106
+ verified: false,
107
+ profile: flags.profile,
108
+ credential: 'oidc-cert',
109
+ tokenSource: 'file',
110
+ oidcSub: sub,
111
+ message: 'The file holds a JWT; it was not exchanged, so the token stays unspent. Run `agledger auth` to exchange it and see the cert.',
112
+ });
113
+ }
114
+ catch (err) {
115
+ this.handleError(err);
116
+ }
117
+ return;
118
+ }
119
+ const credential = this.oidcCredential(flags, { source, ...(agentId ? { agentId } : {}) });
120
+ this.verboseLog(flags, { event: 'auth', credential: 'oidc-cert', source: source.origin, apiUrl });
121
+ try {
122
+ const client = new ApiClient(apiUrl, credential, this.config.version);
123
+ const response = await client.request('GET', '/v1/auth/me');
124
+ if (!response.ok) {
125
+ this.handleApiError(response);
126
+ }
127
+ this.save(flags.profile, stored);
128
+ this.output({
129
+ authenticated: true,
130
+ profile: flags.profile,
131
+ credential: 'oidc-cert',
132
+ tokenSource: 'command',
133
+ cert: credential.cert,
134
+ account: response.body,
135
+ });
136
+ }
137
+ catch (err) {
138
+ this.handleError(err);
139
+ }
140
+ }
141
+ save(name, profile) {
142
+ const config = readConfig();
143
+ config.profiles[name] = profile;
144
+ config.activeProfile = name;
145
+ writeConfig(config);
146
+ }
36
147
  }
@@ -1,12 +1,14 @@
1
1
  import { BaseCommand } from '../base.js';
2
2
  export default class Logout extends BaseCommand {
3
3
  static description: string;
4
+ static examples: string[];
4
5
  static flags: {
5
6
  json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
6
7
  quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
7
8
  'api-key': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
8
9
  'api-url': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
9
- profile: import("@oclif/core/interfaces").OptionFlag<string, import("@oclif/core/interfaces").CustomOptions>;
10
+ verbose: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
+ profile: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
10
12
  all: import("@oclif/core/interfaces").BooleanFlag<boolean>;
11
13
  };
12
14
  run(): Promise<void>;
@@ -1,11 +1,16 @@
1
1
  import { Flags } from '@oclif/core';
2
- import { BaseCommand } from '../base.js';
2
+ import { BaseCommand, ErrorCode, ExitCode } from '../base.js';
3
3
  import { readConfig, writeConfig } from '../util/config.js';
4
4
  export default class Logout extends BaseCommand {
5
- static description = 'Remove a stored profile from ~/.agledger/config.json';
5
+ static description = 'Remove a stored profile from ~/.agledger/config.json: the active profile, the one named by --profile, or every profile with --all. Exits 2 when there is no such profile to remove. Removing the active profile leaves none active: pick another with `config use`.';
6
+ static examples = [
7
+ '<%= config.bin %> logout',
8
+ '<%= config.bin %> logout --profile prod',
9
+ '<%= config.bin %> logout --all',
10
+ ];
6
11
  static flags = {
7
12
  ...BaseCommand.baseFlags,
8
- profile: Flags.string({ description: 'Profile name to remove', default: 'default' }),
13
+ profile: Flags.string({ description: 'Profile name to remove (default: the active profile)' }),
9
14
  all: Flags.boolean({ description: 'Remove all profiles', default: false }),
10
15
  };
11
16
  async run() {
@@ -13,20 +18,39 @@ export default class Logout extends BaseCommand {
13
18
  const config = readConfig();
14
19
  if (flags.all) {
15
20
  const removed = Object.keys(config.profiles);
21
+ if (removed.length === 0) {
22
+ this.failWith(ErrorCode.MISSING_INPUT, 'No profiles are stored, so nothing was removed.', ExitCode.USAGE_ERROR, 'Run `agledger login` to store one.');
23
+ }
16
24
  writeConfig({ profiles: {} });
17
25
  this.output({ loggedOut: true, removedProfiles: removed });
18
26
  return;
19
27
  }
20
- if (!config.profiles[flags.profile]) {
21
- this.output({ loggedOut: false, message: `No profile '${flags.profile}' found.` });
22
- return;
28
+ // The profile a plain command authenticates with is the active one, so
29
+ // that is the one a plain logout removes. Removing nothing is an error,
30
+ // not a quiet success: the caller would otherwise stay logged in.
31
+ const name = flags.profile ?? config.activeProfile;
32
+ if (!name) {
33
+ this.failWith(ErrorCode.MISSING_INPUT, 'No active profile, so there is nothing to log out of.', ExitCode.USAGE_ERROR, 'Run `agledger config list` to see profiles, then `agledger logout --profile <name>`.');
23
34
  }
24
- delete config.profiles[flags.profile];
25
- if (config.activeProfile === flags.profile) {
26
- const remaining = Object.keys(config.profiles);
27
- config.activeProfile = remaining[0];
35
+ if (!config.profiles[name]) {
36
+ this.failWith(ErrorCode.MISSING_INPUT, `Profile '${name}' not found, so nothing was removed.`, ExitCode.USAGE_ERROR, 'Run `agledger config list` to see profiles.');
28
37
  }
38
+ delete config.profiles[name];
39
+ // Logging out the active profile leaves none active. Promoting whichever
40
+ // profile came first would quietly switch the next call to a different
41
+ // identity (an admin key, say), so the caller picks one with `config use`.
42
+ const wasActive = config.activeProfile === name;
43
+ if (wasActive)
44
+ delete config.activeProfile;
29
45
  writeConfig(config);
30
- this.output({ loggedOut: true, profile: flags.profile, activeProfile: config.activeProfile });
46
+ const remaining = Object.keys(config.profiles);
47
+ this.output({
48
+ loggedOut: true,
49
+ profile: name,
50
+ activeProfile: config.activeProfile ?? null,
51
+ ...(wasActive && remaining.length > 0
52
+ ? { note: `No profile is active now. Run \`agledger config use <name>\` to choose one of: ${remaining.join(', ')}.` }
53
+ : {}),
54
+ });
31
55
  }
32
56
  }
@@ -1,4 +1,13 @@
1
+ import { type VerifyExportResult } from '@agledger/verify-core';
1
2
  import { BaseCommand } from '../base.js';
3
+ /** One prose string from a result, its option names given as this command's flags. */
4
+ export declare function flagWording(text: string): string;
5
+ /**
6
+ * A result with its prose worded by {@link flagWording}: `brokenAt.detail`,
7
+ * each entry's detail, and the key-trust detail and each finding, note and
8
+ * accounted-for detail. Ids, digests and instants are data and pass through.
9
+ */
10
+ export declare function resultWithFlags(result: VerifyExportResult): VerifyExportResult;
2
11
  /**
3
12
  * Offline verification of a record audit export (format 2.0, COSE_Sign1).
4
13
  * Runs entirely offline: no network calls, no API key required. The
@@ -9,15 +18,21 @@ import { BaseCommand } from '../base.js';
9
18
  export default class Verify extends BaseCommand {
10
19
  static description: string;
11
20
  static examples: string[];
21
+ static usage: string;
12
22
  static args: {
13
- file: import("@oclif/core/interfaces").Arg<string, Record<string, unknown>>;
23
+ file: import("@oclif/core/interfaces").Arg<string | undefined, Record<string, unknown>>;
14
24
  };
15
25
  static flags: {
16
26
  json: import("@oclif/core/interfaces").BooleanFlag<boolean>;
17
27
  quiet: import("@oclif/core/interfaces").BooleanFlag<boolean>;
18
28
  keys: import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
29
+ 'trust-anchor': import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
30
+ 'distrusted-key': import("@oclif/core/interfaces").OptionFlag<string[] | undefined, import("@oclif/core/interfaces").CustomOptions>;
31
+ 'agent-keys': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
19
32
  'require-key-id': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
33
+ 'distrusted-keys': import("@oclif/core/interfaces").OptionFlag<string | undefined, import("@oclif/core/interfaces").CustomOptions>;
20
34
  'require-out-of-band-keys': import("@oclif/core/interfaces").BooleanFlag<boolean>;
35
+ 'require-supplied-keys': import("@oclif/core/interfaces").BooleanFlag<boolean>;
21
36
  };
22
37
  run(): Promise<void>;
23
38
  /**
@@ -29,5 +44,11 @@ export default class Verify extends BaseCommand {
29
44
  * untouched; verify-core then validates the shape and throws on anything else.
30
45
  */
31
46
  private unwrapKeys;
47
+ /**
48
+ * A single JWK, a list of JWKs, or a `{keys: [...]}` JWK Set, as a list. An
49
+ * entry that wraps its key as `{ publicKeyJwk: {...} }` (how an agent records
50
+ * the key it sent at cert exchange) is unwrapped. verify-core validates each.
51
+ */
52
+ private unwrapAgentKeys;
32
53
  private renderHuman;
33
54
  }