From 12b6f2715142176ce6eedfe15a1d0e924788705e Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Wed, 6 Nov 2024 10:35:27 +0400 Subject: [PATCH 1/2] fix envkey --- .../external-migration-fns.ts | 76 ++++++++++++++----- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/backend/src/services/external-migration/external-migration-fns.ts b/backend/src/services/external-migration/external-migration-fns.ts index 70498e16e..2984fb0e8 100644 --- a/backend/src/services/external-migration/external-migration-fns.ts +++ b/backend/src/services/external-migration/external-migration-fns.ts @@ -126,6 +126,8 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise(); + const processBranches = () => { for (const subEnv of parsedJson.subEnvironments) { const app = parsedJson.apps.find((a) => a.id === subEnv.envParentId); @@ -135,12 +137,21 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise e.id === subEnv.parentEnvironmentId); - infisicalImportData.folders.push({ - name: subEnv.subName, - parentFolderId: subEnv.parentEnvironmentId, - environmentId: branchEnvironment!.id, - id: subEnv.id - }); + // check if the folder already exists in the same parent environment with the same name + + const folderExists = infisicalImportData.folders.some( + (f) => f.name === subEnv.subName && f.parentFolderId === subEnv.parentEnvironmentId + ); + + // No need to map to target ID's here, because we are not dealing with blocks + if (!folderExists) { + infisicalImportData.folders.push({ + name: subEnv.subName, + parentFolderId: subEnv.parentEnvironmentId, + environmentId: branchEnvironment!.id, + id: subEnv.id + }); + } } if (block) { @@ -162,13 +173,22 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise f.name === subEnv.subName && f.parentFolderId === matchingAppEnv.id + ); + + if (folderExists !== -1) { + // 3. Create a folder in the matching app environment + infisicalImportData.folders.push({ + name: subEnv.subName, + parentFolderId: matchingAppEnv.id, + environmentId: matchingAppEnv.id, + id: `${subEnv.id}-${appId}` // Create unique ID for each app's copy of the branch + }); + } else { + // folder already exists, so lets map the old folder id to the new folder id + targetIdToFolderIdsMap.set(subEnv.id, `${subEnv.id}-${appId}`); + } // 4. Process secrets in the block branch for this app const branchSecrets = parsedJson.envs[subEnv.id]?.variables || {}; @@ -408,17 +428,18 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise s.name === secretName && s.environmentId === environmentId + (s) => + s.name === secretName && + (s.environmentId === subEnv?.parentEnvironmentId || s.environmentId === env) && + (s.folderId ? s.folderId === subEnv?.id : true) && + (secretData.val ? s.value === secretData.val : true) ); if (secretData.inheritsEnvironmentId) { const resolvedSecret = findRootInheritedSecret(secretData, secretName, parsedJson.envs); - // Check if there's already a secret with this name in the environment, if there is, we should override it. Because if there's already one, we know its coming from a block. // Variables from the normal environment should take precedence over variables from the block. - if (indexOfExistingSecret !== -1) { // if a existing secret is found, we should replace it directly const newSecret: (typeof infisicalImportData.secrets)[number] = { @@ -456,12 +477,14 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise(); @@ -599,6 +623,8 @@ export const importDataIntoInfisicalFn = async ({ // Skip if we can't find either an environment or folder mapping for this secret if (!originalToNewEnvironmentId.get(secret.environmentId) && !originalToNewFolderId.get(targetId)) { + logger.info({ secret }, "[importDataIntoInfisicalFn]: Could not find environment or folder for secret"); + // eslint-disable-next-line no-continue continue; } @@ -606,10 +632,22 @@ export const importDataIntoInfisicalFn = async ({ if (!mappedToEnvironmentId.has(targetId)) { mappedToEnvironmentId.set(targetId, []); } + + const alreadyHasSecret = mappedToEnvironmentId + .get(targetId)! + .find((el) => el.secretKey === secret.name && el.folderId === secret.folderId); + + if (alreadyHasSecret && alreadyHasSecret.isFromBlock) { + // remove the existing secret if any + mappedToEnvironmentId + .get(targetId)! + .splice(mappedToEnvironmentId.get(targetId)!.indexOf(alreadyHasSecret), 1); + } mappedToEnvironmentId.get(targetId)!.push({ secretKey: secret.name, secretValue: secret.value || "", - folderId: secret.folderId + folderId: secret.folderId, + isFromBlock: secret.appBlockOrderIndex !== undefined }); } From 34d87ca30f65c3ba859d9d3aa8bf880da92c7950 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Wed, 6 Nov 2024 10:49:45 +0400 Subject: [PATCH 2/2] Update external-migration-fns.ts --- .../src/services/external-migration/external-migration-fns.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/services/external-migration/external-migration-fns.ts b/backend/src/services/external-migration/external-migration-fns.ts index 2984fb0e8..7ae0d0aad 100644 --- a/backend/src/services/external-migration/external-migration-fns.ts +++ b/backend/src/services/external-migration/external-migration-fns.ts @@ -173,11 +173,11 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise f.name === subEnv.subName && f.parentFolderId === matchingAppEnv.id ); - if (folderExists !== -1) { + if (!folderExists) { // 3. Create a folder in the matching app environment infisicalImportData.folders.push({ name: subEnv.subName,