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(encryption): streamline key rotation logic and enhance error handling
Victor Hugo dos Santosd383d06e8277
diff --git a/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.ts b/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.tsindex b2417e06a0..48957c169f 100644--- a/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.ts+++ b/backend/src/services/encryption-key-rotation/encryption-key-rotation-service.ts@@ -120,12 +120,6 @@ export const encryptionKeyRotationServiceFactory = ({});}- const isFipsEnabled = crypto.isFipsModeEnabled();- const key = generateRootEncryptionKey(isFipsEnabled);- const kekBuffer = resolveKekBuffer(key, isFipsEnabled);- const encryptedRootKey = kmsService.encryptRootKeyForKek(kekBuffer);- const fingerprint = getKekFingerprint(kekBuffer);-const staged = await kmsRootConfigDAL.transaction(async (tx) => {await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsRootKeyInit]);@@ -150,9 +144,14 @@ export const encryptionKeyRotationServiceFactory = ({});}+ const isFipsEnabled = crypto.isFipsModeEnabled();+ const key = generateRootEncryptionKey(isFipsEnabled);+ const kekBuffer = resolveKekBuffer(key, isFipsEnabled);+ const fingerprint = getKekFingerprint(kekBuffer);+const row = await kmsRootConfigDAL.create({- encryptedRootKey,+ encryptedRootKey: kmsService.encryptRootKeyForKek(kekBuffer),encryptionStrategy: RootKeyEncryptionStrategy.Software,kekFingerprint: fingerprint,activatedAt: null@@ -160,16 +159,16 @@ export const encryptionKeyRotationServiceFactory = ({tx);- return { row, retained };+ return { row, retained, key, fingerprint };});const pending = staged.row;- logger.info(`Encryption key rotation staged [rotationId=${pending.id}] [fingerprint=${fingerprint}]`);+ logger.info(`Encryption key rotation staged [rotationId=${pending.id}] [fingerprint=${staged.fingerprint}]`);return {id: pending.id,- fingerprint,- key,+ fingerprint: staged.fingerprint,+ key: staged.key,...(staged.retained? {supersedesRetainedKey: {diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.tsindex bbb29033a0..ba4577f37c 100644--- a/backend/src/services/kms/kms-service.ts+++ b/backend/src/services/kms/kms-service.ts@@ -1825,6 +1825,15 @@ export const kmsServiceFactory = ({}}+ if (kmsRootConfig.encryptionStrategy === RootKeyEncryptionStrategy.Software) {+ const currentFingerprint = $currentKekFingerprint();+ if (kmsRootConfig.kekFingerprint && currentFingerprint && kmsRootConfig.kekFingerprint !== currentFingerprint) {+ throw new BadRequestError({+ message: `This instance is running with encryption key fingerprint '${currentFingerprint}', but the active root key was written with '${kmsRootConfig.kekFingerprint}'. Restart it with the current encryption key before changing the root key encryption strategy.`+ });+ }+ }+// Both can be HSM round trips, so they stay outside the transaction.const decryptedRootKey = await $decryptRootKey(kmsRootConfig);const encryptedRootKey = await $encryptRootKey(decryptedRootKey, strategy);@@ -1837,9 +1846,10 @@ export const kmsServiceFactory = ({await kmsRootConfigDAL.transaction(async (tx) => {await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.KmsRootKeyInit]);- const [pendingNow, retainedNow] = await Promise.all([+ const [pendingNow, retainedNow, sentinelNow] = await Promise.all([kmsRootConfigDAL.findPending(tx),- kmsRootConfigDAL.findRetained(tx)+ kmsRootConfigDAL.findRetained(tx),+ kmsRootConfigDAL.findById(KMS_ROOT_CONFIG_UUID, tx)]);if (pendingNow.length || retainedNow.length) {throw new BadRequestError({@@ -1848,6 +1858,13 @@ export const kmsServiceFactory = ({});}+ if (!sentinelNow || !sentinelNow.encryptedRootKey.equals(kmsRootConfig.encryptedRootKey)) {+ throw new BadRequestError({+ message:+ "The active root key changed while this request was in flight, so the encryption strategy was left unchanged. Retry the change."+ });+ }+await kmsRootConfigDAL.updateById(KMS_ROOT_CONFIG_UUID,{