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
fix(pki): remove stale keystore files and ignore blank export passwords
bernie-g7b1e2e788732
diff --git a/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts b/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.tsindex df18fbb2d3..c7e9b51075 100644--- a/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/linux-server/linux-server-pki-sync-fns.ts@@ -29,6 +29,7 @@ import { PkiSyncError } from "../pki-sync-errors";import {buildFileCollisionMessage,exportCertificateForSync,+ getStaleCertificateFiles,isKeystoreExportFormat,PemCertificateExtension,PkiSyncExportFormat@@ -548,6 +549,18 @@ export const linuxServerPkiSyncFactory = ({]);}if (record) {+ const previousFiles =+ (record.syncMetadata as TSyncMetadata)?.files ??+ [record.externalIdentifier].filter((p): p is string => Boolean(p));+ const staleFiles = getStaleCertificateFiles({ previousFiles, writtenPaths, deliveredPaths });+ for (const staleFile of staleFiles) {+ await unlinkIfExists(sftp, staleFile);+ }+ if (staleFiles.length > 0) {+ logger.info(+ `Linux Server PKI sync [syncId=${pkiSync.id}]: removed ${staleFiles.length} file(s) "${baseName}" no longer uses`+ );+ }await certificateSyncDAL.updateById(record.id, {externalIdentifier: primaryPath,syncMetadata: { files: writtenPaths }diff --git a/backend/src/services/pki-sync/pki-sync-export-fns.test.ts b/backend/src/services/pki-sync/pki-sync-export-fns.test.tsindex bb98753725..6f0eb7d43a 100644--- a/backend/src/services/pki-sync/pki-sync-export-fns.test.ts+++ b/backend/src/services/pki-sync/pki-sync-export-fns.test.ts@@ -3,6 +3,7 @@ import forge from "node-forge";import {exportCertificateForSync,getExportedCertificateFileSuffixes,+ getStaleCertificateFiles,PemCertificateExtension,PkiSyncExportFormat,stripUnusedKeystoreOptions,@@ -225,3 +226,36 @@ describe("stripUnusedKeystoreOptions", () => {});});});++describe("getStaleCertificateFiles", () => {+ test("returns files written last time that this run no longer writes", () => {+ expect(+ getStaleCertificateFiles({+ previousFiles: ["/certs/app.jks", "/certs/app.truststore.jks"],+ writtenPaths: ["/certs/app.jks"],+ deliveredPaths: new Set(["/certs/app.jks"])+ })+ ).toEqual(["/certs/app.truststore.jks"]);+ });++ test("keeps a file another certificate wrote this run", () => {+ expect(+ getStaleCertificateFiles({+ previousFiles: ["/certs/app.pem", "/certs/shared.pem"],+ writtenPaths: ["/certs/app.jks"],+ deliveredPaths: new Set(["/certs/app.jks", "/certs/shared.pem"])+ })+ ).toEqual(["/certs/app.pem"]);+ });++ test("ignores case when paths are case-insensitive", () => {+ expect(+ getStaleCertificateFiles({+ previousFiles: ["C:\\certs\\App.jks", "C:\\certs\\app.truststore.jks"],+ writtenPaths: ["C:\\certs\\app.jks"],+ deliveredPaths: new Set(["C:\\certs\\app.jks"]),+ caseInsensitive: true+ })+ ).toEqual(["C:\\certs\\app.truststore.jks"]);+ });+});diff --git a/backend/src/services/pki-sync/pki-sync-export-fns.ts b/backend/src/services/pki-sync/pki-sync-export-fns.tsindex 7fe662bca8..4b2eabaab4 100644--- a/backend/src/services/pki-sync/pki-sync-export-fns.ts+++ b/backend/src/services/pki-sync/pki-sync-export-fns.ts@@ -21,6 +21,24 @@ export const stripUnusedKeystoreOptions = (syncOptions: Record<string, unknown>)return stripped;};+// Files this sync wrote for a certificate last time but no longer writes, such as a truststore+// after it is turned off. Paths another certificate wrote this run are left alone.+export const getStaleCertificateFiles = ({+ previousFiles,+ writtenPaths,+ deliveredPaths,+ caseInsensitive = false+}: {+ previousFiles: string[];+ writtenPaths: string[];+ deliveredPaths: Set<string>;+ caseInsensitive?: boolean;+}): string[] => {+ const normalize = (filePath: string) => (caseInsensitive ? filePath.toLowerCase() : filePath);+ const current = new Set([...writtenPaths, ...deliveredPaths].map(normalize));+ return previousFiles.filter((filePath) => !current.has(normalize(filePath)));+};+export const buildFileCollisionMessage = (filePath: string) =>`Another certificate in this sync already writes "${filePath}". Change the certificate name schema so each certificate gets its own file names.`;diff --git a/backend/src/services/pki-sync/pki-sync-service.ts b/backend/src/services/pki-sync/pki-sync-service.tsindex 77ff374a75..124fcfab95 100644--- a/backend/src/services/pki-sync/pki-sync-service.ts+++ b/backend/src/services/pki-sync/pki-sync-service.ts@@ -742,9 +742,10 @@ export const pkiSyncServiceFactory = ({await $assertTargetHostReachable({ destination, connection, destinationConfig });- const encryptedCredentials = credentials?.exportPassword- ? await encryptPkiSyncCredentials({ orgId: actor.orgId, projectId, credentials, kmsService })- : undefined;+ const encryptedCredentials =+ credentials?.exportPassword && !isBlankExportPassword(credentials.exportPassword)+ ? await encryptPkiSyncCredentials({ orgId: actor.orgId, projectId, credentials, kmsService })+ : undefined;try {const pkiSync = await pkiSyncDAL.create({@@ -994,9 +995,10 @@ export const pkiSyncServiceFactory = ({throw new BadRequestError({ message: EXPORT_PASSWORD_BLANK_MESSAGE });}- const encryptedCredentials = credentials?.exportPassword- ? await encryptPkiSyncCredentials({ orgId: actor.orgId, projectId: pkiSync.projectId, credentials, kmsService })- : undefined;+ const encryptedCredentials =+ credentials?.exportPassword && !isBlankExportPassword(credentials.exportPassword)+ ? await encryptPkiSyncCredentials({ orgId: actor.orgId, projectId: pkiSync.projectId, credentials, kmsService })+ : undefined;const isHealthCheckBeingCleared =resolvedSyncOptions !== undefined &&diff --git a/backend/src/services/pki-sync/windows-server/windows-server-pki-sync-fns.ts b/backend/src/services/pki-sync/windows-server/windows-server-pki-sync-fns.tsindex feb1c7fdcc..98d91bed5f 100644--- a/backend/src/services/pki-sync/windows-server/windows-server-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/windows-server/windows-server-pki-sync-fns.ts@@ -20,6 +20,7 @@ import { PkiSyncError } from "../pki-sync-errors";import {buildFileCollisionMessage,exportCertificateForSync,+ getStaleCertificateFiles,isKeystoreExportFormat,PemCertificateExtension,PkiSyncExportFormat@@ -417,9 +418,31 @@ export const windowsServerPkiSyncFactory = ({]);}if (record) {+ const previousFiles =+ (record.syncMetadata as TSyncMetadata)?.files ??+ [record.externalIdentifier].filter((p): p is string => Boolean(p));+ const staleFiles = getStaleCertificateFiles({+ previousFiles,+ writtenPaths: paths,+ deliveredPaths,+ caseInsensitive: true+ });+ let staleFilesToRetry: string[] = [];+ if (staleFiles.length > 0) {+ try {+ await executeWinRMGatewayOperation(+ { ...target, endpoint: WinRmRpcEndpoint.RemoveFiles, params: { paths: staleFiles } },+ gatewayDeps+ );+ } catch (removeErr) {+ // Keep tracking them so a later run can retry the delete.+ staleFilesToRetry = staleFiles;+ failedRemovals.push({ name: baseName, error: describeFailure(removeErr) });+ }+ }await certificateSyncDAL.updateById(record.id, {externalIdentifier: paths[0],- syncMetadata: { files: paths }+ syncMetadata: { files: [...paths, ...staleFilesToRetry] }});}}diff --git a/frontend/src/components/pki-syncs/forms/EditPkiSyncForm.tsx b/frontend/src/components/pki-syncs/forms/EditPkiSyncForm.tsxindex f1d77634f6..c4c36879c4 100644--- a/frontend/src/components/pki-syncs/forms/EditPkiSyncForm.tsx+++ b/frontend/src/components/pki-syncs/forms/EditPkiSyncForm.tsx@@ -243,10 +243,14 @@ export const EditPkiSyncForm = ({}, [isDirty, onDirtyChange]);const applyUpdate = async ({ connection, ...formData }: TUpdatePkiSyncForm) => {+ const hasNewExportPassword = Boolean(+ (formData as { credentials?: { exportPassword?: string } }).credentials?.exportPassword+ );try {const updatedPkiSync = await updatePkiSync.mutateAsync({syncId: pkiSync.id,...formData,+ ...(hasNewExportPassword ? {} : { credentials: undefined }),connectionId: connection.id,projectId: pkiSync.projectId,destination: pkiSync.destination