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
fix retry
Thiago Araujo da Silva7da32bd8fab8
diff --git a/backend/src/ee/services/audit-log/audit-log-queue.test.ts b/backend/src/ee/services/audit-log/audit-log-queue.test.tsindex f2e41e70e8..9920c39e68 100644--- a/backend/src/ee/services/audit-log/audit-log-queue.test.ts+++ b/backend/src/ee/services/audit-log/audit-log-queue.test.ts@@ -242,6 +242,13 @@ describe("audit-log-queue pushToLog", () => {await expect(service.pushToLog(dto({ orgId: "o" }) as never)).resolves.toBeUndefined();expect(keyStore.streamAdd).not.toHaveBeenCalled();});++ test("pushToLogOrThrow rejects when streamAdd fails", async () => {+ const { service, keyStore } = await createHarness();+ keyStore.streamAdd.mockRejectedValueOnce(new Error("redis down"));++ await expect(service.pushToLogOrThrow(dto({ orgId: "o" }) as never)).rejects.toThrow("redis down");+ });});describe("audit-log-queue event class settings", () => {diff --git a/backend/src/ee/services/audit-log/audit-log-queue.ts b/backend/src/ee/services/audit-log/audit-log-queue.tsindex 43978b2b0e..a1e7fa66e0 100644--- a/backend/src/ee/services/audit-log/audit-log-queue.ts+++ b/backend/src/ee/services/audit-log/audit-log-queue.ts@@ -35,6 +35,7 @@ type TAuditLogQueueServiceFactoryDep = {export type TAuditLogQueueServiceFactory = {pushToLog: (data: TCreateAuditLogDTO) => Promise<void>;+ pushToLogOrThrow: (data: TCreateAuditLogDTO) => Promise<void>;};const normalizeJsonPayload = (payload: unknown) => {@@ -354,6 +355,7 @@ export const auditLogQueueServiceFactory = async ({}return {- pushToLog+ pushToLog,+ pushToLogOrThrow: appendToIngestStream};};diff --git a/backend/src/ee/services/audit-log/audit-log-service.test.ts b/backend/src/ee/services/audit-log/audit-log-service.test.tsindex 08a1861c7b..544ce5b8e2 100644--- a/backend/src/ee/services/audit-log/audit-log-service.test.ts+++ b/backend/src/ee/services/audit-log/audit-log-service.test.ts@@ -55,7 +55,10 @@ const createHarness = ({ windowAcquired = true, storedCount = "0", retentionDaysincrementByWithExpiry: vi.fn<(key: string, by: number, ttl: number) => Promise<number>>(async () => 1),deleteItem: vi.fn<(key: string) => Promise<number>>(async () => 1)};- const auditLogQueue = { pushToLog: vi.fn<(data: TPushedLog) => Promise<void>>(async () => undefined) };+ const auditLogQueue = {+ pushToLog: vi.fn<(data: TPushedLog) => Promise<void>>(async () => undefined),+ pushToLogOrThrow: vi.fn<(data: TPushedLog) => Promise<void>>(async () => undefined)+ };const licenseService = { getPlan: vi.fn(async () => ({ auditLogsRetentionDays: retentionDays })) };const auditLogSettingsService = {getEffectiveSettings: vi.fn<(orgId: string) => Promise<TEffectiveAuditLogSettings>>(async () => ({@@ -196,6 +199,24 @@ describe("recordPermissionDenied", () => {await expect(service.recordPermissionDenied(denial())).resolves.toBeUndefined();expect(auditLogQueue.pushToLog).not.toHaveBeenCalled();});++ test("a failed flush enqueue closes the window so later denials are not lost", async () => {+ const { service, queueService, keyStore, auditLogQueue } = createHarness();+ queueService.queue.mockRejectedValueOnce(new Error("queue down"));++ await expect(service.recordPermissionDenied(denial())).resolves.toBeUndefined();++ expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(1);+ const [windowKey] = keyStore.setItemWithExpiryNX.mock.calls[0];+ expect(keyStore.deleteItem).toHaveBeenCalledWith(windowKey);++ await service.recordPermissionDenied(denial());++ expect(keyStore.setItemWithExpiryNX).toHaveBeenCalledTimes(2);+ expect(keyStore.incrementByWithExpiry).not.toHaveBeenCalled();+ expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(2);+ expect(queueService.queue).toHaveBeenCalledTimes(2);+ });});describe("createCollapsedAuditLog", () => {@@ -254,8 +275,9 @@ describe("collapsed audit log flush job", () => {expect(keyStore.getItem.mock.calls[0][0]).toContain(jobData.collapseKey);expect(keyStore.getItem.mock.calls[0][0]).toContain(jobData.windowStart);expect(keyStore.deleteItem).not.toHaveBeenCalled();- expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(1);- expect(auditLogQueue.pushToLog.mock.calls[0][0]).toMatchObject({+ expect(auditLogQueue.pushToLog).not.toHaveBeenCalled();+ expect(auditLogQueue.pushToLogOrThrow).toHaveBeenCalledTimes(1);+ expect(auditLogQueue.pushToLogOrThrow.mock.calls[0][0]).toMatchObject({orgId: "org-1",projectId: "project-1",actor: { type: "identity", metadata: { identityId: "identity-1" } },@@ -276,6 +298,23 @@ describe("collapsed audit log flush job", () => {await flush({ data: jobData });- expect(auditLogQueue.pushToLog).not.toHaveBeenCalled();+ expect(auditLogQueue.pushToLogOrThrow).not.toHaveBeenCalled();+ });++ test("a failed summary push rejects so the job is retried", async () => {+ const { flush, auditLogQueue } = createHarness({ storedCount: "3" });+ auditLogQueue.pushToLogOrThrow.mockRejectedValueOnce(new Error("redis down"));++ await expect(flush({ data: jobData })).rejects.toThrow("redis down");+ });++ test("the flush job is enqueued with retries", async () => {+ const { service, queueService } = createHarness();++ await service.recordPermissionDenied(denial());++ const opts = queueService.queue.mock.calls[0][3] as { attempts?: number; backoff?: { type: string } };+ expect(opts.attempts).toBeGreaterThan(1);+ expect(opts.backoff?.type).toBe("exponential");});});diff --git a/backend/src/ee/services/audit-log/audit-log-service.ts b/backend/src/ee/services/audit-log/audit-log-service.tsindex 5d05801294..0fc2d81608 100644--- a/backend/src/ee/services/audit-log/audit-log-service.ts+++ b/backend/src/ee/services/audit-log/audit-log-service.ts@@ -40,6 +40,8 @@ const DEFAULT_COLLAPSE_WINDOW_SECONDS = 60;// The flush runs after the window key has expired, so every repeat it counts was made while// that window was open.const COLLAPSE_FLUSH_GRACE_MS = 2_000;+const COLLAPSE_FLUSH_ATTEMPTS = 5;+const COLLAPSE_FLUSH_BACKOFF_MS = 3_000;type TAuditLogServiceFactoryDep = {auditLogDAL: TAuditLogDALFactory;@@ -49,7 +51,10 @@ type TAuditLogServiceFactoryDep = {auditLogSettingsService: Pick<TAuditLogSettingsServiceFactory, "getEffectiveSettings">;licenseService: Pick<TLicenseServiceFactory, "getPlan">;queueService: Pick<TQueueServiceFactory, "queue" | "start">;- keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "setItemWithExpiryNX" | "incrementByWithExpiry">;+ keyStore: Pick<+ TKeyStoreFactory,+ "getItem" | "setItemWithExpiry" | "setItemWithExpiryNX" | "incrementByWithExpiry" | "deleteItem"+ >;smtpService: Pick<TSmtpService, "sendMail">;userDAL: Pick<TUserDALFactory, "getUsersByFilter">;notificationService: Pick<TNotificationServiceFactory, "createUserNotifications">;@@ -200,7 +205,8 @@ export const auditLogServiceFactory = ({};// Runs outside a request, so it skips createAuditLog. The payload already carries the- // actor's permission metadata from the first event.+ // actor's permission metadata from the first event. A failed push rejects so the job retries;+ // the request-path push would swallow it and the whole window's repeats would vanish.const flushCollapsedRepeats = async ({collapseKey,windowStart,@@ -216,7 +222,7 @@ export const auditLogServiceFactory = ({suppressedFrom: windowStart,suppressedUntil: windowEnd};- await auditLogQueue.pushToLog({+ await auditLogQueue.pushToLogOrThrow({...auditLog,event: { ...auditLog.event, metadata: { ...auditLog.event.metadata, ...summary } } as Event});@@ -256,17 +262,24 @@ export const auditLogServiceFactory = ({await createAuditLog(data);const windowEnd = new Date(Date.parse(windowStart) + collapseWindowSeconds * 1000).toISOString();- await queueService.queue(- QueueName.AuditLogCollapsedFlush,- QueueJobs.AuditLogCollapsedFlush,- { ...data, collapseKey, windowStart, windowEnd },- {- jobId: `audit-log-collapse-${collapseKey}-${Date.parse(windowStart)}`,- delay: collapseWindowSeconds * 1000 + COLLAPSE_FLUSH_GRACE_MS,- removeOnComplete: true,- removeOnFail: true- }- );+ try {+ await queueService.queue(+ QueueName.AuditLogCollapsedFlush,+ QueueJobs.AuditLogCollapsedFlush,+ { ...data, collapseKey, windowStart, windowEnd },+ {+ jobId: `audit-log-collapse-${collapseKey}-${Date.parse(windowStart)}`,+ delay: collapseWindowSeconds * 1000 + COLLAPSE_FLUSH_GRACE_MS,+ attempts: COLLAPSE_FLUSH_ATTEMPTS,+ backoff: { type: "exponential", delay: COLLAPSE_FLUSH_BACKOFF_MS },+ removeOnComplete: true,+ removeOnFail: true+ }+ );+ } catch (error) {+ await keyStore.deleteItem(windowKey);+ throw error;+ }};const createCollapsedAuditLog: TAuditLogServiceFactory["createCollapsedAuditLog"] = async ({