Skip to content

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

refactor(pki): drop pre-authorization gating from renewal failure events

bernie-g68e3667f9629
diff --git a/backend/src/services/certificate-common/certificate-issuance-audit-fns.ts b/backend/src/services/certificate-common/certificate-issuance-audit-fns.ts
index b7592e1fc5..4cd0d5c52f 100644
--- a/backend/src/services/certificate-common/certificate-issuance-audit-fns.ts
+++ b/backend/src/services/certificate-common/certificate-issuance-audit-fns.ts
@@ -54,15 +54,6 @@ export const tagErrorWithCertificateRequest = (error: unknown, certificateReques
const getTaggedCertificateRequestId = (error: unknown) =>
error && typeof error === "object" ? certificateRequestIdByError.get(error) : undefined;
-const errorsAfterAuthorization = new WeakSet<object>();
-
-export const tagErrorAsAfterAuthorization = (error: unknown) => {
- if (error && typeof error === "object") errorsAfterAuthorization.add(error);
-};
-
-export const isErrorAfterAuthorization = (error: unknown) =>
- Boolean(error && typeof error === "object" && errorsAfterAuthorization.has(error));
-
// Never throws, so callers can rethrow the original error untouched.
export const recordCertificateIssuanceFailure = async (
{ auditLogService, certificateAuthorityDAL, pkiApplicationDAL }: TRecordCertificateIssuanceFailureDeps,
diff --git a/backend/src/services/certificate-v3/certificate-renewal-service.ts b/backend/src/services/certificate-v3/certificate-renewal-service.ts
index c67aa392c6..29c05f253e 100644
--- a/backend/src/services/certificate-v3/certificate-renewal-service.ts
+++ b/backend/src/services/certificate-v3/certificate-renewal-service.ts
@@ -55,7 +55,6 @@ import {
mapExtendedKeyUsageToLegacy,
mapKeyUsageToLegacy
} from "../certificate-common/certificate-constants";
-import { tagErrorAsAfterAuthorization } from "../certificate-common/certificate-issuance-audit-fns";
import {
detectSanType,
extractCertificateFromBuffer,
@@ -1070,23 +1069,20 @@ export const certificateRenewalServiceFactory = ({
return application?.name ?? null;
};
- const $renewCertificate = async (
- {
- certificateId,
- actor,
- actorId,
- actorAuthMethod,
- actorOrgId,
- internal = false,
- removeRootsFromChain,
- renewalKeySource: requestedKeySource,
- csr,
- attributes
- }: Omit<TRenewCertificateDTO, "certificateRequestId"> & {
- internal?: boolean;
- },
- onAuthorized: () => void
- ): Promise<TCertificateIssuanceResponse> => {
+ const renewCertificate = async ({
+ certificateId,
+ actor,
+ actorId,
+ actorAuthMethod,
+ actorOrgId,
+ internal = false,
+ removeRootsFromChain,
+ renewalKeySource: requestedKeySource,
+ csr,
+ attributes
+ }: Omit<TRenewCertificateDTO, "certificateRequestId"> & {
+ internal?: boolean;
+ }): Promise<TCertificateIssuanceResponse> => {
const actorCtx: TRenewalActor = { actor, actorId, actorAuthMethod, actorOrgId };
const keySource = resolveRenewalKeySource({ renewalKeySource: requestedKeySource, csr });
let csrRenewalRequest: TCertificateRequest | null = null;
@@ -1123,7 +1119,6 @@ export const certificateRenewalServiceFactory = ({
const renewalAuth = internal
? undefined
: await $authorizeRenewal({ originalCert, profile, actorCtx, isEditingCertificate }, tx);
- onAuthorized();
const { issuerType, ca, caType, policy } = await $resolveRenewalIssuer(
{
@@ -1514,18 +1509,6 @@ export const certificateRenewalServiceFactory = ({
return { ...response, changedAttributes };
};
- const renewCertificate = async (dto: Parameters<typeof $renewCertificate>[0]) => {
- let authorized = false;
- try {
- return await $renewCertificate(dto, () => {
- authorized = true;
- });
- } catch (error) {
- if (authorized) tagErrorAsAfterAuthorization(error);
- throw error;
- }
- };
-
const $loadCertificateForRenewalConfig = async ({
certificateId,
actor,
diff --git a/backend/src/services/certificate-v3/certificate-v3-service.test.ts b/backend/src/services/certificate-v3/certificate-v3-service.test.ts
index 79c29d9e85..6a8c419466 100644
--- a/backend/src/services/certificate-v3/certificate-v3-service.test.ts
+++ b/backend/src/services/certificate-v3/certificate-v3-service.test.ts
@@ -43,7 +43,6 @@ import {
extractAlgorithmsFromCSR,
extractCertificateRequestFromCSR
} from "../certificate-common/certificate-csr-utils";
-import { tagErrorAsAfterAuthorization } from "../certificate-common/certificate-issuance-audit-fns";
import { buildCertificateQuotaKey } from "../certificate-common/certificate-quota-key";
import { certificateV3ServiceFactory, TCertificateV3ServiceFactory } from "./certificate-v3-service";
import { CertificateRenewalKeySource } from "./certificate-v3-types";
@@ -3736,11 +3735,11 @@ describe("CertificateV3Service", () => {
applicationId: "app-123"
};
- it("records a failed renewal with the certificate's details once the caller is authorized", async () => {
+ it("records a failed renewal against the original certificate", async () => {
vi.mocked(mockCertificateDAL.findById).mockResolvedValue(storedCertificate as never);
- const failure = new BadRequestError({ message: "CA is disabled" });
- tagErrorAsAfterAuthorization(failure);
- vi.mocked(mockCertificateDAL.transaction).mockRejectedValueOnce(failure);
+ vi.mocked(mockCertificateDAL.transaction).mockRejectedValueOnce(
+ new BadRequestError({ message: "CA is disabled" })
+ );
await expect(service.renewCertificate(renewDto)).rejects.toThrow("CA is disabled");
@@ -3759,25 +3758,5 @@ describe("CertificateV3Service", () => {
})
);
});
-
- it("records only what the caller sent when renewal fails before authorization", async () => {
- vi.mocked(mockCertificateDAL.findById).mockResolvedValue(storedCertificate as never);
- vi.mocked(mockCertificateDAL.transaction).mockRejectedValueOnce(
- new BadRequestError({ message: "A CSR is required when renewalKeySource is csr" })
- );
-
- await expect(service.renewCertificate(renewDto)).rejects.toThrow("A CSR is required");
-
- const [[call]] = mockAuditLogService.createCollapsedAuditLog.mock.calls as [
- [{ event: { metadata: Record<string, unknown> } }]
- ];
- expect(call.event.metadata).toEqual({
- operation: CertificateIssuanceOperation.RENEW,
- enrollmentType: EnrollmentType.API,
- originalCertificateId: "cert-123",
- errorName: "BadRequest",
- error: "A CSR is required when renewalKeySource is csr"
- });
- });
});
});
diff --git a/backend/src/services/certificate-v3/certificate-v3-service.ts b/backend/src/services/certificate-v3/certificate-v3-service.ts
index 7466f415fe..c5463bbd9d 100644
--- a/backend/src/services/certificate-v3/certificate-v3-service.ts
+++ b/backend/src/services/certificate-v3/certificate-v3-service.ts
@@ -51,7 +51,6 @@ import {
import { validateGoDaddyIssuanceInputs } from "@app/services/certificate-authority/godaddy/godaddy-certificate-authority-validators";
import { TInternalCertificateAuthorityServiceFactory } from "@app/services/certificate-authority/internal/internal-certificate-authority-service";
import {
- isErrorAfterAuthorization,
recordCertificateIssuanceFailure,
tagErrorWithCertificateRequest
} from "@app/services/certificate-common/certificate-issuance-audit-fns";
@@ -564,7 +563,6 @@ export const certificateV3ServiceFactory = ({
actorOrgId,
projectId,
profileId,
- authorized = true,
error,
metadata
}: {
@@ -572,7 +570,6 @@ export const certificateV3ServiceFactory = ({
actorOrgId: string;
projectId?: string;
profileId?: string | null;
- authorized?: boolean;
error: unknown;
metadata: Omit<
Parameters<typeof recordCertificateIssuanceFailure>[1]["metadata"],
@@ -589,9 +586,8 @@ export const certificateV3ServiceFactory = ({
const project = await projectDAL.findById(resolvedProjectId);
if (project?.orgId !== actorOrgId) return;
- // Before the caller is authorized, record only what they sent and look nothing up by name.
await recordCertificateIssuanceFailure(
- authorized ? { auditLogService, certificateAuthorityDAL, pkiApplicationDAL } : { auditLogService },
+ { auditLogService, certificateAuthorityDAL, pkiApplicationDAL },
{
auditLogInfo,
projectId: resolvedProjectId,
@@ -599,7 +595,8 @@ export const certificateV3ServiceFactory = ({
metadata: {
...metadata,
certificateProfileId: profile?.id,
- ...(authorized && { profileName: profile?.slug, caId: metadata.caId ?? profile?.caId })
+ profileName: profile?.slug,
+ caId: metadata.caId ?? profile?.caId
}
}
);
@@ -2452,23 +2449,19 @@ export const certificateV3ServiceFactory = ({
? await certificateDAL.findById(dto.certificateId).catch(() => undefined)
: undefined;
if (certificate) {
- const authorized = isErrorAfterAuthorization(error);
await $recordIssuanceFailure({
auditLogInfo: dto.auditLogInfo,
actorOrgId: dto.actorOrgId,
projectId: certificate.projectId,
- profileId: authorized ? certificate.profileId : null,
- authorized,
+ profileId: certificate.profileId,
error,
metadata: {
operation: CertificateIssuanceOperation.RENEW,
enrollmentType: EnrollmentType.API,
- originalCertificateId: dto.certificateId,
- ...(authorized && {
- commonName: certificate.commonName,
- caId: certificate.caId,
- applicationId: certificate.applicationId
- })
+ originalCertificateId: certificate.id,
+ commonName: certificate.commonName,
+ caId: certificate.caId,
+ applicationId: certificate.applicationId
}
});
}