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/core/admin.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { encodeSecretKey, generateSecretKey, publicKeyOf, seal, type SecretKey } from './crypto'
|
|
1
|
+
import { encodeSecretKey, generateSecretKey, type PublicKey, publicKeyOf, seal, type SecretKey } from './crypto'
|
|
2
2
|
import { utf8 } from './encoding'
|
|
3
3
|
import { signerOf } from './entries'
|
|
4
4
|
import { TypeVaultError } from './errors'
|
|
5
|
-
import { wrapPath } from './keyring'
|
|
5
|
+
import { ownerWrapPath, wrapPath } from './keyring'
|
|
6
6
|
import { withLock } from './lock'
|
|
7
|
-
import {
|
|
7
|
+
import { rotateTiers } from './rotate'
|
|
8
8
|
import {
|
|
9
9
|
ceilingsDoc,
|
|
10
|
-
type
|
|
11
|
-
|
|
10
|
+
type MemberKind,
|
|
11
|
+
membersDoc,
|
|
12
|
+
type Tier,
|
|
13
|
+
tiersDoc,
|
|
12
14
|
includersOf,
|
|
13
|
-
|
|
15
|
+
parseMemberName,
|
|
16
|
+
parseTierName,
|
|
14
17
|
reach,
|
|
15
18
|
signersDoc,
|
|
16
19
|
type VaultState,
|
|
@@ -18,16 +21,26 @@ import {
|
|
|
18
21
|
import { commitTxn, Txn } from './txn'
|
|
19
22
|
import type { Vault } from './vault'
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
export type NewMember = PublicKey & {
|
|
25
|
+
readonly kind: MemberKind
|
|
26
|
+
readonly owner: boolean
|
|
27
|
+
readonly tiers: readonly string[]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Every tier `tiers` reach: rotating all of them keeps old generations out of a member's hands. */
|
|
31
|
+
const reachOf = (state: VaultState, tiers: readonly string[]): readonly string[] =>
|
|
32
|
+
[...new Set(tiers.flatMap((tier) => [...reach(state.tiers, tier)]))].sort()
|
|
33
|
+
|
|
34
|
+
/** Owner-only changes to tiers, members, signers and ceilings. Each one lands as a single transaction. */
|
|
22
35
|
export class Admin {
|
|
23
36
|
readonly #vault: Vault
|
|
24
|
-
readonly #
|
|
37
|
+
readonly #owner: SecretKey
|
|
25
38
|
|
|
26
39
|
constructor(vault: Vault) {
|
|
27
|
-
const
|
|
28
|
-
if (
|
|
40
|
+
const owner = vault.keyring.owner
|
|
41
|
+
if (owner === undefined) throw new TypeVaultError('denied', 'this needs the owner key')
|
|
29
42
|
this.#vault = vault
|
|
30
|
-
this.#
|
|
43
|
+
this.#owner = owner
|
|
31
44
|
}
|
|
32
45
|
|
|
33
46
|
#mutate(build: (state: VaultState, txn: Txn) => Promise<void>): Promise<void> {
|
|
@@ -40,92 +53,136 @@ export class Admin {
|
|
|
40
53
|
})
|
|
41
54
|
}
|
|
42
55
|
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const meta = state.groups[included]
|
|
46
|
-
if (key === undefined || meta === undefined)
|
|
47
|
-
throw new TypeVaultError('not_found', `cannot unlock group "${included}"`)
|
|
48
|
-
const sealed = await seal([holder.group.recipient], utf8(encodeSecretKey(key)))
|
|
49
|
-
txn.put(wrapPath(included, meta.gen, { kind: 'group', group: holder.name, gen: holder.group.gen }), sealed)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
addGroup(rawName: string): Promise<void> {
|
|
53
|
-
const name = parseGroupName(rawName)
|
|
56
|
+
addTier(rawName: string): Promise<void> {
|
|
57
|
+
const name = parseTierName(rawName)
|
|
54
58
|
return this.#mutate(async (state, txn) => {
|
|
55
|
-
if (state.
|
|
56
|
-
const key = await generateSecretKey({ kind: '
|
|
57
|
-
const
|
|
58
|
-
txn.put(
|
|
59
|
-
|
|
60
|
-
await seal([state.operator.recipient], utf8(encodeSecretKey(key))),
|
|
61
|
-
)
|
|
62
|
-
txn.put('groups.json', groupsDoc({ ...state.groups, [name]: group }, this.#operator, state.seqs.groups + 1))
|
|
59
|
+
if (state.tiers[name] !== undefined) throw new TypeVaultError('conflict', `tier "${name}" already exists`)
|
|
60
|
+
const key = await generateSecretKey({ kind: 'tier', tier: name, gen: 1 })
|
|
61
|
+
const tier: Tier = { includes: [], gen: 1, ...(await publicKeyOf(key)) }
|
|
62
|
+
txn.put(wrapPath(name, 1, { kind: 'owner' }), await seal([state.owner.recipient], utf8(encodeSecretKey(key))))
|
|
63
|
+
txn.put('tiers.json', tiersDoc({ ...state.tiers, [name]: tier }, this.#owner, state.seqs.tiers + 1))
|
|
63
64
|
})
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
|
|
67
|
+
removeTier(name: string): Promise<void> {
|
|
67
68
|
return this.#mutate(async (state, txn) => {
|
|
68
|
-
if (state.
|
|
69
|
-
if (includersOf(state.
|
|
70
|
-
throw new TypeVaultError('conflict', `other
|
|
69
|
+
if (state.tiers[name] === undefined) throw new TypeVaultError('not_found', `no tier "${name}"`)
|
|
70
|
+
if (includersOf(state.tiers, name).length > 0)
|
|
71
|
+
throw new TypeVaultError('conflict', `other tiers include "${name}"`)
|
|
72
|
+
const holders = Object.entries(state.members).filter(([, member]) => member.tiers.includes(name))
|
|
73
|
+
if (holders.length > 0) {
|
|
74
|
+
const names = holders.map(([member]) => member).join(', ')
|
|
75
|
+
throw new TypeVaultError('conflict', `members still hold "${name}": ${names}`)
|
|
76
|
+
}
|
|
71
77
|
const { entries } = await this.#vault.scan()
|
|
72
|
-
if (entries.some((entry) => entry.envelope.
|
|
73
|
-
throw new TypeVaultError('conflict', `
|
|
78
|
+
if (entries.some((entry) => entry.envelope.tier === name)) {
|
|
79
|
+
throw new TypeVaultError('conflict', `tier "${name}" still has entries`)
|
|
74
80
|
}
|
|
75
|
-
const { [name]: _removed, ...rest } = state.
|
|
76
|
-
for (const included of state.
|
|
77
|
-
const gen = state.
|
|
78
|
-
txn.remove(wrapPath(included, gen, { kind: '
|
|
81
|
+
const { [name]: _removed, ...rest } = state.tiers
|
|
82
|
+
for (const included of state.tiers[name]?.includes ?? []) {
|
|
83
|
+
const gen = state.tiers[included]?.gen ?? 0
|
|
84
|
+
txn.remove(wrapPath(included, gen, { kind: 'tier', tier: name, gen: state.tiers[name]?.gen ?? 0 }))
|
|
79
85
|
}
|
|
80
|
-
txn.remove(`
|
|
81
|
-
txn.put('
|
|
86
|
+
txn.remove(`tiers/${name}`)
|
|
87
|
+
txn.put('tiers.json', tiersDoc(rest, this.#owner, state.seqs.tiers + 1))
|
|
82
88
|
})
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Makes `tier` include `included`, rotating every tier `included` reaches in the same transaction, so the includer
|
|
93
|
+
* can open neither their older ciphertext in git history nor entries deleted before the include.
|
|
94
|
+
*/
|
|
95
|
+
include(tier: string, included: string): Promise<void> {
|
|
86
96
|
return this.#mutate(async (state, txn) => {
|
|
87
|
-
const holder = state.
|
|
88
|
-
if (holder === undefined || state.
|
|
89
|
-
throw new TypeVaultError('not_found', 'no such
|
|
97
|
+
const holder = state.tiers[tier]
|
|
98
|
+
if (holder === undefined || state.tiers[included] === undefined)
|
|
99
|
+
throw new TypeVaultError('not_found', 'no such tier')
|
|
90
100
|
if (holder.includes.includes(included)) return
|
|
91
|
-
if (reach(state.
|
|
92
|
-
throw new TypeVaultError('invalid', `"${included}" already reaches "${
|
|
101
|
+
if (reach(state.tiers, included).has(tier)) {
|
|
102
|
+
throw new TypeVaultError('invalid', `"${included}" already reaches "${tier}"; including it would form a cycle`)
|
|
93
103
|
}
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
|
|
104
|
+
const tiers = { ...state.tiers, [tier]: { ...holder, includes: [...holder.includes, included] } }
|
|
105
|
+
const context = { vault: this.#vault, owner: this.#owner, state: { ...state, tiers }, txn, exposed: false }
|
|
106
|
+
await rotateTiers(context, reachOf(state, [included]))
|
|
97
107
|
})
|
|
98
108
|
}
|
|
99
109
|
|
|
100
|
-
/**
|
|
101
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Drops an include and rotates the included tier in one transaction: a crash leaves both or neither, never a
|
|
112
|
+
* dropped include whose old wrap still opens the included tier.
|
|
113
|
+
*/
|
|
114
|
+
async exclude(tier: string, included: string): Promise<readonly string[]> {
|
|
115
|
+
let affected: readonly string[] = []
|
|
102
116
|
await this.#mutate(async (state, txn) => {
|
|
103
|
-
const holder = state.
|
|
117
|
+
const holder = state.tiers[tier]
|
|
104
118
|
if (holder === undefined || !holder.includes.includes(included)) {
|
|
105
|
-
throw new TypeVaultError('not_found', `"${
|
|
119
|
+
throw new TypeVaultError('not_found', `"${tier}" does not include "${included}"`)
|
|
106
120
|
}
|
|
107
|
-
const meta = state.groups[included]
|
|
108
|
-
txn.remove(wrapPath(included, meta?.gen ?? 0, { kind: 'group', group, gen: holder.gen }))
|
|
109
121
|
const updated = { ...holder, includes: holder.includes.filter((name) => name !== included) }
|
|
110
|
-
|
|
122
|
+
// Rotating removes the included tier's old generation, the dropped wrap with it, and rewrites tiers.json.
|
|
123
|
+
const context = { vault: this.#vault, owner: this.#owner, txn, exposed: true }
|
|
124
|
+
affected = await rotateTiers({ ...context, state: { ...state, tiers: { ...state.tiers, [tier]: updated } } }, [
|
|
125
|
+
included,
|
|
126
|
+
])
|
|
111
127
|
})
|
|
112
|
-
return
|
|
128
|
+
return affected
|
|
113
129
|
}
|
|
114
130
|
|
|
115
131
|
/** New key generation for `name`. Returns the names of every entry it could reach, now flagged for rotation. */
|
|
116
132
|
async rotate(name: string): Promise<readonly string[]> {
|
|
117
133
|
let affected: readonly string[] = []
|
|
118
134
|
await this.#mutate(async (state, txn) => {
|
|
119
|
-
affected = await
|
|
135
|
+
affected = await rotateTiers({ vault: this.#vault, owner: this.#owner, state, txn, exposed: true }, [name])
|
|
136
|
+
})
|
|
137
|
+
return affected
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Lists a member and wraps the owner key (for owners) and their tiers' keys to them. Every tier they reach is
|
|
142
|
+
* rotated in the same transaction, so they can open neither older ciphertext in git history nor entries deleted
|
|
143
|
+
* before they joined.
|
|
144
|
+
*/
|
|
145
|
+
addMember(rawName: string, member: NewMember): Promise<void> {
|
|
146
|
+
const name = parseMemberName(rawName)
|
|
147
|
+
return this.#mutate(async (state, txn) => {
|
|
148
|
+
if (state.members[name] !== undefined) throw new TypeVaultError('conflict', `member "${name}" already exists`)
|
|
149
|
+
const unknown = member.tiers.find((tier) => state.tiers[tier] === undefined)
|
|
150
|
+
if (unknown !== undefined) throw new TypeVaultError('not_found', `no tier "${unknown}"`)
|
|
151
|
+
const { kind, recipient, signPublic, owner, tiers } = member
|
|
152
|
+
const members = { ...state.members, [name]: { kind, recipient, signPublic, owner, tiers: [...tiers] } }
|
|
153
|
+
if (owner) txn.put(ownerWrapPath(name), await seal([recipient], utf8(encodeSecretKey(this.#owner))))
|
|
154
|
+
txn.put('members.json', membersDoc(members, this.#owner, state.seqs.members + 1))
|
|
155
|
+
const context = { vault: this.#vault, owner: this.#owner, state: { ...state, members }, txn, exposed: false }
|
|
156
|
+
await rotateTiers(context, reachOf(state, tiers))
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Drops a member and rotates every tier they reached, in one transaction: a crash leaves both or neither, never a
|
|
162
|
+
* removed member whose keys still open new ciphertext. Returns the entries they could read, now flagged for
|
|
163
|
+
* rotation: what they already decrypted can only be revoked at each secret's source.
|
|
164
|
+
*/
|
|
165
|
+
async removeMember(name: string): Promise<readonly string[]> {
|
|
166
|
+
let affected: readonly string[] = []
|
|
167
|
+
await this.#mutate(async (state, txn) => {
|
|
168
|
+
const member = state.members[name]
|
|
169
|
+
if (member === undefined) throw new TypeVaultError('not_found', `no member "${name}"`)
|
|
170
|
+
// Every entry is also sealed to the owner key, which can't be rotated yet: a former owner would keep reading.
|
|
171
|
+
if (member.owner) throw new TypeVaultError('invalid', `"${name}" is an owner; removing an owner is not supported`)
|
|
172
|
+
const { [name]: _removed, ...rest } = state.members
|
|
173
|
+
txn.remove(`members/${name}.key`)
|
|
174
|
+
txn.put('members.json', membersDoc(rest, this.#owner, state.seqs.members + 1))
|
|
175
|
+
const context = { vault: this.#vault, owner: this.#owner, state: { ...state, members: rest }, txn, exposed: true }
|
|
176
|
+
affected = await rotateTiers(context, reachOf(state, member.tiers))
|
|
120
177
|
})
|
|
121
178
|
return affected
|
|
122
179
|
}
|
|
123
180
|
|
|
124
|
-
setSigner(id: string, signPublic: string,
|
|
181
|
+
setSigner(id: string, signPublic: string, tiers: readonly string[]): Promise<void> {
|
|
125
182
|
return this.#mutate(async (state, txn) => {
|
|
126
183
|
txn.put(
|
|
127
184
|
'signers.json',
|
|
128
|
-
signersDoc({ ...state.signers, [id]: { signPublic,
|
|
185
|
+
signersDoc({ ...state.signers, [id]: { signPublic, tiers } }, this.#owner, state.seqs.signers + 1),
|
|
129
186
|
)
|
|
130
187
|
})
|
|
131
188
|
}
|
|
@@ -133,16 +190,16 @@ export class Admin {
|
|
|
133
190
|
removeSigner(id: string): Promise<void> {
|
|
134
191
|
return this.#mutate(async (state, txn) => {
|
|
135
192
|
const { [id]: _removed, ...rest } = state.signers
|
|
136
|
-
txn.put('signers.json', signersDoc(rest, this.#
|
|
193
|
+
txn.put('signers.json', signersDoc(rest, this.#owner, state.seqs.signers + 1))
|
|
137
194
|
})
|
|
138
195
|
}
|
|
139
196
|
|
|
140
|
-
setCeiling(name: string,
|
|
197
|
+
setCeiling(name: string, tier: string): Promise<void> {
|
|
141
198
|
return this.#mutate(async (state, txn) => {
|
|
142
|
-
if (state.
|
|
199
|
+
if (state.tiers[tier] === undefined) throw new TypeVaultError('not_found', `no tier "${tier}"`)
|
|
143
200
|
txn.put(
|
|
144
201
|
'ceilings.json',
|
|
145
|
-
ceilingsDoc({ ...state.ceilings, [
|
|
202
|
+
ceilingsDoc({ ...state.ceilings, [parseTierName(name)]: tier }, this.#owner, state.seqs.ceilings + 1),
|
|
146
203
|
)
|
|
147
204
|
})
|
|
148
205
|
}
|
|
@@ -150,17 +207,11 @@ export class Admin {
|
|
|
150
207
|
removeCeiling(name: string): Promise<void> {
|
|
151
208
|
return this.#mutate(async (state, txn) => {
|
|
152
209
|
const { [name]: _removed, ...rest } = state.ceilings
|
|
153
|
-
txn.put('ceilings.json', ceilingsDoc(rest, this.#
|
|
210
|
+
txn.put('ceilings.json', ceilingsDoc(rest, this.#owner, state.seqs.ceilings + 1))
|
|
154
211
|
})
|
|
155
212
|
}
|
|
156
213
|
|
|
157
|
-
async exportKey(group: string): Promise<string> {
|
|
158
|
-
const key = await this.#vault.keyring.unlock(group)
|
|
159
|
-
if (key === undefined) throw new TypeVaultError('not_found', `no group "${group}"`)
|
|
160
|
-
return encodeSecretKey(key)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
214
|
get signer(): ReturnType<typeof signerOf> {
|
|
164
|
-
return signerOf(this.#
|
|
215
|
+
return signerOf(this.#owner)
|
|
165
216
|
}
|
|
166
217
|
}
|
package/src/core/adopt.ts
CHANGED
|
@@ -1,33 +1,64 @@
|
|
|
1
1
|
import { join } from 'node:path'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { sha256Hex } from './crypto'
|
|
4
|
+
import { fromBase64, utf8 } from './encoding'
|
|
4
5
|
import { signCiphertext, signerOf } from './entries'
|
|
5
6
|
import { atomicWrite } from './fsx'
|
|
6
7
|
import { withLock } from './lock'
|
|
8
|
+
import type { EntryName } from './naming'
|
|
7
9
|
import type { Vault } from './vault'
|
|
8
10
|
|
|
11
|
+
/** A write whose certificate expired before adoption; `digest` pins this exact write, revision and content. */
|
|
12
|
+
export type ExpiredWrite = {
|
|
13
|
+
readonly name: string
|
|
14
|
+
readonly rev: number
|
|
15
|
+
readonly at: string
|
|
16
|
+
readonly digest: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type Adoption = {
|
|
20
|
+
readonly adopted: readonly string[]
|
|
21
|
+
readonly expired: readonly ExpiredWrite[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A write the owner chose to keep, by its name and the digest `doctor` listed it with. */
|
|
25
|
+
export type ExpiredPin = { readonly name: EntryName; readonly digest: string }
|
|
26
|
+
|
|
27
|
+
const digestOf = (sig: string): string => sha256Hex(utf8(sig)).slice(0, 16)
|
|
28
|
+
|
|
9
29
|
/**
|
|
10
30
|
* Re-signs entries written under a sealed grant's certificate with a key this process holds, so they outlive the
|
|
11
31
|
* certificate: readers stop accepting a certificate once their own clock passes its expiry. Run by the daemon
|
|
12
|
-
* periodically and by `doctor --fix`.
|
|
32
|
+
* periodically and by `doctor --fix`.
|
|
33
|
+
*
|
|
34
|
+
* A write whose certificate already expired is adopted only when the owner pins it in `expired`: its `at` is chosen
|
|
35
|
+
* by the holder, who keeps the signing key after expiry, so nothing proves it was written before the expiry. A name
|
|
36
|
+
* alone can't pin it: a later forgery under the same name replaces the file, so the pin also carries the digest of
|
|
37
|
+
* the exact write the owner reviewed.
|
|
13
38
|
*/
|
|
14
39
|
export function adoptCertWrites(
|
|
15
40
|
vault: Vault,
|
|
16
|
-
options: { readonly expired?:
|
|
17
|
-
): Promise<
|
|
41
|
+
options: { readonly expired?: readonly ExpiredPin[] } = {},
|
|
42
|
+
): Promise<Adoption> {
|
|
18
43
|
return withLock(vault.layout.lockFile, 'exclusive', async () => {
|
|
19
|
-
|
|
20
|
-
const { entries } = await vault.scan({ acceptExpiredCerts:
|
|
44
|
+
const keep = new Set((options.expired ?? []).map((pin) => `${pin.name}@${pin.digest}`))
|
|
45
|
+
const { entries } = await vault.scan({ acceptExpiredCerts: true })
|
|
21
46
|
const adopted: string[] = []
|
|
47
|
+
const expired: ExpiredWrite[] = []
|
|
22
48
|
for (const { envelope, payload } of entries) {
|
|
23
49
|
if (envelope.signer.kind !== 'cert') continue
|
|
24
|
-
const
|
|
50
|
+
const digest = digestOf(envelope.sig)
|
|
51
|
+
if (Date.now() > Date.parse(envelope.signer.cert.exp) && !keep.has(`${payload.name}@${digest}`)) {
|
|
52
|
+
expired.push({ name: payload.name, rev: envelope.rev, at: envelope.at, digest })
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
const key = (await vault.keyring.unlock(envelope.tier)) ?? vault.keyring.owner
|
|
25
56
|
if (key === undefined) continue
|
|
26
|
-
const draft = { id: envelope.id,
|
|
57
|
+
const draft = { id: envelope.id, tier: envelope.tier, rev: envelope.rev + 1, at: new Date().toISOString() }
|
|
27
58
|
const resigned = signCiphertext(draft, fromBase64(envelope.payload), vault.state, signerOf(key))
|
|
28
59
|
atomicWrite(join(vault.layout.entries, `${envelope.id}.json`), `${JSON.stringify(resigned, null, 2)}\n`)
|
|
29
60
|
adopted.push(payload.name)
|
|
30
61
|
}
|
|
31
|
-
return adopted
|
|
62
|
+
return { adopted, expired }
|
|
32
63
|
})
|
|
33
64
|
}
|
package/src/core/crypto.ts
CHANGED
|
@@ -8,8 +8,9 @@ import { fromBase64, fromBase64Url, fromUtf8, toBase64, toBase64Url, utf8 } from
|
|
|
8
8
|
import { TypeVaultError } from './errors'
|
|
9
9
|
|
|
10
10
|
export type KeyRole =
|
|
11
|
-
| { readonly kind: '
|
|
12
|
-
| { readonly kind: '
|
|
11
|
+
| { readonly kind: 'owner' }
|
|
12
|
+
| { readonly kind: 'tier'; readonly tier: string; readonly gen: number }
|
|
13
|
+
| { readonly kind: 'member'; readonly name: string }
|
|
13
14
|
|
|
14
15
|
/** An age identity for decryption plus an Ed25519 key for signing. */
|
|
15
16
|
export type SecretKey = { readonly role: KeyRole; readonly identity: string; readonly signSecret: Uint8Array }
|
|
@@ -18,8 +19,9 @@ export type PublicKey = { readonly recipient: string; readonly signPublic: strin
|
|
|
18
19
|
const KEY_PREFIX = 'tvk1:'
|
|
19
20
|
|
|
20
21
|
const RoleSchema = z.discriminatedUnion('kind', [
|
|
21
|
-
z.object({ kind: z.literal('
|
|
22
|
-
z.object({ kind: z.literal('
|
|
22
|
+
z.object({ kind: z.literal('owner') }),
|
|
23
|
+
z.object({ kind: z.literal('tier'), tier: z.string(), gen: z.number().int().positive() }),
|
|
24
|
+
z.object({ kind: z.literal('member'), name: z.string() }),
|
|
23
25
|
])
|
|
24
26
|
const EncodedKeySchema = z.object({ role: RoleSchema, age: z.string().startsWith('AGE-SECRET-KEY-1'), ed: z.string() })
|
|
25
27
|
|
package/src/core/doctor.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type DoctorReport = {
|
|
|
10
10
|
readonly problems: readonly ScanProblem[]
|
|
11
11
|
readonly mergesResolved: readonly string[]
|
|
12
12
|
readonly duplicatesRemoved: readonly string[]
|
|
13
|
-
/** Same name in several
|
|
13
|
+
/** Same name in several tiers: reported for the owner, never deleted automatically. */
|
|
14
14
|
readonly duplicatesKept: readonly string[]
|
|
15
15
|
readonly needsRotation: readonly string[]
|
|
16
16
|
}
|
|
@@ -60,18 +60,18 @@ export function doctor(vault: Vault, fix: boolean): Promise<DoctorReport> {
|
|
|
60
60
|
const duplicatesKept: string[] = []
|
|
61
61
|
for (const [name, copies] of byName) {
|
|
62
62
|
if (copies.length < 2) continue
|
|
63
|
-
// Only same-
|
|
64
|
-
// the
|
|
65
|
-
for (const [
|
|
66
|
-
const keep = resolveName(vault.state.
|
|
67
|
-
for (const copy of
|
|
63
|
+
// Only same-tier copies are removed: across tiers the higher tier already wins reads, and
|
|
64
|
+
// the owner decides what to delete.
|
|
65
|
+
for (const [tier, inTier] of Map.groupBy(copies, (entry) => entry.envelope.tier)) {
|
|
66
|
+
const keep = resolveName(vault.state.tiers, inTier)
|
|
67
|
+
for (const copy of inTier) {
|
|
68
68
|
if (copy === keep) continue
|
|
69
69
|
if (fix) rmSync(join(vault.layout.entries, `${copy.envelope.id}.json`))
|
|
70
|
-
duplicatesRemoved.push(`${name} (${
|
|
70
|
+
duplicatesRemoved.push(`${name} (${tier}, ${copy.envelope.id})`)
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
const
|
|
74
|
-
if (
|
|
73
|
+
const tiers = new Set(copies.map((entry) => entry.envelope.tier))
|
|
74
|
+
if (tiers.size > 1) duplicatesKept.push(`${name} (in ${[...tiers].sort().join(', ')})`)
|
|
75
75
|
}
|
|
76
76
|
const needsRotation = entries
|
|
77
77
|
.filter((entry) => entry.payload.needsRotation === true)
|