-
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.
[Alerting] Update rules detail page to resolve SO IDs if necessary (#…
…108091) (#109194) * Adding internal resolve API to resolve rules given an ID * Updating after merge * Updating after merge * Adding resolveRule api to client and adding spacesOss plugin dependency * Handling 404 errors by calling resolve. Updating unit tests * Handling aliasMatch and conflict results * Fixing types * Unit tests for spaces oss components * Adding audit event for resolve Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: ymao1 <[email protected]>
- Loading branch information
1 parent
d9c71d4
commit 35f9af3
Showing
22 changed files
with
1,287 additions
and
112 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
182 changes: 182 additions & 0 deletions
182
x-pack/plugins/alerting/server/routes/resolve_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,182 @@ | ||
/* | ||
* 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 { pick } from 'lodash'; | ||
import { resolveRuleRoute } from './resolve_rule'; | ||
import { httpServiceMock } from 'src/core/server/mocks'; | ||
import { licenseStateMock } from '../lib/license_state.mock'; | ||
import { verifyApiAccess } from '../lib/license_api_access'; | ||
import { mockHandlerArguments } from './_mock_handler_arguments'; | ||
import { rulesClientMock } from '../rules_client.mock'; | ||
import { ResolvedSanitizedRule } from '../types'; | ||
import { AsApiContract } from './lib'; | ||
|
||
const rulesClient = rulesClientMock.create(); | ||
jest.mock('../lib/license_api_access.ts', () => ({ | ||
verifyApiAccess: jest.fn(), | ||
})); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('resolveRuleRoute', () => { | ||
const mockedRule: ResolvedSanitizedRule<{ | ||
bar: boolean; | ||
}> = { | ||
id: '1', | ||
alertTypeId: '1', | ||
schedule: { interval: '10s' }, | ||
params: { | ||
bar: true, | ||
}, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
actions: [ | ||
{ | ||
group: 'default', | ||
id: '2', | ||
actionTypeId: 'test', | ||
params: { | ||
foo: true, | ||
}, | ||
}, | ||
], | ||
consumer: 'bar', | ||
name: 'abc', | ||
tags: ['foo'], | ||
enabled: true, | ||
muteAll: false, | ||
notifyWhen: 'onActionGroupChange', | ||
createdBy: '', | ||
updatedBy: '', | ||
apiKeyOwner: '', | ||
throttle: '30s', | ||
mutedInstanceIds: [], | ||
executionStatus: { | ||
status: 'unknown', | ||
lastExecutionDate: new Date('2020-08-20T19:23:38Z'), | ||
}, | ||
outcome: 'aliasMatch', | ||
alias_target_id: '2', | ||
}; | ||
|
||
const resolveResult: AsApiContract<ResolvedSanitizedRule<{ bar: boolean }>> = { | ||
...pick( | ||
mockedRule, | ||
'consumer', | ||
'name', | ||
'schedule', | ||
'tags', | ||
'params', | ||
'throttle', | ||
'enabled', | ||
'alias_target_id' | ||
), | ||
rule_type_id: mockedRule.alertTypeId, | ||
notify_when: mockedRule.notifyWhen, | ||
mute_all: mockedRule.muteAll, | ||
created_by: mockedRule.createdBy, | ||
updated_by: mockedRule.updatedBy, | ||
api_key_owner: mockedRule.apiKeyOwner, | ||
muted_alert_ids: mockedRule.mutedInstanceIds, | ||
created_at: mockedRule.createdAt, | ||
updated_at: mockedRule.updatedAt, | ||
id: mockedRule.id, | ||
execution_status: { | ||
status: mockedRule.executionStatus.status, | ||
last_execution_date: mockedRule.executionStatus.lastExecutionDate, | ||
}, | ||
actions: [ | ||
{ | ||
group: mockedRule.actions[0].group, | ||
id: mockedRule.actions[0].id, | ||
params: mockedRule.actions[0].params, | ||
connector_type_id: mockedRule.actions[0].actionTypeId, | ||
}, | ||
], | ||
outcome: 'aliasMatch', | ||
}; | ||
|
||
it('resolves a rule with proper parameters', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
resolveRuleRoute(router, licenseState); | ||
const [config, handler] = router.get.mock.calls[0]; | ||
|
||
expect(config.path).toMatchInlineSnapshot(`"/internal/alerting/rule/{id}/_resolve"`); | ||
|
||
rulesClient.resolve.mockResolvedValueOnce(mockedRule); | ||
|
||
const [context, req, res] = mockHandlerArguments( | ||
{ rulesClient }, | ||
{ | ||
params: { id: '1' }, | ||
}, | ||
['ok'] | ||
); | ||
await handler(context, req, res); | ||
|
||
expect(rulesClient.resolve).toHaveBeenCalledTimes(1); | ||
expect(rulesClient.resolve.mock.calls[0][0].id).toEqual('1'); | ||
|
||
expect(res.ok).toHaveBeenCalledWith({ | ||
body: resolveResult, | ||
}); | ||
}); | ||
|
||
it('ensures the license allows resolving rules', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
resolveRuleRoute(router, licenseState); | ||
|
||
const [, handler] = router.get.mock.calls[0]; | ||
|
||
rulesClient.resolve.mockResolvedValueOnce(mockedRule); | ||
|
||
const [context, req, res] = mockHandlerArguments( | ||
{ rulesClient }, | ||
{ | ||
params: { id: '1' }, | ||
}, | ||
['ok'] | ||
); | ||
|
||
await handler(context, req, res); | ||
|
||
expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); | ||
}); | ||
|
||
it('ensures the license check prevents getting rules', async () => { | ||
const licenseState = licenseStateMock.create(); | ||
const router = httpServiceMock.createRouter(); | ||
|
||
(verifyApiAccess as jest.Mock).mockImplementation(() => { | ||
throw new Error('OMG'); | ||
}); | ||
|
||
resolveRuleRoute(router, licenseState); | ||
|
||
const [, handler] = router.get.mock.calls[0]; | ||
|
||
rulesClient.resolve.mockResolvedValueOnce(mockedRule); | ||
|
||
const [context, req, res] = mockHandlerArguments( | ||
{ rulesClient }, | ||
{ | ||
params: { id: '1' }, | ||
}, | ||
['ok'] | ||
); | ||
|
||
expect(handler(context, req, res)).rejects.toMatchInlineSnapshot(`[Error: OMG]`); | ||
|
||
expect(verifyApiAccess).toHaveBeenCalledWith(licenseState); | ||
}); | ||
}); |
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,84 @@ | ||
/* | ||
* 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 { omit } from 'lodash'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { IRouter } from 'kibana/server'; | ||
import { ILicenseState } from '../lib'; | ||
import { verifyAccessAndContext, RewriteResponseCase } from './lib'; | ||
import { | ||
AlertTypeParams, | ||
AlertingRequestHandlerContext, | ||
INTERNAL_BASE_ALERTING_API_PATH, | ||
ResolvedSanitizedRule, | ||
} from '../types'; | ||
|
||
const paramSchema = schema.object({ | ||
id: schema.string(), | ||
}); | ||
|
||
const rewriteBodyRes: RewriteResponseCase<ResolvedSanitizedRule<AlertTypeParams>> = ({ | ||
alertTypeId, | ||
createdBy, | ||
updatedBy, | ||
createdAt, | ||
updatedAt, | ||
apiKeyOwner, | ||
notifyWhen, | ||
muteAll, | ||
mutedInstanceIds, | ||
executionStatus, | ||
actions, | ||
scheduledTaskId, | ||
...rest | ||
}) => ({ | ||
...rest, | ||
rule_type_id: alertTypeId, | ||
created_by: createdBy, | ||
updated_by: updatedBy, | ||
created_at: createdAt, | ||
updated_at: updatedAt, | ||
api_key_owner: apiKeyOwner, | ||
notify_when: notifyWhen, | ||
mute_all: muteAll, | ||
muted_alert_ids: mutedInstanceIds, | ||
scheduled_task_id: scheduledTaskId, | ||
execution_status: executionStatus && { | ||
...omit(executionStatus, 'lastExecutionDate'), | ||
last_execution_date: executionStatus.lastExecutionDate, | ||
}, | ||
actions: actions.map(({ group, id, actionTypeId, params }) => ({ | ||
group, | ||
id, | ||
params, | ||
connector_type_id: actionTypeId, | ||
})), | ||
}); | ||
|
||
export const resolveRuleRoute = ( | ||
router: IRouter<AlertingRequestHandlerContext>, | ||
licenseState: ILicenseState | ||
) => { | ||
router.get( | ||
{ | ||
path: `${INTERNAL_BASE_ALERTING_API_PATH}/rule/{id}/_resolve`, | ||
validate: { | ||
params: paramSchema, | ||
}, | ||
}, | ||
router.handleLegacyErrors( | ||
verifyAccessAndContext(licenseState, async function (context, req, res) { | ||
const rulesClient = context.alerting.getRulesClient(); | ||
const { id } = req.params; | ||
const rule = await rulesClient.resolve({ id }); | ||
return res.ok({ | ||
body: rewriteBodyRes(rule), | ||
}); | ||
}) | ||
) | ||
); | ||
}; |
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
Oops, something went wrong.