Skip to content

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

Merge branch 'main' into feat/adilsitos/eng-5532

adilsonec7ad973df90
diff --cc backend/src/services/secret-folder/secret-folder-service.ts
index 2e7ce0aee7,c87d4eafa7..4ae618e89b
--- a/backend/src/services/secret-folder/secret-folder-service.ts
+++ b/backend/src/services/secret-folder/secret-folder-service.ts
@@@ -639,13 -611,52 +640,52 @@@ export const secretFolderServiceFactor
}: {
projectId: string;
env: TProjectEnvironments;
- parentId: string;
- idOrName: string;
- actor: ActorType;
+ folderId: string;
+ folderTree: Awaited<ReturnType<typeof $getEnvFolderTree>>;
+ tx?: Knex;
}) => {
- const { folderPaths, childrenMap } = folderTree;
+ let targetFolder = await folderDAL
+ .findOne({
+ envId: env.id,
+ name: idOrName,
+ parentId,
+ isReserved: false
+ })
+ .catch(() => null);
+
+ if (!targetFolder && uuidValidate(idOrName)) {
+ targetFolder = await folderDAL
+ .findOne({
+ envId: env.id,
+ id: idOrName,
+ parentId,
+ isReserved: false
+ })
+ .catch(() => null);
+ }
+
+ if (!targetFolder) {
+ throw new NotFoundError({ message: `Target folder not found` });
+ }
- const targetFolderWithPath = folderPaths.find((f) => f.id === folderId);
+ // get environment root folder (as it's needed to get all folders under it)
+ const rootFolder = await folderDAL.findBySecretPath(projectId, env.slug, "/");
+ if (!rootFolder) throw new NotFoundError({ message: `Root folder not found` });
+ // get all folders under environment root folder
+ const folderPaths = await folderDAL.findByEnvsDeep({ parentIds: [rootFolder.id] });
+
+ // create a map of folders by parent id
+ const normalizeKey = (key: string | null | undefined): string => key ?? "root";
+ const folderMap = new Map<string, (TSecretFolders & { path: string; depth: number; environment: string })[]>();
+ for (const folder of folderPaths) {
+ if (!folderMap.has(normalizeKey(folder.parentId))) {
+ folderMap.set(normalizeKey(folder.parentId), []);
+ }
+ folderMap.get(normalizeKey(folder.parentId))?.push(folder);
+ }
+
+ // Find the target folder in the folderPaths to get its full details
+ const targetFolderWithPath = folderPaths.find((f) => f.id === targetFolder!.id);
if (!targetFolderWithPath) {
throw new NotFoundError({ message: `Target folder path not found` });
}
@@@ -662,23 -681,21 +702,28 @@@
// get secrets under the given folders
const secrets = await secretV2BridgeDAL.findByFolderIds({
- folderIds: folderPolicyPaths.map((p) => p.id)
+ folderIds: folderPolicyPaths.map((p) => p.id),
+ tx
});
+ const folderIdsWithSecrets = new Set(secrets.map((s) => s.folderId));
+ const pathsWithSecrets = folderPolicyPaths.filter((p) => folderIdsWithSecrets.has(p.id));
+ if (!pathsWithSecrets.length) return;
- for await (const folderPolicyPath of folderPolicyPaths) {
- // eslint-disable-next-line no-continue
- if (!secrets.some((s) => s.folderId === folderPolicyPath.id)) continue;
+ const policyByPath = await secretApprovalPolicyService.getSecretApprovalPolicyByPaths(
+ projectId,
+ env.slug,
+ pathsWithSecrets.map((p) => p.path),
+ tx
+ );
- for (const folderPolicyPath of pathsWithSecrets) {
- const policy = policyByPath.get(folderPolicyPath.path);
- if (policy) {
+ const policy = await secretApprovalPolicyService.getSecretApprovalPolicy(
+ projectId,
+ env.slug,
+ folderPolicyPath.path
+ );
+
+ // if there is an enforced policy and there are secrets under the given folder, throw error
+ if (shouldApplyPolicy(policy, actor)) {
throw new BadRequestError({
message: `You cannot delete the selected folder because it contains one or more secrets that are protected by the change policy "${policy.name}" at folder path "${folderPolicyPath.path}". Please remove the secrets at folder path "${folderPolicyPath.path}" and try again.`,
name: "DeleteFolderProtectedByPolicy"