misc: created abstraction for get kms by id

This commit is contained in:
Sheen Capadngan
2024-07-20 02:33:16 +08:00
committed by =
parent ffc1b1ec1c
commit 64869ea8e0
3 changed files with 18 additions and 15 deletions

View File

@@ -627,8 +627,7 @@ export const registerRoutes = async (
projectUserMembershipRoleDAL,
identityProjectMembershipRoleDAL,
keyStore,
kmsService,
kmsDAL
kmsService
});
const projectEnvService = projectEnvServiceFactory({

View File

@@ -666,6 +666,18 @@ export const kmsServiceFactory = ({
};
};
const getKmsById = async (kmsKeyId: string, tx?: Knex) => {
const kms = await kmsDAL.findByIdWithAssociatedKms(kmsKeyId, tx);
if (!kms.id) {
throw new NotFoundError({
message: "KMS not found"
});
}
return kms;
};
const startService = async () => {
const appCfg = getConfig();
// This will switch to a seal process and HMS flow in future
@@ -725,6 +737,7 @@ export const kmsServiceFactory = ({
getProjectSecretManagerKmsKey,
updateProjectSecretManagerKmsKey,
getProjectKeyBackup,
loadProjectKeyBackup
loadProjectKeyBackup,
getKmsById
};
};

View File

@@ -21,7 +21,6 @@ import { TCertificateAuthorityDALFactory } from "../certificate-authority/certif
import { TIdentityOrgDALFactory } from "../identity/identity-org-dal";
import { TIdentityProjectDALFactory } from "../identity-project/identity-project-dal";
import { TIdentityProjectMembershipRoleDALFactory } from "../identity-project/identity-project-membership-role-dal";
import { TKmsKeyDALFactory } from "../kms/kms-key-dal";
import { TKmsServiceFactory } from "../kms/kms-service";
import { TOrgDALFactory } from "../org/org-dal";
import { TOrgServiceFactory } from "../org/org-service";
@@ -82,9 +81,8 @@ type TProjectServiceFactoryDep = {
keyStore: Pick<TKeyStoreFactory, "deleteItem">;
kmsService: Pick<
TKmsServiceFactory,
"updateProjectSecretManagerKmsKey" | "getProjectKeyBackup" | "loadProjectKeyBackup"
"updateProjectSecretManagerKmsKey" | "getProjectKeyBackup" | "loadProjectKeyBackup" | "getKmsById"
>;
kmsDAL: Pick<TKmsKeyDALFactory, "findByIdWithAssociatedKms">;
};
export type TProjectServiceFactory = ReturnType<typeof projectServiceFactory>;
@@ -110,8 +108,7 @@ export const projectServiceFactory = ({
certificateAuthorityDAL,
certificateDAL,
keyStore,
kmsService,
kmsDAL
kmsService
}: TProjectServiceFactoryDep) => {
/*
* Create workspace. Make user the admin
@@ -152,13 +149,7 @@ export const projectServiceFactory = ({
const ghostUser = await orgService.addGhostUser(organization.id, tx);
if (kmsKeyId) {
const kms = await kmsDAL.findByIdWithAssociatedKms(kmsKeyId, tx);
if (!kms.id) {
throw new NotFoundError({
message: "KMS not found"
});
}
const kms = await kmsService.getKmsById(kmsKeyId, tx);
if (kms.orgId !== organization.id) {
throw new BadRequestError({