|
| 1 | +import { createClient, type Client } from "@libsql/client"; |
| 2 | +import { afterEach, describe, expect, test } from "@effect/vitest"; |
| 3 | +import { Effect } from "effect"; |
| 4 | + |
| 5 | +import { runSqliteEncryptedSecretsRepartition } from "./repartition-migration"; |
| 6 | + |
| 7 | +// A real in-memory libSQL database with the plugin_storage shape the hosts |
| 8 | +// use (unique on tenant/owner/subject/plugin_id/collection/key), so the |
| 9 | +// insert-or-ignore + delete choreography is exercised for real. |
| 10 | +const PLUGIN_STORAGE_DDL = `CREATE TABLE plugin_storage ( |
| 11 | + row_id text PRIMARY KEY NOT NULL, |
| 12 | + tenant text NOT NULL, |
| 13 | + owner text NOT NULL, |
| 14 | + subject text NOT NULL, |
| 15 | + plugin_id text NOT NULL, |
| 16 | + collection text NOT NULL, |
| 17 | + key text NOT NULL, |
| 18 | + data text NOT NULL, |
| 19 | + created_at integer NOT NULL, |
| 20 | + updated_at integer NOT NULL, |
| 21 | + CONSTRAINT plugin_storage_uidx UNIQUE (tenant, owner, subject, plugin_id, collection, key) |
| 22 | +)`; |
| 23 | + |
| 24 | +type Row = { |
| 25 | + readonly rowId: string; |
| 26 | + readonly owner: string; |
| 27 | + readonly subject: string; |
| 28 | + readonly key: string; |
| 29 | + readonly pluginId?: string; |
| 30 | + readonly collection?: string; |
| 31 | +}; |
| 32 | + |
| 33 | +let client: Client | undefined; |
| 34 | + |
| 35 | +const makeDb = async (rows: readonly Row[]): Promise<Client> => { |
| 36 | + client = createClient({ url: ":memory:" }); |
| 37 | + await client.execute(PLUGIN_STORAGE_DDL); |
| 38 | + for (const row of rows) { |
| 39 | + await client.execute({ |
| 40 | + sql: `INSERT INTO plugin_storage |
| 41 | + (row_id, tenant, owner, subject, plugin_id, collection, key, data, created_at, updated_at) |
| 42 | + VALUES (?, 'tenant-a', ?, ?, ?, ?, ?, 'v1.iv.tag.ct', 1, 1)`, |
| 43 | + args: [ |
| 44 | + row.rowId, |
| 45 | + row.owner, |
| 46 | + row.subject, |
| 47 | + row.pluginId ?? "encryptedSecrets", |
| 48 | + row.collection ?? "secrets", |
| 49 | + row.key, |
| 50 | + ], |
| 51 | + }); |
| 52 | + } |
| 53 | + return client; |
| 54 | +}; |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + client?.close(); |
| 58 | + client = undefined; |
| 59 | +}); |
| 60 | + |
| 61 | +const partitions = async (db: Client) => { |
| 62 | + const result = await db.execute( |
| 63 | + "SELECT owner, subject, key, data FROM plugin_storage ORDER BY key", |
| 64 | + ); |
| 65 | + return result.rows.map((row) => ({ |
| 66 | + owner: row.owner, |
| 67 | + subject: row.subject, |
| 68 | + key: row.key, |
| 69 | + data: row.data, |
| 70 | + })); |
| 71 | +}; |
| 72 | + |
| 73 | +describe("runSqliteEncryptedSecretsRepartition", () => { |
| 74 | + test("moves org-embedded rows out of a user partition, ciphertext intact", async () => { |
| 75 | + const db = await makeDb([ |
| 76 | + { rowId: "r1", owner: "user", subject: "user-123", key: "oauth:org:linear:main" }, |
| 77 | + { rowId: "r2", owner: "user", subject: "user-123", key: "oauth:org:linear:main:refresh" }, |
| 78 | + ]); |
| 79 | + const moved = await Effect.runPromise(runSqliteEncryptedSecretsRepartition(db)); |
| 80 | + expect(moved).toBe(2); |
| 81 | + expect(await partitions(db)).toEqual([ |
| 82 | + { owner: "org", subject: "", key: "oauth:org:linear:main", data: "v1.iv.tag.ct" }, |
| 83 | + { owner: "org", subject: "", key: "oauth:org:linear:main:refresh", data: "v1.iv.tag.ct" }, |
| 84 | + ]); |
| 85 | + }); |
| 86 | + |
| 87 | + test("leaves correctly filed rows and unscoped ids alone", async () => { |
| 88 | + const db = await makeDb([ |
| 89 | + { rowId: "r1", owner: "org", subject: "", key: "oauth:org:linear:main" }, |
| 90 | + { rowId: "r2", owner: "user", subject: "user-123", key: "connection:user:notion:p:token" }, |
| 91 | + { rowId: "r3", owner: "user", subject: "user-123", key: "legacy-random-id" }, |
| 92 | + ]); |
| 93 | + const moved = await Effect.runPromise(runSqliteEncryptedSecretsRepartition(db)); |
| 94 | + expect(moved).toBe(0); |
| 95 | + expect((await partitions(db)).map((row) => row.key)).toEqual([ |
| 96 | + "connection:user:notion:p:token", |
| 97 | + "legacy-random-id", |
| 98 | + "oauth:org:linear:main", |
| 99 | + ]); |
| 100 | + }); |
| 101 | + |
| 102 | + test("a post-fix org row wins over the mis-filed duplicate", async () => { |
| 103 | + const db = await makeDb([ |
| 104 | + { rowId: "old", owner: "user", subject: "user-123", key: "oauth:org:linear:main" }, |
| 105 | + { rowId: "new", owner: "org", subject: "", key: "oauth:org:linear:main" }, |
| 106 | + ]); |
| 107 | + await db.execute("UPDATE plugin_storage SET data = 'v1.new' WHERE row_id = 'new'"); |
| 108 | + const moved = await Effect.runPromise(runSqliteEncryptedSecretsRepartition(db)); |
| 109 | + expect(moved).toBe(1); |
| 110 | + expect(await partitions(db)).toEqual([ |
| 111 | + { owner: "org", subject: "", key: "oauth:org:linear:main", data: "v1.new" }, |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + test("does not touch other plugins' rows", async () => { |
| 116 | + const db = await makeDb([ |
| 117 | + { |
| 118 | + rowId: "r1", |
| 119 | + owner: "user", |
| 120 | + subject: "user-123", |
| 121 | + key: "oauth:org:slack:main", |
| 122 | + pluginId: "workosVault", |
| 123 | + collection: "metadata", |
| 124 | + }, |
| 125 | + ]); |
| 126 | + const moved = await Effect.runPromise(runSqliteEncryptedSecretsRepartition(db)); |
| 127 | + expect(moved).toBe(0); |
| 128 | + expect((await partitions(db))[0]).toMatchObject({ owner: "user", subject: "user-123" }); |
| 129 | + }); |
| 130 | + |
| 131 | + test("is a no-op on a database without the table", async () => { |
| 132 | + client = createClient({ url: ":memory:" }); |
| 133 | + const moved = await Effect.runPromise(runSqliteEncryptedSecretsRepartition(client)); |
| 134 | + expect(moved).toBe(0); |
| 135 | + }); |
| 136 | + |
| 137 | + test("is idempotent", async () => { |
| 138 | + const db = await makeDb([ |
| 139 | + { rowId: "r1", owner: "user", subject: "user-123", key: "oauth:org:linear:main" }, |
| 140 | + ]); |
| 141 | + expect(await Effect.runPromise(runSqliteEncryptedSecretsRepartition(db))).toBe(1); |
| 142 | + expect(await Effect.runPromise(runSqliteEncryptedSecretsRepartition(db))).toBe(0); |
| 143 | + }); |
| 144 | +}); |
0 commit comments