typevault 0.1.0 → 0.2.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 +7 -7
- package/src/access/scoped.ts +100 -44
- package/src/access/sealed-access.ts +45 -23
- package/src/access/sealed.ts +33 -21
- package/src/access/types.ts +25 -30
- package/src/admin.ts +68 -0
- package/src/audit/log.ts +14 -9
- package/src/cli/commands/admin.ts +87 -47
- package/src/cli/commands/daemon.ts +33 -15
- package/src/cli/commands/entries.ts +166 -55
- package/src/cli/commands/grant.ts +12 -20
- package/src/cli/commands/members.ts +281 -0
- package/src/cli/commands/run.ts +61 -24
- package/src/cli/context.ts +155 -15
- package/src/cli/index.ts +27 -4
- package/src/cli/password.ts +74 -0
- package/src/cli/web-prompt.ts +65 -0
- package/src/core/admin.ts +125 -74
- package/src/core/adopt.ts +40 -9
- package/src/core/crypto.ts +6 -4
- package/src/core/doctor.ts +9 -9
- package/src/core/entries.ts +66 -52
- package/src/core/errors.ts +6 -0
- package/src/core/fsx.ts +7 -3
- package/src/core/holder.ts +84 -0
- package/src/core/identity.ts +130 -0
- package/src/core/init.ts +73 -23
- package/src/core/keyring.ts +100 -56
- package/src/core/layout.ts +145 -26
- package/src/core/lock.ts +128 -16
- package/src/core/naming.ts +146 -49
- package/src/core/password.ts +111 -0
- package/src/core/payload.ts +42 -38
- package/src/core/revisions.ts +8 -4
- package/src/core/rotate.ts +59 -42
- package/src/core/state.ts +88 -48
- package/src/core/txn.ts +5 -4
- package/src/core/vault.ts +108 -46
- package/src/core/worktrees.ts +39 -0
- package/src/daemon/admin-routes.ts +33 -21
- package/src/daemon/agent-routes.ts +29 -11
- package/src/daemon/client.ts +15 -0
- package/src/daemon/daemon.ts +210 -32
- package/src/daemon/grants.ts +21 -4
- package/src/daemon/http.ts +3 -0
- package/src/daemon/live-access.ts +4 -4
- package/src/daemon/protocol.ts +17 -12
- package/src/daemon/requests.ts +17 -22
- package/src/daemon/spawn.ts +188 -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 +2 -2
- 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/page.ts +19 -11
- package/src/prompt/styles.ts +64 -34
- package/src/prompt/types.ts +3 -3
package/src/access/scoped.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { appendAudit, type AuditEvent, type AuditMode } from '../audit/log'
|
|
2
2
|
import { signerOf } from '../core/entries'
|
|
3
3
|
import { TypeVaultError } from '../core/errors'
|
|
4
|
-
import {
|
|
4
|
+
import { withLock } from '../core/lock'
|
|
5
|
+
import { type EntryName, type Field, matchesOnly } from '../core/naming'
|
|
5
6
|
import { resolveName, type StoredEntry, type Vault } from '../core/vault'
|
|
6
7
|
import { needsRefresh, refreshOAuth } from '../oauth/refresh'
|
|
7
|
-
import {
|
|
8
|
+
import { secretValues } from './env'
|
|
9
|
+
import { allowsEntry, allowsTier, type ScopeChain } from './scope'
|
|
8
10
|
import {
|
|
9
11
|
type Access,
|
|
10
12
|
type EntrySummary,
|
|
@@ -20,14 +22,14 @@ export type ScopedIdentity = {
|
|
|
20
22
|
readonly mode: AuditMode
|
|
21
23
|
readonly grant?: string
|
|
22
24
|
readonly label?: string
|
|
23
|
-
/**
|
|
24
|
-
readonly
|
|
25
|
-
/**
|
|
26
|
-
readonly
|
|
25
|
+
/** Tiers held directly; see `Access.ownTiers`. */
|
|
26
|
+
readonly ownTiers?: readonly string[]
|
|
27
|
+
/** Tier used by `set` when the caller names none. */
|
|
28
|
+
readonly defaultTier?: string
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
const valueOf = (entry: StoredEntry): EntryValue =>
|
|
30
|
-
toValue(entry.envelope.
|
|
32
|
+
toValue(entry.envelope.tier, entry.envelope.id, entry.envelope.rev, entry.payload)
|
|
31
33
|
|
|
32
34
|
/**
|
|
33
35
|
* Vault access bounded by a scope chain, with every read and write audited. The daemon wraps each live
|
|
@@ -56,7 +58,7 @@ export class ScopedVault implements Access {
|
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
#allows(entry: StoredEntry): boolean {
|
|
59
|
-
return allowsEntry(this.#chain, this.#vault.state, entry.envelope.
|
|
61
|
+
return allowsEntry(this.#chain, this.#vault.state, entry.envelope.tier, entry.payload.name)
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
/** One entry per name: the copy each name resolves to within this scope (see `resolveName`). */
|
|
@@ -66,7 +68,7 @@ export class ScopedVault implements Access {
|
|
|
66
68
|
entries.filter((entry) => this.#allows(entry)),
|
|
67
69
|
(entry) => entry.payload.name,
|
|
68
70
|
)
|
|
69
|
-
return [...byName.values()].flatMap((copies) => resolveName(this.#vault.state.
|
|
71
|
+
return [...byName.values()].flatMap((copies) => resolveName(this.#vault.state.tiers, copies) ?? [])
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
async #find(name: EntryName): Promise<StoredEntry> {
|
|
@@ -75,37 +77,62 @@ export class ScopedVault implements Access {
|
|
|
75
77
|
return entry
|
|
76
78
|
}
|
|
77
79
|
|
|
78
|
-
async #signerFor(
|
|
79
|
-
// Direct mode is the key holder acting itself, so the
|
|
80
|
-
// of grants and signs with the
|
|
81
|
-
const
|
|
82
|
-
const key =
|
|
83
|
-
if (key === undefined) throw new TypeVaultError('denied', `no key here can write
|
|
80
|
+
async #signerFor(tier: string): Promise<ReturnType<typeof signerOf>> {
|
|
81
|
+
// Direct mode is the key holder acting itself, so the owner signs as the owner. The daemon writes on behalf
|
|
82
|
+
// of grants and signs with the tier's own key, so a grant's writes never look owner-authored.
|
|
83
|
+
const owner = this.#identity.mode === 'direct' ? this.#vault.keyring.owner : undefined
|
|
84
|
+
const key = owner ?? (await this.#vault.keyring.unlock(tier)) ?? this.#vault.keyring.owner
|
|
85
|
+
if (key === undefined) throw new TypeVaultError('denied', `no key here can write tier "${tier}"`)
|
|
84
86
|
return signerOf(key)
|
|
85
87
|
}
|
|
86
88
|
|
|
87
|
-
/** OAuth
|
|
89
|
+
/** OAuth fields past their expiry are refreshed and written back before anyone reads them. */
|
|
88
90
|
async #fresh(entry: StoredEntry): Promise<StoredEntry> {
|
|
89
91
|
const now = new Date()
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
92
|
+
const fields: Record<string, Field> = { ...entry.payload.fields }
|
|
93
|
+
let changed = false
|
|
94
|
+
try {
|
|
95
|
+
for (const [name, field] of Object.entries(fields)) {
|
|
96
|
+
switch (field.type) {
|
|
97
|
+
case 'oauth':
|
|
98
|
+
if (needsRefresh(field.value, now)) {
|
|
99
|
+
fields[name] = { type: 'oauth', value: await refreshOAuth(field.value, now) }
|
|
100
|
+
changed = true
|
|
101
|
+
}
|
|
102
|
+
break
|
|
103
|
+
case 'secret':
|
|
104
|
+
case 'text':
|
|
105
|
+
case 'totp':
|
|
106
|
+
break
|
|
107
|
+
default:
|
|
108
|
+
throw new TypeVaultError('invalid', `unexpected field ${JSON.stringify(field satisfies never)}`)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} catch (error) {
|
|
112
|
+
// A provider may have rotated an earlier field's refresh token already: store it before failing.
|
|
113
|
+
if (changed) await this.#storeRefreshed(entry, fields)
|
|
114
|
+
throw error
|
|
115
|
+
}
|
|
116
|
+
return changed ? this.#storeRefreshed(entry, fields) : entry
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async #storeRefreshed(entry: StoredEntry, fields: Readonly<Record<string, Field>>): Promise<StoredEntry> {
|
|
120
|
+
const { name, env, origin, tags } = entry.payload
|
|
93
121
|
const input = {
|
|
94
122
|
name,
|
|
95
|
-
type,
|
|
96
123
|
fields,
|
|
97
|
-
|
|
124
|
+
tier: entry.envelope.tier,
|
|
98
125
|
tags,
|
|
99
126
|
...(env === undefined ? {} : { env }),
|
|
100
127
|
...(origin === undefined ? {} : { origin }),
|
|
101
128
|
}
|
|
102
|
-
const refreshed = await this.#vault.put(input, await this.#signerFor(entry.envelope.
|
|
129
|
+
const refreshed = await this.#vault.put(input, await this.#signerFor(entry.envelope.tier))
|
|
103
130
|
await this.#audit({ action: 'refresh', entries: [name] })
|
|
104
131
|
return refreshed
|
|
105
132
|
}
|
|
106
133
|
|
|
107
134
|
#remember(value: EntryValue): EntryValue {
|
|
108
|
-
for (const
|
|
135
|
+
for (const secret of secretValues([value])) this.#handedOut.add(secret)
|
|
109
136
|
return value
|
|
110
137
|
}
|
|
111
138
|
|
|
@@ -124,7 +151,7 @@ export class ScopedVault implements Access {
|
|
|
124
151
|
const fresh = await Promise.all(picked.map((entry) => this.#fresh(entry)))
|
|
125
152
|
const names = fresh.map((entry) => entry.payload.name)
|
|
126
153
|
await this.#audit({ action: 'read', entries: names, ...(reason === undefined ? {} : { reason }) })
|
|
127
|
-
const own = this.#identity.
|
|
154
|
+
const own = this.#identity.ownTiers ?? []
|
|
128
155
|
return fresh.map((entry) =>
|
|
129
156
|
this.#remember({
|
|
130
157
|
...valueOf(entry),
|
|
@@ -133,14 +160,34 @@ export class ScopedVault implements Access {
|
|
|
133
160
|
)
|
|
134
161
|
}
|
|
135
162
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
163
|
+
// The tier is chosen under the same exclusive lock as the write, from freshly read state, so a tier another
|
|
164
|
+
// process adds in between makes the choice ambiguous instead of silently picking the old only tier.
|
|
165
|
+
set(input: SetInput): Promise<EntrySummary> {
|
|
166
|
+
return withLock(this.#vault.layout.lockFile, 'exclusive', async () => {
|
|
167
|
+
this.#vault.refresh()
|
|
168
|
+
return this.#write(input, input.tier ?? this.#identity.defaultTier ?? (await this.#onlyWritableTier()))
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Only infer when there is exactly one answer: the tier decides who can read the secret. */
|
|
173
|
+
async #onlyWritableTier(): Promise<string> {
|
|
174
|
+
const { state, keyring } = this.#vault
|
|
175
|
+
const writable = [...(await keyring.reachableTiers())].filter((name) => allowsTier(this.#chain, state, name))
|
|
176
|
+
const [only, ...others] = writable
|
|
177
|
+
if (only === undefined) {
|
|
178
|
+
throw new TypeVaultError('invalid', 'no tier to write to yet: create one with `typevault tier add <name>`')
|
|
179
|
+
}
|
|
180
|
+
if (others.length === 0) return only
|
|
181
|
+
const viaEnv = this.#identity.mode === 'direct' ? ' or set TYPEVAULT_TIER' : ''
|
|
182
|
+
throw new TypeVaultError('invalid', `choose a tier with --tier (one of: ${writable.join(', ')})${viaEnv}`)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async #write(input: SetInput, tier: string): Promise<EntrySummary> {
|
|
139
186
|
if (
|
|
140
|
-
!
|
|
141
|
-
!allowsEntry(this.#chain, this.#vault.state,
|
|
187
|
+
!allowsTier(this.#chain, this.#vault.state, tier) ||
|
|
188
|
+
!allowsEntry(this.#chain, this.#vault.state, tier, input.name)
|
|
142
189
|
) {
|
|
143
|
-
throw new TypeVaultError('denied', `this grant cannot write ${input.name} in
|
|
190
|
+
throw new TypeVaultError('denied', `this grant cannot write ${input.name} in tier "${tier}"`)
|
|
144
191
|
}
|
|
145
192
|
const existing = await this.#vault.find(input.name)
|
|
146
193
|
if (existing !== undefined && !this.#allows(existing)) {
|
|
@@ -148,34 +195,43 @@ export class ScopedVault implements Access {
|
|
|
148
195
|
}
|
|
149
196
|
const { ifRev } = input
|
|
150
197
|
const stored = await this.#vault.put(
|
|
151
|
-
entryInputOf(input,
|
|
152
|
-
await this.#signerFor(
|
|
198
|
+
entryInputOf(input, tier),
|
|
199
|
+
await this.#signerFor(tier),
|
|
153
200
|
ifRev === undefined ? {} : { ifRev },
|
|
154
201
|
)
|
|
155
|
-
await this.#audit({ action: 'write', entries: [input.name],
|
|
202
|
+
await this.#audit({ action: 'write', entries: [input.name], tiers: [tier] })
|
|
156
203
|
return toSummary(valueOf(stored))
|
|
157
204
|
}
|
|
158
205
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
206
|
+
// The name is resolved once and the delete targets that copy's id: resolving it again could land on a copy that
|
|
207
|
+
// arrived in between (another process, or a git pull, which takes no lock), say in a tier above this scope.
|
|
208
|
+
remove(name: EntryName, tier?: string): Promise<void> {
|
|
209
|
+
return withLock(this.#vault.layout.lockFile, 'exclusive', async () => {
|
|
210
|
+
let entry: StoredEntry | undefined
|
|
211
|
+
if (tier === undefined) entry = await this.#find(name)
|
|
212
|
+
else {
|
|
213
|
+
const { entries } = await this.#vault.scan()
|
|
214
|
+
entry = entries.find((copy) => copy.payload.name === name && copy.envelope.tier === tier)
|
|
215
|
+
if (entry === undefined || !this.#allows(entry)) throw new TypeVaultError('not_found', `no entry named ${name}`)
|
|
216
|
+
}
|
|
217
|
+
await this.#vault.removeById(entry.envelope.id, (envelope) =>
|
|
218
|
+
allowsEntry(this.#chain, this.#vault.state, envelope.tier, name),
|
|
219
|
+
)
|
|
220
|
+
await this.#audit({ action: 'remove', entries: [name], ...(tier === undefined ? {} : { tiers: [tier] }) })
|
|
221
|
+
})
|
|
166
222
|
}
|
|
167
223
|
|
|
168
224
|
async move(from: EntryName, to: EntryName): Promise<void> {
|
|
169
225
|
const entry = await this.#find(from)
|
|
170
|
-
if (!allowsEntry(this.#chain, this.#vault.state, entry.envelope.
|
|
226
|
+
if (!allowsEntry(this.#chain, this.#vault.state, entry.envelope.tier, to)) {
|
|
171
227
|
throw new TypeVaultError('denied', `this grant cannot use the name ${to}`)
|
|
172
228
|
}
|
|
173
|
-
await this.#vault.move(from, to, await this.#signerFor(entry.envelope.
|
|
229
|
+
await this.#vault.move(from, to, await this.#signerFor(entry.envelope.tier))
|
|
174
230
|
await this.#audit({ action: 'move', entries: [from, to] })
|
|
175
231
|
}
|
|
176
232
|
|
|
177
|
-
async
|
|
178
|
-
return this.#identity.
|
|
233
|
+
async ownTiers(): Promise<readonly string[]> {
|
|
234
|
+
return this.#identity.ownTiers ?? []
|
|
179
235
|
}
|
|
180
236
|
|
|
181
237
|
async readValues(): Promise<readonly string[]> {
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs'
|
|
2
2
|
|
|
3
3
|
import { appendAudit, type AuditEvent } from '../audit/log'
|
|
4
|
+
import { signerAuthority } from '../core/entries'
|
|
4
5
|
import { TypeVaultError } from '../core/errors'
|
|
5
|
-
import {
|
|
6
|
+
import type { Layout } from '../core/layout'
|
|
6
7
|
import { type EntryName, matchesOnly } from '../core/naming'
|
|
7
8
|
import { Vault } from '../core/vault'
|
|
9
|
+
import { secretValues } from './env'
|
|
8
10
|
import { allowsEntry } from './scope'
|
|
9
11
|
import { type SealedBundle, sealedSigningSecret } from './sealed'
|
|
10
12
|
import { type Access, type EntrySummary, type EntryValue, entryInputOf, type SetInput, toSummary } from './types'
|
|
@@ -17,12 +19,13 @@ type Env = Readonly<Record<string, string | undefined>>
|
|
|
17
19
|
*/
|
|
18
20
|
export class SealedAccess implements Access {
|
|
19
21
|
readonly #bundle: SealedBundle
|
|
20
|
-
readonly #
|
|
22
|
+
readonly #layout: Layout | undefined
|
|
21
23
|
readonly #env: Env
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
/** `layout` is undefined where no vault is selected: reads still work, writes refuse. */
|
|
26
|
+
constructor(bundle: SealedBundle, layout: Layout | undefined, env: Env) {
|
|
24
27
|
this.#bundle = bundle
|
|
25
|
-
this.#
|
|
28
|
+
this.#layout = layout
|
|
26
29
|
this.#env = env
|
|
27
30
|
}
|
|
28
31
|
|
|
@@ -38,16 +41,16 @@ export class SealedAccess implements Access {
|
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
async #vault(): Promise<Vault> {
|
|
41
|
-
if (!existsSync(
|
|
42
|
-
throw new TypeVaultError('unavailable', 'writing with a sealed grant needs the vault directory
|
|
44
|
+
if (this.#layout === undefined || !existsSync(this.#layout.ownerFile)) {
|
|
45
|
+
throw new TypeVaultError('unavailable', 'writing with a sealed grant needs the vault directory')
|
|
43
46
|
}
|
|
44
|
-
return Vault.open(this.#
|
|
47
|
+
return Vault.open(this.#layout, [], this.#env)
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
/** Best effort: a sealed grant may run where no vault directory exists. */
|
|
48
51
|
async #audit(event: Omit<AuditEvent, 'mode' | 'grant' | 'label'>): Promise<void> {
|
|
49
|
-
const layout =
|
|
50
|
-
if (!existsSync(layout.
|
|
52
|
+
const layout = this.#layout
|
|
53
|
+
if (layout === undefined || !existsSync(layout.ownerFile)) return
|
|
51
54
|
await appendAudit(layout, { ...event, mode: 'sealed', grant: this.#bundle.id, label: this.#bundle.label })
|
|
52
55
|
}
|
|
53
56
|
|
|
@@ -74,23 +77,26 @@ export class SealedAccess implements Access {
|
|
|
74
77
|
async set(input: SetInput): Promise<EntrySummary> {
|
|
75
78
|
const bundle = this.#live()
|
|
76
79
|
const vault = await this.#vault()
|
|
77
|
-
const group = input.group ?? bundle.scope.groups[0]
|
|
78
|
-
if (group === undefined || !allowsEntry([bundle.scope], vault.state, group, input.name)) {
|
|
79
|
-
throw new TypeVaultError('denied', `this grant cannot write ${input.name} in group "${group ?? '?'}"`)
|
|
80
|
-
}
|
|
81
80
|
const prior = bundle.entries.find((value) => value.name === input.name)
|
|
81
|
+
const tier = input.tier ?? prior?.tier ?? bundle.scope.tiers[0]
|
|
82
|
+
if (tier === undefined || !allowsEntry([bundle.scope], vault.state, tier, input.name)) {
|
|
83
|
+
throw new TypeVaultError('denied', `this grant cannot write ${input.name} in tier "${tier ?? '?'}"`)
|
|
84
|
+
}
|
|
85
|
+
// Blind writes can't remove the old copy (that takes reading it), so moving tiers would leave two.
|
|
86
|
+
if (prior !== undefined && prior.tier !== tier) {
|
|
87
|
+
throw new TypeVaultError('denied', `${input.name} is in tier "${prior.tier}"; a sealed grant cannot move it`)
|
|
88
|
+
}
|
|
82
89
|
const { ifRev } = input
|
|
83
90
|
const stored = await vault.put(
|
|
84
|
-
entryInputOf(input,
|
|
91
|
+
entryInputOf(input, tier),
|
|
85
92
|
{ ref: { kind: 'cert', cert: bundle.write.cert }, signSecret: sealedSigningSecret(bundle) },
|
|
86
93
|
{ ...(ifRev === undefined ? {} : { ifRev }), ...(prior === undefined ? {} : { base: prior }) },
|
|
87
94
|
)
|
|
88
|
-
await this.#audit({ action: 'write', entries: [input.name],
|
|
95
|
+
await this.#audit({ action: 'write', entries: [input.name], tiers: [tier] })
|
|
89
96
|
const { envelope, payload } = stored
|
|
90
97
|
return {
|
|
91
98
|
name: payload.name,
|
|
92
|
-
|
|
93
|
-
group,
|
|
99
|
+
tier,
|
|
94
100
|
rev: envelope.rev,
|
|
95
101
|
tags: payload.tags,
|
|
96
102
|
rotated: payload.rotated,
|
|
@@ -98,23 +104,39 @@ export class SealedAccess implements Access {
|
|
|
98
104
|
}
|
|
99
105
|
}
|
|
100
106
|
|
|
101
|
-
|
|
107
|
+
/**
|
|
108
|
+
* The bundle is holder-editable (whoever holds the grant can re-seal it), so only the issuer-signed
|
|
109
|
+
* certificate bounds what may be deleted: its tiers, `--only` and expiry, checked against the file on disk.
|
|
110
|
+
* The entry must still be the revision this grant carried (of an id its certificate lists), or one this grant's
|
|
111
|
+
* certificate wrote since.
|
|
112
|
+
*/
|
|
113
|
+
async remove(name: EntryName, _tier?: string): Promise<void> {
|
|
102
114
|
const entry = this.#entry(name)
|
|
103
|
-
|
|
115
|
+
const { cert } = this.#bundle.write
|
|
116
|
+
const vault = await this.#vault()
|
|
117
|
+
await vault.removeById(
|
|
118
|
+
entry.id,
|
|
119
|
+
(envelope) =>
|
|
120
|
+
envelope.tier === entry.tier &&
|
|
121
|
+
((cert.ids.includes(envelope.id) && envelope.rev === entry.rev) ||
|
|
122
|
+
(envelope.signer.kind === 'cert' && envelope.signer.cert.signPublic === cert.signPublic)) &&
|
|
123
|
+
allowsEntry([{ tiers: cert.tiers, only: cert.only }], vault.state, envelope.tier, name) &&
|
|
124
|
+
signerAuthority({ kind: 'cert', cert }, envelope.tier, vault.state, new Date().toISOString()) !== undefined,
|
|
125
|
+
)
|
|
104
126
|
await this.#audit({ action: 'remove', entries: [name] })
|
|
105
127
|
}
|
|
106
128
|
|
|
107
129
|
async move(from: EntryName, to: EntryName): Promise<void> {
|
|
108
130
|
const entry = this.#entry(from)
|
|
109
|
-
await this.set({ name: to,
|
|
131
|
+
await this.set({ name: to, fields: entry.fields, tier: entry.tier, tags: entry.tags })
|
|
110
132
|
await this.remove(from)
|
|
111
133
|
}
|
|
112
134
|
|
|
113
|
-
async
|
|
114
|
-
return this.#bundle.scope.
|
|
135
|
+
async ownTiers(): Promise<readonly string[]> {
|
|
136
|
+
return this.#bundle.scope.tiers
|
|
115
137
|
}
|
|
116
138
|
|
|
117
139
|
async readValues(): Promise<readonly string[]> {
|
|
118
|
-
return this.#bundle.entries
|
|
140
|
+
return secretValues(this.#bundle.entries)
|
|
119
141
|
}
|
|
120
142
|
}
|
package/src/access/sealed.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { generateX25519Identity, identityToRecipient } from 'age-encryption'
|
|
|
4
4
|
import { z } from 'zod'
|
|
5
5
|
|
|
6
6
|
import { appendAudit } from '../audit/log'
|
|
7
|
-
import { generateSigningKey, randomHex, seal, unseal } from '../core/crypto'
|
|
7
|
+
import { generateSigningKey, randomHex, seal, type SecretKey, unseal } from '../core/crypto'
|
|
8
8
|
import { fromBase64, fromBase64Url, fromUtf8, toBase64, toBase64Url, utf8 } from '../core/encoding'
|
|
9
9
|
import { CertSchema, issueCert, signerOf } from '../core/entries'
|
|
10
10
|
import { TypeVaultError } from '../core/errors'
|
|
@@ -20,7 +20,7 @@ const BundleSchema = z.object({
|
|
|
20
20
|
v: z.literal(1),
|
|
21
21
|
id: z.string(),
|
|
22
22
|
label: z.string(),
|
|
23
|
-
|
|
23
|
+
tiers: z.array(z.string()),
|
|
24
24
|
only: z.array(z.string()),
|
|
25
25
|
exp: z.iso.datetime(),
|
|
26
26
|
entries: z.array(EntryValueSchema),
|
|
@@ -40,33 +40,34 @@ export type SealedRequest = GrantScope & { readonly ttlMs: number; readonly labe
|
|
|
40
40
|
|
|
41
41
|
/** Re-seals every entry the scope allows into a self-contained grant. The issuer's own key never leaves. */
|
|
42
42
|
export async function issueSealedGrant(vault: Vault, request: SealedRequest): Promise<string> {
|
|
43
|
-
const issuer =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return role.kind === 'group' && request.groups.every((group) => reach(vault.state.groups, role.group).has(group))
|
|
48
|
-
})
|
|
49
|
-
if (issuer === undefined) throw new TypeVaultError('denied', 'no key here reaches every requested group')
|
|
50
|
-
for (const group of request.groups) {
|
|
51
|
-
if (vault.state.groups[group] === undefined) throw new TypeVaultError('not_found', `no group "${group}"`)
|
|
43
|
+
const issuer = vault.keyring.owner ?? (await memberTierKey(vault, request.tiers))
|
|
44
|
+
if (issuer === undefined) throw new TypeVaultError('denied', 'no key here reaches every requested tier')
|
|
45
|
+
for (const tier of request.tiers) {
|
|
46
|
+
if (vault.state.tiers[tier] === undefined) throw new TypeVaultError('not_found', `no tier "${tier}"`)
|
|
52
47
|
}
|
|
53
|
-
const chain = [{
|
|
48
|
+
const chain = [{ tiers: request.tiers, only: request.only }]
|
|
54
49
|
const { entries } = await vault.scan()
|
|
55
|
-
const allowed = entries.filter((entry) => allowsEntry(chain, vault.state, entry.envelope.
|
|
50
|
+
const allowed = entries.filter((entry) => allowsEntry(chain, vault.state, entry.envelope.tier, entry.payload.name))
|
|
56
51
|
const granted = [...Map.groupBy(allowed, (entry) => entry.payload.name).values()]
|
|
57
|
-
.flatMap((copies) => resolveName(vault.state.
|
|
52
|
+
.flatMap((copies) => resolveName(vault.state.tiers, copies) ?? [])
|
|
58
53
|
.map((entry) => ({
|
|
59
|
-
...toValue(entry.envelope.
|
|
60
|
-
trustedWriter: writtenFromAbove(entry.envelope.signer, vault.state, request.
|
|
54
|
+
...toValue(entry.envelope.tier, entry.envelope.id, entry.envelope.rev, entry.payload),
|
|
55
|
+
trustedWriter: writtenFromAbove(entry.envelope.signer, vault.state, request.tiers),
|
|
61
56
|
}))
|
|
62
57
|
const id = randomHex(8)
|
|
63
58
|
const exp = new Date(Date.now() + request.ttlMs).toISOString()
|
|
64
59
|
const signing = generateSigningKey()
|
|
65
60
|
const cert = issueCert(
|
|
66
|
-
{
|
|
61
|
+
{
|
|
62
|
+
signPublic: signing.signPublic,
|
|
63
|
+
tiers: [...request.tiers],
|
|
64
|
+
only: [...request.only],
|
|
65
|
+
ids: granted.map((value) => value.id),
|
|
66
|
+
exp,
|
|
67
|
+
},
|
|
67
68
|
signerOf(issuer),
|
|
68
69
|
)
|
|
69
|
-
const bundle = { v: 1, id, label: request.label,
|
|
70
|
+
const bundle = { v: 1, id, label: request.label, tiers: request.tiers, only: request.only, exp, entries: granted }
|
|
70
71
|
const write = { signSecret: toBase64(signing.signSecret), cert }
|
|
71
72
|
const identity = await generateX25519Identity()
|
|
72
73
|
const sealed = await seal([await identityToRecipient(identity)], utf8(JSON.stringify({ ...bundle, write })))
|
|
@@ -75,12 +76,23 @@ export async function issueSealedGrant(vault: Vault, request: SealedRequest): Pr
|
|
|
75
76
|
mode: 'sealed',
|
|
76
77
|
grant: id,
|
|
77
78
|
label: request.label,
|
|
78
|
-
|
|
79
|
+
tiers: request.tiers,
|
|
79
80
|
entries: granted.map((value) => value.name),
|
|
80
81
|
})
|
|
81
82
|
return `${PREFIX}${toBase64Url(utf8(identity))}.${toBase64Url(sealed)}`
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
/** The key of a tier this member holds that reaches every one of `tiers`, to sign the grant's certificate. */
|
|
86
|
+
async function memberTierKey(vault: Vault, tiers: readonly string[]): Promise<SecretKey | undefined> {
|
|
87
|
+
const held = vault.member === undefined ? [] : (vault.state.members[vault.member]?.tiers ?? [])
|
|
88
|
+
for (const tier of held) {
|
|
89
|
+
if (!tiers.every((wanted) => reach(vault.state.tiers, tier).has(wanted))) continue
|
|
90
|
+
const key = await vault.keyring.unlock(tier)
|
|
91
|
+
if (key !== undefined) return key
|
|
92
|
+
}
|
|
93
|
+
return undefined
|
|
94
|
+
}
|
|
95
|
+
|
|
84
96
|
/** Opens `tvg1:...` or `@/path/to/file` holding one. */
|
|
85
97
|
export async function readSealedGrant(raw: string): Promise<SealedBundle> {
|
|
86
98
|
const text = (raw.startsWith('@') ? readFileSync(raw.slice(1), 'utf8') : raw).trim()
|
|
@@ -91,10 +103,10 @@ export async function readSealedGrant(raw: string): Promise<SealedBundle> {
|
|
|
91
103
|
const plaintext = await unseal([fromUtf8(fromBase64Url(identity))], fromBase64Url(ciphertext))
|
|
92
104
|
const parsed = BundleSchema.safeParse(JSON.parse(fromUtf8(plaintext)))
|
|
93
105
|
if (!parsed.success) throw new TypeVaultError('invalid', 'malformed sealed grant')
|
|
94
|
-
const {
|
|
106
|
+
const { tiers, only, entries, ...rest } = parsed.data
|
|
95
107
|
return {
|
|
96
108
|
...rest,
|
|
97
|
-
scope: {
|
|
109
|
+
scope: { tiers, only },
|
|
98
110
|
entries: entries.map(toEntryValue),
|
|
99
111
|
}
|
|
100
112
|
}
|
package/src/access/types.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import type { SignerRef } from '../core/entries'
|
|
2
2
|
import { TypeVaultError } from '../core/errors'
|
|
3
|
-
import { type EntryName, type
|
|
3
|
+
import { type EntryName, type Field, isReservedEnvName } from '../core/naming'
|
|
4
4
|
import type { EntryPayload } from '../core/payload'
|
|
5
|
-
import {
|
|
5
|
+
import { reachesTier, type VaultState } from '../core/state'
|
|
6
6
|
|
|
7
7
|
export type EntrySummary = {
|
|
8
8
|
readonly name: EntryName
|
|
9
|
-
readonly
|
|
10
|
-
readonly group: string
|
|
9
|
+
readonly tier: string
|
|
11
10
|
readonly rev: number
|
|
12
11
|
readonly tags: readonly string[]
|
|
13
12
|
readonly rotated: string
|
|
@@ -17,18 +16,17 @@ export type EntrySummary = {
|
|
|
17
16
|
export type EntryValue = EntrySummary & {
|
|
18
17
|
readonly id: string
|
|
19
18
|
readonly created: string
|
|
20
|
-
readonly fields: Readonly<Record<string,
|
|
19
|
+
readonly fields: Readonly<Record<string, Field>>
|
|
21
20
|
readonly env?: string
|
|
22
21
|
readonly origin?: string
|
|
23
|
-
/** Written by the
|
|
22
|
+
/** Written by the owner or by a key that reaches all of the reader's own tiers: its `--env` override counts. */
|
|
24
23
|
readonly trustedWriter?: boolean
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
export type SetInput = {
|
|
28
27
|
readonly name: EntryName
|
|
29
|
-
readonly
|
|
30
|
-
readonly
|
|
31
|
-
readonly group?: string | undefined
|
|
28
|
+
readonly fields: Readonly<Record<string, Field>>
|
|
29
|
+
readonly tier?: string | undefined
|
|
32
30
|
readonly env?: string | undefined
|
|
33
31
|
readonly origin?: string | undefined
|
|
34
32
|
readonly tags?: readonly string[] | undefined
|
|
@@ -36,15 +34,14 @@ export type SetInput = {
|
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
/** The vault-level input for a write, without the optional keys a parsed request leaves undefined. */
|
|
39
|
-
export function entryInputOf(input: SetInput,
|
|
37
|
+
export function entryInputOf(input: SetInput, tier: string) {
|
|
40
38
|
if (input.env !== undefined && isReservedEnvName(input.env)) {
|
|
41
39
|
throw new TypeVaultError('invalid', `${input.env} is reserved: run never sets it from the vault`)
|
|
42
40
|
}
|
|
43
41
|
return {
|
|
44
42
|
name: input.name,
|
|
45
|
-
type: input.type,
|
|
46
43
|
fields: input.fields,
|
|
47
|
-
|
|
44
|
+
tier,
|
|
48
45
|
...(input.env === undefined ? {} : { env: input.env }),
|
|
49
46
|
...(input.origin === undefined ? {} : { origin: input.origin }),
|
|
50
47
|
...(input.tags === undefined ? {} : { tags: input.tags }),
|
|
@@ -57,21 +54,20 @@ export interface Access {
|
|
|
57
54
|
get(name: EntryName, reason?: string): Promise<EntryValue>
|
|
58
55
|
values(only: readonly string[], reason?: string): Promise<readonly EntryValue[]>
|
|
59
56
|
set(input: SetInput): Promise<EntrySummary>
|
|
60
|
-
/** `
|
|
61
|
-
remove(name: EntryName,
|
|
57
|
+
/** `tier` picks one copy when a name exists in unrelated tiers. */
|
|
58
|
+
remove(name: EntryName, tier?: string): Promise<void>
|
|
62
59
|
move(from: EntryName, to: EntryName): Promise<void>
|
|
63
|
-
/** The
|
|
64
|
-
|
|
60
|
+
/** The tiers this caller holds directly (not through includes): what `run` loads in bulk. */
|
|
61
|
+
ownTiers(): Promise<readonly string[]>
|
|
65
62
|
/** Every secret value this grant has handed out so far, for redaction. */
|
|
66
63
|
readValues(): Promise<readonly string[]>
|
|
67
64
|
}
|
|
68
65
|
|
|
69
|
-
export function toValue(
|
|
66
|
+
export function toValue(tier: string, id: string, rev: number, payload: EntryPayload): EntryValue {
|
|
70
67
|
return {
|
|
71
68
|
id,
|
|
72
69
|
name: payload.name,
|
|
73
|
-
|
|
74
|
-
group,
|
|
70
|
+
tier,
|
|
75
71
|
rev,
|
|
76
72
|
tags: payload.tags,
|
|
77
73
|
created: payload.created,
|
|
@@ -85,22 +81,21 @@ export function toValue(group: string, id: string, rev: number, payload: EntryPa
|
|
|
85
81
|
|
|
86
82
|
export const toSummary = (value: EntryValue): EntrySummary => ({
|
|
87
83
|
name: value.name,
|
|
88
|
-
|
|
89
|
-
group: value.group,
|
|
84
|
+
tier: value.tier,
|
|
90
85
|
rev: value.rev,
|
|
91
86
|
tags: value.tags,
|
|
92
87
|
rotated: value.rotated,
|
|
93
88
|
needsRotation: value.needsRotation,
|
|
94
89
|
})
|
|
95
90
|
|
|
96
|
-
/** Whether `signer` is at least as privileged as a reader holding `
|
|
97
|
-
export function writtenFromAbove(signer: SignerRef, state: VaultState,
|
|
98
|
-
if (signer.kind === '
|
|
99
|
-
const
|
|
100
|
-
signer.kind === '
|
|
101
|
-
? [signer.
|
|
91
|
+
/** Whether `signer` is at least as privileged as a reader holding `ownTiers`. */
|
|
92
|
+
export function writtenFromAbove(signer: SignerRef, state: VaultState, ownTiers: readonly string[]): boolean {
|
|
93
|
+
if (signer.kind === 'owner') return true
|
|
94
|
+
const tiers =
|
|
95
|
+
signer.kind === 'tier'
|
|
96
|
+
? [signer.tier]
|
|
102
97
|
: signer.kind === 'signer'
|
|
103
|
-
? (state.signers[signer.id]?.
|
|
104
|
-
: signer.cert.
|
|
105
|
-
return
|
|
98
|
+
? (state.signers[signer.id]?.tiers ?? [])
|
|
99
|
+
: signer.cert.tiers
|
|
100
|
+
return ownTiers.length > 0 && ownTiers.every((own) => reachesTier(state.tiers, tiers, own))
|
|
106
101
|
}
|