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
refactor(alerts): drop the InTx suffix and the test-only channel wrappers
Calvinbf61b62bfd5b
diff --git a/backend/src/services/alert/alert-channel-service.test.ts b/backend/src/services/alert/alert-channel-service.test.tsindex 0e94070302..82c892d6d1 100644--- a/backend/src/services/alert/alert-channel-service.test.ts+++ b/backend/src/services/alert/alert-channel-service.test.ts@@ -99,6 +99,24 @@ const buildService = (opts?: { seed?: TRow[]; projectUserIds?: string[] }) => {return { service, store, recipients, scopeCheckTxs };};+type TService = ReturnType<typeof buildService>["service"];++// Prepares then applies in one transaction, the way the alert service writes a channel.+const createChannel = async (+ service: TService,+ input: Parameters<TService["prepareChannelCreate"]>[0],+ enc: Parameters<TService["prepareChannelCreate"]>[1],+ trx: Knex+) => service.applyChannelCreate(await service.prepareChannelCreate(input, enc, trx), trx);++const updateChannel = async (+ service: TService,+ input: Parameters<TService["prepareChannelUpdate"]>[0],+ channel: Parameters<TService["prepareChannelUpdate"]>[1],+ channelCipher: Parameters<TService["prepareChannelUpdate"]>[2],+ trx: Knex+) => service.applyChannelUpdate(await service.prepareChannelUpdate(input, channel, channelCipher, trx), trx);+const seedRow = (over: Partial<TRow> & { id: string; channelType: AlertChannelType; encryptedConfig: Buffer }): TRow => ({@@ -115,7 +133,8 @@ const seedRow = (describe("alert channel service", () => {test("creates a webhook channel, stores config, and redacts secrets in the detail view", async () => {const { service } = buildService();- const channel = await service.createChannelInTx(+ const channel = await createChannel(+ service,{name: "Ops webhook",channelType: AlertChannelType.WEBHOOK,@@ -137,7 +156,8 @@ describe("alert channel service", () => {test("requires recipients for a directed (email) channel and rejects them for others", async () => {const { service } = buildService();await expect(- service.createChannelInTx(+ createChannel(+ service,{ name: "Team email", channelType: AlertChannelType.EMAIL, config: {}, orgId: "org-1", ...CHANNEL_INPUT },encryptor as never,tx@@ -145,7 +165,8 @@ describe("alert channel service", () => {).rejects.toThrow("require at least one recipient");await expect(- service.createChannelInTx(+ createChannel(+ service,{name: "Ops webhook",channelType: AlertChannelType.WEBHOOK,@@ -162,7 +183,8 @@ describe("alert channel service", () => {test("creates a directed email channel with recipients", async () => {const { service, recipients } = buildService();- const channel = await service.createChannelInTx(+ const channel = await createChannel(+ service,{name: "Team email",channelType: AlertChannelType.EMAIL,@@ -183,7 +205,8 @@ describe("alert channel service", () => {test("rejects duplicate recipients instead of dropping them", async () => {const { service } = buildService();await expect(- service.createChannelInTx(+ createChannel(+ service,{name: "Team email",channelType: AlertChannelType.EMAIL,@@ -209,7 +232,8 @@ describe("alert channel service", () => {test("stores email recipients exactly as sent", async () => {const { service, recipients } = buildService();- const channel = await service.createChannelInTx(+ const channel = await createChannel(+ service,{name: "Team email",channelType: AlertChannelType.EMAIL,@@ -238,7 +262,8 @@ describe("alert channel service", () => {test("rejects an email recipient on an unverified domain", async () => {const { service } = buildService();await expect(- service.createChannelInTx(+ createChannel(+ service,{name: "Team email",channelType: AlertChannelType.EMAIL,@@ -279,7 +304,8 @@ describe("alert channel service", () => {});// Config sent without signingSecret -> keep the existing one.- await service.updateChannelInTx(+ await updateChannel(+ service,{ recipientScope: RECIPIENT_SCOPE, channelId: "ch-1", config: { url: "https://example.com/hook" } },store.get("ch-1") as never,cipher,@@ -299,7 +325,8 @@ describe("alert channel service", () => {});await expect(- service.updateChannelInTx(+ updateChannel(+ service,{ recipientScope: RECIPIENT_SCOPE, channelId: "ch-1", channelType: AlertChannelType.SLACK },channel as never,cipher,@@ -316,7 +343,8 @@ describe("alert channel service", () => {encryptedConfig: encConfig({ url: "https://example.com/hook", signingSecret: "s3cr3t" })});- await service.updateChannelInTx(+ await updateChannel(+ service,{recipientScope: RECIPIENT_SCOPE,channelId: "ch-1",@@ -340,7 +368,8 @@ describe("alert channel service", () => {encryptedConfig: encConfig({ url: "https://example.com/hook", signingSecret: "old" })});- await service.updateChannelInTx(+ await updateChannel(+ service,{recipientScope: RECIPIENT_SCOPE,channelId: "ch-1",@@ -363,7 +392,8 @@ describe("alert channel service", () => {});await expect(- service.updateChannelInTx(+ updateChannel(+ service,{ recipientScope: RECIPIENT_SCOPE, channelId: "ch-1", config: { webhookUrl: "" } },channel as never,cipher,@@ -374,7 +404,8 @@ describe("alert channel service", () => {test("accepts all project members on a channel in the same project", async () => {const { service, recipients } = buildService();- const channel = await service.createChannelInTx(+ const channel = await createChannel(+ service,{name: "Email",channelType: AlertChannelType.EMAIL,@@ -396,7 +427,8 @@ describe("alert channel service", () => {test("rejects all project members on an org-scoped channel", async () => {const { service } = buildService();await expect(- service.createChannelInTx(+ createChannel(+ service,{name: "Email",channelType: AlertChannelType.EMAIL,@@ -415,7 +447,8 @@ describe("alert channel service", () => {test("rejects all project members that names another project", async () => {const { service } = buildService();await expect(- service.createChannelInTx(+ createChannel(+ service,{name: "Email",channelType: AlertChannelType.EMAIL,@@ -434,7 +467,8 @@ describe("alert channel service", () => {test("checks recipient scope inside the caller's transaction", async () => {const { service, scopeCheckTxs } = buildService();- await service.createChannelInTx(+ await createChannel(+ service,{name: "Email",channelType: AlertChannelType.EMAIL,@@ -453,7 +487,8 @@ describe("alert channel service", () => {test("accepts a platform creator with no actor id", async () => {const { service, store } = buildService();- const channel = await service.createChannelInTx(+ const channel = await createChannel(+ service,{name: "Email",channelType: AlertChannelType.EMAIL,diff --git a/backend/src/services/alert/alert-channel-service.ts b/backend/src/services/alert/alert-channel-service.tsindex cfb5d6734c..3ca7269e1b 100644--- a/backend/src/services/alert/alert-channel-service.ts+++ b/backend/src/services/alert/alert-channel-service.ts@@ -43,10 +43,10 @@ export type TAlertChannelServiceFactoryDep = {export type TAlertChannelServiceFactory = ReturnType<typeof alertChannelServiceFactory>;-// Everything the transaction-aware primitives need to write a channel inline. Channels are only ever-// created through their owning alert, so authorization is the alert's (the caller has already run the-// provider's assertPermission) and channel names are not required to be unique.-export type TCreateChannelInTxInput = {+// What a channel write needs. Channels are only ever created through their owning alert, so authorization+// is the alert's (the caller has already run the provider's assertPermission) and channel names are not+// required to be unique.+export type TCreateChannelInput = {name: string;channelType: AlertChannelType | string;config: Record<string, unknown>;@@ -59,7 +59,7 @@ export type TCreateChannelInTxInput = {createdByActorType: string;};-export type TUpdateChannelInTxInput = {+export type TUpdateChannelInput = {channelId: string;channelType?: AlertChannelType | string;name?: string;@@ -212,7 +212,7 @@ export const alertChannelServiceFactory = ({};const prepareChannelCreate = async (- input: TCreateChannelInTxInput,+ input: TCreateChannelInput,encryptor: TAlertEncryptor,tx?: Knex): Promise<TPreparedChannelCreate> => {@@ -237,6 +237,8 @@ export const alertChannelServiceFactory = ({};};+ // The apply and delete steps take a required tx: a channel only means something together with its+ // alert membership, which the caller writes in the same transaction.const applyChannelCreate = async (prepared: TPreparedChannelCreate, tx: Knex): Promise<TAlertChannels> => {const created = await alertChannelDAL.create(prepared.row, tx);if (prepared.recipients.length) {@@ -252,14 +254,8 @@ export const alertChannelServiceFactory = ({return created;};- const createChannelInTx = async (- input: TCreateChannelInTxInput,- encryptor: TAlertEncryptor,- tx: Knex- ): Promise<TAlertChannels> => applyChannelCreate(await prepareChannelCreate(input, encryptor, tx), tx);-const prepareChannelUpdate = async (- input: TUpdateChannelInTxInput,+ input: TUpdateChannelInput,channel: TAlertChannels,cipher: { encryptor: TAlertEncryptor; decryptor: TAlertDecryptor },tx?: Knex@@ -315,13 +311,6 @@ export const alertChannelServiceFactory = ({}};- const updateChannelInTx = async (- input: TUpdateChannelInTxInput,- channel: TAlertChannels,- cipher: { encryptor: TAlertEncryptor; decryptor: TAlertDecryptor },- tx: Knex- ): Promise<void> => applyChannelUpdate(await prepareChannelUpdate(input, channel, cipher, tx), tx);-// For callers that carry recipient lists from outside the alert module (eg reminder recipients that// were never pruned when someone left the project), where an out-of-scope id should be dropped// rather than fail the whole write.@@ -350,7 +339,7 @@ export const alertChannelServiceFactory = ({});};- const deleteChannelInTx = async (channelId: string, tx: Knex): Promise<void> => {+ const deleteChannel = async (channelId: string, tx: Knex): Promise<void> => {await alertChannelDAL.deleteById(channelId, tx);};@@ -388,13 +377,11 @@ export const alertChannelServiceFactory = ({};return {- createChannelInTx,- updateChannelInTx,prepareChannelCreate,applyChannelCreate,prepareChannelUpdate,applyChannelUpdate,- deleteChannelInTx,+ deleteChannel,getDetailsForChannels,validateRecipients,assertRecipientTypesAllowed,diff --git a/backend/src/services/alert/alert-service.test.ts b/backend/src/services/alert/alert-service.test.tsindex 7998358498..e474f8e41d 100644--- a/backend/src/services/alert/alert-service.test.ts+++ b/backend/src/services/alert/alert-service.test.ts@@ -204,7 +204,7 @@ const buildService = (opts?: {});return {};},- deleteChannelInTx: async (channelId: string) => {+ deleteChannel: async (channelId: string) => {channels.delete(channelId);detach(channelId);},diff --git a/backend/src/services/alert/alert-service.ts b/backend/src/services/alert/alert-service.tsindex c9d07a4f57..c726f40fbb 100644--- a/backend/src/services/alert/alert-service.ts+++ b/backend/src/services/alert/alert-service.ts@@ -56,7 +56,7 @@ export type TAlertServiceFactoryDep = {| "applyChannelCreate"| "prepareChannelUpdate"| "applyChannelUpdate"- | "deleteChannelInTx"+ | "deleteChannel"| "getDetailsForChannels"| "filterRecipientsInScope">;@@ -446,7 +446,7 @@ export const alertServiceFactory = ({Object.keys(plan.patch).length > 0 ? await alertDAL.updateById(plan.alert.id, plan.patch, tx) : plan.alert;for (const channelId of plan.deleteChannelIds) {// eslint-disable-next-line no-await-in-loop -- one shared tx connection; writes must be serial- await alertChannelService.deleteChannelInTx(channelId, tx);+ await alertChannelService.deleteChannel(channelId, tx);}for (const prepared of plan.channelUpdates) {// eslint-disable-next-line no-await-in-loop -- one shared tx connection; writes must be serial