Skip to content

Commit

Permalink
Merge pull request #2417 from wazuh/4.0-7.8.0-rbac
Browse files Browse the repository at this point in the history
RBAC adapt
  • Loading branch information
jsanchez91 authored Sep 22, 2020
2 parents ffe3a09 + bc3fe43 commit 877dd37
Show file tree
Hide file tree
Showing 249 changed files with 9,727 additions and 3,074 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import { resolve } from 'path';
import { WazuhPlugin, LegacySetup } from './server/plugin';
import { WAZUH_ALERTS_PATTERN, WAZUH_MONITORING_PATTERN } from './util/constants'

export default (kibana) => {
return new kibana.Plugin({
Expand All @@ -23,7 +24,7 @@ export default (kibana) => {
id: 'wazuh',
title: 'Wazuh',
description: 'Wazuh app for Kibana',
icon: 'plugins/wazuh/img/icon_blue.svg',
icon: 'plugins/wazuh/img/icon_blue.png',
main: 'plugins/wazuh/app'
},
hacks: ['plugins/wazuh/icon-style'],
Expand Down Expand Up @@ -60,7 +61,7 @@ export default (kibana) => {

xpackMainPlugin.registerFeature({
id: featureId,
name: 'C',
name: 'Wazuh',
navLinkId: featureId,
icon: '/plugins/wazuh/img/icon_blue.svg',
app: [featureId],
Expand All @@ -70,8 +71,8 @@ export default (kibana) => {
app: [featureId],
api: [],
savedObject: {
all: ['wazuh-alerts-3.x-*', 'wazuh-monitoring-3.x-*'],
read: ['wazuh-alerts-3.x-*', 'wazuh-monitoring-3.x-*']
all: [WAZUH_ALERTS_PATTERN, WAZUH_MONITORING_PATTERN],
read: [WAZUH_ALERTS_PATTERN, WAZUH_MONITORING_PATTERN]
},
ui: ['save', 'show']
},
Expand All @@ -80,7 +81,7 @@ export default (kibana) => {
api: [],
savedObject: {
all: [],
read: ['wazuh-alerts-3.x-*', 'wazuh-monitoring-3.x-*']
read: [WAZUH_ALERTS_PATTERN, WAZUH_MONITORING_PATTERN]
},
ui: ['show']
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"revision": "4001",
"code": "4001-0",
"kibana": {
"version": "7.8.1"
"version": "7.8.0"
},
"description": "Wazuh app",
"main": "index.js",
Expand Down Expand Up @@ -50,6 +50,7 @@
"install": "^0.10.1",
"js2xmlparser": "^3.0.0",
"json2csv": "^4.1.2",
"jwt-decode": "^2.2.0",
"needle": "^2.0.1",
"node-cron": "^1.1.2",
"pdfmake": "^0.1.37",
Expand Down
21 changes: 11 additions & 10 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ import './services';
import './controllers';
import './factories';

// Imports to update adminMode when app starts
import { checkAdminMode } from './controllers/management/components/management/configuration/utils/wz-fetch';
// Imports to update currentPlatform when app starts
import { checkCurrentSecurityPlatform } from './controllers/management/components/management/configuration/utils/wz-fetch';
import store from './redux/store';
import { updateAdminMode } from './redux/actions/appStateActions';
import { updateCurrentPlatform } from './redux/actions/appStateActions';
import { WzAuthentication } from './react-services/wz-authentication'

import { getAngularModule } from 'plugins/kibana/discover/kibana_services';
const app = getAngularModule('app/wazuh');
Expand Down Expand Up @@ -111,12 +112,12 @@ app.run([
changeWazuhNavLogo();
app.$injector = _$injector;

// Set adminMode in Redux when app starts.
// It prevents the first rendering, which depends on adminMode, from blinking due to a request to the app backend
checkAdminMode()
.then(adminMode => {
store.dispatch(updateAdminMode(adminMode))
})
.catch(() => {/* Do nothing if it fails */ })
// Set currentSecurity platform in Redux when app starts.
checkCurrentSecurityPlatform().then((item) => {
store.dispatch(updateCurrentPlatform(item))
}).catch(() => {})

// Init the process of refreshing the user's token when app start.
WzAuthentication.refresh();
}
]);
19 changes: 18 additions & 1 deletion public/components/add-modules-data/WzSampleDataWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ import {
} from '@elastic/eui';
import WzSampleData from './sample-data'
import WzReduxProvider from '../../redux/wz-redux-provider';
import { withUserAuthorizationPrompt } from '../../components/common/hocs/withUserAuthorization';
import store from '../../redux/store';
import { updateSelectedSettingsSection } from '../../redux/actions/appStateActions';
import { WAZUH_ROLE_ADMINISTRATOR_NAME } from '../../../util/constants';

export class WzSampleDataWrapper extends Component {
export class WzSampleDataProvider extends Component {
constructor(props) {
super(props);
this.state = {};
}

componentDidMount(){
store.dispatch(updateSelectedSettingsSection('sample_data'));
}

render() {
return (
<EuiPage>
Expand Down Expand Up @@ -64,3 +72,12 @@ export class WzSampleDataWrapper extends Component {
);
}
}

const WzSampleDataWrapperWithAdministrator = withUserAuthorizationPrompt(null, [WAZUH_ROLE_ADMINISTRATOR_NAME])(WzSampleDataProvider);
export function WzSampleDataWrapper() {
return (
<WzReduxProvider>
<WzSampleDataWrapperWithAdministrator />
</WzReduxProvider>
)
}
81 changes: 26 additions & 55 deletions public/components/add-modules-data/sample-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
*/

import React, { Component, Fragment } from 'react';
import { checkAdminMode } from '../../controllers/management/components/management/configuration/utils/wz-fetch';
import { updateAdminMode } from '../../redux/actions/appStateActions';
import { WzButtonPermissions } from '../../components/common/permissions/button';

import {
EuiFlexItem,
Expand All @@ -29,14 +28,10 @@ import {

import { toastNotifications } from 'ui/notify';
import { WzRequest } from '../../react-services/wz-request';
import { connect } from 'react-redux';
import { AppState } from '../../react-services/app-state';
import { WAZUH_ROLE_ADMINISTRATOR_NAME } from '../../../util/constants';

interface IWzSampleDataProps{
adminMode?: boolean
updateAdminMode: (adminMode: boolean) => void
};

class WzSampleData extends Component<IWzSampleDataProps> {
export default class WzSampleData extends Component {
categories: {title: string, description: string, image: string, categorySampleAlertsIndex: string}[]
generateAlertsParams: any
state: {
Expand Down Expand Up @@ -79,20 +74,9 @@ class WzSampleData extends Component<IWzSampleDataProps> {
});
}
async componentDidMount(){
// Check adminMode
try{
const adminMode: boolean = await checkAdminMode();
if(this.props.adminMode !== adminMode){
this.props.updateAdminMode(adminMode);
};
if(!adminMode){ // redirect to root path of the application
window.location.href = `#/`;
};
}catch(error){}

// Check if sample data for each category was added
try{
const results = await PromiseAllRecusiveObject(this.categories.reduce((accum, cur) => {
const results = await PromiseAllRecursiveObject(this.categories.reduce((accum, cur) => {
accum[cur.categorySampleAlertsIndex] = WzRequest.genericReq('GET', `/elastic/samplealerts/${cur.categorySampleAlertsIndex}`)
return accum
},{}));
Expand All @@ -108,14 +92,15 @@ class WzSampleData extends Component<IWzSampleDataProps> {

// Get information about cluster/manager
try{
const managerInfo = await WzRequest.apiReq('GET', '/manager/info', {});
const clusterName = AppState.getClusterInfo().cluster;
const managerName = AppState.getClusterInfo().manager;
this.generateAlertsParams.manager = {
name: managerInfo.data.data.name
name: managerName
};
if (managerInfo.data.data.cluster) {
if (clusterName && clusterName !== 'Disabled') {
this.generateAlertsParams.cluster = {
name: managerInfo.data.data.cluster.name,
node: managerInfo.data.data.cluster.node_name
name: clusterName,
node: clusterName
};
};

Expand Down Expand Up @@ -173,7 +158,6 @@ class WzSampleData extends Component<IWzSampleDataProps> {
}
renderCard(category){
const { addDataLoading, exists, removeDataLoading } = this.state[category.categorySampleAlertsIndex];
const { adminMode } = this.props;
return (
<EuiFlexItem key={`sample-data-${category.title}`}>
<EuiCard
Expand All @@ -186,20 +170,21 @@ class WzSampleData extends Component<IWzSampleDataProps> {
<EuiFlexGroup justifyContent="flexEnd">
<EuiFlexItem grow={false}>
{exists && (
<EuiButton
isLoading={removeDataLoading}
isDisabled={!adminMode}
<WzButtonPermissions
color='danger'
onClick={() => this.removeSampleData(category)}>
{removeDataLoading && 'Removing data' || 'Remove data'}
</EuiButton>
roles={[WAZUH_ROLE_ADMINISTRATOR_NAME]}
onClick={() => this.removeSampleData(category)}
>
{removeDataLoading && 'Removing data' || 'Remove data'}
</WzButtonPermissions>
) || (
<EuiButton
<WzButtonPermissions
isLoading={addDataLoading}
isDisabled={!adminMode}
onClick={() => this.addSampleData(category)}>
{addDataLoading && 'Adding data' || 'Add data'}
</EuiButton>
roles={[WAZUH_ROLE_ADMINISTRATOR_NAME]}
onClick={() => this.addSampleData(category)}
>
{addDataLoading && 'Adding data' || 'Add data'}
</WzButtonPermissions>
)}
</EuiFlexItem>
</EuiFlexGroup>
Expand All @@ -224,29 +209,15 @@ const zipObject = (keys = [], values = []) => {
}, {})
}

const PromiseAllRecusiveObject = function (obj) {
const PromiseAllRecursiveObject = function (obj) {
const keys = Object.keys(obj);
return Promise.all(keys.map(key => {
const value = obj[key];
// Promise.resolve(value) !== value should work, but !value.then always works
if (typeof value === 'object' && !value.then) {
return PromiseAllRecusiveObject(value);
return PromiseAllRecursiveObject(value);
}
return value;
}))
.then(result => zipObject(keys, result));
};

const mapStateToProps = state => {
return {
adminMode: state.appStateReducers.adminMode
};
};

const mapDispatchToProps = dispatch => {
return {
updateAdminMode: adminMode => dispatch(updateAdminMode(adminMode))
}
};

export default connect(mapStateToProps,mapDispatchToProps)(WzSampleData);
};
Loading

0 comments on commit 877dd37

Please sign in to comment.