diff --git a/x-pack/plugins/alerting/common/alert.ts b/x-pack/plugins/alerting/common/alert.ts index 4431f185ac9ca..8db51e223056a 100644 --- a/x-pack/plugins/alerting/common/alert.ts +++ b/x-pack/plugins/alerting/common/alert.ts @@ -31,6 +31,7 @@ export enum AlertExecutionStatusErrorReasons { Unknown = 'unknown', License = 'license', Timeout = 'timeout', + Disabled = 'disabled', } export interface AlertExecutionStatus { diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts index f70cbaa13f7d1..d370a278e0a5c 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.test.ts @@ -212,6 +212,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -399,6 +400,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -642,6 +644,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -842,6 +845,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -913,6 +917,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -1090,6 +1095,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -1158,6 +1164,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -1204,6 +1211,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -1514,6 +1522,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -1872,6 +1881,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -1994,6 +2004,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2097,6 +2108,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2300,6 +2312,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2328,6 +2341,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2359,7 +2373,9 @@ describe('Task Runner', () => { encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({ id: '1', type: 'alert', - attributes: {}, + attributes: { + enabled: true, + }, references: [], }); @@ -2396,6 +2412,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2441,6 +2458,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2667,6 +2685,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2784,6 +2803,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -2900,6 +2920,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3020,6 +3041,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3070,6 +3092,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3103,6 +3126,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3144,6 +3168,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3200,6 +3225,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3486,6 +3512,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3692,6 +3719,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -3889,6 +3917,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -4092,6 +4121,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -4266,6 +4296,7 @@ describe('Task Runner', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); @@ -4401,4 +4432,86 @@ describe('Task Runner', () => { { refresh: false, namespace: undefined } ); }); + + test('successfully bails on execution if the rule is disabled', async () => { + const state = { + ...mockedTaskInstance.state, + previousStartedAt: new Date(Date.now() - 5 * 60 * 1000).toISOString(), + }; + const taskRunner = new TaskRunner( + alertType, + { + ...mockedTaskInstance, + state, + }, + taskRunnerFactoryInitializerParams + ); + rulesClient.get.mockResolvedValue(mockedAlertTypeSavedObject); + encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValue({ + id: '1', + type: 'alert', + attributes: { + apiKey: Buffer.from('123:abc').toString('base64'), + enabled: false, + }, + references: [], + }); + const runnerResult = await taskRunner.run(); + expect(runnerResult.state.previousStartedAt?.toISOString()).toBe(state.previousStartedAt); + expect(runnerResult.schedule).toStrictEqual(mockedTaskInstance.schedule); + + const eventLogger = taskRunnerFactoryInitializerParams.eventLogger; + expect(eventLogger.logEvent).toHaveBeenCalledTimes(2); + expect(eventLogger.logEvent.mock.calls[0][0]).toStrictEqual({ + '@timestamp': '1970-01-01T00:00:00.000Z', + event: { + action: 'execute-start', + kind: 'alert', + category: ['alerts'], + }, + kibana: { + saved_objects: [ + { rel: 'primary', type: 'alert', id: '1', namespace: undefined, type_id: 'test' }, + ], + task: { scheduled: '1970-01-01T00:00:00.000Z', schedule_delay: 0 }, + }, + rule: { + id: '1', + license: 'basic', + category: 'test', + ruleset: 'alerts', + }, + message: 'alert execution start: "1"', + }); + expect(eventLogger.logEvent.mock.calls[1][0]).toStrictEqual({ + '@timestamp': '1970-01-01T00:00:00.000Z', + event: { + action: 'execute', + kind: 'alert', + category: ['alerts'], + reason: 'disabled', + outcome: 'failure', + }, + kibana: { + saved_objects: [ + { rel: 'primary', type: 'alert', id: '1', namespace: undefined, type_id: 'test' }, + ], + task: { + scheduled: '1970-01-01T00:00:00.000Z', + schedule_delay: 0, + }, + alerting: { status: 'error' }, + }, + rule: { + id: '1', + license: 'basic', + category: 'test', + ruleset: 'alerts', + }, + error: { + message: 'Rule failed to execute because rule ran after it was disabled.', + }, + message: 'test:1: execution failed', + }); + }); }); diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner.ts b/x-pack/plugins/alerting/server/task_runner/task_runner.ts index fe95ec646387d..fb7268ef529da 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner.ts @@ -117,19 +117,22 @@ export class TaskRunner< this.cancelled = false; } - async getApiKeyForAlertPermissions(alertId: string, spaceId: string) { + async getDecryptedAttributes( + ruleId: string, + spaceId: string + ): Promise<{ apiKey: string | null; enabled: boolean }> { const namespace = this.context.spaceIdToNamespace(spaceId); // Only fetch encrypted attributes here, we'll create a saved objects client // scoped with the API key to fetch the remaining data. const { - attributes: { apiKey }, + attributes: { apiKey, enabled }, } = await this.context.encryptedSavedObjectsClient.getDecryptedAsInternalUser( 'alert', - alertId, + ruleId, { namespace } ); - return apiKey; + return { apiKey, enabled }; } private getFakeKibanaRequest(spaceId: string, apiKey: RawAlert['apiKey']) { @@ -516,12 +519,23 @@ export class TaskRunner< const { params: { alertId, spaceId }, } = this.taskInstance; + let enabled: boolean; let apiKey: string | null; try { - apiKey = await this.getApiKeyForAlertPermissions(alertId, spaceId); + const decryptedAttributes = await this.getDecryptedAttributes(alertId, spaceId); + apiKey = decryptedAttributes.apiKey; + enabled = decryptedAttributes.enabled; } catch (err) { throw new ErrorWithReason(AlertExecutionStatusErrorReasons.Decrypt, err); } + + if (!enabled) { + throw new ErrorWithReason( + AlertExecutionStatusErrorReasons.Disabled, + new Error(`Rule failed to execute because rule ran after it was disabled.`) + ); + } + const [services, rulesClient] = this.getServicesWithSpaceLevelPermissions(spaceId, apiKey); let alert: SanitizedAlert; diff --git a/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts b/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts index 95cb356af3c1a..c82cc0a7f21e8 100644 --- a/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts +++ b/x-pack/plugins/alerting/server/task_runner/task_runner_cancel.test.ts @@ -171,6 +171,7 @@ describe('Task Runner Cancel', () => { type: 'alert', attributes: { apiKey: Buffer.from('123:abc').toString('base64'), + enabled: true, }, references: [], }); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_list/translations.ts b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_list/translations.ts index 8181a5171d198..293a5e79e29b2 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_list/translations.ts +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/alerts_list/translations.ts @@ -99,6 +99,13 @@ export const ALERT_ERROR_TIMEOUT_REASON = i18n.translate( } ); +export const ALERT_ERROR_DISABLED_REASON = i18n.translate( + 'xpack.triggersActionsUI.sections.alertsList.alertErrorReasonDisabled', + { + defaultMessage: 'Rule failed to execute because rule ran after it was disabled.', + } +); + export const alertsErrorReasonTranslationsMapping = { read: ALERT_ERROR_READING_REASON, decrypt: ALERT_ERROR_DECRYPTING_REASON, @@ -106,4 +113,5 @@ export const alertsErrorReasonTranslationsMapping = { unknown: ALERT_ERROR_UNKNOWN_REASON, license: ALERT_ERROR_LICENSE_REASON, timeout: ALERT_ERROR_TIMEOUT_REASON, + disabled: ALERT_ERROR_DISABLED_REASON, };