-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move authorization inside validateSystemActions
- Loading branch information
Showing
10 changed files
with
296 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...ck/plugins/alerting/server/connector_adapters/get_system_action_kibana_privileges.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import { ConnectorAdapterRegistry } from './connector_adapter_registry'; | ||
import { getSystemActionKibanaPrivileges } from './get_system_action_kibana_privileges'; | ||
import type { ConnectorAdapter } from './types'; | ||
|
||
describe('getSystemActionKibanaPrivileges', () => { | ||
const connectorAdapter: ConnectorAdapter = { | ||
connectorTypeId: '.test', | ||
ruleActionParamsSchema: schema.object({ foo: schema.string() }), | ||
buildActionParams: jest.fn(), | ||
getKibanaPrivileges: (args) => [`my-priv:${args.consumer}`], | ||
}; | ||
|
||
const systemActions = [ | ||
{ id: 'my-id', actionTypeId: '.test', params: {} }, | ||
{ id: 'my-id-2', actionTypeId: '.test-2', params: {} }, | ||
]; | ||
|
||
let registry: ConnectorAdapterRegistry; | ||
|
||
beforeEach(() => { | ||
registry = new ConnectorAdapterRegistry(); | ||
registry.register(connectorAdapter); | ||
|
||
registry.register({ | ||
...connectorAdapter, | ||
connectorTypeId: '.test-2', | ||
getKibanaPrivileges: (args) => [`my-priv-2:${args.consumer}`], | ||
}); | ||
|
||
registry.register({ | ||
...connectorAdapter, | ||
connectorTypeId: '.no-priv', | ||
}); | ||
}); | ||
|
||
it('should return an empty array if systemActions are empty', () => { | ||
const privileges = getSystemActionKibanaPrivileges({ | ||
connectorAdapterRegistry: registry, | ||
systemActions: [], | ||
rule: { consumer: 'stackAlerts' }, | ||
}); | ||
|
||
expect(privileges).toEqual([]); | ||
}); | ||
|
||
it('should return an empty array if systemActions are not defined', () => { | ||
const privileges = getSystemActionKibanaPrivileges({ | ||
connectorAdapterRegistry: registry, | ||
rule: { consumer: 'stackAlerts' }, | ||
}); | ||
|
||
expect(privileges).toEqual([]); | ||
}); | ||
|
||
it('should return the privileges correctly', () => { | ||
const privileges = getSystemActionKibanaPrivileges({ | ||
connectorAdapterRegistry: registry, | ||
systemActions, | ||
rule: { consumer: 'stackAlerts' }, | ||
}); | ||
|
||
expect(privileges).toEqual(['my-priv:stackAlerts', 'my-priv-2:stackAlerts']); | ||
}); | ||
|
||
it('should return the privileges correctly with system actions without connector adapter', () => { | ||
const privileges = getSystemActionKibanaPrivileges({ | ||
connectorAdapterRegistry: registry, | ||
systemActions: [...systemActions, { id: 'my-id-2', actionTypeId: '.not-valid', params: {} }], | ||
rule: { consumer: 'stackAlerts' }, | ||
}); | ||
|
||
expect(privileges).toEqual(['my-priv:stackAlerts', 'my-priv-2:stackAlerts']); | ||
}); | ||
|
||
it('should return the privileges correctly with system actions without getKibanaPrivileges defined', () => { | ||
const privileges = getSystemActionKibanaPrivileges({ | ||
connectorAdapterRegistry: registry, | ||
systemActions: [...systemActions, { id: 'my-id-2', actionTypeId: '.no-priv', params: {} }], | ||
rule: { consumer: 'stackAlerts' }, | ||
}); | ||
|
||
expect(privileges).toEqual(['my-priv:stackAlerts', 'my-priv-2:stackAlerts']); | ||
}); | ||
|
||
it('should not return duplicated privileges', () => { | ||
const privileges = getSystemActionKibanaPrivileges({ | ||
connectorAdapterRegistry: registry, | ||
systemActions: [systemActions[0], systemActions[0]], | ||
rule: { consumer: 'stackAlerts' }, | ||
}); | ||
|
||
expect(privileges).toEqual(['my-priv:stackAlerts']); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/alerting/server/connector_adapters/get_system_action_kibana_privileges.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { RuleSystemAction } from '../types'; | ||
import { ConnectorAdapterRegistry } from './connector_adapter_registry'; | ||
|
||
interface Args { | ||
connectorAdapterRegistry: ConnectorAdapterRegistry; | ||
rule: { consumer: string }; | ||
systemActions?: RuleSystemAction[]; | ||
} | ||
|
||
export const getSystemActionKibanaPrivileges = ({ | ||
connectorAdapterRegistry, | ||
systemActions = [], | ||
rule, | ||
}: Args): string[] => { | ||
const kibanaPrivileges = systemActions | ||
.filter((action) => connectorAdapterRegistry.has(action.actionTypeId)) | ||
.map((action) => connectorAdapterRegistry.get(action.actionTypeId)) | ||
.map((adapter) => adapter.getKibanaPrivileges?.({ consumer: rule.consumer }) ?? []) | ||
.flat(); | ||
|
||
return Array.from(new Set(kibanaPrivileges)); | ||
}; |
34 changes: 0 additions & 34 deletions
34
x-pack/plugins/alerting/server/lib/get_system_action_kibana_privileges.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.