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
Address greptile comments
Carlos Monastyrskida60a3400d61
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.tsindex a2c35a9022..6bd8c0c4c1 100644--- a/backend/src/ee/services/audit-log/audit-log-types.ts+++ b/backend/src/ee/services/audit-log/audit-log-types.ts@@ -4711,6 +4711,7 @@ interface PkiSyncLinkCertificatesEvent {certificates: { id: string; commonName: string }[];filters?: TPkiSyncFilters;applicationId?: string;+ applicationName?: string;};}@@ -4723,6 +4724,7 @@ interface PkiSyncUnlinkCertificatesEvent {certificates: { id: string; commonName: string }[];removedFromDestination?: boolean;applicationId?: string;+ applicationName?: string;};}@@ -4735,6 +4737,7 @@ interface PkiSyncSkipCertificateEvent {commonName: string;reason: string;applicationId?: string;+ applicationName?: string;};}diff --git a/backend/src/services/certificate-sync/certificate-sync-dal.ts b/backend/src/services/certificate-sync/certificate-sync-dal.tsindex ba8b9d13dd..bbf185002f 100644--- a/backend/src/services/certificate-sync/certificate-sync-dal.ts+++ b/backend/src/services/certificate-sync/certificate-sync-dal.ts@@ -50,7 +50,8 @@ export const certificateSyncDALFactory = (db: TDbClient) => {};const findIneligibleFilteredLinks = async (- limit: number+ limit: number,+ offset = 0): Promise<Array<{ certificateId: string; applicationId: string }>> => {try {const docs = (await db@@ -68,7 +69,8 @@ export const certificateSyncDALFactory = (db: TDbClient) => {.select(`${TableName.CertificateSync}.certificateId`, `${TableName.PkiSync}.applicationId`).groupBy(`${TableName.CertificateSync}.certificateId`, `${TableName.PkiSync}.applicationId`).orderByRaw(`min(??) asc`, [`${TableName.CertificateSync}.updatedAt`])- .limit(limit)) as Array<{ certificateId: string; applicationId: string }>;+ .limit(limit)+ .offset(offset)) as Array<{ certificateId: string; applicationId: string }>;return docs;} catch (error) {diff --git a/backend/src/services/certificate/certificate-service.ts b/backend/src/services/certificate/certificate-service.tsindex 9e92aec52c..80b61c9f0f 100644--- a/backend/src/services/certificate/certificate-service.ts+++ b/backend/src/services/certificate/certificate-service.ts@@ -531,7 +531,7 @@ export const certificateServiceFactory = ({await triggerSyncsForDeletedCertificate(cert.id,pkiSyncIdsHoldingCertificate,- { pkiSyncDAL, pkiSyncQueue, auditLogService },+ { pkiSyncDAL, pkiSyncQueue, auditLogService, pkiApplicationDAL },{ commonName: cert.commonName, projectId: cert.projectId, applicationId: cert.applicationId },auditLogInfo);diff --git a/backend/src/services/pki-sync/aws-certificate-manager/aws-certificate-manager-pki-sync-fns.ts b/backend/src/services/pki-sync/aws-certificate-manager/aws-certificate-manager-pki-sync-fns.tsindex d471354d39..35dad292e6 100644--- a/backend/src/services/pki-sync/aws-certificate-manager/aws-certificate-manager-pki-sync-fns.ts+++ b/backend/src/services/pki-sync/aws-certificate-manager/aws-certificate-manager-pki-sync-fns.ts@@ -549,6 +549,43 @@ export const awsCertificateManagerPkiSyncFactory = ({}}+ const untaggedTrackedArns = existingSyncRecords+ .map((syncRecord) => syncRecord.externalIdentifier)+ .filter((arn): arn is string => {+ if (!arn) return false;++ const acmCert = acmCertificatesByArn.get(arn);+ return Boolean(+ acmCert?.Tags && !acmCert.Tags.some((tag) => tag.Key === INFISICAL_PKI_SYNC_TAG && tag.Value === pkiSync.id)+ );+ });++ if (untaggedTrackedArns.length > 0) {+ await executeWithConcurrencyLimit(+ untaggedTrackedArns,+ async (certificateArn) => {+ try {+ await withRateLimitRetry(+ () =>+ acm.send(+ new AddTagsToCertificateCommand({+ CertificateArn: certificateArn,+ Tags: [{ Key: INFISICAL_PKI_SYNC_TAG, Value: pkiSync.id }]+ })+ ),+ { operation: "tag-existing-certificate", syncId: pkiSync.id }+ );+ } catch (error) {+ logger.warn(+ error,+ `Could not tag an existing AWS Certificate Manager certificate with its sync [syncId=${pkiSync.id}]`+ );+ }+ },+ { operation: "tag-existing-certificates", syncId: pkiSync.id }+ );+ }+const certificatesToRemove: string[] = [];if (canRemoveCertificates) {diff --git a/backend/src/services/pki-sync/pki-sync-cleanup-queue.ts b/backend/src/services/pki-sync/pki-sync-cleanup-queue.tsindex 8ad17d1ef2..20e484e5b1 100644--- a/backend/src/services/pki-sync/pki-sync-cleanup-queue.ts+++ b/backend/src/services/pki-sync/pki-sync-cleanup-queue.ts@@ -7,6 +7,7 @@ import { TPkiSyncDALFactory } from "./pki-sync-dal";import { TPkiSyncQueueFactory } from "./pki-sync-queue";const INELIGIBLE_LINK_BATCH = 500;+const INELIGIBLE_LINK_MAX_BATCHES_PER_RUN = 10;type TPkiSyncCleanupQueueServiceFactoryDep = {cronJob: TCronJobFactory;@@ -60,20 +61,33 @@ export const pkiSyncCleanupQueueServiceFactory = ({const reconcileIneligibleFilteredLinks = async () => {try {- const stale = await certificateSyncDAL.findIneligibleFilteredLinks(INELIGIBLE_LINK_BATCH);+ let queued = 0;++ for (let page = 0; page < INELIGIBLE_LINK_MAX_BATCHES_PER_RUN; page += 1) {+ // eslint-disable-next-line no-await-in-loop+ const stale = await certificateSyncDAL.findIneligibleFilteredLinks(+ INELIGIBLE_LINK_BATCH,+ page * INELIGIBLE_LINK_BATCH+ );++ if (stale.length === 0) break;++ for (const { certificateId, applicationId } of stale) {+ try {+ // eslint-disable-next-line no-await-in-loop+ await pkiSyncQueue.queuePkiSyncLinkMatchingCertificates({ certificateId, applicationId });+ queued += 1;+ } catch (error) {+ logger.error(error, `Failed to queue a filter reconcile [certificateId=${certificateId}]`);+ }+ }- if (stale.length === 0) return;+ if (stale.length < INELIGIBLE_LINK_BATCH) break;+ }- logger.info(`cron[pki-sync-cleanup]: re-evaluating ${stale.length} certificate(s) held by a filtered sync`);+ if (queued === 0) return;- for (const { certificateId, applicationId } of stale) {- try {- // eslint-disable-next-line no-await-in-loop- await pkiSyncQueue.queuePkiSyncLinkMatchingCertificates({ certificateId, applicationId });- } catch (error) {- logger.error(error, `Failed to queue a filter reconcile [certificateId=${certificateId}]`);- }- }+ logger.info(`cron[pki-sync-cleanup]: re-evaluating ${queued} certificate(s) held by a filtered sync`);} catch (error) {logger.error(error, "Failed to re-evaluate certificates held by a filtered sync");}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 ebde7bd943..badc70b5f5 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@@ -8,6 +8,7 @@ import { ActorType } from "@app/services/auth/auth-type";import { TCertificateDALFactory } from "../certificate/certificate-dal";import { TCertificateSyncDALFactory } from "../certificate-sync/certificate-sync-dal";+import { TPkiApplicationDALFactory } from "../pki-application/pki-application-dal";import { PkiSync } from "./pki-sync-enums";import { hasAnyPkiSyncFilter } from "./pki-sync-filter-fns";import { assertPkiSyncCanHoldCertificateCount } from "./pki-sync-fns";@@ -46,6 +47,7 @@ type TPkiSyncFilterReconcileDeps = {queuePkiSyncRemoveCertificatesById: (payload: TQueuePkiSyncRemoveCertificatesByIdDTO) => Promise<unknown>;};auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;+ pkiApplicationDAL: Pick<TPkiApplicationDALFactory, "findById">;};const PKI_SYNC_FILTER_LOCK_TTL_MS = 60 * 1000;@@ -84,6 +86,16 @@ const getPkiSyncLinkDestinationTargets = (record: {return [...(record.externalIdentifier ? [record.externalIdentifier] : []), ...files];};+export const describePkiSyncApplication = async (+ applicationId: string | null | undefined,+ pkiApplicationDAL: Pick<TPkiApplicationDALFactory, "findById">+): Promise<{ applicationId?: string; applicationName?: string }> => {+ if (!applicationId) return {};++ const application = await pkiApplicationDAL.findById(applicationId);+ return { applicationId, ...(application?.name ? { applicationName: application.name } : {}) };+};+export const computePkiSyncCertificateDiff = async (pkiSync: TPkiSyncReconcileTarget,filters: TPkiSyncFilters | null | undefined,@@ -132,7 +144,7 @@ export const applyPkiSyncCertificateDiff = async (auditLogInfo?: AuditLogInfo,writeFilters?: (tx: Knex) => Promise<void>): Promise<{ linked: TPkiSyncCertificateRef[]; unlinked: TPkiSyncCertificateRef[] }> => {- const { certificateSyncDAL, pkiSyncQueue, auditLogService } = deps;+ const { certificateSyncDAL, pkiSyncQueue, auditLogService, pkiApplicationDAL } = deps;const { toLink, toUnlink } = diff;const unlinkIds = new Set(toUnlink.map((certificate) => certificate.id));@@ -181,6 +193,7 @@ export const applyPkiSyncCertificateDiff = async (if (toLink.length === 0 && toUnlink.length === 0) return { linked: [], unlinked: [] };const auditActor: AuditLogInfo = auditLogInfo ?? { actor: { type: ActorType.PLATFORM, metadata: {} } };+ const applicationLabel = await describePkiSyncApplication(pkiSync.applicationId, pkiApplicationDAL);if (toLink.length > 0) {await auditLogService.createAuditLog({@@ -193,7 +206,7 @@ export const applyPkiSyncCertificateDiff = async (name: pkiSync.name,count: toLink.length,certificates: toLink.map(({ id, commonName }) => ({ id, commonName })),- ...(pkiSync.applicationId ? { applicationId: pkiSync.applicationId } : {})+ ...applicationLabel}}});@@ -218,7 +231,7 @@ export const applyPkiSyncCertificateDiff = async (count: toUnlink.length,certificates: toUnlink,removedFromDestination: idsAtDestination.length > 0,- ...(pkiSync.applicationId ? { applicationId: pkiSync.applicationId } : {})+ ...applicationLabel}}});diff --git a/backend/src/services/pki-sync/pki-sync-queue.ts b/backend/src/services/pki-sync/pki-sync-queue.tsindex 1817ff9413..d563065797 100644--- a/backend/src/services/pki-sync/pki-sync-queue.ts+++ b/backend/src/services/pki-sync/pki-sync-queue.ts@@ -772,6 +772,7 @@ export const pkiSyncQueueFactory = ({pkiSyncDAL,pkiSyncQueue: { queuePkiSyncSyncCertificatesById, queuePkiSyncRemoveCertificatesById },auditLogService,+ pkiApplicationDAL,withSyncFilterLock: (syncId, run) => withPkiSyncFilterLock(keyStore, syncId, run)});} catch (error) {diff --git a/backend/src/services/pki-sync/pki-sync-service.ts b/backend/src/services/pki-sync/pki-sync-service.tsindex 640c785829..1999e16001 100644--- a/backend/src/services/pki-sync/pki-sync-service.ts+++ b/backend/src/services/pki-sync/pki-sync-service.ts@@ -503,7 +503,7 @@ export const pkiSyncServiceFactory = ({return applyPkiSyncCertificateDiff(pkiSync,diff,- { certificateDAL, certificateSyncDAL, pkiSyncQueue, auditLogService },+ { certificateDAL, certificateSyncDAL, pkiSyncQueue, auditLogService, pkiApplicationDAL },opts.auditLogInfo,opts.writeFilters);@@ -648,7 +648,7 @@ export const pkiSyncServiceFactory = ({});- if (pkiSync.isAutoSyncEnabled) {+ if (pkiSync.isAutoSyncEnabled && linkedCertificates.length === 0) {await pkiSyncQueue.queuePkiSyncSyncCertificatesById({ syncId: pkiSync.id });}diff --git a/backend/src/services/pki-sync/pki-sync-utils.ts b/backend/src/services/pki-sync/pki-sync-utils.tsindex 376e58433a..fe7acc7447 100644--- a/backend/src/services/pki-sync/pki-sync-utils.ts+++ b/backend/src/services/pki-sync/pki-sync-utils.ts@@ -6,10 +6,15 @@ import { ActorType } from "@app/services/auth/auth-type";import { TCertificateDALFactory } from "../certificate/certificate-dal";import { TCertificateSyncDALFactory } from "../certificate-sync/certificate-sync-dal";+import { TPkiApplicationDALFactory } from "../pki-application/pki-application-dal";import { TPkiSyncDALFactory } from "./pki-sync-dal";import { PkiSync } from "./pki-sync-enums";import { hasAnyPkiSyncFilter } from "./pki-sync-filter-fns";-import { applyPkiSyncCertificateDiff, TPkiSyncReconcileTarget } from "./pki-sync-filter-reconcile-fns";+import {+ applyPkiSyncCertificateDiff,+ describePkiSyncApplication,+ TPkiSyncReconcileTarget+} from "./pki-sync-filter-reconcile-fns";import { assertPkiSyncCanHoldCertificateCount } from "./pki-sync-fns";import {TPkiSyncFilters,@@ -118,6 +123,7 @@ export const triggerSyncsForDeletedCertificate = async (pkiSyncDAL: Pick<TPkiSyncDALFactory, "find">;pkiSyncQueue: TPkiSyncRunQueue;auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;+ pkiApplicationDAL: Pick<TPkiApplicationDALFactory, "findById">;},certificate: { commonName: string; projectId: string; applicationId?: string | null },auditLogInfo?: AuditLogInfo@@ -126,6 +132,10 @@ export const triggerSyncsForDeletedCertificate = async (try {const pkiSyncs = await dependencies.pkiSyncDAL.find({ $in: { id: pkiSyncIds } });+ const applicationLabel = await describePkiSyncApplication(+ certificate.applicationId,+ dependencies.pkiApplicationDAL+ );await Promise.all(pkiSyncs.map((pkiSync) =>@@ -142,7 +152,7 @@ export const triggerSyncsForDeletedCertificate = async (removedFromDestination: Boolean((pkiSync.syncOptions as { canRemoveCertificates?: boolean } | null)?.canRemoveCertificates),- ...(pkiSync.applicationId ? { applicationId: pkiSync.applicationId } : {})+ ...applicationLabel}}})@@ -229,6 +239,7 @@ type TReconcileCertificateAgainstSyncsDeps = {queuePkiSyncRemoveCertificatesById: (payload: TQueuePkiSyncRemoveCertificatesByIdDTO) => Promise<unknown>;};auditLogService: Pick<TAuditLogServiceFactory, "createAuditLog">;+ pkiApplicationDAL: Pick<TPkiApplicationDALFactory, "findById">;withSyncFilterLock: <T>(syncId: string, run: () => Promise<T>) => Promise<T>;};@@ -308,7 +319,7 @@ const $reconcileCertificateAgainstSync = async (certificateId,commonName: matched[0].commonName,reason: limitError instanceof Error ? limitError.message : "This sync cannot hold another certificate.",- ...(pkiSync.applicationId ? { applicationId: pkiSync.applicationId } : {})+ ...(await describePkiSyncApplication(pkiSync.applicationId, dependencies.pkiApplicationDAL))}}});diff --git a/frontend/src/components/pki-syncs/CertificateManagementModal.tsx b/frontend/src/components/pki-syncs/CertificateManagementModal.tsxindex 4b1ed95fd5..7657468481 100644--- a/frontend/src/components/pki-syncs/CertificateManagementModal.tsx+++ b/frontend/src/components/pki-syncs/CertificateManagementModal.tsx@@ -101,11 +101,18 @@ export const CertificateManagementModal = ({const totalPages = Math.ceil(totalCount / pageSize);const [selectedIds, setSelectedIds] = useState<string[]>([]);+ const orderNamesSeen = React.useRef(new Map<string, string>());React.useEffect(() => {setSelectedIds(preselectedOrderIds);}, [JSON.stringify(preselectedOrderIds)]);+ React.useEffect(() => {+ allCertificates.forEach((cert) => {+ if (cert.orderId) orderNamesSeen.current.set(cert.orderId, cert.commonName);+ });+ }, [allCertificates]);+const handleToggleSelection = (orderId: string) => {setSelectedIds((prev) => {if (prev.includes(orderId)) {@@ -165,9 +172,11 @@ export const CertificateManagementModal = ({}, [isOpen]);const handleSaveCertificates = () => {- const orderNames = allCertificates- .filter((cert) => cert.orderId && selectedIds.includes(cert.orderId))- .map((cert) => [cert.orderId as string, cert.commonName] as [string, string]);+ const orderNames = selectedIds+ .filter((orderId) => orderNamesSeen.current.has(orderId))+ .map(+ (orderId) => [orderId, orderNamesSeen.current.get(orderId) as string] as [string, string]+ );onOrderSelectionChange?.(selectedIds, orderNames);onClose();