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
move alert helpers out of alert-types into the files that use them
Carlos Monastyrskib6988e6977c3
diff --git a/backend/src/services/alert/alert-channel-test-service.ts b/backend/src/services/alert/alert-channel-test-service.tsindex 3757cc15eb..16d192f068 100644--- a/backend/src/services/alert/alert-channel-test-service.ts+++ b/backend/src/services/alert/alert-channel-test-service.ts@@ -14,19 +14,12 @@ import { TAlertChannelDALFactory } from "./alert-channel-dal";import { TAlertChannelServiceFactory } from "./alert-channel-service";import { TAlertChannelDeps, TAlertRecipient } from "./alert-channel-types";import { TAlertDALFactory } from "./alert-dal";-import { TAlertProviderRegistry } from "./alert-provider-registry";+import { getRecipientScope } from "./alert-principal-scope-fns";+import { getAlertResourceName, resolveAlertProjectId, TAlertProviderRegistry } from "./alert-provider-registry";import { TAlertRecipientResolver } from "./alert-recipient-resolver";import { TTestAlertChannelDTO, TTestAlertChannelResponse, TTestAlertChannelResult } from "./alert-service-types";import { buildTestAlertPayload } from "./alert-test-payload-fns";-import {- AlertPermissionAction,- getAlertResourceName,- getRecipientScope,- IResourceAlertProvider,- resolveAlertProjectId,- TAlertRecipientScope,- toAlertActor-} from "./alert-types";+import { AlertPermissionAction, IResourceAlertProvider, TAlertRecipientScope, toAlertActor } from "./alert-types";export type TAlertChannelTestServiceFactoryDep = {alertChannelDAL: Pick<TAlertChannelDALFactory, "findById">;diff --git a/backend/src/services/alert/alert-engine.ts b/backend/src/services/alert/alert-engine.tsindex a20dc61ebc..4bbd418179 100644--- a/backend/src/services/alert/alert-engine.ts+++ b/backend/src/services/alert/alert-engine.ts@@ -11,15 +11,10 @@ import { TAlertChannelDALFactory } from "./alert-channel-dal";import { TAlertChannelRecipientDALFactory } from "./alert-channel-recipient-dal";import { AlertChannelType, TAlertChannelDeps, TAlertRecipient, TChannelTargetResult } from "./alert-channel-types";import { getDedupCutoff, TAlertHistoryDALFactory } from "./alert-history-dal";+import { getRecipientScope } from "./alert-principal-scope-fns";import { TAlertProviderRegistry } from "./alert-provider-registry";import { TAlertRecipientResolver } from "./alert-recipient-resolver";-import {- AlertRunStatus,- DEFAULT_DEDUP_WINDOW_HOURS,- getRecipientScope,- IResourceAlertProvider,- TAlertContext-} from "./alert-types";+import { AlertRunStatus, DEFAULT_DEDUP_WINDOW_HOURS, IResourceAlertProvider, TAlertContext } from "./alert-types";import { ALERT_CHANNEL_REGISTRY } from "./channels/alert-channel-registry";const ALERT_DELIVERY_CONCURRENCY = 10;diff --git a/backend/src/services/alert/alert-principal-scope-fns.ts b/backend/src/services/alert/alert-principal-scope-fns.tsindex c03577d67e..9ad448f37a 100644--- a/backend/src/services/alert/alert-principal-scope-fns.ts+++ b/backend/src/services/alert/alert-principal-scope-fns.ts@@ -7,6 +7,8 @@ import { TGroupDALFactory } from "@app/ee/services/group/group-dal";import { TOrgDALFactory } from "@app/services/org/org-dal";import { TProjectDALFactory } from "@app/services/project/project-dal";+import { IResourceAlertProvider, TAlertRecipientScope } from "./alert-types";+export type TPrincipalScopeDALs = {orgDAL: Pick<TOrgDALFactory, "findMembership">;projectDAL: Pick<TProjectDALFactory, "findEffectiveProjectSubjectsMembership">;@@ -87,3 +89,20 @@ export const resolvePrincipalsInScope = async (const orgGroups = await groupDAL.find({ $in: { id: groupIds }, orgId }, { tx });return { userIds: inScopeUserIds, groupIds: new Set(orgGroups.map((group) => group.id)) };};++export const getRecipientScope = (+ provider: Pick<IResourceAlertProvider, "recipientPolicy">,+ projectId?: string | null+): TAlertRecipientScope => ({+ projectId: provider.recipientPolicy?.atOrgScope ? null : (projectId ?? null),+ allowEmailAddresses: Boolean(provider.recipientPolicy?.allowEmailAddresses)+});++export const getAlertResourceName = async (+ provider: Pick<IResourceAlertProvider, "getResourceNames">,+ orgId: string,+ resourceId?: string | null+): Promise<string | null> => {+ if (!resourceId || !provider.getResourceNames) return null;+ return (await provider.getResourceNames({ orgId, resourceIds: [resourceId] })).get(resourceId) ?? null;+};diff --git a/backend/src/services/alert/alert-provider-registry.ts b/backend/src/services/alert/alert-provider-registry.tsindex 909e63b527..69247c7359 100644--- a/backend/src/services/alert/alert-provider-registry.ts+++ b/backend/src/services/alert/alert-provider-registry.ts@@ -1,5 +1,22 @@import { AlertTriggerType, IResourceAlertProvider } from "./alert-types";+export const resolveAlertProjectId = async (+ provider: IResourceAlertProvider,+ { orgId, projectId, resourceId }: { orgId: string; projectId?: string | null; resourceId?: string | null }+): Promise<string | null> => {+ if (projectId || !resourceId || !provider.resolveProjectId) return projectId ?? null;+ return provider.resolveProjectId({ orgId, resourceId });+};++export const getAlertResourceName = async (+ provider: Pick<IResourceAlertProvider, "getResourceNames">,+ orgId: string,+ resourceId?: string | null+): Promise<string | null> => {+ if (!resourceId || !provider.getResourceNames) return null;+ return (await provider.getResourceNames({ orgId, resourceIds: [resourceId] })).get(resourceId) ?? null;+};+export type TAlertProviderRegistry = ReturnType<typeof alertProviderRegistryFactory>;export const alertProviderRegistryFactory = () => {diff --git a/backend/src/services/alert/alert-service.ts b/backend/src/services/alert/alert-service.tsindex 2d40bdba6b..6de353ba23 100644--- a/backend/src/services/alert/alert-service.ts+++ b/backend/src/services/alert/alert-service.ts@@ -2,7 +2,7 @@ import { Knex } from "knex";import { z } from "zod";import { TAlertChannels, TAlerts } from "@app/db/schemas";-import { Event as TAuditEvent } from "@app/ee/services/audit-log/audit-log-types";+import { Event as TAuditEvent, EventType } from "@app/ee/services/audit-log/audit-log-types";import { BadRequestError, NotFoundError } from "@app/lib/errors";import { TGenericPermission } from "@app/lib/types";import { TKmsServiceFactory } from "@app/services/kms/kms-service";@@ -14,7 +14,8 @@ import { TAlertChannelServiceFactory } from "./alert-channel-service";import { TAlertChannelEmbedded, TAlertChannelInput } from "./alert-channel-service-types";import { TAlertDALFactory } from "./alert-dal";import { TAlertHistoryDALFactory } from "./alert-history-dal";-import { TAlertProviderRegistry } from "./alert-provider-registry";+import { getRecipientScope } from "./alert-principal-scope-fns";+import { getAlertResourceName, resolveAlertProjectId, TAlertProviderRegistry } from "./alert-provider-registry";import {TAlertLastRun,TAlertResponse,@@ -29,11 +30,7 @@ import {AlertPermissionAction,AlertRunStatus,AlertTelemetryAction,- buildGenericAlertAuditEvent,- getAlertResourceName,- getRecipientScope,IResourceAlertProvider,- resolveAlertProjectId,TAlertAuditInput,TAlertEventDefinition,TAlertRecipientScope,@@ -55,6 +52,36 @@ export type TAlertServiceFactoryDep = {export type TAlertServiceFactory = ReturnType<typeof alertServiceFactory>;+const buildGenericAlertAuditEvent = (input: TAlertAuditInput): TAuditEvent => {+ if (input.action === AlertAuditAction.TestChannel) {+ const { test } = input;+ return {+ type: EventType.TEST_ALERT_CHANNEL,+ metadata: {+ channelId: test.channelId,+ channelType: test.channelType,+ resourceType: test.resourceType,+ resourceId: test.resourceId,+ success: test.success,+ deliveredTo: test.deliveredTo,+ error: test.error+ }+ };+ }+ const { alert } = input;+ const metadata = {+ alertId: alert.id,+ name: alert.name,+ resourceType: alert.resourceType,+ eventType: alert.eventType+ };+ if (input.action === AlertAuditAction.Create) {+ return { type: EventType.CREATE_ALERT, metadata: { ...metadata, resourceId: alert.resourceId } };+ }+ if (input.action === AlertAuditAction.Update) return { type: EventType.UPDATE_ALERT, metadata };+ return { type: EventType.DELETE_ALERT, metadata };+};+export const alertServiceFactory = ({alertDAL,alertChannelDAL,diff --git a/backend/src/services/alert/alert-types.ts b/backend/src/services/alert/alert-types.tsindex 0703d545f7..7702a81ff3 100644--- a/backend/src/services/alert/alert-types.ts+++ b/backend/src/services/alert/alert-types.ts@@ -1,6 +1,6 @@import { z } from "zod";-import { Event as TAuditEvent, EventType } from "@app/ee/services/audit-log/audit-log-types";+import { Event as TAuditEvent } from "@app/ee/services/audit-log/audit-log-types";import { TGenericPermission } from "@app/lib/types";import { TPostHogEvent } from "@app/services/telemetry/telemetry-types";@@ -51,14 +51,6 @@ export const toAlertActor = (dto: TGenericPermission): TGenericPermission => ({actorOrgId: dto.actorOrgId});-export const resolveAlertProjectId = async (- provider: IResourceAlertProvider,- { orgId, projectId, resourceId }: { orgId: string; projectId?: string | null; resourceId?: string | null }-): Promise<string | null> => {- if (projectId || !resourceId || !provider.resolveProjectId) return projectId ?? null;- return provider.resolveProjectId({ orgId, resourceId });-};-type TAlertRecipientPolicy = {atOrgScope?: boolean;allowEmailAddresses?: boolean;@@ -69,23 +61,6 @@ export type TAlertRecipientScope = {allowEmailAddresses: boolean;};-export const getRecipientScope = (- provider: Pick<IResourceAlertProvider, "recipientPolicy">,- projectId?: string | null-): TAlertRecipientScope => ({- projectId: provider.recipientPolicy?.atOrgScope ? null : (projectId ?? null),- allowEmailAddresses: Boolean(provider.recipientPolicy?.allowEmailAddresses)-});--export const getAlertResourceName = async (- provider: Pick<IResourceAlertProvider, "getResourceNames">,- orgId: string,- resourceId?: string | null-): Promise<string | null> => {- if (!resourceId || !provider.getResourceNames) return null;- return (await provider.getResourceNames({ orgId, resourceIds: [resourceId] })).get(resourceId) ?? null;-};-export const DEFAULT_DEDUP_WINDOW_HOURS = 24;// Providers scan this many days ahead of `alertBefore` so alerts fire at LEAST `alertBefore` before@@ -173,36 +148,6 @@ export type TAlertAuditInput =| { action: AlertAuditAction.Create | AlertAuditAction.Update | AlertAuditAction.Delete; alert: TAlertAuditAlert }| { action: AlertAuditAction.TestChannel; test: TAlertChannelTestAudit };-export const buildGenericAlertAuditEvent = (input: TAlertAuditInput): TAuditEvent => {- if (input.action === AlertAuditAction.TestChannel) {- const { test } = input;- return {- type: EventType.TEST_ALERT_CHANNEL,- metadata: {- channelId: test.channelId,- channelType: test.channelType,- resourceType: test.resourceType,- resourceId: test.resourceId,- success: test.success,- deliveredTo: test.deliveredTo,- error: test.error- }- };- }- const { alert } = input;- const metadata = {- alertId: alert.id,- name: alert.name,- resourceType: alert.resourceType,- eventType: alert.eventType- };- if (input.action === AlertAuditAction.Create) {- return { type: EventType.CREATE_ALERT, metadata: { ...metadata, resourceId: alert.resourceId } };- }- if (input.action === AlertAuditAction.Update) return { type: EventType.UPDATE_ALERT, metadata };- return { type: EventType.DELETE_ALERT, metadata };-};-export enum AlertTelemetryAction {Create = "create",Update = "update",