Merge pull request #1373 from akhilmhdh/feat/patch-server-cfg-init

feat: fixed server cfg stale in replication
This commit is contained in:
Maidul Islam
2024-02-07 01:09:42 -05:00
committed by GitHub
8 changed files with 31 additions and 38 deletions
+6 -10
View File
@@ -1,21 +1,17 @@
import knex from "knex"; import knex from "knex";
export type TDbClient = ReturnType<typeof initDbConnection>; export type TDbClient = ReturnType<typeof initDbConnection>;
export const initDbConnection = ({ export const initDbConnection = ({ dbConnectionUri, dbRootCert }: { dbConnectionUri: string; dbRootCert?: string }) => {
dbConnectionUri,
dbRootCert
}: {
dbConnectionUri: string;
dbRootCert?: string;
}) => {
const db = knex({ const db = knex({
client: "pg", client: "pg",
connection: { connection: {
connectionString: dbConnectionUri, connectionString: dbConnectionUri,
ssl: dbRootCert ? { ssl: dbRootCert
? {
rejectUnauthorized: true, rejectUnauthorized: true,
ca: Buffer.from(dbRootCert, 'base64').toString('ascii') ca: Buffer.from(dbRootCert, "base64").toString("ascii")
} : false }
: false
} }
}); });
+1 -1
View File
@@ -79,7 +79,7 @@ export const registerSamlRouter = async (server: FastifyZodProvider) => {
// eslint-disable-next-line // eslint-disable-next-line
async (req, profile, cb) => { async (req, profile, cb) => {
try { try {
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
if (!profile) throw new BadRequestError({ message: "Missing profile" }); if (!profile) throw new BadRequestError({ message: "Missing profile" });
const { firstName } = profile; const { firstName } = profile;
const email = profile?.email ?? (profile?.emailAddress as string); // emailRippling is added because in Rippling the field `email` reserved const email = profile?.email ?? (profile?.emailAddress as string); // emailRippling is added because in Rippling the field `email` reserved
+1 -1
View File
@@ -40,7 +40,7 @@ const run = async () => {
port: appCfg.PORT, port: appCfg.PORT,
host: appCfg.HOST, host: appCfg.HOST,
listenTextResolver: (address) => { listenTextResolver: (address) => {
bootstrap(); void bootstrap();
return address; return address;
} }
}); });
+2 -2
View File
@@ -12,9 +12,9 @@ type BootstrapOpt = {
db: Knex; db: Knex;
}; };
const bootstrapCb = () => { const bootstrapCb = async () => {
const appCfg = getConfig(); const appCfg = getConfig();
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
if (!serverCfg.initialized) { if (!serverCfg.initialized) {
console.info(`Welcome to Infisical console.info(`Welcome to Infisical
+2 -2
View File
@@ -513,9 +513,9 @@ export const registerRoutes = async (
}) })
} }
}, },
handler: () => { handler: async () => {
const cfg = getConfig(); const cfg = getConfig();
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
return { return {
date: new Date(), date: new Date(),
message: "Ok" as const, message: "Ok" as const,
+3 -3
View File
@@ -20,8 +20,8 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
}) })
} }
}, },
handler: () => { handler: async () => {
const config = getServerCfg(); const config = await getServerCfg();
return { config }; return { config };
} }
}); });
@@ -78,7 +78,7 @@ export const registerAdminRouter = async (server: FastifyZodProvider) => {
}, },
handler: async (req, res) => { handler: async (req, res) => {
const appCfg = getConfig(); const appCfg = getConfig();
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
if (serverCfg.initialized) if (serverCfg.initialized)
throw new UnauthorizedError({ name: "Admin sign up", message: "Admin has been created" }); throw new UnauthorizedError({ name: "Admin sign up", message: "Admin has been created" });
const { user, token } = await server.services.superAdmin.adminSignUp({ const { user, token } = await server.services.superAdmin.adminSignUp({
+3 -3
View File
@@ -42,7 +42,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
async (req, _accessToken, _refreshToken, profile, cb) => { async (req, _accessToken, _refreshToken, profile, cb) => {
try { try {
const email = profile?.emails?.[0]?.value; const email = profile?.emails?.[0]?.value;
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
if (!email) if (!email)
throw new BadRequestError({ throw new BadRequestError({
message: "Email not found", message: "Email not found",
@@ -84,7 +84,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
try { try {
const ghEmails = await fetchGithubEmails(accessToken); const ghEmails = await fetchGithubEmails(accessToken);
const { email } = ghEmails.filter((gitHubEmail) => gitHubEmail.primary)[0]; const { email } = ghEmails.filter((gitHubEmail) => gitHubEmail.primary)[0];
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({ const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({
email, email,
firstName: profile.displayName, firstName: profile.displayName,
@@ -120,7 +120,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
async (req: any, _accessToken: string, _refreshToken: string, profile: any, cb: any) => { async (req: any, _accessToken: string, _refreshToken: string, profile: any, cb: any) => {
try { try {
const email = profile.emails[0].value; const email = profile.emails[0].value;
const serverCfg = getServerCfg(); const serverCfg = await getServerCfg();
const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({ const { isUserCompleted, providerAuthToken } = await server.services.login.oauth2Login({
email, email,
firstName: profile.displayName, firstName: profile.displayName,
@@ -17,11 +17,8 @@ type TSuperAdminServiceFactoryDep = {
export type TSuperAdminServiceFactory = ReturnType<typeof superAdminServiceFactory>; export type TSuperAdminServiceFactory = ReturnType<typeof superAdminServiceFactory>;
let serverCfg: TSuperAdmin; // eslint-disable-next-line
export const getServerCfg = () => { export let getServerCfg: () => Promise<TSuperAdmin>;
if (!serverCfg) throw new BadRequestError({ name: "Get server cfg", message: "Server cfg not initialized" });
return serverCfg;
};
export const superAdminServiceFactory = ({ export const superAdminServiceFactory = ({
serverCfgDAL, serverCfgDAL,
@@ -30,18 +27,18 @@ export const superAdminServiceFactory = ({
orgService orgService
}: TSuperAdminServiceFactoryDep) => { }: TSuperAdminServiceFactoryDep) => {
const initServerCfg = async () => { const initServerCfg = async () => {
serverCfg = await serverCfgDAL.findOne({}); // TODO(akhilmhdh): bad pattern time less change this later to me itself
if (!serverCfg) { getServerCfg = () => serverCfgDAL.findOne({});
const serverCfg = await serverCfgDAL.findOne({});
if (serverCfg) return;
const newCfg = await serverCfgDAL.create({ initialized: false, allowSignUp: true }); const newCfg = await serverCfgDAL.create({ initialized: false, allowSignUp: true });
serverCfg = newCfg;
return newCfg; return newCfg;
}
return serverCfg;
}; };
const updateServerCfg = async (data: TSuperAdminUpdate) => { const updateServerCfg = async (data: TSuperAdminUpdate) => {
const serverCfg = await getServerCfg();
const cfg = await serverCfgDAL.updateById(serverCfg.id, data); const cfg = await serverCfgDAL.updateById(serverCfg.id, data);
serverCfg = cfg;
return cfg; return cfg;
}; };