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
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 49 additions & 4 deletions public/components/security/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import {
EuiTabs,
EuiTab,
EuiPanel,
EuiCallOut,
EuiEmptyPrompt,
EuiSpacer,
} from '@elastic/eui';
import { Users } from './users/users';
import { Roles } from './roles/roles';
import { Policies } from './policies/policies';
import { GenericRequest } from '../../react-services/generic-request';
import { API_USER_STATUS_RUN_AS } from '../../../server/lib/cache-api-user-has-run-as';
import { AppState } from '../../react-services/app-state';
import { ErrorHandler } from '../../react-services/error-handler';
import { RolesMapping } from './roles-mapping/roles-mapping';
import { withReduxProvider, withGlobalBreadcrumb, withUserAuthorizationPrompt } from '../common/hocs';
import { compose } from 'redux';
Expand Down Expand Up @@ -55,11 +60,34 @@ export const WzSecurity = compose(

const [selectedTabId, setSelectedTabId] = useState(tab && tab[1] || 'users');


const listenerLocationChanged = () => {
const tab = window.location.href.match(securityTabRegExp);
setSelectedTabId(tab && tab[1] || 'users')
}
// This allows to redirect to a Security tab if you click a Security link in menu when you're already in a Security section

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

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

const [allowRunAs, setAllowRunAs] = useState();
useEffect(() => {
checkRunAsUser()
.then(result => setAllowRunAs(result))
.catch(error => console.log(error, 'Error checking if run_as user is enabled'))
}, [])

// This allows to redirect to a Security tab if you click a Security link in menu when you're already in a Security section
useEffect(() => {
window.addEventListener('popstate', listenerLocationChanged)
return () => window.removeEventListener('popstate', listenerLocationChanged);
Expand All @@ -71,7 +99,7 @@ export const WzSecurity = compose(

const onSelectedTabChanged = id => {
window.location.href = window.location.href.replace(`tab=${selectedTabId}`, `tab=${id}`);
setSelectedTabId(id);
setSelectedTabId(id);
};

const renderTabs = () => {
Expand All @@ -87,6 +115,20 @@ export const WzSecurity = compose(
));
};


const isNotRunAs = () => {
return (
<EuiFlexGroup >
<EuiFlexItem >
<EuiCallOut title=" The role mapping has no effect because the Wazuh API's configurated user has not the run_as setting enabled in the configuration or is not allowed to use it. " color="warning" iconType="alert">
</EuiCallOut>
<EuiSpacer></EuiSpacer>
</EuiFlexItem >
</EuiFlexGroup>
);
}


return (
<EuiPage>
<EuiFlexGroup>
Expand All @@ -103,10 +145,13 @@ export const WzSecurity = compose(
<Policies></Policies>
}
{selectedTabId === 'roleMapping' &&
<RolesMapping></RolesMapping>
<>
{allowRunAs !== API_USER_STATUS_RUN_AS.ENABLED && allowRunAs !== undefined && isNotRunAs()}
<RolesMapping></RolesMapping>
</>
}
</EuiFlexItem>
</EuiFlexGroup>
</EuiPage>
);
});
});
141 changes: 70 additions & 71 deletions public/components/security/roles-mapping/components/rule-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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 @@ -279,80 +278,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