2019-08-23 20:42:25 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
|
|
* or more contributor license agreements. Licensed under the Elastic License;
|
|
|
|
* you may not use this file except in compliance with the Elastic License.
|
|
|
|
*/
|
|
|
|
|
2020-02-06 20:47:41 +01:00
|
|
|
import { actionsClientMock } from './actions_client.mock';
|
Allow action types to perform their own mustache variable escaping in parameter templates (#83919)
resolves https://github.com/elastic/kibana/issues/79371
resolves https://github.com/elastic/kibana/issues/62928
In this PR, we allow action types to determine how to escape the
variables used in their parameters, when rendered as mustache
templates. Prior to this, action parameters were recursively
rendered as mustache templates using the default mustache
templating, by the alerts library. The default mustache
templating used html escaping.
Action types opt-in to the new capability via a new optional
method in the action type, `renderParameterTemplates()`. If not
provided, the previous recursive rendering is done, but now with
no escaping at all.
For #62928, changed the mustache template rendering to be
replaced with the error message, if an error occurred,
so at least you can now see that an error occurred. Useful
to diagnose problems with invalid mustache templates.
2020-12-15 02:41:13 +01:00
|
|
|
import { PluginSetupContract, PluginStartContract, renderActionParameterTemplates } from './plugin';
|
2020-04-27 19:46:04 +02:00
|
|
|
import { Services } from './types';
|
|
|
|
import {
|
|
|
|
elasticsearchServiceMock,
|
|
|
|
savedObjectsClientMock,
|
|
|
|
} from '../../../../src/core/server/mocks';
|
2020-07-22 15:45:57 +02:00
|
|
|
import { actionsAuthorizationMock } from './authorization/actions_authorization.mock';
|
|
|
|
export { actionsAuthorizationMock };
|
2020-02-06 20:47:41 +01:00
|
|
|
export { actionsClientMock };
|
|
|
|
|
|
|
|
const createSetupMock = () => {
|
|
|
|
const mock: jest.Mocked<PluginSetupContract> = {
|
|
|
|
registerType: jest.fn(),
|
|
|
|
};
|
|
|
|
return mock;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createStartMock = () => {
|
|
|
|
const mock: jest.Mocked<PluginStartContract> = {
|
2020-03-20 15:49:37 +01:00
|
|
|
isActionTypeEnabled: jest.fn(),
|
2020-04-24 23:38:28 +02:00
|
|
|
isActionExecutable: jest.fn(),
|
2020-02-06 20:47:41 +01:00
|
|
|
getActionsClientWithRequest: jest.fn().mockResolvedValue(actionsClientMock.create()),
|
2020-07-22 15:45:57 +02:00
|
|
|
getActionsAuthorizationWithRequest: jest
|
|
|
|
.fn()
|
|
|
|
.mockReturnValue(actionsAuthorizationMock.create()),
|
2020-04-08 18:54:42 +02:00
|
|
|
preconfiguredActions: [],
|
Allow action types to perform their own mustache variable escaping in parameter templates (#83919)
resolves https://github.com/elastic/kibana/issues/79371
resolves https://github.com/elastic/kibana/issues/62928
In this PR, we allow action types to determine how to escape the
variables used in their parameters, when rendered as mustache
templates. Prior to this, action parameters were recursively
rendered as mustache templates using the default mustache
templating, by the alerts library. The default mustache
templating used html escaping.
Action types opt-in to the new capability via a new optional
method in the action type, `renderParameterTemplates()`. If not
provided, the previous recursive rendering is done, but now with
no escaping at all.
For #62928, changed the mustache template rendering to be
replaced with the error message, if an error occurred,
so at least you can now see that an error occurred. Useful
to diagnose problems with invalid mustache templates.
2020-12-15 02:41:13 +01:00
|
|
|
renderActionParameterTemplates: jest.fn(),
|
2020-02-06 20:47:41 +01:00
|
|
|
};
|
|
|
|
return mock;
|
|
|
|
};
|
|
|
|
|
Allow action types to perform their own mustache variable escaping in parameter templates (#83919)
resolves https://github.com/elastic/kibana/issues/79371
resolves https://github.com/elastic/kibana/issues/62928
In this PR, we allow action types to determine how to escape the
variables used in their parameters, when rendered as mustache
templates. Prior to this, action parameters were recursively
rendered as mustache templates using the default mustache
templating, by the alerts library. The default mustache
templating used html escaping.
Action types opt-in to the new capability via a new optional
method in the action type, `renderParameterTemplates()`. If not
provided, the previous recursive rendering is done, but now with
no escaping at all.
For #62928, changed the mustache template rendering to be
replaced with the error message, if an error occurred,
so at least you can now see that an error occurred. Useful
to diagnose problems with invalid mustache templates.
2020-12-15 02:41:13 +01:00
|
|
|
// this is a default renderer that escapes nothing
|
|
|
|
export function renderActionParameterTemplatesDefault<RecordType>(
|
|
|
|
actionTypeId: string,
|
|
|
|
params: Record<string, unknown>,
|
|
|
|
variables: Record<string, unknown>
|
|
|
|
) {
|
|
|
|
return renderActionParameterTemplates(undefined, actionTypeId, params, variables);
|
|
|
|
}
|
|
|
|
|
2020-04-27 19:46:04 +02:00
|
|
|
const createServicesMock = () => {
|
2020-05-22 09:08:58 +02:00
|
|
|
const mock: jest.Mocked<
|
|
|
|
Services & {
|
|
|
|
savedObjectsClient: ReturnType<typeof savedObjectsClientMock.create>;
|
|
|
|
}
|
|
|
|
> = {
|
2020-07-07 13:38:17 +02:00
|
|
|
callCluster: elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser,
|
2020-07-14 16:48:24 +02:00
|
|
|
getLegacyScopedClusterClient: jest.fn(),
|
2020-04-27 19:46:04 +02:00
|
|
|
savedObjectsClient: savedObjectsClientMock.create(),
|
2020-10-20 15:54:48 +02:00
|
|
|
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
|
2020-04-27 19:46:04 +02:00
|
|
|
};
|
|
|
|
return mock;
|
|
|
|
};
|
|
|
|
|
2020-02-06 20:47:41 +01:00
|
|
|
export const actionsMock = {
|
2020-04-27 19:46:04 +02:00
|
|
|
createServices: createServicesMock,
|
2020-02-06 20:47:41 +01:00
|
|
|
createSetup: createSetupMock,
|
|
|
|
createStart: createStartMock,
|
|
|
|
};
|