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
Make cert chain optional on imports
Carlos Monastyrski83db620f5d4f
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.tsindex 40a521477d..a2b8973125 100644--- a/backend/src/lib/api-docs/constants.ts+++ b/backend/src/lib/api-docs/constants.ts@@ -2199,12 +2199,13 @@ export const CERTIFICATES = {projectSlug: "Slug of the project to import the certificate into.",certificatePem: "The PEM-encoded leaf certificate.",privateKeyPem: "Optional PEM-encoded private key associated with the certificate.",- chainPem: "The PEM-encoded chain of intermediate certificates.",+ chainPem: "Optional PEM-encoded chain of intermediate certificates.",friendlyName: "A friendly name for the certificate.",pkiCollectionId: "The ID of the PKI collection to add the certificate to.",certificate: "The imported certificate.",- certificateChain: "The certificate chain associated with the imported certificate.",+ certificateChain:+ "The certificate chain associated with the imported certificate. Returned only when a chain was supplied at import.",privateKey:"The PEM-encoded private key associated with the imported certificate. Returned only when a private key was supplied at import.",serialNumber: "The serial number of the imported certificate."diff --git a/backend/src/server/routes/v1/certificate-router.ts b/backend/src/server/routes/v1/certificate-router.tsindex 2b7aacf761..644e7c2c3c 100644--- a/backend/src/server/routes/v1/certificate-router.ts+++ b/backend/src/server/routes/v1/certificate-router.ts@@ -1466,7 +1466,7 @@ export const registerCertificateRouter = async (server: FastifyZodProvider) => {body: z.object({certificatePem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.certificatePem),privateKeyPem: z.string().trim().min(1).optional().describe(CERTIFICATES.IMPORT.privateKeyPem),- chainPem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.chainPem),+ chainPem: z.string().trim().min(1).optional().describe(CERTIFICATES.IMPORT.chainPem),friendlyName: z.string().trim().optional().describe(CERTIFICATES.IMPORT.friendlyName),pkiCollectionId: z.string().trim().optional().describe(CERTIFICATES.IMPORT.pkiCollectionId),@@ -1475,7 +1475,7 @@ export const registerCertificateRouter = async (server: FastifyZodProvider) => {response: {200: z.object({certificate: z.string().trim().describe(CERTIFICATES.IMPORT.certificate),- certificateChain: z.string().trim().describe(CERTIFICATES.IMPORT.certificateChain),+ certificateChain: z.string().trim().optional().describe(CERTIFICATES.IMPORT.certificateChain),privateKey: z.string().trim().optional().describe(CERTIFICATES.IMPORT.privateKey),serialNumber: z.string().trim().describe(CERTIFICATES.IMPORT.serialNumber)})diff --git a/backend/src/server/routes/v1/deprecated-certificate-router.ts b/backend/src/server/routes/v1/deprecated-certificate-router.tsindex 72cfba52d6..fc71385884 100644--- a/backend/src/server/routes/v1/deprecated-certificate-router.ts+++ b/backend/src/server/routes/v1/deprecated-certificate-router.ts@@ -302,7 +302,7 @@ export const registerDeprecatedCertRouter = async (server: FastifyZodProvider) =certificatePem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.certificatePem),privateKeyPem: z.string().trim().min(1).optional().describe(CERTIFICATES.IMPORT.privateKeyPem),- chainPem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.chainPem),+ chainPem: z.string().trim().min(1).optional().describe(CERTIFICATES.IMPORT.chainPem),friendlyName: z.string().trim().optional().describe(CERTIFICATES.IMPORT.friendlyName),pkiCollectionId: z.string().trim().optional().describe(CERTIFICATES.IMPORT.pkiCollectionId)@@ -310,7 +310,7 @@ export const registerDeprecatedCertRouter = async (server: FastifyZodProvider) =response: {200: z.object({certificate: z.string().trim().describe(CERTIFICATES.IMPORT.certificate),- certificateChain: z.string().trim().describe(CERTIFICATES.IMPORT.certificateChain),+ certificateChain: z.string().trim().optional().describe(CERTIFICATES.IMPORT.certificateChain),privateKey: z.string().trim().optional().describe(CERTIFICATES.IMPORT.privateKey),serialNumber: z.string().trim().describe(CERTIFICATES.IMPORT.serialNumber)})diff --git a/backend/src/services/certificate/certificate-service.ts b/backend/src/services/certificate/certificate-service.tsindex 89d869e3ba..1fd7381f4a 100644--- a/backend/src/services/certificate/certificate-service.ts+++ b/backend/src/services/certificate/certificate-service.ts@@ -730,38 +730,40 @@ export const certificateServiceFactory = ({const leafCert = new x509.X509Certificate(certificatePem);- // Verify the certificate chain- const chainCerts = splitPemChain(chainPem).map((pem) => new x509.X509Certificate(pem));+ // Verify the certificate chain when one is provided+ if (chainPem) {+ const chainCerts = splitPemChain(chainPem).map((pem) => new x509.X509Certificate(pem));- // Remove leaf cert from the chain if it's present- if (chainCerts[0].equal(leafCert)) {- chainCerts.splice(0, 1);- }+ // Remove leaf cert from the chain if it's present+ if (chainCerts[0]?.equal(leafCert)) {+ chainCerts.splice(0, 1);+ }- if (chainCerts.length === 0) {- throw new BadRequestError({- message: "Certificate chain must contain at least one issuer certificate"- });- }+ if (chainCerts.length === 0) {+ throw new BadRequestError({+ message: "Certificate chain must contain at least one issuer certificate"+ });+ }- // Verify leaf certificate is signed by the first certificate in the chain- const isLeafVerified = await leafCert.verify({ publicKey: chainCerts[0].publicKey }).catch(() => false);- if (!isLeafVerified) {- throw new BadRequestError({ message: "Leaf certificate verification against chain failed" });- }+ // Verify leaf certificate is signed by the first certificate in the chain+ const isLeafVerified = await leafCert.verify({ publicKey: chainCerts[0].publicKey }).catch(() => false);+ if (!isLeafVerified) {+ throw new BadRequestError({ message: "Leaf certificate verification against chain failed" });+ }- // Verify the entire chain of trust- const verificationPromises = chainCerts.slice(0, -1).map(async (currentCert, index) => {- const issuerCert = chainCerts[index + 1];- return currentCert.verify({ publicKey: issuerCert.publicKey }).catch(() => false);- });+ // Verify the entire chain of trust+ const verificationPromises = chainCerts.slice(0, -1).map(async (currentCert, index) => {+ const issuerCert = chainCerts[index + 1];+ return currentCert.verify({ publicKey: issuerCert.publicKey }).catch(() => false);+ });- const verificationResults = await Promise.all(verificationPromises);+ const verificationResults = await Promise.all(verificationPromises);- if (verificationResults.some((result) => !result)) {- throw new BadRequestError({- message: "Certificate chain verification failed: broken trust chain"- });+ if (verificationResults.some((result) => !result)) {+ throw new BadRequestError({+ message: "Certificate chain verification failed: broken trust chain"+ });+ }}// Verify private key matches the certificate when one is provided@@ -838,9 +840,9 @@ export const certificateServiceFactory = ({extendedKeyUsages = extKeyUsageExt.usages.map((ekuOid) => CertExtendedKeyUsageOIDToName[ekuOid as string]);}- const { cipherTextBlob: encryptedCertificateChain } = await kmsEncryptor({- plainText: Buffer.from(chainPem)- });+ const encryptedCertificateChain = chainPem+ ? (await kmsEncryptor({ plainText: Buffer.from(chainPem) })).cipherTextBlob+ : null;const cert = await certificateDAL.transaction(async (tx) => {try {diff --git a/backend/src/services/certificate/certificate-types.ts b/backend/src/services/certificate/certificate-types.tsindex 2628ca7d57..e99e2e6b73 100644--- a/backend/src/services/certificate/certificate-types.ts+++ b/backend/src/services/certificate/certificate-types.ts@@ -153,7 +153,7 @@ export type TImportCertDTO = {certificatePem: string;privateKeyPem?: string;- chainPem: string;+ chainPem?: string;} & Omit<TProjectPermission, "projectId">;export type TGetCertPrivateKeyDTO = {diff --git a/frontend/src/hooks/api/certificates/types.ts b/frontend/src/hooks/api/certificates/types.tsindex 91ef46b2d9..3a6ef78915 100644--- a/frontend/src/hooks/api/certificates/types.ts+++ b/frontend/src/hooks/api/certificates/types.ts@@ -82,7 +82,7 @@ export type TRevokeCertDTO = {export type TImportCertificateDTO = {certificatePem: string;privateKeyPem?: string;- chainPem: string;+ chainPem?: string;pkiCollectionId?: string;friendlyName?: string;@@ -91,7 +91,7 @@ export type TImportCertificateDTO = {export type TImportCertificateResponse = {certificate: string;- certificateChain: string;+ certificateChain?: string;privateKey?: string;serialNumber: string;};diff --git a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateContent.tsx b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateContent.tsxindex 3284825d3f..457129222b 100644--- a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateContent.tsx+++ b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateContent.tsx@@ -8,7 +8,7 @@ import { useTimedReset } from "@app/hooks";type Props = {serialNumber: string;certificate: string;- certificateChain: string;+ certificateChain?: string;privateKey?: string;};diff --git a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsxindex 89c1a3b472..c28342fc9b 100644--- a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx+++ b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx@@ -13,7 +13,7 @@ import { CertificateContent } from "./CertificateContent";const schema = z.object({certificatePem: z.string().trim().min(1, "Certificate PEM is required"),privateKeyPem: z.string().trim().optional(),- chainPem: z.string().trim().min(1, "Certificate Chain PEM is required")+ chainPem: z.string().trim().optional()});export type FormData = z.infer<typeof schema>;@@ -30,7 +30,7 @@ type Props = {type TCertificateDetails = {serialNumber: string;certificate: string;- certificateChain: string;+ certificateChain?: string;privateKey?: string;};@@ -53,10 +53,11 @@ export const CertificateImportModal = ({ popUp, handlePopUpToggle, applicationIdconst onFormSubmit = async ({ certificatePem, privateKeyPem, chainPem }: FormData) => {const trimmedPrivateKey = privateKeyPem?.trim();+ const trimmedChain = chainPem?.trim();const { serialNumber, certificate, certificateChain, privateKey } = await importCertificate({certificatePem,...(trimmedPrivateKey ? { privateKeyPem: trimmedPrivateKey } : {}),- chainPem,+ ...(trimmedChain ? { chainPem: trimmedChain } : {}),applicationId});@@ -108,10 +109,9 @@ export const CertificateImportModal = ({ popUp, handlePopUpToggle, applicationIdname="chainPem"render={({ field, fieldState: { error } }) => (<FormControl- label="Certificate Chain PEM"+ label="Certificate Chain PEM (optional)"isError={Boolean(error)}errorText={error?.message}- isRequired><TextArea {...field} isDisabled={Boolean(cert)} /></FormControl>