typevault 0.8.0 → 0.9.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/scoped.ts +4 -7
- package/src/access/sealed-access.ts +18 -3
- package/src/access/types.ts +20 -0
- package/src/core/vault.ts +13 -2
- package/src/daemon/protocol.ts +3 -1
package/package.json
CHANGED
package/src/access/scoped.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
type EntryValue,
|
|
14
14
|
entryInputOf,
|
|
15
15
|
type SetInput,
|
|
16
|
+
putOptionsOf,
|
|
16
17
|
toSummary,
|
|
17
18
|
toValue,
|
|
18
19
|
writtenFromAbove,
|
|
@@ -154,7 +155,8 @@ export class ScopedVault implements Access {
|
|
|
154
155
|
: undefined
|
|
155
156
|
const key = original ?? (await this.#vault.keyring.unlock(tier))
|
|
156
157
|
if (key === undefined) throw new TypeVaultError('denied', `no key here can refresh tier "${tier}"`)
|
|
157
|
-
|
|
158
|
+
// A refresh replaces short-lived tokens, not the credentials a rotation warning is about.
|
|
159
|
+
const refreshed = await this.#vault.put(input, signerOf(key), { rotates: false })
|
|
158
160
|
await this.#audit({ action: 'refresh', entries: [name] })
|
|
159
161
|
return refreshed
|
|
160
162
|
}
|
|
@@ -240,12 +242,7 @@ export class ScopedVault implements Access {
|
|
|
240
242
|
if (existing !== undefined && !this.#allows(existing)) {
|
|
241
243
|
throw new TypeVaultError('denied', `the name ${input.name} is not available to this grant`)
|
|
242
244
|
}
|
|
243
|
-
const
|
|
244
|
-
const stored = await this.#vault.put(
|
|
245
|
-
entryInputOf(input, tier),
|
|
246
|
-
await this.#signerFor(tier),
|
|
247
|
-
ifRev === undefined ? {} : { ifRev },
|
|
248
|
-
)
|
|
245
|
+
const stored = await this.#vault.put(entryInputOf(input, tier), await this.#signerFor(tier), putOptionsOf(input))
|
|
249
246
|
await this.#audit({ action: 'write', entries: [input.name], tiers: [tier] })
|
|
250
247
|
return toSummary(valueOf(stored))
|
|
251
248
|
}
|
|
@@ -9,7 +9,15 @@ import { Vault } from '../core/vault'
|
|
|
9
9
|
import { secretValues } from './env'
|
|
10
10
|
import { allowsEntry } from './scope'
|
|
11
11
|
import { type SealedBundle, sealedSigningSecret } from './sealed'
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
type Access,
|
|
14
|
+
type EntrySummary,
|
|
15
|
+
type EntryValue,
|
|
16
|
+
entryInputOf,
|
|
17
|
+
putOptionsOf,
|
|
18
|
+
type SetInput,
|
|
19
|
+
toSummary,
|
|
20
|
+
} from './types'
|
|
13
21
|
|
|
14
22
|
type Env = Readonly<Record<string, string | undefined>>
|
|
15
23
|
|
|
@@ -84,6 +92,14 @@ export class SealedAccess implements Access {
|
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
async set(input: SetInput): Promise<EntrySummary> {
|
|
95
|
+
// A blind writer sees only the bundled snapshot, not what the name holds now, so it can neither check the entry's
|
|
96
|
+
// id nor carry its current rotation warning forward. Refuse rather than report a guarantee it didn't give.
|
|
97
|
+
if (input.ifId !== undefined || input.rotates === false) {
|
|
98
|
+
throw new TypeVaultError(
|
|
99
|
+
'unavailable',
|
|
100
|
+
`a sealed grant can't verify the entry it replaces, so it can't take ${input.ifId === undefined ? 'rotates: false' : 'ifId'}; use a live grant`,
|
|
101
|
+
)
|
|
102
|
+
}
|
|
87
103
|
const bundle = this.#live()
|
|
88
104
|
const vault = await this.#vault()
|
|
89
105
|
const prior = bundle.entries.find((value) => value.name === input.name)
|
|
@@ -95,11 +111,10 @@ export class SealedAccess implements Access {
|
|
|
95
111
|
if (prior !== undefined && prior.tier !== tier) {
|
|
96
112
|
throw new TypeVaultError('denied', `${input.name} is in tier "${prior.tier}"; a sealed grant cannot move it`)
|
|
97
113
|
}
|
|
98
|
-
const { ifRev } = input
|
|
99
114
|
const stored = await vault.put(
|
|
100
115
|
entryInputOf(input, tier),
|
|
101
116
|
{ ref: { kind: 'cert', cert: bundle.write.cert }, signSecret: sealedSigningSecret(bundle) },
|
|
102
|
-
{ ...(
|
|
117
|
+
{ ...putOptionsOf(input), ...(prior === undefined ? {} : { base: prior }) },
|
|
103
118
|
)
|
|
104
119
|
await this.#audit({ action: 'write', entries: [input.name], tiers: [tier] }, vault.anchorsFile)
|
|
105
120
|
const { envelope, payload } = stored
|
package/src/access/types.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { TypeVaultError } from '../core/errors'
|
|
|
3
3
|
import { type EntryName, type Field, isReservedEnvName } from '../core/naming'
|
|
4
4
|
import type { EntryPayload } from '../core/payload'
|
|
5
5
|
import { reachesTier, type VaultState } from '../core/state'
|
|
6
|
+
import type { PutOptions } from '../core/vault'
|
|
6
7
|
|
|
7
8
|
export type EntrySummary = {
|
|
8
9
|
readonly name: EntryName
|
|
@@ -36,6 +37,25 @@ export type SetInput = {
|
|
|
36
37
|
readonly source?: string | undefined
|
|
37
38
|
readonly tags?: readonly string[] | undefined
|
|
38
39
|
readonly ifRev?: number | undefined
|
|
40
|
+
/**
|
|
41
|
+
* Write only if the name still holds this entry (`EntryValue.id`): a same-name replacement can share its revision.
|
|
42
|
+
* A sealed grant refuses it, since it can't read what the name holds now.
|
|
43
|
+
*/
|
|
44
|
+
readonly ifId?: string | undefined
|
|
45
|
+
/**
|
|
46
|
+
* False when the new fields don't rotate the entry's credentials, such as a passkey's sign count: its rotation time
|
|
47
|
+
* and any `needsRotation` warning stay as they were. A sealed grant refuses `false`, since it can't read the warning.
|
|
48
|
+
*/
|
|
49
|
+
readonly rotates?: boolean | undefined
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The compare-and-swap and rotation options of a write, without the ones a parsed request leaves undefined. */
|
|
53
|
+
export function putOptionsOf(input: SetInput): PutOptions {
|
|
54
|
+
return {
|
|
55
|
+
...(input.ifRev === undefined ? {} : { ifRev: input.ifRev }),
|
|
56
|
+
...(input.ifId === undefined ? {} : { ifId: input.ifId }),
|
|
57
|
+
...(input.rotates === undefined ? {} : { rotates: input.rotates }),
|
|
58
|
+
}
|
|
39
59
|
}
|
|
40
60
|
|
|
41
61
|
/** The vault-level input for a write, without the optional keys a parsed request leaves undefined. */
|
package/src/core/vault.ts
CHANGED
|
@@ -40,7 +40,13 @@ export type EntryBase = {
|
|
|
40
40
|
readonly source?: string
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export type PutOptions = {
|
|
43
|
+
export type PutOptions = {
|
|
44
|
+
readonly ifRev?: number
|
|
45
|
+
readonly ifId?: string
|
|
46
|
+
/** False for a write that changes fields without rotating the entry's credentials (an OAuth refresh, a counter). */
|
|
47
|
+
readonly rotates?: boolean
|
|
48
|
+
readonly base?: EntryBase
|
|
49
|
+
}
|
|
44
50
|
|
|
45
51
|
export type EntryInput = {
|
|
46
52
|
readonly name: EntryName
|
|
@@ -253,8 +259,13 @@ export class Vault {
|
|
|
253
259
|
if (options.ifRev !== undefined && currentRev !== options.ifRev) {
|
|
254
260
|
throw new TypeVaultError('conflict', `${input.name} is at revision ${currentRev}, not ${options.ifRev}`)
|
|
255
261
|
}
|
|
262
|
+
// A name deleted and recreated can be back at the same revision; the id tells the two entries apart.
|
|
263
|
+
if (options.ifId !== undefined && prior?.id !== options.ifId) {
|
|
264
|
+
throw new TypeVaultError('conflict', `${input.name} is not the entry ${options.ifId}`)
|
|
265
|
+
}
|
|
256
266
|
const now = new Date().toISOString()
|
|
257
|
-
const changed =
|
|
267
|
+
const changed =
|
|
268
|
+
(found === undefined || !sameFields(found.payload.fields, input.fields)) && options.rotates !== false
|
|
258
269
|
const source = input.source ?? prior?.source
|
|
259
270
|
const payload: EntryPayload = {
|
|
260
271
|
v: 1,
|
package/src/daemon/protocol.ts
CHANGED
|
@@ -25,10 +25,12 @@ export const SetBody = z.object({
|
|
|
25
25
|
origin: z.string().optional(),
|
|
26
26
|
tags: z.array(z.string()).optional(),
|
|
27
27
|
ifRev: z.number().int().nonnegative().optional(),
|
|
28
|
+
ifId: z.string().optional(),
|
|
29
|
+
rotates: z.boolean().optional(),
|
|
28
30
|
})
|
|
29
31
|
/** The entries an import writes, sent as staged chunks of their serialized JSON. */
|
|
30
32
|
export const ImportEntries = z.array(
|
|
31
|
-
SetBody.omit({ tier: true, ifRev: true }).extend({ source: z.string().optional() }),
|
|
33
|
+
SetBody.omit({ tier: true, ifRev: true, ifId: true, rotates: true }).extend({ source: z.string().optional() }),
|
|
32
34
|
)
|
|
33
35
|
/** One base64 chunk of an import, staged until `ImportBody` commits it; the first chunk starts a new upload. */
|
|
34
36
|
export const ImportStageBody = z.object({ upload: z.string().max(64).optional(), chunk: z.base64() })
|