Skip to content

Commit

Permalink
Switching to EPIC #2727 and stoping this issue temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
sortiz committed Jan 5, 2021
1 parent 2983c59 commit 9ae2fc1
Show file tree
Hide file tree
Showing 133 changed files with 1,197 additions and 312 deletions.
2 changes: 2 additions & 0 deletions public/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AppDependencies } from './types';
//import { ToastNotificationsModal } from './components/notifications/modal';
import { HealthCheck } from './components/health-check';
import { MainOverview } from './components/main-overview';
import { MainAgents } from './components/main-agents';


const LANDING_PAGE_URL = '/overview';
Expand All @@ -33,6 +34,7 @@ export function AppRouter(props: AppDependencies) {
<Switch>
<Route path={LANDING_PAGE_URL} render={() => <MainOverview />} />
<Route path="/health-check" render={() => <HealthCheck />} />
<Route path="/agents-preview" render={() => <MainAgents />} />
<Redirect exact from="/" to={LANDING_PAGE_URL} />
</Switch>
</EuiPageBody>
Expand Down
4 changes: 2 additions & 2 deletions public/components/add-modules-data/sample-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import {
} from '@elastic/eui';

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

export default class WzSampleData extends Component {
Expand Down
2 changes: 1 addition & 1 deletion public/components/agents/fim/inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
FilterBar,
RegistryTable
} from './inventory/';
import { WzRequest } from '../../../react-services/wz-request';
import WzRequest from '../../../react-services/wz-request';
import exportCsv from '../../../react-services/wz-csv';
import { toastNotifications } from 'ui/notify';
import { ICustomBadges } from '../../wz-search-bar/components';
Expand Down
4 changes: 2 additions & 2 deletions public/components/agents/fim/inventory/flyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {
EuiLoadingContent,
EuiCallOut
} from '@elastic/eui';
import { WzRequest } from '../../../../react-services/wz-request';
import WzRequest from '../../../../react-services/wz-request';
import { FileDetails } from './fileDetail';
import { AppState } from '../../../../react-services/app-state';
import AppState from '../../../../react-services/app-state';

export class FlyoutDetail extends Component {
state: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Find more information about this on the LICENSE file.
*/
import { WzRequest } from '../../../../../react-services/wz-request';
import WzRequest from '../../../../../react-services/wz-request';

export async function getFilterValues(field, value, agentId, filters={}, format=(item) => item) {

Expand Down
2 changes: 1 addition & 1 deletion public/components/agents/fim/inventory/registry-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { WzRequest } from '../../../../react-services/wz-request';
import WzRequest from '../../../../react-services/wz-request';
import { FlyoutDetail } from './flyout';
import { filtersToObject } from '../../../wz-search-bar';

Expand Down
2 changes: 1 addition & 1 deletion public/components/agents/fim/inventory/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Direction,
EuiOverlayMask,
} from '@elastic/eui';
import { WzRequest } from '../../../../react-services/wz-request';
import WzRequest from '../../../../react-services/wz-request';
import { FlyoutDetail } from './flyout';
import './inventory.scss';
import { filtersToObject, IFilter } from '../../../wz-search-bar';
Expand Down
1 change: 1 addition & 0 deletions public/components/agents/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { WzAgents } from './main';
32 changes: 32 additions & 0 deletions public/components/agents/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React, { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import {
EuiPage,
EuiFlexGroup,
EuiFlexItem,
EuiTabs,
EuiTab,
EuiPanel,
EuiEmptyPrompt,
EuiSpacer,
} from '@elastic/eui';
import { withReduxProvider, withGlobalBreadcrumb, withUserAuthorizationPrompt } from '../common/hocs';
import { compose } from 'redux';
import { WAZUH_ROLE_ADMINISTRATOR_NAME } from '../../../util/constants';
import { updateSecuritySection } from '../../redux/actions/securityActions';
import {AgentsPreview} from '../../controllers/agent/components/agents-preview';
import {AgentsTable} from '../../controllers/agent/components/agents-table';

export const WzAgents = compose(
withReduxProvider,
withGlobalBreadcrumb([{ text: '' }, { text: 'Agents' }]),
withUserAuthorizationPrompt(null, [WAZUH_ROLE_ADMINISTRATOR_NAME])
)(() => {

return (
<>
<AgentsPreview></AgentsPreview>
<AgentsTable></AgentsTable>
</>
);
});
2 changes: 1 addition & 1 deletion public/components/agents/sca/inventory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
EuiCallOut,
EuiPopover
} from '@elastic/eui';
import { WzRequest } from '../../../react-services/wz-request';
import WzRequest from '../../../react-services/wz-request';
import TimeService from '../../../react-services/time-service'
import exportCsv from '../../../react-services/wz-csv';
import { toastNotifications } from 'ui/notify';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from "react";
import { EuiPanel, EuiFlexGroup, EuiButtonEmpty, EuiFlexItem, EuiText, EuiLoadingSpinner, EuiFieldSearch, EuiHorizontalRule, EuiIcon, EuiBasicTable } from "@elastic/eui";
import { useApiRequest } from '../../../common/hooks/useApiRequest';
import { KeyEquivalence } from '../../../../../util/csv-key-equivalence';
import { AppState } from '../../../../react-services/app-state';
import AppState from '../../../../react-services/app-state';


export function SyscollectorTable({ tableParams }) {
Expand Down
2 changes: 1 addition & 1 deletion public/components/common/hooks/use-index-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { useState, useEffect} from 'react';
//@ts-ignore
import { getServices } from '../../../../../../src/plugins/discover/public/kibana_services';
import { AppState } from '../../../react-services/app-state';
import AppState from '../../../react-services/app-state';
import { IIndexPattern } from '../../../../../../src/plugins/data/public';

export const useIndexPattern = (): IIndexPattern | undefined => {
Expand Down
137 changes: 75 additions & 62 deletions public/components/common/modules/discover/discover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ import React, { Component, } from 'react';
import './discover.scss';
import { FilterManager, Filter } from '../../../../../../../src/plugins/data/public/'
import { GenericRequest } from '../../../../react-services/generic-request';
import { AppState } from '../../../../react-services/app-state';
import AppState from '../../../../react-services/app-state';
import { AppNavigate } from '../../../../react-services/app-navigate';
import { RowDetails } from './row-details';
//@ts-ignore
import { npSetup } from 'ui/new_platform';
//@ts-ignore
import { getServices } from '../../../../../../../src/plugins/discover/public/kibana_services';
import DateMatch from '@elastic/datemath';
import { toastNotifications } from 'ui/notify';
import { WazuhConfig } from '../../../../react-services/wazuh-config';
import { KbnSearchBar } from '../../../kbn-search-bar';
import { FlyoutTechnique } from '../../../../components/overview/mitre/components/techniques/components/flyout-technique';
import { withReduxProvider } from '../../../common/hocs';
import { connect } from 'react-redux';
import { compose } from 'redux';
import _ from 'lodash';

import {
EuiBasicTable,
Expand All @@ -55,6 +51,7 @@ import {
IFieldType
} from '../../../../../../../src/plugins/data/common';
import '../../../../components/agents/fim/inventory/inventory.scss';
import { getDataPlugin, getToasts, getUiSettings } from '../../../../kibana-services';

const mapStateToProps = state => ({
currentAgentData: state.appStateReducers.currentAgentData
Expand Down Expand Up @@ -106,12 +103,13 @@ export const Discover = compose(
includeFilters?: string,
initialColumns: string[],
shareFilterManager: object[],
refreshAngularDiscover?: number
}
constructor(props) {
super(props);
this.KibanaServices = getServices();
this.filterManager = props.shareFilterManager ? this.KibanaServices.filterManager : new FilterManager(npSetup.core.uiSettings);
this.timefilter = this.KibanaServices.timefilter;
this.KibanaServices = getDataPlugin();
this.filterManager = props.shareFilterManager ? this.KibanaServices.query.filterManager : new FilterManager(getUiSettings);
this.timefilter = this.KibanaServices.query.timefilter.timefilter;
this.state = {
sort: {},
selectedTechnique: "",
Expand Down Expand Up @@ -160,7 +158,7 @@ export const Discover = compose(
}

showToast = (color, title, time) => {
toastNotifications.add({
getToasts().add({
color: color,
title: title,
toastLifeTimeMs: time,
Expand All @@ -172,7 +170,7 @@ export const Discover = compose(
try {
this.setState({columns: this.getColumns(), searchBarFilters: this.props.shareFilterManager || []}) //initial columns
await this.getIndexPattern();
this.getAlerts();
await this.getAlerts();
} catch (err) {
console.log(err);
}
Expand All @@ -186,12 +184,30 @@ export const Discover = compose(
if (!this._isMount) { return; }
if((!prevProps.currentAgentData.id && this.props.currentAgentData.id) || (prevProps.currentAgentData.id && !this.props.currentAgentData.id)){
this.setState({ columns: this.getColumns() }); // Updates the columns to be rendered if you change the selected agent to none or vice versa
return;
}
try {
await this.getAlerts();
} catch (err) {
console.log(err);
}
if(!_.isEqual(this.props.query,prevProps.query)){
this.setState({ query: {...this.props.query}});
return;
};
if((!_.isEqual(this.props.shareFilterManager, prevProps.shareFilterManager))
|| (this.props.currentAgentData.id !== prevProps.currentAgentData.id)
|| (!_.isEqual(this.state.query, prevState.query))
|| (!_.isEqual(this.state.searchBarFilters, prevState.searchBarFilters))
|| (!_.isEqual(this.state.dateRange, prevState.dateRange))
|| (this.props.refreshAngularDiscover !== prevProps.refreshAngularDiscover)
){
this.setState({ pageIndex: 0 , tsUpdated: Date.now()});
return;
};

if(['pageIndex', 'pageSize', 'sortField', 'sortDirection'].some(field => this.state[field] !== prevState[field]) || (this.state.tsUpdated !== prevState.tsUpdated)){
try {
await this.getAlerts();
} catch (err) {
console.log(err);
};
};
}

getColumns () {
Expand Down Expand Up @@ -265,7 +281,7 @@ export const Discover = compose(
buildFilter() {
const dateParse = ds => /\d+-\d+-\d+T\d+:\d+:\d+.\d+Z/.test(ds) ? DateMatch.parse(ds).toDate().getTime() : ds;
const { searchBarFilters } = this.state;
const { query = this.state.query } = this.props;
const { query } = this.state;
const { hideManagerAlerts } = this.wazuhConfig.getConfig();
const extraFilters = [];
if (hideManagerAlerts) extraFilters.push({
Expand All @@ -288,47 +304,60 @@ export const Discover = compose(
undefined,
query,
[...searchBarFilters, ...extraFilters, ...shareFilterManager],
getEsQueryConfig(npSetup.core.uiSettings)
getEsQueryConfig(getUiSettings())
);
const pattern = AppState.getCurrentPattern();
const { filters, sortField, sortDirection } = this.state;
const { from: oldFrom, to: oldTo } = this.timefilter.getTime();
const sort = { ...(sortField && { [sortField]: { "order": sortDirection } }) };
const offset = Math.floor((this.state.pageIndex * this.state.pageSize) / this.state.requestSize) * this.state.requestSize;
const from = dateParse(oldFrom);
const to = dateParse(oldTo);
return { filters, sort, from, to, offset, pattern, elasticQuery };

const { sortField, sortDirection } = this.state;

const range = {
range: {
timestamp: {
gte: dateParse(this.timefilter.getTime().from),
lte: dateParse(this.timefilter.getTime().to),
format: 'epoch_millis'
}
}
}
elasticQuery.bool.must.push(range);

if(this.props.implicitFilters){
this.props.implicitFilters.map(impicitFilter => elasticQuery.bool.must.push({
match: impicitFilter
}));
};
if(this.props.currentAgentData.id){
elasticQuery.bool.must.push({
match: {"agent.id": this.props.currentAgentData.id}
});
};
return {
query: elasticQuery,
size: this.state.pageSize,
from: this.state.pageIndex*this.state.pageSize,
...(sortField ? {sort: { [sortField]: { "order": sortDirection } }}: {})
};
}

async getAlerts() {
if (!this.indexPattern || this.state.isLoading) return;
//compare filters so we only make a request into Elasticsearch if needed
const newFilters = this.buildFilter();
try {
if (JSON.stringify(newFilters) !== JSON.stringify(this.state.requestFilters)) {
if (newFilters.offset === this.state.requestFilters.offset) // we only reset pageIndex to 0 if the requestFilters has changed but the offset is the same
this.setState({ isLoading: true, pageIndex: 0 });
else
this.setState({ isLoading: true});
let filtersReq = [...newFilters['filters'], ...this.props.implicitFilters];
if(this.props.currentAgentData.id){
filtersReq.push({"agent.id": this.props.currentAgentData.id})
}
this.setState({ isLoading: true});

const alerts = await GenericRequest.request(
'POST',
`/elastic/alerts`,
`/elastic/esAlerts`,
{
...newFilters,
filters: filtersReq
index: AppState.getCurrentPattern(),
body: newFilters
}
);
if (this._isMount) {
this.setState({ alerts: alerts.data.alerts, total: alerts.data.hits, isLoading: false, requestFilters: newFilters, filters: newFilters.filters });
this.props.updateTotalHits(alerts.data.hits);

if (this._isMount) {
this.setState({ alerts: alerts.data.hits.hits, total: alerts.data.hits.total.value, isLoading: false, requestFilters: newFilters, filters: newFilters.filters });
this.props.updateTotalHits(alerts.data.hits.total.value);
}
}
} catch (err) {
if (this._isMount) {
this.setState({ alerts: [], total: 0, isLoading: false, requestFilters: newFilters, filters: newFilters.filters });
Expand Down Expand Up @@ -477,24 +506,9 @@ export const Discover = compose(
pageSize,
sortField,
sortDirection,
}, async () => this.getAlerts())
});
};


getpageIndexItems() {
let items: {}[] = [];

const start = (this.state.pageIndex * this.state.pageSize) % this.state.requestSize;
const end = start + this.state.pageSize
for (let i = start; i < end && (this.state.pageIndex * this.state.pageSize) < this.state.total; i++) {
if (this.state.alerts[i] && this.state.alerts[i]._source) {
items.push({ ...this.state.alerts[i]._source, _id: this.state.alerts[i]._id })
}
}
return items;

}

getFiltersAsObject(filters) {
var result = {};
for (var i = 0; i < filters.length; i++) {
Expand Down Expand Up @@ -545,7 +559,7 @@ export const Discover = compose(
}

onQuerySubmit = (payload: { dateRange: TimeRange, query: Query | undefined }) => {
this.setState(payload);
this.setState({...payload, tsUpdated: Date.now()});
}

onFiltersUpdated = (filters: Filter[]) => {
Expand Down Expand Up @@ -582,7 +596,6 @@ export const Discover = compose(
};
};

const pageIndexItems = this.getpageIndexItems();
const columns = this.columns();

const sorting: EuiTableSortingType<{}> = {
Expand Down Expand Up @@ -622,9 +635,9 @@ export const Discover = compose(
{total
? <EuiFlexGroup>
<EuiFlexItem>
{pageIndexItems.length && (
{this.state.alerts.length && (
<EuiBasicTable
items={pageIndexItems}
items={this.state.alerts.map(alert => ({...alert._source, _id: alert._id}))}
className="module-discover-table"
itemId="_id"
itemIdToExpandedRowMap={itemIdToExpandedRowMap}
Expand Down
Loading

0 comments on commit 9ae2fc1

Please sign in to comment.