Skip to content

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

Address greptile comments

Carlos Monastyrski8ec229bcfda6
diff --git a/backend/src/services/certificate/certificate-dal.ts b/backend/src/services/certificate/certificate-dal.ts
index 8a0451273a..28c88ca271 100644
--- a/backend/src/services/certificate/certificate-dal.ts
+++ b/backend/src/services/certificate/certificate-dal.ts
@@ -744,15 +744,12 @@ export const certificateDALFactory = (db: TDbClient) => {
type TCertificateLookupFilter = { id: string; serialNumber?: never } | { id?: never; serialNumber: string };
- const findLatestRenewalOf = async (
- anchor: { id: string; orderId: string },
- tx?: Knex
- ): Promise<string | null> => {
+ const findLatestRenewalOf = async (anchor: { id: string; orderId: string }, tx?: Knex): Promise<string | null> => {
try {
// Walks the anchor's own descendants rather than the whole order. A certificate renewed twice
// leaves two branches sharing one orderId, and the sibling branch never replaced this anchor,
// so picking the newest row in the order can hand back a certificate for a different name.
- const result = await (tx || db.replicaNode()).raw(
+ const result = await (tx || db.replicaNode()).raw<{ rows: { id: string }[] }>(
`
WITH RECURSIVE descendants AS (
SELECT id, "createdAt", status, "notAfter", 1 AS depth
@@ -780,7 +777,7 @@ export const certificateDALFactory = (db: TDbClient) => {
]
);
- return (result.rows as { id: string }[])[0]?.id ?? null;
+ return result.rows[0]?.id ?? null;
} catch (error) {
throw new DatabaseError({ error, name: "Find latest renewal of certificate" });
}
diff --git a/backend/src/services/certificate/certificate-service.ts b/backend/src/services/certificate/certificate-service.ts
index b27bf024f4..393be02e12 100644
--- a/backend/src/services/certificate/certificate-service.ts
+++ b/backend/src/services/certificate/certificate-service.ts
@@ -272,7 +272,33 @@ export const certificateServiceFactory = ({
}
}
- const latestRenewalCertificateId = await certificateDAL.findLatestRenewalOf(cert);
+ const resolvedRenewalId = await certificateDAL.findLatestRenewalOf(cert);
+ let latestRenewalCertificateId: string | null = null;
+
+ if (resolvedRenewalId) {
+ const renewal = await certificateDAL.findById(resolvedRenewalId);
+
+ if (renewal) {
+ const renewalMetadataRows = await resourceMetadataDAL.find({ certificateId: renewal.id });
+ const renewalReadSubject = subject(ProjectPermissionSub.Certificates, {
+ commonName: renewal.commonName,
+ altNames: renewal.altNames?.split(",").map((s) => s.trim()),
+ serialNumber: renewal.serialNumber,
+ metadata: renewalMetadataRows.map(({ key, value }) => ({ key, value: value || "" }))
+ });
+
+ const canReadRenewal =
+ permission.can(ProjectPermissionCertificateActions.Read, renewalReadSubject) ||
+ (await $canActOnCertViaApplication(renewal, ResourcePermissionCertificateActions.Read, {
+ type: actor,
+ id: actorId,
+ authMethod: actorAuthMethod,
+ orgId: actorOrgId
+ }));
+
+ if (canReadRenewal) latestRenewalCertificateId = renewal.id;
+ }
+ }
return {
cert: {
@@ -395,7 +421,25 @@ export const certificateServiceFactory = ({
let deletedCert;
try {
- deletedCert = await certificateDAL.deleteById(cert.id);
+ deletedCert = await certificateDAL.transaction(async (tx) => {
+ const successors = await certificateDAL.find({ renewedFromCertificateId: cert.id }, { tx });
+
+ await certificateDAL.update(
+ { renewedFromCertificateId: cert.id },
+ { renewedFromCertificateId: cert.renewedFromCertificateId ?? null },
+ tx
+ );
+
+ if (cert.renewedFromCertificateId && successors.length > 0) {
+ await certificateDAL.updateById(
+ cert.renewedFromCertificateId,
+ { renewedByCertificateId: successors[0].id },
+ tx
+ );
+ }
+
+ return certificateDAL.deleteById(cert.id, tx);
+ });
} catch (err) {
const innerError = err instanceof DatabaseError ? (err.error as { code?: string; constraint?: string }) : null;
if (innerError?.code === "23503") {