misc: moved away from dedicated slack admin config

This commit is contained in:
Sheen Capadngan
2024-09-08 17:00:50 +08:00
parent ecf177fecc
commit 17b0d0081d
13 changed files with 123 additions and 188 deletions
@@ -1,11 +0,0 @@
import { TDbClient } from "@app/db";
import { TableName } from "@app/db/schemas";
import { ormify } from "@app/lib/knex";
export type TAdminSlackConfigDALFactory = ReturnType<typeof adminSlackConfigDALFactory>;
export const adminSlackConfigDALFactory = (db: TDbClient) => {
const adminSlackConfigOrm = ormify(db, TableName.AdminSlackConfig);
return adminSlackConfigOrm;
};
-29
View File
@@ -6,12 +6,9 @@ import { logger } from "@app/lib/logger";
import { TKmsServiceFactory } from "../kms/kms-service";
import { KmsDataKey } from "../kms/kms-types";
import { TProjectDALFactory } from "../project/project-dal";
import { TAdminSlackConfigDALFactory } from "./admin-slack-config-dal";
import { TProjectSlackConfigDALFactory } from "./project-slack-config-dal";
import { SlackTriggerFeature } from "./slack-types";
const ADMIN_CONFIG_DB_UUID = "00000000-0000-0000-0000-000000000000";
export const getCustomSlackBotManifest = () => {
const appCfg = getConfig();
@@ -142,29 +139,3 @@ export const triggerSlackNotification = async ({
.catch((err) => void logger.error(err));
}
};
export const getAdminSlackCredentials = async ({
adminSlackConfigDAL,
kmsService
}: {
adminSlackConfigDAL: Pick<TAdminSlackConfigDALFactory, "findById">;
kmsService: Pick<TKmsServiceFactory, "encryptWithRootKey" | "decryptWithRootKey">;
}) => {
const adminSlackConfig = await adminSlackConfigDAL.findById(ADMIN_CONFIG_DB_UUID);
let clientId = "";
let clientSecret = "";
const decrypt = await kmsService.decryptWithRootKey();
if (adminSlackConfig.encryptedClientId) {
clientId = (await decrypt({ cipherTextBlob: adminSlackConfig.encryptedClientId })).toString();
}
if (adminSlackConfig.encryptedClientSecret) {
clientSecret = (await decrypt({ cipherTextBlob: adminSlackConfig.encryptedClientSecret })).toString();
}
return {
clientId,
clientSecret
};
};
+15 -16
View File
@@ -8,10 +8,10 @@ import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { TKmsServiceFactory } from "../kms/kms-service";
import { KmsDataKey } from "../kms/kms-types";
import { getServerCfg } from "../super-admin/super-admin-service";
import { TWorkflowIntegrationDALFactory } from "../workflow-integration/workflow-integration-dal";
import { WorkflowIntegration } from "../workflow-integration/workflow-integration-types";
import { TAdminSlackConfigDALFactory } from "./admin-slack-config-dal";
import { fetchSlackChannels, getAdminSlackCredentials } from "./slack-fns";
import { fetchSlackChannels } from "./slack-fns";
import { TSlackIntegrationDALFactory } from "./slack-integration-dal";
import {
TCompleteSlackIntegrationDTO,
@@ -36,7 +36,6 @@ type TSlackServiceFactoryDep = {
>;
permissionService: Pick<TPermissionServiceFactory, "getProjectPermission" | "getOrgPermission">;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey" | "encryptWithRootKey" | "decryptWithRootKey">;
adminSlackConfigDAL: Pick<TAdminSlackConfigDALFactory, "findById">;
workflowIntegrationDAL: Pick<TWorkflowIntegrationDALFactory, "transaction" | "create" | "updateById" | "deleteById">;
};
@@ -46,7 +45,6 @@ export const slackServiceFactory = ({
permissionService,
slackIntegrationDAL,
kmsService,
adminSlackConfigDAL,
workflowIntegrationDAL
}: TSlackServiceFactoryDep) => {
const completeSlackIntegration = async ({
@@ -138,20 +136,21 @@ export const slackServiceFactory = ({
const getSlackInstaller = async () => {
const appCfg = getConfig();
const adminSlackCredentials = await getAdminSlackCredentials({
kmsService,
adminSlackConfigDAL
});
const serverCfg = await getServerCfg();
let slackClientId = "";
let slackClientSecret = "";
let slackClientId = appCfg.SLACK_CLIENT_ID as string;
let slackClientSecret = appCfg.SLACK_CLIENT_SECRET as string;
if (adminSlackCredentials.clientId && adminSlackCredentials.clientSecret) {
slackClientId = adminSlackCredentials.clientId;
slackClientSecret = adminSlackCredentials.clientSecret;
} else {
slackClientId = appCfg.SLACK_CLIENT_ID as string;
slackClientSecret = appCfg.SLACK_CLIENT_SECRET as string;
const decrypt = await kmsService.decryptWithRootKey();
if (serverCfg.encryptedSlackClientId) {
slackClientId = (await decrypt({ cipherTextBlob: Buffer.from(serverCfg.encryptedSlackClientId) })).toString();
}
if (serverCfg.encryptedSlackClientSecret) {
slackClientSecret = (
await decrypt({ cipherTextBlob: Buffer.from(serverCfg.encryptedSlackClientSecret) })
).toString();
}
if (!slackClientId || !slackClientSecret) {