Fix: Better error message on project not found during bot lookup

This commit is contained in:
Daniel Hougaard
2024-05-23 15:47:08 +02:00
parent 290d99e02c
commit 68d1849ba0
3 changed files with 16 additions and 6 deletions
@@ -3,6 +3,7 @@ import { decryptAsymmetric, infisicalSymmetricDecrypt } from "@app/lib/crypto/en
import { BadRequestError } from "@app/lib/errors"; import { BadRequestError } from "@app/lib/errors";
import { TProjectBotDALFactory } from "@app/services/project-bot/project-bot-dal"; import { TProjectBotDALFactory } from "@app/services/project-bot/project-bot-dal";
import { TProjectDALFactory } from "../project/project-dal";
import { TGetPrivateKeyDTO } from "./project-bot-types"; import { TGetPrivateKeyDTO } from "./project-bot-types";
export const getBotPrivateKey = ({ bot }: TGetPrivateKeyDTO) => export const getBotPrivateKey = ({ bot }: TGetPrivateKeyDTO) =>
@@ -13,11 +14,20 @@ export const getBotPrivateKey = ({ bot }: TGetPrivateKeyDTO) =>
ciphertext: bot.encryptedPrivateKey ciphertext: bot.encryptedPrivateKey
}); });
export const getBotKeyFnFactory = (projectBotDAL: TProjectBotDALFactory) => { export const getBotKeyFnFactory = (
projectBotDAL: TProjectBotDALFactory,
projectDAL: Pick<TProjectDALFactory, "findById">
) => {
const getBotKeyFn = async (projectId: string) => { const getBotKeyFn = async (projectId: string) => {
const bot = await projectBotDAL.findOne({ projectId }); const project = await projectDAL.findById(projectId);
if (!project)
throw new BadRequestError({
message: "Project not found during bot lookup."
});
if (!bot) throw new BadRequestError({ message: "failed to find bot key" }); const bot = await projectBotDAL.findOne({ projectId: project.id });
if (!bot) throw new BadRequestError({ message: "Failed to find bot key" });
if (!bot.isActive) throw new BadRequestError({ message: "Bot is not active" }); if (!bot.isActive) throw new BadRequestError({ message: "Bot is not active" });
if (!bot.encryptedProjectKeyNonce || !bot.encryptedProjectKey) if (!bot.encryptedProjectKeyNonce || !bot.encryptedProjectKey)
throw new BadRequestError({ message: "Encryption key missing" }); throw new BadRequestError({ message: "Encryption key missing" });
@@ -25,7 +25,7 @@ export const projectBotServiceFactory = ({
projectDAL, projectDAL,
permissionService permissionService
}: TProjectBotServiceFactoryDep) => { }: TProjectBotServiceFactoryDep) => {
const getBotKeyFn = getBotKeyFnFactory(projectBotDAL); const getBotKeyFn = getBotKeyFnFactory(projectBotDAL, projectDAL);
const getBotKey = async (projectId: string) => { const getBotKey = async (projectId: string) => {
return getBotKeyFn(projectId); return getBotKeyFn(projectId);
+2 -2
View File
@@ -608,7 +608,7 @@ export const createManySecretsRawFnFactory = ({
secretVersionTagDAL, secretVersionTagDAL,
folderDAL folderDAL
}: TCreateManySecretsRawFnFactory) => { }: TCreateManySecretsRawFnFactory) => {
const getBotKeyFn = getBotKeyFnFactory(projectBotDAL); const getBotKeyFn = getBotKeyFnFactory(projectBotDAL, projectDAL);
const createManySecretsRawFn = async ({ const createManySecretsRawFn = async ({
projectId, projectId,
environment, environment,
@@ -706,7 +706,7 @@ export const updateManySecretsRawFnFactory = ({
secretVersionTagDAL, secretVersionTagDAL,
folderDAL folderDAL
}: TUpdateManySecretsRawFnFactory) => { }: TUpdateManySecretsRawFnFactory) => {
const getBotKeyFn = getBotKeyFnFactory(projectBotDAL); const getBotKeyFn = getBotKeyFnFactory(projectBotDAL, projectDAL);
const updateManySecretsRawFn = async ({ const updateManySecretsRawFn = async ({
projectId, projectId,
environment, environment,