-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
922 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { KibanaResponseFactory } from '../../../../../../src/core/server'; | ||
import { ErrorThatHandlesItsOwnResponse } from './types'; | ||
|
||
export class RuleMutedError extends Error implements ErrorThatHandlesItsOwnResponse { | ||
constructor(message: string) { | ||
super(message); | ||
} | ||
|
||
public sendResponse(res: KibanaResponseFactory) { | ||
return res.badRequest({ body: { message: this.message } }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
x-pack/plugins/alerting/server/lib/validate_snooze_date.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export const validateSnoozeDate = (date: string) => { | ||
const parsedValue = Date.parse(date); | ||
if (isNaN(parsedValue)) return `Invalid date: ${date}`; | ||
if (parsedValue <= Date.now()) return `Invalid snooze date as it is in the past: ${date}`; | ||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
x-pack/plugins/alerting/server/routes/snooze_rule.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { snoozeRuleRoute } from './snooze_rule'; | ||
import { httpServiceMock } from 'src/core/server/mocks'; | ||
import { licenseStateMock } from '../lib/license_state.mock'; | ||
import { mockHandlerArguments } from './_mock_handler_arguments'; | ||
import { rulesClientMock } from '../rules_client.mock'; | ||
import { AlertTypeDisabledError } from '../lib/errors/alert_type_disabled'; | ||
|
||
const rulesClient = rulesClientMock.create(); | ||
jest.mock('../lib/license_api_access.ts', () => ({ | ||
verifyApiAccess: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
const SNOOZE_END_TIME = '2025-03-07T00:00:00.000Z'; | ||
|
||
describe('snoozeAlertRoute', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers('modern'); | ||
jest.setSystemTime(new Date(2020, 3, 1)); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
it('snoozes an alert', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
snoozeRuleRoute(router, licenseState); | ||
|
||
const [config, handler] = router.post.mock.calls[0]; | ||
|
||
expect(config.path).toMatchInlineSnapshot(`"/internal/alerting/rule/{id}/_snooze"`); | ||
|
||
rulesClient.snooze.mockResolvedValueOnce(); | ||
|
||
const [context, req, res] = mockHandlerArguments( | ||
{ rulesClient }, | ||
{ | ||
params: { | ||
id: '1', | ||
}, | ||
body: { | ||
snooze_end_time: SNOOZE_END_TIME, | ||
}, | ||
}, | ||
['noContent'] | ||
); | ||
|
||
expect(await handler(context, req, res)).toEqual(undefined); | ||
|
||
expect(rulesClient.snooze).toHaveBeenCalledTimes(1); | ||
expect(rulesClient.snooze.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"id": "1", | ||
"snoozeEndTime": "${SNOOZE_END_TIME}", | ||
}, | ||
] | ||
`); | ||
|
||
expect(res.noContent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('also snoozes an alert when passed snoozeEndTime of -1', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
snoozeRuleRoute(router, licenseState); | ||
|
||
const [config, handler] = router.post.mock.calls[0]; | ||
|
||
expect(config.path).toMatchInlineSnapshot(`"/internal/alerting/rule/{id}/_snooze"`); | ||
|
||
rulesClient.snooze.mockResolvedValueOnce(); | ||
|
||
const [context, req, res] = mockHandlerArguments( | ||
{ rulesClient }, | ||
{ | ||
params: { | ||
id: '1', | ||
}, | ||
body: { | ||
snooze_end_time: -1, | ||
}, | ||
}, | ||
['noContent'] | ||
); | ||
|
||
expect(await handler(context, req, res)).toEqual(undefined); | ||
|
||
expect(rulesClient.snooze).toHaveBeenCalledTimes(1); | ||
expect(rulesClient.snooze.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"id": "1", | ||
"snoozeEndTime": -1, | ||
}, | ||
] | ||
`); | ||
|
||
expect(res.noContent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('ensures the rule type gets validated for the license', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
snoozeRuleRoute(router, licenseState); | ||
|
||
const [, handler] = router.post.mock.calls[0]; | ||
|
||
rulesClient.snooze.mockRejectedValue(new AlertTypeDisabledError('Fail', 'license_invalid')); | ||
|
||
const [context, req, res] = mockHandlerArguments({ rulesClient }, { params: {}, body: {} }, [ | ||
'ok', | ||
'forbidden', | ||
]); | ||
|
||
await handler(context, req, res); | ||
|
||
expect(res.forbidden).toHaveBeenCalledWith({ body: { message: 'Fail' } }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { IRouter } from 'kibana/server'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { ILicenseState, RuleMutedError } from '../lib'; | ||
import { verifyAccessAndContext, RewriteRequestCase } from './lib'; | ||
import { SnoozeOptions } from '../rules_client'; | ||
import { AlertingRequestHandlerContext, INTERNAL_BASE_ALERTING_API_PATH } from '../types'; | ||
import { validateSnoozeDate } from '../lib/validate_snooze_date'; | ||
|
||
const paramSchema = schema.object({ | ||
id: schema.string(), | ||
}); | ||
|
||
const bodySchema = schema.object({ | ||
snooze_end_time: schema.oneOf([ | ||
schema.string({ | ||
validate: validateSnoozeDate, | ||
}), | ||
schema.literal(-1), | ||
]), | ||
}); | ||
|
||
const rewriteBodyReq: RewriteRequestCase<SnoozeOptions> = ({ snooze_end_time: snoozeEndTime }) => ({ | ||
snoozeEndTime, | ||
}); | ||
|
||
export const snoozeRuleRoute = ( | ||
router: IRouter<AlertingRequestHandlerContext>, | ||
licenseState: ILicenseState | ||
) => { | ||
router.post( | ||
{ | ||
path: `${INTERNAL_BASE_ALERTING_API_PATH}/rule/{id}/_snooze`, | ||
validate: { | ||
params: paramSchema, | ||
body: bodySchema, | ||
}, | ||
}, | ||
router.handleLegacyErrors( | ||
verifyAccessAndContext(licenseState, async function (context, req, res) { | ||
const rulesClient = context.alerting.getRulesClient(); | ||
const params = req.params; | ||
const body = rewriteBodyReq(req.body); | ||
try { | ||
await rulesClient.snooze({ ...params, ...body }); | ||
return res.noContent(); | ||
} catch (e) { | ||
if (e instanceof RuleMutedError) { | ||
return e.sendResponse(res); | ||
} | ||
throw e; | ||
} | ||
}) | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.