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 PR bot comments
Carlos Monastyrskiaec101e08f36
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.tsindex 7aaaed5e52..0ed3b7f28b 100644--- a/backend/src/ee/services/audit-log/audit-log-types.ts+++ b/backend/src/ee/services/audit-log/audit-log-types.ts@@ -4556,6 +4556,8 @@ interface PkiSyncPreflightCheckEvent {type: EventType.PKI_SYNC_PREFLIGHT_CHECK;metadata: {syncId: string;+ syncName: string;+ destination: string;command: string;ranAt: Date;result?: TPreflightCommandResult;diff --git a/backend/src/services/pki-sync/pki-sync-enums.ts b/backend/src/services/pki-sync/pki-sync-enums.tsindex 37743f3d37..30937322a9 100644--- a/backend/src/services/pki-sync/pki-sync-enums.ts+++ b/backend/src/services/pki-sync/pki-sync-enums.ts@@ -25,3 +25,5 @@ export enum PkiSyncAction {ImportCertificates = "import-certificates",RemoveCertificates = "remove-certificates"}++export const PKI_SYNC_CONNECTION_CONCURRENCY_LIMIT = 3;diff --git a/backend/src/services/pki-sync/pki-sync-host-command-fns.ts b/backend/src/services/pki-sync/pki-sync-host-command-fns.tsindex 8f3d9398a1..173fda038e 100644--- a/backend/src/services/pki-sync/pki-sync-host-command-fns.ts+++ b/backend/src/services/pki-sync/pki-sync-host-command-fns.ts@@ -255,15 +255,17 @@ export const runHostCommand = async (args: {export const hostCommandFailurePrefix = (kind: HostCommandKind): string => `${sentenceCase(kind)} failed`;-export const buildHostCommandFailureMessage = (kind: HostCommandKind, result: THostCommandResult): string => {+export const buildHostCommandFailureMessage = (+ kind: HostCommandKind,+ result: THostCommandResult,+ failurePrefix: string = hostCommandFailurePrefix(kind)+): string => {const detail = result.failureDetail ?? firstNonEmptyLine(result.output);if (result.exitCode === undefined) {const reason = detail ?? result.error;- return reason- ? `${hostCommandFailurePrefix(kind)}: ${truncate(reason, MAX_FAILURE_DETAIL_CHARS)}`- : hostCommandFailurePrefix(kind);+ return reason ? `${failurePrefix}: ${truncate(reason, MAX_FAILURE_DETAIL_CHARS)}` : failurePrefix;}- const prefix = `${hostCommandFailurePrefix(kind)} (exit ${result.exitCode})`;+ const prefix = `${failurePrefix} (exit ${result.exitCode})`;return detail ? `${prefix}: ${truncate(detail, MAX_FAILURE_DETAIL_CHARS)}` : prefix;};diff --git a/backend/src/services/pki-sync/pki-sync-preflight-command-fns.test.ts b/backend/src/services/pki-sync/pki-sync-preflight-command-fns.test.tsindex 4e956739d3..6122054ddc 100644--- a/backend/src/services/pki-sync/pki-sync-preflight-command-fns.test.ts+++ b/backend/src/services/pki-sync/pki-sync-preflight-command-fns.test.ts@@ -15,10 +15,12 @@ import {buildPreflightCommandFailureMessage,buildPreflightCommandPlan,buildPreflightFailureSyncResult,+ buildScheduledPreflightFailureMessage,didPreflightCheckFail,normalizeNewPreflightCommand,PREFLIGHT_COMMAND_TIMEOUT_MS,- runPreflightCommand+ runPreflightCommand,+ SCHEDULED_PREFLIGHT_FAILURE_PREFIX} from "./pki-sync-preflight-command-fns";import { TCertificateMap } from "./pki-sync-types";@@ -262,6 +264,26 @@ describe("buildPreflightCommandPlan with nothing linked to the sync", () => {});});+describe("the scheduled check owns a prefix distinct from a sync run's", () => {+ const failure = {+ status: PkiSyncStatus.Failed as const,+ exitCode: 3,+ durationMs: 12,+ failureDetail: "nginx is down"+ };++ test("a run's blocked preflight does not match the scheduled prefix, so a later check cannot clear it", () => {+ const fromRun = buildPreflightCommandFailureMessage(failure);+ expect(fromRun.startsWith(SCHEDULED_PREFLIGHT_FAILURE_PREFIX)).toBe(false);+ });++ test("the scheduled check's own message matches, so it can take over and clear it", () => {+ const fromSchedule = buildScheduledPreflightFailureMessage(failure);+ expect(fromSchedule.startsWith(SCHEDULED_PREFLIGHT_FAILURE_PREFIX)).toBe(true);+ expect(fromSchedule).toContain("nginx is down");+ });+});+describe("preflight failure messages carry the prefix the DAL matches on", () => {test("every failure shape starts with the prefix, so a check can take over and clear its own status", () => {const prefix = hostCommandFailurePrefix(HostCommandKind.Preflight);diff --git a/backend/src/services/pki-sync/pki-sync-preflight-command-fns.ts b/backend/src/services/pki-sync/pki-sync-preflight-command-fns.tsindex 78442b91c3..199de328f6 100644--- a/backend/src/services/pki-sync/pki-sync-preflight-command-fns.ts+++ b/backend/src/services/pki-sync/pki-sync-preflight-command-fns.ts@@ -102,6 +102,11 @@ export const runPreflightCommand = (args: {export const buildPreflightCommandFailureMessage = (result: TPreflightCommandResult): string =>buildHostCommandFailureMessage(HostCommandKind.Preflight, result);+export const SCHEDULED_PREFLIGHT_FAILURE_PREFIX = "Scheduled preflight check failed";++export const buildScheduledPreflightFailureMessage = (result: TPreflightCommandResult): string =>+ buildHostCommandFailureMessage(HostCommandKind.Preflight, result, SCHEDULED_PREFLIGHT_FAILURE_PREFIX);+export const didPreflightCheckFail = (result: TPreflightCommandResult | undefined): boolean =>result?.status === PkiSyncStatus.Failed;diff --git a/backend/src/services/pki-sync/pki-sync-preflight-queue.ts b/backend/src/services/pki-sync/pki-sync-preflight-queue.tsindex 23f53d028e..975e702c8b 100644--- a/backend/src/services/pki-sync/pki-sync-preflight-queue.ts+++ b/backend/src/services/pki-sync/pki-sync-preflight-queue.ts@@ -21,12 +21,14 @@ import { TCertificateAuthorityDALFactory } from "../certificate-authority/certifimport { buildCertificateMap } from "./pki-sync-certificate-map-fns";import { hydratePkiSyncCredentials } from "./pki-sync-credentials-fns";import { TPkiSyncDALFactory } from "./pki-sync-dal";+import { PKI_SYNC_CONNECTION_CONCURRENCY_LIMIT } from "./pki-sync-enums";import { PkiSyncFns, truncateSyncMessage } from "./pki-sync-fns";-import { commandNeedsCertificateData, hostCommandFailurePrefix, HostCommandKind } from "./pki-sync-host-command-fns";+import { commandNeedsCertificateData } from "./pki-sync-host-command-fns";import {- buildPreflightCommandFailureMessage,+ buildScheduledPreflightFailureMessage,didPreflightCheckFail,getPreflightCommand,+ SCHEDULED_PREFLIGHT_FAILURE_PREFIX,TPreflightCommandResult} from "./pki-sync-preflight-command-fns";import { TCertificateMap, TPkiSyncRaw } from "./pki-sync-types";@@ -43,6 +45,8 @@ const CRON_HANDLER_TIMEOUT_MS = 60 * 1000;const PREFLIGHT_LOCK_TTL_MS = 60 * 1000;+const CONNECTION_SLOT_TTL_S = 5 * 60;+const CONNECTION_LOCK_RETRY = { retryCount: 10, retryDelay: 3_000, retryJitter: 500 };type TPkiSyncPreflightQueueFactoryDep = {@@ -52,7 +56,7 @@ type TPkiSyncPreflightQueueFactoryDep = {TPkiSyncDALFactory,"findPkiSyncsWithPreflightCommand" | "reportPreflightFailure" | "clearReportedPreflightFailure" | "findById">;- keyStore: Pick<TKeyStoreFactory, "acquireLock" | "getItem">;+ keyStore: Pick<TKeyStoreFactory, "acquireLock" | "incrementByAndRefreshExpiryIfUnderLimit" | "decrementByOrDelete">;appConnectionDAL: Pick<TAppConnectionDALFactory, "findById">;projectDAL: TProjectDALFactory;kmsService: Pick<@@ -121,10 +125,8 @@ export const pkiSyncPreflightQueueFactory = ({};const $recordCheckResult = async (pkiSync: TPkiSyncRaw, result: TPreflightCommandResult, isFinalAttempt: boolean) => {- const failurePrefix = hostCommandFailurePrefix(HostCommandKind.Preflight);-if (!didPreflightCheckFail(result)) {- const cleared = await pkiSyncDAL.clearReportedPreflightFailure(pkiSync.id, failurePrefix);+ const cleared = await pkiSyncDAL.clearReportedPreflightFailure(pkiSync.id, SCHEDULED_PREFLIGHT_FAILURE_PREFIX);if (cleared) {logger.info(`cron[${CronJobName.PkiSyncPreflightCheck}]: host recovered, cleared the reported failure [syncId=${pkiSync.id}]`@@ -143,8 +145,8 @@ export const pkiSyncPreflightQueueFactory = ({const reported = await pkiSyncDAL.reportPreflightFailure(pkiSync.id,- truncateSyncMessage(buildPreflightCommandFailureMessage(result)),- failurePrefix+ truncateSyncMessage(buildScheduledPreflightFailureMessage(result)),+ SCHEDULED_PREFLIGHT_FAILURE_PREFIX);if (!reported) {logger.info(@@ -192,15 +194,18 @@ export const pkiSyncPreflightQueueFactory = ({}let lock: Awaited<ReturnType<typeof keyStore.acquireLock>> | null = null;+ let admittedSlot = false;let result;try {- const syncJobsInFlight = Number.parseInt(- (await keyStore.getItem(KeyStorePrefixes.AppConnectionConcurrentJobs(pkiSync.connectionId))) || "0",- 10- );- if (syncJobsInFlight > 0) {+ admittedSlot =+ (await keyStore.incrementByAndRefreshExpiryIfUnderLimit(+ KeyStorePrefixes.AppConnectionConcurrentJobs(pkiSync.connectionId),+ PKI_SYNC_CONNECTION_CONCURRENCY_LIMIT,+ CONNECTION_SLOT_TTL_S+ )) !== -1;+ if (!admittedSlot) {logger.info(- `cron[${CronJobName.PkiSyncPreflightCheck}]: connection is busy syncing, skipping [syncId=${syncId}] [inFlight=${syncJobsInFlight}]`+ `cron[${CronJobName.PkiSyncPreflightCheck}]: connection is at its concurrency limit, skipping [syncId=${syncId}]`);return;}@@ -224,7 +229,13 @@ export const pkiSyncPreflightQueueFactory = ({await $recordCheckResult(pkiSync, result, isFinalAttempt);} finally {- await Promise.allSettled([lock?.release(), connectionLock.release()]);+ await Promise.allSettled([+ admittedSlot+ ? keyStore.decrementByOrDelete(KeyStorePrefixes.AppConnectionConcurrentJobs(pkiSync.connectionId))+ : undefined,+ lock?.release(),+ connectionLock.release()+ ]);}await auditLogService.createAuditLog({@@ -232,7 +243,14 @@ export const pkiSyncPreflightQueueFactory = ({actor: { type: ActorType.PLATFORM, metadata: {} },event: {type: EventType.PKI_SYNC_PREFLIGHT_CHECK,- metadata: { syncId, command, ranAt: new Date(), result }+ metadata: {+ syncId,+ syncName: pkiSync.name,+ destination: pkiSync.destination,+ command,+ ranAt: new Date(),+ result+ }}});};diff --git a/backend/src/services/pki-sync/pki-sync-queue.ts b/backend/src/services/pki-sync/pki-sync-queue.tsindex 6f4b67b1df..a96811c0bd 100644--- a/backend/src/services/pki-sync/pki-sync-queue.ts+++ b/backend/src/services/pki-sync/pki-sync-queue.ts@@ -29,7 +29,7 @@ import { TCertificateSyncDALFactory } from "../certificate-sync/certificate-syncimport { CertificateSyncStatus } from "../certificate-sync/certificate-sync-enums";import { buildCertificateMap } from "./pki-sync-certificate-map-fns";import { TPkiSyncDALFactory } from "./pki-sync-dal";-import { PkiSyncStatus } from "./pki-sync-enums";+import { PKI_SYNC_CONNECTION_CONCURRENCY_LIMIT, PkiSyncStatus } from "./pki-sync-enums";import { PkiSyncError } from "./pki-sync-errors";import { enterprisePkiSyncCheck, parsePkiSyncErrorMessage, PkiSyncFns, truncateSyncMessage } from "./pki-sync-fns";import {@@ -87,8 +87,6 @@ const JITTER_MS = 10 * 1000;const REQUEUE_MS = 30 * 1000;const REQUEUE_LIMIT = 30;-const CONNECTION_CONCURRENCY_LIMIT = 3;-const getRequeueDelay = (failureCount?: number) => {const jitter = Math.random() * JITTER_MS;if (!failureCount) return jitter;@@ -139,7 +137,7 @@ export const pkiSyncQueueFactory = ({if (Number.isNaN(count)) return false;- return count >= CONNECTION_CONCURRENCY_LIMIT;+ return count >= PKI_SYNC_CONNECTION_CONCURRENCY_LIMIT;};const $incrementConnectionConcurrencyCount = async (connectionId: string) => {