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

Feature/add warning when configuring new role mapping #2876

Merged
merged 8 commits into from
Feb 5, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
174 changes: 102 additions & 72 deletions public/components/security/roles-mapping/components/rule-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {
decodeJsonRule,
getSelectedUsersFromRules,
} from '../helpers/rule-editor.helper';
import { getToasts } from '../../../../kibana-services';
import { GenericRequest } from '../../../../react-services/generic-request';
import { ErrorHandler } from '../../../../react-services/error-handler';
import { WAZUH_SECURITY_PLUGIN_OPEN_DISTRO_FOR_ELASTICSEARCH } from '../../../../../common/constants';
import { AppState } from '../../../../react-services/app-state';
import 'brace/mode/json';
import 'brace/snippets/json';
import 'brace/ext/language_tools';
Expand All @@ -53,7 +57,6 @@ export const RuleEditor = ({ save, initialRule, isLoading, isReserved, internalU
const default_user_field = currentPlatform === WAZUH_SECURITY_PLUGIN_OPEN_DISTRO_FOR_ELASTICSEARCH ? 'user_name' : 'username';
const default_rule = { user_field: default_user_field, searchOperation: 'FIND', value: 'wazuh' };


useEffect(() => {
if (initialRule) {
setStateFromRule(JSON.stringify(initialRule));
Expand Down Expand Up @@ -123,6 +126,29 @@ export const RuleEditor = ({ save, initialRule, isLoading, isReserved, internalU
return { customRules, internalUsersRules, wrongFormat, logicalOperator };
};

const showToast = (color, title, text = '', time = 3000) => {
getToasts().add({
color,
title,
text,
toastLifeTimeMs: time
});
}

const checkRunAsUser = async () => {
const currentApi = AppState.getCurrentAPI();
try {
const ApiCheck = await GenericRequest.request('POST',
'/api/check-api',
currentApi
);
return ApiCheck;

} catch (error) {
ErrorHandler.handle(error, 'Error checking the current API');
}
}

const printRules = () => {
const rulesList = rules.map((item, idx) => {
return (
Expand Down Expand Up @@ -227,7 +253,11 @@ export const RuleEditor = ({ save, initialRule, isLoading, isReserved, internalU
}
};

const saveRule = () => {
const saveRule = async () => {
const isRunAS = await checkRunAsUser();
if (!isRunAS.data.allow_run_as) {
showToast('warning', 'These changes will not take effect until run_as is activated in /usr/share/kibana/data/wazuh/config/wazuh.yml and the Kibana service is restarted');
frankeros marked this conversation as resolved.
Show resolved Hide resolved
}
if (isJsonEditor) {
save(JSON.parse(ruleJson));
} else {
Expand Down Expand Up @@ -279,80 +309,80 @@ export const RuleEditor = ({ save, initialRule, isLoading, isReserved, internalU
aria-label="Code Editor"
/>
)) || (
<Fragment>
<EuiTitle size="s">
<h2>Map internal users</h2>
</EuiTitle>
<EuiFormRow
label="Internal users"
helpText="Assign internal users to the selected role mapping"
>
<EuiComboBox
placeholder="Select internal users"
options={internalUsersOptions}
selectedOptions={selectedUsers}
isLoading={isLoading}
onChange={onChangeSelectedUsers}
isClearable={true}
data-test-subj="demoComboBox"
/>
</EuiFormRow>
<EuiSpacer />
<EuiTitle size="s">
<h2>Custom rules</h2>
</EuiTitle>
<EuiPopover
ownFocus
button={
<EuiButtonEmpty
disabled={isLoading || isReserved}
onClick={onButtonClick}
iconType="arrowDown"
iconSide="right"
>
{logicalOperator === 'AND' ? 'All are true' : 'Any are true'}
</EuiButtonEmpty>
}
isOpen={isLogicalPopoverOpen}
closePopover={closeLogicalPopover}
anchorPosition="downCenter"
>
<div>
<EuiFlexGroup>
<EuiFlexItem>
<EuiButtonEmpty
disabled={isLoading || isReserved}
color="text"
onClick={() => selectOperator('AND')}
>
{logicalOperator === 'AND' && <EuiIcon type="check" />}All are true
<Fragment>
<EuiTitle size="s">
<h2>Map internal users</h2>
</EuiTitle>
<EuiFormRow
label="Internal users"
helpText="Assign internal users to the selected role mapping"
>
<EuiComboBox
placeholder="Select internal users"
options={internalUsersOptions}
selectedOptions={selectedUsers}
isLoading={isLoading}
onChange={onChangeSelectedUsers}
isClearable={true}
data-test-subj="demoComboBox"
/>
</EuiFormRow>
<EuiSpacer />
<EuiTitle size="s">
<h2>Custom rules</h2>
</EuiTitle>
<EuiPopover
ownFocus
button={
<EuiButtonEmpty
disabled={isLoading || isReserved}
onClick={onButtonClick}
iconType="arrowDown"
iconSide="right"
>
{logicalOperator === 'AND' ? 'All are true' : 'Any are true'}
</EuiButtonEmpty>
}
isOpen={isLogicalPopoverOpen}
closePopover={closeLogicalPopover}
anchorPosition="downCenter"
>
<div>
<EuiFlexGroup>
<EuiFlexItem>
<EuiButtonEmpty
disabled={isLoading || isReserved}
color="text"
onClick={() => selectOperator('AND')}
>
{logicalOperator === 'AND' && <EuiIcon type="check" />}All are true
</EuiButtonEmpty>
</EuiFlexItem>
</EuiFlexGroup>
<EuiFlexGroup>
<EuiFlexItem>
<EuiButtonEmpty
disabled={isLoading || isReserved}
color="text"
onClick={() => selectOperator('OR')}
>
{logicalOperator === 'OR' && <EuiIcon type="check" />}Any are true
</EuiFlexItem>
</EuiFlexGroup>
<EuiFlexGroup>
<EuiFlexItem>
<EuiButtonEmpty
disabled={isLoading || isReserved}
color="text"
onClick={() => selectOperator('OR')}
>
{logicalOperator === 'OR' && <EuiIcon type="check" />}Any are true
</EuiButtonEmpty>
</EuiFlexItem>
</EuiFlexGroup>
</div>
</EuiPopover>
{printRules()}
</EuiFlexItem>
</EuiFlexGroup>
</div>
</EuiPopover>
{printRules()}

<EuiButtonEmpty
disabled={isLoading || isReserved}
color="primary"
onClick={() => addNewRule()}
>
Add new rule
<EuiButtonEmpty
disabled={isLoading || isReserved}
color="primary"
onClick={() => addNewRule()}
>
Add new rule
</EuiButtonEmpty>
</Fragment>
)}
</Fragment>
)}
</EuiPanel>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down