Skip to content

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

revert(pki): count link rows for the certificate cap again

bernie-g715d7ff378a0
diff --git a/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.test.ts b/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.test.ts
deleted file mode 100644
index 35fd2b67dd..0000000000
--- a/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.test.ts
+++ /dev/null
@@ -1,52 +0,0 @@
-import { countCertificatesHeldAfterUpdate } from "./pki-sync-filter-reconcile-fns";
-
-const buildDeps = (matchCount: number, linkRows: number) => ({
- certificateDAL: { countCertificatesMatchingSyncFilters: vi.fn().mockResolvedValue(matchCount) },
- certificateSyncDAL: {
- findByPkiSyncId: vi.fn().mockResolvedValue(Array.from({ length: linkRows }, (_, i) => ({ id: `row-${i}` })))
- }
-});
-
-const sync = { id: "sync-1", projectId: "project-1", applicationId: "app-1" };
-
-describe("countCertificatesHeldAfterUpdate", () => {
- test("counts what the filters match, not the link rows left behind", async () => {
- const deps = buildDeps(1, 3);
-
- await expect(
- countCertificatesHeldAfterUpdate(sync, { certificateOrderIds: ["order-1"] }, deps as never)
- ).resolves.toBe(1);
-
- expect(deps.certificateSyncDAL.findByPkiSyncId).not.toHaveBeenCalled();
- expect(deps.certificateDAL.countCertificatesMatchingSyncFilters).toHaveBeenCalledWith(
- { certificateOrderIds: ["order-1"] },
- { projectId: "project-1", applicationId: "app-1" }
- );
- });
-
- test("counts the link rows when the sync has no filters", async () => {
- const deps = buildDeps(1, 3);
-
- await expect(countCertificatesHeldAfterUpdate(sync, null, deps as never)).resolves.toBe(3);
- expect(deps.certificateDAL.countCertificatesMatchingSyncFilters).not.toHaveBeenCalled();
- });
-
- test("counts the link rows for a sync with no application, such as a subscriber sync", async () => {
- const deps = buildDeps(1, 2);
-
- await expect(
- countCertificatesHeldAfterUpdate(
- { ...sync, applicationId: null },
- { certificateOrderIds: ["order-1"] },
- deps as never
- )
- ).resolves.toBe(2);
- expect(deps.certificateDAL.countCertificatesMatchingSyncFilters).not.toHaveBeenCalled();
- });
-
- test("reports the larger count when the filters match more than is linked", async () => {
- const deps = buildDeps(5, 1);
-
- await expect(countCertificatesHeldAfterUpdate(sync, { profileIds: ["profile-1"] }, deps as never)).resolves.toBe(5);
- });
-});
diff --git a/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.ts b/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.ts
index 669d857593..77d1978335 100644
--- a/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.ts
+++ b/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.ts
@@ -177,29 +177,6 @@ export const computePkiSyncCertificateDiff = async (
return { matched, toLink, toUnlink };
};
-// What the sync will hold after the edit. A filtered sync holds whatever its filters match; link
-// rows also carry a certificate on its way out and whatever a failed removal left behind.
-export const countCertificatesHeldAfterUpdate = async (
- pkiSync: { id: string; projectId: string; applicationId?: string | null },
- filters: TPkiSyncFilters | null | undefined,
- {
- certificateDAL,
- certificateSyncDAL
- }: {
- certificateDAL: Pick<TCertificateDALFactory, "countCertificatesMatchingSyncFilters">;
- certificateSyncDAL: Pick<TCertificateSyncDALFactory, "findByPkiSyncId">;
- }
-): Promise<number> => {
- if (hasAnyPkiSyncFilter(filters) && pkiSync.applicationId) {
- return certificateDAL.countCertificatesMatchingSyncFilters(filters, {
- projectId: pkiSync.projectId,
- applicationId: pkiSync.applicationId
- });
- }
-
- return (await certificateSyncDAL.findByPkiSyncId(pkiSync.id)).length;
-};
-
export const assertPkiSyncCanHoldMatchedCertificates = (pkiSync: TPkiSyncReconcileTarget, matchedCount: number) =>
assertPkiSyncCanHoldCertificateCount(
pkiSync.destination as PkiSync,
diff --git a/backend/src/services/pki-sync/pki-sync-service.ts b/backend/src/services/pki-sync/pki-sync-service.ts
index 923f46c0cd..f092a5387c 100644
--- a/backend/src/services/pki-sync/pki-sync-service.ts
+++ b/backend/src/services/pki-sync/pki-sync-service.ts
@@ -50,7 +50,6 @@ import {
applyPkiSyncCertificateDiff,
assertPkiSyncCanHoldMatchedCertificates,
assertPkiSyncUnlinkAllowed,
- countCertificatesHeldAfterUpdate,
computePkiSyncCertificateDiff,
findPkiSyncUnlinkIdsAtDestination,
TPkiSyncReconcileTarget,
@@ -985,24 +984,18 @@ export const pkiSyncServiceFactory = ({
});
if (syncOptions || destinationConfig) {
- const effectiveFilters = filters === undefined ? (pkiSync.filters as TPkiSyncFilters | null) : filters;
-
- const heldCount = await countCertificatesHeldAfterUpdate(pkiSync, effectiveFilters, {
- certificateDAL,
- certificateSyncDAL
- });
-
+ const existingCount = (await certificateSyncDAL.findByPkiSyncId(id)).length;
assertPkiSyncCertificateCapsAllowCount(
pkiSync.destination,
effectiveSyncOptions,
effectiveDestinationConfig,
- heldCount
+ existingCount
);
assertFiltersCannotExceedCertificateCap(
pkiSync.destination,
effectiveSyncOptions,
effectiveDestinationConfig,
- effectiveFilters
+ filters === undefined ? (pkiSync.filters as TPkiSyncFilters | null) : filters
);
}