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): keep jks settings while switching formats
bernie-g412fb5854d0b
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 d0f9906070..bb98753725 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@@ -5,6 +5,7 @@ import {getExportedCertificateFileSuffixes,PemCertificateExtension,PkiSyncExportFormat,+ stripUnusedKeystoreOptions,TExportedCertificateFile} from "./pki-sync-export-fns";@@ -205,3 +206,22 @@ describe("getExportedCertificateFileSuffixes matches the real export", () => {expect(exported.find((f) => f.suffix === ".truststore.jks")?.isPrivateKey).toBeFalsy();});});++describe("stripUnusedKeystoreOptions", () => {+ test("drops keystore-only options a format does not use", () => {+ const base = { keystoreAlias: "tomcat", includeTruststore: true, certificateNameSchema: "{{commonName}}" };+ expect(stripUnusedKeystoreOptions({ ...base, exportFormat: PkiSyncExportFormat.Pem })).toEqual({+ exportFormat: PkiSyncExportFormat.Pem,+ certificateNameSchema: "{{commonName}}"+ });+ expect(stripUnusedKeystoreOptions({ ...base, exportFormat: PkiSyncExportFormat.Pkcs12 })).toEqual({+ exportFormat: PkiSyncExportFormat.Pkcs12,+ keystoreAlias: "tomcat",+ certificateNameSchema: "{{commonName}}"+ });+ expect(stripUnusedKeystoreOptions({ ...base, exportFormat: PkiSyncExportFormat.Jks })).toEqual({+ ...base,+ exportFormat: PkiSyncExportFormat.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 2440448f4a..ccfdffb11e 100644--- a/backend/src/services/pki-sync/pki-sync-export-fns.ts+++ b/backend/src/services/pki-sync/pki-sync-export-fns.ts@@ -14,6 +14,13 @@ export enum PkiSyncExportFormat {export const isKeystoreExportFormat = (format: unknown): boolean =>format === PkiSyncExportFormat.Pkcs12 || format === PkiSyncExportFormat.Jks;+export const stripUnusedKeystoreOptions = (syncOptions: Record<string, unknown>): Record<string, unknown> => {+ const stripped = { ...syncOptions };+ if (!isKeystoreExportFormat(stripped.exportFormat)) delete stripped.keystoreAlias;+ if (stripped.exportFormat !== PkiSyncExportFormat.Jks) delete stripped.includeTruststore;+ return stripped;+};+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 d1900aab34..4123e0995c 100644--- a/backend/src/services/pki-sync/pki-sync-service.ts+++ b/backend/src/services/pki-sync/pki-sync-service.ts@@ -36,7 +36,11 @@ import { TSyncMetadata } from "../certificate-sync/certificate-sync-schemas";import { encryptPkiSyncCredentials } from "./pki-sync-credentials-fns";import { TPkiSyncDALFactory } from "./pki-sync-dal";import { HEALTH_CHECK_COMMAND_OPTION_KEY, PkiSync, PkiSyncStatus } from "./pki-sync-enums";-import { isKeystoreExportFormat, KEYSTORE_PASSWORD_REQUIRED_MESSAGE } from "./pki-sync-export-fns";+import {+ isKeystoreExportFormat,+ KEYSTORE_PASSWORD_REQUIRED_MESSAGE,+ stripUnusedKeystoreOptions+} from "./pki-sync-export-fns";import { hasAnyPkiSyncFilter, PKI_SYNC_FILTER_KINDS, PKI_SYNC_PREVIEW_PAGE_SIZE } from "./pki-sync-filter-fns";import {applyPkiSyncCertificateDiff,@@ -684,11 +688,13 @@ export const pkiSyncServiceFactory = ({});const providerCapabilities = getPkiSyncProviderCapabilities(destination);- const resolvedSyncOptions = normalizeNewHealthCheckCommand(- normalizeNewPostSyncCommand({- ...providerCapabilities,- ...syncOptions- })+ const resolvedSyncOptions = stripUnusedKeystoreOptions(+ normalizeNewHealthCheckCommand(+ normalizeNewPostSyncCommand({+ ...providerCapabilities,+ ...syncOptions+ })+ ));await $assertHostCommandWrite({@@ -921,9 +927,11 @@ export const pkiSyncServiceFactory = ({});}- resolvedSyncOptions = applyHealthCheckCommandUpdate(- applyPostSyncCommandUpdate({ ...providerCapabilities, ...syncOptions }, storedSyncOptions?.postSyncCommand),- storedSyncOptions?.healthCheckCommand+ resolvedSyncOptions = stripUnusedKeystoreOptions(+ applyHealthCheckCommandUpdate(+ applyPostSyncCommandUpdate({ ...providerCapabilities, ...syncOptions }, storedSyncOptions?.postSyncCommand),+ storedSyncOptions?.healthCheckCommand+ ));}diff --git a/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/ServerExportFormatFields.tsx b/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/ServerExportFormatFields.tsxindex b6fe2738b2..462460c618 100644--- a/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/ServerExportFormatFields.tsx+++ b/frontend/src/components/pki-syncs/forms/PkiSyncOptionsFields/ServerExportFormatFields.tsx@@ -1,4 +1,3 @@-import { useEffect } from "react";import { Controller, useFormContext } from "react-hook-form";import { Info } from "lucide-react";@@ -34,18 +33,10 @@ type Props = {};export const ServerExportFormatFields = ({ isUpdate }: Props) => {- const { control, watch, setValue, getValues } = useFormContext<TPkiSyncForm>();+ const { control, watch } = useFormContext<TPkiSyncForm>();const exportFormat = watch("syncOptions.exportFormat");const isKeystore = isKeystoreExportFormat(exportFormat);- useEffect(() => {- if (!isKeystore && getValues("syncOptions.keystoreAlias")) {- setValue("syncOptions.keystoreAlias", undefined, { shouldDirty: true });- }- if (exportFormat !== PkiSyncExportFormat.Jks && getValues("syncOptions.includeTruststore")) {- setValue("syncOptions.includeTruststore", undefined, { shouldDirty: true });- }- }, [exportFormat, isKeystore]);const keystoreLabel = exportFormat === PkiSyncExportFormat.Jks ? "JKS" : "PKCS#12";const keystoreFile = exportFormat === PkiSyncExportFormat.Jks ? ".jks keystore" : ".pfx bundle";diff --git a/frontend/src/components/pki-syncs/forms/PkiSyncReviewFields.tsx b/frontend/src/components/pki-syncs/forms/PkiSyncReviewFields.tsxindex 97593c91fd..cdf9cb318b 100644--- a/frontend/src/components/pki-syncs/forms/PkiSyncReviewFields.tsx+++ b/frontend/src/components/pki-syncs/forms/PkiSyncReviewFields.tsx@@ -20,6 +20,7 @@ import {} from "@app/helpers/pkiSyncs";import { useListCertificateProfiles } from "@app/hooks/api/certificateProfiles";import {+ isKeystoreExportFormat,PKI_SYNC_EXPORT_FORMAT_LABELS,PkiSyncExportFormat,usePkiSyncOption,@@ -104,6 +105,12 @@ export const PkiSyncReviewFields = ({ applicationId }: Props = {}) => {});const postSyncCommand =syncOptions && "postSyncCommand" in syncOptions ? syncOptions.postSyncCommand : undefined;+ const exportFormat = (syncOptions as { exportFormat?: PkiSyncExportFormat } | undefined)+ ?.exportFormat;+ const inapplicableOptionKeys = new Set<string>([+ ...(isKeystoreExportFormat(exportFormat) ? [] : ["keystoreAlias"]),+ ...(exportFormat === PkiSyncExportFormat.Jks ? [] : ["includeTruststore"])+ ]);const healthCheckCommand =syncOptions && "healthCheckCommand" in syncOptions ? syncOptions.healthCheckCommand : undefined;@@ -198,7 +205,7 @@ export const PkiSyncReviewFields = ({ applicationId }: Props = {}) => {</ReviewFieldLabel>{BOOLEAN_SYNC_OPTION_FIELDS.map(({ key, label }) => {const optionValue = (syncOptions as Record<string, unknown> | undefined)?.[key];- if (typeof optionValue !== "boolean") return null;+ if (typeof optionValue !== "boolean" || inapplicableOptionKeys.has(key)) return null;return (<ReviewFieldLabel key={key} label={label}><Badge variant={optionValue ? "success" : "danger"}>@@ -225,7 +232,12 @@ export const PkiSyncReviewFields = ({ applicationId }: Props = {}) => {})}{VALUE_SYNC_OPTION_FIELDS.map(({ key, label }) => {const optionValue = (syncOptions as Record<string, unknown> | undefined)?.[key];- if (optionValue === undefined || optionValue === null || optionValue === "")+ if (+ optionValue === undefined ||+ optionValue === null ||+ optionValue === "" ||+ inapplicableOptionKeys.has(key)+ )return null;return (<ReviewFieldLabel key={key} label={label}>