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
feat: refactored getPathfromId to folder service
Akhil Mohan91947df5f68e
diff --git a/backend/src/controllers/v1/secretImportController.ts b/backend/src/controllers/v1/secretImportController.tsindex 368ffccff7..76b4945bdc 100644--- a/backend/src/controllers/v1/secretImportController.ts+++ b/backend/src/controllers/v1/secretImportController.ts@@ -1,29 +1,14 @@import { Request, Response } from "express";-import path from "path";import { isValidScope, validateMembership } from "../../helpers";import { ServiceTokenData } from "../../models";-import Folder, { TFolderRootSchema } from "../../models/folder";+import Folder from "../../models/folder";import SecretImport from "../../models/secretImports";-import { searchByFolderIdWithDir } from "../../services/FolderService";import { getAllImportedSecrets } from "../../services/SecretImportService";+import { getFolderWithPathFromId } from "../../services/FolderService";import { BadRequestError, ResourceNotFoundError,UnauthorizedRequestError } from "../../utils/errors";import { ADMIN, MEMBER } from "../../variables";import { EEAuditLogService } from "../../ee/services";import { EventType } from "../../ee/models";-import { getFolderPath } from "../../services/FolderService";--const getFolderWithPathFromId = (folders: TFolderRootSchema, parentFolderId: string) => {- const search = searchByFolderIdWithDir(folders.nodes, parentFolderId);- if (!search) {- throw { message: "Folder permission denied" };- }- const { folder, dir } = search;- const folderPath = path.join(- "/",- ...dir.filter(({ name }) => name !== "root").map(({ name }) => name)- );- return { folder, folderPath, dir };-};export const createSecretImport = async (req: Request, res: Response) => {const { workspaceId, environment, folderId, secretImport } = req.body;@@ -34,12 +19,14 @@ export const createSecretImport = async (req: Request, res: Response) => {}).lean();if (!folders && folderId !== "root") {- throw BadRequestError({ message: "Folder doesn't exist" });+ throw ResourceNotFoundError({+ message: "Failed to find folder"+ });}let secretPath = "/";if (folders) {- const { folderPath } = getFolderWithPathFromId(folders, folderId);+ const { folderPath } = getFolderWithPathFromId(folders.nodes, folderId);secretPath = folderPath;}if (req.authData.authPayload instanceof ServiceTokenData) {@@ -56,16 +43,7 @@ export const createSecretImport = async (req: Request, res: Response) => {folderId});- const folders = await Folder.findOne({- workspace: workspaceId,- environment,- }).lean();-- if (!folders) throw ResourceNotFoundError({- message: "Failed to find folder"- });-- const importToSecretPath = await getFolderPath(folders, folderId);+ const importToSecretPath = folders?getFolderWithPathFromId(folders.nodes, folderId).folderPath:"/";if (!importSecDoc) {const doc = new SecretImport({@@ -154,7 +132,7 @@ export const updateSecretImport = async (req: Request, res: Response) => {let secretPath = "/";if (folders) {- const { folderPath } = getFolderWithPathFromId(folders, importSecDoc.folderId);+ const { folderPath } = getFolderWithPathFromId(folders.nodes, importSecDoc.folderId);secretPath = folderPath;}@@ -182,7 +160,7 @@ export const updateSecretImport = async (req: Request, res: Response) => {message: "Failed to find folder"});- const importToSecretPath = await getFolderPath(folders, importSecDoc.folderId);+ const importToSecretPath = folders?getFolderWithPathFromId(folders.nodes, importSecDoc.folderId).folderPath:"/";await EEAuditLogService.createAuditLog(req.authData,@@ -227,7 +205,7 @@ export const deleteSecretImport = async (req: Request, res: Response) => {let secretPath = "/";if (folders) {- const { folderPath } = getFolderWithPathFromId(folders, importSecDoc.folderId);+ const { folderPath } = getFolderWithPathFromId(folders.nodes, importSecDoc.folderId);secretPath = folderPath;}@@ -255,7 +233,7 @@ export const deleteSecretImport = async (req: Request, res: Response) => {message: "Failed to find folder"});- const importToSecretPath = await getFolderPath(folders, importSecDoc.folderId);+ const importToSecretPath = folders?getFolderWithPathFromId(folders.nodes, importSecDoc.folderId).folderPath:"/";await EEAuditLogService.createAuditLog(req.authData,@@ -299,7 +277,7 @@ export const getSecretImports = async (req: Request, res: Response) => {let secretPath = "/";if (folders) {- const { folderPath } = getFolderWithPathFromId(folders, importSecDoc.folderId);+ const { folderPath } = getFolderWithPathFromId(folders.nodes, importSecDoc.folderId);secretPath = folderPath;}@@ -341,7 +319,7 @@ export const getAllSecretsFromImport = async (req: Request, res: Response) => {let secretPath = "/";if (folders) {- const { folderPath } = getFolderWithPathFromId(folders, importSecDoc.folderId);+ const { folderPath } = getFolderWithPathFromId(folders.nodes, importSecDoc.folderId);secretPath = folderPath;}diff --git a/backend/src/controllers/v1/secretsFolderController.ts b/backend/src/controllers/v1/secretsFolderController.tsindex 2cb74b55fb..46d4c3c1f9 100644--- a/backend/src/controllers/v1/secretsFolderController.ts+++ b/backend/src/controllers/v1/secretsFolderController.ts@@ -1,39 +1,24 @@import { Request, Response } from "express";import { Types } from "mongoose";-import path from "path";import { EventType, FolderVersion } from "../../ee/models";import { EEAuditLogService, EESecretService } from "../../ee/services";import { validateMembership } from "../../helpers/membership";import { isValidScope } from "../../helpers/secrets";import { Secret, ServiceTokenData } from "../../models";-import Folder, { TFolderRootSchema } from "../../models/folder";+import Folder from "../../models/folder";import {appendFolder,deleteFolderById,generateFolderId,getAllFolderIds,getFolderByPath,- getFolderPath,+ getFolderWithPathFromId,getParentFromFolderId,- searchByFolderIdWithDir,validateFolderName} from "../../services/FolderService";import { BadRequestError, UnauthorizedRequestError } from "../../utils/errors";import { ADMIN, MEMBER } from "../../variables";-const getFolderWithPathFromId = (folders: TFolderRootSchema, parentFolderId: string) => {- const search = searchByFolderIdWithDir(folders.nodes, parentFolderId);- if (!search) {- throw { message: "Folder permission denied" };- }- const { folder, dir } = search;- const folderPath = path.join(- "/",- ...dir.filter(({ name }) => name !== "root").map(({ name }) => name)- );- return { folder, folderPath, dir };-};-// verify workspace id/environmentexport const createFolder = async (req: Request, res: Response) => {const { workspaceId, environment, folderName, parentFolderId } = req.body;@@ -106,7 +91,7 @@ export const createFolder = async (req: Request, res: Response) => {await Folder.findByIdAndUpdate(folders._id, folders);const { folder: parentFolder, folderPath: parentFolderPath } = getFolderWithPathFromId(- folders,+ folders.nodes,parentFolderId || "root");@@ -137,7 +122,7 @@ export const createFolder = async (req: Request, res: Response) => {folderId: parentFolderId});- const folderPath = await getFolderPath(folders, folder.id);+ const {folderPath} = getFolderWithPathFromId(folders.nodes, folder.id);await EEAuditLogService.createAuditLog(req.authData,@@ -192,7 +177,7 @@ export const updateFolderById = async (req: Request, res: Response) => {}if (req.authData.authPayload instanceof ServiceTokenData) {- const { folderPath: secretPath } = getFolderWithPathFromId(folders, parentFolder.id);+ const { folderPath: secretPath } = getFolderWithPathFromId(folders.nodes, parentFolder.id);// root checkconst isValidScopeAccess = isValidScope(req.authData.authPayload, environment, secretPath);if (!isValidScopeAccess) {@@ -200,6 +185,7 @@ export const updateFolderById = async (req: Request, res: Response) => {}}+ const oldFolderName = folder.name;parentFolder.version += 1;folder.name = name;@@ -217,7 +203,7 @@ export const updateFolderById = async (req: Request, res: Response) => {folderId: parentFolder.id});- const folderPath = await getFolderPath(folders, folder.id);+ const {folderPath} = getFolderWithPathFromId(folders.nodes, folder.id);await EEAuditLogService.createAuditLog(req.authData,@@ -260,7 +246,7 @@ export const deleteFolder = async (req: Request, res: Response) => {});}- const folderPath = await getFolderPath(folders, folderId);+ const {folderPath} = getFolderWithPathFromId(folders.nodes, folderId);const delOp = deleteFolderById(folders.nodes, folderId);if (!delOp) {@@ -269,7 +255,7 @@ export const deleteFolder = async (req: Request, res: Response) => {const { deletedNode: delFolder, parent: parentFolder } = delOp;if (req.authData.authPayload instanceof ServiceTokenData) {- const { folderPath: secretPath } = getFolderWithPathFromId(folders, parentFolder.id);+ const { folderPath: secretPath } = getFolderWithPathFromId(folders.nodes, parentFolder.id);const isValidScopeAccess = isValidScope(req.authData.authPayload, environment, secretPath);if (!isValidScopeAccess) {throw UnauthorizedRequestError({ message: "Folder Permission Denied" });@@ -384,7 +370,7 @@ export const getFolders = async (req: Request, res: Response) => {return;}- const { folder, folderPath, dir } = getFolderWithPathFromId(folders, parentFolderId);+ const { folder, folderPath, dir } = getFolderWithPathFromId(folders.nodes, parentFolderId);if (req.authData.authPayload instanceof ServiceTokenData) {const isValidScopeAccess = isValidScope(req.authData.authPayload, environment, folderPath);if (!isValidScopeAccess) {diff --git a/backend/src/services/FolderService.ts b/backend/src/services/FolderService.tsindex 8e1c00750a..81f169f4b3 100644--- a/backend/src/services/FolderService.ts+++ b/backend/src/services/FolderService.ts@@ -1,7 +1,7 @@import { nanoid } from "nanoid";import { Types } from "mongoose";-import Folder, { TFolderRootSchema, TFolderSchema } from "../models/folder";-import { ResourceNotFoundError } from "../utils/errors";+import Folder, { TFolderSchema } from "../models/folder";+import path from "path";type TAppendFolderDTO = {folderName: string;@@ -59,7 +59,7 @@ const appendChild = (folders: TFolderSchema, folderName: string) => {id,name: folderName,children: [],- version: 1,+ version: 1});return { id, name: folderName };};@@ -109,10 +109,7 @@ export const deleteFolderById = (folders: TFolderSchema, folderId: string) => {};// bfs but return parent of the folderID-export const getParentFromFolderId = (- folders: TFolderSchema,- folderId: string-) => {+export const getParentFromFolderId = (folders: TFolderSchema, folderId: string) => {const queue = [folders];while (queue.length) {const folder = queue.pop() as TFolderSchema;@@ -141,10 +138,7 @@ export const getAllFolderIds = (folders: TFolderSchema) => {// We then record the number of childs of each root node// When we reach leaf node or when all childs of a root node are visited// We remove it from path recorded by using the total child record-export const searchByFolderIdWithDir = (- folders: TFolderSchema,- folderId: string-) => {+export const searchByFolderIdWithDir = (folders: TFolderSchema, folderId: string) => {const stack = [folders];const dir: Array<{ id: string; name: string }> = [];const hits: Record<string, number> = {};@@ -173,19 +167,19 @@ export const searchByFolderIdWithDir = (return;};-export const getFolderPath = (- folders: TFolderRootSchema,- folderId: string- ) => {- const folderBySearch = searchByFolderIdWithDir(folders.nodes, folderId);-- if (!folderBySearch) throw ResourceNotFoundError({- message: "Failed to find folder"- });-- const folderPath = folderBySearch.dir.map((folder) => folder.name).join("/");- return folderPath;+// used for get folder path from id+export const getFolderWithPathFromId = (folders: TFolderSchema, parentFolderId: string) => {+ const search = searchByFolderIdWithDir(folders, parentFolderId);+ if (!search) {+ throw { message: "Folder permission denied" };}+ const { folder, dir } = search;+ const folderPath = path.join(+ "/",+ ...dir.filter(({ name }) => name !== "root").map(({ name }) => name)+ );+ return { folder, folderPath, dir };+};// to get folder of a path given// Like /frontend/folder#1@@ -216,7 +210,7 @@ export const getFolderIdFromServiceToken = async () => {const folders = await Folder.findOne({workspace: workspaceId,- environment,+ environment});if (!folders) {