Skip to content

Commit

Permalink
Merge branch 'actions/license-checks' of github.com:elastic/kibana in…
Browse files Browse the repository at this point in the history
…to actions/license-checks
  • Loading branch information
mikecote committed Mar 5, 2020
2 parents cae5286 + 52c06ca commit 0a50516
Show file tree
Hide file tree
Showing 28 changed files with 117 additions and 120 deletions.
26 changes: 16 additions & 10 deletions x-pack/plugins/actions/server/action_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
*/

import { taskManagerMock } from '../../task_manager/server/task_manager.mock';
import { ActionTypeRegistry } from './action_type_registry';
import { ActionTypeRegistry, ActionTypeRegistryOpts } from './action_type_registry';
import { ExecutorType } from './types';
import { ActionExecutor, ExecutorError, TaskRunnerFactory } from './lib';
import { configUtilsMock } from './actions_config.mock';
import { actionsConfigMock } from './actions_config.mock';
import { ActionsConfigurationUtilities } from './actions_config';

const mockTaskManager = taskManagerMock.setup();
const actionTypeRegistryParams = {
taskManager: mockTaskManager,
taskRunnerFactory: new TaskRunnerFactory(
new ActionExecutor({ isESOUsingEphemeralEncryptionKey: false })
),
actionsConfigUtils: configUtilsMock,
};
let mockedActionsConfig: jest.Mocked<ActionsConfigurationUtilities>;
let actionTypeRegistryParams: ActionTypeRegistryOpts;

beforeEach(() => jest.resetAllMocks());
beforeEach(() => {
jest.resetAllMocks();
mockedActionsConfig = actionsConfigMock.create();
actionTypeRegistryParams = {
taskManager: mockTaskManager,
taskRunnerFactory: new TaskRunnerFactory(
new ActionExecutor({ isESOUsingEphemeralEncryptionKey: false })
),
actionsConfigUtils: mockedActionsConfig,
};
});

const executor: ExecutorType = async options => {
return { status: 'ok', actionId: options.actionId };
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/action_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ActionType } from './types';
import { ActionType as CommonActionType } from '../common';
import { ActionsConfigurationUtilities } from './actions_config';

interface ConstructorOptions {
export interface ActionTypeRegistryOpts {
taskManager: TaskManagerSetupContract;
taskRunnerFactory: TaskRunnerFactory;
actionsConfigUtils: ActionsConfigurationUtilities;
Expand All @@ -24,7 +24,7 @@ export class ActionTypeRegistry {
private readonly taskRunnerFactory: TaskRunnerFactory;
private readonly actionsConfigUtils: ActionsConfigurationUtilities;

constructor(constructorParams: ConstructorOptions) {
constructor(constructorParams: ActionTypeRegistryOpts) {
this.taskManager = constructorParams.taskManager;
this.taskRunnerFactory = constructorParams.taskRunnerFactory;
this.actionsConfigUtils = constructorParams.actionsConfigUtils;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ActionsClient } from './actions_client';
import { ExecutorType } from './types';
import { ActionExecutor, TaskRunnerFactory } from './lib';
import { taskManagerMock } from '../../task_manager/server/task_manager.mock';
import { configUtilsMock } from './actions_config.mock';
import { actionsConfigMock } from './actions_config.mock';
import { getActionsConfigurationUtilities } from './actions_config';

import {
Expand All @@ -30,7 +30,7 @@ const actionTypeRegistryParams = {
taskRunnerFactory: new TaskRunnerFactory(
new ActionExecutor({ isESOUsingEphemeralEncryptionKey: false })
),
actionsConfigUtils: configUtilsMock,
actionsConfigUtils: actionsConfigMock.create(),
};

let actionsClient: ActionsClient;
Expand Down
21 changes: 14 additions & 7 deletions x-pack/plugins/actions/server/actions_config.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@

import { ActionsConfigurationUtilities } from './actions_config';

export const configUtilsMock: ActionsConfigurationUtilities = {
isWhitelistedHostname: _ => true,
isWhitelistedUri: _ => true,
isActionTypeEnabled: _ => true,
ensureWhitelistedHostname: _ => {},
ensureWhitelistedUri: _ => {},
ensureActionTypeEnabled: _ => {},
const createActionsConfigMock = () => {
const mocked: jest.Mocked<ActionsConfigurationUtilities> = {
isWhitelistedHostname: jest.fn().mockReturnValue(true),
isWhitelistedUri: jest.fn().mockReturnValue(true),
isActionTypeEnabled: jest.fn().mockReturnValue(true),
ensureWhitelistedHostname: jest.fn().mockReturnValue({}),
ensureWhitelistedUri: jest.fn().mockReturnValue({}),
ensureActionTypeEnabled: jest.fn().mockReturnValue({}),
};
return mocked;
};

export const actionsConfigMock = {
create: createActionsConfigMock,
};
11 changes: 4 additions & 7 deletions x-pack/plugins/actions/server/builtin_action_types/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Logger } from '../../../../../src/core/server';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';

import { ActionType, ActionTypeExecutorOptions } from '../types';
import { configUtilsMock } from '../actions_config.mock';
import { actionsConfigMock } from '../actions_config.mock';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { createActionTypeRegistry } from './index.test';
import { sendEmail } from './lib/send_email';
Expand All @@ -37,13 +37,10 @@ const services = {
let actionType: ActionType;
let mockedLogger: jest.Mocked<Logger>;

beforeAll(() => {
const { actionTypeRegistry } = createActionTypeRegistry();
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
});

beforeEach(() => {
jest.resetAllMocks();
const { actionTypeRegistry } = createActionTypeRegistry();
actionType = actionTypeRegistry.get(ACTION_TYPE_ID);
});

describe('actionTypeRegistry.get() works', () => {
Expand Down Expand Up @@ -128,7 +125,7 @@ describe('config validation', () => {
actionType = getActionType({
logger: mockedLogger,
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
isWhitelistedHostname: hostname => hostname === NODEMAILER_AOL_SERVICE_HOST,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { taskManagerMock } from '../../../task_manager/server/task_manager.mock'
import { registerBuiltInActionTypes } from './index';
import { Logger } from '../../../../../src/core/server';
import { loggingServiceMock } from '../../../../../src/core/server/mocks';
import { configUtilsMock } from '../actions_config.mock';
import { actionsConfigMock } from '../actions_config.mock';

const ACTION_TYPE_IDS = ['.index', '.email', '.pagerduty', '.server-log', '.slack', '.webhook'];

Expand All @@ -24,12 +24,12 @@ export function createActionTypeRegistry(): {
taskRunnerFactory: new TaskRunnerFactory(
new ActionExecutor({ isESOUsingEphemeralEncryptionKey: false })
),
actionsConfigUtils: configUtilsMock,
actionsConfigUtils: actionsConfigMock.create(),
});
registerBuiltInActionTypes({
logger,
actionTypeRegistry,
actionsConfigUtils: configUtilsMock,
actionsConfigUtils: actionsConfigMock.create(),
});
return { logger, actionTypeRegistry };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { postPagerduty } from './lib/post_pagerduty';
import { createActionTypeRegistry } from './index.test';
import { Logger } from '../../../../../src/core/server';
import { configUtilsMock } from '../actions_config.mock';
import { actionsConfigMock } from '../actions_config.mock';

const postPagerdutyMock = postPagerduty as jest.Mock;

Expand Down Expand Up @@ -60,7 +60,7 @@ describe('validateConfig()', () => {
actionType = getActionType({
logger: mockedLogger,
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedUri: url => {
expect(url).toEqual('https://events.pagerduty.com/v2/enqueue');
},
Expand All @@ -76,7 +76,7 @@ describe('validateConfig()', () => {
actionType = getActionType({
logger: mockedLogger,
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedUri: _ => {
throw new Error(`target url is not whitelisted`);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { validateConfig, validateSecrets, validateParams } from '../lib';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { postServiceNow } from './lib/post_servicenow';
import { createActionTypeRegistry } from './index.test';
import { configUtilsMock } from '../actions_config.mock';
import { actionsConfigMock } from '../actions_config.mock';

const postServiceNowMock = postServiceNow as jest.Mock;

Expand Down Expand Up @@ -70,7 +70,7 @@ describe('validateConfig()', () => {
test('should validate and pass when the servicenow url is whitelisted', () => {
actionType = getActionType({
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedUri: url => {
expect(url).toEqual('https://events.servicenow.com/v2/enqueue');
},
Expand All @@ -85,7 +85,7 @@ describe('validateConfig()', () => {
test('config validation returns an error if the specified URL isnt whitelisted', () => {
actionType = getActionType({
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedUri: _ => {
throw new Error(`target url is not whitelisted`);
},
Expand Down
10 changes: 5 additions & 5 deletions x-pack/plugins/actions/server/builtin_action_types/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ActionType, Services, ActionTypeExecutorOptions } from '../types';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { validateParams, validateSecrets } from '../lib';
import { getActionType } from './slack';
import { configUtilsMock } from '../actions_config.mock';
import { actionsConfigMock } from '../actions_config.mock';

const ACTION_TYPE_ID = '.slack';

Expand All @@ -22,7 +22,7 @@ let actionType: ActionType;
beforeAll(() => {
actionType = getActionType({
async executor(options: ActionTypeExecutorOptions): Promise<any> {},
configurationUtilities: configUtilsMock,
configurationUtilities: actionsConfigMock.create(),
});
});

Expand Down Expand Up @@ -85,7 +85,7 @@ describe('validateActionTypeSecrets()', () => {
test('should validate and pass when the slack webhookUrl is whitelisted', () => {
actionType = getActionType({
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedUri: url => {
expect(url).toEqual('https://api.slack.com/');
},
Expand All @@ -100,7 +100,7 @@ describe('validateActionTypeSecrets()', () => {
test('config validation returns an error if the specified URL isnt whitelisted', () => {
actionType = getActionType({
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedHostname: url => {
throw new Error(`target hostname is not whitelisted`);
},
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('execute()', () => {

actionType = getActionType({
executor: mockSlackExecutor,
configurationUtilities: configUtilsMock,
configurationUtilities: actionsConfigMock.create(),
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { getActionType } from './webhook';
import { ActionType, Services } from '../types';
import { validateConfig, validateSecrets, validateParams } from '../lib';
import { savedObjectsClientMock } from '../../../../../src/core/server/mocks';
import { configUtilsMock } from '../actions_config.mock';
import { actionsConfigMock } from '../actions_config.mock';
import { createActionTypeRegistry } from './index.test';
import { Logger } from '../../../../../src/core/server';
import axios from 'axios';
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('config validation', () => {
actionType = getActionType({
logger: mockedLogger,
configurationUtilities: {
...configUtilsMock,
...actionsConfigMock.create(),
ensureWhitelistedUri: _ => {
throw new Error(`target url is not whitelisted`);
},
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('execute()', () => {
axiosRequestMock.mockReset();
actionType = getActionType({
logger: mockedLogger,
configurationUtilities: configUtilsMock,
configurationUtilities: actionsConfigMock.create(),
});
});

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/lib/license_api_access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

import Boom from 'boom';
import { LicenseState } from './license_state';
import { ILicenseState } from './license_state';

export function verifyApiAccess(licenseState: LicenseState) {
export function verifyApiAccess(licenseState: ILicenseState) {
const licenseCheckResults = licenseState.getLicenseInformation();

if (licenseCheckResults.showAppLink && licenseCheckResults.enableAppLink) {
Expand Down
41 changes: 13 additions & 28 deletions x-pack/plugins/actions/server/lib/license_state.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,20 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { of } from 'rxjs';
import { LicenseState } from './license_state';
import { LICENSE_CHECK_STATE, ILicense } from '../../../licensing/server';
import { ILicenseState } from './license_state';
import { LICENSE_CHECK_STATE } from '../../../licensing/server';

export const mockLicenseState = () => {
const license: ILicense = {
uid: '123',
status: 'active',
isActive: true,
signature: 'sig',
isAvailable: true,
toJSON: () => ({
signature: 'sig',
export const createLicenseStateMock = () => {
const licenseState: jest.Mocked<ILicenseState> = {
clean: jest.fn(),
getLicenseInformation: jest.fn(),
checkLicense: jest.fn().mockResolvedValue({
state: LICENSE_CHECK_STATE.Valid,
}),
getUnavailableReason: () => undefined,
hasAtLeast() {
return true;
},
check() {
return {
state: LICENSE_CHECK_STATE.Valid,
};
},
getFeature() {
return {
isAvailable: true,
isEnabled: true,
};
},
};
return new LicenseState(of(license));
return licenseState;
};

export const licenseStateMock = {
create: createLicenseStateMock,
};
2 changes: 2 additions & 0 deletions x-pack/plugins/actions/server/lib/license_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { assertNever } from '../../../../../src/core/utils';
import { ILicense, LICENSE_CHECK_STATE } from '../../../licensing/common/types';
import { PLUGIN } from '../constants/plugin';

export type ILicenseState = PublicMethodsOf<LicenseState>;

export interface ActionsLicenseInformation {
showAppLink: boolean;
enableAppLink: boolean;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/actions/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
listActionTypesRoute,
executeActionRoute,
} from './routes';
import { LicenseState } from './lib/license_state';
import { LicenseState, ILicenseState } from './lib/license_state';
import { IEventLogger, IEventLogService } from '../../event_log/server';

const EVENT_LOG_PROVIDER = 'actions';
Expand Down Expand Up @@ -88,7 +88,7 @@ export class ActionsPlugin implements Plugin<Promise<PluginSetupContract>, Plugi
private taskRunnerFactory?: TaskRunnerFactory;
private actionTypeRegistry?: ActionTypeRegistry;
private actionExecutor?: ActionExecutor;
private licenseState: LicenseState | null = null;
private licenseState: ILicenseState | null = null;
private spaces?: SpacesServiceSetup;
private eventLogger?: IEventLogger;
private isESOUsingEphemeralEncryptionKey?: boolean;
Expand Down
Loading

0 comments on commit 0a50516

Please sign in to comment.