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
test(pki): cover the certificate cap count on sync update
bernie-g9b81ef4326ef
diff --git a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.tsindex df1995019d..9636f37671 100644--- a/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/imperva/imperva-pki-sync-fns.ts@@ -16,10 +16,10 @@ import {withImpervaOperation} from "@app/services/app-connection/imperva/imperva-connection-fns";import { TImpervaConnection } from "@app/services/app-connection/imperva/imperva-connection-types";-import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";-import { splitPemChain } from "@app/services/certificate/certificate-fns";import { TCertificateSyncDALFactory } from "@app/services/certificate-sync/certificate-sync-dal";import { CertificateSyncStatus } from "@app/services/certificate-sync/certificate-sync-enums";+import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";+import { splitPemChain } from "@app/services/certificate/certificate-fns";import { TCertificateMap } from "@app/services/pki-sync/pki-sync-types";import { PkiSyncError } from "../pki-sync-errors";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.tsnew file mode 100644index 0000000000..35fd2b67dd--- /dev/null+++ b/backend/src/services/pki-sync/pki-sync-filter-reconcile-fns.test.ts@@ -0,0 +1,52 @@+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.tsindex 77d1978335..669d857593 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,6 +177,29 @@ 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.tsindex 5fd571c3fa..923f46c0cd 100644--- a/backend/src/services/pki-sync/pki-sync-service.ts+++ b/backend/src/services/pki-sync/pki-sync-service.ts@@ -50,6 +50,7 @@ import {applyPkiSyncCertificateDiff,assertPkiSyncCanHoldMatchedCertificates,assertPkiSyncUnlinkAllowed,+ countCertificatesHeldAfterUpdate,computePkiSyncCertificateDiff,findPkiSyncUnlinkIdsAtDestination,TPkiSyncReconcileTarget,@@ -986,15 +987,10 @@ export const pkiSyncServiceFactory = ({if (syncOptions || destinationConfig) {const effectiveFilters = filters === undefined ? (pkiSync.filters as TPkiSyncFilters | null) : filters;- // Link rows also carry a certificate on its way out and whatever a failed removal left, and- // counting those blocks the edit that fixes them.- const heldCount =- hasAnyPkiSyncFilter(effectiveFilters) && pkiSync.applicationId- ? await certificateDAL.countCertificatesMatchingSyncFilters(effectiveFilters, {- projectId: pkiSync.projectId,- applicationId: pkiSync.applicationId- })- : (await certificateSyncDAL.findByPkiSyncId(id)).length;+ const heldCount = await countCertificatesHeldAfterUpdate(pkiSync, effectiveFilters, {+ certificateDAL,+ certificateSyncDAL+ });assertPkiSyncCertificateCapsAllowCount(pkiSync.destination,diff --git a/frontend/src/components/pki-syncs/forms/ImpervaPkiSyncFields.tsx b/frontend/src/components/pki-syncs/forms/ImpervaPkiSyncFields.tsxindex 416534a6f4..e0af5ddebc 100644--- a/frontend/src/components/pki-syncs/forms/ImpervaPkiSyncFields.tsx+++ b/frontend/src/components/pki-syncs/forms/ImpervaPkiSyncFields.tsx@@ -14,8 +14,8 @@ import {import { PkiSync } from "@app/hooks/api/pkiSyncs";import { useListImpervaSites } from "@app/hooks/api/pkiSyncs/queries";-import { TPkiSyncForm } from "./schemas/pki-sync-schema";import { PkiSyncConnectionField } from "./PkiSyncConnectionField";+import { TPkiSyncForm } from "./schemas/pki-sync-schema";type TImpervaForm = TPkiSyncForm & { destination: PkiSync.Imperva };