Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Alerting] Preconfigured alert history index connector #94909

Merged
merged 62 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
f055c2a
Adding preconfigured alert history index
ymao1 Mar 16, 2021
3ac2315
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 16, 2021
59146f6
Adding functions to build alert history document
ymao1 Mar 17, 2021
97d9815
Adding functions to build alert history document
ymao1 Mar 17, 2021
3ecfa2a
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 18, 2021
1b028a7
Moving index template creation to plugin start
ymao1 Mar 18, 2021
f2b34d6
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 18, 2021
b03e052
Adding unit tests
ymao1 Mar 18, 2021
61948fd
Adding unit tests
ymao1 Mar 18, 2021
3fa6846
Adding unit tests
ymao1 Mar 18, 2021
b9ad6c3
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 22, 2021
4b1b787
Simplifying
ymao1 Mar 22, 2021
957c333
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 22, 2021
4167d49
Revert "Merge branch 'master' of https://github.com/elastic/kibana in…
ymao1 Mar 23, 2021
9b4eda6
Reverting some changes
ymao1 Mar 23, 2021
6c30518
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 23, 2021
3a25517
Reverting some changes
ymao1 Mar 23, 2021
24f2f2d
Adding index override
ymao1 Mar 23, 2021
1ce3a36
Updating UI with index override
ymao1 Mar 23, 2021
22d5a59
Only allow indexOverride for preconfigured alert history connector
ymao1 Mar 23, 2021
2554887
Handling preconfigured connector id clashes
ymao1 Mar 23, 2021
2af2999
Cleanup
ymao1 Mar 23, 2021
ac4248d
UI unit tests
ymao1 Mar 24, 2021
92531a7
Fixing default schema shown in UI
ymao1 Mar 24, 2021
fd94545
Fixing functional tests
ymao1 Mar 24, 2021
c8b44ee
Adding functional test
ymao1 Mar 24, 2021
5a88ab5
Fixing functional tests
ymao1 Mar 24, 2021
f75a958
Adding docs and link to docs
ymao1 Mar 24, 2021
6d769e8
Adding config to docker allowlist
ymao1 Mar 24, 2021
7363eb3
Fixing wrong typescript operator
ymao1 Mar 24, 2021
c5060ee
Changing default for config to false
ymao1 Mar 24, 2021
0d4acb0
Cleanup
ymao1 Mar 24, 2021
56848f9
Adding note about index privileges to docs
ymao1 Mar 24, 2021
bbe5fd6
Fixing i18n
ymao1 Mar 24, 2021
85ccb96
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 24, 2021
7444b08
PR fixes
ymao1 Mar 29, 2021
776e8df
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 29, 2021
af35a12
PR fixes
ymao1 Mar 29, 2021
6f420b1
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Mar 29, 2021
8bc04c3
Merge branch 'master' into alerting/default-es-index-schema
kibanamachine Mar 29, 2021
07eb28d
PR fixes
ymao1 Apr 1, 2021
79b282c
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Apr 1, 2021
f3fea18
PR fixes - wording
ymao1 Apr 1, 2021
19f3673
PR fixes
ymao1 Apr 1, 2021
dcbf451
Fixing unit and functional tests
ymao1 Apr 1, 2021
f689618
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Apr 2, 2021
75281b0
Fixing types check
ymao1 Apr 2, 2021
030bf55
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Apr 7, 2021
5aedddb
ES -> Elasticsearch
ymao1 Apr 7, 2021
75dcb88
Moving files
ymao1 Apr 7, 2021
095b495
Adding kibana- to beginning of prefix
ymao1 Apr 7, 2021
094aa60
Namespacing alert data within schema with kibana
ymao1 Apr 7, 2021
da82858
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Apr 7, 2021
9e42c60
Fix i18n
ymao1 Apr 7, 2021
3f6c078
Updating docs
ymao1 Apr 7, 2021
55e75a5
Fixing unit tests
ymao1 Apr 7, 2021
38e5519
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Apr 7, 2021
1a17d94
Fixing doc links
ymao1 Apr 7, 2021
8923efa
Merging in master
ymao1 Apr 8, 2021
45278e2
Fixing types check
ymao1 Apr 8, 2021
fa502cc
PR fixes
ymao1 Apr 8, 2021
b23fca9
Merge branch 'master' of https://github.com/elastic/kibana into alert…
ymao1 Apr 8, 2021
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
6 changes: 5 additions & 1 deletion docs/settings/alert-action-settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ You can configure the following settings in the `kibana.yml` file.
+
Disabled action types will not appear as an option when creating new connectors, but existing connectors and actions of that type will remain in {kib} and will not function.

| `xpack.actions`
`.preconfiguredAlertHistoryEsIndex` {ess-icon}
| Enables a preconfigured alert history {es} <<index-action-type, Index>> connector. Defaults to `false`.

| `xpack.actions.preconfigured`
| Specifies preconfigured action IDs and configs. Defaults to {}.
| Specifies preconfigured connector IDs and configs. Defaults to {}.

| `xpack.actions.proxyUrl` {ess-icon}
| Specifies the proxy URL to use, if using a proxy for actions. By default, no proxy is used.
Expand Down
35 changes: 35 additions & 0 deletions docs/user/alerting/action-types/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,38 @@ PUT test
}
}
--------------------------------------------------

[float]
[[preconfigured-connector-alert-history]]
=== Alert history preconfigured {es} index connector
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This title is a little hard to read. Is the word "preconfigured" needed?

Alert history {es} index connector


experimental[] {kib} offers a preconfigured index connector to facilitate indexing active alert data into {es}.

[WARNING]
==================================================
This functionality is experimental and may be changed or removed completely in a future release.
==================================================

To use this connector, set the <<action-settings, `xpack.actions.preconfiguredAlertHistoryEsIndex`>> configuration to `true`.

```js
xpack.actions.preconfiguredAlertHistoryEsIndex: true
```

When creating a new rule, add an <<index-action-type, Index action>> and select the `Alert history Elasticsearch index (preconfigured)` connector.

[role="screenshot"]
image::images/pre-configured-alert-history-connector.png[Select pre-configured alert history connectors]

Documents are indexed using a preconfigured schema that captures the <<defining-alerts-actions-variables, action variables>> available for the rule. By default, these documents are indexed into the `kibana-alert-history-default` index, but you can specify a different index. Index names must start with `kibana-alert-history-` to take advantage of the preconfigured alert history index template.

[IMPORTANT]
==============================================
To write documents to the preconfigured index, you must have `all` or `write` privileges to the `kibana-alert-history-*` indices. Refer to <<xpack-kibana-role-management>> for more information.
==============================================

[NOTE]
==================================================
The `kibana-alert-history-*` indices are not configured to use ILM so they must be maintained manually. If the index size grows large,
consider using the {ref}/docs-delete-by-query.html[delete by query] API to clean up older documents in the index.
==================================================
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ two out-of-the box connectors: <<slack-action-type, Slack>> and <<webhook-action
Sensitive properties, such as passwords, can also be stored in the <<creating-keystore, {kib} keystore>>.
==============================================

[float]
[[build-in-preconfigured-connectors]]
==== Built-in preconfigured connectors

{kib} provides one built-in preconfigured connector:

* <<preconfigured-connector-alert-history, Alert history preconfigured {es} index connector>>

[float]
[[managing-pre-configured-connectors]]
==== View preconfigured connectors
Expand All @@ -63,4 +71,4 @@ image::images/pre-configured-connectors-managing.png[Connectors managing tab wit
Clicking a preconfigured connector shows the description, but not the configuration. A message indicates that this is a preconfigured connector.

[role="screenshot"]
image::images/pre-configured-connectors-view-screen.png[Pre-configured connector view details]
image::images/pre-configured-connectors-view-screen.png[Pre-configured connector view details]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/core/public/doc_links/doc_links_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export class DocLinksService {
indexThreshold: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/rule-type-index-threshold.html`,
pagerDutyAction: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/pagerduty-action-type.html`,
preconfiguredConnectors: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/pre-configured-connectors.html`,
preconfiguredAlertHistoryConnector: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/index-action-type.html#preconfigured-connector-alert-history`,
serviceNowAction: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/servicenow-action-type.html#configuring-servicenow`,
setupPrerequisites: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/alerting-getting-started.html#alerting-setup-prerequisites`,
slackAction: `${ELASTIC_WEBSITE_URL}guide/en/kibana/${DOC_LINK_VERSION}/slack-action-type.html#configuring-slack`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ kibana_vars=(
xpack.actions.allowedHosts
xpack.actions.enabled
xpack.actions.enabledActionTypes
xpack.actions.preconfiguredAlertHistoryEsIndex
xpack.actions.preconfigured
xpack.actions.proxyHeaders
xpack.actions.proxyRejectUnauthorizedCertificates
Expand Down
122 changes: 122 additions & 0 deletions x-pack/plugins/actions/common/alert_history_schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 { buildAlertHistoryDocument } from './alert_history_schema';

function getVariables(overrides = {}) {
return {
date: '2021-01-01T00:00:00.000Z',
rule: {
id: 'rule-id',
name: 'rule-name',
type: 'rule-type',
spaceId: 'space-id',
},
context: {
contextVar1: 'contextValue1',
contextVar2: 'contextValue2',
},
params: {
ruleParam: 1,
ruleParamString: 'another param',
},
tags: ['abc', 'def'],
alert: {
id: 'alert-id',
actionGroup: 'action-group-id',
actionGroupName: 'Action Group',
},
...overrides,
};
}

describe('buildAlertHistoryDocument', () => {
it('handles empty variables', () => {
expect(buildAlertHistoryDocument({})).toBeNull();
});

it('returns null if rule type is not defined', () => {
expect(buildAlertHistoryDocument(getVariables({ rule: { type: undefined } }))).toBeNull();
});

it('returns null if alert variables are not defined', () => {
expect(buildAlertHistoryDocument(getVariables({ alert: undefined }))).toBeNull();
});

it('returns null if rule variables are not defined', () => {
expect(buildAlertHistoryDocument(getVariables({ rule: undefined }))).toBeNull();
});

it('includes @timestamp field if date is null', () => {
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ date: undefined }));
expect(alertHistoryDoc).not.toBeNull();
expect(alertHistoryDoc!['@timestamp']).toBeTruthy();
});

it(`doesn't include context if context is empty`, () => {
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ context: {} }));
expect(alertHistoryDoc).not.toBeNull();
expect(alertHistoryDoc!.kibana?.alert?.context).toBeFalsy();
});

it(`doesn't include params if params is empty`, () => {
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ params: {} }));
expect(alertHistoryDoc).not.toBeNull();
expect(alertHistoryDoc!.rule?.params).toBeFalsy();
});

it(`doesn't include tags if tags is empty array`, () => {
const alertHistoryDoc = buildAlertHistoryDocument(getVariables({ tags: [] }));
expect(alertHistoryDoc).not.toBeNull();
expect(alertHistoryDoc!.tags).toBeFalsy();
});

it(`included message if context contains message`, () => {
const alertHistoryDoc = buildAlertHistoryDocument(
getVariables({
context: { contextVar1: 'contextValue1', contextVar2: 'contextValue2', message: 'hello!' },
})
);
expect(alertHistoryDoc).not.toBeNull();
expect(alertHistoryDoc!.message).toEqual('hello!');
});

it('builds alert history document from variables', () => {
expect(buildAlertHistoryDocument(getVariables())).toEqual({
'@timestamp': '2021-01-01T00:00:00.000Z',
kibana: {
alert: {
actionGroup: 'action-group-id',
actionGroupName: 'Action Group',
context: {
'rule-type': {
contextVar1: 'contextValue1',
contextVar2: 'contextValue2',
},
},
id: 'alert-id',
},
},
event: {
kind: 'alert',
},
rule: {
id: 'rule-id',
name: 'rule-name',
params: {
'rule-type': {
ruleParam: 1,
ruleParamString: 'another param',
},
},
space: 'space-id',
type: 'rule-type',
},
tags: ['abc', 'def'],
});
});
});
90 changes: 90 additions & 0 deletions x-pack/plugins/actions/common/alert_history_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 { isEmpty } from 'lodash';

export const ALERT_HISTORY_PREFIX = 'kibana-alert-history-';
export const AlertHistoryDefaultIndexName = `${ALERT_HISTORY_PREFIX}default`;
export const AlertHistoryEsIndexConnectorId = 'preconfigured-alert-history-es-index';

export const buildAlertHistoryDocument = (variables: Record<string, unknown>) => {
const { date, alert: alertVariables, context, params, tags, rule: ruleVariables } = variables as {
date: string;
alert: Record<string, unknown>;
context: Record<string, unknown>;
params: Record<string, unknown>;
rule: Record<string, unknown>;
tags: string[];
};

if (!alertVariables || !ruleVariables) {
return null;
}

const { actionGroup, actionGroupName, id: alertId } = alertVariables as {
actionGroup: string;
actionGroupName: string;
id: string;
};

const { id: ruleId, name, spaceId, type } = ruleVariables as {
id: string;
name: string;
spaceId: string;
type: string;
};

if (!type) {
// can't build the document without a type
return null;
}

const ruleType = type.replace(/\./g, '__');

const rule = {
...(ruleId ? { id: ruleId } : {}),
...(name ? { name } : {}),
...(!isEmpty(params) ? { params: { [ruleType]: params } } : {}),
...(spaceId ? { space: spaceId } : {}),
...(type ? { type } : {}),
};
const alert = {
...(alertId ? { id: alertId } : {}),
...(!isEmpty(context) ? { context: { [ruleType]: context } } : {}),
...(actionGroup ? { actionGroup } : {}),
...(actionGroupName ? { actionGroupName } : {}),
};

const alertHistoryDoc = {
'@timestamp': date ? date : new Date().toISOString(),
...(tags && tags.length > 0 ? { tags } : {}),
...(context?.message ? { message: context.message } : {}),
...(!isEmpty(rule) ? { rule } : {}),
...(!isEmpty(alert) ? { kibana: { alert } } : {}),
};

return !isEmpty(alertHistoryDoc) ? { ...alertHistoryDoc, event: { kind: 'alert' } } : null;
};

export const AlertHistoryDocumentTemplate = Object.freeze(
buildAlertHistoryDocument({
rule: {
id: '{{rule.id}}',
name: '{{rule.name}}',
type: '{{rule.type}}',
spaceId: '{{rule.spaceId}}',
},
context: '{{context}}',
params: '{{params}}',
tags: '{{rule.tags}}',
alert: {
id: '{{alert.id}}',
actionGroup: '{{alert.actionGroup}}',
actionGroupName: '{{alert.actionGroupName}}',
},
})
);
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

export * from './types';
export * from './alert_history_schema';
export * from './rewrite_request_case';

export const BASE_ACTION_API_PATH = '/api/actions';

export * from './rewrite_request_case';
1 change: 1 addition & 0 deletions x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ describe('create()', () => {
enabled: true,
enabledActionTypes: ['some-not-ignored-action-type'],
allowedHosts: ['*'],
preconfiguredAlertHistoryEsIndex: false,
preconfigured: {},
proxyRejectUnauthorizedCertificates: true,
rejectUnauthorized: true,
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/actions/server/actions_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultActionsConfig: ActionsConfig = {
enabled: false,
allowedHosts: [],
enabledActionTypes: [],
preconfiguredAlertHistoryEsIndex: false,
preconfigured: {},
proxyRejectUnauthorizedCertificates: true,
rejectUnauthorized: true,
Expand Down
Loading