Skip to content

Commit

Permalink
Prepare alerts and actions for feature controls (#52956) (#53542)
Browse files Browse the repository at this point in the history
* Save consumer with alert

* Make consumer mandatory
  • Loading branch information
mikecote authored Dec 19, 2019
1 parent 4836f75 commit c8bc7fc
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/alerting/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
}
}
},
"consumer": {
"type": "keyword"
},
"actions": {
"type": "nested",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/alerting/server/alerts_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function getMockData(overwrites: Record<string, any> = {}) {
name: 'abc',
tags: ['foo'],
alertTypeId: '123',
consumer: 'bar',
schedule: { interval: '10s' },
throttle: null,
params: {
Expand Down Expand Up @@ -184,6 +185,7 @@ describe('create()', () => {
"alertTypeId": "123",
"apiKey": undefined,
"apiKeyOwner": undefined,
"consumer": "bar",
"createdBy": "elastic",
"enabled": true,
"muteAll": false,
Expand Down Expand Up @@ -795,6 +797,7 @@ describe('create()', () => {
},
],
alertTypeId: '123',
consumer: 'bar',
name: 'abc',
params: { bar: true },
apiKey: Buffer.from('123:abc').toString('base64'),
Expand Down
3 changes: 3 additions & 0 deletions x-pack/legacy/plugins/alerting/server/routes/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ server.route(createAlertRoute);

const mockedAlert = {
alertTypeId: '1',
consumer: 'bar',
name: 'abc',
schedule: { interval: '10s' },
tags: ['foo'],
Expand Down Expand Up @@ -64,6 +65,7 @@ test('creates an alert with proper parameters', async () => {
},
],
"alertTypeId": "1",
"consumer": "bar",
"id": "123",
"name": "abc",
"params": Object {
Expand Down Expand Up @@ -92,6 +94,7 @@ test('creates an alert with proper parameters', async () => {
},
],
"alertTypeId": "1",
"consumer": "bar",
"enabled": true,
"name": "abc",
"params": Object {
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/alerting/server/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface ScheduleRequest extends Hapi.Request {
name: string;
tags: string[];
alertTypeId: string;
consumer: string;
schedule: IntervalSchedule;
actions: Array<{
group: string;
Expand Down Expand Up @@ -43,6 +44,7 @@ export const createAlertRoute = {
.items(Joi.string())
.default([]),
alertTypeId: Joi.string().required(),
consumer: Joi.string().required(),
throttle: getDurationSchema().default(null),
schedule: Joi.object()
.keys({
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface Alert {
name: string;
tags: string[];
alertTypeId: string;
consumer: string;
schedule: IntervalSchedule;
actions: AlertAction[];
params: Record<string, any>;
Expand All @@ -87,6 +88,7 @@ export interface RawAlert extends SavedObjectAttributes {
name: string;
tags: string[];
alertTypeId: string;
consumer: string;
schedule: SavedObjectAttributes;
actions: RawAlertAction[];
params: SavedObjectAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export const getResult = (): RuleAlertType => ({
name: 'Detect Root/Admin Users',
tags: [`${INTERNAL_RULE_ID_KEY}:rule-1`, `${INTERNAL_IMMUTABLE_KEY}:false`],
alertTypeId: 'siem.signals',
consumer: 'siem',
params: {
createdAt: '2019-12-13T16:40:33.400Z',
updatedAt: '2019-12-13T16:40:33.400Z',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SIGNALS_ID } from '../../../../common/constants';
import { APP_ID, SIGNALS_ID } from '../../../../common/constants';
import { RuleParams } from './types';
import { addTags } from './add_tags';

Expand Down Expand Up @@ -42,6 +42,7 @@ export const createRules = async ({
name,
tags: addTags(tags, ruleId, immutable),
alertTypeId: SIGNALS_ID,
consumer: APP_ID,
params: {
createdAt: new Date().toISOString(),
description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export class AlertUtils {
throttle: '1m',
tags: [],
alertTypeId: 'test.always-firing',
consumer: 'bar',
params: {
index: ES_TEST_INDEX_NAME,
reference,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function getTestAlertData(overwrites = {}) {
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'bar',
schedule: { interval: '1m' },
throttle: '1m',
actions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
break;
case 'space_1_all at space1':
case 'superuser at space1':
expect(response.statusCode).to.eql(200);
// Wait until alerts scheduled actions 3 times before disabling the alert and waiting for tasks to finish
await esTestIndexTool.waitForDocs('alert:test.always-firing', reference, 3);
await alertUtils.disable(response.body.id);
Expand Down Expand Up @@ -582,6 +583,7 @@ export default function alertTests({ getService }: FtrProviderContext) {
break;
case 'space_1_all at space1':
case 'superuser at space1':
expect(response.statusCode).to.eql(200);
// Actions should execute twice before widning things down
await esTestIndexTool.waitForDocs('action:test.index-record', reference, 2);
await alertUtils.disable(response.body.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
],
enabled: true,
alertTypeId: 'test.noop',
consumer: 'bar',
params: {},
createdBy: user.username,
schedule: { interval: '1m' },
Expand Down Expand Up @@ -201,10 +202,10 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
statusCode: 400,
error: 'Bad Request',
message:
'child "name" fails because ["name" is required]. child "alertTypeId" fails because ["alertTypeId" is required]. child "schedule" fails because ["schedule" is required]. child "params" fails because ["params" is required]. child "actions" fails because ["actions" is required]',
'child "name" fails because ["name" is required]. child "alertTypeId" fails because ["alertTypeId" is required]. child "consumer" fails because ["consumer" is required]. child "schedule" fails because ["schedule" is required]. child "params" fails because ["params" is required]. child "actions" fails because ["actions" is required]',
validation: {
source: 'payload',
keys: ['name', 'alertTypeId', 'schedule', 'params', 'actions'],
keys: ['name', 'alertTypeId', 'consumer', 'schedule', 'params', 'actions'],
},
});
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function createFindTests({ getService }: FtrProviderContext) {
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'bar',
schedule: { interval: '1m' },
enabled: true,
actions: [],
Expand Down Expand Up @@ -138,6 +139,7 @@ export default function createFindTests({ getService }: FtrProviderContext) {
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'bar',
schedule: { interval: '1m' },
enabled: false,
actions: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default function createGetTests({ getService }: FtrProviderContext) {
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'bar',
schedule: { interval: '1m' },
enabled: true,
actions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
...updatedData,
id: createdAlert.id,
alertTypeId: 'test.noop',
consumer: 'bar',
createdBy: 'elastic',
enabled: true,
updatedBy: user.username,
Expand Down Expand Up @@ -274,7 +275,13 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
.put(`${getUrlPrefix(space.id)}/api/alert/1`)
.set('kbn-xsrf', 'foo')
.auth(user.username, user.password)
.send(getTestAlertData({ schedule: { interval: '10x' }, enabled: undefined }));
.send(
getTestAlertData({
schedule: { interval: '10x' },
enabled: undefined,
consumer: undefined,
})
);

switch (scenario.id) {
case 'no_kibana_privileges at space1':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
],
enabled: true,
alertTypeId: 'test.noop',
consumer: 'bar',
params: {},
createdBy: null,
schedule: { interval: '1m' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function createFindTests({ getService }: FtrProviderContext) {
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'bar',
schedule: { interval: '1m' },
enabled: true,
actions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function createGetTests({ getService }: FtrProviderContext) {
name: 'abc',
tags: ['foo'],
alertTypeId: 'test.noop',
consumer: 'bar',
schedule: { interval: '1m' },
enabled: true,
actions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function createUpdateTests({ getService }: FtrProviderContext) {
id: createdAlert.id,
tags: ['bar'],
alertTypeId: 'test.noop',
consumer: 'bar',
createdBy: null,
enabled: true,
updatedBy: null,
Expand Down

0 comments on commit c8bc7fc

Please sign in to comment.