Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Payload:
|tags|A list of keywords to reference and search in the future.|string[]|
|alertTypeId|The id value of the alert type you want to call when the alert is scheduled to execute.|string|
|interval|The interval in seconds, minutes, hours or days the alert should execute. Example: `10s`, `5m`, `1h`, `1d`.|string|
|alertTypeParams|The parameters to pass in to the alert type executor `params` value. This will also validate against the alert type params validator if defined.|object|
|params|The parameters to pass in to the alert type executor `params` value. This will also validate against the alert type params validator if defined.|object|
|actions|Array of the following:<br> - `group` (string): We support grouping actions in the scenario of escalations or different types of alert instances. If you don't need this, feel free to use `default` as a value.<br>- `id` (string): The id of the action saved object to execute.<br>- `params` (object): The map to the `params` the action type will receive. In order to help apply context to strings, we handle them as mustache templates and pass in a default set of context. (see templating actions).|array|

#### `DELETE /api/alert/{id}`: Delete alert
Expand Down Expand Up @@ -246,7 +246,7 @@ Payload:
|interval|The interval in seconds, minutes, hours or days the alert should execute. Example: `10s`, `5m`, `1h`, `1d`.|string|
|name|A name to reference and search in the future.|string|
|tags|A list of keywords to reference and search in the future.|string[]|
|alertTypeParams|The parameters to pass in to the alert type executor `params` value. This will also validate against the alert type params validator if defined.|object|
|params|The parameters to pass in to the alert type executor `params` value. This will also validate against the alert type params validator if defined.|object|
|actions|Array of the following:<br> - `group` (string): We support grouping actions in the scenario of escalations or different types of alert instances. If you don't need this, feel free to use `default` as a value.<br>- `id` (string): The id of the action saved object to execute.<br>- `params` (object): There map to the `params` the action type will receive. In order to help apply context to strings, we handle them as mustache templates and pass in a default set of context. (see templating actions).|array|

#### `POST /api/alert/{id}/_enable`: Enable an alert
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/alerting/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
}
}
},
"alertTypeParams": {
"params": {
"enabled": false,
"type": "object"
},
Expand Down
298 changes: 149 additions & 149 deletions x-pack/legacy/plugins/alerting/server/alerts_client.test.ts

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions x-pack/legacy/plugins/alerting/server/alerts_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ interface UpdateOptions {
tags: string[];
interval: string;
actions: AlertAction[];
alertTypeParams: Record<string, any>;
params: Record<string, any>;
};
}

Expand Down Expand Up @@ -111,7 +111,7 @@ export class AlertsClient {
public async create({ data, options }: CreateOptions) {
// Throws an error if alert type isn't registered
const alertType = this.alertTypeRegistry.get(data.alertTypeId);
const validatedAlertTypeParams = validateAlertTypeParams(alertType, data.alertTypeParams);
const validatedAlertTypeParams = validateAlertTypeParams(alertType, data.params);
const apiKey = await this.createAPIKey();
const username = await this.getUserName();

Expand All @@ -125,7 +125,7 @@ export class AlertsClient {
apiKey: apiKey.created
? Buffer.from(`${apiKey.result.id}:${apiKey.result.api_key}`).toString('base64')
: undefined,
alertTypeParams: validatedAlertTypeParams,
params: validatedAlertTypeParams,
muteAll: false,
mutedInstanceIds: [],
});
Expand Down Expand Up @@ -199,7 +199,7 @@ export class AlertsClient {
const apiKey = await this.createAPIKey();

// Validate
const validatedAlertTypeParams = validateAlertTypeParams(alertType, data.alertTypeParams);
const validatedAlertTypeParams = validateAlertTypeParams(alertType, data.params);
this.validateActions(alertType, data.actions);

const { actions, references } = this.extractReferences(data.actions);
Expand All @@ -210,7 +210,7 @@ export class AlertsClient {
{
...attributes,
...data,
alertTypeParams: validatedAlertTypeParams,
params: validatedAlertTypeParams,
actions,
updatedBy: username,
apiKeyOwner: apiKey.created ? username : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const mockedAlertTypeSavedObject = {
alertTypeId: '123',
interval: '10s',
mutedInstanceIds: [],
alertTypeParams: {
params: {
bar: true,
},
actions: [
Expand Down Expand Up @@ -253,7 +253,7 @@ test('validates params before executing the alert type', async () => {
references: [],
});
await expect(taskRunner.run()).rejects.toThrowErrorMatchingInlineSnapshot(
`"alertTypeParams invalid: [param1]: expected value of type [string] but got [undefined]"`
`"params invalid: [param1]: expected value of type [string] but got [undefined]"`
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export class TaskRunnerFactory {
const services = getServices(fakeRequest);
// Ensure API key is still valid and user has access
const {
attributes: { alertTypeParams, actions, interval, throttle, muteAll, mutedInstanceIds },
attributes: { params, actions, interval, throttle, muteAll, mutedInstanceIds },
references,
} = await services.savedObjectsClient.get<RawAlert>('alert', alertId);

// Validate
const validatedAlertTypeParams = validateAlertTypeParams(alertType, alertTypeParams);
const validatedAlertTypeParams = validateAlertTypeParams(alertType, params);

// Inject ids into actions
const actionsWithIds = actions.map(action => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ test('should validate and throw error when params is invalid', () => {
{}
)
).toThrowErrorMatchingInlineSnapshot(
`"alertTypeParams invalid: [param1]: expected value of type [string] but got [undefined]"`
`"params invalid: [param1]: expected value of type [string] but got [undefined]"`
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export function validateAlertTypeParams<T extends Record<string, any>>(
try {
return validator.validate(params);
} catch (err) {
throw Boom.badRequest(`alertTypeParams invalid: ${err.message}`);
throw Boom.badRequest(`params invalid: ${err.message}`);
}
}
20 changes: 10 additions & 10 deletions x-pack/legacy/plugins/alerting/server/routes/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const mockedAlert = {
name: 'abc',
interval: '10s',
tags: ['foo'],
alertTypeParams: {
params: {
bar: true,
},
actions: [
Expand Down Expand Up @@ -57,12 +57,12 @@ test('creates an alert with proper parameters', async () => {
},
],
"alertTypeId": "1",
"alertTypeParams": Object {
"bar": true,
},
"id": "123",
"interval": "10s",
"name": "abc",
"params": Object {
"bar": true,
},
"tags": Array [
"foo",
],
Expand All @@ -83,12 +83,12 @@ test('creates an alert with proper parameters', async () => {
},
],
"alertTypeId": "1",
"alertTypeParams": Object {
"bar": true,
},
"enabled": true,
"interval": "10s",
"name": "abc",
"params": Object {
"bar": true,
},
"tags": Array [
"foo",
],
Expand All @@ -112,12 +112,12 @@ test('creates an alert with proper parameters', async () => {
},
],
"alertTypeId": "1",
"alertTypeParams": Object {
"bar": true,
},
"enabled": true,
"interval": "10s",
"name": "abc",
"params": Object {
"bar": true,
},
"tags": Array [
"foo",
],
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/server/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ScheduleRequest extends Hapi.Request {
alertTypeId: string;
interval: string;
actions: AlertAction[];
alertTypeParams: Record<string, any>;
params: Record<string, any>;
throttle: string | null;
};
}
Expand All @@ -41,7 +41,7 @@ export const createAlertRoute = {
alertTypeId: Joi.string().required(),
throttle: getDurationSchema().default(null),
interval: getDurationSchema().required(),
alertTypeParams: Joi.object().required(),
params: Joi.object().required(),
actions: Joi.array()
.items(
Joi.object().keys({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/alerting/server/routes/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const mockedAlert = {
id: '1',
alertTypeId: '1',
interval: '10s',
alertTypeParams: {
params: {
bar: true,
},
actions: [
Expand Down
10 changes: 5 additions & 5 deletions x-pack/legacy/plugins/alerting/server/routes/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const mockedResponse = {
alertTypeId: '1',
tags: ['foo'],
interval: '12s',
alertTypeParams: {
params: {
otherField: false,
},
actions: [
Expand All @@ -40,7 +40,7 @@ test('calls the update function with proper parameters', async () => {
name: 'abc',
tags: ['bar'],
interval: '12s',
alertTypeParams: {
params: {
otherField: false,
},
actions: [
Expand Down Expand Up @@ -74,11 +74,11 @@ test('calls the update function with proper parameters', async () => {
},
},
],
"alertTypeParams": Object {
"otherField": false,
},
"interval": "12s",
"name": "abc",
"params": Object {
"otherField": false,
},
"tags": Array [
"bar",
],
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/server/routes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface UpdateRequest extends Hapi.Request {
tags: string[];
interval: string;
actions: AlertAction[];
alertTypeParams: Record<string, any>;
params: Record<string, any>;
throttle: string | null;
};
}
Expand All @@ -43,7 +43,7 @@ export const updateAlertRoute = {
.items(Joi.string())
.required(),
interval: getDurationSchema().required(),
alertTypeParams: Joi.object().required(),
params: Joi.object().required(),
actions: Joi.array()
.items(
Joi.object().keys({
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface Alert {
alertTypeId: string;
interval: string;
actions: AlertAction[];
alertTypeParams: Record<string, any>;
params: Record<string, any>;
scheduledTaskId?: string;
createdBy: string | null;
updatedBy: string | null;
Expand All @@ -83,7 +83,7 @@ export interface RawAlert extends SavedObjectAttributes {
alertTypeId: string;
interval: string;
actions: RawAlertAction[];
alertTypeParams: SavedObjectAttributes;
params: SavedObjectAttributes;
scheduledTaskId?: string;
createdBy: string | null;
updatedBy: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const createSignals = async ({
name,
tags: [],
alertTypeId: SIGNALS_ID,
alertTypeParams: {
params: {
description,
ruleId,
index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ describe('read_signals', () => {
test('should return a single value of rule-1 with multiple values', async () => {
const result1 = getResult();
result1.id = '4baa53f8-96da-44ee-ad58-41bccb7f9f3d';
result1.alertTypeParams.ruleId = 'rule-1';
result1.params.ruleId = 'rule-1';

const result2 = getResult();
result2.id = '5baa53f8-96da-44ee-ad58-41bccb7f9f3d';
result2.alertTypeParams.ruleId = 'rule-2';
result2.params.ruleId = 'rule-2';

const alertsClient = alertsClientMock.create();
alertsClient.get.mockResolvedValue(getResult());
Expand All @@ -150,11 +150,11 @@ describe('read_signals', () => {
test('should return a single value of rule-2 with multiple values', async () => {
const result1 = getResult();
result1.id = '4baa53f8-96da-44ee-ad58-41bccb7f9f3d';
result1.alertTypeParams.ruleId = 'rule-1';
result1.params.ruleId = 'rule-1';

const result2 = getResult();
result2.id = '5baa53f8-96da-44ee-ad58-41bccb7f9f3d';
result2.alertTypeParams.ruleId = 'rule-2';
result2.params.ruleId = 'rule-2';

const alertsClient = alertsClientMock.create();
alertsClient.get.mockResolvedValue(getResult());
Expand All @@ -171,11 +171,11 @@ describe('read_signals', () => {
test('should return null for a made up value with multiple values', async () => {
const result1 = getResult();
result1.id = '4baa53f8-96da-44ee-ad58-41bccb7f9f3d';
result1.alertTypeParams.ruleId = 'rule-1';
result1.params.ruleId = 'rule-1';

const result2 = getResult();
result2.id = '5baa53f8-96da-44ee-ad58-41bccb7f9f3d';
result2.alertTypeParams.ruleId = 'rule-2';
result2.params.ruleId = 'rule-2';

const alertsClient = alertsClientMock.create();
alertsClient.get.mockResolvedValue(getResult());
Expand All @@ -194,8 +194,8 @@ describe('read_signals', () => {
test('returns null if the objects are not of a signal rule type', () => {
const signal = findSignalInArrayByRuleId(
[
{ alertTypeId: 'made up 1', alertTypeParams: { ruleId: '123' } },
{ alertTypeId: 'made up 2', alertTypeParams: { ruleId: '456' } },
{ alertTypeId: 'made up 1', params: { ruleId: '123' } },
{ alertTypeId: 'made up 2', params: { ruleId: '456' } },
],
'123'
);
Expand All @@ -205,30 +205,30 @@ describe('read_signals', () => {
test('returns correct type if the objects are of a signal rule type', () => {
const signal = findSignalInArrayByRuleId(
[
{ alertTypeId: SIGNALS_ID, alertTypeParams: { ruleId: '123' } },
{ alertTypeId: 'made up 2', alertTypeParams: { ruleId: '456' } },
{ alertTypeId: SIGNALS_ID, params: { ruleId: '123' } },
{ alertTypeId: 'made up 2', params: { ruleId: '456' } },
],
'123'
);
expect(signal).toEqual({ alertTypeId: 'siem.signals', alertTypeParams: { ruleId: '123' } });
expect(signal).toEqual({ alertTypeId: 'siem.signals', params: { ruleId: '123' } });
});

test('returns second correct type if the objects are of a signal rule type', () => {
const signal = findSignalInArrayByRuleId(
[
{ alertTypeId: SIGNALS_ID, alertTypeParams: { ruleId: '123' } },
{ alertTypeId: SIGNALS_ID, alertTypeParams: { ruleId: '456' } },
{ alertTypeId: SIGNALS_ID, params: { ruleId: '123' } },
{ alertTypeId: SIGNALS_ID, params: { ruleId: '456' } },
],
'456'
);
expect(signal).toEqual({ alertTypeId: 'siem.signals', alertTypeParams: { ruleId: '456' } });
expect(signal).toEqual({ alertTypeId: 'siem.signals', params: { ruleId: '456' } });
});

test('returns null with correct types but data does not exist', () => {
const signal = findSignalInArrayByRuleId(
[
{ alertTypeId: SIGNALS_ID, alertTypeParams: { ruleId: '123' } },
{ alertTypeId: SIGNALS_ID, alertTypeParams: { ruleId: '456' } },
{ alertTypeId: SIGNALS_ID, params: { ruleId: '123' } },
{ alertTypeId: SIGNALS_ID, params: { ruleId: '456' } },
],
'892'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const findSignalInArrayByRuleId = (
if (isAlertTypeArray(objects)) {
const signals: SignalAlertType[] = objects;
const signal: SignalAlertType[] = signals.filter(datum => {
return datum.alertTypeParams.ruleId === ruleId;
return datum.params.ruleId === ruleId;
});
if (signal.length !== 0) {
return signal[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export type AlertTypeParams = Omit<SignalAlertParams, 'name' | 'enabled' | 'inte

export type SignalAlertType = Alert & {
id: string;
alertTypeParams: AlertTypeParams;
params: AlertTypeParams;
};

export interface SignalsRequest extends RequestFacade {
Expand Down
Loading