@airctrl/cli 0.0.0-stage → 0.4.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/README.md +2080 -2
- package/dist/cli-error.js +29 -0
- package/dist/cli.js +502 -0
- package/dist/config.js +102 -0
- package/dist/deadline.js +13 -0
- package/dist/gateway-commands.js +487 -0
- package/dist/group-commands.js +92 -0
- package/dist/identity-rotation.js +550 -0
- package/dist/index.js +21 -0
- package/dist/migrate.js +232 -0
- package/dist/parser.js +120 -0
- package/dist/pat-expired.js +31 -0
- package/dist/project-commands.js +121 -0
- package/dist/prompt.js +45 -0
- package/dist/record-commands.js +330 -0
- package/dist/resolve-client.js +160 -0
- package/dist/resolve-status-principal.js +69 -0
- package/dist/sa-commands.js +1485 -0
- package/dist/service-account-read-commands.js +38 -0
- package/dist/store/atomic-write.js +344 -0
- package/dist/store/base-url.js +71 -0
- package/dist/store/canonical.js +45 -0
- package/dist/store/durable-delete.js +86 -0
- package/dist/store/errors.js +25 -0
- package/dist/store/fsops.js +94 -0
- package/dist/store/handoff-store.js +183 -0
- package/dist/store/identity-rotation-journal.js +396 -0
- package/dist/store/index.js +20 -0
- package/dist/store/lock.js +294 -0
- package/dist/store/names.js +52 -0
- package/dist/store/paths.js +219 -0
- package/dist/store/profile-store.js +262 -0
- package/dist/store/receipt-store.js +75 -0
- package/dist/store/resolve-principal.js +125 -0
- package/dist/store/rotation-store.js +66 -0
- package/dist/store/schema.js +554 -0
- package/package.json +42 -4
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { parseArgv, recordEnvironmentFlag, ParseError } from './parser.js';
|
|
2
|
+
import { resolveClient, resolveHumanClient } from './resolve-client.js';
|
|
3
|
+
const profile = ['profile'];
|
|
4
|
+
export const RECORD_SPECS = {
|
|
5
|
+
list: {
|
|
6
|
+
positionals: [],
|
|
7
|
+
flags: ['account', 'project', 'tab', 'include-archived', 'limit', 'offset', ...profile],
|
|
8
|
+
},
|
|
9
|
+
versions: { positionals: ['recordId'], flags: profile },
|
|
10
|
+
audit: { positionals: ['recordId'], flags: ['limit', ...profile] },
|
|
11
|
+
'audit-events': {
|
|
12
|
+
positionals: ['recordId'],
|
|
13
|
+
flags: ['limit', 'offset', 'cursor', 'action', 'actor', 'from', 'to', ...profile],
|
|
14
|
+
},
|
|
15
|
+
anomalies: { positionals: [], flags: ['account', 'scope-type', 'scope-id', ...profile] },
|
|
16
|
+
activity: {
|
|
17
|
+
positionals: ['day'],
|
|
18
|
+
flags: ['account', 'scope-type', 'scope-id', ...profile],
|
|
19
|
+
},
|
|
20
|
+
alerts: {
|
|
21
|
+
positionals: [],
|
|
22
|
+
flags: ['account', 'scope-type', 'scope-id', 'cursor', 'limit', 'offset', ...profile],
|
|
23
|
+
},
|
|
24
|
+
access: {
|
|
25
|
+
positionals: [],
|
|
26
|
+
flags: ['account', 'scope-type', 'scope-id', 'cursor', 'limit', 'offset', 'actions', ...profile],
|
|
27
|
+
},
|
|
28
|
+
metadata: {
|
|
29
|
+
positionals: ['recordId'],
|
|
30
|
+
flags: ['name', 'tags', 'secret-due', 'rotation-days', 'env', ...profile],
|
|
31
|
+
},
|
|
32
|
+
archive: { positionals: ['recordId'], flags: profile },
|
|
33
|
+
restore: { positionals: ['recordId'], flags: profile },
|
|
34
|
+
transfer: {
|
|
35
|
+
positionals: ['recordId'],
|
|
36
|
+
flags: ['owner', ...profile],
|
|
37
|
+
requiredFlags: ['owner'],
|
|
38
|
+
},
|
|
39
|
+
rollback: {
|
|
40
|
+
positionals: ['recordId'],
|
|
41
|
+
flags: ['version', ...profile],
|
|
42
|
+
requiredFlags: ['version'],
|
|
43
|
+
},
|
|
44
|
+
set: {
|
|
45
|
+
positionals: ['recordId'],
|
|
46
|
+
flags: ['idempotency-key', 'env', ...profile],
|
|
47
|
+
repeatable: ['field'],
|
|
48
|
+
keyValue: ['field'],
|
|
49
|
+
},
|
|
50
|
+
rotate: { positionals: ['recordId'], flags: profile },
|
|
51
|
+
'share-user': {
|
|
52
|
+
positionals: ['recordId'],
|
|
53
|
+
flags: ['user', 'permission', ...profile],
|
|
54
|
+
requiredFlags: ['user'],
|
|
55
|
+
},
|
|
56
|
+
'share-sa': {
|
|
57
|
+
positionals: ['recordId'],
|
|
58
|
+
flags: ['account', 'service-account', ...profile],
|
|
59
|
+
requiredFlags: ['account', 'service-account'],
|
|
60
|
+
},
|
|
61
|
+
'share-group': {
|
|
62
|
+
positionals: ['recordId'],
|
|
63
|
+
flags: ['group', ...profile],
|
|
64
|
+
requiredFlags: ['group'],
|
|
65
|
+
},
|
|
66
|
+
'revoke-user': {
|
|
67
|
+
positionals: ['recordId'],
|
|
68
|
+
flags: ['user', ...profile],
|
|
69
|
+
requiredFlags: ['user'],
|
|
70
|
+
},
|
|
71
|
+
'revoke-sa': {
|
|
72
|
+
positionals: ['recordId'],
|
|
73
|
+
flags: ['service-account', ...profile],
|
|
74
|
+
requiredFlags: ['service-account'],
|
|
75
|
+
},
|
|
76
|
+
'revoke-group': {
|
|
77
|
+
positionals: ['recordId'],
|
|
78
|
+
flags: ['group', ...profile],
|
|
79
|
+
requiredFlags: ['group'],
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
/** Record subcommands whose API operations explicitly accept a Service Account token. */
|
|
83
|
+
export const SERVICE_ACCOUNT_RECORD_COMMANDS = ['list'];
|
|
84
|
+
export const RECORD_USAGE = `airctrl record commands
|
|
85
|
+
|
|
86
|
+
list [--project <id>] [--tab owned|shared] [--include-archived true|false]
|
|
87
|
+
versions <recordId> | audit <recordId> [--limit <n>]
|
|
88
|
+
audit-events <recordId> [--cursor <opaque>] [--action <name>] [--actor <text>] [--from <iso>] [--to <iso>]
|
|
89
|
+
anomalies | activity <YYYY-MM-DD> | alerts | access [--scope-type user|project]
|
|
90
|
+
metadata <recordId> [--name <n>] [--tags a,b|none] [--secret-due <iso>|null] [--env development|staging|production]
|
|
91
|
+
archive <recordId> | restore <recordId>
|
|
92
|
+
transfer <recordId> --owner <userId>
|
|
93
|
+
rollback <recordId> --version <versionId>
|
|
94
|
+
set <recordId> --field <key=value> [--field <key=value> ...] [--env development|staging|production]
|
|
95
|
+
rotate <recordId>
|
|
96
|
+
share-user <recordId> --user <id> [--permission view|read]
|
|
97
|
+
share-sa <recordId> --account <id> --service-account <id>
|
|
98
|
+
share-group <recordId> --group <id>
|
|
99
|
+
revoke-user|revoke-sa|revoke-group <recordId> --user|--service-account|--group <id>
|
|
100
|
+
`;
|
|
101
|
+
const INTEGER_RE = /^\d+$/;
|
|
102
|
+
function integerFlag(value, minimum = 0) {
|
|
103
|
+
if (value === undefined)
|
|
104
|
+
return undefined;
|
|
105
|
+
// nosemgrep: nodejs_scan.javascript-dos-rule-regex_dos -- CLI flag input is bounded and INTEGER_RE is anchored without nested repetition.
|
|
106
|
+
if (!INTEGER_RE.test(value))
|
|
107
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
108
|
+
const parsed = Number(value);
|
|
109
|
+
if (!Number.isSafeInteger(parsed) || parsed < minimum)
|
|
110
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
111
|
+
return parsed;
|
|
112
|
+
}
|
|
113
|
+
function booleanFlag(value) {
|
|
114
|
+
if (value === undefined)
|
|
115
|
+
return undefined;
|
|
116
|
+
if (value === 'true')
|
|
117
|
+
return true;
|
|
118
|
+
if (value === 'false')
|
|
119
|
+
return false;
|
|
120
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
121
|
+
}
|
|
122
|
+
function tabFlag(value) {
|
|
123
|
+
if (value === undefined || value === 'owned' || value === 'shared')
|
|
124
|
+
return value;
|
|
125
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
126
|
+
}
|
|
127
|
+
function tagsFlag(value) {
|
|
128
|
+
if (value === undefined)
|
|
129
|
+
return undefined;
|
|
130
|
+
if (value === 'none')
|
|
131
|
+
return [];
|
|
132
|
+
const tags = value.split(',').map((tag) => tag.trim()).filter(Boolean);
|
|
133
|
+
if (tags.length === 0)
|
|
134
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
135
|
+
return tags;
|
|
136
|
+
}
|
|
137
|
+
function auditScope(parsed) {
|
|
138
|
+
const scopeType = parsed.flags['scope-type'];
|
|
139
|
+
if (scopeType !== undefined && scopeType !== 'user' && scopeType !== 'project') {
|
|
140
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
141
|
+
}
|
|
142
|
+
const scopeId = parsed.flags['scope-id'];
|
|
143
|
+
if (scopeType === 'project' && !scopeId)
|
|
144
|
+
throw new ParseError('cli_flag_missing_value');
|
|
145
|
+
return { scopeType: scopeType, scopeId, accountId: parsed.flags.account };
|
|
146
|
+
}
|
|
147
|
+
function nullable(value) {
|
|
148
|
+
return value === 'null' ? null : value;
|
|
149
|
+
}
|
|
150
|
+
function writeResult(value, deps) {
|
|
151
|
+
const out = deps.stdout ?? ((text) => process.stdout.write(text));
|
|
152
|
+
out(`${JSON.stringify(value, null, 2)}\n`);
|
|
153
|
+
}
|
|
154
|
+
export async function runRecordCommand(argv, deps) {
|
|
155
|
+
const [command, ...rest] = argv;
|
|
156
|
+
if (command === undefined || command === 'help' || command === '--help') {
|
|
157
|
+
;
|
|
158
|
+
(deps.stdout ?? ((text) => process.stdout.write(text)))(RECORD_USAGE);
|
|
159
|
+
return 0;
|
|
160
|
+
}
|
|
161
|
+
if (!Object.hasOwn(RECORD_SPECS, command))
|
|
162
|
+
throw new ParseError('cli_unknown_command');
|
|
163
|
+
const parsed = parseArgv(rest, RECORD_SPECS[command]);
|
|
164
|
+
const environment = recordEnvironmentFlag(parsed.flags.env);
|
|
165
|
+
const supportsServiceAccount = SERVICE_ACCOUNT_RECORD_COMMANDS.includes(command);
|
|
166
|
+
const resolved = supportsServiceAccount
|
|
167
|
+
? resolveClient({ ...deps.resolve, profileFlag: parsed.flags.profile })
|
|
168
|
+
: resolveHumanClient({ ...deps.resolve, profileFlag: parsed.flags.profile });
|
|
169
|
+
const client = resolved.client;
|
|
170
|
+
const recordId = parsed.positionals[0];
|
|
171
|
+
let result;
|
|
172
|
+
switch (command) {
|
|
173
|
+
case 'list': {
|
|
174
|
+
const includeArchived = booleanFlag(parsed.flags['include-archived']);
|
|
175
|
+
const limit = integerFlag(parsed.flags.limit, 1);
|
|
176
|
+
const offset = integerFlag(parsed.flags.offset);
|
|
177
|
+
if (parsed.flags.project) {
|
|
178
|
+
result = await client.listProjectRecords(parsed.flags.project, { includeArchived, limit, offset });
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
result = await client.listRecords({
|
|
182
|
+
accountId: parsed.flags.account,
|
|
183
|
+
tab: tabFlag(parsed.flags.tab),
|
|
184
|
+
includeArchived,
|
|
185
|
+
limit,
|
|
186
|
+
offset,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
case 'versions':
|
|
192
|
+
result = await client.getRecordVersions(recordId);
|
|
193
|
+
break;
|
|
194
|
+
case 'audit':
|
|
195
|
+
result = await client.getRecordAudit(recordId, integerFlag(parsed.flags.limit, 1));
|
|
196
|
+
break;
|
|
197
|
+
case 'audit-events':
|
|
198
|
+
result = await client.listRecordAudit(recordId, {
|
|
199
|
+
limit: integerFlag(parsed.flags.limit, 1),
|
|
200
|
+
offset: integerFlag(parsed.flags.offset),
|
|
201
|
+
cursor: parsed.flags.cursor,
|
|
202
|
+
action: parsed.flags.action,
|
|
203
|
+
actor: parsed.flags.actor,
|
|
204
|
+
from: parsed.flags.from,
|
|
205
|
+
to: parsed.flags.to,
|
|
206
|
+
});
|
|
207
|
+
break;
|
|
208
|
+
case 'anomalies':
|
|
209
|
+
result = await client.listRecordAnomalies(auditScope(parsed));
|
|
210
|
+
break;
|
|
211
|
+
case 'activity':
|
|
212
|
+
result = await client.listRecordActivity(recordId, auditScope(parsed));
|
|
213
|
+
break;
|
|
214
|
+
case 'alerts':
|
|
215
|
+
result = await client.listRecordAlerts({
|
|
216
|
+
...auditScope(parsed),
|
|
217
|
+
cursor: parsed.flags.cursor,
|
|
218
|
+
limit: integerFlag(parsed.flags.limit, 1),
|
|
219
|
+
offset: integerFlag(parsed.flags.offset),
|
|
220
|
+
});
|
|
221
|
+
break;
|
|
222
|
+
case 'access':
|
|
223
|
+
result = await client.listRecordAccess({
|
|
224
|
+
...auditScope(parsed),
|
|
225
|
+
cursor: parsed.flags.cursor,
|
|
226
|
+
limit: integerFlag(parsed.flags.limit, 1),
|
|
227
|
+
offset: integerFlag(parsed.flags.offset),
|
|
228
|
+
actions: tagsFlag(parsed.flags.actions),
|
|
229
|
+
});
|
|
230
|
+
break;
|
|
231
|
+
case 'metadata': {
|
|
232
|
+
const patch = {};
|
|
233
|
+
if (environment)
|
|
234
|
+
patch.environment = environment;
|
|
235
|
+
if (parsed.flags.name !== undefined)
|
|
236
|
+
patch.name = parsed.flags.name;
|
|
237
|
+
if (parsed.flags.tags !== undefined)
|
|
238
|
+
patch.tags = tagsFlag(parsed.flags.tags);
|
|
239
|
+
if (parsed.flags['secret-due'] !== undefined)
|
|
240
|
+
patch.secretDueAt = nullable(parsed.flags['secret-due']);
|
|
241
|
+
if (parsed.flags['rotation-days'] !== undefined) {
|
|
242
|
+
patch.rotationIntervalDays = parsed.flags['rotation-days'] === 'null'
|
|
243
|
+
? null
|
|
244
|
+
: integerFlag(parsed.flags['rotation-days'], 1);
|
|
245
|
+
}
|
|
246
|
+
if (Object.keys(patch).length === (environment ? 1 : 0))
|
|
247
|
+
throw new ParseError('cli_flag_missing_value');
|
|
248
|
+
result = await client.updateRecordMetadata(recordId, patch);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case 'archive':
|
|
252
|
+
result = await client.setRecordArchiveState(recordId, true);
|
|
253
|
+
break;
|
|
254
|
+
case 'restore':
|
|
255
|
+
result = await client.setRecordArchiveState(recordId, false);
|
|
256
|
+
break;
|
|
257
|
+
case 'transfer':
|
|
258
|
+
result = await client.transferRecordOwnership(recordId, parsed.flags.owner);
|
|
259
|
+
break;
|
|
260
|
+
case 'rollback':
|
|
261
|
+
result = await client.rollbackRecordToVersion(recordId, parsed.flags.version, await deps.resolveVaultPassword());
|
|
262
|
+
break;
|
|
263
|
+
case 'set': {
|
|
264
|
+
const fields = {};
|
|
265
|
+
for (const pair of parsed.repeated.field ?? []) {
|
|
266
|
+
const separator = pair.indexOf('=');
|
|
267
|
+
fields[pair.slice(0, separator)] = pair.slice(separator + 1);
|
|
268
|
+
}
|
|
269
|
+
if (Object.keys(fields).length === 0)
|
|
270
|
+
throw new ParseError('cli_flag_missing_value');
|
|
271
|
+
const isServiceAccount = 'principalKind' in resolved && resolved.principalKind === 'sa';
|
|
272
|
+
result = await client.setSecret({
|
|
273
|
+
recordId,
|
|
274
|
+
fieldValues: fields,
|
|
275
|
+
...(environment ? { environment } : {}),
|
|
276
|
+
changedFields: Object.keys(fields),
|
|
277
|
+
idempotencyKey: parsed.flags['idempotency-key'],
|
|
278
|
+
vaultPassword: isServiceAccount ? undefined : await deps.resolveVaultPassword(),
|
|
279
|
+
});
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
case 'rotate':
|
|
283
|
+
result = await client.rotateRecord({
|
|
284
|
+
recordId,
|
|
285
|
+
vaultPassword: await deps.resolveVaultPassword(),
|
|
286
|
+
});
|
|
287
|
+
break;
|
|
288
|
+
case 'share-user': {
|
|
289
|
+
const permission = parsed.flags.permission;
|
|
290
|
+
if (permission !== undefined && permission !== 'view' && permission !== 'read') {
|
|
291
|
+
throw new ParseError('cli_flag_invalid_value');
|
|
292
|
+
}
|
|
293
|
+
result = await client.shareRecordWithUser({
|
|
294
|
+
recordId,
|
|
295
|
+
granteeUserId: parsed.flags.user,
|
|
296
|
+
permission,
|
|
297
|
+
vaultPassword: await deps.resolveVaultPassword(),
|
|
298
|
+
});
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case 'share-sa':
|
|
302
|
+
result = await client.shareRecordWithServiceAccount({
|
|
303
|
+
accountId: parsed.flags.account,
|
|
304
|
+
recordId,
|
|
305
|
+
serviceAccountId: parsed.flags['service-account'],
|
|
306
|
+
vaultPassword: await deps.resolveVaultPassword(),
|
|
307
|
+
});
|
|
308
|
+
break;
|
|
309
|
+
case 'share-group':
|
|
310
|
+
result = await client.shareRecordWithGroup({
|
|
311
|
+
recordId,
|
|
312
|
+
groupId: parsed.flags.group,
|
|
313
|
+
vaultPassword: await deps.resolveVaultPassword(),
|
|
314
|
+
});
|
|
315
|
+
break;
|
|
316
|
+
case 'revoke-user':
|
|
317
|
+
result = await client.revokeRecordFromUser(recordId, parsed.flags.user);
|
|
318
|
+
break;
|
|
319
|
+
case 'revoke-sa':
|
|
320
|
+
result = await client.revokeRecordFromServiceAccount(recordId, parsed.flags['service-account']);
|
|
321
|
+
break;
|
|
322
|
+
case 'revoke-group':
|
|
323
|
+
result = await client.revokeRecordFromGroup(recordId, parsed.flags.group);
|
|
324
|
+
break;
|
|
325
|
+
default:
|
|
326
|
+
throw new ParseError('cli_unknown_command');
|
|
327
|
+
}
|
|
328
|
+
writeResult(result, deps);
|
|
329
|
+
return 0;
|
|
330
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* U9b-3b · resolve-client (§10.c#15.C/D). Runs `resolvePrincipal` EXACTLY once, builds an `AirctrlClient`
|
|
3
|
+
* with `createClient` (never the deprecated `createServiceClient`), and classifies human / SA-env-active /
|
|
4
|
+
* SA-env-legacy / SA-profile by flow+stage — never re-reading bearer/config/profile and never promoting a
|
|
5
|
+
* stage. Read-eligibility failures throw stable-coded {@link CliError}/{@link ReconcileError}; the clock is
|
|
6
|
+
* injectable for deterministic deadline tests.
|
|
7
|
+
*/
|
|
8
|
+
import { createClient } from '@airctrl/sdk';
|
|
9
|
+
import { resolvePrincipal } from './store/index.js';
|
|
10
|
+
import { CliError, ReconcileError } from './cli-error.js';
|
|
11
|
+
import { saDeadlineExpired } from './deadline.js';
|
|
12
|
+
const s = (p, key) => {
|
|
13
|
+
const v = p[key];
|
|
14
|
+
if (typeof v !== 'string' || v.length === 0)
|
|
15
|
+
throw new CliError('sa_profile_not_active'); // corrupt/incomplete → fail closed
|
|
16
|
+
return v;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* SSOT sealed-client constructor for ANY VALIDATED profile that carries `bearer` + `credentialId` + `capPrivateKeyB64`
|
|
20
|
+
* (post-gate F4): `auth` = the profile bearer, `credential` = `{credentialId, capPrivateKeyB64}`. This covers STANDARD/
|
|
21
|
+
* LEGACY `active` (the read path / rotate holder) AND — for `sa migrate` — a LEGACY `activation_prepared` profile once
|
|
22
|
+
* the remote `status` confirms the credential is `active` (§10.c#20.1.i · `ensureSession('sealed')`): the sealed unlock
|
|
23
|
+
* is decided by the remote state, not by the local stage being `active`. Shared by `saFromProfile` (read path),
|
|
24
|
+
* `resolveHolderProfile` (`sa rotate` holder, U9b-3e-4 · D3) and the migrate FSM so the `{auth, credentialId,
|
|
25
|
+
* capPrivateKeyB64}` mapping is never duplicated. `s()` fails closed (`sa_profile_not_active`) on a missing/corrupt field.
|
|
26
|
+
*/
|
|
27
|
+
export function buildSealedClient(profile, baseUrl, make) {
|
|
28
|
+
return make({
|
|
29
|
+
baseUrl,
|
|
30
|
+
auth: s(profile, 'bearer'),
|
|
31
|
+
credential: { credentialId: s(profile, 'credentialId'), capPrivateKeyB64: s(profile, 'capPrivateKeyB64') },
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** Phase 1 (SSOT): resolve the principal EXACTLY once from the injected/real resolver. Shared by both paths. */
|
|
35
|
+
function resolvePrincipalOnce(deps) {
|
|
36
|
+
const resolve = deps.resolvePrincipalFn ?? resolvePrincipal;
|
|
37
|
+
return resolve({
|
|
38
|
+
env: deps.env,
|
|
39
|
+
profileFlag: deps.profileFlag,
|
|
40
|
+
loadHumanConfig: deps.loadHumanConfig,
|
|
41
|
+
loadProfileByName: deps.loadProfileByName,
|
|
42
|
+
profileRoot: deps.profileRoot,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Human-write path (create/set/new/import): resolve the principal once and admit ONLY a human. `none` →
|
|
47
|
+
* `cli_not_authenticated`; ANY service-account principal (env-active, env-legacy, or a profile at ANY stage)
|
|
48
|
+
* → `sa_command_requires_human` — the veto lives here so no command re-reads bearer/config or rebuilds a
|
|
49
|
+
* client (§10.c#15.F6). Never prompts, never mutates.
|
|
50
|
+
*/
|
|
51
|
+
export function resolveHumanClient(deps = {}) {
|
|
52
|
+
const make = deps.makeClient ?? createClient;
|
|
53
|
+
const p = resolvePrincipalOnce(deps);
|
|
54
|
+
if (p.kind === 'none')
|
|
55
|
+
throw new CliError('cli_not_authenticated');
|
|
56
|
+
if (p.kind !== 'human')
|
|
57
|
+
throw new CliError('sa_command_requires_human');
|
|
58
|
+
return { client: make({ baseUrl: p.baseUrl, auth: p.bearer }), baseUrl: p.baseUrl, defaultProject: p.defaultProject };
|
|
59
|
+
}
|
|
60
|
+
/** Read path: resolve the principal once and return a ready client for human OR SA, or throw a stable code. */
|
|
61
|
+
export function resolveClient(deps = {}) {
|
|
62
|
+
const make = deps.makeClient ?? createClient;
|
|
63
|
+
const now = deps.now ?? Date.now;
|
|
64
|
+
const p = resolvePrincipalOnce(deps);
|
|
65
|
+
switch (p.kind) {
|
|
66
|
+
case 'none':
|
|
67
|
+
throw new CliError('cli_not_authenticated');
|
|
68
|
+
case 'human':
|
|
69
|
+
return {
|
|
70
|
+
principalKind: 'human',
|
|
71
|
+
client: make({ baseUrl: p.baseUrl, auth: p.bearer }),
|
|
72
|
+
baseUrl: p.baseUrl,
|
|
73
|
+
needsVaultPassword: true,
|
|
74
|
+
defaultProject: p.defaultProject,
|
|
75
|
+
};
|
|
76
|
+
case 'sa_env_active':
|
|
77
|
+
return {
|
|
78
|
+
principalKind: 'sa',
|
|
79
|
+
saCredentialMode: 'sealed',
|
|
80
|
+
client: make({
|
|
81
|
+
baseUrl: p.baseUrl,
|
|
82
|
+
auth: p.bearer,
|
|
83
|
+
credential: { credentialId: p.credentialId, capPrivateKeyB64: p.capPrivateKeyB64 },
|
|
84
|
+
}),
|
|
85
|
+
baseUrl: p.baseUrl,
|
|
86
|
+
needsVaultPassword: false,
|
|
87
|
+
};
|
|
88
|
+
case 'sa_env_legacy':
|
|
89
|
+
return {
|
|
90
|
+
principalKind: 'sa',
|
|
91
|
+
saCredentialMode: 'legacy',
|
|
92
|
+
client: make({ baseUrl: p.baseUrl, auth: p.bearer }),
|
|
93
|
+
baseUrl: p.baseUrl,
|
|
94
|
+
needsVaultPassword: false,
|
|
95
|
+
};
|
|
96
|
+
case 'sa_profile':
|
|
97
|
+
return saFromProfile(p.profile, p.baseUrl, make, now);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/** Map a validated SA profile to a client by (flow, stage), enforcing legacy deadline; never promotes stage. */
|
|
101
|
+
function saFromProfile(profile, baseUrl, make, now) {
|
|
102
|
+
const flow = profile.flow;
|
|
103
|
+
const stage = profile.stage;
|
|
104
|
+
const sealed = () => ({
|
|
105
|
+
principalKind: 'sa',
|
|
106
|
+
saCredentialMode: 'sealed',
|
|
107
|
+
client: buildSealedClient(profile, baseUrl, make),
|
|
108
|
+
baseUrl,
|
|
109
|
+
needsVaultPassword: false,
|
|
110
|
+
});
|
|
111
|
+
const legacy = () => ({
|
|
112
|
+
principalKind: 'sa',
|
|
113
|
+
saCredentialMode: 'legacy',
|
|
114
|
+
client: make({ baseUrl, auth: s(profile, 'bearer') }),
|
|
115
|
+
baseUrl,
|
|
116
|
+
needsVaultPassword: false,
|
|
117
|
+
});
|
|
118
|
+
const assertNotExpired = () => {
|
|
119
|
+
const expiresAt = profile.expiresAt;
|
|
120
|
+
if (typeof expiresAt === 'string' && saDeadlineExpired(expiresAt, now())) {
|
|
121
|
+
throw new ReconcileError('deadline_elapsed');
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
if (flow === 'standard') {
|
|
125
|
+
if (stage === 'active')
|
|
126
|
+
return sealed();
|
|
127
|
+
throw new CliError('sa_profile_not_active'); // capability_prepared/claimed/activation_prepared/terminal
|
|
128
|
+
}
|
|
129
|
+
// flow === 'legacy_migration'
|
|
130
|
+
switch (stage) {
|
|
131
|
+
case 'legacy_source':
|
|
132
|
+
case 'capability_prepared':
|
|
133
|
+
return legacy(); // no expiresAt on these stages → no deadline
|
|
134
|
+
case 'migration_prepared':
|
|
135
|
+
case 'activation_prepared':
|
|
136
|
+
assertNotExpired();
|
|
137
|
+
return legacy();
|
|
138
|
+
case 'active':
|
|
139
|
+
return sealed();
|
|
140
|
+
default: // terminal (and any unexpected)
|
|
141
|
+
throw new CliError('sa_profile_not_active');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export function resolveHolderProfile(deps = {}) {
|
|
145
|
+
const make = deps.makeClient ?? createClient;
|
|
146
|
+
const p = resolvePrincipalOnce(deps);
|
|
147
|
+
if (p.kind !== 'sa_profile')
|
|
148
|
+
throw new CliError('sa_holder_requires_profile');
|
|
149
|
+
const profile = p.profile;
|
|
150
|
+
if (profile.flow !== 'standard' || profile.stage !== 'active')
|
|
151
|
+
throw new CliError('sa_holder_not_active');
|
|
152
|
+
return {
|
|
153
|
+
profileName: p.profileName,
|
|
154
|
+
serviceAccountId: profile.serviceAccountId,
|
|
155
|
+
credentialId: s(profile, 'credentialId'),
|
|
156
|
+
patId: s(profile, 'patId'),
|
|
157
|
+
client: buildSealedClient(profile, p.baseUrl, make),
|
|
158
|
+
baseUrl: p.baseUrl,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* U9b-3e-2 · status-only principal resolution (§10.c#18.7). Runs `resolvePrincipal` EXACTLY once and maps the
|
|
3
|
+
* admitted principals to the minimal tuple `{ bearer, serviceAccountId, credentialId }` a status query needs. It
|
|
4
|
+
* builds an `AirctrlClient` with ONLY `baseUrl + auth` (NEVER a `credential`) and returns it typed as the minimal
|
|
5
|
+
* {@link StatusClient} surface, so — by TypeScript surface + command wiring + tests — `sa status` can only call
|
|
6
|
+
* `saGetCredentialStatus`. It does NOT relax or reuse `resolveClient` (which rejects the pending stages status
|
|
7
|
+
* admits), never reloads profile/env/config, never unlocks, and never mutates. Non-admitted principals throw a
|
|
8
|
+
* stable code; none of the status-specific codes asserts the bearer is "live" — revoked/locked/expired are the
|
|
9
|
+
* server's decision, surfaced by the remote request.
|
|
10
|
+
*/
|
|
11
|
+
import { createClient } from '@airctrl/sdk';
|
|
12
|
+
import { resolvePrincipal } from './store/index.js';
|
|
13
|
+
import { CliError } from './cli-error.js';
|
|
14
|
+
/** Read a schema-validated string field off a profile; a missing/empty value is corrupt → fail closed. */
|
|
15
|
+
const field = (p, key) => {
|
|
16
|
+
const v = p[key];
|
|
17
|
+
if (typeof v !== 'string' || v.length === 0)
|
|
18
|
+
throw new CliError('sa_profile_not_active');
|
|
19
|
+
return v;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the status principal EXACTLY once. Admits `sa_env_active` WITH a companion `serviceAccountId`, STANDARD
|
|
23
|
+
* `claimed`/`activation_prepared`/`active`, and LEGACY `active`. Everything else throws its exact stable code.
|
|
24
|
+
*/
|
|
25
|
+
export function resolveStatusPrincipal(deps = {}) {
|
|
26
|
+
const make = deps.makeClient ?? createClient;
|
|
27
|
+
const resolve = deps.resolvePrincipalFn ?? resolvePrincipal;
|
|
28
|
+
const p = resolve({
|
|
29
|
+
env: deps.env,
|
|
30
|
+
profileFlag: deps.profileFlag,
|
|
31
|
+
loadHumanConfig: deps.loadHumanConfig,
|
|
32
|
+
loadProfileByName: deps.loadProfileByName,
|
|
33
|
+
profileRoot: deps.profileRoot,
|
|
34
|
+
});
|
|
35
|
+
// baseUrl + auth ONLY — never a `credential`. The runtime object is still a full `AirctrlClient`; the guarantee
|
|
36
|
+
// that `sa status` reaches nothing but `saGetCredentialStatus` is the minimal `StatusClient` return type + command
|
|
37
|
+
// wiring + tests (NOT runtime impossibility). Without a credential the sealed/unlock ops are not usable correctly
|
|
38
|
+
// anyway, so exposing them by type would only widen the surface for no benefit.
|
|
39
|
+
const build = (bearer, baseUrl, serviceAccountId, credentialId) => ({
|
|
40
|
+
principalKind: 'sa',
|
|
41
|
+
client: make({ baseUrl, auth: bearer }),
|
|
42
|
+
serviceAccountId,
|
|
43
|
+
credentialId,
|
|
44
|
+
});
|
|
45
|
+
switch (p.kind) {
|
|
46
|
+
case 'none':
|
|
47
|
+
throw new CliError('cli_not_authenticated');
|
|
48
|
+
case 'human':
|
|
49
|
+
case 'sa_env_legacy':
|
|
50
|
+
// Not a status-capable SA credential (missing the local `{ bearer, serviceAccountId, credentialId }` tuple).
|
|
51
|
+
throw new CliError('sa_status_requires_credential');
|
|
52
|
+
case 'sa_env_active':
|
|
53
|
+
if (p.serviceAccountId === undefined)
|
|
54
|
+
throw new CliError('sa_status_service_account_id_required'); // companion absent → before fetch
|
|
55
|
+
return build(p.bearer, p.baseUrl, p.serviceAccountId, p.credentialId);
|
|
56
|
+
case 'sa_profile': {
|
|
57
|
+
const { profile, baseUrl } = p;
|
|
58
|
+
// §18.7: STANDARD claimed/activation_prepared/active carry a live pending/active bearer + credentialId; LEGACY
|
|
59
|
+
// admits ONLY `active` (its pre-migration bearer is a legacy bearer, not a credential bound by pat_id). Any
|
|
60
|
+
// other stage keeps the EXISTING `sa_profile_not_active` (never remapped to a status-specific code).
|
|
61
|
+
const admitted = profile.flow === 'standard'
|
|
62
|
+
? profile.stage === 'claimed' || profile.stage === 'activation_prepared' || profile.stage === 'active'
|
|
63
|
+
: profile.flow === 'legacy_migration' && profile.stage === 'active';
|
|
64
|
+
if (!admitted)
|
|
65
|
+
throw new CliError('sa_profile_not_active');
|
|
66
|
+
return build(field(profile, 'bearer'), baseUrl, field(profile, 'serviceAccountId'), field(profile, 'credentialId'));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|