Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix migration
Thiago Araujo da Silva0f89fa834a9f
diff --git a/backend/e2e-test/routes/v1/alert-identity-cleanup.spec.ts b/backend/e2e-test/routes/v1/alert-identity-cleanup.spec.tsnew file mode 100644index 0000000000..5ba99e1079--- /dev/null+++ b/backend/e2e-test/routes/v1/alert-identity-cleanup.spec.ts@@ -0,0 +1,185 @@+import { OrgMembershipRole } from "@app/db/schemas";+import { seedData1 } from "@app/db/seed-data";++const RESOURCE_TYPE = "identity.authentication";+const EVENT_TYPE = "identity.authentication.expiry";++const createOrgIdentity = async (name: string) => {+ const res = await testServer.inject({+ method: "POST",+ url: "/api/v1/identities",+ body: { name, role: OrgMembershipRole.Admin, organizationId: seedData1.organization.id },+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);+ return res.json().identity as { id: string; name: string };+};++const createProjectIdentity = async (name: string, projectId: string) => {+ const res = await testServer.inject({+ method: "POST",+ url: `/api/v1/projects/${projectId}/identities`,+ body: { name, roles: [{ role: "admin" }] },+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);+ return res.json().identity as { id: string; name: string };+};++const createAlert = async (opts: { identityId: string; name: string; projectId?: string }) => {+ const res = await testServer.inject({+ method: "POST",+ url: "/api/v1/alerts",+ body: {+ name: opts.name,+ resourceType: RESOURCE_TYPE,+ resourceId: opts.identityId,+ eventType: EVENT_TYPE,+ condition: { alertBefore: "30d" },+ ...(opts.projectId ? { projectId: opts.projectId } : {}),+ channels: [+ {+ name: `${opts.name}-email`,+ channelType: "email",+ config: {},+ recipients: [{ principalType: "user", principalId: seedData1.id }]+ }+ ]+ },+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);+ return res.json().alert as { id: string; channels: { id: string }[] };+};++const alertRows = async (identityId: string) =>+ testDb("alerts").where({ resourceType: RESOURCE_TYPE, resourceId: identityId }).select("id", "orgId", "projectId");++describe("Alert cleanup on identity deletion", async () => {+ test("hard delete via the org identity route (identity-v2) reaps the identity's alerts", async () => {+ const identity = await createOrgIdentity("alert-cleanup-v2");+ const alert = await createAlert({ identityId: identity.id, name: "v2-hard-delete" });++ expect(await alertRows(identity.id)).toHaveLength(1);++ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/organization/identities/${identity.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);++ expect(await alertRows(identity.id)).toEqual([]);+ // The channel and its membership must go with the alert, not linger as orphans.+ expect(await testDb("alert_channels").where({ id: alert.channels[0].id })).toEqual([]);+ expect(await testDb("alert_channel_memberships").where({ alertId: alert.id })).toEqual([]);+ });++ test("hard delete via the legacy v1 identity route reaps the identity's alerts", async () => {+ const identity = await createOrgIdentity("alert-cleanup-v1");+ await createAlert({ identityId: identity.id, name: "v1-hard-delete" });++ expect(await alertRows(identity.id)).toHaveLength(1);++ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/identities/${identity.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);++ expect(await alertRows(identity.id)).toEqual([]);+ });++ test("hard delete reaps the identity's alerts in other orgs too", async () => {+ const identity = await createOrgIdentity("alert-cleanup-cross-org");+ await createAlert({ identityId: identity.id, name: "cross-org-home" });++ // A second org watching the same identity. Inserted directly: there is no API for creating an+ // alert in an org the caller's token is not scoped to.+ const [otherOrg] = await testDb("organizations")+ .insert({ name: "alert-cleanup-other-org", slug: "alert-cleanup-other-org" })+ .returning("id");+ await testDb("alerts").insert({+ name: "cross-org-foreign",+ resourceType: RESOURCE_TYPE,+ resourceId: identity.id,+ eventType: EVENT_TYPE,+ triggerType: "scheduled",+ orgId: otherOrg.id,+ createdByActorId: seedData1.id,+ createdByActorType: "user"+ });++ expect(await alertRows(identity.id)).toHaveLength(2);++ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/organization/identities/${identity.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);++ expect(await alertRows(identity.id)).toEqual([]);++ await testDb("organizations").where({ id: otherOrg.id }).delete();+ });++ test("removing a project membership reaps the project alert and spares the org alert", async () => {+ const identity = await createOrgIdentity("alert-cleanup-membership");+ const orgAlert = await createAlert({ identityId: identity.id, name: "membership-org-scoped" });++ const addRes = await testServer.inject({+ method: "POST",+ url: `/api/v1/projects/${seedData1.project.id}/memberships/identities/${identity.id}`,+ body: { roles: [{ role: "admin" }] },+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(addRes.statusCode).toBe(200);++ const projectAlert = await createAlert({+ identityId: identity.id,+ name: "membership-project-scoped",+ projectId: seedData1.project.id+ });++ expect(await alertRows(identity.id)).toHaveLength(2);++ const removeRes = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/projects/${seedData1.project.id}/memberships/identities/${identity.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(removeRes.statusCode).toBe(200);++ const remaining = await alertRows(identity.id);+ expect(remaining.map((a) => a.id)).toEqual([orgAlert.id]);+ expect(remaining.map((a) => a.id)).not.toContain(projectAlert.id);++ await testServer.inject({+ method: "DELETE",+ url: `/api/v1/organization/identities/${identity.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ });++ test("deleting a project-scoped identity reaps its project alert", async () => {+ const identity = await createProjectIdentity("alert-cleanup-project-identity", seedData1.project.id);+ await createAlert({+ identityId: identity.id,+ name: "project-identity-scoped",+ projectId: seedData1.project.id+ });++ expect(await alertRows(identity.id)).toHaveLength(1);++ const res = await testServer.inject({+ method: "DELETE",+ url: `/api/v1/projects/${seedData1.project.id}/identities/${identity.id}`,+ headers: { authorization: `Bearer ${jwtAuthToken}` }+ });+ expect(res.statusCode).toBe(200);++ expect(await alertRows(identity.id)).toEqual([]);+ });+});diff --git a/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.ts b/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.tsindex 4bde8ac819..78f0022e94 100644--- a/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.ts+++ b/backend/src/db/migrations/20260801120000_add-alert-resource-lookup-index.ts@@ -2,29 +2,52 @@ import { Knex } from "knex";import { TableName } from "../schemas";+// Reaping a deleted resource's alerts (deleteAlertsForDeletedResource, on every identity delete)+// filters alerts on resourceType + resourceId alone — no orgId or projectId to narrow it — so+// without this index it seq-scans the whole table once per delete. resourceType leads so the+// composite also serves the resourceType-only lookups (findEnabledByResourceType, and the+// resourceType predicate in findActiveByScope / findScopedDuplicate) as a leftmost prefix.const RESOURCE_LOOKUP_INDEX = "idx_alerts_resource_type_resource_id";-const MIGRATION_TIMEOUT = 60 * 60 * 1000;-const MIGRATION_LOCK_TIMEOUT = 30 * 1000;+const MIGRATION_TIMEOUT = 60 * 60 * 1000; // 60 minutes+const MIGRATION_LOCK_TIMEOUT = 30 * 1000; // 30 secondsexport async function up(knex: Knex): Promise<void> {- const stmtResult = await knex.raw("SHOW statement_timeout");- const originalStatementTimeout = stmtResult.rows[0].statement_timeout;- const lockResult = await knex.raw("SHOW lock_timeout");- const originalLockTimeout = lockResult.rows[0].lock_timeout;+ // statement_timeout / lock_timeout are session-local, so every statement here has to run on the+ // same pooled connection as the CREATE INDEX CONCURRENTLY it is meant to govern.+ const connection = await knex.client.acquireConnection();+ const raw = (sql: string) => knex.raw(sql).connection(connection);try {- await knex.raw(`SET statement_timeout = ${MIGRATION_TIMEOUT}`);- await knex.raw(`SET lock_timeout = ${MIGRATION_LOCK_TIMEOUT}`);-- if (await knex.schema.hasColumn(TableName.Alert, "resourceId")) {- await knex.raw(`- CREATE INDEX CONCURRENTLY IF NOT EXISTS "${RESOURCE_LOOKUP_INDEX}"- ON ${TableName.Alert} ("resourceType", "resourceId")- `);+ const stmtResult = await raw("SHOW statement_timeout");+ const originalStatementTimeout = stmtResult.rows[0].statement_timeout;+ const lockResult = await raw("SHOW lock_timeout");+ const originalLockTimeout = lockResult.rows[0].lock_timeout;++ try {+ await raw(`SET statement_timeout = ${MIGRATION_TIMEOUT}`);+ await raw(`SET lock_timeout = ${MIGRATION_LOCK_TIMEOUT}`);++ if (await knex.schema.hasColumn(TableName.Alert, "resourceId")) {+ // A cancelled or failed CREATE INDEX CONCURRENTLY leaves behind an INVALID index that+ // IF NOT EXISTS would happily skip over, so drop it before retrying.+ const invalid = await raw(+ `SELECT 1 FROM pg_class c JOIN pg_index i ON i.indexrelid = c.oid WHERE c.relname = '${RESOURCE_LOOKUP_INDEX}' AND NOT i.indisvalid`+ );+ if (invalid.rows.length > 0) {+ await raw(`DROP INDEX CONCURRENTLY IF EXISTS "${RESOURCE_LOOKUP_INDEX}"`);+ }++ await raw(`+ CREATE INDEX CONCURRENTLY IF NOT EXISTS "${RESOURCE_LOOKUP_INDEX}"+ ON ${TableName.Alert} ("resourceType", "resourceId")+ `);+ }+ } finally {+ await raw(`SET statement_timeout = '${originalStatementTimeout}'`);+ await raw(`SET lock_timeout = '${originalLockTimeout}'`);}} finally {- await knex.raw(`SET statement_timeout = '${originalStatementTimeout}'`);- await knex.raw(`SET lock_timeout = '${originalLockTimeout}'`);+ await knex.client.releaseConnection(connection);}}