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,91 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
4
|
+
import { api } from "./_generated/api.js";
|
|
5
|
+
import { initConvexTest } from "./setup.test.js";
|
|
6
|
+
|
|
7
|
+
describe("component cleanup", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
vi.setSystemTime(new Date("2026-03-07T00:00:00.000Z"));
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.useRealTimers();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("cleanupSecrets removes retained expired secrets and leaves perpetual secrets intact", async () => {
|
|
18
|
+
const t = initConvexTest();
|
|
19
|
+
await t.mutation(api.lib.put, {
|
|
20
|
+
name: "permanent",
|
|
21
|
+
encryptedValue: "ciphertext-1",
|
|
22
|
+
iv: "iv-1",
|
|
23
|
+
wrappedDEK: "wrapped-1",
|
|
24
|
+
dekIv: "dekIv-1",
|
|
25
|
+
keyVersion: 1,
|
|
26
|
+
});
|
|
27
|
+
await t.mutation(api.lib.put, {
|
|
28
|
+
name: "expired",
|
|
29
|
+
encryptedValue: "ciphertext-2",
|
|
30
|
+
iv: "iv-2",
|
|
31
|
+
wrappedDEK: "wrapped-2",
|
|
32
|
+
dekIv: "dekIv-2",
|
|
33
|
+
keyVersion: 1,
|
|
34
|
+
expiresAt: Date.now() + 1_000,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
vi.setSystemTime(new Date("2026-04-08T00:00:00.000Z"));
|
|
38
|
+
|
|
39
|
+
const cleanupResult = await t.mutation(api.cleanup.cleanupSecrets, {
|
|
40
|
+
retentionMs: 24 * 60 * 60 * 1000,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(cleanupResult.deleted).toBe(1);
|
|
44
|
+
|
|
45
|
+
expect(
|
|
46
|
+
await t.query(api.lib.has, { name: "permanent", now: Date.now() }),
|
|
47
|
+
).toBe(true);
|
|
48
|
+
expect(
|
|
49
|
+
await t.query(api.lib.has, { name: "expired", now: Date.now() }),
|
|
50
|
+
).toBe(false);
|
|
51
|
+
|
|
52
|
+
const events = await t.query(api.lib.listEvents, {
|
|
53
|
+
paginationOpts: { numItems: 20, cursor: null },
|
|
54
|
+
});
|
|
55
|
+
expect(events.page.some((event) => event.name === "expired")).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("cleanupSecrets validates retentionMs", async () => {
|
|
59
|
+
const t = initConvexTest();
|
|
60
|
+
await expect(
|
|
61
|
+
t.mutation(api.cleanup.cleanupSecrets, { retentionMs: 0 }),
|
|
62
|
+
).rejects.toThrow();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("cleanupEvents deletes retained audit rows independently", async () => {
|
|
66
|
+
const t = initConvexTest();
|
|
67
|
+
await t.mutation(api.lib.put, {
|
|
68
|
+
name: "old-event-secret",
|
|
69
|
+
encryptedValue: "ciphertext",
|
|
70
|
+
iv: "iv",
|
|
71
|
+
wrappedDEK: "wrapped",
|
|
72
|
+
dekIv: "dekIv",
|
|
73
|
+
keyVersion: 1,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
vi.setSystemTime(new Date("2026-10-07T00:00:00.000Z"));
|
|
77
|
+
|
|
78
|
+
const cleanup = await t.mutation(api.cleanup.cleanupEvents, {
|
|
79
|
+
retentionMs: 24 * 60 * 60 * 1000,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
expect(cleanup.deleted).toBeGreaterThan(0);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("cleanupEvents validates retentionMs", async () => {
|
|
86
|
+
const t = initConvexTest();
|
|
87
|
+
await expect(
|
|
88
|
+
t.mutation(api.cleanup.cleanupEvents, { retentionMs: 0 }),
|
|
89
|
+
).rejects.toThrow();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { mutation } from "./_generated/server.js";
|
|
2
|
+
import type { MutationCtx } from "./_generated/server.js";
|
|
3
|
+
import { api } from "./_generated/api.js";
|
|
4
|
+
import { ConvexError, v } from "convex/values";
|
|
5
|
+
import type { Id } from "./_generated/dataModel.js";
|
|
6
|
+
|
|
7
|
+
const SECRET_BATCH_SIZE = 100;
|
|
8
|
+
const EVENT_BATCH_SIZE = 100;
|
|
9
|
+
|
|
10
|
+
const cleanupResultValidator = v.object({
|
|
11
|
+
deleted: v.number(),
|
|
12
|
+
isDone: v.boolean(),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
function assertRetentionMs(retentionMs: number) {
|
|
16
|
+
if (!Number.isFinite(retentionMs) || retentionMs <= 0) {
|
|
17
|
+
throw new ConvexError({
|
|
18
|
+
code: "invalid_argument",
|
|
19
|
+
message: `retentionMs must be a positive finite number, got ${retentionMs}`,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function recordExpiredCleanupEvent(
|
|
25
|
+
ctx: MutationCtx,
|
|
26
|
+
secret: {
|
|
27
|
+
_id: Id<"secrets">;
|
|
28
|
+
namespace?: string;
|
|
29
|
+
name: string;
|
|
30
|
+
},
|
|
31
|
+
) {
|
|
32
|
+
await ctx.db.insert("secretEvents", {
|
|
33
|
+
secretId: secret._id,
|
|
34
|
+
namespace: secret.namespace,
|
|
35
|
+
name: secret.name,
|
|
36
|
+
type: "deleted",
|
|
37
|
+
metadata: { deletedReason: "expired_cleanup" },
|
|
38
|
+
createdAt: Date.now(),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function cleanupSecretsImpl(ctx: MutationCtx, retentionMs: number) {
|
|
43
|
+
assertRetentionMs(retentionMs);
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
const cutoff = now - retentionMs;
|
|
46
|
+
|
|
47
|
+
const expiredSecrets = await ctx.db
|
|
48
|
+
.query("secrets")
|
|
49
|
+
.withIndex("by_expires_at", (q) =>
|
|
50
|
+
q.gte("expiresAt", 0).lt("expiresAt", cutoff),
|
|
51
|
+
)
|
|
52
|
+
.take(SECRET_BATCH_SIZE);
|
|
53
|
+
|
|
54
|
+
for (const secret of expiredSecrets) {
|
|
55
|
+
await recordExpiredCleanupEvent(ctx, secret);
|
|
56
|
+
await ctx.db.delete(secret._id);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const deleted = expiredSecrets.length;
|
|
60
|
+
const isDone = deleted < SECRET_BATCH_SIZE;
|
|
61
|
+
|
|
62
|
+
if (!isDone) {
|
|
63
|
+
await ctx.scheduler.runAfter(0, api.cleanup.cleanupSecrets, {
|
|
64
|
+
retentionMs,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { deleted, isDone };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function cleanupEventsImpl(ctx: MutationCtx, retentionMs: number) {
|
|
72
|
+
assertRetentionMs(retentionMs);
|
|
73
|
+
const cutoff = Date.now() - retentionMs;
|
|
74
|
+
|
|
75
|
+
const oldEvents = await ctx.db
|
|
76
|
+
.query("secretEvents")
|
|
77
|
+
.withIndex("by_created_at", (q) => q.lt("createdAt", cutoff))
|
|
78
|
+
.take(EVENT_BATCH_SIZE);
|
|
79
|
+
|
|
80
|
+
for (const event of oldEvents) {
|
|
81
|
+
await ctx.db.delete(event._id);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const deleted = oldEvents.length;
|
|
85
|
+
const isDone = deleted < EVENT_BATCH_SIZE;
|
|
86
|
+
|
|
87
|
+
if (!isDone) {
|
|
88
|
+
await ctx.scheduler.runAfter(0, api.cleanup.cleanupEvents, {
|
|
89
|
+
retentionMs,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { deleted, isDone };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Hard-deletes expired secrets older than `retentionMs`.
|
|
98
|
+
*
|
|
99
|
+
* The component treats `expiresAt` as the point when a secret becomes unusable.
|
|
100
|
+
* This cleanup deletes only rows whose expiry timestamp is older than the
|
|
101
|
+
* retention window, and records a final `deleted` audit event with
|
|
102
|
+
* `deletedReason: "expired_cleanup"` before deletion.
|
|
103
|
+
*/
|
|
104
|
+
export const cleanupSecrets = mutation({
|
|
105
|
+
args: { retentionMs: v.number() },
|
|
106
|
+
returns: cleanupResultValidator,
|
|
107
|
+
handler: async (ctx, { retentionMs }) => {
|
|
108
|
+
return await cleanupSecretsImpl(ctx, retentionMs);
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Hard-deletes audit events older than `retentionMs`.
|
|
114
|
+
*
|
|
115
|
+
* Event cleanup is independent from secret cleanup. Audit rows may outlive
|
|
116
|
+
* their parent secrets until this retention window expires.
|
|
117
|
+
*/
|
|
118
|
+
export const cleanupEvents = mutation({
|
|
119
|
+
args: { retentionMs: v.number() },
|
|
120
|
+
returns: cleanupResultValidator,
|
|
121
|
+
handler: async (ctx, { retentionMs }) => {
|
|
122
|
+
return await cleanupEventsImpl(ctx, retentionMs);
|
|
123
|
+
},
|
|
124
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|
|
2
|
+
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
4
|
+
import { api } from "./_generated/api.js";
|
|
5
|
+
import { initConvexTest } from "./setup.test.js";
|
|
6
|
+
|
|
7
|
+
describe("component lib", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
vi.setSystemTime(new Date("2026-03-07T00:00:00.000Z"));
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.useRealTimers();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("put/get/has/list round-trip for a non-namespaced secret", async () => {
|
|
18
|
+
const t = initConvexTest();
|
|
19
|
+
const putResult = await t.mutation(api.lib.put, {
|
|
20
|
+
name: "openai",
|
|
21
|
+
encryptedValue: "ciphertext",
|
|
22
|
+
iv: "iv",
|
|
23
|
+
wrappedDEK: "wrapped",
|
|
24
|
+
dekIv: "dekIv",
|
|
25
|
+
keyVersion: 1,
|
|
26
|
+
metadata: { provider: "openai" },
|
|
27
|
+
expiresAt: Date.now() + 60_000,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
expect(putResult.isNew).toBe(true);
|
|
31
|
+
|
|
32
|
+
const hasSecret = await t.query(api.lib.has, {
|
|
33
|
+
name: "openai",
|
|
34
|
+
now: Date.now(),
|
|
35
|
+
});
|
|
36
|
+
expect(hasSecret).toBe(true);
|
|
37
|
+
|
|
38
|
+
const getResult = await t.query(api.lib.get, {
|
|
39
|
+
name: "openai",
|
|
40
|
+
now: Date.now(),
|
|
41
|
+
});
|
|
42
|
+
expect(getResult).toEqual({
|
|
43
|
+
ok: true,
|
|
44
|
+
encryptedValue: "ciphertext",
|
|
45
|
+
iv: "iv",
|
|
46
|
+
wrappedDEK: "wrapped",
|
|
47
|
+
dekIv: "dekIv",
|
|
48
|
+
keyVersion: 1,
|
|
49
|
+
metadata: { provider: "openai" },
|
|
50
|
+
expiresAt: Date.now() + 60_000,
|
|
51
|
+
updatedAt: Date.now(),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const listResult = await t.query(api.lib.list, {
|
|
55
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
56
|
+
now: Date.now(),
|
|
57
|
+
});
|
|
58
|
+
expect(listResult.page).toHaveLength(1);
|
|
59
|
+
expect(listResult.page[0]).toMatchObject({
|
|
60
|
+
name: "openai",
|
|
61
|
+
keyVersion: 1,
|
|
62
|
+
effectiveState: "active",
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("put overwrite preserves omitted metadata and expiry, and null clears them", async () => {
|
|
67
|
+
const t = initConvexTest();
|
|
68
|
+
const firstExpiry = Date.now() + 60_000;
|
|
69
|
+
await t.mutation(api.lib.put, {
|
|
70
|
+
namespace: "acme:production",
|
|
71
|
+
name: "resend",
|
|
72
|
+
encryptedValue: "ciphertext-1",
|
|
73
|
+
iv: "iv-1",
|
|
74
|
+
wrappedDEK: "wrapped-1",
|
|
75
|
+
dekIv: "dekIv-1",
|
|
76
|
+
keyVersion: 1,
|
|
77
|
+
metadata: { owner: "platform" },
|
|
78
|
+
expiresAt: firstExpiry,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await t.mutation(api.lib.put, {
|
|
82
|
+
namespace: "acme:production",
|
|
83
|
+
name: "resend",
|
|
84
|
+
encryptedValue: "ciphertext-2",
|
|
85
|
+
iv: "iv-2",
|
|
86
|
+
wrappedDEK: "wrapped-2",
|
|
87
|
+
dekIv: "dekIv-2",
|
|
88
|
+
keyVersion: 2,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const preserved = await t.query(api.lib.get, {
|
|
92
|
+
namespace: "acme:production",
|
|
93
|
+
name: "resend",
|
|
94
|
+
now: Date.now(),
|
|
95
|
+
});
|
|
96
|
+
expect(preserved).toMatchObject({
|
|
97
|
+
ok: true,
|
|
98
|
+
metadata: { owner: "platform" },
|
|
99
|
+
expiresAt: firstExpiry,
|
|
100
|
+
keyVersion: 2,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
await t.mutation(api.lib.put, {
|
|
104
|
+
namespace: "acme:production",
|
|
105
|
+
name: "resend",
|
|
106
|
+
encryptedValue: "ciphertext-3",
|
|
107
|
+
iv: "iv-3",
|
|
108
|
+
wrappedDEK: "wrapped-3",
|
|
109
|
+
dekIv: "dekIv-3",
|
|
110
|
+
keyVersion: 3,
|
|
111
|
+
metadata: null,
|
|
112
|
+
expiresAt: null,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const cleared = await t.query(api.lib.get, {
|
|
116
|
+
namespace: "acme:production",
|
|
117
|
+
name: "resend",
|
|
118
|
+
now: Date.now(),
|
|
119
|
+
});
|
|
120
|
+
expect(cleared).toMatchObject({
|
|
121
|
+
ok: true,
|
|
122
|
+
keyVersion: 3,
|
|
123
|
+
});
|
|
124
|
+
if (cleared.ok) {
|
|
125
|
+
expect(cleared.metadata).toBeUndefined();
|
|
126
|
+
expect(cleared.expiresAt).toBeUndefined();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("listEvents rejects combining name and type filters", async () => {
|
|
131
|
+
const t = initConvexTest();
|
|
132
|
+
await expect(
|
|
133
|
+
t.query(api.lib.listEvents, {
|
|
134
|
+
namespace: "acme:production",
|
|
135
|
+
name: "stripe",
|
|
136
|
+
type: "created",
|
|
137
|
+
paginationOpts: { numItems: 10, cursor: null },
|
|
138
|
+
}),
|
|
139
|
+
).rejects.toThrow();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("empty namespace is rejected", async () => {
|
|
143
|
+
const t = initConvexTest();
|
|
144
|
+
await expect(
|
|
145
|
+
t.mutation(api.lib.put, {
|
|
146
|
+
namespace: "",
|
|
147
|
+
name: "openai",
|
|
148
|
+
encryptedValue: "ciphertext",
|
|
149
|
+
iv: "iv",
|
|
150
|
+
wrappedDEK: "wrapped",
|
|
151
|
+
dekIv: "dekIv",
|
|
152
|
+
keyVersion: 1,
|
|
153
|
+
}),
|
|
154
|
+
).rejects.toThrow();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("component rejects non-integer key versions", async () => {
|
|
158
|
+
const t = initConvexTest();
|
|
159
|
+
await expect(
|
|
160
|
+
t.mutation(api.lib.put, {
|
|
161
|
+
name: "openai",
|
|
162
|
+
encryptedValue: "ciphertext",
|
|
163
|
+
iv: "iv",
|
|
164
|
+
wrappedDEK: "wrapped",
|
|
165
|
+
dekIv: "dekIv",
|
|
166
|
+
keyVersion: 1.5,
|
|
167
|
+
}),
|
|
168
|
+
).rejects.toThrow();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("updateWrappedDEK returns stale when the row changed after it was read", async () => {
|
|
172
|
+
const t = initConvexTest();
|
|
173
|
+
const created = await t.mutation(api.lib.put, {
|
|
174
|
+
name: "stripe",
|
|
175
|
+
encryptedValue: "ciphertext-1",
|
|
176
|
+
iv: "iv-1",
|
|
177
|
+
wrappedDEK: "wrapped-1",
|
|
178
|
+
dekIv: "dekIv-1",
|
|
179
|
+
keyVersion: 1,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
await t.mutation(api.lib.put, {
|
|
183
|
+
name: "stripe",
|
|
184
|
+
encryptedValue: "ciphertext-2",
|
|
185
|
+
iv: "iv-2",
|
|
186
|
+
wrappedDEK: "wrapped-2",
|
|
187
|
+
dekIv: "dekIv-2",
|
|
188
|
+
keyVersion: 2,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const result = await t.mutation(api.lib.updateWrappedDEK, {
|
|
192
|
+
secretId: created.secretId,
|
|
193
|
+
expectedKeyVersion: 1,
|
|
194
|
+
expectedUpdatedAt: created.updatedAt,
|
|
195
|
+
wrappedDEK: "wrapped-rotated",
|
|
196
|
+
dekIv: "dekIv-rotated",
|
|
197
|
+
keyVersion: 3,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
expect(result).toEqual({ ok: false, reason: "stale" });
|
|
201
|
+
});
|
|
202
|
+
});
|