Skip to content

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

Fix KMS memory leak Adds a clean up method because KMS clients like GCP use a persistent connection snd if not closed, will continue to eat up the memory.

Maidul Islam694ab35f53b3
diff --git a/backend/src/ee/services/external-kms/external-kms-service.ts b/backend/src/ee/services/external-kms/external-kms-service.ts
index 49ac293ed1..4d7b1a5b5c 100644
--- a/backend/src/ee/services/external-kms/external-kms-service.ts
+++ b/backend/src/ee/services/external-kms/external-kms-service.ts
@@ -83,18 +83,26 @@ export const externalKmsServiceFactory = ({
throw error;
});
- // if missing kms key this generate a new kms key id and returns new provider input
- const newProviderInput = await externalKms.generateInputKmsKey();
- sanitizedProviderInput = JSON.stringify(newProviderInput);
+ try {
+ // if missing kms key this generate a new kms key id and returns new provider input
+ const newProviderInput = await externalKms.generateInputKmsKey();
+ sanitizedProviderInput = JSON.stringify(newProviderInput);
- await externalKms.validateConnection();
+ await externalKms.validateConnection();
+ } finally {
+ await externalKms.cleanup();
+ }
}
break;
case KmsProviders.Gcp:
{
const externalKms = await GcpKmsProviderFactory({ inputs: provider.inputs });
- await externalKms.validateConnection();
- sanitizedProviderInput = JSON.stringify(provider.inputs);
+ try {
+ await externalKms.validateConnection();
+ sanitizedProviderInput = JSON.stringify(provider.inputs);
+ } finally {
+ await externalKms.cleanup();
+ }
}
break;
default:
@@ -186,8 +194,12 @@ export const externalKmsServiceFactory = ({
);
const updatedProviderInput = { ...decryptedProviderInput, ...provider.inputs };
const externalKms = await AwsKmsProviderFactory({ inputs: updatedProviderInput });
- await externalKms.validateConnection();
- sanitizedProviderInput = JSON.stringify(updatedProviderInput);
+ try {
+ await externalKms.validateConnection();
+ sanitizedProviderInput = JSON.stringify(updatedProviderInput);
+ } finally {
+ await externalKms.cleanup();
+ }
}
break;
case KmsProviders.Gcp:
@@ -197,8 +209,12 @@ export const externalKmsServiceFactory = ({
);
const updatedProviderInput = { ...decryptedProviderInput, ...provider.inputs };
const externalKms = await GcpKmsProviderFactory({ inputs: updatedProviderInput });
- await externalKms.validateConnection();
- sanitizedProviderInput = JSON.stringify(updatedProviderInput);
+ try {
+ await externalKms.validateConnection();
+ sanitizedProviderInput = JSON.stringify(updatedProviderInput);
+ } finally {
+ await externalKms.cleanup();
+ }
}
break;
default:
@@ -368,7 +384,11 @@ export const externalKmsServiceFactory = ({
const fetchGcpKeys = async ({ credential, gcpRegion }: Pick<TExternalKmsGcpSchema, "credential" | "gcpRegion">) => {
const externalKms = await GcpKmsProviderFactory({ inputs: { credential, gcpRegion, keyName: "" } });
- return externalKms.getKeysList();
+ try {
+ return await externalKms.getKeysList();
+ } finally {
+ await externalKms.cleanup();
+ }
};
return {
diff --git a/backend/src/ee/services/external-kms/providers/aws-kms.ts b/backend/src/ee/services/external-kms/providers/aws-kms.ts
index 6d9166a3a9..8622390422 100644
--- a/backend/src/ee/services/external-kms/providers/aws-kms.ts
+++ b/backend/src/ee/services/external-kms/providers/aws-kms.ts
@@ -2,6 +2,8 @@ import { CreateKeyCommand, DecryptCommand, DescribeKeyCommand, EncryptCommand, K
import { AssumeRoleCommand, STSClient } from "@aws-sdk/client-sts";
import { randomUUID } from "crypto";
+import { logger } from "@app/lib/logger";
+
import { ExternalKmsAwsSchema, KmsAwsCredentialType, TExternalKmsAwsSchema, TExternalKmsProviderFns } from "./model";
const getAwsKmsClient = async (providerInputs: TExternalKmsAwsSchema) => {
@@ -102,10 +104,21 @@ export const AwsKmsProviderFactory = async ({ inputs }: AwsKmsProviderArgs): Pro
return { data: Buffer.from(decryptionCommand.Plaintext) };
};
+ const cleanup = async () => {
+ try {
+ awsClient.destroy();
+ return true;
+ } catch (error) {
+ logger.error(error, "cleanup: failed to destroy AWS KMS client");
+ return false;
+ }
+ };
+
return {
generateInputKmsKey,
validateConnection,
encrypt,
- decrypt
+ decrypt,
+ cleanup
};
};
diff --git a/backend/src/ee/services/external-kms/providers/gcp-kms.ts b/backend/src/ee/services/external-kms/providers/gcp-kms.ts
index bee1eb24ba..5abe0afc7c 100644
--- a/backend/src/ee/services/external-kms/providers/gcp-kms.ts
+++ b/backend/src/ee/services/external-kms/providers/gcp-kms.ts
@@ -45,6 +45,16 @@ export const GcpKmsProviderFactory = async ({ inputs }: GcpKmsProviderArgs): Pro
}
};
+ const cleanup = async () => {
+ try {
+ await gcpKmsClient.close();
+ return true;
+ } catch (error) {
+ logger.error(error, "cleanup: failed to close GCP KMS client");
+ return false;
+ }
+ };
+
// Used when adding the KMS to fetch the list of keys in specified region
const getKeysList = async () => {
try {
@@ -108,6 +118,7 @@ export const GcpKmsProviderFactory = async ({ inputs }: GcpKmsProviderArgs): Pro
validateConnection,
getKeysList,
encrypt,
- decrypt
+ decrypt,
+ cleanup
};
};
diff --git a/backend/src/ee/services/external-kms/providers/model.ts b/backend/src/ee/services/external-kms/providers/model.ts
index 436b39423a..71f108dbc8 100644
--- a/backend/src/ee/services/external-kms/providers/model.ts
+++ b/backend/src/ee/services/external-kms/providers/model.ts
@@ -98,4 +98,5 @@ export type TExternalKmsProviderFns = {
validateConnection: () => Promise<boolean>;
encrypt: (data: Buffer) => Promise<{ encryptedBlob: Buffer }>;
decrypt: (encryptedBlob: Buffer) => Promise<{ data: Buffer }>;
+ cleanup: () => Promise<boolean>;
};
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index e576d67680..bc9c4afa3a 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -12,6 +12,7 @@ import { generateSrpServerKey, srpCheckClientProof } from "@app/lib/crypto";
import { infisicalSymmetricEncypt } from "@app/lib/crypto/encryption";
import { getUserPrivateKey } from "@app/lib/crypto/srp";
import { BadRequestError, DatabaseError, ForbiddenRequestError, UnauthorizedError } from "@app/lib/errors";
+import { removeTrailingSlash } from "@app/lib/fn";
import { logger } from "@app/lib/logger";
import { getUserAgentType } from "@app/server/plugins/audit-log";
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
@@ -39,7 +40,6 @@ import {
AuthTokenType,
MfaMethod
} from "./auth-type";
-import { removeTrailingSlash } from "@app/lib/fn";
type TAuthLoginServiceFactoryDep = {
userDAL: TUserDALFactory;
diff --git a/backend/src/services/kms/kms-service.ts b/backend/src/services/kms/kms-service.ts
index 07ed90bef5..8c23ce5479 100644
--- a/backend/src/services/kms/kms-service.ts
+++ b/backend/src/services/kms/kms-service.ts
@@ -342,9 +342,12 @@ export const kmsServiceFactory = ({
}
return async ({ cipherTextBlob }: Pick<TDecryptWithKmsDTO, "cipherTextBlob">) => {
- const { data } = await externalKms.decrypt(cipherTextBlob);
-
- return data;
+ try {
+ const { data } = await externalKms.decrypt(cipherTextBlob);
+ return data;
+ } finally {
+ await externalKms.cleanup();
+ }
};
}
@@ -557,9 +560,12 @@ export const kmsServiceFactory = ({
}
return async ({ plainText }: Pick<TEncryptWithKmsDTO, "plainText">) => {
- const { encryptedBlob } = await externalKms.encrypt(plainText);
-
- return { cipherTextBlob: encryptedBlob };
+ try {
+ const { encryptedBlob } = await externalKms.encrypt(plainText);
+ return { cipherTextBlob: encryptedBlob };
+ } finally {
+ await externalKms.cleanup();
+ }
};
}