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

Security - allow for custom cluster privileges #43817

Merged
merged 7 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { shallow } from 'enzyme';
import React from 'react';
import { Role } from '../../../../../../../common/model';
import { ClusterPrivileges } from './cluster_privileges';
import { mountWithIntl } from 'test_utils/enzyme_helpers';

test('it renders without crashing', () => {
const role: Role = {
Expand All @@ -24,8 +25,37 @@ test('it renders without crashing', () => {
<ClusterPrivileges
role={role}
onChange={jest.fn()}
availableClusterPrivileges={['all', 'manage', 'monitor']}
builtinClusterPrivileges={['all', 'manage', 'monitor']}
/>
);
expect(wrapper).toMatchSnapshot();
});

test('it allows for custom cluster privileges', () => {
const role: Role = {
name: '',
elasticsearch: {
cluster: ['existing-custom', 'monitor'],
indices: [],
run_as: [],
},
kibana: [],
};

const onChange = jest.fn();
const wrapper = mountWithIntl(
<ClusterPrivileges
role={role}
onChange={onChange}
builtinClusterPrivileges={['all', 'manage', 'monitor']}
/>
);

const clusterPrivsSelect = wrapper.find(
'EuiComboBox[data-test-subj="cluster-privileges-combobox"]'
);

(clusterPrivsSelect.props() as any).onCreateOption('custom-cluster-privilege');

expect(onChange).toHaveBeenCalledWith(['existing-custom', 'monitor', 'custom-cluster-privilege']);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@

import { EuiComboBox, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import React, { Component } from 'react';
import _ from 'lodash';
import { Role } from '../../../../../../../common/model';
import { isReadOnlyRole } from '../../../../../../lib/role_utils';

interface Props {
role: Role;
availableClusterPrivileges: string[];
builtinClusterPrivileges: string[];
onChange: (privs: string[]) => void;
}

export class ClusterPrivileges extends Component<Props, {}> {
public render() {
return <EuiFlexGroup>{this.buildComboBox(this.props.availableClusterPrivileges)}</EuiFlexGroup>;
const availableClusterPrivileges = this.getAvailableClusterPrivileges();
return <EuiFlexGroup>{this.buildComboBox(availableClusterPrivileges)}</EuiFlexGroup>;
}

public buildComboBox = (items: string[]) => {
Expand All @@ -32,9 +34,11 @@ export class ClusterPrivileges extends Component<Props, {}> {
return (
<EuiFlexItem key={'clusterPrivs'}>
<EuiComboBox
data-test-subj={'cluster-privileges-combobox'}
options={options}
selectedOptions={selectedOptions}
onChange={this.onClusterPrivilegesChange}
onCreateOption={this.onCreateCustomPrivilege}
isDisabled={isReadOnlyRole(role)}
/>
</EuiFlexItem>
Expand All @@ -44,4 +48,17 @@ export class ClusterPrivileges extends Component<Props, {}> {
public onClusterPrivilegesChange = (selectedPrivileges: any) => {
this.props.onChange(selectedPrivileges.map((priv: any) => priv.label));
};

private onCreateCustomPrivilege = (customPrivilege: string) => {
this.props.onChange([...this.props.role.elasticsearch.cluster, customPrivilege]);
};

private getAvailableClusterPrivileges = () => {
const availableClusterPrivileges = [
...this.props.builtinClusterPrivileges,
...this.props.role.elasticsearch.cluster,
];

return _.uniq(availableClusterPrivileges);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ElasticsearchPrivileges extends Component<Props, {}> {
<ClusterPrivileges
role={this.props.role}
onChange={this.onClusterPrivilegesChange}
availableClusterPrivileges={builtinESPrivileges.cluster}
builtinClusterPrivileges={builtinESPrivileges.cluster}
/>
</EuiFormRow>
</EuiDescribedFormGroup>
Expand Down