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
refactor(keystore): enhance Redis cluster support and add new KMS key creation locks
Victor Santos0e92fa2b58b8
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.tsindex 5533ca4296..3cda7acda3 100644--- a/backend/src/keystore/keystore.ts+++ b/backend/src/keystore/keystore.ts@@ -1,4 +1,5 @@-import { Cluster, Redis } from "ioredis";+import type { Cluster } from "ioredis";+import { Redis } from "ioredis";import { Knex } from "knex";import { buildRedisFromConfig, TRedisConfigKeys } from "@app/lib/config/redis";@@ -28,7 +29,11 @@ export const PgSqlLock = {IdentityLogin: (identityId: string, nonce: string) => pgAdvisoryLockHashText(`identity-login:${identityId}:${nonce}`),PamResourceSshCaInit: (resourceId: string) => pgAdvisoryLockHashText(`pam-resource-ssh-ca-init:${resourceId}`),CreateIdentity: (orgId: string) => pgAdvisoryLockHashText(`create-identity:${orgId}`),- AccessSharedSecret: (sharedSecretId: string) => pgAdvisoryLockHashText(`access-shared-secret:${sharedSecretId}`)+ AccessSharedSecret: (sharedSecretId: string) => pgAdvisoryLockHashText(`access-shared-secret:${sharedSecretId}`),+ KmsOrgKeyCreation: (orgId: string) => pgAdvisoryLockHashText(`kms-org-key:${orgId}`),+ KmsOrgDataKeyCreation: (orgId: string) => pgAdvisoryLockHashText(`kms-org-data-key:${orgId}`),+ KmsProjectKeyCreation: (projectId: string) => pgAdvisoryLockHashText(`kms-project-key:${projectId}`),+ KmsProjectDataKeyCreation: (projectId: string) => pgAdvisoryLockHashText(`kms-project-data-key:${projectId}`)} as const;// all the key prefixes used must be set here to avoid conflict@@ -160,6 +165,7 @@ export type TKeyStoreFactory = {settings?: Partial<Settings>): Promise<{ release: () => Promise<ExecutionResult> }>;waitTillReady: ({ key, waitingCb, keyCheckCb, waitIteration, delay, jitter }: TWaitTillReady) => Promise<void>;+ isRedisClusterMode: () => boolean;};const pickPrimaryOrSecondaryRedis = (primary: Redis | Cluster, secondaries?: Array<Redis | Cluster>) => {@@ -177,6 +183,9 @@ export const keyStoreFactory = (keyValueStoreDAL: TKeyValueStoreDALFactory): TKeyStoreFactory => {const primaryRedis = buildRedisFromConfig(redisConfigKeys);+ // Detect if Redis is running in cluster mode - affects distributed locking behavior+ const isClusterMode = Boolean(redisConfigKeys.REDIS_CLUSTER_HOSTS && redisConfigKeys.REDIS_CLUSTER_HOSTS.length > 0);+const redisReadReplicas = redisConfigKeys.REDIS_READ_REPLICAS?.map((el) => {if (redisConfigKeys.REDIS_URL) {const primaryNode = new URL(redisConfigKeys?.REDIS_URL);@@ -335,6 +344,7 @@ export const keyStoreFactory = (listPush,listRange,listRemove,- listLength+ listLength,+ isRedisClusterMode: () => isClusterMode};};diff --git a/backend/src/keystore/memory.ts b/backend/src/keystore/memory.tsindex 913252f405..9025f55fb3 100644--- a/backend/src/keystore/memory.ts+++ b/backend/src/keystore/memory.ts@@ -112,6 +112,7 @@ export const inMemoryKeyStore = (): TKeyStoreFactory => {},listLength: async (key) => {return listStore[key]?.length ?? 0;- }+ },+ isRedisClusterMode: () => false};};diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.tsindex a63f0d41dd..61af04f0ed 100644--- a/backend/src/services/kms/kms-service.ts+++ b/backend/src/services/kms/kms-service.ts@@ -56,7 +56,7 @@ type TKmsServiceFactoryDep = {projectDAL: Pick<TProjectDALFactory, "findById" | "updateById" | "transaction">;orgDAL: Pick<TOrgDALFactory, "findById" | "updateById" | "transaction">;kmsRootConfigDAL: Pick<TKmsRootConfigDALFactory, "findById" | "create" | "updateById" | "transaction">;- keyStore: Pick<TKeyStoreFactory, "acquireLock" | "waitTillReady" | "setItemWithExpiry">;+ keyStore: Pick<TKeyStoreFactory, "acquireLock" | "waitTillReady" | "setItemWithExpiry" | "isRedisClusterMode">;internalKmsDAL: Pick<TInternalKmsDALFactory, "create">;hsmService: THsmServiceFactory;envConfig: Pick<TEnvConfig, "ENCRYPTION_KEY" | "ROOT_ENCRYPTION_KEY">;@@ -198,6 +198,30 @@ export const kmsServiceFactory = ({* In mean time the rest of the request will wait until creation is finished followed by getting the created on* In real time this would be milliseconds*/+ // Helper function to create org KMS key within a transaction+ const $createOrgKmsKey = async (orgId: string, tx: Knex) => {+ const org = await orgDAL.findById(orgId, tx);+ if (org.kmsDefaultKeyId) {+ return org.kmsDefaultKeyId;+ }++ const key = await generateKmsKey({+ isReserved: true,+ orgId: org.id,+ tx+ });++ await orgDAL.updateById(+ org.id,+ {+ kmsDefaultKeyId: key.id+ },+ tx+ );++ return key.id;+ };+const getOrgKmsKeyId = async (orgId: string, trx?: Knex) => {let org = await orgDAL.findById(orgId, trx);@@ -206,6 +230,17 @@ export const kmsServiceFactory = ({}if (!org.kmsDefaultKeyId) {+ // In Redis Cluster mode, use PostgreSQL advisory lock for reliable distributed locking+ if (keyStore.isRedisClusterMode()) {+ const keyId = await (trx || orgDAL).transaction(async (tx) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsOrgKeyCreation(orgId)]);+ return $createOrgKmsKey(orgId, tx);+ });++ return keyId;+ }++ // Standalone Redis mode - use Redis lockconst lock = await keyStore.acquireLock([KeyStorePrefixes.KmsOrgKeyCreation, orgId], 3000, { retryCount: 3 }).catch(() => null);@@ -221,28 +256,9 @@ export const kmsServiceFactory = ({org = await orgDAL.findById(orgId, trx);} else {const keyId = await (trx || orgDAL).transaction(async (tx) => {- org = await orgDAL.findById(orgId, tx);- if (org.kmsDefaultKeyId) {- return org.kmsDefaultKeyId;- }-- const key = await generateKmsKey({- isReserved: true,- orgId: org.id,- tx- });-- await orgDAL.updateById(- org.id,- {- kmsDefaultKeyId: key.id- },- tx- );-+ const createdKeyId = await $createOrgKmsKey(orgId, tx);await keyStore.setItemWithExpiry(`${KeyStorePrefixes.WaitUntilReadyKmsOrgKeyCreation}${orgId}`, 10, "true");-- return key.id;+ return createdKeyId;});return keyId;@@ -583,6 +599,22 @@ export const kmsServiceFactory = ({};};+ // Helper function to create org data key within a transaction+ const $createOrgKmsDataKey = async (orgId: string, kmsKeyId: string, tx: Knex) => {+ const org = await orgDAL.findById(orgId, tx);+ if (org.kmsEncryptedDataKey) {+ return;+ }++ const dataKey = crypto.randomBytes(32);+ const kmsEncryptor = await encryptWithKmsKey({ kmsId: kmsKeyId }, tx);+ const { cipherTextBlob } = await kmsEncryptor({ plainText: dataKey });++ await orgDAL.updateById(org.id, { kmsEncryptedDataKey: cipherTextBlob }, tx);++ return dataKey;+ };+const $getOrgKmsDataKey = async (orgId: string, trx?: Knex) => {const kmsKeyId = await getOrgKmsKeyId(orgId, trx);let org = await orgDAL.findById(orgId, trx);@@ -592,61 +624,49 @@ export const kmsServiceFactory = ({}if (!org.kmsEncryptedDataKey) {- const lock = await keyStore- .acquireLock([KeyStorePrefixes.KmsOrgDataKeyCreation, orgId], 500, { retryCount: 0 })- .catch(() => null);-- try {- if (!lock) {- await keyStore.waitTillReady({- key: `${KeyStorePrefixes.WaitUntilReadyKmsOrgDataKeyCreation}${orgId}`,- keyCheckCb: (val) => val === "true",- waitingCb: () => logger.info("KMS. Waiting for org data key to be created")- });-- org = await orgDAL.findById(orgId, trx);- } else {- const orgDataKey = await (trx || orgDAL).transaction(async (tx) => {- org = await orgDAL.findById(orgId, tx);- if (org.kmsEncryptedDataKey) {- return;- }+ // In Redis Cluster mode, use PostgreSQL advisory lock for reliable distributed locking+ if (keyStore.isRedisClusterMode()) {+ const orgDataKey = await (trx || orgDAL).transaction(async (tx) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsOrgDataKeyCreation(orgId)]);+ return $createOrgKmsDataKey(orgId, kmsKeyId, tx);+ });- const dataKey = crypto.randomBytes(32);- const kmsEncryptor = await encryptWithKmsKey(- {- kmsId: kmsKeyId- },- tx- );+ if (orgDataKey) {+ return orgDataKey;+ }+ } else {+ // Standalone Redis mode - use Redis lock+ const lock = await keyStore+ .acquireLock([KeyStorePrefixes.KmsOrgDataKeyCreation, orgId], 500, { retryCount: 0 })+ .catch(() => null);- const { cipherTextBlob } = await kmsEncryptor({- plainText: dataKey+ try {+ if (!lock) {+ await keyStore.waitTillReady({+ key: `${KeyStorePrefixes.WaitUntilReadyKmsOrgDataKeyCreation}${orgId}`,+ keyCheckCb: (val) => val === "true",+ waitingCb: () => logger.info("KMS. Waiting for org data key to be created")});- await orgDAL.updateById(- org.id,- {- kmsEncryptedDataKey: cipherTextBlob- },- tx- );-- await keyStore.setItemWithExpiry(- `${KeyStorePrefixes.WaitUntilReadyKmsOrgDataKeyCreation}${orgId}`,- 10,- "true"- );-- return dataKey;- });+ org = await orgDAL.findById(orgId, trx);+ } else {+ const orgDataKey = await (trx || orgDAL).transaction(async (tx) => {+ const createdDataKey = await $createOrgKmsDataKey(orgId, kmsKeyId, tx);+ await keyStore.setItemWithExpiry(+ `${KeyStorePrefixes.WaitUntilReadyKmsOrgDataKeyCreation}${orgId}`,+ 10,+ "true"+ );+ return createdDataKey;+ });- if (orgDataKey) {- return orgDataKey;+ if (orgDataKey) {+ return orgDataKey;+ }}+ } finally {+ await lock?.release();}- } finally {- await lock?.release();}}@@ -664,6 +684,24 @@ export const kmsServiceFactory = ({});};+ // Helper function to create project KMS key within a transaction+ const $createProjectKmsKey = async (projectId: string, tx: Knex) => {+ const project = await projectDAL.findById(projectId, tx);+ if (project.kmsSecretManagerKeyId) {+ return project.kmsSecretManagerKeyId;+ }++ const key = await generateKmsKey({+ isReserved: true,+ orgId: project.orgId,+ tx+ });++ await projectDAL.updateById(projectId, { kmsSecretManagerKeyId: key.id }, tx);++ return key.id;+ };+const getProjectSecretManagerKmsKeyId = async (projectId: string, trx?: Knex) => {let project = await projectDAL.findById(projectId, trx);if (!project) {@@ -671,6 +709,17 @@ export const kmsServiceFactory = ({}if (!project.kmsSecretManagerKeyId) {+ // In Redis Cluster mode, use PostgreSQL advisory lock for reliable distributed locking+ if (keyStore.isRedisClusterMode()) {+ const kmsKeyId = await (trx || projectDAL).transaction(async (tx) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsProjectKeyCreation(projectId)]);+ return $createProjectKmsKey(projectId, tx);+ });++ return kmsKeyId;+ }++ // Standalone Redis mode - use Redis lockconst lock = await keyStore.acquireLock([KeyStorePrefixes.KmsProjectKeyCreation, projectId], 3000, { retryCount: 0 }).catch(() => null);@@ -687,34 +736,15 @@ export const kmsServiceFactory = ({project = await projectDAL.findById(projectId);} else {const kmsKeyId = await (trx || projectDAL).transaction(async (tx) => {- project = await projectDAL.findById(projectId, tx);- if (project.kmsSecretManagerKeyId) {- return project.kmsSecretManagerKeyId;- }-- const key = await generateKmsKey({- isReserved: true,- orgId: project.orgId,- tx- });-- await projectDAL.updateById(- projectId,- {- kmsSecretManagerKeyId: key.id- },- tx+ const createdKeyId = await $createProjectKmsKey(projectId, tx);+ await keyStore.setItemWithExpiry(+ `${KeyStorePrefixes.WaitUntilReadyKmsProjectKeyCreation}${projectId}`,+ 10,+ "true");-- return key.id;+ return createdKeyId;});- await keyStore.setItemWithExpiry(- `${KeyStorePrefixes.WaitUntilReadyKmsProjectKeyCreation}${projectId}`,- 10,- "true"- );-return kmsKeyId;}} finally {@@ -729,76 +759,81 @@ export const kmsServiceFactory = ({return project.kmsSecretManagerKeyId;};+ // Helper function to create project data key within a transaction+ const $createProjectKmsDataKey = async (projectId: string, kmsKeyId: string, tx: Knex) => {+ const project = await projectDAL.findById(projectId, tx);+ if (project.kmsSecretManagerEncryptedDataKey) {+ return;+ }++ const dataKey = crypto.randomBytes(32);+ const kmsEncryptor = await encryptWithKmsKey({ kmsId: kmsKeyId }, tx);+ const { cipherTextBlob } = await kmsEncryptor({ plainText: dataKey });++ await projectDAL.updateById(projectId, { kmsSecretManagerEncryptedDataKey: cipherTextBlob }, tx);++ return dataKey;+ };+const $getProjectSecretManagerKmsDataKey = async (projectId: string, trx?: Knex) => {const kmsKeyId = await getProjectSecretManagerKmsKeyId(projectId, trx);let project = await projectDAL.findById(projectId, trx);if (!project.kmsSecretManagerEncryptedDataKey) {- const lock = await keyStore- .acquireLock([KeyStorePrefixes.KmsProjectDataKeyCreation, projectId], 3000, { retryCount: 0 })- .catch((err) => {- logger.error(err, "KMS. Failed to acquire lock.");- return null;+ // In Redis Cluster mode, use PostgreSQL advisory lock for reliable distributed locking+ if (keyStore.isRedisClusterMode()) {+ const projectDataKey = await (trx || projectDAL).transaction(async (tx) => {+ await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsProjectDataKeyCreation(projectId)]);+ return $createProjectKmsDataKey(projectId, kmsKeyId, tx);});- try {- if (!lock) {- await keyStore.waitTillReady({- key: `${KeyStorePrefixes.WaitUntilReadyKmsProjectDataKeyCreation}${projectId}`,- keyCheckCb: (val) => val === "true",- waitingCb: () => logger.info("KMS. Waiting for secret manager data key to be created"),- delay: 500+ if (projectDataKey) {+ return projectDataKey;+ }+ } else {+ // Standalone Redis mode - use Redis lock+ const lock = await keyStore+ .acquireLock([KeyStorePrefixes.KmsProjectDataKeyCreation, projectId], 3000, { retryCount: 0 })+ .catch((err) => {+ logger.error(err, "KMS. Failed to acquire lock.");+ return null;});- project = await projectDAL.findById(projectId, trx);- } else {- logger.info(`KMS. Generating KMS key for project ${projectId}`);- const projectDataKey = await (trx || projectDAL).transaction(async (tx) => {- project = await projectDAL.findById(projectId, tx);- if (project.kmsSecretManagerEncryptedDataKey) {- return project.kmsSecretManagerEncryptedDataKey;- }-- const dataKey = crypto.randomBytes(32);- const kmsEncryptor = await encryptWithKmsKey(- {- kmsId: kmsKeyId- },- tx- );-- const { cipherTextBlob } = await kmsEncryptor({- plainText: dataKey+ try {+ if (!lock) {+ await keyStore.waitTillReady({+ key: `${KeyStorePrefixes.WaitUntilReadyKmsProjectDataKeyCreation}${projectId}`,+ keyCheckCb: (val) => val === "true",+ waitingCb: () => logger.info("KMS. Waiting for secret manager data key to be created"),+ delay: 500});- await projectDAL.updateById(- projectId,- {- kmsSecretManagerEncryptedDataKey: cipherTextBlob- },- tx- );-- await keyStore.setItemWithExpiry(- `${KeyStorePrefixes.WaitUntilReadyKmsProjectDataKeyCreation}${projectId}`,- 10,- "true"- );- return dataKey;- });+ project = await projectDAL.findById(projectId, trx);+ } else {+ logger.info(`KMS. Generating KMS data key for project ${projectId}`);+ const projectDataKey = await (trx || projectDAL).transaction(async (tx) => {+ const createdDataKey = await $createProjectKmsDataKey(projectId, kmsKeyId, tx);+ await keyStore.setItemWithExpiry(+ `${KeyStorePrefixes.WaitUntilReadyKmsProjectDataKeyCreation}${projectId}`,+ 10,+ "true"+ );+ return createdDataKey;+ });- if (projectDataKey) {- return projectDataKey;+ if (projectDataKey) {+ return projectDataKey;+ }}+ } catch (error) {+ logger.error(+ error,+ `getProjectSecretManagerKmsDataKey: Failed to get project data key for [projectId=${projectId}]`+ );+ throw error;+ } finally {+ await lock?.release();}- } catch (error) {- logger.error(- error,- `getProjectSecretManagerKmsDataKey: Failed to get project data key for [projectId=${projectId}]`- );- throw error;- } finally {- await lock?.release();}}