Skip to content

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

fix time drift between cron execution and job run

Thiago Araujo da Silva7847548bb585
diff --git a/backend/src/queue/queue-service.ts b/backend/src/queue/queue-service.ts
index 9b1454f811..27f2250cb1 100644
--- a/backend/src/queue/queue-service.ts
+++ b/backend/src/queue/queue-service.ts
@@ -482,7 +482,7 @@ export type TQueueJobTypes = {
};
[QueueName.AlertDispatch]: {
name: QueueJobs.AlertDispatch;
- payload: { alertId: string };
+ payload: { alertId: string; scheduledAt: string };
};
[QueueName.AuditReportGeneration]: {
name: QueueJobs.GenerateAuditReport;
diff --git a/backend/src/services/alert/alert-engine.ts b/backend/src/services/alert/alert-engine.ts
index 44e2441f8d..81c7486e7d 100644
--- a/backend/src/services/alert/alert-engine.ts
+++ b/backend/src/services/alert/alert-engine.ts
@@ -38,7 +38,7 @@ export const alertEngineFactory = ({
kmsService,
smtpService
}: TAlertEngineDep) => {
- const runAlert = async (alert: TAlerts) => {
+ const runAlert = async (alert: TAlerts, opts?: { asOf?: Date }) => {
const provider = alertProviderRegistry.get(alert.resourceType);
if (!provider) {
logger.warn(`No alert provider registered for resource type '${alert.resourceType}' [alertId=${alert.id}]`);
@@ -50,7 +50,8 @@ export const alertEngineFactory = ({
projectId: alert.projectId,
resourceId: alert.resourceId,
eventType: alert.eventType,
- condition: alert.condition
+ condition: alert.condition,
+ asOf: opts?.asOf ?? new Date()
});
if (targets.length === 0) return;
diff --git a/backend/src/services/alert/alert-queue.ts b/backend/src/services/alert/alert-queue.ts
index 23f13fa53a..ddf75827b4 100644
--- a/backend/src/services/alert/alert-queue.ts
+++ b/backend/src/services/alert/alert-queue.ts
@@ -26,7 +26,8 @@ export const alertQueueServiceFactory = ({
}: TAlertQueueServiceFactoryDep) => {
const appCfg = getConfig();
- const enqueueDueAlerts = async () => {
+ const enqueueEnabledAlerts = async () => {
+ const scheduledAt = new Date().toISOString();
for (const resourceType of alertProviderRegistry.resourceTypes()) {
// eslint-disable-next-line no-await-in-loop
const alerts = await alertDAL.findEnabledByResourceType(resourceType);
@@ -36,7 +37,7 @@ export const alertQueueServiceFactory = ({
queueService.queue(
QueueName.AlertDispatch,
QueueJobs.AlertDispatch,
- { alertId: alert.id },
+ { alertId: alert.id, scheduledAt },
{
jobId: `alert-dispatch-${alert.id}`,
removeOnComplete: true,
@@ -53,10 +54,10 @@ export const alertQueueServiceFactory = ({
queueService.start(
QueueName.AlertDispatch,
async (job) => {
- const { alertId } = job.data;
+ const { alertId, scheduledAt } = job.data;
const alert = await alertDAL.findActiveById(alertId);
if (!alert || !alert.enabled) return;
- await alertEngine.runAlert(alert);
+ await alertEngine.runAlert(alert, { asOf: scheduledAt ? new Date(scheduledAt) : new Date() });
},
{ concurrency: ALERT_DISPATCH_CONCURRENCY }
);
@@ -67,8 +68,8 @@ export const alertQueueServiceFactory = ({
runHashTtlS: 60 * 60 * 24,
enabled: !appCfg.isSecondaryInstance,
handler: async () => {
- logger.info("cron[daily-alert-processing]: enqueueing due alerts");
- await enqueueDueAlerts();
+ logger.info("cron[daily-alert-processing]: enqueueing enabled alerts");
+ await enqueueEnabledAlerts();
logger.info("cron[daily-alert-processing]: enqueue complete");
}
});
diff --git a/backend/src/services/alert/alert-types.ts b/backend/src/services/alert/alert-types.ts
index 1069016ee9..42971b32da 100644
--- a/backend/src/services/alert/alert-types.ts
+++ b/backend/src/services/alert/alert-types.ts
@@ -56,6 +56,7 @@ export type TFindDueTargetsInput = {
resourceId?: string | null;
eventType: string;
condition: unknown;
+ asOf: Date;
};
export interface IResourceAlertProvider<TTarget = unknown> {
diff --git a/backend/src/services/alert/providers/identity-credential-alert-dal.ts b/backend/src/services/alert/providers/identity-credential-alert-dal.ts
index 412faa0177..1b98277a31 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-dal.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-dal.ts
@@ -21,8 +21,15 @@ export const identityCredentialAlertDALFactory = (db: TDbClient) => {
orgId,
projectId,
identityId,
- alertBeforeInterval
- }: { orgId: string; projectId?: string | null; identityId?: string | null; alertBeforeInterval: string },
+ alertBeforeInterval,
+ asOf
+ }: {
+ orgId: string;
+ projectId?: string | null;
+ identityId?: string | null;
+ alertBeforeInterval: string;
+ asOf: Date;
+ },
tx?: Knex
): Promise<TExpiringUaClientSecret[]> => {
try {
@@ -44,8 +51,8 @@ export const identityCredentialAlertDALFactory = (db: TDbClient) => {
.where(`${TableName.Membership}.scopeOrgId`, orgId)
.where(`${TableName.IdentityUaClientSecret}.isClientSecretRevoked`, false)
.where(`${TableName.IdentityUaClientSecret}.clientSecretTTL`, ">", 0)
- .whereRaw(`${expiresAtSql} > NOW()`)
- .whereRaw(`${expiresAtSql} <= NOW() + ?::interval`, [alertBeforeInterval]);
+ .whereRaw(`${expiresAtSql} > ?`, [asOf])
+ .whereRaw(`${expiresAtSql} <= ? + ?::interval`, [asOf, alertBeforeInterval]);
if (projectId) {
void query
diff --git a/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts b/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
index ed67f15248..7f5d2f0b76 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
@@ -44,6 +44,7 @@ const buildProvider = (opts?: {
projectId?: string | null;
identityId?: string | null;
alertBeforeInterval: string;
+ asOf: Date;
}) => void;
abilityRules?: { action: string; subject: string; conditions?: Record<string, unknown> }[];
inOrg?: boolean;
@@ -56,6 +57,7 @@ const buildProvider = (opts?: {
projectId?: string | null;
identityId?: string | null;
alertBeforeInterval: string;
+ asOf: Date;
}) => {
opts?.onFind?.(args);
return opts?.secrets ?? [];
@@ -87,7 +89,9 @@ describe("identity credential alert provider", () => {
});
test("findDueTargets converts alertBefore to a postgres interval and tags credential type", async () => {
- let seenArgs: { alertBeforeInterval: string; projectId?: string | null; identityId?: string | null } | undefined;
+ let seenArgs:
+ | { alertBeforeInterval: string; projectId?: string | null; identityId?: string | null; asOf: Date }
+ | undefined;
const provider = buildProvider({
secrets: [sampleSecret()],
onFind: (args) => {
@@ -95,15 +99,18 @@ describe("identity credential alert provider", () => {
}
});
+ const asOf = new Date("2026-07-24T00:00:00.000Z");
const targets = await provider.findDueTargets({
orgId: "org-1",
resourceId: "ident-1",
eventType: IDENTITY_AUTHENTICATION_EXPIRY_EVENT,
- condition: { alertBefore: "30d" }
+ condition: { alertBefore: "30d" },
+ asOf
});
expect(seenArgs?.alertBeforeInterval).toBe("30 days");
expect(seenArgs?.identityId).toBe("ident-1");
+ expect(seenArgs?.asOf).toBe(asOf);
expect(targets).toHaveLength(1);
expect(provider.targetId(targets[0])).toBe("ua-client-secret:sec-1");
});
@@ -121,7 +128,8 @@ describe("identity credential alert provider", () => {
projectId: "proj-1",
resourceId: null,
eventType: IDENTITY_AUTHENTICATION_EXPIRY_EVENT,
- condition: { alertBefore: "30d" }
+ condition: { alertBefore: "30d" },
+ asOf: new Date("2026-07-24T00:00:00.000Z")
});
expect(seenArgs?.projectId).toBe("proj-1");
diff --git a/backend/src/services/alert/providers/identity-credential-alert-provider.ts b/backend/src/services/alert/providers/identity-credential-alert-provider.ts
index ef4eaa96b1..d4a364d673 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-provider.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-provider.ts
@@ -132,7 +132,8 @@ export const identityCredentialAlertProviderFactory = ({
orgId: input.orgId,
projectId: input.projectId,
identityId: input.resourceId,
- alertBeforeInterval: intervalSql
+ alertBeforeInterval: intervalSql,
+ asOf: input.asOf
});
return uaSecrets.map((secret) => ({ credentialType: "ua-client-secret" as const, ...secret }));