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-syncs): reuse the existing destination item on renewal when the old certificate's sync link is gone
Carlos Monastyrski7efe8660e5b9
diff --git a/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.test.ts b/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.test.tsindex fdb9b424d2..b0a516f3a2 100644--- a/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.test.ts+++ b/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.test.ts@@ -33,11 +33,17 @@ vi.mock("@aws-sdk/client-secrets-manager", async (importOriginal) => {const CERT_ID = "550e8400-e29b-41d4-a716-446655440000";const SECRET_NAME = "infisical-550e8400e29b41d4a716446655440000";-const makeFactory = (ownedByOtherSync: string[] = []) =>+const makeFactory = (+ ownedByOtherSync: string[] = [],+ {+ certificate = { id: CERT_ID } as Record<string, unknown>,+ syncRecords = [] as Array<{ certificateId: string; externalIdentifier: string }>+ } = {}+) =>awsSecretsManagerPkiSyncFactory({- certificateDAL: { findById: async () => ({ id: CERT_ID }) } as never,+ certificateDAL: { findById: async () => certificate } as never,certificateSyncDAL: {- findByPkiSyncId: async () => [],+ findByPkiSyncId: async () => syncRecords,claimExternalIdentifier: async (_syncId: string, _certId: string, name: string) => {events.push(`claim:${name}`);},@@ -81,4 +87,19 @@ describe("AWS Secrets Manager PKI sync destination ownership", () => {expect(events).toEqual([`DeleteSecretCommand:${orphanSecret}`]);});++ test("updates the preserved secret after a renewal once the old certificate's link is gone", async () => {+ const renewedCertId = "660e8400-e29b-41d4-a716-446655440000";+ const renewedSecretName = "infisical-660e8400e29b41d4a716446655440000";+ existingSecretNames.push(SECRET_NAME);++ await makeFactory([], {+ certificate: { id: renewedCertId, renewedFromCertificateId: CERT_ID },+ syncRecords: [{ certificateId: renewedCertId, externalIdentifier: SECRET_NAME }]+ }).syncCertificates({ ...pkiSync, syncOptions: { ...pkiSync.syncOptions, preserveSecretOnRenewal: true } }, {+ [renewedSecretName]: { cert: "CERT", privateKey: "KEY", certificateId: renewedCertId }+ } as never);++ expect(events).toEqual([`claim:${SECRET_NAME}`, `UpdateSecretCommand:${SECRET_NAME}`]);+ });});diff --git a/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts b/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.tsindex b596672769..5c3c3c6547 100644--- a/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/aws-secrets-manager/aws-secrets-manager-pki-sync-fns.ts@@ -280,8 +280,9 @@ export const awsSecretsManagerPkiSyncFactory = ({continue;}- const syncRecordLookupId = certificate?.renewedFromCertificateId || certificateId;- const existingRecord = syncRecordsByCertId.get(syncRecordLookupId);+ const existingRecord =+ (certificate?.renewedFromCertificateId && syncRecordsByCertId.get(certificate.renewedFromCertificateId)) ||+ syncRecordsByCertId.get(certificateId);let shouldProcess = true;let isUpdate = false;diff --git a/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts b/backend/src/services/pki-sync/chef/chef-pki-sync-fns.tsindex 8457a6af6b..a3959c5834 100644--- a/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/chef/chef-pki-sync-fns.ts@@ -232,8 +232,9 @@ export const chefPkiSyncFactory = ({continue;}- const syncRecordLookupId = certificate?.renewedFromCertificateId || certificateId;- const existingSyncRecord = syncRecordsByCertId.get(syncRecordLookupId);+ const existingSyncRecord =+ (certificate?.renewedFromCertificateId && syncRecordsByCertId.get(certificate.renewedFromCertificateId)) ||+ syncRecordsByCertId.get(certificateId);let shouldProcess = true;let isUpdate = false;diff --git a/backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-fns.ts b/backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-fns.tsindex 2d4b7bccd3..7a8686cf8e 100644--- a/backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/gcp-certificate-manager/gcp-certificate-manager-pki-sync-fns.ts@@ -405,9 +405,9 @@ export const gcpCertificateManagerPkiSyncFactory = ({return { skipReason: error instanceof Error ? error.message : "Unsupported key algorithm" };}- const syncRecordLookupId = renewedFromCertificateId || certificateId;const existingSyncRecord =- typeof syncRecordLookupId === "string" ? syncRecordsByCertId.get(syncRecordLookupId) : undefined;+ (renewedFromCertificateId && syncRecordsByCertId.get(renewedFromCertificateId)) ||+ (typeof certificateId === "string" ? syncRecordsByCertId.get(certificateId) : undefined);const reusableResourceName =preserveItemOnRenewal &&existingSyncRecord?.externalIdentifier &&diff --git a/backend/src/services/pki-sync/kemp-loadmaster/kemp-loadmaster-pki-sync-fns.ts b/backend/src/services/pki-sync/kemp-loadmaster/kemp-loadmaster-pki-sync-fns.tsindex e01bbbac17..8520555498 100644--- a/backend/src/services/pki-sync/kemp-loadmaster/kemp-loadmaster-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/kemp-loadmaster/kemp-loadmaster-pki-sync-fns.ts@@ -397,8 +397,9 @@ export const kempLoadMasterPkiSyncFactory = ({let oldCertificateIdToRemove: string | undefined;if (typeof certificateId === "string") {- const syncRecordLookupId = certificate?.renewedFromCertificateId || certificateId;- const existingSyncRecord = syncRecordsByCertId.get(syncRecordLookupId);+ const existingSyncRecord =+ (certificate?.renewedFromCertificateId && syncRecordsByCertId.get(certificate.renewedFromCertificateId)) ||+ syncRecordsByCertId.get(certificateId);if (certificate?.renewedFromCertificateId && preserveItemOnRenewal && existingSyncRecord?.externalIdentifier) {targetIdentifier = existingSyncRecord.externalIdentifier;diff --git a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.tsindex 0bc5cb1316..6fcc9cb0d3 100644--- a/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/netscaler/netscaler-pki-sync-fns.ts@@ -422,8 +422,10 @@ export const netScalerPkiSyncFactory = ({let oldCertificateIdToRemove: string | undefined;if (typeof certificateId === "string") {- const syncRecordLookupId = certificate?.renewedFromCertificateId || certificateId;- const existingSyncRecord = syncRecordsByCertId.get(syncRecordLookupId);+ const existingSyncRecord =+ (certificate?.renewedFromCertificateId &&+ syncRecordsByCertId.get(certificate.renewedFromCertificateId)) ||+ syncRecordsByCertId.get(certificateId);if (certificate?.renewedFromCertificateId &&