Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(kms): enhance error handling and key store integration

Victor Hugo dos Santose703a898a819
diff --git a/backend/scripts/seed-gamma-fixtures.ts b/backend/scripts/seed-gamma-fixtures.ts
index 9103cd51d1..826a7724b2 100644
--- a/backend/scripts/seed-gamma-fixtures.ts
+++ b/backend/scripts/seed-gamma-fixtures.ts
@@ -33,6 +33,7 @@ import promptSync from "prompt-sync";
import { initializeHsmModule } from "@app/ee/services/hsm/hsm-fns";
import { hsmServiceFactory } from "@app/ee/services/hsm/hsm-service";
import { SamlProviders } from "@app/ee/services/saml-config/saml-config-types";
+import { inMemoryKeyStore } from "@app/keystore/memory";
import { getConfig, getHsmConfig, initEnvConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
import { initLogger, logger } from "@app/lib/logger";
@@ -157,6 +158,7 @@ const main = async () => {
orgDAL,
projectDAL,
hsmService,
+ keyStore: inMemoryKeyStore(),
envConfig
});
// Loads the decrypted root key into the service's in-memory buffer; the
diff --git a/backend/src/db/migrations/utils/services.ts b/backend/src/db/migrations/utils/services.ts
index 4ec1c437dc..7eaf1c908d 100644
--- a/backend/src/db/migrations/utils/services.ts
+++ b/backend/src/db/migrations/utils/services.ts
@@ -129,6 +129,7 @@ export const getMigrationEncryptionServices = async ({
orgDAL,
projectDAL,
hsmService,
+ keyStore,
envConfig
});
diff --git a/backend/src/ee/services/kmip/kmip-operation-service.ts b/backend/src/ee/services/kmip/kmip-operation-service.ts
index 4b4042f6d9..7edde0292b 100644
--- a/backend/src/ee/services/kmip/kmip-operation-service.ts
+++ b/backend/src/ee/services/kmip/kmip-operation-service.ts
@@ -133,6 +133,9 @@ export const kmipOperationServiceFactory = ({
}
const completeKeyDetails = await kmsDAL.findByIdWithAssociatedKms(id);
+ if (!completeKeyDetails) {
+ throw new NotFoundError({ message: `Key with ID '${id}' not found` });
+ }
if (!completeKeyDetails.internalKms) {
throw new BadRequestError({
message: "Cannot destroy external keys"
@@ -187,6 +190,9 @@ export const kmipOperationServiceFactory = ({
}
const completeKeyDetails = await kmsDAL.findByIdWithAssociatedKms(id);
+ if (!completeKeyDetails) {
+ throw new NotFoundError({ message: `Key with ID '${id}' not found` });
+ }
if (!completeKeyDetails.internalKms) {
throw new BadRequestError({
@@ -284,6 +290,9 @@ export const kmipOperationServiceFactory = ({
}
const completeKeyDetails = await kmsDAL.findByIdWithAssociatedKms(id);
+ if (!completeKeyDetails) {
+ throw new NotFoundError({ message: `Key with ID '${id}' not found` });
+ }
if (!completeKeyDetails.internalKms) {
throw new BadRequestError({
@@ -346,6 +355,9 @@ export const kmipOperationServiceFactory = ({
}
const completeKeyDetails = await kmsDAL.findByIdWithAssociatedKms(id);
+ if (!completeKeyDetails) {
+ throw new NotFoundError({ message: `Key with ID '${id}' not found` });
+ }
if (!completeKeyDetails.internalKms) {
throw new BadRequestError({
diff --git a/backend/src/services/kms/kms-key-dal.ts b/backend/src/services/kms/kms-key-dal.ts
index 132c62bfbb..60d1b1e60d 100644
--- a/backend/src/services/kms/kms-key-dal.ts
+++ b/backend/src/services/kms/kms-key-dal.ts
@@ -62,6 +62,8 @@ export const kmskeyDALFactory = (db: TDbClient) => {
db.ref("kmsEncryptedDataKey").withSchema(TableName.Organization).as("orgKmsEncryptedDataKey")
);
+ if (!result) return undefined;
+
const data = {
...KmsKeysSchema.parse(result),
isExternal: Boolean(result?.externalKmsId),
diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.ts
index 61993c43d3..121cd23625 100644
--- a/backend/src/services/kms/kms-service.ts
+++ b/backend/src/services/kms/kms-service.ts
@@ -22,6 +22,7 @@ import { crypto } from "@app/lib/crypto/cryptography";
import { HmacAlgorithm, hmacService } from "@app/lib/crypto/hmac";
import { detectPqcVariantFromDer } from "@app/lib/crypto/pqc/pqc-crypto";
import { AsymmetricKeyAlgorithm, isPqcKeyAlgorithm, KMS_TO_OPENSSL_NAME, signingService } from "@app/lib/crypto/sign";
+import { delay } from "@app/lib/delay";
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { alphaNumericNanoId } from "@app/lib/nanoid";
@@ -273,6 +274,7 @@ export const kmsServiceFactory = ({
const deleteInternalKms = async (kmsId: string, orgId: string, tx?: Knex) => {
const kms = await kmsDAL.findByIdWithAssociatedKms(kmsId, tx);
+ if (!kms) return;
if (kms.isExternal) return;
if (kms.orgId !== orgId) throw new ForbiddenRequestError({ message: "KMS doesn't belong to organization" });
return kmsDAL.deleteById(kmsId, tx);
@@ -972,13 +974,28 @@ export const kmsServiceFactory = ({
return key.id;
};
- // Drops the cached KMS material for a project. Best-effort: a failed delete only shortens
- // the window to the TTL, never corrupts data.
+ // Drops the cached KMS material for a project. Call after the writing transaction commits. Retries transient
+ // Redis failures; a final failure is safe to swallow: stale entries either degrade gracefully (creation,
+ // backup restore) or are recovered by the NotFound self-heal in $getProjectSecretManagerKmsDataKey (rotation).
const $invalidateProjectSecretManagerKmsMaterialCache = async (projectId: string) => {
- try {
- await keyStore.deleteItem(KeyStorePrefixes.KmsProjectSecretManagerMaterial(projectId));
- } catch (err) {
- logger.warn({ err, projectId }, `Failed to invalidate project KMS material cache [projectId=${projectId}]`);
+ const cacheKey = KeyStorePrefixes.KmsProjectSecretManagerMaterial(projectId);
+ const maxAttempts = 3;
+ for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
+ try {
+ // eslint-disable-next-line no-await-in-loop
+ await keyStore.deleteItem(cacheKey);
+ return;
+ } catch (err) {
+ if (attempt === maxAttempts) {
+ logger.error(
+ { err, projectId },
+ `Failed to invalidate project KMS material cache after ${maxAttempts} attempts; stale reads self-heal on decrypt [projectId=${projectId}]`
+ );
+ return;
+ }
+ // eslint-disable-next-line no-await-in-loop
+ await delay(100 * attempt);
+ }
}
};
@@ -1014,9 +1031,9 @@ export const kmsServiceFactory = ({
};
/** Single project row read; reuses snapshot for data-key path to avoid duplicate findById. */
- const $getProjectSecretManagerKmsKeyIdAndProject = async (projectId: string, trx?: Knex) => {
- // Transactional callers (key/data-key creation, rotation, backup restore) must read fresh under their advisory lock.
- if (!trx) {
+ const $getProjectSecretManagerKmsKeyIdAndProject = async (projectId: string, trx?: Knex, skipCache = false) => {
+ // Transactional callers (key/data-key creation, rotation, backup restore) must read fresh under their advisory lock
+ if (!trx && !skipCache) {
const material = await $getCachedProjectSecretManagerKmsMaterial(projectId);
if (material.kmsSecretManagerKeyId) {
return { kmsKeyId: material.kmsSecretManagerKeyId, project: material };
@@ -1073,8 +1090,12 @@ export const kmsServiceFactory = ({
return dataKey;
};
- const $getProjectSecretManagerKmsDataKey = async (projectId: string, trx?: Knex) => {
- const { kmsKeyId, project: projectSnapshot } = await $getProjectSecretManagerKmsKeyIdAndProject(projectId, trx);
+ const $getProjectSecretManagerKmsDataKeyImpl = async (projectId: string, trx?: Knex, skipCache = false) => {
+ const { kmsKeyId, project: projectSnapshot } = await $getProjectSecretManagerKmsKeyIdAndProject(
+ projectId,
+ trx,
+ skipCache
+ );
let project = projectSnapshot;
if (!project.kmsSecretManagerEncryptedDataKey) {
@@ -1116,6 +1137,24 @@ export const kmsServiceFactory = ({
});
};
+ const $getProjectSecretManagerKmsDataKey = async (projectId: string, trx?: Knex) => {
+ try {
+ return await $getProjectSecretManagerKmsDataKeyImpl(projectId, trx);
+ } catch (err) {
+ // Self-heal: a NotFound here means the cached material points at a KMS key deleted by rotation
+ // (invalidation failed). Drop the entry and retry once bypassing the cache.
+ if (!trx && err instanceof NotFoundError) {
+ logger.warn(
+ { err, projectId },
+ `Project KMS material resolved from cache failed with NotFound; invalidating cache and retrying fresh [projectId=${projectId}]`
+ );
+ await $invalidateProjectSecretManagerKmsMaterialCache(projectId);
+ return await $getProjectSecretManagerKmsDataKeyImpl(projectId, undefined, true);
+ }
+ throw err;
+ }
+ };
+
const $getDataKey = async (dto: TEncryptWithKmsDataKeyDTO, trx?: Knex) => {
switch (dto.type) {
case KmsDataKey.SecretManager: {
@@ -1217,7 +1256,8 @@ export const kmsServiceFactory = ({
};
const updateProjectSecretManagerKmsKey = async ({ projectId, kms }: TUpdateProjectSecretManagerKmsKeyDTO) => {
- const kmsKeyId = await getProjectSecretManagerKmsKeyId(projectId);
+ // a stale cached id from a previous rotation whose invalidation failed would point at a deleted key row here.
+ const { kmsKeyId } = await $getProjectSecretManagerKmsKeyIdAndProject(projectId, undefined, true);
const currentKms = await kmsDAL.findById(kmsKeyId);
// case: internal kms -> internal kms. no change needed
@@ -1341,6 +1381,9 @@ export const kmsServiceFactory = ({
}
const kmsDoc = await kmsDAL.findByIdWithAssociatedKms(backupKmsKeyId);
+ if (!kmsDoc) {
+ throw new NotFoundError({ message: `KMS with ID '${backupKmsKeyId}' not found` });
+ }
if (kmsDoc.orgId !== project.orgId)
throw new ForbiddenRequestError({
message: "Backup does not belong to project"
@@ -1369,7 +1412,12 @@ export const kmsServiceFactory = ({
},
tx
);
- return kmsDAL.findByIdWithAssociatedKms(key.id, tx);
+ const restoredKms = await kmsDAL.findByIdWithAssociatedKms(key.id, tx);
+ if (!restoredKms) {
+ // invariant: the key was created in this same transaction
+ throw new NotFoundError({ message: `KMS with ID '${key.id}' not found` });
+ }
+ return restoredKms;
});
// Backup restore re-pointed the project at a freshly generated KMS key + re-wrapped data key,
@@ -1384,7 +1432,7 @@ export const kmsServiceFactory = ({
const getKmsById = async (kmsKeyId: string, tx?: Knex) => {
const kms = await kmsDAL.findByIdWithAssociatedKms(kmsKeyId, tx);
- if (!kms.id) {
+ if (!kms) {
throw new NotFoundError({
message: `KMS with ID '${kmsKeyId}' not found`
});