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] Export rules and connectors #98802

Merged
merged 17 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
6 changes: 6 additions & 0 deletions docs/management/action-types.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ New connectors can be created by clicking the *Create connector* button, which w
[role="screenshot"]
image::images/connector-select-type.png[Connector select type]

[float]
[[importing-and-exporting-connectors]]
=== Importing and exporting connectors

Use the <<managing-saved-objects, Saved Objects Management UI>> to import and export connectors.
ymao1 marked this conversation as resolved.
Show resolved Hide resolved

[float]
[[create-connectors]]
=== Preconfigured connectors
Expand Down
6 changes: 6 additions & 0 deletions docs/user/alerting/rule-management.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ These operations can also be performed in bulk by multi-selecting rules and clic
[role="screenshot"]
image:images/bulk-mute-disable.png[The Manage rules button lets you mute/unmute, enable/disable, and delete in bulk]

[float]
[[importing-and-exporting-rules]]
=== Importing and exporting rules

Use the <<managing-saved-objects, Saved Objects Management UI>> to import and export rules.
ymao1 marked this conversation as resolved.
Show resolved Hide resolved

[float]
=== Required permissions

Expand Down
13 changes: 12 additions & 1 deletion x-pack/plugins/actions/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
* 2.0.
*/

import { SavedObject, SavedObjectsServiceSetup } from 'kibana/server';
import {
SavedObject,
SavedObjectsExportTransformContext,
SavedObjectsServiceSetup,
} from 'kibana/server';
import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server';
import mappings from './mappings.json';
import { getMigrations } from './migrations';
import { RawAction } from '../types';
import { getImportResultMessage, GO_TO_CONNECTORS_BUTTON_LABLE } from './get_import_result_message';
import { transformConnectorsForExport } from './transform_connectors_for_export';

export const ACTION_SAVED_OBJECT_TYPE = 'action';
export const ALERT_SAVED_OBJECT_TYPE = 'alert';
Expand All @@ -32,6 +37,12 @@ export function setupSavedObjects(
getTitle(obj) {
return `Connector: [${obj.attributes.name}]`;
},
onExport<RawAction>(
context: SavedObjectsExportTransformContext,
objects: Array<SavedObject<RawAction>>
Comment on lines +42 to +44
Copy link
Contributor

Choose a reason for hiding this comment

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

(Referring to the getTitle function on top, as comments are not allowed on non-diff lines...)

Ideally, we shouldn't need the object's type to be displayed in its associated title, as it introduces redundancy in some parts of the SOM UI.

Now, the main SOM table is only using an (EUI) icon to display the type associated with each object. Is there an icon associated with rules and connectors in the EUI library? If so, you could add them via the management.icon property of the types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pgayvallet Discussed with @mdefazio and we currently don't have dedicated icons for rules or connectors. If we're using the default icon, should we leave Rules: and Connectors: in the title?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess so. This is low impact and can be changed later anyway, so I think it's fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for being flexible. Our concern is associating icons with Rules and Connectors that may also be used elsewhere for different concepts. The viz type icons were created specifically for those types and aren't used for other things (or shouldn't be).

If we want an icon, we will just need to go the route of creating on specifically for this. However, this type column may quickly turn into a lot of different icons and likely need the title to be shown alongside them anyway.

So let's circle back to it in the future and determine if we want to only show the title.

) {
return transformConnectorsForExport(objects);
},
onImport(connectors) {
return {
warnings: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* 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 { transformConnectorsForExport } from './transform_connectors_for_export';

describe('transform connector for export', () => {
const connectorsWithNoSecrets = [
{
id: '1',
type: 'action',
attributes: {
actionTypeId: '.email',
name: 'email connector without auth',
isMissingSecrets: false,
config: {
hasAuth: false,
from: 'me@me.com',
host: 'host',
port: 22,
service: null,
secure: null,
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '2',
type: 'action',
attributes: {
actionTypeId: '.index',
name: 'index connector',
isMissingSecrets: false,
config: {
index: 'test-index',
refresh: false,
executionTimeField: null,
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '3',
type: 'action',
attributes: {
actionTypeId: '.server-log',
name: 'server log connector',
isMissingSecrets: false,
config: {},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '4',
type: 'action',
attributes: {
actionTypeId: '.webhook',
name: 'webhook connector without auth',
isMissingSecrets: false,
config: {
method: 'post',
hasAuth: false,
url: 'https://webhook',
headers: {},
},
secrets: 'asbqw4tqbef',
},
references: [],
},
];
const connectorsWithSecrets = [
{
id: '1',
type: 'action',
attributes: {
actionTypeId: '.email',
name: 'email connector with auth',
isMissingSecrets: false,
config: {
hasAuth: true,
from: 'me@me.com',
host: 'host',
port: 22,
service: null,
secure: null,
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '2',
type: 'action',
attributes: {
actionTypeId: '.resilient',
name: 'resilient connector',
isMissingSecrets: false,
config: {
apiUrl: 'https://resilient',
orgId: 'origId',
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '3',
type: 'action',
attributes: {
actionTypeId: '.servicenow',
name: 'servicenow itsm connector',
isMissingSecrets: false,
config: {
apiUrl: 'https://servicenow',
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '4',
type: 'action',
attributes: {
actionTypeId: '.pagerduty',
name: 'pagerduty connector',
isMissingSecrets: false,
config: {
apiUrl: 'https://pagerduty',
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '5',
type: 'action',
attributes: {
actionTypeId: '.jira',
name: 'jira connector',
isMissingSecrets: false,
config: {
apiUrl: 'https://jira',
projectKey: 'foo',
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '6',
type: 'action',
attributes: {
actionTypeId: '.teams',
name: 'teams connector',
isMissingSecrets: false,
config: {},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '7',
type: 'action',
attributes: {
actionTypeId: '.slack',
name: 'slack connector',
isMissingSecrets: false,
config: {},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '8',
type: 'action',
attributes: {
actionTypeId: '.servicenow-sir',
name: 'servicenow sir connector',
isMissingSecrets: false,
config: {
apiUrl: 'https://servicenow-sir',
},
secrets: 'asbqw4tqbef',
},
references: [],
},
{
id: '8',
type: 'action',
attributes: {
actionTypeId: '.webhook',
name: 'webhook connector with auth',
isMissingSecrets: false,
config: {
method: 'post',
hasAuth: true,
url: 'https://webhook',
headers: {},
},
secrets: 'asbqw4tqbef',
},
references: [],
},
];

it('should not change connectors without secrets', () => {
expect(transformConnectorsForExport(connectorsWithNoSecrets)).toEqual(connectorsWithNoSecrets);
});

it('should remove secrets for connectors with secrets', () => {
expect(transformConnectorsForExport(connectorsWithSecrets)).toEqual(
connectorsWithSecrets.map((connector) => ({
...connector,
attributes: {
...connector.attributes,
isMissingSecrets: true,
},
}))
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 { SavedObject } from 'kibana/server';
import { RawAction } from '../types';

const CONNECTORS_WITHOUT_SECRETS = ['.index', '.server-log'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe here we can replace the constants with the dynamic approach to check the action type schema?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@YulNaumenko Great idea! I updated this in this commit 58dfb8a

I ran into the issue where the email and webhook secrets validation does not take into account whether hasAuth is set to false inside the connector parameters so in this case, if the secrets validation fails, I'm still checking for the existence of hasAuth. Do you think that works?

const CONNECTORS_CHECK_AUTH = ['.email', '.webhook'];

export function transformConnectorsForExport(
connectors: SavedObject[]
): Array<SavedObject<RawAction>> {
return connectors.map((connector) =>
transformConnectorForExport(connector as SavedObject<RawAction>)
);
}

function connectorHasNoAuth(connector: SavedObject<RawAction>) {
return connector?.attributes?.config?.hasAuth === false;
}

function transformConnectorForExport(connector: SavedObject<RawAction>): SavedObject<RawAction> {
// Skip connectors with no secrets
if (CONNECTORS_WITHOUT_SECRETS.includes(connector.attributes.actionTypeId)) {
return connector;
}

// Skip connectors where hasAuth = false
if (
CONNECTORS_CHECK_AUTH.includes(connector.attributes.actionTypeId) &&
connectorHasNoAuth(connector)
) {
return connector;
}

// Skip connectors
return {
...connector,
attributes: {
...connector.attributes,
isMissingSecrets: true,
},
};
}
20 changes: 18 additions & 2 deletions x-pack/plugins/alerting/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
* 2.0.
*/

import { SavedObjectsServiceSetup } from 'kibana/server';
import {
SavedObject,
SavedObjectsExportTransformContext,
SavedObjectsServiceSetup,
} from 'kibana/server';
import mappings from './mappings.json';
import { getMigrations } from './migrations';
import { EncryptedSavedObjectsPluginSetup } from '../../../encrypted_saved_objects/server';

import { transformRulesForExport } from './transform_rule_for_export';
export { partiallyUpdateAlert } from './partially_update_alert';

export const AlertAttributesExcludedFromAAD = [
Expand Down Expand Up @@ -43,6 +47,18 @@ export function setupSavedObjects(
namespaceType: 'single',
migrations: getMigrations(encryptedSavedObjects),
mappings: mappings.alert,
management: {
importableAndExportable: true,
getTitle(obj) {
return `Rule: [${obj.attributes.name}]`;
},
onExport<RawAlert>(
context: SavedObjectsExportTransformContext,
objects: Array<SavedObject<RawAlert>>
) {
return transformRulesForExport(objects);
},
},
});

savedObjects.registerType({
Expand Down
Loading