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,659 @@
|
|
|
1
|
+
import { ConvexError, v } from "convex/values";
|
|
2
|
+
import { paginationOptsValidator } from "convex/server";
|
|
3
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
4
|
+
import { mutation, query } from "./_generated/server.js";
|
|
5
|
+
import type { MutationCtx, QueryCtx } from "./_generated/server.js";
|
|
6
|
+
import type { Doc, Id } from "./_generated/dataModel.js";
|
|
7
|
+
import schema from "./schema.js";
|
|
8
|
+
import {
|
|
9
|
+
effectiveStateValidator,
|
|
10
|
+
metadataValidator,
|
|
11
|
+
orderValidator,
|
|
12
|
+
secretEventTypeValidator,
|
|
13
|
+
} from "../shared.js";
|
|
14
|
+
|
|
15
|
+
type SecretDoc = Doc<"secrets">;
|
|
16
|
+
type SecretEventDoc = Doc<"secretEvents">;
|
|
17
|
+
type SecretReplacement = Omit<SecretDoc, "_id" | "_creationTime">;
|
|
18
|
+
|
|
19
|
+
function effectiveState(secret: SecretDoc, now: number): "active" | "expired" {
|
|
20
|
+
return secret.expiresAt !== undefined && now >= secret.expiresAt
|
|
21
|
+
? "expired"
|
|
22
|
+
: "active";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function mapSecretRow(secret: SecretDoc, now: number) {
|
|
26
|
+
return {
|
|
27
|
+
secretId: secret._id,
|
|
28
|
+
namespace: secret.namespace,
|
|
29
|
+
name: secret.name,
|
|
30
|
+
metadata: secret.metadata,
|
|
31
|
+
createdAt: secret._creationTime,
|
|
32
|
+
updatedAt: secret.updatedAt,
|
|
33
|
+
expiresAt: secret.expiresAt,
|
|
34
|
+
effectiveState: effectiveState(secret, now),
|
|
35
|
+
keyVersion: secret.keyVersion,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function mapEventRow(event: SecretEventDoc) {
|
|
40
|
+
return {
|
|
41
|
+
eventId: event._id,
|
|
42
|
+
secretId: event.secretId,
|
|
43
|
+
namespace: event.namespace,
|
|
44
|
+
name: event.name,
|
|
45
|
+
type: event.type,
|
|
46
|
+
metadata: event.metadata,
|
|
47
|
+
createdAt: event.createdAt,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function recordEvent(
|
|
52
|
+
ctx: MutationCtx,
|
|
53
|
+
event: {
|
|
54
|
+
secretId: Id<"secrets">;
|
|
55
|
+
namespace: string | undefined;
|
|
56
|
+
name: string;
|
|
57
|
+
type: "created" | "updated" | "deleted" | "rotated";
|
|
58
|
+
metadata?: Record<string, unknown>;
|
|
59
|
+
},
|
|
60
|
+
) {
|
|
61
|
+
await ctx.db.insert("secretEvents", {
|
|
62
|
+
secretId: event.secretId,
|
|
63
|
+
namespace: event.namespace,
|
|
64
|
+
name: event.name,
|
|
65
|
+
type: event.type,
|
|
66
|
+
metadata: event.metadata,
|
|
67
|
+
createdAt: Date.now(),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function assertNullableNonNegativeInteger(
|
|
72
|
+
value: number | null | undefined,
|
|
73
|
+
field: string,
|
|
74
|
+
) {
|
|
75
|
+
if (value === undefined || value === null) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
79
|
+
throw new ConvexError({
|
|
80
|
+
code: "invalid_argument",
|
|
81
|
+
message: `${field} must be a non-negative integer or null`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function assertNonNegativeInteger(value: number, field: string) {
|
|
87
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
88
|
+
throw new ConvexError({
|
|
89
|
+
code: "invalid_argument",
|
|
90
|
+
message: `${field} must be a non-negative integer`,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function assertValidNamespace(namespace: string | undefined) {
|
|
96
|
+
if (namespace === "") {
|
|
97
|
+
throw new ConvexError({
|
|
98
|
+
code: "invalid_argument",
|
|
99
|
+
message: "namespace must not be empty",
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function getSecretByName(
|
|
105
|
+
ctx: Pick<QueryCtx | MutationCtx, "db">,
|
|
106
|
+
namespace: string | undefined,
|
|
107
|
+
name: string,
|
|
108
|
+
) {
|
|
109
|
+
assertValidNamespace(namespace);
|
|
110
|
+
return await ctx.db
|
|
111
|
+
.query("secrets")
|
|
112
|
+
.withIndex("by_namespace_and_name", (q) =>
|
|
113
|
+
q.eq("namespace", namespace).eq("name", name),
|
|
114
|
+
)
|
|
115
|
+
.unique();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function withOptionalField<T extends object, K extends string, V>(
|
|
119
|
+
object: T,
|
|
120
|
+
key: K,
|
|
121
|
+
value: V | undefined,
|
|
122
|
+
): T & Partial<Record<K, V>> {
|
|
123
|
+
return value === undefined ? object : { ...object, [key]: value };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const failureReasonValidator = v.union(
|
|
127
|
+
v.literal("not_found"),
|
|
128
|
+
v.literal("expired"),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const putResultValidator = v.object({
|
|
132
|
+
secretId: v.id("secrets"),
|
|
133
|
+
createdAt: v.number(),
|
|
134
|
+
updatedAt: v.number(),
|
|
135
|
+
expiresAt: v.optional(v.number()),
|
|
136
|
+
isNew: v.boolean(),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const getResultValidator = v.union(
|
|
140
|
+
v.object({
|
|
141
|
+
ok: v.literal(true),
|
|
142
|
+
encryptedValue: v.string(),
|
|
143
|
+
iv: v.string(),
|
|
144
|
+
wrappedDEK: v.string(),
|
|
145
|
+
dekIv: v.string(),
|
|
146
|
+
keyVersion: v.number(),
|
|
147
|
+
metadata: v.optional(metadataValidator),
|
|
148
|
+
expiresAt: v.optional(v.number()),
|
|
149
|
+
updatedAt: v.number(),
|
|
150
|
+
}),
|
|
151
|
+
v.object({
|
|
152
|
+
ok: v.literal(false),
|
|
153
|
+
reason: failureReasonValidator,
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
const removeResultValidator = v.object({
|
|
158
|
+
removed: v.boolean(),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const updateResultValidator = v.object({
|
|
162
|
+
updated: v.boolean(),
|
|
163
|
+
updatedAt: v.optional(v.number()),
|
|
164
|
+
expiresAt: v.optional(v.number()),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const listItemValidator = v.object({
|
|
168
|
+
secretId: v.id("secrets"),
|
|
169
|
+
namespace: v.optional(v.string()),
|
|
170
|
+
name: v.string(),
|
|
171
|
+
metadata: v.optional(metadataValidator),
|
|
172
|
+
createdAt: v.number(),
|
|
173
|
+
updatedAt: v.number(),
|
|
174
|
+
expiresAt: v.optional(v.number()),
|
|
175
|
+
effectiveState: effectiveStateValidator,
|
|
176
|
+
keyVersion: v.number(),
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const listResultValidator = v.object({
|
|
180
|
+
page: v.array(listItemValidator),
|
|
181
|
+
isDone: v.boolean(),
|
|
182
|
+
continueCursor: v.string(),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const eventItemValidator = v.object({
|
|
186
|
+
eventId: v.id("secretEvents"),
|
|
187
|
+
secretId: v.id("secrets"),
|
|
188
|
+
namespace: v.optional(v.string()),
|
|
189
|
+
name: v.string(),
|
|
190
|
+
type: secretEventTypeValidator,
|
|
191
|
+
metadata: v.optional(metadataValidator),
|
|
192
|
+
createdAt: v.number(),
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const listEventsResultValidator = v.object({
|
|
196
|
+
page: v.array(eventItemValidator),
|
|
197
|
+
isDone: v.boolean(),
|
|
198
|
+
continueCursor: v.string(),
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const listByKeyVersionItemValidator = v.object({
|
|
202
|
+
secretId: v.id("secrets"),
|
|
203
|
+
namespace: v.optional(v.string()),
|
|
204
|
+
name: v.string(),
|
|
205
|
+
wrappedDEK: v.string(),
|
|
206
|
+
dekIv: v.string(),
|
|
207
|
+
keyVersion: v.number(),
|
|
208
|
+
updatedAt: v.number(),
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const listByKeyVersionResultValidator = v.object({
|
|
212
|
+
page: v.array(listByKeyVersionItemValidator),
|
|
213
|
+
isDone: v.boolean(),
|
|
214
|
+
continueCursor: v.string(),
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const updateWrappedDEKResultValidator = v.union(
|
|
218
|
+
v.object({
|
|
219
|
+
ok: v.literal(true),
|
|
220
|
+
updatedAt: v.number(),
|
|
221
|
+
}),
|
|
222
|
+
v.object({
|
|
223
|
+
ok: v.literal(false),
|
|
224
|
+
reason: v.union(v.literal("not_found"), v.literal("stale")),
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
export const put = mutation({
|
|
229
|
+
args: {
|
|
230
|
+
namespace: v.optional(v.string()),
|
|
231
|
+
name: v.string(),
|
|
232
|
+
encryptedValue: v.string(),
|
|
233
|
+
iv: v.string(),
|
|
234
|
+
wrappedDEK: v.string(),
|
|
235
|
+
dekIv: v.string(),
|
|
236
|
+
keyVersion: v.number(),
|
|
237
|
+
metadata: v.optional(v.union(metadataValidator, v.null())),
|
|
238
|
+
expiresAt: v.optional(v.union(v.number(), v.null())),
|
|
239
|
+
},
|
|
240
|
+
returns: putResultValidator,
|
|
241
|
+
handler: async (ctx, args) => {
|
|
242
|
+
assertNonNegativeInteger(args.keyVersion, "keyVersion");
|
|
243
|
+
assertNullableNonNegativeInteger(args.expiresAt, "expiresAt");
|
|
244
|
+
const existing = await getSecretByName(ctx, args.namespace, args.name);
|
|
245
|
+
const now = Date.now();
|
|
246
|
+
|
|
247
|
+
if (existing === null) {
|
|
248
|
+
const secretId = await ctx.db.insert("secrets", {
|
|
249
|
+
namespace: args.namespace,
|
|
250
|
+
name: args.name,
|
|
251
|
+
encryptedValue: args.encryptedValue,
|
|
252
|
+
iv: args.iv,
|
|
253
|
+
wrappedDEK: args.wrappedDEK,
|
|
254
|
+
dekIv: args.dekIv,
|
|
255
|
+
keyVersion: args.keyVersion,
|
|
256
|
+
...(args.metadata !== undefined && args.metadata !== null
|
|
257
|
+
? { metadata: args.metadata }
|
|
258
|
+
: {}),
|
|
259
|
+
...(typeof args.expiresAt === "number"
|
|
260
|
+
? { expiresAt: args.expiresAt }
|
|
261
|
+
: {}),
|
|
262
|
+
updatedAt: now,
|
|
263
|
+
});
|
|
264
|
+
await recordEvent(ctx, {
|
|
265
|
+
secretId,
|
|
266
|
+
namespace: args.namespace,
|
|
267
|
+
name: args.name,
|
|
268
|
+
type: "created",
|
|
269
|
+
metadata:
|
|
270
|
+
args.metadata !== undefined && args.metadata !== null
|
|
271
|
+
? args.metadata
|
|
272
|
+
: undefined,
|
|
273
|
+
});
|
|
274
|
+
return {
|
|
275
|
+
secretId,
|
|
276
|
+
createdAt: now,
|
|
277
|
+
updatedAt: now,
|
|
278
|
+
...(typeof args.expiresAt === "number"
|
|
279
|
+
? { expiresAt: args.expiresAt }
|
|
280
|
+
: {}),
|
|
281
|
+
isNew: true,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const {
|
|
286
|
+
_id: _existingId,
|
|
287
|
+
_creationTime: _existingCreatedAt,
|
|
288
|
+
metadata: storedMetadata,
|
|
289
|
+
expiresAt: storedExpiresAt,
|
|
290
|
+
...stored
|
|
291
|
+
} = existing;
|
|
292
|
+
const nextMetadata =
|
|
293
|
+
args.metadata === undefined
|
|
294
|
+
? storedMetadata
|
|
295
|
+
: args.metadata === null
|
|
296
|
+
? undefined
|
|
297
|
+
: args.metadata;
|
|
298
|
+
const nextExpiresAt =
|
|
299
|
+
args.expiresAt === undefined
|
|
300
|
+
? storedExpiresAt
|
|
301
|
+
: args.expiresAt === null
|
|
302
|
+
? undefined
|
|
303
|
+
: args.expiresAt;
|
|
304
|
+
|
|
305
|
+
const replacement: SecretReplacement = withOptionalField(
|
|
306
|
+
withOptionalField(
|
|
307
|
+
{
|
|
308
|
+
...stored,
|
|
309
|
+
namespace: args.namespace,
|
|
310
|
+
name: args.name,
|
|
311
|
+
encryptedValue: args.encryptedValue,
|
|
312
|
+
iv: args.iv,
|
|
313
|
+
wrappedDEK: args.wrappedDEK,
|
|
314
|
+
dekIv: args.dekIv,
|
|
315
|
+
keyVersion: args.keyVersion,
|
|
316
|
+
updatedAt: now,
|
|
317
|
+
},
|
|
318
|
+
"metadata",
|
|
319
|
+
nextMetadata,
|
|
320
|
+
),
|
|
321
|
+
"expiresAt",
|
|
322
|
+
nextExpiresAt,
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
await ctx.db.replace(existing._id, replacement);
|
|
326
|
+
await recordEvent(ctx, {
|
|
327
|
+
secretId: existing._id,
|
|
328
|
+
namespace: args.namespace,
|
|
329
|
+
name: args.name,
|
|
330
|
+
type: "updated",
|
|
331
|
+
metadata:
|
|
332
|
+
args.metadata !== undefined && args.metadata !== null
|
|
333
|
+
? args.metadata
|
|
334
|
+
: undefined,
|
|
335
|
+
});
|
|
336
|
+
return {
|
|
337
|
+
secretId: existing._id,
|
|
338
|
+
createdAt: existing._creationTime,
|
|
339
|
+
updatedAt: now,
|
|
340
|
+
...(replacement.expiresAt !== undefined
|
|
341
|
+
? { expiresAt: replacement.expiresAt }
|
|
342
|
+
: {}),
|
|
343
|
+
isNew: false,
|
|
344
|
+
};
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
export const get = query({
|
|
349
|
+
args: {
|
|
350
|
+
namespace: v.optional(v.string()),
|
|
351
|
+
name: v.string(),
|
|
352
|
+
now: v.number(),
|
|
353
|
+
},
|
|
354
|
+
returns: getResultValidator,
|
|
355
|
+
handler: async (ctx, args) => {
|
|
356
|
+
const secret = await getSecretByName(ctx, args.namespace, args.name);
|
|
357
|
+
if (secret === null) {
|
|
358
|
+
return { ok: false as const, reason: "not_found" as const };
|
|
359
|
+
}
|
|
360
|
+
if (secret.expiresAt !== undefined && args.now >= secret.expiresAt) {
|
|
361
|
+
return { ok: false as const, reason: "expired" as const };
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
ok: true as const,
|
|
365
|
+
encryptedValue: secret.encryptedValue,
|
|
366
|
+
iv: secret.iv,
|
|
367
|
+
wrappedDEK: secret.wrappedDEK,
|
|
368
|
+
dekIv: secret.dekIv,
|
|
369
|
+
keyVersion: secret.keyVersion,
|
|
370
|
+
metadata: secret.metadata,
|
|
371
|
+
expiresAt: secret.expiresAt,
|
|
372
|
+
updatedAt: secret.updatedAt,
|
|
373
|
+
};
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
export const has = query({
|
|
378
|
+
args: {
|
|
379
|
+
namespace: v.optional(v.string()),
|
|
380
|
+
name: v.string(),
|
|
381
|
+
now: v.number(),
|
|
382
|
+
},
|
|
383
|
+
returns: v.boolean(),
|
|
384
|
+
handler: async (ctx, args) => {
|
|
385
|
+
const secret = await getSecretByName(ctx, args.namespace, args.name);
|
|
386
|
+
if (secret === null) {
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
return !(secret.expiresAt !== undefined && args.now >= secret.expiresAt);
|
|
390
|
+
},
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
export const remove = mutation({
|
|
394
|
+
args: {
|
|
395
|
+
namespace: v.optional(v.string()),
|
|
396
|
+
name: v.string(),
|
|
397
|
+
},
|
|
398
|
+
returns: removeResultValidator,
|
|
399
|
+
handler: async (ctx, args) => {
|
|
400
|
+
const secret = await getSecretByName(ctx, args.namespace, args.name);
|
|
401
|
+
if (secret === null) {
|
|
402
|
+
return { removed: false };
|
|
403
|
+
}
|
|
404
|
+
await recordEvent(ctx, {
|
|
405
|
+
secretId: secret._id,
|
|
406
|
+
namespace: secret.namespace,
|
|
407
|
+
name: secret.name,
|
|
408
|
+
type: "deleted",
|
|
409
|
+
metadata: { deletedReason: "explicit_remove" },
|
|
410
|
+
});
|
|
411
|
+
await ctx.db.delete(secret._id);
|
|
412
|
+
return { removed: true };
|
|
413
|
+
},
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
export const update = mutation({
|
|
417
|
+
args: {
|
|
418
|
+
namespace: v.optional(v.string()),
|
|
419
|
+
name: v.string(),
|
|
420
|
+
metadata: v.optional(v.union(metadataValidator, v.null())),
|
|
421
|
+
expiresAt: v.optional(v.union(v.number(), v.null())),
|
|
422
|
+
},
|
|
423
|
+
returns: updateResultValidator,
|
|
424
|
+
handler: async (ctx, args) => {
|
|
425
|
+
if (args.metadata === undefined && args.expiresAt === undefined) {
|
|
426
|
+
return { updated: false };
|
|
427
|
+
}
|
|
428
|
+
assertNullableNonNegativeInteger(args.expiresAt, "expiresAt");
|
|
429
|
+
const secret = await getSecretByName(ctx, args.namespace, args.name);
|
|
430
|
+
if (secret === null) {
|
|
431
|
+
return { updated: false };
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const {
|
|
435
|
+
_id: _secretId,
|
|
436
|
+
_creationTime: _secretCreatedAt,
|
|
437
|
+
metadata: storedMetadata,
|
|
438
|
+
expiresAt: storedExpiresAt,
|
|
439
|
+
...stored
|
|
440
|
+
} = secret;
|
|
441
|
+
const nextMetadata =
|
|
442
|
+
args.metadata === undefined
|
|
443
|
+
? storedMetadata
|
|
444
|
+
: args.metadata === null
|
|
445
|
+
? undefined
|
|
446
|
+
: args.metadata;
|
|
447
|
+
const nextExpiresAt =
|
|
448
|
+
args.expiresAt === undefined
|
|
449
|
+
? storedExpiresAt
|
|
450
|
+
: args.expiresAt === null
|
|
451
|
+
? undefined
|
|
452
|
+
: args.expiresAt;
|
|
453
|
+
const updatedAt = Date.now();
|
|
454
|
+
const replacement: SecretReplacement = withOptionalField(
|
|
455
|
+
withOptionalField(
|
|
456
|
+
{
|
|
457
|
+
...stored,
|
|
458
|
+
updatedAt,
|
|
459
|
+
},
|
|
460
|
+
"metadata",
|
|
461
|
+
nextMetadata,
|
|
462
|
+
),
|
|
463
|
+
"expiresAt",
|
|
464
|
+
nextExpiresAt,
|
|
465
|
+
);
|
|
466
|
+
await ctx.db.replace(secret._id, replacement);
|
|
467
|
+
await recordEvent(ctx, {
|
|
468
|
+
secretId: secret._id,
|
|
469
|
+
namespace: secret.namespace,
|
|
470
|
+
name: secret.name,
|
|
471
|
+
type: "updated",
|
|
472
|
+
metadata:
|
|
473
|
+
args.metadata !== undefined && args.metadata !== null
|
|
474
|
+
? args.metadata
|
|
475
|
+
: undefined,
|
|
476
|
+
});
|
|
477
|
+
return {
|
|
478
|
+
updated: true,
|
|
479
|
+
updatedAt,
|
|
480
|
+
...(replacement.expiresAt !== undefined
|
|
481
|
+
? { expiresAt: replacement.expiresAt }
|
|
482
|
+
: {}),
|
|
483
|
+
};
|
|
484
|
+
},
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
export const list = query({
|
|
488
|
+
args: {
|
|
489
|
+
namespace: v.optional(v.string()),
|
|
490
|
+
paginationOpts: paginationOptsValidator,
|
|
491
|
+
order: orderValidator,
|
|
492
|
+
now: v.number(),
|
|
493
|
+
},
|
|
494
|
+
returns: listResultValidator,
|
|
495
|
+
handler: async (ctx, args) => {
|
|
496
|
+
assertValidNamespace(args.namespace);
|
|
497
|
+
const result = await paginator(ctx.db, schema)
|
|
498
|
+
.query("secrets")
|
|
499
|
+
.withIndex("by_namespace_and_updated_at", (q) =>
|
|
500
|
+
q.eq("namespace", args.namespace),
|
|
501
|
+
)
|
|
502
|
+
.order(args.order ?? "desc")
|
|
503
|
+
.paginate(args.paginationOpts);
|
|
504
|
+
|
|
505
|
+
return {
|
|
506
|
+
page: result.page.map((secret) => mapSecretRow(secret, args.now)),
|
|
507
|
+
isDone: result.isDone,
|
|
508
|
+
continueCursor: result.continueCursor,
|
|
509
|
+
};
|
|
510
|
+
},
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
export const listEvents = query({
|
|
514
|
+
args: {
|
|
515
|
+
paginationOpts: paginationOptsValidator,
|
|
516
|
+
secretId: v.optional(v.id("secrets")),
|
|
517
|
+
namespace: v.optional(v.string()),
|
|
518
|
+
name: v.optional(v.string()),
|
|
519
|
+
type: v.optional(secretEventTypeValidator),
|
|
520
|
+
order: orderValidator,
|
|
521
|
+
},
|
|
522
|
+
returns: listEventsResultValidator,
|
|
523
|
+
handler: async (ctx, args) => {
|
|
524
|
+
assertValidNamespace(args.namespace);
|
|
525
|
+
const hasSecretId = args.secretId !== undefined;
|
|
526
|
+
const hasName = args.name !== undefined;
|
|
527
|
+
const hasType = args.type !== undefined;
|
|
528
|
+
if (hasSecretId && (args.namespace !== undefined || hasName || hasType)) {
|
|
529
|
+
throw new ConvexError({
|
|
530
|
+
code: "invalid_argument",
|
|
531
|
+
message: "secretId cannot be combined with namespace, name, or type",
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
if (hasName && hasType) {
|
|
535
|
+
throw new ConvexError({
|
|
536
|
+
code: "invalid_argument",
|
|
537
|
+
message: "name and type cannot be combined in V1",
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
if ((hasName || hasType) && args.namespace === undefined) {
|
|
541
|
+
throw new ConvexError({
|
|
542
|
+
code: "invalid_argument",
|
|
543
|
+
message: "namespace is required when filtering by name or type",
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const pages = paginator(ctx.db, schema).query("secretEvents");
|
|
548
|
+
const order = args.order ?? "desc";
|
|
549
|
+
const result = hasSecretId
|
|
550
|
+
? await pages
|
|
551
|
+
.withIndex("by_secret_id_and_created_at", (q) =>
|
|
552
|
+
q.eq("secretId", args.secretId!),
|
|
553
|
+
)
|
|
554
|
+
.order(order)
|
|
555
|
+
.paginate(args.paginationOpts)
|
|
556
|
+
: hasName
|
|
557
|
+
? await pages
|
|
558
|
+
.withIndex("by_namespace_and_name_and_created_at", (q) =>
|
|
559
|
+
q.eq("namespace", args.namespace!).eq("name", args.name!),
|
|
560
|
+
)
|
|
561
|
+
.order(order)
|
|
562
|
+
.paginate(args.paginationOpts)
|
|
563
|
+
: hasType
|
|
564
|
+
? await pages
|
|
565
|
+
.withIndex("by_namespace_and_type_and_created_at", (q) =>
|
|
566
|
+
q.eq("namespace", args.namespace!).eq("type", args.type!),
|
|
567
|
+
)
|
|
568
|
+
.order(order)
|
|
569
|
+
.paginate(args.paginationOpts)
|
|
570
|
+
: await pages
|
|
571
|
+
.withIndex("by_namespace_and_created_at", (q) =>
|
|
572
|
+
q.eq("namespace", args.namespace),
|
|
573
|
+
)
|
|
574
|
+
.order(order)
|
|
575
|
+
.paginate(args.paginationOpts);
|
|
576
|
+
|
|
577
|
+
return {
|
|
578
|
+
page: result.page.map(mapEventRow),
|
|
579
|
+
isDone: result.isDone,
|
|
580
|
+
continueCursor: result.continueCursor,
|
|
581
|
+
};
|
|
582
|
+
},
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
export const listByKeyVersion = query({
|
|
586
|
+
args: {
|
|
587
|
+
fromVersion: v.number(),
|
|
588
|
+
paginationOpts: paginationOptsValidator,
|
|
589
|
+
order: orderValidator,
|
|
590
|
+
},
|
|
591
|
+
returns: listByKeyVersionResultValidator,
|
|
592
|
+
handler: async (ctx, args) => {
|
|
593
|
+
assertNonNegativeInteger(args.fromVersion, "fromVersion");
|
|
594
|
+
const result = await paginator(ctx.db, schema)
|
|
595
|
+
.query("secrets")
|
|
596
|
+
.withIndex("by_key_version", (q) => q.eq("keyVersion", args.fromVersion))
|
|
597
|
+
.order(args.order ?? "desc")
|
|
598
|
+
.paginate(args.paginationOpts);
|
|
599
|
+
|
|
600
|
+
return {
|
|
601
|
+
page: result.page.map((secret) => ({
|
|
602
|
+
secretId: secret._id,
|
|
603
|
+
namespace: secret.namespace,
|
|
604
|
+
name: secret.name,
|
|
605
|
+
wrappedDEK: secret.wrappedDEK,
|
|
606
|
+
dekIv: secret.dekIv,
|
|
607
|
+
keyVersion: secret.keyVersion,
|
|
608
|
+
updatedAt: secret.updatedAt,
|
|
609
|
+
})),
|
|
610
|
+
isDone: result.isDone,
|
|
611
|
+
continueCursor: result.continueCursor,
|
|
612
|
+
};
|
|
613
|
+
},
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
export const updateWrappedDEK = mutation({
|
|
617
|
+
args: {
|
|
618
|
+
secretId: v.id("secrets"),
|
|
619
|
+
expectedKeyVersion: v.number(),
|
|
620
|
+
expectedUpdatedAt: v.number(),
|
|
621
|
+
wrappedDEK: v.string(),
|
|
622
|
+
dekIv: v.string(),
|
|
623
|
+
keyVersion: v.number(),
|
|
624
|
+
},
|
|
625
|
+
returns: updateWrappedDEKResultValidator,
|
|
626
|
+
handler: async (ctx, args) => {
|
|
627
|
+
assertNonNegativeInteger(args.expectedKeyVersion, "expectedKeyVersion");
|
|
628
|
+
assertNonNegativeInteger(args.keyVersion, "keyVersion");
|
|
629
|
+
const secret = await ctx.db.get(args.secretId);
|
|
630
|
+
if (secret === null) {
|
|
631
|
+
return { ok: false as const, reason: "not_found" as const };
|
|
632
|
+
}
|
|
633
|
+
if (
|
|
634
|
+
secret.keyVersion !== args.expectedKeyVersion ||
|
|
635
|
+
secret.updatedAt !== args.expectedUpdatedAt
|
|
636
|
+
) {
|
|
637
|
+
return { ok: false as const, reason: "stale" as const };
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
const updatedAt = Date.now();
|
|
641
|
+
await ctx.db.patch(secret._id, {
|
|
642
|
+
wrappedDEK: args.wrappedDEK,
|
|
643
|
+
dekIv: args.dekIv,
|
|
644
|
+
keyVersion: args.keyVersion,
|
|
645
|
+
updatedAt,
|
|
646
|
+
});
|
|
647
|
+
await recordEvent(ctx, {
|
|
648
|
+
secretId: secret._id,
|
|
649
|
+
namespace: secret.namespace,
|
|
650
|
+
name: secret.name,
|
|
651
|
+
type: "rotated",
|
|
652
|
+
metadata: {
|
|
653
|
+
previousKeyVersion: args.expectedKeyVersion,
|
|
654
|
+
newKeyVersion: args.keyVersion,
|
|
655
|
+
},
|
|
656
|
+
});
|
|
657
|
+
return { ok: true as const, updatedAt };
|
|
658
|
+
},
|
|
659
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { metadataValidator, secretEventTypeValidator } from "../shared.js";
|
|
4
|
+
|
|
5
|
+
export const secretsFields = {
|
|
6
|
+
namespace: v.optional(v.string()),
|
|
7
|
+
name: v.string(),
|
|
8
|
+
encryptedValue: v.string(),
|
|
9
|
+
iv: v.string(),
|
|
10
|
+
wrappedDEK: v.string(),
|
|
11
|
+
dekIv: v.string(),
|
|
12
|
+
keyVersion: v.number(),
|
|
13
|
+
metadata: v.optional(metadataValidator),
|
|
14
|
+
expiresAt: v.optional(v.number()),
|
|
15
|
+
updatedAt: v.number(),
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const secretEventsFields = {
|
|
19
|
+
secretId: v.id("secrets"),
|
|
20
|
+
namespace: v.optional(v.string()),
|
|
21
|
+
name: v.string(),
|
|
22
|
+
type: secretEventTypeValidator,
|
|
23
|
+
metadata: v.optional(metadataValidator),
|
|
24
|
+
createdAt: v.number(),
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default defineSchema({
|
|
28
|
+
secrets: defineTable(secretsFields)
|
|
29
|
+
.index("by_namespace_and_name", ["namespace", "name"])
|
|
30
|
+
.index("by_namespace_and_updated_at", ["namespace", "updatedAt"])
|
|
31
|
+
.index("by_key_version", ["keyVersion"])
|
|
32
|
+
.index("by_expires_at", ["expiresAt"]),
|
|
33
|
+
|
|
34
|
+
secretEvents: defineTable(secretEventsFields)
|
|
35
|
+
.index("by_secret_id_and_created_at", ["secretId", "createdAt"])
|
|
36
|
+
.index("by_namespace_and_created_at", ["namespace", "createdAt"])
|
|
37
|
+
.index("by_namespace_and_name_and_created_at", [
|
|
38
|
+
"namespace",
|
|
39
|
+
"name",
|
|
40
|
+
"createdAt",
|
|
41
|
+
])
|
|
42
|
+
.index("by_namespace_and_type_and_created_at", [
|
|
43
|
+
"namespace",
|
|
44
|
+
"type",
|
|
45
|
+
"createdAt",
|
|
46
|
+
])
|
|
47
|
+
.index("by_created_at", ["createdAt"]),
|
|
48
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
import { test } from "vitest";
|
|
3
|
+
import schema from "./schema.js";
|
|
4
|
+
import { convexTest } from "convex-test";
|
|
5
|
+
export const modules = import.meta.glob("./**/*.*s");
|
|
6
|
+
|
|
7
|
+
export function initConvexTest() {
|
|
8
|
+
const t = convexTest(schema, modules);
|
|
9
|
+
return t;
|
|
10
|
+
}
|
|
11
|
+
test("setup", () => {});
|