Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
add env for pool and set db name
Thiago Araujo da Silvaa65a769f4dc1
diff --git a/backend/src/db/instance.ts b/backend/src/db/instance.tsindex 5d9007a001..c2e3d01747 100644--- a/backend/src/db/instance.ts+++ b/backend/src/db/instance.ts@@ -24,11 +24,24 @@ const parseSslConfig = (dbConnectionUri: string, dbRootCert?: string) => {return { modifiedDbConnectionUri, sslConfig };};+export type TDbPoolConfig = { min: number; max: number };++export const DbApplicationName = {+ Primary: "infisical-core",+ Replica: "infisical-core-replica",+ AuditLog: "infisical-core-audit",+ Migrator: "infisical-core-migrator"+} as const;++const DEFAULT_POOL: TDbPoolConfig = { min: 0, max: 10 };+export type TDbClient = Knex;export const initDbConnection = ({dbConnectionUri,dbRootCert,- readReplicas = []+ readReplicas = [],+ pool = DEFAULT_POOL,+ replicaPool = DEFAULT_POOL}: {dbConnectionUri: string;dbRootCert?: string;@@ -36,6 +49,8 @@ export const initDbConnection = ({dbConnectionUri: string;dbRootCert?: string;}[];+ pool?: TDbPoolConfig;+ replicaPool?: TDbPoolConfig;}) => {// akhilmhdh: the default Knex is knex.Knex<any, any[]>. but when assigned with knex({<config>}) the value is knex.Knex<any, unknown[]>// this was causing issue with files like `snapshot-dal` `findRecursivelySnapshots` this i am explicitly putting the any and unknown[]@@ -67,10 +82,11 @@ export const initDbConnection = ({user: process.env.DB_USER,database: process.env.DB_NAME,password: process.env.DB_PASSWORD,- ssl: sslConfig+ ssl: sslConfig,+ application_name: DbApplicationName.Primary},// https://knexjs.org/guide/#pool- pool: { min: 0, max: 10 },+ pool,migrations: {tableName: "infisical_migrations"}@@ -87,12 +103,13 @@ export const initDbConnection = ({client: "pg",connection: {connectionString: replicaUri,- ssl: replicaSslConfig+ ssl: replicaSslConfig,+ application_name: DbApplicationName.Replica},migrations: {tableName: "infisical_migrations"},- pool: { min: 0, max: 10 }+ pool: replicaPool});});@@ -101,10 +118,12 @@ export const initDbConnection = ({export const initAuditLogDbConnection = ({dbConnectionUri,- dbRootCert+ dbRootCert,+ pool = DEFAULT_POOL}: {dbConnectionUri: string;dbRootCert?: string;+ pool?: TDbPoolConfig;}) => {const { modifiedDbConnectionUri, sslConfig } = parseSslConfig(dbConnectionUri, dbRootCert);@@ -120,12 +139,13 @@ export const initAuditLogDbConnection = ({user: process.env.AUDIT_LOGS_DB_USER,database: process.env.AUDIT_LOGS_DB_NAME,password: process.env.AUDIT_LOGS_DB_PASSWORD,- ssl: sslConfig+ ssl: sslConfig,+ application_name: DbApplicationName.AuditLog},migrations: {tableName: "infisical_migrations"},- pool: { min: 0, max: 10 }+ pool});// we add these overrides so that auditLogDb and the primary DB are interchangeablediff --git a/backend/src/db/knexfile.ts b/backend/src/db/knexfile.tsindex 82a8adea0f..80f0c9b846 100644--- a/backend/src/db/knexfile.ts+++ b/backend/src/db/knexfile.ts@@ -6,6 +6,8 @@ import type { Knex } from "knex";import path from "path";import { initLogger } from "@app/lib/logger";+import { DbApplicationName } from "./instance";+// Update with your config settings. .dotenv.config({path: path.join(__dirname, "../../../.env.migration")@@ -31,7 +33,8 @@ export default {rejectUnauthorized: true,ca: Buffer.from(process.env.DB_ROOT_CERT, "base64").toString("ascii")}- : false+ : false,+ application_name: DbApplicationName.Migrator},pool: {min: 2,@@ -59,7 +62,8 @@ export default {rejectUnauthorized: true,ca: Buffer.from(process.env.DB_ROOT_CERT, "base64").toString("ascii")}- : false+ : false,+ application_name: DbApplicationName.Migrator},pool: {min: 2,diff --git a/backend/src/ee/services/audit-log/audit-log-dal.ts b/backend/src/ee/services/audit-log/audit-log-dal.tsindex be8476a436..e3b24c9e17 100644--- a/backend/src/ee/services/audit-log/audit-log-dal.ts+++ b/backend/src/ee/services/audit-log/audit-log-dal.ts@@ -236,7 +236,7 @@ export const auditLogDALFactory = (db: TDbClient) => {try {// eslint-disable-next-line no-await-in-loopdeletedAuditLogIds = await db.transaction(async (trx) => {- await trx.raw(`SET statement_timeout = ${QUERY_TIMEOUT_MS}`);+ await trx.raw(`SET LOCAL statement_timeout = ${QUERY_TIMEOUT_MS}`);const findExpiredLogSubQuery = trx(TableName.AuditLog).where("expiresAt", "<", today)diff --git a/backend/src/keystore/key-value-store-dal.ts b/backend/src/keystore/key-value-store-dal.tsindex 79e42fe9b7..c3e71efc44 100644--- a/backend/src/keystore/key-value-store-dal.ts+++ b/backend/src/keystore/key-value-store-dal.ts@@ -54,7 +54,7 @@ export const keyValueStoreDALFactory = (db: TDbClient): TKeyValueStoreDALFactorytry {// eslint-disable-next-line no-await-in-loopdeletedIds = await db.transaction(async (trx) => {- await trx.raw(`SET statement_timeout = ${QUERY_TIMEOUT_MS}`);+ await trx.raw(`SET LOCAL statement_timeout = ${QUERY_TIMEOUT_MS}`);const findExpiredKeysSubQuery = trx(TableName.KeyValueStore).where("expiresAt", "<", db.fn.now())diff --git a/backend/src/lib/config/env.ts b/backend/src/lib/config/env.tsindex a4d6d1f70b..916de1c86f 100644--- a/backend/src/lib/config/env.ts+++ b/backend/src/lib/config/env.ts@@ -159,6 +159,24 @@ const envSchema = zDB_PASSWORD: zpStr(z.string().describe("Postgres database password").optional()),DB_NAME: zpStr(z.string().describe("Postgres database name").optional()),DB_READ_REPLICAS: zpStr(z.string().describe("Postgres read replicas").optional()),+ DB_POOL_MIN: z.coerce.number().min(0).default(0).describe("Minimum primary Postgres pool connections"),+ DB_POOL_MAX: z.coerce.number().min(1).default(10).describe("Maximum primary Postgres pool connections"),+ DB_REPLICA_POOL_MIN: z.coerce+ .number()+ .min(0)+ .default(0)+ .describe("Minimum pool connections per Postgres read replica"),+ DB_REPLICA_POOL_MAX: z.coerce+ .number()+ .min(1)+ .default(10)+ .describe("Maximum pool connections per Postgres read replica"),+ AUDIT_LOGS_DB_POOL_MIN: z.coerce.number().min(0).default(0).describe("Minimum audit log Postgres pool connections"),+ AUDIT_LOGS_DB_POOL_MAX: z.coerce+ .number()+ .min(1)+ .default(10)+ .describe("Maximum audit log Postgres pool connections"),BCRYPT_SALT_ROUND: z.number().optional(), // note(daniel): this is deprecated, use SALT_ROUNDS instead. only keeping this for backwards compatibility.NODE_ENV: z.enum(["development", "test", "production"]).default("production"),SALT_ROUNDS: z.coerce.number().default(10),@@ -702,7 +720,9 @@ export const getDatabaseCredentials = (logger?: CustomLogger) => {readReplicas: parsedEnv.data.DB_READ_REPLICAS?.map((el) => ({dbRootCert: el.DB_ROOT_CERT,dbConnectionUri: el.DB_CONNECTION_URI- }))+ })),+ pool: { min: parsedEnv.data.DB_POOL_MIN, max: parsedEnv.data.DB_POOL_MAX },+ replicaPool: { min: parsedEnv.data.DB_REPLICA_POOL_MIN, max: parsedEnv.data.DB_REPLICA_POOL_MAX }};};diff --git a/backend/src/main.ts b/backend/src/main.tsindex 7c3f1b06e7..859c94cad0 100644--- a/backend/src/main.ts+++ b/backend/src/main.ts@@ -55,7 +55,9 @@ const run = async () => {const db = initDbConnection({dbConnectionUri: databaseCredentials.dbConnectionUri,dbRootCert: databaseCredentials.dbRootCert,- readReplicas: databaseCredentials.readReplicas+ readReplicas: databaseCredentials.readReplicas,+ pool: databaseCredentials.pool,+ replicaPool: databaseCredentials.replicaPool});// Register connection-pool and entity-count observable gauges. No-ops when telemetry is disabled.@@ -72,7 +74,8 @@ const run = async () => {const auditLogDb = envConfig.AUDIT_LOGS_DB_CONNECTION_URI? initAuditLogDbConnection({dbConnectionUri: envConfig.AUDIT_LOGS_DB_CONNECTION_URI,- dbRootCert: envConfig.AUDIT_LOGS_DB_ROOT_CERT+ dbRootCert: envConfig.AUDIT_LOGS_DB_ROOT_CERT,+ pool: { min: envConfig.AUDIT_LOGS_DB_POOL_MIN, max: envConfig.AUDIT_LOGS_DB_POOL_MAX }}): undefined;diff --git a/backend/src/services/notification/user-notification-dal.ts b/backend/src/services/notification/user-notification-dal.tsindex 4a5b2f2419..334835de30 100644--- a/backend/src/services/notification/user-notification-dal.ts+++ b/backend/src/services/notification/user-notification-dal.ts@@ -74,7 +74,7 @@ export const userNotificationDALFactory = (db: TDbClient) => {try {// eslint-disable-next-line no-await-in-loopdeletedNotificationIds = await db.transaction(async (trx) => {- await trx.raw(`SET statement_timeout = ${QUERY_TIMEOUT_MS}`);+ await trx.raw(`SET LOCAL statement_timeout = ${QUERY_TIMEOUT_MS}`);const findExpiredNotificationSubQuery = trx(TableName.UserNotifications).where("createdAt", "<", threeMonthsAgo)