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): record EST re-enroll CSR mismatches as issuance failures
bernie-g41017fba8c53
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex 95e0dbd3d7..3a775cfe4e 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -4015,7 +4015,9 @@ export const registerRoutes = async (certificateProfileDAL,estEnrollmentConfigDAL,certificatePolicyDAL,- pkiApplicationProfileDAL+ pkiApplicationProfileDAL,+ pkiApplicationDAL,+ auditLogService});const pkiScepService = pkiScepServiceFactory({diff --git a/backend/src/services/certificate-est-v3/certificate-est-v3-service.test.ts b/backend/src/services/certificate-est-v3/certificate-est-v3-service.test.tsindex c60c50bc83..f4c99ab320 100644--- a/backend/src/services/certificate-est-v3/certificate-est-v3-service.test.ts+++ b/backend/src/services/certificate-est-v3/certificate-est-v3-service.test.ts@@ -6,8 +6,11 @@/* eslint-disable @typescript-eslint/no-explicit-any */import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { EventType } from "@app/ee/services/audit-log/audit-log-types";import { BadRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";+import { ActorType } from "@app/services/auth/auth-type";import { CertStatus } from "@app/services/certificate/certificate-types";+import { CertificateIssuanceOperation } from "@app/services/certificate-common/certificate-constants";import { CertificateRequestStatus } from "@app/services/certificate-request/certificate-request-types";import { EnrollmentType } from "../certificate-profile/certificate-profile-types";@@ -69,6 +72,10 @@ vi.mock("@app/services/certificate-authority/certificate-authority-fns", () => (assertCaInProfileProject: vi.fn()}));+vi.mock("@app/services/certificate-common/certificate-csr-utils", () => ({+ extractCertificateRequestFromCSR: vi.fn().mockReturnValue({ commonName: "different.example.com" })+}));+vi.mock("@app/services/project/project-fns", () => ({getProjectKmsCertificateKeyId: vi.fn(() => Promise.resolve("mock-kms-id"))}));@@ -162,6 +169,9 @@ describe("CertificateEstV3Service", () => {validity: { max: "90d" }};+ const mockAuditLogService = { createCollapsedAuditLog: vi.fn() };+ const mockPkiApplicationDAL = { findById: vi.fn() };+beforeEach(async () => {service = certificateEstV3ServiceFactory({certificateV3Service: mockCertificateV3Service,@@ -173,7 +183,9 @@ describe("CertificateEstV3Service", () => {licenseService: mockLicenseService,certificateProfileDAL: mockCertificateProfileDAL,estEnrollmentConfigDAL: mockEstEnrollmentConfigDAL,- certificatePolicyDAL: mockCertificatePolicyDAL+ certificatePolicyDAL: mockCertificatePolicyDAL,+ pkiApplicationDAL: mockPkiApplicationDAL as never,+ auditLogService: mockAuditLogService});mockCertificateProfileDAL.findByIdWithConfigs.mockResolvedValue(mockProfile);@@ -414,6 +426,39 @@ describe("CertificateEstV3Service", () => {).rejects.toThrow(BadRequestError);});+ it("records a renew failure when the CSR subject doesn't match the client certificate", async () => {+ const { Pkcs10CertificateRequest } = await import("@peculiar/x509");+ (Pkcs10CertificateRequest as any).mockImplementation(() => ({+ subject: "CN=different.example.com",+ extensions: []+ }));++ await expect(+ service.simpleReenrollByProfile({+ csr: "mock-csr",+ profileId: "profile-123",+ sslClientCert: encodeURIComponent("-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----"),+ auditLogInfo: { ipAddress: "127.0.0.1", actor: { type: ActorType.PLATFORM, metadata: {} } }+ })+ ).rejects.toThrow("Subject mismatch");++ expect(mockAuditLogService.createCollapsedAuditLog).toHaveBeenCalledWith(+ expect.objectContaining({+ projectId: "project-123",+ actor: { type: ActorType.EST_ACCOUNT, metadata: { profileId: "profile-123" } },+ event: expect.objectContaining({+ type: EventType.CERTIFICATE_ISSUANCE_FAILED,+ metadata: expect.objectContaining({+ operation: CertificateIssuanceOperation.RENEW,+ enrollmentType: EnrollmentType.EST,+ certificateProfileId: "profile-123",+ error: "Subject mismatch"+ })+ })+ })+ );+ });+it("should throw error when approval is required for re-enrollment", async () => {mockCertificateV3Service.signCertificateFromProfile.mockResolvedValue({status: CertificateRequestStatus.PENDING_APPROVAL,diff --git a/backend/src/services/certificate-est-v3/certificate-est-v3-service.ts b/backend/src/services/certificate-est-v3/certificate-est-v3-service.tsindex b2788d9e00..98195d6659 100644--- a/backend/src/services/certificate-est-v3/certificate-est-v3-service.ts+++ b/backend/src/services/certificate-est-v3/certificate-est-v3-service.ts@@ -1,6 +1,6 @@import * as x509 from "@peculiar/x509";-import { AuditLogInfo } from "@app/ee/services/audit-log/audit-log-types";+import { AuditLogInfo, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";import { extractX509CertFromChain } from "@app/lib/certificates/extract-certificate";import { BadRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";import { ActorType } from "@app/services/auth/auth-type";@@ -15,6 +15,8 @@ import {getCaCertChains} from "@app/services/certificate-authority/certificate-authority-fns";import { CertificateIssuanceOperation } from "@app/services/certificate-common/certificate-constants";+import { extractCertificateRequestFromCSR } from "@app/services/certificate-common/certificate-csr-utils";+import { recordCertificateIssuanceFailure } from "@app/services/certificate-common/certificate-issuance-audit-fns";import { TCertificateProfileDALFactory } from "@app/services/certificate-profile/certificate-profile-dal";import { EnrollmentType } from "@app/services/certificate-profile/certificate-profile-types";import { CertificateRequestStatus } from "@app/services/certificate-request/certificate-request-types";@@ -22,6 +24,7 @@ import { resolveEffectiveTtl } from "@app/services/certificate-v3/certificate-v3import { TCertificateV3ServiceFactory } from "@app/services/certificate-v3/certificate-v3-service";import { TEstEnrollmentConfigDALFactory } from "@app/services/enrollment-config/est-enrollment-config-dal";import { TKmsServiceFactory } from "@app/services/kms/kms-service";+import { TPkiApplicationDALFactory } from "@app/services/pki-application/pki-application-dal";import { TPkiApplicationProfileDALFactory } from "@app/services/pki-application/pki-application-profile-dal";import { TProjectDALFactory } from "@app/services/project/project-dal";import { getProjectKmsCertificateKeyId } from "@app/services/project/project-fns";@@ -42,6 +45,8 @@ type TCertificateEstV3ServiceFactoryDep = {estEnrollmentConfigDAL: Pick<TEstEnrollmentConfigDALFactory, "findById">;certificatePolicyDAL: Pick<TCertificatePolicyDALFactory, "findById">;pkiApplicationProfileDAL?: Pick<TPkiApplicationProfileDALFactory, "findOneByApplicationAndProfile">;+ pkiApplicationDAL: Pick<TPkiApplicationDALFactory, "findById">;+ auditLogService: Pick<TAuditLogServiceFactory, "createCollapsedAuditLog">;};export type TCertificateEstV3ServiceFactory = ReturnType<typeof certificateEstV3ServiceFactory>;@@ -57,7 +62,9 @@ export const certificateEstV3ServiceFactory = ({certificateProfileDAL,estEnrollmentConfigDAL,certificatePolicyDAL,- pkiApplicationProfileDAL+ pkiApplicationProfileDAL,+ pkiApplicationDAL,+ auditLogService}: TCertificateEstV3ServiceFactoryDep) => {const resolveEstConfigId = async (profile: { estConfigId?: string | null },@@ -303,11 +310,39 @@ export const certificateEstV3ServiceFactory = ({throw new UnauthorizedError({ message: "Client certificate has been revoked" });}+ const $rejectReenrollCsr = async (message: string) => {+ const error = new BadRequestError({ message });+ if (auditLogInfo) {+ let commonName: string | undefined;+ try {+ commonName = extractCertificateRequestFromCSR(csr).commonName;+ } catch {+ commonName = undefined;+ }+ await recordCertificateIssuanceFailure(+ { auditLogService, certificateAuthorityDAL, pkiApplicationDAL },+ {+ auditLogInfo: { ...auditLogInfo, actor: { type: ActorType.EST_ACCOUNT, metadata: { profileId } } },+ projectId: profile.projectId,+ error,+ metadata: {+ operation: CertificateIssuanceOperation.RENEW,+ enrollmentType: EnrollmentType.EST,+ certificateProfileId: profileId,+ profileName: profile.slug,+ caId: profile.caId,+ commonName,+ applicationId+ }+ }+ );+ }+ return error;+ };+const csrObj = new x509.Pkcs10CertificateRequest(csr);if (csrObj.subject !== cert.subject) {- throw new BadRequestError({- message: "Subject mismatch"- });+ throw await $rejectReenrollCsr("Subject mismatch");}let csrSanSet: Set<string> = new Set();@@ -325,9 +360,7 @@ export const certificateEstV3ServiceFactory = ({}if (csrSanSet.size !== certSanSet.size || ![...csrSanSet].every((element) => certSanSet.has(element))) {- throw new BadRequestError({- message: "Subject alternative names mismatch"- });+ throw await $rejectReenrollCsr("Subject alternative names mismatch");}const policy = await certificatePolicyDAL.findById(profile.certificatePolicyId);diff --git a/docs/documentation/platform/audit-logs.mdx b/docs/documentation/platform/audit-logs.mdxindex 6f56f5ea21..dba557ca95 100644--- a/docs/documentation/platform/audit-logs.mdx+++ b/docs/documentation/platform/audit-logs.mdx@@ -590,7 +590,7 @@ Here are a few common patterns for filtering audit logs, grouped by product.Infisical records a `certificate-issuance-failed` event for certificates requested from a certificate profile through the API or the Infisical UI, ACME, EST, and SCEP, including renewals and certificates issued after an approval. The event is in the `management` class, so it's always recorded.<Note>- If an automatic renewal fails before Infisical sends it to an external CA, Infisical records an `automated-renew-certificate-failed` event instead. Infisical doesn't record `certificate-issuance-failed` for EST enrollment through a certificate template, for EST requests rejected before Infisical reads the CSR (such as a failed sign-in), or for requests the actor's role doesn't allow, which are recorded as [`permission-denied` events](#permission-denials).+ If an automatic renewal fails before Infisical sends it to an external CA, Infisical records an `automated-renew-certificate-failed` event instead. Infisical doesn't record `certificate-issuance-failed` for EST enrollment through a certificate template, for EST requests rejected before Infisical reads the CSR (such as a failed sign-in or a rejected client certificate), or for requests the actor's role doesn't allow, which are recorded as [`permission-denied` events](#permission-denials).</Note><Tabs>diff --git a/docs/documentation/platform/pki/applications/certificates.mdx b/docs/documentation/platform/pki/applications/certificates.mdxindex 1a6381c343..8d4d564f93 100644--- a/docs/documentation/platform/pki/applications/certificates.mdx+++ b/docs/documentation/platform/pki/applications/certificates.mdx@@ -58,7 +58,7 @@ For automated issuance, configure an [enrollment method](/documentation/platformIf Infisical creates a certificate request and then can't issue the certificate, the request shows a **Failed** status on the **Certificate Requests** tab. Hover over the status to see the reason.-Some requests are rejected before Infisical creates a certificate request, such as a request for a TTL longer than the certificate policy allows, so they don't appear on the tab. Infisical records every failure, including these, as a `certificate-issuance-failed` event in the [audit logs](/documentation/platform/audit-logs#certificate-issuance-failures), with the reason and the certificate the request was for.+Some requests are rejected before Infisical creates a certificate request, such as a request for a TTL longer than the certificate policy allows, so they don't appear on the tab. Infisical records these rejections and the failed requests as `certificate-issuance-failed` events in the [audit logs](/documentation/platform/audit-logs#certificate-issuance-failures), with the reason and the certificate the request was for. The audit logs page lists the failures that aren't recorded, such as EST requests whose client certificate is rejected.## Importing certificates