Skip to content

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

misc: addressed comments

Sheen Capadngan7a70077b7e1e
diff --git a/backend/src/ee/services/pki-acme/pki-acme-order-dal.ts b/backend/src/ee/services/pki-acme/pki-acme-order-dal.ts
index 97d37ff9c5..cc62fb25ab 100644
--- a/backend/src/ee/services/pki-acme/pki-acme-order-dal.ts
+++ b/backend/src/ee/services/pki-acme/pki-acme-order-dal.ts
@@ -36,6 +36,10 @@ export const pkiAcmeOrderDALFactory = (db: TDbClient) => {
)
.forUpdate(TableName.PkiAcmeOrder)
.where(`${TableName.PkiAcmeOrder}.id`, id)
+ // acmeOrderId is not unique, so an order can end up with more than one certificate request
+ // (e.g. a finalization that was retried after an abandoned attempt). Take the newest so the
+ // order's terminal status is driven by the current attempt rather than an arbitrary row.
+ .orderBy(`${TableName.CertificateRequests}.createdAt`, "desc")
.first();
if (!order) {
return null;
diff --git a/backend/src/ee/services/pki-acme/pki-acme-service.ts b/backend/src/ee/services/pki-acme/pki-acme-service.ts
index 118e44b2ca..729bae6eeb 100644
--- a/backend/src/ee/services/pki-acme/pki-acme-service.ts
+++ b/backend/src/ee/services/pki-acme/pki-acme-service.ts
@@ -109,6 +109,21 @@ import {
TRespondToAcmeChallengeResponse
} from "./pki-acme-types";
+/**
+ * How long an order may sit in `processing` before reconciliation treats the issuance attempt as
+ * abandoned. Finalization claims the order, signs outside of any transaction, then records the
+ * outcome, so a crash in between leaves the order in `processing` with nothing to resolve it.
+ *
+ * This has to exceed the worst case of every issuance path, because an attempt that outlives it gets
+ * resolved while it is still legitimately in flight. The binding constraint is the async external-CA
+ * path: `queueCertificateIssuance` gives AWS ACM Public CA `attempts: 30` with a fixed 60s backoff
+ * (~30 minutes) and the certificate request stays PENDING for that entire window. Reaping earlier
+ * would fail the request mid-issuance, and the queue's later `attachCertificate` would then no-op
+ * because the row is no longer in an attachable status, stranding a certificate that AWS did issue.
+ * Keep this above the queue's retry budget in `certificate-issuance-queue.ts` if that changes.
+ */
+const STALE_PROCESSING_ORDER_TIMEOUT_MS = 60 * 60 * 1000;
+
type TPkiAcmeServiceFactoryDep = {
projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction" | "findById">;
certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findByIdWithAssociatedCa">;
@@ -154,7 +169,7 @@ type TPkiAcmeServiceFactoryDep = {
auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;
approvalPolicyDAL: Pick<TApprovalPolicyDALFactory, "findByProjectId" | "findStepsByPolicyId">;
approvalPolicyService: Pick<TApprovalPolicyServiceFactory, "createRequestFromPolicy">;
- certificateRequestDAL: Pick<TCertificateRequestDALFactory, "create" | "updateById">;
+ certificateRequestDAL: Pick<TCertificateRequestDALFactory, "create" | "updateById" | "transitionFromPending">;
pkiApplicationProfileDAL: Pick<TPkiApplicationProfileDALFactory, "findOneByApplicationAndProfile">;
acmeEnrollmentConfigDAL: Pick<TAcmeEnrollmentConfigDALFactory, "findById">;
};
@@ -416,16 +431,59 @@ export const pkiAcmeServiceFactory = ({
throw new NotFoundError({ message: "ACME order not found" });
}
// Check the status again after we have acquired the lock, as things may have changed since we last checked
- if (
- orderWithCertificateRequest.status !== AcmeOrderStatus.Processing ||
- !orderWithCertificateRequest.certificateRequest
- ) {
+ if (orderWithCertificateRequest.status !== AcmeOrderStatus.Processing) {
return orderWithCertificateRequest;
}
+
+ // Finalization stamps `updatedAt` when it claims the order into `processing`, and nothing touches
+ // the order again until it reaches a terminal state, so this measures how long the current
+ // issuance attempt has been running.
+ const isAttemptStale =
+ Date.now() - new Date(orderWithCertificateRequest.updatedAt).getTime() > STALE_PROCESSING_ORDER_TIMEOUT_MS;
+
+ if (!orderWithCertificateRequest.certificateRequest) {
+ // Finalization claims the order before it records a certificate request, so a `processing`
+ // order with no linked request means the process died in between. Issuance always writes the
+ // request before it does anything irreversible, so no certificate can exist yet, which makes
+ // it safe to hand the order back as `ready` for the client to retry. Only act once the
+ // attempt is stale — before that this is simply an issuance still in flight.
+ if (!isAttemptStale) {
+ return orderWithCertificateRequest;
+ }
+ logger.info(
+ { orderId, processingSince: orderWithCertificateRequest.updatedAt },
+ `Reverting abandoned ACME order to ready [orderId=${orderId}]`
+ );
+ return acmeOrderDAL.updateById(orderId, { status: AcmeOrderStatus.Ready }, tx);
+ }
+
let newStatus: AcmeOrderStatus | undefined;
let newCertificateId: string | undefined;
switch (orderWithCertificateRequest.certificateRequest.status) {
- case CertificateRequestStatus.PENDING:
+ case CertificateRequestStatus.PENDING: {
+ // Unlike the no-request case above, signing may already have happened here, so this cannot
+ // be rolled back to `ready` without risking a duplicate certificate. Fail it instead and
+ // let the client create a fresh order.
+ if (!isAttemptStale) break;
+ const failedRequest = await certificateRequestDAL.transitionFromPending(
+ orderWithCertificateRequest.certificateRequest.id,
+ CertificateRequestStatus.FAILED,
+ "Certificate issuance did not complete",
+ tx
+ );
+ // A null result means the request left PENDING between our read and this update (it was
+ // issued concurrently). Leave the order alone so the next sync resolves it as valid.
+ if (failedRequest) {
+ logger.info(
+ { orderId, certificateRequestId: orderWithCertificateRequest.certificateRequest.id },
+ `Failing abandoned ACME certificate issuance [orderId=${orderId}]`
+ );
+ newStatus = AcmeOrderStatus.Invalid;
+ }
+ break;
+ }
+ // Both of these are legitimately long-lived — one waits on a human approver, the other on
+ // external DNS validation — so they are never reaped on staleness.
case CertificateRequestStatus.PENDING_APPROVAL:
case CertificateRequestStatus.PENDING_VALIDATION:
break;
@@ -1167,7 +1225,7 @@ export const pkiAcmeServiceFactory = ({
// TODO: ideally, this should be doen with onRequest: verifyAuth([AuthMode.ACME_JWS_SIGNATURE]), instead?
const { ownerOrgId: actorOrgId } = (await certificateProfileDAL.findByIdWithOwnerOrgId(profileId))!;
- const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId!);
+ const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId);
if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" });
}
@@ -1370,6 +1428,10 @@ export const pkiAcmeServiceFactory = ({
});
const caType = (ca.externalCa?.type as CaType) ?? CaType.INTERNAL;
+ // Set once the certificate and its order-linked request are durably committed, which happens
+ // inside processCertificateIssuanceForOrder rather than here. Anything that fails after that
+ // point must not invalidate the order, or a real certificate is stranded.
+ let issuedCertificateId: string | undefined;
try {
// Signing runs with no ambient transaction. The internal CA and the external-CA paths
// each open their own short write transaction internally, so at no point does one
@@ -1387,6 +1449,7 @@ export const pkiAcmeServiceFactory = ({
ca,
applicationId: accountApplicationId ?? undefined
});
+ issuedCertificateId = result.certificateId;
await acmeOrderDAL.updateById(orderId, {
status: result.certificateId ? AcmeOrderStatus.Valid : AcmeOrderStatus.Processing,
csr,
@@ -1394,13 +1457,23 @@ export const pkiAcmeServiceFactory = ({
});
certIssuanceJobData = result.certIssuanceJobData;
} catch (exp) {
+ logger.error(exp, "Failed to sign certificate");
+ // TODO: audit log the error
+ if (issuedCertificateId) {
+ // The certificate is already issued and committed, and its certificate request is marked
+ // ISSUED against this order, so marking the order invalid here would strand a real
+ // certificate that nothing reconciles. Leave the order in `processing` instead —
+ // checkAndSyncAcmeOrderStatus resolves it to `valid` off the linked request on the next
+ // poll. Only the order bookkeeping failed; the client's certificate is intact.
+ throw new AcmeServerInternalError({
+ message: "Failed to finalize certificate issuance"
+ });
+ }
await acmeOrderDAL.updateById(orderId, {
csr,
status: AcmeOrderStatus.Invalid,
error: exp instanceof Error ? exp.message : "Unknown error"
});
- logger.error(exp, "Failed to sign certificate");
- // TODO: audit log the error
if (exp instanceof BadRequestError) {
throw new AcmeBadCSRError({ message: `Invalid CSR: ${exp.message}` });
}
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 5db12df419..c20d1d525d 100644
--- a/backend/src/services/certificate-v3/certificate-v3-service.test.ts
+++ b/backend/src/services/certificate-v3/certificate-v3-service.test.ts
@@ -247,7 +247,9 @@ describe("CertificateV3Service", () => {
updateById: vi.fn().mockResolvedValue({ id: "cert-req-123" }),
findById: vi.fn().mockResolvedValue({ id: "cert-req-123" }),
create: vi.fn().mockResolvedValue({ id: "cert-req-123" }),
- transaction: vi.fn()
+ transaction: vi.fn(),
+ transitionFromPending: vi.fn().mockResolvedValue({ id: "cert-req-123" }),
+ attachCertificate: vi.fn().mockResolvedValue({ id: "cert-req-123" })
},
userDAL: {
findById: vi.fn().mockResolvedValue({ id: "user-123" })
diff --git a/backend/src/services/certificate-v3/certificate-v3-service.ts b/backend/src/services/certificate-v3/certificate-v3-service.ts
index bd4f34aa10..fe320cbfd3 100644
--- a/backend/src/services/certificate-v3/certificate-v3-service.ts
+++ b/backend/src/services/certificate-v3/certificate-v3-service.ts
@@ -15,7 +15,7 @@ import {
ResourcePermissionSub
} from "@app/ee/services/permission/resource-permission";
import { TPkiAcmeAccountDALFactory } from "@app/ee/services/pki-acme/pki-acme-account-dal";
-import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
+import { BadRequestError, ForbiddenRequestError, InternalServerError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { ms } from "@app/lib/ms";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
@@ -1868,7 +1868,22 @@ export const certificateV3ServiceFactory = ({
await certificateDAL.updateById(newCert.id, updateData, tx);
// Records the outcome: flips the request to ISSUED and links the certificate in one update.
- await certificateRequestDAL.attachCertificate(pendingRequest.id, newCert.id, tx);
+ // Returns null when the request is no longer attachable, meaning it left a pending status
+ // while the signing above was in flight — cancelled by a user, or failed by ACME order
+ // reconciliation. Throwing here rolls this transaction back, certificate rows included, so a
+ // cancelled request can never silently yield an issued certificate. The signature itself is
+ // wasted, which is the right trade: an unstored signature costs an HSM operation and a
+ // serial, whereas committing would hand back a certificate nothing accounts for.
+ const attachedRequest = await certificateRequestDAL.attachCertificate(pendingRequest.id, newCert.id, tx);
+ if (!attachedRequest) {
+ logger.error(
+ { certificateRequestId: pendingRequest.id, projectId: profile.projectId },
+ `Certificate request left a pending status during signing, aborting issuance [certificateRequestId=${pendingRequest.id}]`
+ );
+ throw new InternalServerError({
+ message: "Certificate request is no longer pending, so issuance was aborted"
+ });
+ }
if (metadata && metadata.length > 0) {
await insertMetadataForCertificate(resourceMetadataDAL, {
@@ -1888,13 +1903,21 @@ export const certificateV3ServiceFactory = ({
}
});
} catch (err) {
- // The request row is why this is recoverable. Mark it failed so the certificate is accounted
- // for as an attempt rather than disappearing silently.
- await certificateRequestDAL.transitionFromPending(
- pendingRequest.id,
- CertificateRequestStatus.FAILED,
- err instanceof Error ? err.message : "Certificate issuance failed"
- );
+ // The request row is why this is recoverable. Mark it failed so the attempt is accounted for
+ // rather than disappearing silently. This is best-effort bookkeeping on an already-failing path,
+ // so its own failure must not replace the error the caller actually needs to see.
+ try {
+ await certificateRequestDAL.transitionFromPending(
+ pendingRequest.id,
+ CertificateRequestStatus.FAILED,
+ err instanceof Error ? err.message : "Certificate issuance failed"
+ );
+ } catch (bookkeepingErr) {
+ logger.error(
+ bookkeepingErr,
+ `Failed to mark certificate request as failed [certificateRequestId=${pendingRequest.id}]`
+ );
+ }
throw err;
}