Skip to content

Commit

Permalink
Merge pull request #1373 from akhilmhdh/feat/patch-server-cfg-init
Browse files Browse the repository at this point in the history
feat: fixed server cfg stale in replication
  • Loading branch information
maidul98 authored Feb 7, 2024
2 parents 609b224 + 61ff732 commit 55e75bb
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 38 deletions.
20 changes: 8 additions & 12 deletions backend/src/db/instance.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import knex from "knex";

export type TDbClient = ReturnType<typeof initDbConnection>;
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;
};
};
2 changes: 1 addition & 1 deletion backend/src/ee/routes/v1/saml-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -40,7 +40,7 @@ const run = async () => {
port: appCfg.PORT,
host: appCfg.HOST,
listenTextResolver: (address) => {
bootstrap();
void bootstrap();
return address;
}
});
Expand Down
4 changes: 2 additions & 2 deletions backend/src/server/boot-strap-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions backend/src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/server/routes/v1/admin-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
})
}
},
handler: () => {
const config = getServerCfg();
handler: async () => {
const config = await getServerCfg();
return { config };
}
});
Expand Down Expand Up @@ -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({
Expand Down
6 changes: 3 additions & 3 deletions backend/src/server/routes/v1/sso-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 10 additions & 13 deletions backend/src/services/super-admin/super-admin-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ type TSuperAdminServiceFactoryDep = {

export type TSuperAdminServiceFactory = ReturnType<typeof superAdminServiceFactory>;

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<TSuperAdmin>;

export const superAdminServiceFactory = ({
serverCfgDAL,
Expand All @@ -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;
};

Expand Down

0 comments on commit 55e75bb

Please sign in to comment.