Skip to content

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

fix(pki-sync): skip destination resources owned by other syncs during pattern cleanup

bernie-g736dc6a6cbc9
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.ts
index 23ea52d169..d9b513a6d8 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
@@ -52,11 +52,10 @@ const isInfisicalManagedCertificate = (secretName: string, pkiSync: TPkiSyncWith
const syncOptions = pkiSync.syncOptions as { certificateNameSchema?: string } | undefined;
const certificateNameSchema = syncOptions?.certificateNameSchema;
- if (certificateNameSchema) {
- return matchesCertificateNameSchema(secretName, certificateNameSchema);
- }
-
- return secretName.startsWith(AWS_SECRETS_MANAGER_PKI_SYNC_DEFAULTS.INFISICAL_PREFIX);
+ return matchesCertificateNameSchema(
+ secretName,
+ certificateNameSchema || AWS_SECRETS_MANAGER_PKI_SYNC_DEFAULTS.DEFAULT_CERTIFICATE_NAME_SCHEMA
+ );
};
const parseErrorMessage = (error: unknown): string => {
@@ -110,6 +109,7 @@ type TAwsSecretsManagerPkiSyncFactoryDeps = {
| "updateById"
| "findByPkiSyncId"
| "updateSyncStatus"
+ | "findExternalIdentifiersInUse"
>;
};
@@ -441,12 +441,16 @@ export const awsSecretsManagerPkiSyncFactory = ({
);
const allowPatternCleanup = !certificateNameSchemaHasFreeTextPlaceholder(syncOptions?.certificateNameSchema);
- for (const [secretName] of Object.entries(existingSecrets)) {
- if (!activeExternalIdentifiers.has(secretName)) {
- if (!allowPatternCleanup && !trackedExternalIds.has(secretName)) {
- // eslint-disable-next-line no-continue
- continue;
- }
+ const orphanedSecretNames = Object.keys(existingSecrets).filter(
+ (secretName) => !activeExternalIdentifiers.has(secretName)
+ );
+ const untrackedSecretNames = allowPatternCleanup
+ ? orphanedSecretNames.filter((secretName) => !trackedExternalIds.has(secretName))
+ : [];
+ const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse(untrackedSecretNames, pkiSync.id);
+
+ for (const secretName of orphanedSecretNames) {
+ if (trackedExternalIds.has(secretName) || (allowPatternCleanup && !ownedByOtherSync.has(secretName))) {
try {
await withRateLimitRetry(
() =>
diff --git a/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts b/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts
index 28e1438c98..76666b5a03 100644
--- a/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts
+++ b/backend/src/services/pki-sync/azure-key-vault/azure-key-vault-pki-sync-fns.ts
@@ -61,6 +61,7 @@ type TAzureKeyVaultPkiSyncFactoryDeps = {
| "updateById"
| "findByPkiSyncId"
| "updateSyncStatus"
+ | "findExternalIdentifiersInUse"
>;
certificateDAL: Pick<TCertificateDALFactory, "findById">;
};
@@ -449,21 +450,17 @@ export const azureKeyVaultPkiSyncFactory = ({
});
if (!certificateNameSchemaHasFreeTextPlaceholder(syncOptions?.certificateNameSchema)) {
- Object.keys(vaultCertificates).forEach((certificateName) => {
- const isInfisicalManaged = isInfisicalManagedCertificate(certificateName, pkiSync);
-
- if (isInfisicalManaged) {
- const isTrackedInSyncRecords = existingSyncRecords.some(
- (record) => record.externalIdentifier === certificateName
- );
-
- const isInActiveSet = activeExternalIdentifiers.has(certificateName);
-
- if (!isTrackedInSyncRecords && !isInActiveSet && !certificatesToRemove.includes(certificateName)) {
- certificatesToRemove.push(certificateName);
- }
- }
- });
+ const patternCandidates = Object.keys(vaultCertificates).filter(
+ (certificateName) =>
+ isInfisicalManagedCertificate(certificateName, pkiSync) &&
+ !existingSyncRecords.some((record) => record.externalIdentifier === certificateName) &&
+ !activeExternalIdentifiers.has(certificateName) &&
+ !certificatesToRemove.includes(certificateName)
+ );
+ const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse(patternCandidates, pkiSync.id);
+ certificatesToRemove.push(
+ ...patternCandidates.filter((certificateName) => !ownedByOtherSync.has(certificateName))
+ );
}
}
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.ts
index ac136c6958..e6654abbdd 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
@@ -73,6 +73,7 @@ type TChefPkiSyncFactoryDeps = {
| "updateById"
| "findByPkiSyncId"
| "updateSyncStatus"
+ | "findExternalIdentifiersInUse"
>;
gatewayV2Service?: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
gatewayPoolService?: Pick<TGatewayPoolServiceFactory, "resolveEffectiveGatewayId">;
@@ -382,14 +383,19 @@ export const chefPkiSyncFactory = ({
);
const allowPatternCleanup = !certificateNameSchemaHasFreeTextPlaceholder(syncOptions?.certificateNameSchema);
+ const patternCandidates: string[] = [];
Object.keys(chefDataBagItems).forEach((itemName) => {
if (activeExternalIdentifiers.has(itemName)) return;
- const isTracked = trackedExternalIds.has(itemName);
- if (isTracked || (allowPatternCleanup && isInfisicalManagedCertificate(itemName, pkiSync))) {
+ if (trackedExternalIds.has(itemName)) {
itemsToRemove.push(itemName);
+ } else if (allowPatternCleanup && isInfisicalManagedCertificate(itemName, pkiSync)) {
+ patternCandidates.push(itemName);
}
});
+ const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse(patternCandidates, pkiSync.id);
+ itemsToRemove.push(...patternCandidates.filter((itemName) => !ownedByOtherSync.has(itemName)));
+
if (itemsToRemove.length > 0) {
const removalPromises = itemsToRemove.map(async (itemName) => {
try {
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.ts
index 45c2e8807f..b2117d477e 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
@@ -46,6 +46,7 @@ type TKempLoadMasterPkiSyncFactoryDeps = {
| "updateById"
| "findByPkiSyncId"
| "updateSyncStatus"
+ | "findExternalIdentifiersInUse"
>;
certificateDAL: Pick<TCertificateDALFactory, "findById">;
gatewayV2Service?: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
@@ -631,12 +632,16 @@ export const kempLoadMasterPkiSyncFactory = ({
// this run so a failed upload is not treated as an orphan.
if (!certificateNameSchemaHasFreeTextPlaceholder(certificateNameSchema)) {
const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
- for (const certName of existingCertNames) {
- if (
+ const patternCandidates = [...existingCertNames].filter(
+ (certName) =>
managedCertNamePattern.test(certName) &&
!activeIdentifiers.has(certName) &&
- !attemptedIdentifiers.has(certName)
- ) {
+ !attemptedIdentifiers.has(certName) &&
+ !identifiersToRemove.has(certName)
+ );
+ const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse(patternCandidates, pkiSync.id);
+ for (const certName of patternCandidates) {
+ if (!ownedByOtherSync.has(certName)) {
identifiersToRemove.add(certName);
}
}
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.ts
index bb06c1ceec..38a274c98e 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
@@ -45,6 +45,7 @@ type TNetScalerPkiSyncFactoryDeps = {
| "updateById"
| "findByPkiSyncId"
| "updateSyncStatus"
+ | "findExternalIdentifiersInUse"
>;
certificateDAL: Pick<TCertificateDALFactory, "findById">;
gatewayV2Service?: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">;
@@ -583,8 +584,18 @@ export const netScalerPkiSyncFactory = ({
if (!certificateNameSchemaHasFreeTextPlaceholder(certificateNameSchema)) {
const managedCertNamePattern = buildManagedCertNamePattern(certificateNameSchema);
- for (const certKeyName of existingCertKeyNames) {
- if (managedCertNamePattern.test(certKeyName) && !activeExternalIdentifiers.has(certKeyName)) {
+ const patternCandidates = [...existingCertKeyNames].filter(
+ (certKeyName) =>
+ managedCertNamePattern.test(certKeyName) &&
+ !activeExternalIdentifiers.has(certKeyName) &&
+ !certKeysToRemove.has(certKeyName)
+ );
+ const ownedByOtherSync = await certificateSyncDAL.findExternalIdentifiersInUse(
+ patternCandidates,
+ pkiSync.id
+ );
+ for (const certKeyName of patternCandidates) {
+ if (!ownedByOtherSync.has(certKeyName)) {
certKeysToRemove.add(certKeyName);
}
}