Skip to content

Commit

Permalink
Implemented actions server API for supporting preconfigured connectors (
Browse files Browse the repository at this point in the history
#62382)

* Implemented actions server API for supporting preconfigured connectors defined in kibana.yaml

* Fixed type check

* Fixed due to comments and extended functional tests

* Fixed tests and renamed connectors

* fixed jest tests

* Fixed type checks

* Fixed failing alert save

* Fixed alert client tests

* fixed type checks

* Fixed language check error

* Fixed jest tests

* Added missing comments and docs

* fixed due to comments

* Fixed json config for preconfigured

* fixed type check, reverted config

* config experiment with json stringify

* revert experiment

* Removed the spaces from connector names in config
  • Loading branch information
YulNaumenko authored Apr 8, 2020
1 parent 18c3f75 commit 730dcbf
Show file tree
Hide file tree
Showing 69 changed files with 1,050 additions and 701 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const AlertsConfiguration: React.FC<AlertsConfigurationProps> = (
async function fetchEmailActions() {
const kibanaActions = await kfetch({
method: 'GET',
pathname: `/api/action/_find`,
pathname: `/api/action/_getAll`,
});

const actions = kibanaActions.data.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('Step1', () => {
actionTypeId: '1abc',
name: 'Testing',
config: {},
isPreconfigured: false,
},
];
const selectedEmailActionId = emailActions[0].id;
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('Step1', () => {
actionTypeId: '.email',
name: '',
config: {},
isPreconfigured: false,
},
],
selectedEmailActionId: NEW_ACTION_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { isEmpty } from 'lodash/fp';
import {
CasesConnectorsFindResult,
Connector,
CasesConfigurePatch,
CasesConfigureResponse,
CasesConfigureRequest,
Expand All @@ -18,7 +18,7 @@ import { ApiProps } from '../types';
import { convertToCamelCase, decodeCaseConfigureResponse } from '../utils';
import { CaseConfigure } from './types';

export const fetchConnectors = async ({ signal }: ApiProps): Promise<CasesConnectorsFindResult> => {
export const fetchConnectors = async ({ signal }: ApiProps): Promise<Connector[]> => {
const response = await KibanaServices.get().http.fetch(
`${CASES_CONFIGURE_URL}/connectors/_find`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useConnectors = (): ReturnConnectors => {
const res = await fetchConnectors({ signal: abortCtrl.signal });
if (!didCancel) {
setLoading(false);
setConnectors(res.data);
setConnectors(res);
}
} catch (error) {
if (!didCancel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const connectors: Connector[] = [
id: '123',
actionTypeId: '.servicenow',
name: 'My Connector',
isPreconfigured: false,
config: {
apiUrl: 'https://instance1.service-now.com',
casesConfiguration: {
Expand Down Expand Up @@ -48,6 +49,7 @@ export const connectors: Connector[] = [
id: '456',
actionTypeId: '.servicenow',
name: 'My Connector 2',
isPreconfigured: false,
config: {
apiUrl: 'https://instance2.service-now.com',
casesConfiguration: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ export const createActionResult = (): ActionResult => ({
actionTypeId: 'action-id-1',
name: '',
config: {},
isPreconfigured: false,
});

export const nonRuleAlert = () => ({
Expand Down Expand Up @@ -518,6 +519,7 @@ export const updateActionResult = (): ActionResult => ({
actionTypeId: 'action-id-1',
name: '',
config: {},
isPreconfigured: false,
});

export const getMockPrivilegesResult = () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ set -e
# https:/elastic/kibana/blob/master/x-pack/legacy/plugins/actions/README.md#get-apiaction_find-find-actions
curl -s -k \
-u ${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD} \
-X GET ${KIBANA_URL}${SPACE_URL}/api/action/_find \
-X GET ${KIBANA_URL}${SPACE_URL}/api/action/_getAll \
| jq .
11 changes: 7 additions & 4 deletions x-pack/plugins/actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Table of Contents
- [RESTful API](#restful-api)
- [`POST /api/action`: Create action](#post-apiaction-create-action)
- [`DELETE /api/action/{id}`: Delete action](#delete-apiactionid-delete-action)
- [`GET /api/action/_find`: Find actions](#get-apiactionfind-find-actions)
- [`GET /api/action/_getAll`: Get all actions](#get-apiaction-get-all-actions)
- [`GET /api/action/{id}`: Get action](#get-apiactionid-get-action)
- [`GET /api/action/types`: List action types](#get-apiactiontypes-list-action-types)
- [`PUT /api/action/{id}`: Update action](#put-apiactionid-update-action)
Expand Down Expand Up @@ -92,6 +92,7 @@ Built-In-Actions are configured using the _xpack.actions_ namespoace under _kiba
| _xpack.actions._**enabled** | Feature toggle which enabled Actions in Kibana. | boolean |
| _xpack.actions._**whitelistedHosts** | Which _hostnames_ are whitelisted for the Built-In-Action? This list should contain hostnames of every external service you wish to interact with using Webhooks, Email or any other built in Action. Note that you may use the string "\*" in place of a specific hostname to enable Kibana to target any URL, but keep in mind the potential use of such a feature to execute [SSRF](https://www.owasp.org/index.php/Server_Side_Request_Forgery) attacks from your server. | Array<String> |
| _xpack.actions._**enabledActionTypes** | A list of _actionTypes_ id's that are enabled. A "\*" may be used as an element to indicate all registered actionTypes should be enabled. The actionTypes registered for Kibana are `.server-log`, `.slack`, `.email`, `.index`, `.pagerduty`, `.webhook`. Default: `["*"]` | Array<String> |
| _xpack.actions._**preconfigured** | A list of preconfigured actions. Default: `[]` | Array<Object> |

#### Whitelisting Built-in Action Types

Expand Down Expand Up @@ -174,11 +175,13 @@ Params:
| -------- | --------------------------------------------- | ------ |
| id | The id of the action you're trying to delete. | string |

### `GET /api/action/_find`: Find actions
### `GET /api/action/_getAll`: Get all actions

Params:
No parameters.

See the [saved objects API documentation for find](https://www.elastic.co/guide/en/kibana/master/saved-objects-api-find.html). All the properties are the same except that you cannot pass in `type`.
Return all actions from saved objects merged with predefined list.
Use the [saved objects API for find](https://www.elastic.co/guide/en/kibana/master/saved-objects-api-find.html) with the proprties: `type: 'action'` and `perPage: 10000`.
List of predefined actions should be set up in Kibana.yaml.

### `GET /api/action/{id}`: Get action

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/actions/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export interface ActionResult {
actionTypeId: string;
name: string;
config: Record<string, any>;
isPreconfigured: boolean;
}
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/actions_client.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const createActionsClientMock = () => {
const mocked: jest.Mocked<ActionsClientContract> = {
create: jest.fn(),
get: jest.fn(),
find: jest.fn(),
delete: jest.fn(),
update: jest.fn(),
getAll: jest.fn(),
};
return mocked;
};
Expand Down
94 changes: 78 additions & 16 deletions x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ beforeEach(() => {
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [],
});
});

Expand Down Expand Up @@ -83,6 +84,7 @@ describe('create()', () => {
});
expect(result).toEqual({
id: '1',
isPreconfigured: false,
name: 'my name',
actionTypeId: 'my-action-type',
config: {},
Expand Down Expand Up @@ -178,6 +180,7 @@ describe('create()', () => {
});
expect(result).toEqual({
id: '1',
isPreconfigured: false,
name: 'my name',
actionTypeId: 'my-action-type',
config: {
Expand Down Expand Up @@ -226,6 +229,7 @@ describe('create()', () => {
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [],
});

const savedObjectCreateResult = {
Expand Down Expand Up @@ -305,6 +309,7 @@ describe('get()', () => {
const result = await actionsClient.get({ id: '1' });
expect(result).toEqual({
id: '1',
isPreconfigured: false,
});
expect(savedObjectsClient.get).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.get.mock.calls[0]).toMatchInlineSnapshot(`
Expand All @@ -314,9 +319,44 @@ describe('get()', () => {
]
`);
});

test('return predefined action with id', async () => {
actionsClient = new ActionsClient({
actionTypeRegistry,
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [
{
id: 'testPreconfigured',
actionTypeId: '.slack',
secrets: {
test: 'test1',
},
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
},
],
});

const result = await actionsClient.get({ id: 'testPreconfigured' });
expect(result).toEqual({
id: 'testPreconfigured',
actionTypeId: '.slack',
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
});
expect(savedObjectsClient.get).not.toHaveBeenCalled();
});
});

describe('find()', () => {
describe('getAll()', () => {
test('calls savedObjectsClient with parameters', async () => {
const expectedResult = {
total: 1,
Expand All @@ -327,6 +367,7 @@ describe('find()', () => {
id: '1',
type: 'type',
attributes: {
name: 'test',
config: {
foo: 'bar',
},
Expand All @@ -339,31 +380,50 @@ describe('find()', () => {
scopedClusterClient.callAsInternalUser.mockResolvedValueOnce({
aggregations: {
'1': { doc_count: 6 },
testPreconfigured: { doc_count: 2 },
},
});
const result = await actionsClient.find({});
expect(result).toEqual({
total: 1,
perPage: 10,
page: 1,
data: [

actionsClient = new ActionsClient({
actionTypeRegistry,
savedObjectsClient,
scopedClusterClient,
defaultKibanaIndex,
preconfiguredActions: [
{
id: '1',
id: 'testPreconfigured',
actionTypeId: '.slack',
secrets: {},
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
referencedByCount: 6,
},
],
});
expect(savedObjectsClient.find).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.find.mock.calls[0]).toMatchInlineSnapshot(`
Array [
Object {
"type": "action",
const result = await actionsClient.getAll();
expect(result).toEqual([
{
id: '1',
isPreconfigured: false,
name: 'test',
config: {
foo: 'bar',
},
]
`);
referencedByCount: 6,
},
{
id: 'testPreconfigured',
actionTypeId: '.slack',
isPreconfigured: true,
name: 'test',
config: {
foo: 'bar',
},
referencedByCount: 2,
},
]);
});
});

Expand Down Expand Up @@ -420,6 +480,7 @@ describe('update()', () => {
});
expect(result).toEqual({
id: 'my-action',
isPreconfigured: false,
actionTypeId: 'my-action-type',
name: 'my name',
config: {},
Expand Down Expand Up @@ -524,6 +585,7 @@ describe('update()', () => {
});
expect(result).toEqual({
id: 'my-action',
isPreconfigured: false,
actionTypeId: 'my-action-type',
name: 'my name',
config: {
Expand Down
Loading

0 comments on commit 730dcbf

Please sign in to comment.