From 61ff732ec0629579354d794f6cbb7b18225c68e4 Mon Sep 17 00:00:00 2001 From: Akhil Mohan Date: Wed, 7 Feb 2024 11:36:13 +0530 Subject: [PATCH] feat: fixed server cfg stale in replication --- backend/src/db/instance.ts | 20 +++++++--------- backend/src/ee/routes/v1/saml-router.ts | 2 +- backend/src/main.ts | 4 ++-- backend/src/server/boot-strap-check.ts | 4 ++-- backend/src/server/routes/index.ts | 4 ++-- backend/src/server/routes/v1/admin-router.ts | 6 ++--- backend/src/server/routes/v1/sso-router.ts | 6 ++--- .../super-admin/super-admin-service.ts | 23 ++++++++----------- 8 files changed, 31 insertions(+), 38 deletions(-) diff --git a/backend/src/db/instance.ts b/backend/src/db/instance.ts index c379c24a51..2a321a3bc3 100644 --- a/backend/src/db/instance.ts +++ b/backend/src/db/instance.ts @@ -1,23 +1,19 @@ import knex from "knex"; export type TDbClient = ReturnType; -export const initDbConnection = ({ - dbConnectionUri, - dbRootCert -}: { - dbConnectionUri: string; - dbRootCert?: string; -}) => { +export const initDbConnection = ({ dbConnectionUri, dbRootCert }: { dbConnectionUri: string; dbRootCert?: string }) => { const db = knex({ client: "pg", connection: { connectionString: dbConnectionUri, - ssl: dbRootCert ? { - rejectUnauthorized: true, - ca: Buffer.from(dbRootCert, 'base64').toString('ascii') - } : false + ssl: dbRootCert + ? { + rejectUnauthorized: true, + ca: Buffer.from(dbRootCert, "base64").toString("ascii") + } + : false } }); return db; -}; \ No newline at end of file +}; diff --git a/backend/src/ee/routes/v1/saml-router.ts b/backend/src/ee/routes/v1/saml-router.ts index 6ddebb388c..edea312fd9 100644 --- a/backend/src/ee/routes/v1/saml-router.ts +++ b/backend/src/ee/routes/v1/saml-router.ts @@ -79,7 +79,7 @@ export const registerSamlRouter = async (server: FastifyZodProvider) => { // eslint-disable-next-line async (req, profile, cb) => { try { - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); if (!profile) throw new BadRequestError({ message: "Missing profile" }); const { firstName } = profile; const email = profile?.email ?? (profile?.emailAddress as string); // emailRippling is added because in Rippling the field `email` reserved diff --git a/backend/src/main.ts b/backend/src/main.ts index f85eed3ed5..fab576d3bd 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -16,7 +16,7 @@ const run = async () => { dbConnectionUri: appCfg.DB_CONNECTION_URI, dbRootCert: appCfg.DB_ROOT_CERT }); - + const smtp = smtpServiceFactory(formatSmtpConfig()); const queue = queueServiceFactory(appCfg.REDIS_URL); @@ -40,7 +40,7 @@ const run = async () => { port: appCfg.PORT, host: appCfg.HOST, listenTextResolver: (address) => { - bootstrap(); + void bootstrap(); return address; } }); diff --git a/backend/src/server/boot-strap-check.ts b/backend/src/server/boot-strap-check.ts index d036db8e3b..381e575efa 100644 --- a/backend/src/server/boot-strap-check.ts +++ b/backend/src/server/boot-strap-check.ts @@ -12,9 +12,9 @@ type BootstrapOpt = { db: Knex; }; -const bootstrapCb = () => { +const bootstrapCb = async () => { const appCfg = getConfig(); - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); if (!serverCfg.initialized) { console.info(`Welcome to Infisical diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts index fe79a6f114..28f65ea998 100644 --- a/backend/src/server/routes/index.ts +++ b/backend/src/server/routes/index.ts @@ -513,9 +513,9 @@ export const registerRoutes = async ( }) } }, - handler: () => { + handler: async () => { const cfg = getConfig(); - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); return { date: new Date(), message: "Ok" as const, diff --git a/backend/src/server/routes/v1/admin-router.ts b/backend/src/server/routes/v1/admin-router.ts index b8b61216bf..e23f68c3ae 100644 --- a/backend/src/server/routes/v1/admin-router.ts +++ b/backend/src/server/routes/v1/admin-router.ts @@ -20,8 +20,8 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => { }) } }, - handler: () => { - const config = getServerCfg(); + handler: async () => { + const config = await getServerCfg(); return { config }; } }); @@ -78,7 +78,7 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => { }, handler: async (req, res) => { const appCfg = getConfig(); - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); if (serverCfg.initialized) throw new UnauthorizedError({ name: "Admin sign up", message: "Admin has been created" }); const { user, token } = await server.services.superAdmin.adminSignUp({ diff --git a/backend/src/server/routes/v1/sso-router.ts b/backend/src/server/routes/v1/sso-router.ts index 9c7c85b04d..bfcf2f6ae5 100644 --- a/backend/src/server/routes/v1/sso-router.ts +++ b/backend/src/server/routes/v1/sso-router.ts @@ -42,7 +42,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => { async (req, _accessToken, _refreshToken, profile, cb) => { try { const email = profile?.emails?.[0]?.value; - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); if (!email) throw new BadRequestError({ message: "Email not found", @@ -84,7 +84,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => { try { const ghEmails = await fetchGithubEmails(accessToken); const { email } = ghEmails.filter((gitHubEmail) => gitHubEmail.primary)[0]; - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({ email, firstName: profile.displayName, @@ -120,7 +120,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => { async (req: any, _accessToken: string, _refreshToken: string, profile: any, cb: any) => { try { const email = profile.emails[0].value; - const serverCfg = getServerCfg(); + const serverCfg = await getServerCfg(); const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({ email, firstName: profile.displayName, diff --git a/backend/src/services/super-admin/super-admin-service.ts b/backend/src/services/super-admin/super-admin-service.ts index ac531fa547..ed7e214298 100644 --- a/backend/src/services/super-admin/super-admin-service.ts +++ b/backend/src/services/super-admin/super-admin-service.ts @@ -17,11 +17,8 @@ type TSuperAdminServiceFactoryDep = { export type TSuperAdminServiceFactory = ReturnType; -let serverCfg: TSuperAdmin; -export const getServerCfg = () => { - if (!serverCfg) throw new BadRequestError({ name: "Get server cfg", message: "Server cfg not initialized" }); - return serverCfg; -}; +// eslint-disable-next-line +export let getServerCfg: () => Promise; export const superAdminServiceFactory = ({ serverCfgDAL, @@ -30,18 +27,18 @@ export const superAdminServiceFactory = ({ orgService }: TSuperAdminServiceFactoryDep) => { const initServerCfg = async () => { - serverCfg = await serverCfgDAL.findOne({}); - if (!serverCfg) { - const newCfg = await serverCfgDAL.create({ initialized: false, allowSignUp: true }); - serverCfg = newCfg; - return newCfg; - } - return serverCfg; + // TODO(akhilmhdh): bad pattern time less change this later to me itself + getServerCfg = () => serverCfgDAL.findOne({}); + + const serverCfg = await serverCfgDAL.findOne({}); + if (serverCfg) return; + const newCfg = await serverCfgDAL.create({ initialized: false, allowSignUp: true }); + return newCfg; }; const updateServerCfg = async (data: TSuperAdminUpdate) => { + const serverCfg = await getServerCfg(); const cfg = await serverCfgDAL.updateById(serverCfg.id, data); - serverCfg = cfg; return cfg; };