Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Minor fix in project deletion for concurrent calls on the delete endpoint

Carlos Monastyrskia3aae9502a16
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 38f4f305cc..aacf2d07b1 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -97,7 +97,9 @@ export const KeyStorePrefixes = {
ProjectSSEConnectionsSet: (projectId: string) => `project-sse-connections:${projectId}` as const,
ProjectSSEConnectionsLockoutKey: (projectId: string) => `project-sse-connections:lockout:${projectId}` as const,
ProjectSSEConnection: (projectId: string, connectionId: string) =>
- `project-sse-conn:${projectId}:${connectionId}` as const
+ `project-sse-conn:${projectId}:${connectionId}` as const,
+
+ ProjectDeleteLock: (projectId: string) => `project-delete-lock-${projectId}` as const
};
export const KeyStoreTtls = {
diff --git a/backend/src/services/project/project-service.ts b/backend/src/services/project/project-service.ts
index b9b2b09017..b36c62e785 100644
--- a/backend/src/services/project/project-service.ts
+++ b/backend/src/services/project/project-service.ts
@@ -40,7 +40,7 @@ import { TSshCertificateDALFactory } from "@app/ee/services/ssh-certificate/ssh-
import { TSshCertificateTemplateDALFactory } from "@app/ee/services/ssh-certificate-template/ssh-certificate-template-dal";
import { TSshHostDALFactory } from "@app/ee/services/ssh-host/ssh-host-dal";
import { TSshHostGroupDALFactory } from "@app/ee/services/ssh-host-group/ssh-host-group-dal";
-import { PgSqlLock, TKeyStoreFactory } from "@app/keystore/keystore";
+import { KeyStorePrefixes, PgSqlLock, TKeyStoreFactory } from "@app/keystore/keystore";
import { getProcessedPermissionRules } from "@app/lib/casl/permission-filter-utils";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
@@ -176,7 +176,7 @@ type TProjectServiceFactoryDep = {
licenseService: Pick<TLicenseServiceFactory, "getPlan" | "invalidateGetPlan">;
smtpService: Pick<TSmtpService, "sendMail">;
orgDAL: Pick<TOrgDALFactory, "findOne" | "findEffectiveOrgMembership">;
- keyStore: Pick<TKeyStoreFactory, "deleteItem">;
+ keyStore: Pick<TKeyStoreFactory, "deleteItem" | "acquireLock">;
roleDAL: Pick<TRoleDALFactory, "find" | "insertMany" | "delete">;
kmsService: Pick<
TKmsServiceFactory,
@@ -740,47 +740,62 @@ export const projectServiceFactory = ({
});
}
- const deletedProject = await projectDAL.transaction(async (tx) => {
- // delete these so that project custom roles can be deleted in cascade effect
- // direct deletion of project without these will cause fk error
- // this will clean up all memberships
- await membershipUserDAL.delete(
- { scopeOrgId: project.orgId, scopeProjectId: project.id, scope: AccessScope.Project },
- tx
- );
- const delProject = await projectDAL.deleteById(project.id, tx);
- const projectGhostUser = await projectMembershipDAL.findProjectGhostUser(project.id, tx).catch(() => null);
- // akhilmhdh: before removing those kms checking any other project uses it
- // happened due to project split
- if (delProject.kmsCertificateKeyId) {
- const projectsLinkedToForiegnKey = await projectDAL.find(
- { kmsCertificateKeyId: delProject.kmsCertificateKeyId },
- { tx }
+ let lock: Awaited<ReturnType<typeof keyStore.acquireLock>> | undefined;
+ try {
+ lock = await keyStore.acquireLock([KeyStorePrefixes.ProjectDeleteLock(project.id)], 30_000, {
+ retryCount: 0
+ });
+ } catch {
+ throw new BadRequestError({
+ message: "Project is already being deleted."
+ });
+ }
+
+ try {
+ const deletedProject = await projectDAL.transaction(async (tx) => {
+ // delete these so that project custom roles can be deleted in cascade effect
+ // direct deletion of project without these will cause fk error
+ // this will clean up all memberships
+ await membershipUserDAL.delete(
+ { scopeOrgId: project.orgId, scopeProjectId: project.id, scope: AccessScope.Project },
+ tx
);
- if (!projectsLinkedToForiegnKey.length) {
- await kmsService.deleteInternalKms(delProject.kmsCertificateKeyId, delProject.orgId, tx);
+ const delProject = await projectDAL.deleteById(project.id, tx);
+ const projectGhostUser = await projectMembershipDAL.findProjectGhostUser(project.id, tx).catch(() => null);
+ // akhilmhdh: before removing those kms checking any other project uses it
+ // happened due to project split
+ if (delProject.kmsCertificateKeyId) {
+ const projectsLinkedToForiegnKey = await projectDAL.find(
+ { kmsCertificateKeyId: delProject.kmsCertificateKeyId },
+ { tx }
+ );
+ if (!projectsLinkedToForiegnKey.length) {
+ await kmsService.deleteInternalKms(delProject.kmsCertificateKeyId, delProject.orgId, tx);
+ }
}
- }
- if (delProject.kmsSecretManagerKeyId) {
- const projectsLinkedToForiegnKey = await projectDAL.find(
- { kmsSecretManagerKeyId: delProject.kmsSecretManagerKeyId },
- { tx }
- );
- if (!projectsLinkedToForiegnKey.length) {
- await kmsService.deleteInternalKms(delProject.kmsSecretManagerKeyId, delProject.orgId, tx);
+ if (delProject.kmsSecretManagerKeyId) {
+ const projectsLinkedToForiegnKey = await projectDAL.find(
+ { kmsSecretManagerKeyId: delProject.kmsSecretManagerKeyId },
+ { tx }
+ );
+ if (!projectsLinkedToForiegnKey.length) {
+ await kmsService.deleteInternalKms(delProject.kmsSecretManagerKeyId, delProject.orgId, tx);
+ }
+ }
+ // Delete the org membership for the ghost user if it's found.
+ if (projectGhostUser) {
+ await userDAL.deleteById(projectGhostUser.id, tx);
}
- }
- // Delete the org membership for the ghost user if it's found.
- if (projectGhostUser) {
- await userDAL.deleteById(projectGhostUser.id, tx);
- }
- return delProject;
- });
+ return delProject;
+ });
- await keyStore.deleteItem(`infisical-cloud-plan-${actorOrgId}`);
- return deletedProject;
+ await keyStore.deleteItem(`infisical-cloud-plan-${actorOrgId}`);
+ return deletedProject;
+ } finally {
+ await lock.release();
+ }
};
const getProjects = async ({ actorId, actor, includeRoles, actorAuthMethod, actorOrgId, type }: TListProjectsDTO) => {