Skip to content

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

feat: add new check on global access approval policy on environment deletion

adilsonf60e21f239a2
diff --git a/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts b/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts
index 97c4782bf9..1435f30588 100644
--- a/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts
+++ b/backend/e2e-test/routes/v1/access-approval-global-policy-routing.spec.ts
@@ -57,6 +57,7 @@ const createGlobalPolicy = async (dto: {
name: string;
secretPath: string;
bypassers?: { type: BypasserType; id: string }[];
+ environment?: string;
}) => {
const res = await testServer.inject({
method: "POST",
@@ -64,7 +65,7 @@ const createGlobalPolicy = async (dto: {
headers: authHeaders(),
body: {
projectSlug: seedData1.project.slug,
- environment: seedData1.environment.slug,
+ environment: dto.environment ?? seedData1.environment.slug,
name: dto.name,
secretPath: dto.secretPath,
approvers: [{ type: ApproverType.User, id: seedData1.id }],
@@ -118,6 +119,26 @@ const countPolicies = () =>
headers: authHeaders()
});
+const environmentUrl = `/api/v1/workspace/${seedData1.project.id}/environments`;
+
+const createEnvironment = async (slug: string) => {
+ const res = await testServer.inject({
+ method: "POST",
+ url: environmentUrl,
+ headers: authHeaders(),
+ body: { name: slug, slug }
+ });
+ expect(res.statusCode).toBe(200);
+ return res.json().environment as { id: string; slug: string };
+};
+
+const deleteEnvironment = (envId: string) =>
+ testServer.inject({
+ method: "DELETE",
+ url: `${environmentUrl}/${envId}?hardDelete=true`,
+ headers: authHeaders()
+ });
+
const expectNoLegacyRow = async (policyId: string) => {
const legacyRow = await getDb()(TableName.AccessApprovalPolicy).where({ id: policyId }).first();
expect(legacyRow).toBeUndefined();
@@ -295,6 +316,27 @@ describe("Access approval policy routing", () => {
await expectNoLegacyRow(policy.id);
});
+ test("An environment a global policy covers cannot be deleted until the policy is removed", async () => {
+ const db = getDb();
+ const env = await createEnvironment("policy-routing-env-delete");
+ const policy = await createGlobalPolicy({
+ name: "policy-routing-env-delete",
+ secretPath: "/policy-routing-env-delete",
+ environment: env.slug
+ });
+
+ const blocked = await deleteEnvironment(env.id);
+ expect(blocked.statusCode).toBe(400);
+ expect(blocked.json().message).toBe("Environment is in use by an access approval policy");
+ expect(await db(TableName.ApprovalPolicySecretEnvironment).where({ policyId: policy.id })).toHaveLength(1);
+
+ expect((await deletePolicy(policy.id)).statusCode).toBe(200);
+
+ const deleted = await deleteEnvironment(env.id);
+ expect(deleted.statusCode).toBe(200);
+ expect(await db(TableName.Environment).where({ id: env.id }).first()).toBeUndefined();
+ });
+
test("A legacy policy keeps being served from the legacy tables", async () => {
const db = getDb();
const policy = await createLegacyPolicy({ name: "policy-routing-legacy", secretPath: "/policy-routing-legacy" });
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index 243fc02271..4041fa0da5 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -2432,7 +2432,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.ts
index 09af2a895e..a10b7f28d7 100644
--- a/backend/src/services/project-env/project-env-service.ts
+++ b/backend/src/services/project-env/project-env-service.ts
@@ -9,6 +9,7 @@ import { TSecretApprovalPolicyEnvironmentDALFactory } from "@app/ee/services/sec
import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";
import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
+import { TApprovalPolicySecretEnvironmentDALFactory } from "@app/services/approval-policy/approval-policy-dal";
import { ActorType } from "../auth/auth-type";
import { TSecretFolderDALFactory } from "../secret-folder/secret-folder-dal";
@@ -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,
@@ -284,6 +287,13 @@ export const projectEnvServiceFactory = ({
name: "DeleteEnvironment"
});
}
+ const globalAccessApprovalPolicy = await approvalPolicySecretEnvironmentDAL.findOne({ envId: id }, tx);
+ if (globalAccessApprovalPolicy) {
+ throw new BadRequestError({
+ message: "Environment is in use by an access approval policy",
+ name: "DeleteEnvironment"
+ });
+ }
if (hardDelete) {
const [doc] = await projectEnvDAL.delete({ id, projectId }, tx);
if (!doc)