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: check if a policy exists on the global approval system before deleting it
adilson0d152fbccc21
diff --git a/backend/e2e-test/routes/v1/secret-approval-policy-bridge.spec.ts b/backend/e2e-test/routes/v1/secret-approval-policy-bridge.spec.tsindex 6468d74a10..ba29eb86da 100644--- a/backend/e2e-test/routes/v1/secret-approval-policy-bridge.spec.ts+++ b/backend/e2e-test/routes/v1/secret-approval-policy-bridge.spec.ts@@ -83,6 +83,7 @@ describe("Secret approval policy bridge routing", () => {const legacyIds: string[] = [];const bridgeIds: string[] = [];const requestIds: string[] = [];+ const createdEnvIds: string[] = [];let envId: string;beforeAll(async () => {@@ -96,6 +97,7 @@ describe("Secret approval policy bridge routing", () => {await db(TableName.ApprovalRequests).whereIn("id", requestIds).del();await db(TableName.SecretApprovalPolicy).whereIn("id", legacyIds).del();await db(TableName.ApprovalPolicies).whereIn("id", bridgeIds).del();+ await db(TableName.Environment).whereIn("id", createdEnvIds).del();});test("a legacy policy stays on the legacy tables through update and is soft-deleted on delete", async () => {@@ -279,4 +281,55 @@ describe("Secret approval policy bridge routing", () => {expect.arrayContaining([legacyId, bridgeId]));});++ test("an environment used by a policy on the global approval system cannot be deleted", async () => {+ const guardedSlug = "sap-bridge-env-delete";+ const envRes = await testServer.inject({+ method: "POST",+ url: `/api/v1/workspace/${projectId}/environments`,+ headers: authHeaders(),+ body: { name: "SAP bridge env delete", slug: guardedSlug }+ });+ expect(envRes.statusCode).toBe(200);+ const guardedEnvId = envRes.json().environment.id as string;+ createdEnvIds.push(guardedEnvId);++ const policyRes = await testServer.inject({+ method: "POST",+ url: "/api/v2/secret-approvals",+ headers: authHeaders(),+ body: { projectId, environment: guardedSlug, secretPath: "/", approvers, approvals: 1, name: "env-delete-guard" }+ });+ expect(policyRes.statusCode).toBe(200);+ const bridgeId = policyRes.json().approval.id as string;+ bridgeIds.push(bridgeId);+ expect(await bridgeRow(bridgeId)).toMatchObject({ type: ApprovalPolicyType.SecretChange });++ const deleteEnv = (hardDelete: boolean) =>+ testServer.inject({+ method: "DELETE",+ url: `/api/v1/workspace/${projectId}/environments/${guardedEnvId}?hardDelete=${hardDelete}`,+ headers: authHeaders()+ });++ const softDeleteRes = await deleteEnv(false);+ expect(softDeleteRes.statusCode).toBe(400);+ expect(softDeleteRes.json().message).toBe("Environment is in use by a secret approval policy");++ const hardDeleteRes = await deleteEnv(true);+ expect(hardDeleteRes.statusCode).toBe(400);+ expect(hardDeleteRes.json().message).toBe("Environment is in use by a secret approval policy");++ expect(await getDb()(TableName.Environment).where({ id: guardedEnvId }).first()).toMatchObject({+ deleteAfter: null+ });+ expect(await bridgeEnvRows(bridgeId)).toMatchObject([{ envId: guardedEnvId }]);++ const deletePolicyRes = await deletePolicy(bridgeId);+ expect(deletePolicyRes.statusCode).toBe(200);++ const deleteEnvRes = await deleteEnv(true);+ expect(deleteEnvRes.statusCode).toBe(200);+ expect(deleteEnvRes.json().environment.id).toBe(guardedEnvId);+ });});diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex 14f0990a06..3e3a8885c9 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -2330,7 +2330,8 @@ export const registerRoutes = async (licenseService,folderDAL,accessApprovalPolicyEnvironmentDAL,- secretApprovalPolicyEnvironmentDAL: sapEnvironmentDAL+ secretApprovalPolicyEnvironmentDAL: sapEnvironmentDAL,+ approvalPolicySecretEnvironmentDAL});const secretTagService = secretTagServiceFactory({ secretTagDAL, permissionService, secretV2BridgeDAL });diff --git a/backend/src/services/project-env/project-env-service.ts b/backend/src/services/project-env/project-env-service.tsindex 09af2a895e..19a900a001 100644--- a/backend/src/services/project-env/project-env-service.ts+++ b/backend/src/services/project-env/project-env-service.ts@@ -6,6 +6,7 @@ import { TLicenseServiceFactory } from "@app/ee/services/license/license-serviceimport { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";import { ProjectPermissionActions, ProjectPermissionSub } from "@app/ee/services/permission/project-permission";import { TSecretApprovalPolicyEnvironmentDALFactory } from "@app/ee/services/secret-approval-policy/secret-approval-policy-environment-dal";+import { TApprovalPolicySecretEnvironmentDALFactory } from "@app/ee/services/secret-change-policy-bridge/secret-change-policy-environment-dal";import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";import { BadRequestError, NotFoundError } from "@app/lib/errors";import { logger } from "@app/lib/logger";@@ -31,6 +32,7 @@ type TProjectEnvServiceFactoryDep = {keyStore: Pick<TKeyStoreFactory, "acquireLock" | "setItemWithExpiry" | "getItem" | "waitTillReady" | "deleteItem">;accessApprovalPolicyEnvironmentDAL: Pick<TAccessApprovalPolicyEnvironmentDALFactory, "findAvailablePoliciesByEnvId">;secretApprovalPolicyEnvironmentDAL: Pick<TSecretApprovalPolicyEnvironmentDALFactory, "findAvailablePoliciesByEnvId">;+ approvalPolicySecretEnvironmentDAL: Pick<TApprovalPolicySecretEnvironmentDALFactory, "findOne">;};export type TProjectEnvServiceFactory = ReturnType<typeof projectEnvServiceFactory>;@@ -42,7 +44,8 @@ export const projectEnvServiceFactory = ({keyStore,folderDAL,accessApprovalPolicyEnvironmentDAL,- secretApprovalPolicyEnvironmentDAL+ secretApprovalPolicyEnvironmentDAL,+ approvalPolicySecretEnvironmentDAL}: TProjectEnvServiceFactoryDep) => {const createEnvironment = async ({projectId,@@ -277,6 +280,13 @@ export const projectEnvServiceFactory = ({name: "DeleteEnvironment"});}+ const secretChangeGlobalPolicyEnv = await approvalPolicySecretEnvironmentDAL.findOne({ envId: id }, tx);+ if (secretChangeGlobalPolicyEnv) {+ throw new BadRequestError({+ message: "Environment is in use by a secret approval policy",+ name: "DeleteEnvironment"+ });+ }const accessApprovalPolicies = await accessApprovalPolicyEnvironmentDAL.findAvailablePoliciesByEnvId(id, tx);if (accessApprovalPolicies.length > 0) {throw new BadRequestError({