typevault 0.1.0 → 0.3.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 (76) hide show
  1. package/README.md +156 -115
  2. package/package.json +14 -2
  3. package/src/access/env.ts +77 -17
  4. package/src/access/keys.ts +14 -14
  5. package/src/access/schemas.ts +6 -12
  6. package/src/access/scope.ts +14 -9
  7. package/src/access/scoped.ts +118 -44
  8. package/src/access/sealed-access.ts +50 -23
  9. package/src/access/sealed.ts +56 -31
  10. package/src/access/types.ts +27 -30
  11. package/src/admin.ts +68 -0
  12. package/src/audit/log.ts +28 -12
  13. package/src/cli/commands/admin.ts +93 -48
  14. package/src/cli/commands/daemon.ts +42 -17
  15. package/src/cli/commands/entries.ts +195 -60
  16. package/src/cli/commands/grant.ts +24 -22
  17. package/src/cli/commands/members.ts +307 -0
  18. package/src/cli/commands/run.ts +191 -44
  19. package/src/cli/context.ts +163 -15
  20. package/src/cli/index.ts +33 -5
  21. package/src/cli/password.ts +74 -0
  22. package/src/cli/web-prompt.ts +79 -0
  23. package/src/core/admin.ts +144 -76
  24. package/src/core/adopt.ts +40 -9
  25. package/src/core/crypto.ts +6 -4
  26. package/src/core/doctor.ts +15 -16
  27. package/src/core/entries.ts +66 -52
  28. package/src/core/errors.ts +8 -2
  29. package/src/core/fsx.ts +7 -3
  30. package/src/core/holder.ts +87 -0
  31. package/src/core/identity.ts +132 -0
  32. package/src/core/init.ts +73 -23
  33. package/src/core/json-file.ts +20 -0
  34. package/src/core/keyring.ts +100 -56
  35. package/src/core/layout.ts +143 -26
  36. package/src/core/lock.ts +128 -16
  37. package/src/core/naming.ts +150 -50
  38. package/src/core/password.ts +111 -0
  39. package/src/core/payload.ts +42 -38
  40. package/src/core/revisions.ts +11 -6
  41. package/src/core/rotate.ts +59 -42
  42. package/src/core/state.ts +102 -54
  43. package/src/core/txn.ts +9 -7
  44. package/src/core/vault.ts +108 -46
  45. package/src/core/worktrees.ts +51 -0
  46. package/src/daemon/admin-routes.ts +33 -21
  47. package/src/daemon/agent-routes.ts +31 -11
  48. package/src/daemon/client.ts +24 -2
  49. package/src/daemon/daemon.ts +218 -33
  50. package/src/daemon/grants.ts +25 -7
  51. package/src/daemon/http.ts +11 -2
  52. package/src/daemon/live-access.ts +8 -4
  53. package/src/daemon/protocol.ts +17 -12
  54. package/src/daemon/requests.ts +75 -51
  55. package/src/daemon/spawn.ts +218 -9
  56. package/src/export/1password.ts +157 -0
  57. package/src/import/1password-live.ts +399 -0
  58. package/src/import/1password.ts +26 -5
  59. package/src/import/bitwarden.ts +3 -3
  60. package/src/import/csv.ts +1 -1
  61. package/src/import/dotenv.ts +4 -3
  62. package/src/import/index.ts +1 -0
  63. package/src/import/keepass.ts +2 -2
  64. package/src/import/lastpass.ts +2 -2
  65. package/src/import/shared.ts +67 -10
  66. package/src/import/typeclaw.ts +2 -2
  67. package/src/import/types.ts +1 -1
  68. package/src/index.ts +58 -0
  69. package/src/owner.ts +156 -0
  70. package/src/prompt/browser.ts +48 -15
  71. package/src/prompt/index.ts +121 -64
  72. package/src/prompt/page.ts +27 -11
  73. package/src/prompt/styles.ts +64 -34
  74. package/src/prompt/types.ts +7 -4
  75. package/src/redact/index.ts +48 -12
  76. package/src/redact/scrub-file.ts +15 -7
@@ -1,93 +1,158 @@
1
1
  import { defineCommand } from 'citty'
2
2
  import { z } from 'zod'
3
3
 
4
+ import type { Access } from '../../access/types'
4
5
  import { TypeVaultError } from '../../core/errors'
5
6
  import {
6
- type EntryType,
7
- fieldsOf,
8
- isSecretField,
7
+ ENTRY_NAME_SYNTAX,
8
+ ENTRY_REF_SYNTAX,
9
+ defaultField,
10
+ defaultFieldOf,
11
+ fieldValue,
12
+ resolveField,
13
+ type Field,
14
+ type FieldType,
15
+ PRESETS,
16
+ isFieldName,
17
+ parsePreset,
9
18
  parseEntryName,
10
19
  parseEntryRef,
11
- parseEntryType,
12
- primaryField,
13
20
  } from '../../core/naming'
21
+ import { FieldSchema } from '../../core/payload'
14
22
  import { parseTotpSeed, totpCode, totpRemainingSeconds } from '../../otp/totp'
15
23
  import { accessOf, resolveAccess } from '../context'
16
24
  import { csv, note, print, readStdin, repeated, table } from '../output'
25
+ import { openBrowser, promptFields } from '../web-prompt'
26
+
27
+ /** Runs after the listing it annotates. A daemon from before an upgrade lacks the route, and its listing is still good. */
28
+ async function noteUnreadableEntries(access: Access): Promise<void> {
29
+ let unreadable: number
30
+ try {
31
+ unreadable = await access.unreadableEntries()
32
+ } catch (error) {
33
+ if (error instanceof TypeVaultError && error.code === 'not_found') {
34
+ return note(
35
+ 'this daemon is too old to report entry files it skips: restart it (typevault daemon stop) to see them',
36
+ )
37
+ }
38
+ throw error
39
+ }
40
+ if (unreadable > 0)
41
+ note(`${unreadable} entry file(s) could not be trusted or read and are not listed: run typevault doctor`)
42
+ }
17
43
 
18
44
  export const ls = defineCommand({
19
45
  meta: { name: 'ls', description: 'List the entries your grant reaches' },
20
46
  args: { json: { type: 'boolean', description: 'Print JSON' } },
21
47
  async run({ args }) {
22
- const entries = await (await accessOf(process.env)).list()
48
+ const access = await accessOf(process.env)
49
+ const entries = await access.list()
50
+ await noteUnreadableEntries(access)
23
51
  if (args.json) return print(JSON.stringify(entries, null, 2))
24
52
  const rows = entries.map((e) => [
25
53
  e.name,
26
- e.type,
27
- e.group,
54
+ e.tier,
28
55
  `r${e.rev}`,
29
56
  e.rotated.slice(0, 10),
30
57
  e.needsRotation ? 'needs-rotation' : '',
31
58
  ])
32
59
  print(
33
- table([
34
- ['NAME', 'TYPE', 'GROUP', 'REV', 'ROTATED', ''],
35
- ...rows.sort((a, b) => (a[0] ?? '').localeCompare(b[0] ?? '')),
36
- ]),
60
+ table([['NAME', 'TIER', 'REV', 'ROTATED', ''], ...rows.sort((a, b) => (a[0] ?? '').localeCompare(b[0] ?? ''))]),
37
61
  )
38
62
  },
39
63
  })
40
64
 
41
65
  export const get = defineCommand({
42
- meta: { name: 'get', description: 'Print a secret (it lands in your transcript: prefer `typevault run`)' },
66
+ meta: {
67
+ name: 'get',
68
+ description: 'Print a field. Secret fields need --reveal (they land in your transcript: prefer `typevault run`)',
69
+ },
43
70
  args: {
44
- ref: { type: 'positional', description: '<service>[/<account>][#<field>]', required: true },
71
+ ref: { type: 'positional', description: ENTRY_REF_SYNTAX, required: true },
45
72
  reason: { type: 'string', description: 'Why you need it (audited)' },
46
- json: { type: 'boolean', description: 'Print every field as JSON' },
73
+ json: { type: 'boolean', description: 'Print every field as JSON (secret fields redacted unless --reveal)' },
74
+ reveal: { type: 'boolean', description: 'Print secret fields in plaintext' },
47
75
  },
48
76
  async run({ args }) {
49
77
  const ref = parseEntryRef(args.ref)
50
78
  const value = await (await accessOf(process.env)).get(ref.name, args.reason)
51
- if (args.json) return print(JSON.stringify(value.fields, null, 2))
52
- const field = ref.field ?? primaryField(value.type)
53
- const secret = value.fields[field]
54
- if (secret === undefined) throw new TypeVaultError('not_found', `${value.name} has no field "${field}"`)
55
- print(secret)
79
+ if (args.json) {
80
+ const fields = Object.fromEntries(
81
+ Object.entries(value.fields).map(([field, secret]) => [
82
+ field,
83
+ { ...secret, value: args.reveal || secret.type === 'text' ? secret.value : '<redacted>' },
84
+ ]),
85
+ )
86
+ if (!args.reveal) note('secret fields are redacted; pass --reveal to print them')
87
+ return print(JSON.stringify(fields, null, 2))
88
+ }
89
+ const wanted = ref.field ?? defaultField(value.fields, value.name)
90
+ const secret = resolveField(value.fields, wanted)
91
+ if (secret === undefined) {
92
+ throw new TypeVaultError('not_found', `${value.name} has no field "${wanted}"`)
93
+ }
94
+ if (!args.reveal && secret.type !== 'text') {
95
+ throw new TypeVaultError(
96
+ 'denied',
97
+ `${value.name}#${wanted} is secret. Use \`typevault run\` to pass it to a command, ` +
98
+ 'or --reveal to print it (it lands in logs and transcripts)',
99
+ )
100
+ }
101
+ print(fieldValue(secret))
56
102
  },
57
103
  })
58
104
 
59
- function parseFields(type: EntryType, pairs: readonly string[]): Record<string, string> {
60
- const allowed = fieldsOf(type)
105
+ /** `--field` values land in `ps` and shell history, so a field the preset or entry types as anything but text is refused. */
106
+ function parseFields(
107
+ pairs: readonly string[],
108
+ known: Readonly<Record<string, FieldType | undefined>>,
109
+ ): Record<string, Field> {
61
110
  return Object.fromEntries(
62
111
  pairs.map((pair) => {
63
112
  const eq = pair.indexOf('=')
64
113
  const field = eq === -1 ? '' : pair.slice(0, eq)
65
- if (!allowed.includes(field))
66
- throw new TypeVaultError('invalid', `${type} entries have fields: ${allowed.join(', ')}`)
67
- if (isSecretField(field)) {
68
- throw new TypeVaultError('invalid', `pass ${field} on stdin (--stdin or --stdin-json), not as an argument`)
114
+ if (!isFieldName(field)) throw new TypeVaultError('invalid', `--field expects name=value, got "${field}"`)
115
+ if ((known[field] ?? 'text') !== 'text') {
116
+ throw new TypeVaultError('invalid', `pass "${field}" on stdin (--stdin or --stdin-json), not as an argument`)
69
117
  }
70
- return [field, pair.slice(eq + 1)]
118
+ return [field, { type: 'text', value: pair.slice(eq + 1) }]
71
119
  }),
72
120
  )
73
121
  }
74
122
 
75
- function parseFieldsJson(type: EntryType, text: string): Record<string, string> {
76
- const fields = z.record(z.string(), z.string()).parse(JSON.parse(text))
77
- const allowed = fieldsOf(type)
78
- const stray = Object.keys(fields).find((field) => !allowed.includes(field))
79
- if (stray !== undefined) throw new TypeVaultError('invalid', `${type} entries have fields: ${allowed.join(', ')}`)
80
- return fields
123
+ function parseFieldsJson(text: string): Record<string, string | Field> {
124
+ let value: unknown
125
+ try {
126
+ value = JSON.parse(text)
127
+ } catch (error) {
128
+ if (error instanceof SyntaxError) throw new TypeVaultError('invalid', 'invalid fields JSON')
129
+ throw error
130
+ }
131
+ const fields = z.record(z.string().refine(isFieldName), z.union([z.string(), FieldSchema])).safeParse(value)
132
+ if (!fields.success) throw new TypeVaultError('invalid', 'invalid fields JSON')
133
+ return fields.data
81
134
  }
82
135
 
83
136
  export const set = defineCommand({
84
- meta: { name: 'set', description: 'Create or update an entry. Pipe the secret on stdin with --stdin.' },
137
+ meta: {
138
+ name: 'set',
139
+ description:
140
+ 'Create or update an entry. Pipe the secret on stdin with --stdin, or type it in a browser with --web.',
141
+ },
85
142
  args: {
86
- name: { type: 'positional', description: '<service>[/<account>]', required: true },
87
- type: { type: 'string', description: 'password | api_key | oauth | ssh | totp | note' },
88
- group: { type: 'string', description: "Group (defaults to your grant's group)" },
89
- field: { type: 'string', description: 'k=v, repeatable (non-secret fields; use --stdin for the secret)' },
143
+ name: { type: 'positional', description: ENTRY_NAME_SYNTAX, required: true },
144
+ preset: { type: 'string', description: 'password | api_key | oauth | ssh | totp | note' },
145
+ 'stdin-json': { type: 'boolean', description: 'Read a JSON object of strings or typed fields from stdin' },
146
+ tier: {
147
+ type: 'string',
148
+ description: "Tier (defaults to $TYPEVAULT_TIER, else your grant's tier, else the only tier you reach)",
149
+ },
150
+ field: { type: 'string', description: 'k=v, repeatable (writes text fields; use stdin for secrets)' },
90
151
  stdin: { type: 'boolean', description: 'Read the main secret field from stdin' },
152
+ web: {
153
+ type: 'boolean',
154
+ description: 'Type the fields in a local one-time browser page (default for a new entry from a terminal)',
155
+ },
91
156
  env: { type: 'string', description: 'Env var name for the main field in `run`' },
92
157
  origin: { type: 'string', description: 'Site this login is for (a hint, never a block)' },
93
158
  tag: { type: 'string', description: 'Comma-separated tags' },
@@ -98,47 +163,106 @@ export const set = defineCommand({
98
163
  const access = await accessOf(process.env)
99
164
  const name = parseEntryName(args.name)
100
165
  const existing = (await access.list()).find((entry) => entry.name === name)
101
- const type = args.type === undefined ? existing?.type : parseEntryType(args.type)
102
- if (type === undefined) throw new TypeVaultError('invalid', 'new entries need --type')
103
- const given = parseFields(type, repeated(rawArgs, '--field'))
104
- const fromStdin = args['stdin-json']
105
- ? parseFieldsJson(type, await readStdin())
106
- : args.stdin
107
- ? { [primaryField(type)]: await readStdin() }
108
- : {}
109
- const fields = { ...given, ...fromStdin }
110
- const base = existing !== undefined && !args.replace ? (await access.get(name, 'merge fields for set')).fields : {}
111
- const ifRev = args['if-rev'] === undefined ? undefined : z.coerce.number().int().parse(args['if-rev'])
166
+ const preset = args.preset === undefined ? undefined : PRESETS[parsePreset(args.preset)]
167
+ // --replace never reads the old fields: the read would refresh OAuth credentials that are being replaced.
168
+ const previous =
169
+ existing === undefined || args.replace ? {} : (await access.get(name, 'merge fields for set')).fields
170
+ const suggested: Readonly<Record<string, FieldType>> = preset?.fields ?? {}
171
+ const known = Object.fromEntries(Object.entries(previous).map(([field, value]) => [field, value.type]))
172
+ const given = parseFields(repeated(rawArgs, '--field'), { ...known, ...suggested })
173
+ const tier = args.tier ?? existing?.tier ?? process.env['TYPEVAULT_TIER']
174
+ const target = preset?.input ?? defaultFieldOf(previous) ?? '$default'
175
+ const stdin = args.stdin || args['stdin-json'] ? await readStdin() : undefined
176
+ if (args.stdin && stdin === '') throw new TypeVaultError('invalid', '--stdin did not contain a value')
177
+ const fromStdin = args['stdin-json'] ? parseFieldsJson(stdin ?? '') : args.stdin ? { [target]: stdin ?? '' } : {}
178
+ const typed: Record<string, Field> = {}
179
+ for (const [field, value] of Object.entries(fromStdin)) {
180
+ if (typeof value !== 'string') {
181
+ typed[field] = value
182
+ continue
183
+ }
184
+ const type = suggested[field] ?? previous[field]?.type ?? 'secret'
185
+ switch (type) {
186
+ case 'oauth': {
187
+ if (!args.stdin || args['stdin-json'])
188
+ throw new TypeVaultError('invalid', 'OAuth values need --stdin-json with a typed oauth field object')
189
+ try {
190
+ typed[field] = FieldSchema.parse({ type, value: JSON.parse(value) })
191
+ } catch (error) {
192
+ if (error instanceof SyntaxError || error instanceof z.ZodError)
193
+ throw new TypeVaultError('invalid', 'OAuth stdin needs a JSON object with an access_token')
194
+ throw error
195
+ }
196
+ break
197
+ }
198
+ case 'secret':
199
+ case 'text':
200
+ case 'totp':
201
+ typed[field] = { type, value }
202
+ break
203
+ default:
204
+ throw new TypeVaultError('invalid', `unexpected field type ${type satisfies never}`)
205
+ }
206
+ }
207
+ const web = args.web ?? (existing === undefined && !args.stdin && !args['stdin-json'] && process.stdin.isTTY)
208
+ const fromWeb = web
209
+ ? await promptFields(
210
+ { name, preset: args.preset === undefined ? undefined : parsePreset(args.preset), tier },
211
+ showPrompt,
212
+ )
213
+ : {}
214
+ const fields = { ...given, ...typed, ...fromWeb }
215
+ const merged = { ...(args.replace ? {} : previous), ...fields }
216
+ if (Object.keys(merged).length === 0) {
217
+ throw new TypeVaultError('invalid', 'set needs at least one field')
218
+ }
219
+ if (args.env !== undefined && defaultFieldOf(merged) === undefined) {
220
+ throw new TypeVaultError(
221
+ 'invalid',
222
+ '--env names the default field, but this entry has several fields and no $default',
223
+ )
224
+ }
225
+ const ifRev = args['if-rev'] === undefined ? undefined : Number(args['if-rev'])
226
+ if (ifRev !== undefined && !Number.isInteger(ifRev)) {
227
+ throw new TypeVaultError('invalid', `--if-rev must be a revision number, not "${args['if-rev']}"`)
228
+ }
112
229
  const tags = csv(args.tag)
113
230
  const stored = await access.set({
114
231
  name,
115
- type,
116
- fields: { ...base, ...fields },
117
- group: args.group ?? existing?.group,
232
+ fields: merged,
233
+ tier,
118
234
  env: args.env,
119
235
  origin: args.origin,
120
236
  ...(tags.length === 0 ? {} : { tags }),
121
237
  ifRev,
122
238
  })
123
- note(`stored ${stored.name} in ${stored.group} (r${stored.rev})`)
239
+ note(`stored ${stored.name} in ${stored.tier} (r${stored.rev})`)
124
240
  },
125
241
  })
126
242
 
243
+ function showPrompt(url: string, code: string): void {
244
+ note(`Enter the secret at ${url}\nMatch code: ${code} (the page shows the same code)`)
245
+ if (!openBrowser(url)) note('could not open a browser; open the link yourself')
246
+ }
247
+
127
248
  export const rm = defineCommand({
128
249
  meta: { name: 'rm', description: 'Remove an entry' },
129
250
  args: {
130
- name: { type: 'positional', required: true },
131
- group: { type: 'string', description: 'Remove the copy in this group (settles a name held by unrelated groups)' },
251
+ name: { type: 'positional', description: ENTRY_NAME_SYNTAX, required: true },
252
+ tier: { type: 'string', description: 'Remove the copy in this tier (settles a name held by unrelated tiers)' },
132
253
  },
133
254
  async run({ args }) {
134
- await (await accessOf(process.env)).remove(parseEntryName(args.name), args.group)
255
+ await (await accessOf(process.env)).remove(parseEntryName(args.name), args.tier)
135
256
  note(`removed ${args.name}. Git history still holds its old ciphertext: rotate the secret if it mattered.`)
136
257
  },
137
258
  })
138
259
 
139
260
  export const mv = defineCommand({
140
261
  meta: { name: 'mv', description: 'Rename an entry' },
141
- args: { from: { type: 'positional', required: true }, to: { type: 'positional', required: true } },
262
+ args: {
263
+ from: { type: 'positional', description: ENTRY_NAME_SYNTAX, required: true },
264
+ to: { type: 'positional', description: ENTRY_NAME_SYNTAX, required: true },
265
+ },
142
266
  async run({ args }) {
143
267
  await (await accessOf(process.env)).move(parseEntryName(args.from), parseEntryName(args.to))
144
268
  },
@@ -146,10 +270,21 @@ export const mv = defineCommand({
146
270
 
147
271
  export const otp = defineCommand({
148
272
  meta: { name: 'otp', description: 'Print the current TOTP code' },
149
- args: { name: { type: 'positional', required: true }, reason: { type: 'string' } },
273
+ args: { name: { type: 'positional', description: ENTRY_REF_SYNTAX, required: true }, reason: { type: 'string' } },
150
274
  async run({ args }) {
151
- const value = await (await accessOf(process.env)).get(parseEntryName(args.name), args.reason)
152
- const seed = value.type === 'totp' ? value.fields['seed'] : value.fields['totp']
275
+ const ref = parseEntryRef(args.name)
276
+ const value = await (await accessOf(process.env)).get(ref.name, args.reason)
277
+ const fallback = defaultFieldOf(value.fields)
278
+ const totps = Object.values(value.fields).filter((candidate) => candidate.type === 'totp')
279
+ const field =
280
+ ref.field !== undefined
281
+ ? resolveField(value.fields, ref.field)
282
+ : fallback !== undefined && value.fields[fallback]?.type === 'totp'
283
+ ? value.fields[fallback]
284
+ : totps.length === 1
285
+ ? totps[0]
286
+ : undefined
287
+ const seed = field?.type === 'totp' ? field.value : undefined
153
288
  if (seed === undefined || seed === '') throw new TypeVaultError('not_found', `${value.name} has no TOTP seed`)
154
289
  const params = parseTotpSeed(seed)
155
290
  const now = new Date()
@@ -1,26 +1,18 @@
1
- import { watch } from 'node:fs'
1
+ import { existsSync, watch } from 'node:fs'
2
2
 
3
3
  import { defineCommand } from 'citty'
4
4
  import { z } from 'zod'
5
5
 
6
- import { loadLocalKeys } from '../../access/keys'
6
+ import { machineKeys } from '../../access/keys'
7
7
  import { parseTtl } from '../../access/scope'
8
8
  import { issueSealedGrant } from '../../access/sealed'
9
+ import { GrantInfoSchema, IssuedSchema } from '../../admin'
9
10
  import { readAudit, verifyAudit } from '../../audit/log'
10
11
  import { TypeVaultError } from '../../core/errors'
11
12
  import { adminCall, layoutFrom, openDirect } from '../context'
12
13
  import { csv, print, table } from '../output'
13
14
 
14
- const Issued = z.object({ token: z.string() })
15
- const GrantList = z.array(
16
- z.object({
17
- id: z.string(),
18
- label: z.string(),
19
- groups: z.array(z.string()),
20
- only: z.array(z.string()),
21
- exp: z.string(),
22
- }),
23
- )
15
+ const GrantList = z.array(GrantInfoSchema)
24
16
 
25
17
  export const grant = defineCommand({
26
18
  meta: {
@@ -30,7 +22,7 @@ export const grant = defineCommand({
30
22
  args: {
31
23
  action: { type: 'positional', required: false, description: 'ls | revoke' },
32
24
  id: { type: 'positional', required: false },
33
- group: { type: 'string', description: 'Comma-separated groups' },
25
+ tier: { type: 'string', description: 'Comma-separated tiers' },
34
26
  only: { type: 'string', description: 'Comma-separated names or globs' },
35
27
  ttl: { type: 'string', default: '1h' },
36
28
  label: { type: 'string', description: 'Who this is for, e.g. session:abc or ci:deploy' },
@@ -43,8 +35,8 @@ export const grant = defineCommand({
43
35
  const grants = await adminCall(env, '/v1/grants', {}, GrantList)
44
36
  return print(
45
37
  table([
46
- ['ID', 'LABEL', 'GROUPS', 'ONLY', 'EXPIRES'],
47
- ...grants.map((g) => [g.id, g.label, g.groups.join(','), g.only.join(','), g.exp]),
38
+ ['ID', 'LABEL', 'TIERS', 'ONLY', 'EXPIRES'],
39
+ ...grants.map((g) => [g.id, g.label, g.tiers.join(','), g.only.join(','), g.exp]),
48
40
  ]),
49
41
  )
50
42
  }
@@ -62,15 +54,15 @@ export const grant = defineCommand({
62
54
  default:
63
55
  throw new TypeVaultError('invalid', `unknown grant action "${args.action}"`)
64
56
  }
65
- const groups = csv(args.group)
66
- if (groups.length === 0) throw new TypeVaultError('invalid', 'grant needs --group')
67
- const request = { groups, only: csv(args.only), ttl: args.ttl, label: args.label ?? 'unlabeled' }
68
- // A launcher holding a key (CI) seals locally; everything else asks the daemon within its ceiling.
69
- if (args.sealed && loadLocalKeys(layoutFrom(env), env).length > 0) {
57
+ const tiers = csv(args.tier)
58
+ if (tiers.length === 0) throw new TypeVaultError('invalid', 'grant needs --tier')
59
+ const request = { tiers, only: csv(args.only), ttl: args.ttl, label: args.label ?? 'unlabeled' }
60
+ // An issuer holding a key (CI) seals locally; everything else asks the daemon within its ceiling.
61
+ if (args.sealed && machineKeys(env).length > 0) {
70
62
  const vault = await openDirect(env)
71
63
  return print(await issueSealedGrant(vault, { ...request, ttlMs: parseTtl(args.ttl) }))
72
64
  }
73
- print((await adminCall(env, '/v1/grant', { ...request, sealed: args.sealed === true }, Issued)).token)
65
+ print((await adminCall(env, '/v1/grant', { ...request, sealed: args.sealed === true }, IssuedSchema)).token)
74
66
  },
75
67
  })
76
68
 
@@ -93,6 +85,16 @@ export const audit = defineCommand({
93
85
  shown = records.length
94
86
  }
95
87
  show()
96
- if (args.follow) await new Promise<never>(() => watch(layout.auditLog, show))
88
+ if (args.follow) {
89
+ if (!existsSync(layout.auditLog)) {
90
+ throw new TypeVaultError(
91
+ 'not_found',
92
+ `no audit log at ${layout.auditLog}; retry after the first audited action`,
93
+ )
94
+ }
95
+ await new Promise<never>((_resolve, reject) => {
96
+ watch(layout.auditLog, show).on('error', reject)
97
+ })
98
+ }
97
99
  },
98
100
  })