typevault 0.4.0 → 0.5.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/package.json +1 -1
- package/src/access/schemas.ts +1 -1
- package/src/access/sealed-access.ts +1 -0
- package/src/access/types.ts +4 -1
- package/src/cli/commands/daemon.ts +5 -1
- package/src/cli/commands/entries.ts +8 -4
- package/src/cli/commands/run.ts +4 -2
- package/src/cli/context.ts +44 -1
- package/src/daemon/spawn.ts +21 -0
package/package.json
CHANGED
package/src/access/schemas.ts
CHANGED
|
@@ -13,7 +13,7 @@ const SummaryShape = {
|
|
|
13
13
|
needsRotation: z.boolean(),
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export const EntrySummarySchema = z.object(SummaryShape)
|
|
16
|
+
export const EntrySummarySchema = z.object({ ...SummaryShape, fieldNames: z.array(z.string()) })
|
|
17
17
|
export const EntryValueSchema = z.object({
|
|
18
18
|
...SummaryShape,
|
|
19
19
|
id: z.string(),
|
package/src/access/types.ts
CHANGED
|
@@ -11,9 +11,11 @@ export type EntrySummary = {
|
|
|
11
11
|
readonly tags: readonly string[]
|
|
12
12
|
readonly rotated: string
|
|
13
13
|
readonly needsRotation: boolean
|
|
14
|
+
/** Names only: listing never hands out values. */
|
|
15
|
+
readonly fieldNames: readonly string[]
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
export type EntryValue = EntrySummary & {
|
|
18
|
+
export type EntryValue = Omit<EntrySummary, 'fieldNames'> & {
|
|
17
19
|
readonly id: string
|
|
18
20
|
readonly created: string
|
|
19
21
|
readonly fields: Readonly<Record<string, Field>>
|
|
@@ -93,6 +95,7 @@ export const toSummary = (value: EntryValue): EntrySummary => ({
|
|
|
93
95
|
tags: value.tags,
|
|
94
96
|
rotated: value.rotated,
|
|
95
97
|
needsRotation: value.needsRotation,
|
|
98
|
+
fieldNames: Object.keys(value.fields),
|
|
96
99
|
})
|
|
97
100
|
|
|
98
101
|
/** Whether `signer` is at least as privileged as a reader holding `ownTiers`. */
|
|
@@ -8,7 +8,7 @@ import { TypeVaultError } from '../../core/errors'
|
|
|
8
8
|
import { localLayout } from '../../core/layout'
|
|
9
9
|
import { isDaemonAlive } from '../../daemon/client'
|
|
10
10
|
import { startDaemon } from '../../daemon/daemon'
|
|
11
|
-
import { ensureDaemon, stopDaemon } from '../../daemon/spawn'
|
|
11
|
+
import { ensureDaemon, restartDaemon, stopDaemon } from '../../daemon/spawn'
|
|
12
12
|
import { adminCall, DEFAULT_SESSION_MS, layoutFrom } from '../context'
|
|
13
13
|
import { note, print, readStdin } from '../output'
|
|
14
14
|
|
|
@@ -76,6 +76,10 @@ export const daemon = defineCommand({
|
|
|
76
76
|
meta: { name: 'stop' },
|
|
77
77
|
run: async () => stopDaemon(layoutFrom(process.env)),
|
|
78
78
|
}),
|
|
79
|
+
restart: defineCommand({
|
|
80
|
+
meta: { name: 'restart' },
|
|
81
|
+
run: async () => restartDaemon(layoutFrom(process.env), process.env),
|
|
82
|
+
}),
|
|
79
83
|
status: defineCommand({
|
|
80
84
|
meta: { name: 'status' },
|
|
81
85
|
async run() {
|
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
} from '../../core/naming'
|
|
21
21
|
import { FieldSchema } from '../../core/payload'
|
|
22
22
|
import { parseTotpSeed, totpCode, totpRemainingSeconds } from '../../otp/totp'
|
|
23
|
-
import { accessOf, resolveAccess } from '../context'
|
|
23
|
+
import { accessOf, getEntry, resolveAccess } from '../context'
|
|
24
24
|
import { csv, note, print, readStdin, repeated, table } from '../output'
|
|
25
25
|
import { openBrowser, promptFields } from '../web-prompt'
|
|
26
26
|
|
|
@@ -44,10 +44,14 @@ export const ls = defineCommand({
|
|
|
44
44
|
e.tier,
|
|
45
45
|
`r${e.rev}`,
|
|
46
46
|
e.rotated.slice(0, 10),
|
|
47
|
+
e.fieldNames.join(','),
|
|
47
48
|
e.needsRotation ? 'needs-rotation' : '',
|
|
48
49
|
])
|
|
49
50
|
print(
|
|
50
|
-
table([
|
|
51
|
+
table([
|
|
52
|
+
['NAME', 'TIER', 'REV', 'ROTATED', 'FIELDS', ''],
|
|
53
|
+
...rows.sort((a, b) => (a[0] ?? '').localeCompare(b[0] ?? '')),
|
|
54
|
+
]),
|
|
51
55
|
)
|
|
52
56
|
},
|
|
53
57
|
})
|
|
@@ -65,7 +69,7 @@ export const get = defineCommand({
|
|
|
65
69
|
},
|
|
66
70
|
async run({ args }) {
|
|
67
71
|
const ref = parseEntryRef(args.ref)
|
|
68
|
-
const value = await (await accessOf(process.env)
|
|
72
|
+
const value = await getEntry(await accessOf(process.env), ref.name, args.reason)
|
|
69
73
|
if (args.json) {
|
|
70
74
|
const fields = Object.fromEntries(
|
|
71
75
|
Object.entries(value.fields).map(([field, secret]) => [
|
|
@@ -263,7 +267,7 @@ export const otp = defineCommand({
|
|
|
263
267
|
args: { name: { type: 'positional', description: ENTRY_REF_SYNTAX, required: true }, reason: { type: 'string' } },
|
|
264
268
|
async run({ args }) {
|
|
265
269
|
const ref = parseEntryRef(args.name)
|
|
266
|
-
const value = await (await accessOf(process.env)
|
|
270
|
+
const value = await getEntry(await accessOf(process.env), ref.name, args.reason)
|
|
267
271
|
const fallback = defaultFieldOf(value.fields)
|
|
268
272
|
const totps = Object.values(value.fields).filter((candidate) => candidate.type === 'totp')
|
|
269
273
|
const field =
|
package/src/cli/commands/run.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { TypeVaultError } from '../../core/errors'
|
|
|
8
8
|
import { ENTRY_NAME_SYNTAX, matchesOnly } from '../../core/naming'
|
|
9
9
|
import { importFrom, ONEPASSWORD_VIAS, type ImportSource, type OnePasswordVia, read1Password } from '../../import'
|
|
10
10
|
import { createRedactor, type Redactor, scrubFile } from '../../redact'
|
|
11
|
-
import { resolveAccess } from '../context'
|
|
11
|
+
import { getEntry, inGlobalVault, resolveAccess } from '../context'
|
|
12
12
|
import { csv, note, print, readStdin, repeated } from '../output'
|
|
13
13
|
|
|
14
14
|
async function pump(
|
|
@@ -48,11 +48,13 @@ export const run = defineCommand({
|
|
|
48
48
|
const values = await access.values(only, args.reason)
|
|
49
49
|
const unmatched = only.filter((pattern) => !values.some((value) => matchesOnly(value.name, [pattern])))
|
|
50
50
|
if (unmatched.length > 0) note(`warning: --only matched no entries: ${unmatched.join(', ')}`)
|
|
51
|
+
const inGlobal = await inGlobalVault(process.env, explicitNames(unmatched))
|
|
52
|
+
if (inGlobal.length > 0) note(`the global vault has ${inGlobal.join(', ')}: rerun with -g`)
|
|
51
53
|
const policy = { ownTiers: await access.ownTiers(), explicit: explicitNames(only) }
|
|
52
54
|
const env = envFor(values, new Date(), policy)
|
|
53
55
|
// The child gets its secrets, never the key or grant that opens the vault.
|
|
54
56
|
const inherited = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('TYPEVAULT_')))
|
|
55
|
-
const refs = await resolveEnvRefs(inherited, (name) => access
|
|
57
|
+
const refs = await resolveEnvRefs(inherited, (name) => getEntry(access, name, args.reason), new Date())
|
|
56
58
|
const referenced = new Set(refs.values.map((value) => value.name))
|
|
57
59
|
const skipped = values
|
|
58
60
|
.filter((value) => !envEligible(value, policy) && !referenced.has(value.name))
|
package/src/cli/context.ts
CHANGED
|
@@ -6,13 +6,14 @@ import { machineKeys } from '../access/keys'
|
|
|
6
6
|
import { ScopedVault } from '../access/scoped'
|
|
7
7
|
import { readSealedGrant } from '../access/sealed'
|
|
8
8
|
import { SealedAccess } from '../access/sealed-access'
|
|
9
|
-
import type { Access } from '../access/types'
|
|
9
|
+
import type { Access, EntryValue } from '../access/types'
|
|
10
10
|
import { Admin } from '../core/admin'
|
|
11
11
|
import type { SecretKey } from '../core/crypto'
|
|
12
12
|
import { TypeVaultError } from '../core/errors'
|
|
13
13
|
import { atomicWrite } from '../core/fsx'
|
|
14
14
|
import { type Identity, readIdentity, sealedCopyPath, unlockIdentity } from '../core/identity'
|
|
15
15
|
import { findLocalVault, globalLayout, type Layout, localLayout } from '../core/layout'
|
|
16
|
+
import type { EntryName } from '../core/naming'
|
|
16
17
|
import { loadState } from '../core/state'
|
|
17
18
|
import { Vault } from '../core/vault'
|
|
18
19
|
import { worktreeVaults } from '../core/worktrees'
|
|
@@ -200,6 +201,48 @@ function checkOwnerPin(layout: Layout, env: Env): void {
|
|
|
200
201
|
if (isSet(env['TYPEVAULT_OWNER'])) loadState(layout, env)
|
|
201
202
|
}
|
|
202
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Which of `names` the global vault holds, when the project vault was asked. It asks only an already unlocked global
|
|
206
|
+
* session: never a password, a daemon start or a trust pin, and listing is not audited. Anything else answers none,
|
|
207
|
+
* so a miss points at `-g` only when the global vault really has the name.
|
|
208
|
+
*/
|
|
209
|
+
export async function inGlobalVault(env: Env, names: readonly string[]): Promise<readonly string[]> {
|
|
210
|
+
if (names.length === 0 || wantsGlobal(env) || isSet(env['TYPEVAULT_GRANT'])) return []
|
|
211
|
+
const layout = globalLayout(env)
|
|
212
|
+
if (!(await hasSession(layout))) return []
|
|
213
|
+
try {
|
|
214
|
+
const access = new LiveAccess(layout.agentSocket, readFileSync(layout.sessionFile, 'utf8').trim())
|
|
215
|
+
const held = new Set<string>((await access.list()).map((entry) => entry.name))
|
|
216
|
+
return names.filter((name) => held.has(name))
|
|
217
|
+
} catch (error) {
|
|
218
|
+
// Only a hint: a session that ends meanwhile, or a daemon from another release, gives none.
|
|
219
|
+
if (error instanceof TypeVaultError || (error instanceof Error && 'code' in error && error.code === 'ENOENT')) {
|
|
220
|
+
return []
|
|
221
|
+
}
|
|
222
|
+
throw error
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** `access.get`, whose miss says so when the global vault has the name. */
|
|
227
|
+
export async function getEntry(access: Access, name: EntryName, reason?: string): Promise<EntryValue> {
|
|
228
|
+
try {
|
|
229
|
+
return await access.get(name, reason)
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (
|
|
232
|
+
error instanceof TypeVaultError &&
|
|
233
|
+
error.code === 'not_found' &&
|
|
234
|
+
(await inGlobalVault(process.env, [name])).length > 0
|
|
235
|
+
) {
|
|
236
|
+
throw new TypeVaultError(
|
|
237
|
+
'not_found',
|
|
238
|
+
`${error.message} in the project vault; the global vault has it: rerun with -g`,
|
|
239
|
+
{ cause: error },
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
throw error
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
203
246
|
/** How long an unlocked session lasts unless `unlock --for` says otherwise. */
|
|
204
247
|
export const DEFAULT_SESSION_MS = 8 * 60 * 60_000
|
|
205
248
|
|
package/src/daemon/spawn.ts
CHANGED
|
@@ -50,6 +50,27 @@ export async function ensureDaemon(layout: Layout, env: Env): Promise<void> {
|
|
|
50
50
|
await startBackground(layout, env, [process.execPath, CLI, 'daemon', 'run', ...vaultArgs(layout)])
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Stops this checkout's daemon, if running, and starts a fresh one. Only a machine member's daemon can come back on
|
|
55
|
+
* its own: an unlocked session's key would be gone with it, so that one is refused before anything stops.
|
|
56
|
+
*/
|
|
57
|
+
export async function restartDaemon(layout: Layout, env: Env): Promise<void> {
|
|
58
|
+
// A token with no daemon answering was left by a killed session; the new daemon clears it on start.
|
|
59
|
+
if (existsSync(layout.sessionFile) && (await isDaemonAlive(layout.agentSocket))) {
|
|
60
|
+
throw new TypeVaultError(
|
|
61
|
+
'conflict',
|
|
62
|
+
'this daemon holds an unlocked session, which a restart would end: run `typevault lock`, then `typevault unlock`',
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
if (machineKeys(env).length === 0) throw new TypeVaultError('locked', LOCKED_MESSAGE)
|
|
66
|
+
try {
|
|
67
|
+
await stopDaemon(layout)
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (!(error instanceof TypeVaultError && error.code === 'not_found')) throw error
|
|
70
|
+
}
|
|
71
|
+
await ensureDaemon(layout, env)
|
|
72
|
+
}
|
|
73
|
+
|
|
53
74
|
function versionMismatch(layout: Layout, running: string | null, version: string): TypeVaultError {
|
|
54
75
|
const from = running === null ? 'is from an older release' : `is version ${running}`
|
|
55
76
|
const fix = existsSync(layout.sessionFile)
|