Skip to content

Commit

Permalink
Allow to disable system actions
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jul 22, 2023
1 parent 25a93f0 commit f479dff
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 77 deletions.
35 changes: 19 additions & 16 deletions x-pack/plugins/actions/server/action_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ describe('actionTypeRegistry', () => {
isSystemAction: false,
},
{
actionTypeId: '.cases',
actionTypeId: 'test.system-action',
config: {},
id: 'system-connector-.cases',
name: 'System action: .cases',
id: 'system-connector-test.system-action',
name: 'System action: test.system-action',
secrets: {},
isPreconfigured: false,
isDeprecated: false,
Expand Down Expand Up @@ -393,7 +393,7 @@ describe('actionTypeRegistry', () => {
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);

actionTypeRegistry.register({
id: '.cases',
id: 'test.system-action',
name: 'Cases',
minimumLicenseRequired: 'platinum',
supportedFeatureIds: ['alerting'],
Expand All @@ -410,7 +410,7 @@ describe('actionTypeRegistry', () => {

expect(actionTypes).toEqual([
{
id: '.cases',
id: 'test.system-action',
name: 'Cases',
enabled: true,
enabledInConfig: true,
Expand Down Expand Up @@ -497,13 +497,16 @@ describe('actionTypeRegistry', () => {
expect(actionTypeRegistry.isActionExecutable('my-slack1', 'foo')).toEqual(true);
});

test('should return true when isActionTypeEnabled is false and isLicenseValidForActionType is true and it has system connectors', async () => {
test('should return false when isActionTypeEnabled is false and isLicenseValidForActionType is true and it has system connectors', async () => {
mockedActionsConfig.isActionTypeEnabled.mockReturnValue(false);
mockedLicenseState.isLicenseValidForActionType.mockReturnValue({ isValid: true });

expect(
actionTypeRegistry.isActionExecutable('system-connector-.cases', 'system-action-type')
).toEqual(true);
actionTypeRegistry.isActionExecutable(
'system-connector-test.system-action',
'system-action-type'
)
).toEqual(false);
});

test('should call isLicenseValidForActionType of the license state with notifyUsage false by default', async () => {
Expand Down Expand Up @@ -662,7 +665,7 @@ describe('actionTypeRegistry', () => {
const registry = new ActionTypeRegistry(actionTypeRegistryParams);

registry.register({
id: '.cases',
id: 'test.system-action',
name: 'Cases',
minimumLicenseRequired: 'platinum',
supportedFeatureIds: ['alerting'],
Expand All @@ -675,7 +678,7 @@ describe('actionTypeRegistry', () => {
executor,
});

const result = registry.isSystemActionType('.cases');
const result = registry.isSystemActionType('test.system-action');
expect(result).toBe(true);
});

Expand Down Expand Up @@ -720,7 +723,7 @@ describe('actionTypeRegistry', () => {
const registry = new ActionTypeRegistry(actionTypeRegistryParams);

registry.register({
id: '.cases',
id: 'test.system-action',
name: 'Cases',
minimumLicenseRequired: 'platinum',
supportedFeatureIds: ['alerting'],
Expand All @@ -734,15 +737,15 @@ describe('actionTypeRegistry', () => {
executor,
});

const result = registry.getSystemActionKibanaPrivileges('.cases');
const result = registry.getSystemActionKibanaPrivileges('test.system-action');
expect(result).toEqual(['test/create']);
});

it('should return an empty array if the system action does not define any kibana privileges', () => {
const registry = new ActionTypeRegistry(actionTypeRegistryParams);

registry.register({
id: '.cases',
id: 'test.system-action',
name: 'Cases',
minimumLicenseRequired: 'platinum',
supportedFeatureIds: ['alerting'],
Expand All @@ -755,7 +758,7 @@ describe('actionTypeRegistry', () => {
executor,
});

const result = registry.getSystemActionKibanaPrivileges('.cases');
const result = registry.getSystemActionKibanaPrivileges('test.system-action');
expect(result).toEqual([]);
});

Expand Down Expand Up @@ -784,7 +787,7 @@ describe('actionTypeRegistry', () => {
const getKibanaPrivileges = jest.fn().mockReturnValue(['test/create']);

registry.register({
id: '.cases',
id: 'test.system-action',
name: 'Cases',
minimumLicenseRequired: 'platinum',
supportedFeatureIds: ['alerting'],
Expand All @@ -798,7 +801,7 @@ describe('actionTypeRegistry', () => {
executor,
});

registry.getSystemActionKibanaPrivileges('.cases', { foo: 'bar' });
registry.getSystemActionKibanaPrivileges('test.system-action', { foo: 'bar' });
expect(getKibanaPrivileges).toHaveBeenCalledWith({ params: { foo: 'bar' } });
});
});
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/actions/server/action_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,24 @@ export class ActionTypeRegistry {
}

/**
* Returns true if action type is enabled or it is an in memory action type.
* Returns true if action type is enabled or it is a preconfigured action type
* It is possible for to disable an action type but use a preconfigured action
* of action type the disabled one. This does not apply to system actions.
* It should be possible to disable a system action type.
*/
public isActionExecutable(
actionId: string,
actionTypeId: string,
options: { notifyUsage: boolean } = { notifyUsage: false }
) {
const actionTypeEnabled = this.isActionTypeEnabled(actionTypeId, options);
const inMemoryConnector = this.inMemoryConnectors.find(
(connector) => connector.id === actionId
);

return (
actionTypeEnabled ||
(!actionTypeEnabled &&
this.inMemoryConnectors.find((inMemoryConnector) => inMemoryConnector.id === actionId) !==
undefined)
(!actionTypeEnabled && inMemoryConnector !== undefined && inMemoryConnector.isPreconfigured)
);
}

Expand Down
Loading

0 comments on commit f479dff

Please sign in to comment.