Skip to content

Commit

Permalink
fix: resolved infinite recursion cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhil Mohan authored and akhilmhdh committed Aug 1, 2023
1 parent 6574b64 commit 086652a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
21 changes: 16 additions & 5 deletions backend/src/helpers/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -991,11 +991,16 @@ const recursivelyExpandSecret = async (
expandedSec: Record<string, string>,
interpolatedSec: Record<string, string>,
fetchCrossEnv: (env: string, secPath: string[], secKey: string) => Promise<string>,
recursionChainBreaker: Record<string, boolean>,
key: string
) => {
if (expandedSec?.[key]) {
return expandedSec[key];
}
if (recursionChainBreaker?.[key]) {
return "";
}
recursionChainBreaker[key] = true;

let interpolatedValue = interpolatedSec[key];
if (!interpolatedValue) {
Expand All @@ -1013,12 +1018,13 @@ const recursivelyExpandSecret = async (
expandedSec,
interpolatedSec,
fetchCrossEnv,
recursionChainBreaker,
interpolationKey
);
if (val) {
interpolatedValue = interpolatedValue.replace(interpolationSyntax, val);
interpolatedValue = interpolatedValue.replaceAll(interpolationSyntax, val);
}
return;
continue;
}

if (entities.length > 1) {
Expand All @@ -1027,11 +1033,12 @@ const recursivelyExpandSecret = async (
const secRefKey = entities[entities.length - 1];

const val = await fetchCrossEnv(secRefEnv, secRefPath, secRefKey);
interpolatedValue = interpolatedValue.replace(interpolationSyntax, val);
interpolatedValue = interpolatedValue.replaceAll(interpolationSyntax, val);
}
}
}

expandedSec[key] = interpolatedValue;
return interpolatedValue;
};

Expand All @@ -1057,17 +1064,21 @@ export const expandSecrets = async (
for (const key of Object.keys(secrets)) {
if (expandedSec?.[key]) {
secrets[key].value = expandedSec[key];
return;
continue;
}

// this is to avoid recursion loop. So the graph should be direct graph rather than cyclic
// so for any recursion building if there is an entity two times same key meaning it will be looped
const recursionChainBreaker: Record<string, boolean> = {};
const expandedVal = await recursivelyExpandSecret(
expandedSec,
interpolatedSec,
crossSecEnvFetch,
recursionChainBreaker,
key
);

secrets[key].value = expandedVal || "";
secrets[key].value = expandedVal;
}

return secrets;
Expand Down
10 changes: 6 additions & 4 deletions backend/src/integrations/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ import _ from "lodash";
import sodium from "libsodium-wrappers";
import { standardRequest } from "../config/request";

const getSecretKeyValuePair = (secrets: Record<string, { value: string; comment?: string }>) =>
const getSecretKeyValuePair = (
secrets: Record<string, { value: string; comment?: string } | null>
) =>
Object.keys(secrets).reduce<Record<string, string>>((prev, key) => {
prev[key] = secrets[key].value;
if (secrets[key]) prev[key] = secrets[key]?.value || "";
return prev;
}, {});

Expand Down Expand Up @@ -667,7 +669,7 @@ const syncSecretsHeroku = async ({
accessToken
}: {
integration: IIntegration;
secrets: Record<string, { value: string; comment?: string }>;
secrets: Record<string, { value: string; comment?: string } | null>;
accessToken: string;
}) => {
const herokuSecrets = (
Expand All @@ -682,7 +684,7 @@ const syncSecretsHeroku = async ({

Object.keys(herokuSecrets).forEach((key) => {
if (!(key in secrets)) {
delete secrets[key];
secrets[key] = null;
}
});

Expand Down
19 changes: 5 additions & 14 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"compilerOptions": {
"target": "es2016",
"lib": [
"es6"
],
"lib": ["es6", "es2021"],
"module": "commonjs",
"rootDir": "src",
"resolveJsonModule": true,
Expand All @@ -15,15 +13,8 @@
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true,
"typeRoots": [
"./src/types",
"./node_modules/@types"
]
"typeRoots": ["./src/types", "./node_modules/@types"]
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

0 comments on commit 086652a

Please sign in to comment.