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

License checks for alerts plugin #85649

Merged
merged 13 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const alertType: AlertType<
{ id: 'large', name: 'Large t-shirt' },
],
defaultActionGroupId: DEFAULT_ACTION_GROUP,
minimumLicenseRequired: 'basic',
async executor({
services,
params: { instances = DEFAULT_INSTANCES_TO_GENERATE, thresholds },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const alertType: AlertType = {
id: 'example.people-in-space',
name: 'People In Space Right Now',
actionGroups: [{ id: 'default', name: 'default' }],
minimumLicenseRequired: 'basic',
defaultActionGroupId: 'default',
recoveryActionGroup: {
id: 'hasLandedBackOnEarth',
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/action_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class ActionTypeRegistry {
minimumLicenseRequired: actionType.minimumLicenseRequired,
enabled: this.isActionTypeEnabled(actionTypeId),
enabledInConfig: this.actionsConfigUtils.isActionTypeEnabled(actionTypeId),
enabledInLicense: this.licenseState.isLicenseValidForActionType(actionType).isValid === true,
enabledInLicense: !!this.licenseState.isLicenseValidForActionType(actionType).isValid,
}));
}
}
2 changes: 2 additions & 0 deletions x-pack/plugins/alerts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ server.newPlatform.setup.plugins.alerts.registerType({
{ name: 'cpuUsage', description: 'CPU usage' },
],
},
minimumLicenseRequired: 'basic',
async executor({
alertId,
startedAt,
Expand Down Expand Up @@ -239,6 +240,7 @@ server.newPlatform.setup.plugins.alerts.registerType({
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
actionVariables: {
context: [
{ name: 'server', description: 'the server' },
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerts/common/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum AlertExecutionStatusErrorReasons {
Decrypt = 'decrypt',
Execute = 'execute',
Unknown = 'unknown',
License = 'license',
}

export interface AlertExecutionStatus {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/alerts/common/alert_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { LicenseType } from '../../licensing/common/types';

export interface AlertType {
id: string;
name: string;
Expand All @@ -12,6 +14,7 @@ export interface AlertType {
actionVariables: string[];
defaultActionGroupId: ActionGroup['id'];
producer: string;
minimumLicenseRequired: LicenseType;
}

export interface ActionGroup {
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/alerts/public/alert_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('loadAlertTypes', () => {
actionVariables: ['var1'],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
recoveryActionGroup: RecoveredActionGroup,
producer: 'alerts',
},
Expand All @@ -46,6 +47,7 @@ describe('loadAlertType', () => {
actionVariables: ['var1'],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
recoveryActionGroup: RecoveredActionGroup,
producer: 'alerts',
};
Expand All @@ -67,6 +69,7 @@ describe('loadAlertType', () => {
actionVariables: [],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
recoveryActionGroup: RecoveredActionGroup,
producer: 'alerts',
};
Expand All @@ -83,6 +86,7 @@ describe('loadAlertType', () => {
actionVariables: [],
actionGroups: [{ id: 'default', name: 'Default' }],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
recoveryActionGroup: RecoveredActionGroup,
producer: 'alerts',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const mockAlertType = (id: string): AlertType => ({
actionVariables: [],
defaultActionGroupId: 'default',
producer: 'alerts',
minimumLicenseRequired: 'basic',
});

describe('AlertNavigationRegistry', () => {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/alerts/server/alert_type_registry.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const createAlertTypeRegistryMock = () => {
register: jest.fn(),
get: jest.fn(),
list: jest.fn(),
ensureAlertTypeEnabled: jest.fn(),
};
return mocked;
};
Expand Down
89 changes: 76 additions & 13 deletions x-pack/plugins/alerts/server/alert_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@
*/

import { TaskRunnerFactory } from './task_runner';
import { AlertTypeRegistry } from './alert_type_registry';
import { AlertTypeRegistry, ConstructorOptions } from './alert_type_registry';
import { AlertType } from './types';
import { taskManagerMock } from '../../task_manager/server/mocks';
import { ILicenseState } from './lib/license_state';
import { licenseStateMock } from './lib/license_state.mock';
import { licensingMock } from '../../licensing/server/mocks';
let mockedLicenseState: jest.Mocked<ILicenseState>;
let alertTypeRegistryParams: ConstructorOptions;

const taskManager = taskManagerMock.createSetup();
const alertTypeRegistryParams = {
taskManager,
taskRunnerFactory: new TaskRunnerFactory(),
};

beforeEach(() => jest.resetAllMocks());
beforeEach(() => {
jest.resetAllMocks();
mockedLicenseState = licenseStateMock.create();
alertTypeRegistryParams = {
taskManager,
taskRunnerFactory: new TaskRunnerFactory(),
licenseState: mockedLicenseState,
licensing: licensingMock.createSetup(),
};
});

describe('has()', () => {
test('returns false for unregistered alert types', () => {
Expand All @@ -35,6 +45,7 @@ describe('has()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
});
Expand All @@ -44,7 +55,7 @@ describe('has()', () => {

describe('register()', () => {
test('throws if AlertType Id contains invalid characters', () => {
const alertType = {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionGroups: [
Expand All @@ -54,6 +65,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
};
Expand All @@ -75,7 +87,7 @@ describe('register()', () => {
});

test('throws if AlertType Id isnt a string', () => {
const alertType = {
const alertType: AlertType = {
id: (123 as unknown) as string,
name: 'Test',
actionGroups: [
Expand All @@ -85,6 +97,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
};
Expand All @@ -96,7 +109,7 @@ describe('register()', () => {
});

test('throws if AlertType action groups contains reserved group id', () => {
const alertType = {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionGroups: [
Expand All @@ -110,6 +123,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
};
Expand All @@ -123,7 +137,7 @@ describe('register()', () => {
});

test('allows an AlertType to specify a custom recovery group', () => {
const alertType = {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionGroups: [
Expand All @@ -139,6 +153,7 @@ describe('register()', () => {
},
executor: jest.fn(),
producer: 'alerts',
minimumLicenseRequired: 'basic',
};
const registry = new AlertTypeRegistry(alertTypeRegistryParams);
registry.register(alertType);
Expand All @@ -157,7 +172,7 @@ describe('register()', () => {
});

test('throws if the custom recovery group is contained in the AlertType action groups', () => {
const alertType = {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionGroups: [
Expand All @@ -175,6 +190,7 @@ describe('register()', () => {
name: 'Back To Awesome',
},
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
};
Expand All @@ -188,7 +204,7 @@ describe('register()', () => {
});

test('registers the executor with the task manager', () => {
const alertType = {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionGroups: [
Expand All @@ -198,6 +214,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
};
Expand Down Expand Up @@ -227,6 +244,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
};
Expand All @@ -248,6 +266,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
});
Expand All @@ -262,6 +281,7 @@ describe('register()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
})
Expand All @@ -282,6 +302,7 @@ describe('get()', () => {
},
],
defaultActionGroupId: 'default',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
});
Expand All @@ -306,6 +327,7 @@ describe('get()', () => {
"defaultActionGroupId": "default",
"executor": [MockFunction],
"id": "test",
"minimumLicenseRequired": "basic",
"name": "Test",
"producer": "alerts",
"recoveryActionGroup": Object {
Expand Down Expand Up @@ -343,6 +365,7 @@ describe('list()', () => {
},
],
defaultActionGroupId: 'testActionGroup',
minimumLicenseRequired: 'basic',
executor: jest.fn(),
producer: 'alerts',
});
Expand All @@ -366,7 +389,9 @@ describe('list()', () => {
"state": Array [],
},
"defaultActionGroupId": "testActionGroup",
"enabledInLicense": false,
"id": "test",
"minimumLicenseRequired": "basic",
"name": "Test",
"producer": "alerts",
"recoveryActionGroup": Object {
Expand Down Expand Up @@ -413,12 +438,50 @@ describe('list()', () => {
});
});

describe('ensureAlertTypeEnabled', () => {
let alertTypeRegistry: AlertTypeRegistry;

beforeEach(() => {
alertTypeRegistry = new AlertTypeRegistry(alertTypeRegistryParams);
alertTypeRegistry.register({
id: 'test',
name: 'Test',
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
producer: 'alerts',
minimumLicenseRequired: 'basic',
recoveryActionGroup: { id: 'recovered', name: 'Recovered' },
});
});

test('should call ensureLicenseForAlertType on the license state', async () => {
alertTypeRegistry.ensureAlertTypeEnabled('test');
expect(mockedLicenseState.ensureLicenseForAlertType).toHaveBeenCalled();
});

test('should throw when ensureLicenseForAlertType throws', async () => {
mockedLicenseState.ensureLicenseForAlertType.mockImplementation(() => {
throw new Error('Fail');
});
expect(() =>
alertTypeRegistry.ensureAlertTypeEnabled('test')
).toThrowErrorMatchingInlineSnapshot(`"Fail"`);
});
});

function alertTypeWithVariables(id: string, context: string, state: string): AlertType {
const baseAlert = {
const baseAlert: AlertType = {
id,
name: `${id}-name`,
actionGroups: [],
defaultActionGroupId: id,
minimumLicenseRequired: 'basic',
async executor() {},
producer: 'alerts',
};
Expand Down
Loading