convex-secret-store 0.1.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/LICENSE +201 -0
- package/README.md +604 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/base64.d.ts +3 -0
- package/dist/client/base64.d.ts.map +1 -0
- package/dist/client/base64.js +32 -0
- package/dist/client/base64.js.map +1 -0
- package/dist/client/crypto.d.ts +43 -0
- package/dist/client/crypto.d.ts.map +1 -0
- package/dist/client/crypto.js +133 -0
- package/dist/client/crypto.js.map +1 -0
- package/dist/client/errors.d.ts +14 -0
- package/dist/client/errors.d.ts.map +1 -0
- package/dist/client/errors.js +29 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/operations.d.ts +88 -0
- package/dist/client/operations.d.ts.map +1 -0
- package/dist/client/operations.js +388 -0
- package/dist/client/operations.js.map +1 -0
- package/dist/client/options.d.ts +27 -0
- package/dist/client/options.d.ts.map +1 -0
- package/dist/client/options.js +66 -0
- package/dist/client/options.js.map +1 -0
- package/dist/client/types.d.ts +133 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +187 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/cleanup.d.ts +27 -0
- package/dist/component/cleanup.d.ts.map +1 -0
- package/dist/component/cleanup.js +96 -0
- package/dist/component/cleanup.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/lib.d.ts +177 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +531 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +73 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +45 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/shared.d.ts +7 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +12 -0
- package/dist/shared.js.map +1 -0
- package/package.json +115 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/base64.ts +37 -0
- package/src/client/crypto.ts +229 -0
- package/src/client/errors.ts +64 -0
- package/src/client/index.test.ts +480 -0
- package/src/client/index.ts +34 -0
- package/src/client/operations.ts +513 -0
- package/src/client/options.ts +110 -0
- package/src/client/setup.test.ts +26 -0
- package/src/client/types.ts +213 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +222 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/cleanup.test.ts +91 -0
- package/src/component/cleanup.ts +124 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/lib.test.ts +202 -0
- package/src/component/lib.ts +659 -0
- package/src/component/schema.ts +48 -0
- package/src/component/setup.test.ts +11 -0
- package/src/shared.ts +28 -0
- package/src/test.ts +18 -0
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import { afterEach, describe, expect, expectTypeOf, test, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
SecretStore,
|
|
4
|
+
SecretStoreClientError,
|
|
5
|
+
isSecretStoreClientError,
|
|
6
|
+
} from "./index.js";
|
|
7
|
+
import { normalizeSecretStoreOptions } from "./options.js";
|
|
8
|
+
import { components, initConvexTest } from "./setup.test.js";
|
|
9
|
+
import type { RunMutationCtx, RunQueryCtx } from "./types.js";
|
|
10
|
+
|
|
11
|
+
const KEY_V1 = Buffer.alloc(32, 1).toString("base64");
|
|
12
|
+
const KEY_V2 = Buffer.alloc(32, 2).toString("base64");
|
|
13
|
+
|
|
14
|
+
function ctxFrom(t: ReturnType<typeof initConvexTest>) {
|
|
15
|
+
const mutationCtx: RunMutationCtx = {
|
|
16
|
+
runMutation: (mutation, args) => t.mutation(mutation, args),
|
|
17
|
+
};
|
|
18
|
+
const queryCtx: RunQueryCtx = {
|
|
19
|
+
runQuery: (query, args) => t.query(query, args),
|
|
20
|
+
};
|
|
21
|
+
return { mutationCtx, queryCtx, ctx: { ...mutationCtx, ...queryCtx } };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
vi.useRealTimers();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("SecretStore client", () => {
|
|
29
|
+
test("constructs a typed client", () => {
|
|
30
|
+
const client = new SecretStore(components.secretStore, {
|
|
31
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(client).toBeInstanceOf(SecretStore);
|
|
35
|
+
expect(client.options.activeVersion).toBe(1);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("put/get round-trips a secret value", async () => {
|
|
39
|
+
const t = initConvexTest();
|
|
40
|
+
const { mutationCtx, queryCtx } = ctxFrom(t);
|
|
41
|
+
const client = new SecretStore<{
|
|
42
|
+
namespace: string;
|
|
43
|
+
metadata: { provider: string };
|
|
44
|
+
}>(components.secretStore, {
|
|
45
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const created = await client.put(mutationCtx, {
|
|
49
|
+
namespace: "acme:production",
|
|
50
|
+
name: "openai",
|
|
51
|
+
value: "sk-secret",
|
|
52
|
+
metadata: { provider: "openai" },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
expect(created.isNew).toBe(true);
|
|
56
|
+
|
|
57
|
+
const loaded = await client.get(queryCtx, {
|
|
58
|
+
namespace: "acme:production",
|
|
59
|
+
name: "openai",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(loaded.ok).toBe(true);
|
|
63
|
+
if (!loaded.ok) return;
|
|
64
|
+
expect(loaded.value).toBe("sk-secret");
|
|
65
|
+
expect(loaded.metadata).toEqual({ provider: "openai" });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("returns key_version_unavailable when ciphertext version is not configured", async () => {
|
|
69
|
+
const t = initConvexTest();
|
|
70
|
+
const { mutationCtx, queryCtx } = ctxFrom(t);
|
|
71
|
+
const initial = new SecretStore(components.secretStore, {
|
|
72
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
await initial.put(mutationCtx, {
|
|
76
|
+
name: "stripe",
|
|
77
|
+
value: "sk_live_123",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const rotatedOut = new SecretStore(components.secretStore, {
|
|
81
|
+
keys: [{ version: 2, value: KEY_V2 }],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const result = await rotatedOut.get(queryCtx, { name: "stripe" });
|
|
85
|
+
expect(result).toEqual({ ok: false, reason: "key_version_unavailable" });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("rotateKeys rewraps rows to the active version", async () => {
|
|
89
|
+
const t = initConvexTest();
|
|
90
|
+
const { mutationCtx, queryCtx, ctx } = ctxFrom(t);
|
|
91
|
+
const v1Client = new SecretStore(components.secretStore, {
|
|
92
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
await v1Client.put(mutationCtx, {
|
|
96
|
+
name: "resend",
|
|
97
|
+
value: "re_123",
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const rotatingClient = new SecretStore(components.secretStore, {
|
|
101
|
+
keys: [
|
|
102
|
+
{ version: 2, value: KEY_V2 },
|
|
103
|
+
{ version: 1, value: KEY_V1 },
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const rotated = await rotatingClient.rotateKeys(ctx, {
|
|
108
|
+
fromVersion: 1,
|
|
109
|
+
batchSize: 10,
|
|
110
|
+
cursor: null,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(rotated.processed).toBe(1);
|
|
114
|
+
expect(rotated.rotated).toBe(1);
|
|
115
|
+
expect(rotated.skipped).toBe(0);
|
|
116
|
+
|
|
117
|
+
const listed = await rotatingClient.list(queryCtx, {
|
|
118
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
119
|
+
});
|
|
120
|
+
expect(listed.page[0]?.keyVersion).toBe(2);
|
|
121
|
+
|
|
122
|
+
const activeOnlyClient = new SecretStore(components.secretStore, {
|
|
123
|
+
keys: [{ version: 2, value: KEY_V2 }],
|
|
124
|
+
});
|
|
125
|
+
const loaded = await activeOnlyClient.get(queryCtx, { name: "resend" });
|
|
126
|
+
expect(loaded.ok).toBe(true);
|
|
127
|
+
if (!loaded.ok) return;
|
|
128
|
+
expect(loaded.value).toBe("re_123");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("rotateKeys does not report done when a stale row still remains on the old version", async () => {
|
|
132
|
+
vi.useFakeTimers();
|
|
133
|
+
vi.setSystemTime(new Date("2026-03-09T12:00:00.000Z"));
|
|
134
|
+
|
|
135
|
+
const t = initConvexTest();
|
|
136
|
+
const { mutationCtx, queryCtx } = ctxFrom(t);
|
|
137
|
+
const v1Client = new SecretStore<{
|
|
138
|
+
metadata: { owner?: string };
|
|
139
|
+
}>(components.secretStore, {
|
|
140
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await v1Client.put(mutationCtx, {
|
|
144
|
+
name: "github",
|
|
145
|
+
value: "ghp_123",
|
|
146
|
+
metadata: { owner: "platform" },
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const rotatingClient = new SecretStore<{
|
|
150
|
+
metadata: { owner?: string };
|
|
151
|
+
}>(components.secretStore, {
|
|
152
|
+
keys: [
|
|
153
|
+
{ version: 2, value: KEY_V2 },
|
|
154
|
+
{ version: 1, value: KEY_V1 },
|
|
155
|
+
],
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
let injectedConcurrentUpdate = false;
|
|
159
|
+
const ctx: RunMutationCtx & RunQueryCtx = {
|
|
160
|
+
runQuery: (query, args) => t.query(query, args),
|
|
161
|
+
runMutation: async (mutation, args) => {
|
|
162
|
+
if (!injectedConcurrentUpdate) {
|
|
163
|
+
injectedConcurrentUpdate = true;
|
|
164
|
+
vi.setSystemTime(new Date("2026-03-09T12:00:01.000Z"));
|
|
165
|
+
await t.mutation(components.secretStore.lib.update, {
|
|
166
|
+
name: "github",
|
|
167
|
+
metadata: { owner: "rotated-later" },
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return await t.mutation(mutation, args);
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const result = await rotatingClient.rotateKeys(ctx, {
|
|
175
|
+
fromVersion: 1,
|
|
176
|
+
batchSize: 10,
|
|
177
|
+
cursor: null,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
expect(result.processed).toBe(1);
|
|
181
|
+
expect(result.rotated).toBe(0);
|
|
182
|
+
expect(result.skipped).toBe(1);
|
|
183
|
+
expect(result.isDone).toBe(false);
|
|
184
|
+
expect(result.continueCursor).toBeNull();
|
|
185
|
+
|
|
186
|
+
const listed = await rotatingClient.list(queryCtx, {
|
|
187
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
188
|
+
});
|
|
189
|
+
expect(listed.page[0]?.keyVersion).toBe(1);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("client rejects empty namespace", async () => {
|
|
193
|
+
const t = initConvexTest();
|
|
194
|
+
const { mutationCtx } = ctxFrom(t);
|
|
195
|
+
const client = new SecretStore<{ namespace: string }>(
|
|
196
|
+
components.secretStore,
|
|
197
|
+
{
|
|
198
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
199
|
+
},
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
await expect(
|
|
203
|
+
client.put(mutationCtx, {
|
|
204
|
+
namespace: "" as string,
|
|
205
|
+
name: "openai",
|
|
206
|
+
value: "sk-secret",
|
|
207
|
+
}),
|
|
208
|
+
).rejects.toSatisfy(
|
|
209
|
+
(error: unknown) =>
|
|
210
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("has, update, and remove follow expected edge-result paths", async () => {
|
|
215
|
+
const t = initConvexTest();
|
|
216
|
+
const { mutationCtx, queryCtx } = ctxFrom(t);
|
|
217
|
+
const client = new SecretStore(components.secretStore, {
|
|
218
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
expect(await client.has(queryCtx, { name: "missing" })).toBe(false);
|
|
222
|
+
|
|
223
|
+
expect(
|
|
224
|
+
await client.update(mutationCtx, {
|
|
225
|
+
name: "missing",
|
|
226
|
+
metadata: { owner: "nobody" },
|
|
227
|
+
}),
|
|
228
|
+
).toEqual({ updated: false });
|
|
229
|
+
|
|
230
|
+
expect(await client.remove(mutationCtx, { name: "missing" })).toEqual({
|
|
231
|
+
removed: false,
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test("listEvents rejects name + type in the client surface", async () => {
|
|
236
|
+
const client = new SecretStore(components.secretStore, {
|
|
237
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
238
|
+
});
|
|
239
|
+
const ctx: RunQueryCtx = {
|
|
240
|
+
runQuery: async () => {
|
|
241
|
+
throw new Error("should not be called");
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
await expect(
|
|
246
|
+
client.listEvents(ctx, {
|
|
247
|
+
name: "openai",
|
|
248
|
+
type: "created",
|
|
249
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
250
|
+
}),
|
|
251
|
+
).rejects.toSatisfy(
|
|
252
|
+
(error: unknown) =>
|
|
253
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test("listEvents supports secretId lookups without namespace", async () => {
|
|
258
|
+
const t = initConvexTest();
|
|
259
|
+
const { mutationCtx, queryCtx } = ctxFrom(t);
|
|
260
|
+
const client = new SecretStore(components.secretStore, {
|
|
261
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const created = await client.put(mutationCtx, {
|
|
265
|
+
name: "slack",
|
|
266
|
+
value: "xoxb-secret",
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
const events = await client.listEvents(queryCtx, {
|
|
270
|
+
secretId: created.secretId,
|
|
271
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
expect(events.page).toHaveLength(1);
|
|
275
|
+
expect(events.page[0]?.secretId).toBe(created.secretId);
|
|
276
|
+
expect(events.page[0]?.type).toBe("created");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("rotateKeys validates version and batch arguments", async () => {
|
|
280
|
+
const client = new SecretStore(components.secretStore, {
|
|
281
|
+
keys: [
|
|
282
|
+
{ version: 2, value: KEY_V2 },
|
|
283
|
+
{ version: 1, value: KEY_V1 },
|
|
284
|
+
],
|
|
285
|
+
});
|
|
286
|
+
const ctx = {} as RunMutationCtx & RunQueryCtx;
|
|
287
|
+
|
|
288
|
+
await expect(client.rotateKeys(ctx, { fromVersion: -1 })).rejects.toSatisfy(
|
|
289
|
+
(error: unknown) =>
|
|
290
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
await expect(client.rotateKeys(ctx, { fromVersion: 2 })).rejects.toSatisfy(
|
|
294
|
+
(error: unknown) =>
|
|
295
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
await expect(client.rotateKeys(ctx, { fromVersion: 9 })).rejects.toSatisfy(
|
|
299
|
+
(error: unknown) =>
|
|
300
|
+
isSecretStoreClientError(error) &&
|
|
301
|
+
error.code === "KEY_VERSION_UNAVAILABLE",
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
await expect(
|
|
305
|
+
client.rotateKeys(ctx, { fromVersion: 1, batchSize: 0 }),
|
|
306
|
+
).rejects.toSatisfy(
|
|
307
|
+
(error: unknown) =>
|
|
308
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
309
|
+
);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("cleanupSecrets and cleanupEvents validate retention arguments", async () => {
|
|
313
|
+
const client = new SecretStore(components.secretStore, {
|
|
314
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
315
|
+
});
|
|
316
|
+
const ctx = {} as RunMutationCtx;
|
|
317
|
+
|
|
318
|
+
await expect(
|
|
319
|
+
client.cleanupSecrets(ctx, { retentionMs: 0 }),
|
|
320
|
+
).rejects.toSatisfy(
|
|
321
|
+
(error: unknown) =>
|
|
322
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
await expect(
|
|
326
|
+
client.cleanupEvents(ctx, { retentionMs: 0 }),
|
|
327
|
+
).rejects.toSatisfy(
|
|
328
|
+
(error: unknown) =>
|
|
329
|
+
isSecretStoreClientError(error) && error.code === "INVALID_ARGUMENT",
|
|
330
|
+
);
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
describe("error and option contracts", () => {
|
|
335
|
+
test("normalizeSecretStoreOptions validates keys and defaults", () => {
|
|
336
|
+
const normalized = normalizeSecretStoreOptions({
|
|
337
|
+
keys: [{ version: 7, value: KEY_V1 }],
|
|
338
|
+
defaults: { ttlMs: 60_000 },
|
|
339
|
+
logLevel: "debug",
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
expect(normalized.activeVersion).toBe(7);
|
|
343
|
+
expect(normalized.defaults.ttlMs).toBe(60_000);
|
|
344
|
+
expect(normalized.logLevel).toBe("debug");
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("constructor throws typed INVALID_OPTIONS", () => {
|
|
348
|
+
expect(
|
|
349
|
+
() =>
|
|
350
|
+
new SecretStore(components.secretStore, {
|
|
351
|
+
keys: [],
|
|
352
|
+
}),
|
|
353
|
+
).toThrow(SecretStoreClientError);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test("normalizeSecretStoreOptions rejects invalid key material and defaults", () => {
|
|
357
|
+
expect(() =>
|
|
358
|
+
normalizeSecretStoreOptions({
|
|
359
|
+
keys: [{ version: 1, value: "" }],
|
|
360
|
+
}),
|
|
361
|
+
).toThrow(SecretStoreClientError);
|
|
362
|
+
|
|
363
|
+
expect(() =>
|
|
364
|
+
normalizeSecretStoreOptions({
|
|
365
|
+
keys: [
|
|
366
|
+
{ version: 1, value: KEY_V1 },
|
|
367
|
+
{ version: 1, value: KEY_V2 },
|
|
368
|
+
],
|
|
369
|
+
}),
|
|
370
|
+
).toThrow(SecretStoreClientError);
|
|
371
|
+
|
|
372
|
+
expect(() =>
|
|
373
|
+
normalizeSecretStoreOptions({
|
|
374
|
+
keys: [{ version: 1, value: Buffer.alloc(16, 1).toString("base64") }],
|
|
375
|
+
}),
|
|
376
|
+
).toThrow(SecretStoreClientError);
|
|
377
|
+
|
|
378
|
+
expect(() =>
|
|
379
|
+
normalizeSecretStoreOptions({
|
|
380
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
381
|
+
defaults: { ttlMs: -1 },
|
|
382
|
+
}),
|
|
383
|
+
).toThrow(SecretStoreClientError);
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
test("operation failures preserve cause", async () => {
|
|
387
|
+
const client = new SecretStore(components.secretStore, {
|
|
388
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
389
|
+
logLevel: "none",
|
|
390
|
+
});
|
|
391
|
+
const ctx: RunQueryCtx = {
|
|
392
|
+
runQuery: async () => {
|
|
393
|
+
throw new Error("db down");
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
const error = await client.get(ctx, { name: "missing" }).catch((e) => e);
|
|
398
|
+
expect(isSecretStoreClientError(error)).toBe(true);
|
|
399
|
+
if (isSecretStoreClientError(error)) {
|
|
400
|
+
expect(error.code).toBe("OPERATION_FAILED");
|
|
401
|
+
expect(error.cause).toBeInstanceOf(Error);
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test("client type contracts remain stable", () => {
|
|
407
|
+
const _baseClient = new SecretStore(components.secretStore, {
|
|
408
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
type BasePutArgs = Parameters<typeof _baseClient.put>[1];
|
|
412
|
+
const basePutArgs: BasePutArgs = {
|
|
413
|
+
name: "openai",
|
|
414
|
+
value: "sk",
|
|
415
|
+
};
|
|
416
|
+
void basePutArgs;
|
|
417
|
+
|
|
418
|
+
const _namespacedClient = new SecretStore<{ namespace: `env:${string}` }>(
|
|
419
|
+
components.secretStore,
|
|
420
|
+
{
|
|
421
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
422
|
+
},
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
type NamespacedPutArgs = Parameters<typeof _namespacedClient.put>[1];
|
|
426
|
+
const validNamespacedArgs: NamespacedPutArgs = {
|
|
427
|
+
namespace: "env:prod",
|
|
428
|
+
name: "openai",
|
|
429
|
+
value: "sk",
|
|
430
|
+
};
|
|
431
|
+
expectTypeOf(validNamespacedArgs.namespace).toEqualTypeOf<`env:${string}`>();
|
|
432
|
+
|
|
433
|
+
// @ts-expect-error namespace is required when configured in the generic.
|
|
434
|
+
const missingNamespace: NamespacedPutArgs = { name: "openai", value: "sk" };
|
|
435
|
+
void missingNamespace;
|
|
436
|
+
|
|
437
|
+
type NamespacedListArgs = Parameters<typeof _namespacedClient.list>[1];
|
|
438
|
+
const validNamespacedListArgs: NamespacedListArgs = {
|
|
439
|
+
namespace: "env:prod",
|
|
440
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
441
|
+
};
|
|
442
|
+
void validNamespacedListArgs;
|
|
443
|
+
|
|
444
|
+
// @ts-expect-error namespace is required for list when configured in the generic.
|
|
445
|
+
const missingListNamespace: NamespacedListArgs = {
|
|
446
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
447
|
+
};
|
|
448
|
+
void missingListNamespace;
|
|
449
|
+
|
|
450
|
+
type NamespacedListEventsArgs = Parameters<
|
|
451
|
+
typeof _namespacedClient.listEvents
|
|
452
|
+
>[1];
|
|
453
|
+
const validNamespacedListEventsArgs: NamespacedListEventsArgs = {
|
|
454
|
+
namespace: "env:prod",
|
|
455
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
456
|
+
};
|
|
457
|
+
void validNamespacedListEventsArgs;
|
|
458
|
+
|
|
459
|
+
// @ts-expect-error namespace is required for namespaced event listing unless querying by secretId.
|
|
460
|
+
const missingEventsNamespace: NamespacedListEventsArgs = {
|
|
461
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
462
|
+
};
|
|
463
|
+
void missingEventsNamespace;
|
|
464
|
+
|
|
465
|
+
const _metadataClient = new SecretStore<{
|
|
466
|
+
metadata: { provider: "openai" | "stripe"; label?: string };
|
|
467
|
+
}>(components.secretStore, {
|
|
468
|
+
keys: [{ version: 1, value: KEY_V1 }],
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
type MetadataPutArgs = Parameters<typeof _metadataClient.put>[1];
|
|
472
|
+
const metadataArgs: MetadataPutArgs = {
|
|
473
|
+
name: "stripe",
|
|
474
|
+
value: "sk",
|
|
475
|
+
metadata: { provider: "stripe", label: "primary" },
|
|
476
|
+
};
|
|
477
|
+
expectTypeOf(metadataArgs.metadata?.provider).toEqualTypeOf<
|
|
478
|
+
"openai" | "stripe" | undefined
|
|
479
|
+
>();
|
|
480
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export { SecretStore, SecretStore as default } from "./operations.js";
|
|
2
|
+
|
|
3
|
+
export { SecretStoreClientError, isSecretStoreClientError } from "./errors.js";
|
|
4
|
+
|
|
5
|
+
export type { SecretStoreClientErrorCode } from "./errors.js";
|
|
6
|
+
export type { SecretStoreOptions } from "./options.js";
|
|
7
|
+
|
|
8
|
+
export type {
|
|
9
|
+
CleanupEventsArgs,
|
|
10
|
+
CleanupEventsResult,
|
|
11
|
+
CleanupSecretsArgs,
|
|
12
|
+
CleanupSecretsResult,
|
|
13
|
+
GetArgs,
|
|
14
|
+
GetResult,
|
|
15
|
+
HasArgs,
|
|
16
|
+
ListArgs,
|
|
17
|
+
ListEventsArgs,
|
|
18
|
+
ListEventsResult,
|
|
19
|
+
ListResult,
|
|
20
|
+
PaginationOptions,
|
|
21
|
+
PutArgs,
|
|
22
|
+
PutResult,
|
|
23
|
+
RemoveArgs,
|
|
24
|
+
RemoveResult,
|
|
25
|
+
RotateKeysArgs,
|
|
26
|
+
RotateKeysResult,
|
|
27
|
+
RunMutationCtx,
|
|
28
|
+
RunQueryCtx,
|
|
29
|
+
RunReadWriteCtx,
|
|
30
|
+
SecretId,
|
|
31
|
+
SecretStoreTypeOptions,
|
|
32
|
+
UpdateArgs,
|
|
33
|
+
UpdateResult,
|
|
34
|
+
} from "./types.js";
|