Repository navigation
Expand file tree
/
Copy pathplugin-credentials.ts
More file actions
234 lines (232 loc) · 7.43 KB
/
Copy pathplugin-credentials.ts
File metadata and controls
234 lines (232 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { randomUUID } from "node:crypto";
import { z } from "zod";
import type { DB, Schema } from "@db/index.js";
import { transact } from "@db/transactions.js";
import type {
PluginCredentialMetadata,
PluginCredentialServiceV1,
} from "@smartdoca/plugin-sdk/storage";
import {
CredentialCryptoError,
type CredentialCipher,
} from "./credential-cipher.js";
export class PluginCredentialError extends Error {
readonly name = "PluginCredentialError";
constructor(
readonly code: "invalid-input" | "conflict" | "unavailable",
message: string,
) {
super(message);
}
}
const invalid = () =>
new PluginCredentialError("invalid-input", "Invalid plugin credential input");
const unavailable = () =>
new PluginCredentialError(
"unavailable",
"Plugin credential storage is unavailable",
);
const idParser = z.string().uuid();
const revisionParser = z.number().int().min(1).max(2147483647);
const valueParser = z.string().min(1);
function parse<T>(parser: z.ZodType<T>, input: unknown): T {
const result = parser.safeParse(input);
if (!result.success) throw invalid();
return result.data;
}
async function safe<T>(run: () => Promise<T>): Promise<T> {
try {
return await run();
} catch (error) {
if (error instanceof PluginCredentialError) throw error;
if (
error instanceof CredentialCryptoError &&
error.code === "invalid-credential"
)
throw invalid();
throw unavailable();
}
}
/** Durable key identity; a different replica key cannot silently create a second set of secrets. No rotation or conversion. */
export async function verifyCredentialKey(db: DB, cipher: CredentialCipher) {
await safe(() =>
transact(db, async (tx) => {
await tx
.insertInto("plugin_credential_keys")
.values({
id: "global",
fingerprint: cipher.fingerprint,
created_at: new Date().toISOString(),
})
.onConflict((c) => c.column("id").doNothing())
.execute();
const row = await tx
.selectFrom("plugin_credential_keys")
.selectAll()
.where("id", "=", "global")
.executeTakeFirstOrThrow();
if (row.fingerprint !== cipher.fingerprint)
throw new PluginCredentialError(
"unavailable",
"DOCA_CREDENTIAL_MASTER_KEY does not match credential storage; retain the original key",
);
}),
);
}
/** Only the host supplies namespace, generation and the installation guard. No selector for another plugin or backend. */
export function bindPluginCredentials(
db: DB,
cipher: CredentialCipher,
pluginId: string,
namespace: string,
generation: number,
current: (tx: DB, lock?: boolean) => Promise<unknown>,
): PluginCredentialServiceV1 {
const metadata = (
row: Schema["plugin_credentials"],
): PluginCredentialMetadata => {
if (
!Number.isSafeInteger(row.revision) ||
row.revision < 1 ||
row.revision > 2147483647
)
throw unavailable();
return Object.freeze({
id: row.id,
revision: row.revision,
createdAt: row.created_at,
updatedAt: row.updated_at,
});
};
const binding = (id: string, revision: number) => ({
pluginId: namespace,
credentialId: `${generation}:${id}`,
revision,
});
const rowFor = (tx: DB, id: string) =>
tx
.selectFrom("plugin_credentials")
.selectAll()
.where("plugin_id", "=", pluginId)
.where("namespace", "=", namespace)
.where("generation", "=", generation)
.where("id", "=", id)
.executeTakeFirst();
const open = (row: Schema["plugin_credentials"]) => {
metadata(row);
return cipher.open(binding(row.id, row.revision), JSON.parse(row.sealed));
};
const updateParser = z
.object({
id: idParser,
value: valueParser,
expectedRevision: revisionParser,
})
.strict();
const removeParser = z
.object({ id: idParser, expectedRevision: revisionParser })
.strict();
const conflict = () =>
new PluginCredentialError(
"conflict",
"Plugin credential revision changed; read the current credential before retrying",
);
return Object.freeze<PluginCredentialServiceV1>({
create: (input) =>
safe(async () => {
const { value } = parse(
z.object({ value: valueParser }).strict(),
input,
);
const id = randomUUID();
return transact(db, async (tx) => {
await current(tx, true);
const now = new Date().toISOString();
const row = {
plugin_id: pluginId,
namespace,
generation,
id,
revision: 1,
sealed: JSON.stringify(cipher.seal(binding(id, 1), value)),
created_at: now,
updated_at: now,
};
await tx.insertInto("plugin_credentials").values(row).execute();
return metadata(row);
});
}),
inspect: (id) =>
safe(async () => {
parse(idParser, id);
return transact(db, async (tx) => {
await current(tx, true);
const row = await rowFor(tx, id);
return row ? metadata(row) : null;
});
}),
get: (id) =>
safe(async () => {
parse(idParser, id);
return transact(db, async (tx) => {
await current(tx, true);
const row = await rowFor(tx, id);
return row ? { credential: metadata(row), value: open(row) } : null;
});
}),
update: (input) =>
safe(async () => {
const { id, value, expectedRevision } = parse(updateParser, input);
return transact(db, async (tx) => {
await current(tx, true);
const row = await rowFor(tx, id);
if (!row) throw unavailable();
if (row.revision !== expectedRevision || row.revision === 2147483647)
throw conflict();
// Authenticate the old record before replacing it; do not mask corruption or a wrong key.
open(row);
const next = {
...row,
revision: row.revision + 1,
updated_at: new Date().toISOString(),
sealed: JSON.stringify(
cipher.seal(binding(id, row.revision + 1), value),
),
};
const result = await tx
.updateTable("plugin_credentials")
.set({
revision: next.revision,
updated_at: next.updated_at,
sealed: next.sealed,
})
.where("plugin_id", "=", pluginId)
.where("namespace", "=", namespace)
.where("generation", "=", generation)
.where("id", "=", id)
.where("revision", "=", expectedRevision)
.executeTakeFirst();
if (Number(result.numUpdatedRows) !== 1) throw conflict();
return metadata(next);
});
}),
remove: (input) =>
safe(async () => {
const { id, expectedRevision } = parse(removeParser, input);
await transact(db, async (tx) => {
await current(tx, true);
const row = await rowFor(tx, id);
if (!row) return;
if (row.revision !== expectedRevision) throw conflict();
await tx
.deleteFrom("plugin_credentials")
.where("plugin_id", "=", pluginId)
.where("namespace", "=", namespace)
.where("generation", "=", generation)
.where("id", "=", id)
.where("revision", "=", expectedRevision)
.execute();
});
}),
});
}