mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-11 06:29:04 +00:00
chore: refactor
This commit is contained in:
Generated
+2
-1
@@ -37,7 +37,6 @@
|
||||
"@slack/oauth": "^3.0.1",
|
||||
"@slack/web-api": "^7.3.4",
|
||||
"@team-plain/typescript-sdk": "^4.6.1",
|
||||
"@types/sjcl": "^1.0.34",
|
||||
"@ucast/mongo2js": "^1.3.4",
|
||||
"ajv": "^8.12.0",
|
||||
"argon2": "^0.31.2",
|
||||
@@ -119,6 +118,7 @@
|
||||
"@types/prompt-sync": "^4.2.3",
|
||||
"@types/resolve": "^1.20.6",
|
||||
"@types/safe-regex": "^1.1.6",
|
||||
"@types/sjcl": "^1.0.34",
|
||||
"@types/uuid": "^9.0.7",
|
||||
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
||||
"@typescript-eslint/parser": "^6.20.0",
|
||||
@@ -7302,6 +7302,7 @@
|
||||
"version": "1.0.34",
|
||||
"resolved": "https://registry.npmjs.org/@types/sjcl/-/sjcl-1.0.34.tgz",
|
||||
"integrity": "sha512-bQHEeK5DTQRunIfQeUMgtpPsNNCcZyQ9MJuAfW1I7iN0LDunTc78Fu17STbLMd7KiEY/g2zHVApippa70h6HoQ==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/uuid": {
|
||||
|
||||
@@ -80,6 +80,7 @@
|
||||
"@types/prompt-sync": "^4.2.3",
|
||||
"@types/resolve": "^1.20.6",
|
||||
"@types/safe-regex": "^1.1.6",
|
||||
"@types/sjcl": "^1.0.34",
|
||||
"@types/uuid": "^9.0.7",
|
||||
"@typescript-eslint/eslint-plugin": "^6.20.0",
|
||||
"@typescript-eslint/parser": "^6.20.0",
|
||||
@@ -134,7 +135,6 @@
|
||||
"@slack/oauth": "^3.0.1",
|
||||
"@slack/web-api": "^7.3.4",
|
||||
"@team-plain/typescript-sdk": "^4.6.1",
|
||||
"@types/sjcl": "^1.0.34",
|
||||
"@ucast/mongo2js": "^1.3.4",
|
||||
"ajv": "^8.12.0",
|
||||
"argon2": "^0.31.2",
|
||||
|
||||
@@ -7,7 +7,7 @@ import { AuthMode } from "@app/services/auth/auth-type";
|
||||
export const registerExternalMigrationRouter = async (server: FastifyZodProvider) => {
|
||||
server.route({
|
||||
method: "POST",
|
||||
url: "/envkey",
|
||||
url: "/env-key",
|
||||
config: {
|
||||
rateLimit: readLimit
|
||||
},
|
||||
@@ -20,15 +20,12 @@ export const registerExternalMigrationRouter = async (server: FastifyZodProvider
|
||||
})
|
||||
}),
|
||||
response: {
|
||||
200: z.object({
|
||||
success: z.boolean(),
|
||||
error: z.string().optional()
|
||||
})
|
||||
200: z.object({})
|
||||
}
|
||||
},
|
||||
onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),
|
||||
handler: async (req) => {
|
||||
const result = await server.services.migration.importEnvnKeyData({
|
||||
await server.services.migration.importEnvnKeyData({
|
||||
decryptionKey: req.body.decryptionKey,
|
||||
encryptedJson: req.body.encryptedJson,
|
||||
actorId: req.permission.id,
|
||||
@@ -36,7 +33,6 @@ export const registerExternalMigrationRouter = async (server: FastifyZodProvider
|
||||
actorOrgId: req.permission.orgId,
|
||||
actorAuthMethod: req.permission.authMethod
|
||||
});
|
||||
return result;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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