feat(server): added cyclic detector

This commit is contained in:
Akhil Mohan
2024-04-26 01:00:42 +05:30
parent 35e38c23dd
commit 2d2d9a5987
@@ -19,12 +19,14 @@ type TSecretImportSecrets = {
}; };
const LEVEL_BREAK = 10; const LEVEL_BREAK = 10;
const getImportUniqKey = (envSlug: string, path: string) => `${envSlug}=${path}`;
export const fnSecretsFromImports = async ({ export const fnSecretsFromImports = async ({
allowedImports, allowedImports: nonCyclicImports,
folderDAL, folderDAL,
secretDAL, secretDAL,
secretImportDAL, secretImportDAL,
depth = 0 depth = 0,
cyclicDetector = new Set()
}: { }: {
allowedImports: (Omit<TSecretImports, "importEnv"> & { allowedImports: (Omit<TSecretImports, "importEnv"> & {
importEnv: { id: string; slug: string; name: string }; importEnv: { id: string; slug: string; name: string };
@@ -33,10 +35,15 @@ export const fnSecretsFromImports = async ({
secretDAL: Pick<TSecretDALFactory, "find">; secretDAL: Pick<TSecretDALFactory, "find">;
secretImportDAL: Pick<TSecretImportDALFactory, "findByFolderIds">; secretImportDAL: Pick<TSecretImportDALFactory, "findByFolderIds">;
depth?: number; depth?: number;
cyclicDetector?: Set<string>;
}) => { }) => {
// avoid going more than a depth // avoid going more than a depth
if (depth >= LEVEL_BREAK) return []; if (depth >= LEVEL_BREAK) return [];
const allowedImports = nonCyclicImports.filter(
({ importPath, importEnv }) => !cyclicDetector.has(getImportUniqKey(importEnv.slug, importPath))
);
const importedFolders = ( const importedFolders = (
await folderDAL.findByManySecretPath( await folderDAL.findByManySecretPath(
allowedImports.map(({ importEnv, importPath }) => ({ allowedImports.map(({ importEnv, importPath }) => ({
@@ -63,6 +70,9 @@ export const fnSecretsFromImports = async ({
const importedSecretsGroupByFolderId = groupBy(importedSecrets, (i) => i.folderId); const importedSecretsGroupByFolderId = groupBy(importedSecrets, (i) => i.folderId);
allowedImports.forEach(({ importPath, importEnv }) => {
cyclicDetector.add(getImportUniqKey(importEnv.slug, importPath));
});
// now we need to check recursively deeper imports made inside other imports // now we need to check recursively deeper imports made inside other imports
// we go level wise meaning we take all imports of a tree level and then go deeper ones level by level // we go level wise meaning we take all imports of a tree level and then go deeper ones level by level
const deeperImports = await secretImportDAL.findByFolderIds(importedFolderIds); const deeperImports = await secretImportDAL.findByFolderIds(importedFolderIds);
@@ -73,7 +83,8 @@ export const fnSecretsFromImports = async ({
secretImportDAL, secretImportDAL,
folderDAL, folderDAL,
secretDAL, secretDAL,
depth: depth + 1 depth: depth + 1,
cyclicDetector
}); });
} }
const secretsFromdeeperImportGroupedByFolderId = groupBy(secretsFromDeeperImports, (i) => i.importFolderId); const secretsFromdeeperImportGroupedByFolderId = groupBy(secretsFromDeeperImports, (i) => i.importFolderId);