Skip to content

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

fix(pki): jks truststore root, key match check, exportPassword variable

bernie-ge909fd439f83
diff --git a/backend/src/services/certificate/certificate-jks-fns.test.ts b/backend/src/services/certificate/certificate-jks-fns.test.ts
index b461f17636..6015107510 100644
--- a/backend/src/services/certificate/certificate-jks-fns.test.ts
+++ b/backend/src/services/certificate/certificate-jks-fns.test.ts
@@ -268,28 +268,36 @@ describe("getJksChainCertificates", () => {
});
describe("getJksTruststoreCertificates", () => {
- test("includes the root from caCertificate when the delivered chain omits it", () => {
+ test("keeps the root of an imported chain that has no Infisical CA", () => {
const trusted = getJksTruststoreCertificates({
certificate: leaf.certificate,
- certificateChain: intermediate.certificate,
- caCertificate: root.certificate
+ fullCertificateChain: `${intermediate.certificate}\n${root.certificate}`
});
expect(trusted.map((c) => der(c).toString("base64"))).toEqual(
[intermediate, root].map((c) => der(c.certificate).toString("base64"))
);
});
+ test("adds the CA certificate when the stored chain has no root", () => {
+ const trusted = getJksTruststoreCertificates({
+ certificate: leaf.certificate,
+ fullCertificateChain: intermediate.certificate,
+ caCertificate: root.certificate
+ });
+ expect(trusted).toHaveLength(2);
+ });
+
test("never trusts the leaf and does not repeat the root", () => {
const trusted = getJksTruststoreCertificates({
certificate: leaf.certificate,
- certificateChain: `${leaf.certificate}\n${intermediate.certificate}\n${root.certificate}`,
+ fullCertificateChain: `${leaf.certificate}\n${intermediate.certificate}\n${root.certificate}`,
caCertificate: root.certificate
});
expect(trusted).toHaveLength(2);
});
test("is empty when there is nothing to trust", () => {
- expect(getJksTruststoreCertificates({ certificate: leaf.certificate, certificateChain: " \n" })).toEqual([]);
+ expect(getJksTruststoreCertificates({ certificate: leaf.certificate, fullCertificateChain: " \n" })).toEqual([]);
});
});
diff --git a/backend/src/services/certificate/certificate-jks-fns.ts b/backend/src/services/certificate/certificate-jks-fns.ts
index 9d19ee4bdd..8df79b52a0 100644
--- a/backend/src/services/certificate/certificate-jks-fns.ts
+++ b/backend/src/services/certificate/certificate-jks-fns.ts
@@ -112,12 +112,7 @@ const privateKeyPkcs8Der = (privateKeyPem: string, leafCertificatePem: string):
});
}
- const publicKeyDer = crypto.nativeCrypto.createPublicKey(key).export({ type: "spki", format: "der" });
- const leafPublicKeyDer = new crypto.nativeCrypto.X509Certificate(leafCertificatePem).publicKey.export({
- type: "spki",
- format: "der"
- });
- if (!publicKeyDer.equals(leafPublicKeyDer)) {
+ if (!new crypto.nativeCrypto.X509Certificate(leafCertificatePem).checkPrivateKey(key)) {
throw new BadRequestError({
message: "The certificate's private key does not match its public key, so it cannot be exported"
});
@@ -140,17 +135,20 @@ const uniqueCertificates = (pems: string[], exclude: string[] = []): string[] =>
export const getJksChainCertificates = (certificate: string, certificateChain?: string): string[] =>
uniqueCertificates(splitPemChain(certificateChain ?? ""), [certificate]);
-// The trust anchor comes from caCertificate because the delivered chain may omit the root.
+// Built from the full stored chain because the delivered chain may have had its root removed.
export const getJksTruststoreCertificates = ({
certificate,
- certificateChain,
+ fullCertificateChain,
caCertificate
}: {
certificate: string;
- certificateChain?: string;
+ fullCertificateChain?: string;
caCertificate?: string;
}): string[] =>
- uniqueCertificates([...splitPemChain(certificateChain ?? ""), ...splitPemChain(caCertificate ?? "")], [certificate]);
+ uniqueCertificates(
+ [...splitPemChain(fullCertificateChain ?? ""), ...splitPemChain(caCertificate ?? "")],
+ [certificate]
+ );
const encodeCertificate = (pem: string) => {
const der = certificateDer(pem);
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.ts
index 8bf9196b6b..20007d86b8 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
@@ -473,7 +473,7 @@ export const linuxServerPkiSyncFactory = ({
await removeStaleTempFiles(sftp, config.destinationPath);
for (const [baseName, certData] of Object.entries(certificateMap)) {
- const { cert, privateKey, certificateChain, caCertificate, certificateId } = certData;
+ const { cert, privateKey, certificateChain, fullCertificateChain, caCertificate, certificateId } = certData;
if (!cert) {
skippedCertificates.push({ name: baseName, reason: "Missing certificate data" });
@@ -499,6 +499,7 @@ export const linuxServerPkiSyncFactory = ({
...exportOptions,
certificate: cert,
certificateChain,
+ fullCertificateChain,
caCertificate,
privateKey,
password: exportPassword,
diff --git a/backend/src/services/pki-sync/pki-sync-certificate-map-fns.ts b/backend/src/services/pki-sync/pki-sync-certificate-map-fns.ts
index 1d9e7f35df..05a70db359 100644
--- a/backend/src/services/pki-sync/pki-sync-certificate-map-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-certificate-map-fns.ts
@@ -209,6 +209,7 @@ export const buildCertificateMap = async (
cert: certificatePem,
privateKey: certPrivateKey || "",
certificateChain: processedCertificateChain,
+ fullCertificateChain: certificateChain,
caCertificate,
alternativeNames,
certificateId: certificate.id,
diff --git a/backend/src/services/pki-sync/pki-sync-export-fns.ts b/backend/src/services/pki-sync/pki-sync-export-fns.ts
index c416848836..c868bc2ed7 100644
--- a/backend/src/services/pki-sync/pki-sync-export-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-export-fns.ts
@@ -41,6 +41,7 @@ export type TExportCertificateForSyncParams = {
// Friendly name / alias used inside a PKCS#12 or JKS keystore.
alias: string;
includeTruststore?: boolean;
+ fullCertificateChain?: string;
caCertificate?: string;
// PEM only: the extension for the certificate and chain files. Defaults to ".pem".
pemCertificateExtension?: PemCertificateExtension;
@@ -111,6 +112,7 @@ export const exportCertificateForSync = ({
pemCertificateExtension,
combineCertificateChain,
includeTruststore,
+ fullCertificateChain,
caCertificate
}: TExportCertificateForSyncParams): Promise<TExportedCertificateFile[]> | TExportedCertificateFile[] => {
if (format === PkiSyncExportFormat.Pkcs12) {
@@ -138,7 +140,11 @@ export const exportCertificateForSync = ({
}
];
const trustedCertificates = includeTruststore
- ? getJksTruststoreCertificates({ certificate, certificateChain, caCertificate })
+ ? getJksTruststoreCertificates({
+ certificate,
+ fullCertificateChain: fullCertificateChain ?? certificateChain,
+ caCertificate
+ })
: [];
if (trustedCertificates.length > 0) {
files.push({
diff --git a/backend/src/services/pki-sync/pki-sync-health-check-command-fns.test.ts b/backend/src/services/pki-sync/pki-sync-health-check-command-fns.test.ts
index a5e3ea91af..a3297fe9fd 100644
--- a/backend/src/services/pki-sync/pki-sync-health-check-command-fns.test.ts
+++ b/backend/src/services/pki-sync/pki-sync-health-check-command-fns.test.ts
@@ -6,6 +6,7 @@ import { PkiSyncStatus } from "./pki-sync-enums";
import { PemCertificateExtension, PkiSyncExportFormat } from "./pki-sync-export-fns";
import {
applyHealthCheckCommandUpdate,
+ assertHealthCheckCommandIsTestable,
buildHealthCheckCommandFailureMessage,
buildHealthCheckCommandPlan,
buildHealthCheckFailureMessageFor,
@@ -451,3 +452,11 @@ describe("applyHealthCheckCommandUpdate", () => {
expect(input).toEqual({ healthCheckCommand: "" });
});
});
+
+describe("assertHealthCheckCommandIsTestable", () => {
+ test.each(["{{exportPassword}}", "{{pkcs12Password}}"])("rejects %s because tests have no password", (variable) => {
+ expect(() => assertHealthCheckCommandIsTestable({ healthCheckCommand: `test -n ${variable}` })).toThrow(
+ BadRequestError
+ );
+ });
+});
diff --git a/backend/src/services/pki-sync/pki-sync-health-check-command-fns.ts b/backend/src/services/pki-sync/pki-sync-health-check-command-fns.ts
index b98922709e..37bb53b204 100644
--- a/backend/src/services/pki-sync/pki-sync-health-check-command-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-health-check-command-fns.ts
@@ -58,11 +58,12 @@ export const assertHealthCheckCommandIsTestable = (
});
}
- if (commandUsesHostCommandVariable(command, HostCommandVariable.Pkcs12Password)) {
+ const passwordVariables = [HostCommandVariable.ExportPassword, HostCommandVariable.Pkcs12Password].filter(
+ (variable) => commandUsesHostCommandVariable(command, variable)
+ );
+ if (passwordVariables.length > 0) {
throw new BadRequestError({
- message: `A test cannot resolve ${formatHostCommandVariables([
- HostCommandVariable.Pkcs12Password
- ])} because the export password is only generated when a saved sync delivers a certificate. Run the health check on a saved sync instead.`
+ message: `A test cannot resolve ${formatHostCommandVariables(passwordVariables)} because the export password is only generated when a saved sync delivers a certificate. Run the health check on a saved sync instead.`
});
}
@@ -83,7 +84,7 @@ const hasTruststoreCertificates = (
return (
getJksTruststoreCertificates({
certificate: certData.cert,
- certificateChain: certData.certificateChain,
+ fullCertificateChain: certData.fullCertificateChain ?? certData.certificateChain,
caCertificate: certData.caCertificate
}).length > 0
);
diff --git a/backend/src/services/pki-sync/pki-sync-host-command-fns.ts b/backend/src/services/pki-sync/pki-sync-host-command-fns.ts
index b4f205031d..a7992fac4c 100644
--- a/backend/src/services/pki-sync/pki-sync-host-command-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-host-command-fns.ts
@@ -21,6 +21,7 @@ export enum HostCommandVariable {
CertificateDirectory = "certificateDirectory",
CertificateFiles = "certificateFiles",
CommonName = "commonName",
+ ExportPassword = "exportPassword",
Pkcs12Password = "pkcs12Password"
}
@@ -175,6 +176,7 @@ export const renderHostCommandContext = (
[HostCommandVariable.CertificateDirectory, context.certificateDirectory],
[HostCommandVariable.CertificateFiles, context.certificateFiles.join("\n")],
[HostCommandVariable.CommonName, context.commonName],
+ [HostCommandVariable.ExportPassword, context.pkcs12Password],
[HostCommandVariable.Pkcs12Password, context.pkcs12Password]
]),
toShellLiteral
diff --git a/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts b/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts
index 69ee05dd35..920d3f3026 100644
--- a/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts
+++ b/backend/src/services/pki-sync/pki-sync-post-sync-command-fns.test.ts
@@ -153,6 +153,13 @@ describe("renderHostCommandContext", () => {
);
});
+ test("renders exportPassword and its older pkcs12Password name to the same value", () => {
+ const withPassword = { ...context, pkcs12Password: "s3cret" };
+ expect(
+ renderHostCommandContext("echo {{exportPassword}} {{pkcs12Password}}", withPassword, toPosixShellLiteral)
+ ).toBe("echo 's3cret' 's3cret'");
+ });
+
test("renders an absent optional variable as an empty literal rather than the placeholder", () => {
expect(renderHostCommandContext("echo {{pkcs12Password}}", context, toPosixShellLiteral)).toBe("echo ''");
});
diff --git a/backend/src/services/pki-sync/pki-sync-types.ts b/backend/src/services/pki-sync/pki-sync-types.ts
index fe0f66d81f..97766a8bbc 100644
--- a/backend/src/services/pki-sync/pki-sync-types.ts
+++ b/backend/src/services/pki-sync/pki-sync-types.ts
@@ -127,6 +127,8 @@ export type TCertificateMap = Record<
cert: string;
privateKey: string;
certificateChain?: string;
+ // The stored chain before includeRootCa is applied.
+ fullCertificateChain?: string;
caCertificate?: string;
alternativeNames?: string[];
certificateId?: string;
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.ts
index 7fd88446f6..668254b1fd 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
@@ -351,7 +351,7 @@ export const windowsServerPkiSyncFactory = ({
// Deliver each certificate over its own gateway operation so one certificate's failure is
// recorded against that certificate only, rather than failing the whole batch.
for (const [baseName, certData] of Object.entries(certificateMap)) {
- const { cert, privateKey, certificateChain, caCertificate, certificateId } = certData;
+ const { cert, privateKey, certificateChain, fullCertificateChain, caCertificate, certificateId } = certData;
if (!cert) {
skippedCertificates.push({ name: baseName, reason: "Missing certificate data" });
@@ -375,6 +375,7 @@ export const windowsServerPkiSyncFactory = ({
...exportOptions,
certificate: cert,
certificateChain,
+ fullCertificateChain,
caCertificate,
privateKey,
password: exportPassword,
diff --git a/docs/documentation/platform/pki/applications/certificate-syncs/linux-server.mdx b/docs/documentation/platform/pki/applications/certificate-syncs/linux-server.mdx
index d46caaeeef..94044c49ca 100644
--- a/docs/documentation/platform/pki/applications/certificate-syncs/linux-server.mdx
+++ b/docs/documentation/platform/pki/applications/certificate-syncs/linux-server.mdx
@@ -206,7 +206,7 @@ svc-infisical ALL=(root) NOPASSWD: /usr/bin/systemctl reload nginx
Only Application Admins can set either command, so treat that role as access to the target server.
<Warning>
- The rendered command is visible in the target server's process table while it runs. This only matters if you reference `{{pkcs12Password}}`, and only to accounts that already have a shell on the server the sync writes private keys to.
+ The rendered command is visible in the target server's process table while it runs. This only matters if you reference `{{exportPassword}}`, and only to accounts that already have a shell on the server the sync writes private keys to.
</Warning>
### Health check
@@ -243,7 +243,7 @@ Write a check that answers a question rather than one that changes something. `t
```
</Step>
<Step title="Test it, then save the sync">
- **Test check** runs the check against the host straight away, so you can see the result without waiting for a sync. It resolves variables against the certificates linked to the sync, so it works once at least one is selected and tells you if none is. `{{pkcs12Password}}` is the exception: the export password only exists once a saved sync delivers a certificate, so a test always rejects it.
+ **Test check** runs the check against the host straight away, so you can see the result without waiting for a sync. It resolves variables against the certificates linked to the sync, so it works once at least one is selected and tells you if none is. `{{exportPassword}}` is the exception: the export password only exists once a saved sync delivers a certificate, so a test always rejects it.
Save the sync once the check passes. On a real run, a failing check fails the sync without delivering anything, and the reason appears as the sync's last error.
</Step>
diff --git a/docs/documentation/platform/pki/applications/certificate-syncs/windows-server.mdx b/docs/documentation/platform/pki/applications/certificate-syncs/windows-server.mdx
index 773f179e04..2d3fcd30f8 100644
--- a/docs/documentation/platform/pki/applications/certificate-syncs/windows-server.mdx
+++ b/docs/documentation/platform/pki/applications/certificate-syncs/windows-server.mdx
@@ -249,7 +249,7 @@ Write a check that answers a question rather than one that changes something. Te
```
</Step>
<Step title="Test it, then save the sync">
- **Test check** runs the check against the host straight away, so you can see the result without waiting for a sync. It resolves variables against the certificates linked to the sync, so it works once at least one is selected and tells you if none is. `{{pkcs12Password}}` is the exception: the export password only exists once a saved sync delivers a certificate, so a test always rejects it.
+ **Test check** runs the check against the host straight away, so you can see the result without waiting for a sync. It resolves variables against the certificates linked to the sync, so it works once at least one is selected and tells you if none is. `{{exportPassword}}` is the exception: the export password only exists once a saved sync delivers a certificate, so a test always rejects it.
Save the sync once the check passes. On a real run, a failing check fails the sync without delivering anything, and the reason appears as the sync's last error.
</Step>
@@ -271,7 +271,7 @@ It runs once per sync run that delivers at least one file. If a sync run doesn't
Use a variable wherever you need a value from the run. Typing `{{` in the command field lists the ones available for this sync, so you can pick one instead of remembering the name:
```powershell
- Import-PfxCertificate -FilePath {{certificatePath}} -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString {{pkcs12Password}} -AsPlainText -Force)
+ Import-PfxCertificate -FilePath {{certificatePath}} -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString {{exportPassword}} -AsPlainText -Force)
```
</Step>
<Step title="Save and trigger a sync">
diff --git a/docs/snippets/documentation/platform/pki/applications/certificate-syncs/command-variables.mdx b/docs/snippets/documentation/platform/pki/applications/certificate-syncs/command-variables.mdx
index 5b9602632a..8bc639ad48 100644
--- a/docs/snippets/documentation/platform/pki/applications/certificate-syncs/command-variables.mdx
+++ b/docs/snippets/documentation/platform/pki/applications/certificate-syncs/command-variables.mdx
@@ -6,7 +6,7 @@ Write a variable where you want this run's value. Infisical substitutes it befor
| `{{certificateDirectory}}` | The destination directory |
| `{{certificateFiles}}` | Every path this run writes, one per line |
| `{{commonName}}` | The certificate's common name. Single-certificate syncs only |
-| `{{pkcs12Password}}` | The export password, set only when the export format is PKCS#12 or Java KeyStore |
+| `{{exportPassword}}` | The export password, set only when the export format is PKCS#12 or Java KeyStore. `{{pkcs12Password}}` is an older name for the same value and still works |
The paths are the same for both commands, but a health check runs before anything is written, so they describe the files the run is *about to* write rather than files already on the host.
diff --git a/frontend/src/components/pki-syncs/forms/HostCommandField.tsx b/frontend/src/components/pki-syncs/forms/HostCommandField.tsx
index c46797bb39..30a62d38ee 100644
--- a/frontend/src/components/pki-syncs/forms/HostCommandField.tsx
+++ b/frontend/src/components/pki-syncs/forms/HostCommandField.tsx
@@ -38,7 +38,9 @@ export const HostCommandField = ({
const isKeystore = isKeystoreExportFormat(watch("syncOptions.exportFormat"));
const variables = Object.values(HostCommandVariable).filter(
- (variable) => variable !== HostCommandVariable.Pkcs12Password || isKeystore
+ (variable) =>
+ variable !== HostCommandVariable.Pkcs12Password &&
+ (variable !== HostCommandVariable.ExportPassword || isKeystore)
);
return (
diff --git a/frontend/src/helpers/pkiSyncs.ts b/frontend/src/helpers/pkiSyncs.ts
index 599040243e..cc3059db15 100644
--- a/frontend/src/helpers/pkiSyncs.ts
+++ b/frontend/src/helpers/pkiSyncs.ts
@@ -153,7 +153,7 @@ export const BOOLEAN_SYNC_OPTION_FIELDS = [
{ key: "preserveItemOnRenewal", label: "Preserve Item on Renewal" },
{ key: "updateExistingCertificates", label: "Update Existing Certificates" },
{ key: "combineCertificateChain", label: "Combine Certificate and Chain" },
- { key: "includeTruststore", label: "Deliver JKS Truststore" },
+ { key: "includeTruststore", label: "Deliver Truststore" },
{ key: "includePrivateKey", label: "Include Private Key" }
] as const;
@@ -189,7 +189,9 @@ export const POST_SYNC_COMMAND_VARIABLE_DESCRIPTIONS: Record<HostCommandVariable
[HostCommandVariable.CertificateDirectory]: "The destination directory",
[HostCommandVariable.CertificateFiles]: "Every path written this run, one per line",
[HostCommandVariable.CommonName]: "The certificate's common name",
- [HostCommandVariable.Pkcs12Password]: "The PKCS#12 or JKS export password"
+ [HostCommandVariable.ExportPassword]: "The PKCS#12 or JKS export password",
+ [HostCommandVariable.Pkcs12Password]:
+ "The PKCS#12 or JKS export password (older name for exportPassword)"
};
export const HEALTH_CHECK_COMMAND_VARIABLE_DESCRIPTIONS: Record<HostCommandVariable, string> = {
@@ -197,7 +199,9 @@ export const HEALTH_CHECK_COMMAND_VARIABLE_DESCRIPTIONS: Record<HostCommandVaria
[HostCommandVariable.CertificateDirectory]: "The directory the sync is about to write to",
[HostCommandVariable.CertificateFiles]: "Every path this run will write, one per line",
[HostCommandVariable.CommonName]: "The certificate's common name",
- [HostCommandVariable.Pkcs12Password]: "The PKCS#12 or JKS export password"
+ [HostCommandVariable.ExportPassword]: "The PKCS#12 or JKS export password",
+ [HostCommandVariable.Pkcs12Password]:
+ "The PKCS#12 or JKS export password (older name for exportPassword)"
};
const SINGLE_CERTIFICATE_HOST_COMMAND_VARIABLES = [
diff --git a/frontend/src/hooks/api/pkiSyncs/enums.ts b/frontend/src/hooks/api/pkiSyncs/enums.ts
index 5f423133ef..cb43097beb 100644
--- a/frontend/src/hooks/api/pkiSyncs/enums.ts
+++ b/frontend/src/hooks/api/pkiSyncs/enums.ts
@@ -39,6 +39,7 @@ export enum HostCommandVariable {
CertificateDirectory = "certificateDirectory",
CertificateFiles = "certificateFiles",
CommonName = "commonName",
+ ExportPassword = "exportPassword",
Pkcs12Password = "pkcs12Password"
}