mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 18:41:21 +00:00
chore: refactor
This commit is contained in:
@@ -3,6 +3,8 @@ import sjcl from "sjcl";
|
||||
import tweetnacl from "tweetnacl";
|
||||
import tweetnaclUtil from "tweetnacl-util";
|
||||
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
|
||||
import { InfisicalImportData, TEnvKeyExportJSON } from "./external-migration-types";
|
||||
|
||||
const { codec, hash } = sjcl;
|
||||
@@ -16,7 +18,7 @@ export const decryptEnvKeyData = async (decryptionKey: string, encryptedJson: {
|
||||
const decrypted = secretbox.open(encryptedData, nonce, key);
|
||||
|
||||
if (!decrypted) {
|
||||
throw new Error("Decryption failed, please check the entered encryption key");
|
||||
throw new BadRequestError({ message: "Decryption failed, please check the entered encryption key" });
|
||||
}
|
||||
|
||||
const decryptedJson = tweetnaclUtil.encodeUTF8(decrypted);
|
||||
@@ -33,7 +35,7 @@ export const parseEnvKeyData = async (decryptedJson: string): Promise<InfisicalI
|
||||
};
|
||||
|
||||
parsedJson.apps.forEach((app: { name: string; id: string }) => {
|
||||
infisicalImportData.projects?.set(app.id, { name: app.name, id: app.id });
|
||||
infisicalImportData.projects.set(app.id, { name: app.name, id: app.id });
|
||||
});
|
||||
|
||||
// string to string map for env templates
|
||||
|
||||
@@ -36,11 +36,7 @@ export const externalMigrationServiceFactory = ({
|
||||
}: TImportInfisicalDataCreate) => {
|
||||
// Import data to infisical
|
||||
if (!data || !data.projects) {
|
||||
logger.error("No projects found in data");
|
||||
return {
|
||||
success: false,
|
||||
message: "No projects found in data"
|
||||
};
|
||||
throw new BadRequestError({ message: "No projects found in data" });
|
||||
}
|
||||
|
||||
const orginalToNewProjectId = new Map<string, string>();
|
||||
@@ -55,13 +51,12 @@ export const externalMigrationServiceFactory = ({
|
||||
actorId,
|
||||
actorOrgId,
|
||||
actorAuthMethod,
|
||||
workspaceName: project?.name,
|
||||
workspaceName: project.name,
|
||||
createDefaultEnvs: false
|
||||
})
|
||||
.then((projectResponse) => {
|
||||
if (!projectResponse) {
|
||||
logger.error(`Failed to import project: [name:${project.name}] [id:${id}]`);
|
||||
throw new Error(`Failed to import project: [name:${project.name}] [id:${id}]`);
|
||||
throw new BadRequestError({ message: `Failed to import to project [name:${project.name}] [id:${id}]` });
|
||||
}
|
||||
orginalToNewProjectId.set(project.id, projectResponse.id);
|
||||
});
|
||||
@@ -86,19 +81,14 @@ export const externalMigrationServiceFactory = ({
|
||||
})
|
||||
});
|
||||
if (!response) {
|
||||
logger.error(`Failed to invite user to projects: [userId:${actorId}]`);
|
||||
return {
|
||||
success: false,
|
||||
message: `Failed to invite user to project: [userId:${actorId}]`
|
||||
};
|
||||
throw new BadRequestError({ message: `Failed to invite user to projects: [userId:${actorId}]` });
|
||||
}
|
||||
|
||||
// Import environments
|
||||
if (data.environments) {
|
||||
for (const [id, environment] of data.environments) {
|
||||
for await (const [id, environment] of data.environments) {
|
||||
try {
|
||||
// TODO: we can create envs parallely once the position constraint is handled differently
|
||||
// eslint-disable-next-line
|
||||
const newEnvironment = await projectEnvService.createEnvironment({
|
||||
actor,
|
||||
actorId,
|
||||
@@ -126,7 +116,7 @@ export const externalMigrationServiceFactory = ({
|
||||
|
||||
// Import secrets
|
||||
if (data.secrets) {
|
||||
for (const [id, secret] of data.secrets) {
|
||||
for await (const [id, secret] of data.secrets) {
|
||||
const dataProjectId = data.environments?.get(secret.environmentId)?.projectId;
|
||||
if (!dataProjectId) {
|
||||
logger.error(`Failed to import secret: [name:${secret.name}] [id:${id}], project not found`);
|
||||
@@ -137,7 +127,6 @@ export const externalMigrationServiceFactory = ({
|
||||
}
|
||||
const projectId = orginalToNewProjectId.get(dataProjectId);
|
||||
// TODO: we can create secrets parallely once the KMS ID bug on create is fixed
|
||||
// eslint-disable-next-line
|
||||
const newSecret = await secretService.createSecretRaw({
|
||||
actorId,
|
||||
actor,
|
||||
@@ -151,18 +140,12 @@ export const externalMigrationServiceFactory = ({
|
||||
secretValue: secret.value
|
||||
});
|
||||
if (!newSecret) {
|
||||
logger.error(`Failed to import secret: [name:${secret.name}] [id:${id}]`);
|
||||
return {
|
||||
success: false,
|
||||
message: `Failed to import secret: [name:${secret.name}] [id:${id}]`
|
||||
};
|
||||
throw new BadRequestError({ message: `Failed to import secret: [name:${secret.name}] [id:${id}]` });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
const importEnvnKeyData = async ({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ActorAuthMethod, ActorType } from "../auth/auth-type";
|
||||
|
||||
export type InfisicalImportData = {
|
||||
projects?: Map<string, { name: string; id: string }>;
|
||||
projects: Map<string, { name: string; id: string }>;
|
||||
|
||||
environments?: Map<
|
||||
string,
|
||||
|
||||
@@ -208,7 +208,15 @@ export const projectServiceFactory = ({
|
||||
);
|
||||
|
||||
// set default environments and root folder for provided environments
|
||||
let envs;
|
||||
let envs: {
|
||||
id: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
projectId: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
position: number;
|
||||
}[] = [];
|
||||
if (createDefaultEnvs) {
|
||||
envs = await projectEnvDAL.insertMany(
|
||||
DEFAULT_PROJECT_ENVS.map((el, i) => ({ ...el, projectId: project.id, position: i + 1 })),
|
||||
@@ -364,7 +372,7 @@ export const projectServiceFactory = ({
|
||||
|
||||
return {
|
||||
...project,
|
||||
environments: envs || [],
|
||||
environments: envs,
|
||||
_id: project.id
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user