From 96f5dc7300a57fcf0d1d531256d22a88c238f90e Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Wed, 16 Oct 2024 01:05:45 +0400 Subject: [PATCH 1/3] Update external-migration-fns.ts --- .../external-migration-fns.ts | 19 +++++++++++++++++-- 1 file changed, 17 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 b299d00c7..08bf8da34 100644 --- a/backend/src/services/external-migration/external-migration-fns.ts +++ b/backend/src/services/external-migration/external-migration-fns.ts @@ -143,16 +143,31 @@ export const importDataIntoInfisicalFn = async ({ logger.error(e, `Failed to import to project [name:${project.name}]`); throw new BadRequestError({ message: `Failed to import to project [name:${project.name}]` }); }); - originalToNewProjectId.set(project.id, newProject.id); } // Import environments if (data.environments) { for await (const environment of data.environments) { - const projectId = originalToNewProjectId.get(environment.projectId)!; + let projectId = originalToNewProjectId.get(environment.projectId); const slug = slugify(`${environment.name}-${alphaNumericNanoId(4)}`); + // case: if for some reason the project ID is not found, we will create a new project and import the corresponding environment into it. + if (!projectId) { + const newProject = await projectService.createProject({ + actor, + actorId, + actorOrgId, + actorAuthMethod, + workspaceName: `project-import-${environment.projectId}`, + createDefaultEnvs: false, + tx + }); + + originalToNewProjectId.set(environment.projectId, newProject.id); + projectId = newProject.id; + } + const existingEnv = await projectEnvDAL.findOne({ projectId, slug }, tx); if (existingEnv) { From d61f36bca8f59520f3354c8df3fb708198927615 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Wed, 16 Oct 2024 01:33:57 +0400 Subject: [PATCH 2/3] requested changes --- .../external-migration-fns.ts | 33 +++++++++++-------- .../external-migration-queue.ts | 13 +++++++- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/backend/src/services/external-migration/external-migration-fns.ts b/backend/src/services/external-migration/external-migration-fns.ts index 08bf8da34..379c3433a 100644 --- a/backend/src/services/external-migration/external-migration-fns.ts +++ b/backend/src/services/external-migration/external-migration-fns.ts @@ -89,6 +89,13 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise project.id === env.projectId)) { + infisicalImportData.projects.push({ name: "Unknown", id: env.projectId }); + } + } + // secrets for (const env of Object.keys(parsedJson.envs)) { if (!env.includes("|")) { @@ -126,6 +133,7 @@ export const importDataIntoInfisicalFn = async ({ const originalToNewProjectId = new Map(); const originalToNewEnvironmentId = new Map(); + const projectsNotImported: string[] = []; await projectDAL.transaction(async (tx) => { for await (const project of data.projects) { @@ -149,23 +157,13 @@ export const importDataIntoInfisicalFn = async ({ // Import environments if (data.environments) { for await (const environment of data.environments) { - let projectId = originalToNewProjectId.get(environment.projectId); + const projectId = originalToNewProjectId.get(environment.projectId); const slug = slugify(`${environment.name}-${alphaNumericNanoId(4)}`); - // case: if for some reason the project ID is not found, we will create a new project and import the corresponding environment into it. if (!projectId) { - const newProject = await projectService.createProject({ - actor, - actorId, - actorOrgId, - actorAuthMethod, - workspaceName: `project-import-${environment.projectId}`, - createDefaultEnvs: false, - tx - }); - - originalToNewProjectId.set(environment.projectId, newProject.id); - projectId = newProject.id; + projectsNotImported.push(environment.projectId); + // eslint-disable-next-line no-continue + continue; } const existingEnv = await projectEnvDAL.findOne({ projectId, slug }, tx); @@ -195,6 +193,11 @@ export const importDataIntoInfisicalFn = async ({ >(); for (const secret of data.secrets) { + if (!originalToNewEnvironmentId.get(secret.environmentId)) { + // eslint-disable-next-line no-continue + continue; + } + if (!mappedToEnvironmentId.has(secret.environmentId)) { mappedToEnvironmentId.set(secret.environmentId, []); } @@ -269,4 +272,6 @@ export const importDataIntoInfisicalFn = async ({ } } }); + + return { projectsNotImported }; }; diff --git a/backend/src/services/external-migration/external-migration-queue.ts b/backend/src/services/external-migration/external-migration-queue.ts index 0ee51c5fa..76c7ceb1e 100644 --- a/backend/src/services/external-migration/external-migration-queue.ts +++ b/backend/src/services/external-migration/external-migration-queue.ts @@ -97,7 +97,7 @@ export const externalMigrationQueueFactory = ({ const decryptedJson = JSON.parse(decrypted) as TImportInfisicalDataCreate; - await importDataIntoInfisicalFn({ + const { projectsNotImported } = await importDataIntoInfisicalFn({ input: decryptedJson, projectDAL, projectEnvDAL, @@ -112,6 +112,17 @@ export const externalMigrationQueueFactory = ({ secretV2BridgeService }); + if (projectsNotImported.length) { + logger.info( + { + actorEmail, + actorOrgId: decryptedJson.actorOrgId, + projectsNotImported + }, + "One or more projects were not imported during import from external source" + ); + } + await smtpService.sendMail({ recipients: [actorEmail], subjectLine: "Infisical import successful", From 5d5da97b45d4a1a213c66f764a2f51ceff4d0d9f Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Wed, 16 Oct 2024 01:58:06 +0400 Subject: [PATCH 3/3] Update external-migration-fns.ts --- .../services/external-migration/external-migration-fns.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/backend/src/services/external-migration/external-migration-fns.ts b/backend/src/services/external-migration/external-migration-fns.ts index 379c3433a..6d996022a 100644 --- a/backend/src/services/external-migration/external-migration-fns.ts +++ b/backend/src/services/external-migration/external-migration-fns.ts @@ -89,13 +89,6 @@ export const parseEnvKeyDataFn = async (decryptedJson: string): Promise project.id === env.projectId)) { - infisicalImportData.projects.push({ name: "Unknown", id: env.projectId }); - } - } - // secrets for (const env of Object.keys(parsedJson.envs)) { if (!env.includes("|")) {