Skip to content

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

validate if channel exists on enqueing

Thiago Araujo da Silva413a83844755
diff --git a/backend/src/services/alert/alert-dal.test.ts b/backend/src/services/alert/alert-dal.test.ts
index 79f47ddb5d..bf55f5e51b 100644
--- a/backend/src/services/alert/alert-dal.test.ts
+++ b/backend/src/services/alert/alert-dal.test.ts
@@ -6,7 +6,37 @@ import { alertDALFactory } from "./alert-dal";
// soft-deleted projects (regression for: a soft-deleted project must not keep firing alerts during
// its cleanup grace window).
const buildDAL = () => {
- const calls = { where: [] as unknown[][], whereNull: [] as unknown[], leftJoin: [] as unknown[][] };
+ const calls = {
+ where: [] as unknown[][],
+ whereNull: [] as unknown[],
+ leftJoin: [] as unknown[][],
+ // The whereExists subquery is recorded through its own chain below.
+ existsFrom: [] as unknown[],
+ existsJoin: [] as unknown[][],
+ existsWhere: [] as unknown[][],
+ existsWhereRaw: [] as unknown[]
+ };
+
+ const existsChain = {
+ select: () => existsChain,
+ from: (table: unknown) => {
+ calls.existsFrom.push(table);
+ return existsChain;
+ },
+ join: (...args: unknown[]) => {
+ calls.existsJoin.push(args);
+ return existsChain;
+ },
+ whereRaw: (sql: unknown) => {
+ calls.existsWhereRaw.push(sql);
+ return existsChain;
+ },
+ where: (...args: unknown[]) => {
+ calls.existsWhere.push(args);
+ return existsChain;
+ }
+ };
+
const chain = {
leftJoin: (...args: unknown[]) => {
calls.leftJoin.push(args);
@@ -20,11 +50,15 @@ const buildDAL = () => {
calls.whereNull.push(col);
return chain;
},
+ whereExists: (cb: (qb: typeof existsChain) => void) => {
+ cb(existsChain);
+ return chain;
+ },
select: () => chain,
orderBy: async () => [] as TAlerts[]
};
const queryBuilder = () => chain;
- const db = { replicaNode: () => queryBuilder } as never;
+ const db = Object.assign(queryBuilder, { replicaNode: () => queryBuilder, raw: (sql: string) => sql }) as never;
return { dal: alertDALFactory(db), calls };
};
@@ -42,4 +76,17 @@ describe("alert dal", () => {
// the cron sweep must only pick up scheduled alerts, never event-driven ones
expect(calls.where).toContainEqual([`${TableName.Alert}.triggerType`, "scheduled"]);
});
+
+ test("findEnabledByResourceType skips alerts with no enabled channel so the cron enqueues no dead jobs", async () => {
+ const { dal, calls } = buildDAL();
+
+ await dal.findEnabledByResourceType("identity.authentication");
+
+ // Semi-join through the membership table to the channels, correlated on the outer alert row.
+ expect(calls.existsFrom).toContainEqual(TableName.AlertChannelMembership);
+ expect(calls.existsJoin[0]?.[0]).toBe(TableName.AlertChannel);
+ expect(calls.existsWhereRaw[0]).toBe(`"${TableName.AlertChannelMembership}"."alertId" = "${TableName.Alert}"."id"`);
+ // A channel that exists but is switched off must not keep the alert in the sweep.
+ expect(calls.existsWhere).toContainEqual([`${TableName.AlertChannel}.enabled`, true]);
+ });
});
diff --git a/backend/src/services/alert/alert-dal.ts b/backend/src/services/alert/alert-dal.ts
index 6da65c8efe..64fc688cc6 100644
--- a/backend/src/services/alert/alert-dal.ts
+++ b/backend/src/services/alert/alert-dal.ts
@@ -20,6 +20,19 @@ export const alertDALFactory = (db: TDbClient) => {
.where(`${TableName.Alert}.triggerType`, AlertTriggerType.Scheduled)
.where(`${TableName.Alert}.enabled`, true)
.whereNull(`${TableName.Project}.deleteAfter`)
+ .whereExists(
+ (qb) =>
+ void qb
+ .select(db.raw("1"))
+ .from(TableName.AlertChannelMembership)
+ .join(
+ TableName.AlertChannel,
+ `${TableName.AlertChannelMembership}.channelId`,
+ `${TableName.AlertChannel}.id`
+ )
+ .whereRaw(`"${TableName.AlertChannelMembership}"."alertId" = "${TableName.Alert}"."id"`)
+ .where(`${TableName.AlertChannel}.enabled`, true)
+ )
.select(selectAllTableCols(TableName.Alert))
.orderBy(`${TableName.Alert}.createdAt`, "asc");