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.
- package/README.md +156 -115
- package/package.json +14 -2
- package/src/access/env.ts +77 -17
- package/src/access/keys.ts +14 -14
- package/src/access/schemas.ts +6 -12
- package/src/access/scope.ts +14 -9
- package/src/access/scoped.ts +118 -44
- package/src/access/sealed-access.ts +50 -23
- package/src/access/sealed.ts +56 -31
- package/src/access/types.ts +27 -30
- package/src/admin.ts +68 -0
- package/src/audit/log.ts +28 -12
- package/src/cli/commands/admin.ts +93 -48
- package/src/cli/commands/daemon.ts +42 -17
- package/src/cli/commands/entries.ts +195 -60
- package/src/cli/commands/grant.ts +24 -22
- package/src/cli/commands/members.ts +307 -0
- package/src/cli/commands/run.ts +191 -44
- package/src/cli/context.ts +163 -15
- package/src/cli/index.ts +33 -5
- package/src/cli/password.ts +74 -0
- package/src/cli/web-prompt.ts +79 -0
- package/src/core/admin.ts +144 -76
- package/src/core/adopt.ts +40 -9
- package/src/core/crypto.ts +6 -4
- package/src/core/doctor.ts +15 -16
- package/src/core/entries.ts +66 -52
- package/src/core/errors.ts +8 -2
- package/src/core/fsx.ts +7 -3
- package/src/core/holder.ts +87 -0
- package/src/core/identity.ts +132 -0
- package/src/core/init.ts +73 -23
- package/src/core/json-file.ts +20 -0
- package/src/core/keyring.ts +100 -56
- package/src/core/layout.ts +143 -26
- package/src/core/lock.ts +128 -16
- package/src/core/naming.ts +150 -50
- package/src/core/password.ts +111 -0
- package/src/core/payload.ts +42 -38
- package/src/core/revisions.ts +11 -6
- package/src/core/rotate.ts +59 -42
- package/src/core/state.ts +102 -54
- package/src/core/txn.ts +9 -7
- package/src/core/vault.ts +108 -46
- package/src/core/worktrees.ts +51 -0
- package/src/daemon/admin-routes.ts +33 -21
- package/src/daemon/agent-routes.ts +31 -11
- package/src/daemon/client.ts +24 -2
- package/src/daemon/daemon.ts +218 -33
- package/src/daemon/grants.ts +25 -7
- package/src/daemon/http.ts +11 -2
- package/src/daemon/live-access.ts +8 -4
- package/src/daemon/protocol.ts +17 -12
- package/src/daemon/requests.ts +75 -51
- package/src/daemon/spawn.ts +218 -9
- package/src/export/1password.ts +157 -0
- package/src/import/1password-live.ts +399 -0
- package/src/import/1password.ts +26 -5
- package/src/import/bitwarden.ts +3 -3
- package/src/import/csv.ts +1 -1
- package/src/import/dotenv.ts +4 -3
- package/src/import/index.ts +1 -0
- package/src/import/keepass.ts +2 -2
- package/src/import/lastpass.ts +2 -2
- package/src/import/shared.ts +67 -10
- package/src/import/typeclaw.ts +2 -2
- package/src/import/types.ts +1 -1
- package/src/index.ts +58 -0
- package/src/owner.ts +156 -0
- package/src/prompt/browser.ts +48 -15
- package/src/prompt/index.ts +121 -64
- package/src/prompt/page.ts +27 -11
- package/src/prompt/styles.ts +64 -34
- package/src/prompt/types.ts +7 -4
- package/src/redact/index.ts +48 -12
- 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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
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.
|
|
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: {
|
|
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:
|
|
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)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
60
|
-
|
|
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 (!
|
|
66
|
-
|
|
67
|
-
|
|
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(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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: {
|
|
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:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
|
|
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
|
-
|
|
116
|
-
|
|
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.
|
|
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
|
-
|
|
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.
|
|
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: {
|
|
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
|
|
152
|
-
const
|
|
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 {
|
|
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
|
|
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
|
-
|
|
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', '
|
|
47
|
-
...grants.map((g) => [g.id, g.label, g.
|
|
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
|
|
66
|
-
if (
|
|
67
|
-
const request = {
|
|
68
|
-
//
|
|
69
|
-
if (args.sealed &&
|
|
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 },
|
|
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)
|
|
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
|
})
|