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

Rule details flyout on detector view page #292

Merged
Show file tree
Hide file tree
Changes from 4 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
71 changes: 14 additions & 57 deletions public/pages/CreateDetector/containers/CreateDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import {
RuleItem,
RuleItemInfo,
} from '../components/DefineDetector/components/DetectionRules/types/interfaces';
import { RuleInfo } from '../../../../server/models/interfaces';
import { NotificationsStart } from 'opensearch-dashboards/public';
import {
errorNotificationToast,
successNotificationToast,
getPlugins,
} from '../../../utils/helpers';
import { RulesViewModelActor } from '../../Rules/models/RulesViewModelActor';

interface CreateDetectorProps extends RouteComponentProps {
isEdit: boolean;
Expand All @@ -55,9 +55,11 @@ interface CreateDetectorState {

export default class CreateDetector extends Component<CreateDetectorProps, CreateDetectorState> {
static contextType = CoreServicesContext;
private rulesViewModelActor: RulesViewModelActor;

constructor(props: CreateDetectorProps) {
super(props);
this.rulesViewModelActor = new RulesViewModelActor(props.services.ruleService);
this.state = {
currentStep: DetectorCreationStep.DEFINE_DETECTOR,
detector: EMPTY_DEFAULT_DETECTOR,
Expand Down Expand Up @@ -177,13 +179,21 @@ export default class CreateDetector extends Component<CreateDetectorProps, Creat
}

async setupRulesState() {
const prePackagedRules = await this.getRules(true);
const customRules = await this.getRules(false);
const { detector_type } = this.state.detector;

const allRules = await this.rulesViewModelActor.fetchRules(undefined, {
bool: {
must: [{ match: { 'rule.category': `${detector_type}` } }],
},
});

const prePackagedRules = allRules.filter((rule) => rule.prePackaged);
const customRules = allRules.filter((rule) => !rule.prePackaged);

this.setState({
rulesState: {
...this.state.rulesState,
allRules: customRules.concat(prePackagedRules),
allRules: customRules.concat(prePackagedRules).map((rule) => ({ ...rule, enabled: true })),
page: {
index: 0,
},
Expand All @@ -210,59 +220,6 @@ export default class CreateDetector extends Component<CreateDetectorProps, Creat
this.setState({ plugins });
}

async getRules(prePackaged: boolean): Promise<RuleItemInfo[]> {
try {
const { detector_type } = this.state.detector;

if (!detector_type) {
return [];
}

const rulesRes = await this.props.services.ruleService.getRules(prePackaged, {
from: 0,
size: 5000,
query: {
nested: {
path: 'rule',
query: {
bool: {
must: [{ match: { 'rule.category': `${detector_type}` } }],
},
},
},
},
});

if (rulesRes.ok) {
const rules: RuleItemInfo[] = rulesRes.response.hits.hits.map((ruleInfo: RuleInfo) => {
return {
...ruleInfo,
enabled: true,
prePackaged,
};
});

return rules;
} else {
errorNotificationToast(
this.props.notifications,
'retrieve',
`${prePackaged ? 'pre-packaged' : 'custom'}`,
rulesRes.error
);
return [];
}
} catch (error: any) {
errorNotificationToast(
this.props.notifications,
'retrieve',
`${prePackaged ? 'pre-packaged' : 'custom'}`,
error
);
return [];
}
}

onPageChange = (page: { index: number; size: number }) => {
this.setState({
rulesState: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { ContentPanel } from '../../../../components/ContentPanel';
import React, { useContext, useEffect, useState } from 'react';
import React, { useContext, useEffect, useState, useMemo } from 'react';
import { EuiAccordion, EuiButton, EuiInMemoryTable, EuiSpacer, EuiText } from '@elastic/eui';
import { RuleItem } from '../../../CreateDetector/components/DefineDetector/components/DetectionRules/types/interfaces';
import { getRulesColumns } from '../../../CreateDetector/components/DefineDetector/components/DetectionRules/utils/constants';
Expand All @@ -16,6 +16,7 @@ import { NotificationsStart } from 'opensearch-dashboards/public';
import { RulesTable } from '../../../Rules/components/RulesTable/RulesTable';
import { RuleTableItem } from '../../../Rules/utils/helpers';
import { RuleViewerFlyout } from '../../../Rules/components/RuleViewerFlyout/RuleViewerFlyout';
import { RulesViewModelActor } from '../../../Rules/models/RulesViewModelActor';

export interface DetectorRulesViewProps {
detector: Detector;
Expand Down Expand Up @@ -59,33 +60,12 @@ export const DetectorRulesView: React.FC<DetectorRulesViewProps> = (props) => {
];
const services = useContext(ServicesContext);

useEffect(() => {
const getRules = async (prePackaged: boolean): Promise<RuleInfo[]> => {
const getRulesRes = await services?.ruleService.getRules(prePackaged, {
from: 0,
size: 5000,
query: {
nested: {
path: 'rule',
query: {
bool: {
must: [
{ match: { 'rule.category': `${props.detector.detector_type.toLowerCase()}` } },
],
},
},
},
},
});

if (getRulesRes?.ok) {
return getRulesRes.response.hits.hits;
} else {
errorNotificationToast(props.notifications, 'retrieve', 'rules', getRulesRes?.error);
return [];
}
};
const rulesViewModelActor = useMemo(
() => (services ? new RulesViewModelActor(services?.ruleService) : null),
[services]
);

useEffect(() => {
const updateRulesState = async () => {
setLoading(true);
const enabledPrePackagedRuleIds = new Set(
Expand All @@ -95,26 +75,32 @@ export const DetectorRulesView: React.FC<DetectorRulesViewProps> = (props) => {
props.detector.inputs[0].detector_input.custom_rules.map((ruleInfo) => ruleInfo.id)
);

const prePackagedRules = await getRules(true);
const customRules = await getRules(false);
const allRules = await rulesViewModelActor?.fetchRules(undefined, {
bool: {
must: [{ match: { 'rule.category': `${props.detector.detector_type.toLowerCase()}` } }],
},
});

const prePackagedRules = allRules?.filter((rule) => rule.prePackaged);
const customRules = allRules?.filter((rule) => !rule.prePackaged);

const enabledPrePackagedRules = prePackagedRules.filter((hit: RuleInfo) => {
const enabledPrePackagedRules = prePackagedRules?.filter((hit: RuleInfo) => {
return enabledPrePackagedRuleIds.has(hit._id);
});

const enabledCustomRules = customRules.filter((hit: RuleInfo) => {
const enabledCustomRules = customRules?.filter((hit: RuleInfo) => {
return enabledCustomRuleIds.has(hit._id);
});

const enabledRuleItems = translateToRuleItems(
enabledPrePackagedRules,
enabledCustomRules,
enabledPrePackagedRules || [],
enabledCustomRules || [],
props.detector.detector_type,
() => true
);
const allRuleItems = translateToRuleItems(
prePackagedRules,
customRules,
prePackagedRules || [],
customRules || [],
props.detector.detector_type,
(ruleInfo) =>
enabledPrePackagedRuleIds.has(ruleInfo._id) || enabledCustomRuleIds.has(ruleInfo._id)
Expand All @@ -129,9 +115,21 @@ export const DetectorRulesView: React.FC<DetectorRulesViewProps> = (props) => {
});
}, [services, props.detector]);

const onRuleDetails = (ruleItem: RuleItem) => {
setFlyoutData(() => ({
title: ruleItem.name,
level: ruleItem.severity,
category: ruleItem.logType,
description: ruleItem.description,
source: ruleItem.library,
ruleInfo: ruleItem.ruleInfo,
ruleId: ruleItem.id,
}));
};

const rules = (
<EuiInMemoryTable
columns={getRulesColumns(false)}
columns={getRulesColumns(false, undefined, undefined, onRuleDetails)}
items={enabledRuleItems}
itemId={(item: RuleItem) => `${item.name}`}
pagination
Expand Down
14 changes: 9 additions & 5 deletions public/pages/Rules/models/RulesViewModelActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export class RulesViewModelActor {
return this.rulesViewModel.allRules;
}

public async fetchRules(terms?: { [key: string]: string[] }): Promise<RuleItemInfoBase[]> {
let prePackagedRules = await this.getRules(true, terms);
let customRules = await this.getRules(false, terms);
public async fetchRules(
terms?: { [key: string]: string[] },
query?: any
): Promise<RuleItemInfoBase[]> {
let prePackagedRules = await this.getRules(true, terms, query);
let customRules = await this.getRules(false, terms, query);

prePackagedRules = this.extractAndAddDetection(prePackagedRules);
customRules = this.extractAndAddDetection(customRules);
Expand All @@ -41,15 +44,16 @@ export class RulesViewModelActor {
prePackaged: boolean,
terms?: {
[key: string]: string[];
}
},
query?: any
): Promise<RuleItemInfoBase[]> {
const getRulesRes = await this.service.getRules(prePackaged, {
from: 0,
size: 5000,
query: {
nested: {
path: 'rule',
query: {
query: query || {
terms: terms
? terms
: {
Expand Down