Skip to content

Commit

Permalink
[frontend] Remove attack pattern store
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois authored Dec 30, 2024
1 parent c1040ac commit 38327f2
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 52 deletions.
6 changes: 0 additions & 6 deletions openbas-front/src/actions/attack_patterns/AttackPattern.d.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const InjectResultList: FunctionComponent<Props> = ({
},
{
field: 'inject_status',
label: 'Status',
label: 'Execution status',
isSortable: false,
value: (injectResultOutput: InjectResultOutput) => {
return (<ItemStatus isInject status={injectResultOutput.inject_status?.status_name} label={t(injectResultOutput.inject_status?.status_name)} variant="inList" />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Button, Typography } from '@mui/material';
import { makeStyles } from '@mui/styles';
import { FunctionComponent, useEffect } from 'react';

import type { AttackPatternStore } from '../../../../actions/attack_patterns/AttackPattern';
import type { AttackPatternHelper } from '../../../../actions/attack_patterns/attackpattern-helper';
import type { InjectorContractHelper } from '../../../../actions/injector_contracts/injector-contract-helper';
import { fetchInjectorsContracts } from '../../../../actions/InjectorContracts';
Expand Down Expand Up @@ -173,7 +172,7 @@ const MitreFilter: FunctionComponent<MitreFilterProps> = ({

// Attack Pattern
const getAttackPatterns = (killChainPhase: KillChainPhase) => {
return attackPatterns.filter((attackPattern: AttackPatternStore) => attackPattern.attack_pattern_kill_chain_phases?.includes(killChainPhase.phase_id));
return attackPatterns.filter((attackPattern: AttackPattern) => attackPattern.attack_pattern_kill_chain_phases?.includes(killChainPhase.phase_id));
};

return (
Expand Down
43 changes: 25 additions & 18 deletions openbas-front/src/admin/components/common/injects/CreateInject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Chip, Grid, List, ListItem, ListItemButton, ListItemIcon, ListItemText,
import { makeStyles } from '@mui/styles';
import { CSSProperties, FunctionComponent, useEffect, useMemo, useRef, useState } from 'react';

import type { AttackPatternStore } from '../../../../actions/attack_patterns/AttackPattern';
import type { AttackPatternHelper } from '../../../../actions/attack_patterns/attackpattern-helper';
import { searchInjectorContracts } from '../../../../actions/InjectorContracts';
import type { InjectorHelper } from '../../../../actions/injectors/injector-helper';
Expand All @@ -18,7 +17,7 @@ import { Header } from '../../../../components/common/SortHeadersList';
import { useFormatter } from '../../../../components/i18n';
import PlatformIcon from '../../../../components/PlatformIcon';
import { useHelper } from '../../../../store';
import type { FilterGroup, Inject, InjectorContractOutput, KillChainPhase } from '../../../../utils/api-types';
import { AttackPattern, FilterGroup, Inject, InjectorContractOutput, KillChainPhase } from '../../../../utils/api-types';
import computeAttackPatterns from '../../../../utils/injector_contract/InjectorContractUtils';
import { isNotEmptyField } from '../../../../utils/utils';
import CreateInjectDetails from './CreateInjectDetails';
Expand Down Expand Up @@ -100,15 +99,15 @@ const CreateInject: FunctionComponent<Props> = ({ title, onCreateInject, open =
field: 'kill_chain_phase',
label: 'Kill chain phase',
isSortable: false,
value: (_: InjectorContractOutput, killChainPhase: KillChainPhase, __: Record<string, AttackPatternStore>) => {
value: (_: InjectorContractOutput, killChainPhase: KillChainPhase, __: Record<string, AttackPattern>) => {
return <>{(killChainPhase ? killChainPhase.phase_name : t('Unknown'))}</>;
},
},
{
field: 'injector_contract_labels',
label: 'Label',
isSortable: false,
value: (contract: InjectorContractOutput, _: KillChainPhase, __: Record<string, AttackPatternStore>) => (
value: (contract: InjectorContractOutput, _: KillChainPhase, __: Record<string, AttackPattern>) => (
<Tooltip title={tPick(contract.injector_contract_labels)}>
<>{tPick(contract.injector_contract_labels)}</>
</Tooltip>
Expand All @@ -118,24 +117,32 @@ const CreateInject: FunctionComponent<Props> = ({ title, onCreateInject, open =
field: 'injector_contract_platforms',
label: 'Platforms',
isSortable: false,
value: (contract: InjectorContractOutput, _: KillChainPhase, __: Record<string, AttackPatternStore>) => contract.injector_contract_platforms?.map(
(platform: string) => <PlatformIcon key={platform} width={20} platform={platform} marginRight={10} />,
value: (contract: InjectorContractOutput, _: KillChainPhase, __: Record<string, AttackPattern>) => (
<>
{(contract.injector_contract_platforms ?? []).map(
(platform: string) => <PlatformIcon key={platform} width={20} platform={platform} marginRight={10} />,
)}
</>
),
},
{
field: 'attack_patterns',
label: 'Attack patterns',
isSortable: false,
value: (contract: InjectorContractOutput, _: KillChainPhase, contractAttackPatterns: Record<string, AttackPatternStore>) => contractAttackPatterns
.map((contractAttackPattern: AttackPatternStore) => (
<Chip
key={`${contract.injector_contract_id}-${contractAttackPattern.attack_pattern_id}-${Math.random()}`}
variant="outlined"
classes={{ root: classes.chipInList }}
color="primary"
label={contractAttackPattern.attack_pattern_external_id}
/>
)),
value: (contract: InjectorContractOutput, _: KillChainPhase, contractAttackPatterns: Record<string, AttackPattern>) => (
<>
{Object.values(contractAttackPatterns)
.map((contractAttackPattern: AttackPattern) => (
<Chip
key={`${contract.injector_contract_id}-${contractAttackPattern.attack_pattern_id}-${Math.random()}`}
variant="outlined"
classes={{ root: classes.chipInList }}
color="primary"
label={contractAttackPattern.attack_pattern_external_id}
/>
))}
</>
),
},
], []);

Expand Down Expand Up @@ -215,7 +222,7 @@ const CreateInject: FunctionComponent<Props> = ({ title, onCreateInject, open =
if (selectedContract !== null && contracts[selectedContract] !== undefined) {
const selectedContractAttackPatterns = computeAttackPatterns(contracts[selectedContract], attackPatternsMap);
// eslint-disable-next-line max-len
const killChainPhaseforSelection = selectedContractAttackPatterns.map((contractAttackPattern: AttackPatternStore) => contractAttackPattern.attack_pattern_kill_chain_phases ?? []).flat().at(0);
const killChainPhaseforSelection = selectedContractAttackPatterns.map((contractAttackPattern: AttackPattern) => contractAttackPattern.attack_pattern_kill_chain_phases ?? []).flat().at(0);
selectedContractKillChainPhase = killChainPhaseforSelection && killChainPhasesMap[killChainPhaseforSelection] && killChainPhasesMap[killChainPhaseforSelection].phase_name;
}

Expand Down Expand Up @@ -264,7 +271,7 @@ const CreateInject: FunctionComponent<Props> = ({ title, onCreateInject, open =
{contracts.map((contract, index) => {
const contractAttackPatterns = computeAttackPatterns(contract, attackPatternsMap);
// eslint-disable-next-line max-len
const contractKillChainPhase = contractAttackPatterns.map((contractAttackPattern: AttackPatternStore) => contractAttackPattern.attack_pattern_kill_chain_phases ?? []).flat().at(0);
const contractKillChainPhase = contractAttackPatterns.map((contractAttackPattern: AttackPattern) => contractAttackPattern.attack_pattern_kill_chain_phases ?? []).flat().at(0);
const resolvedContractKillChainPhase = contractKillChainPhase && killChainPhasesMap[contractKillChainPhase];
return (
<ListItemButton
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { makeStyles, useTheme } from '@mui/styles';
import { FunctionComponent } from 'react';

import type { AttackPatternStore } from '../../../../actions/attack_patterns/AttackPattern';
import type { InjectExpectationResultsByAttackPatternStore } from '../../../../actions/exercises/Exercise';
import type { Theme } from '../../../../components/Theme';
import type { AttackPattern, KillChainPhase } from '../../../../utils/api-types';
Expand Down Expand Up @@ -44,8 +43,8 @@ const KillChainPhaseColumn: FunctionComponent<KillChainPhaseComponentProps> = ({
return 0;
};
// Inject Results
const getInjectResult = (attack: AttackPatternStore) => {
return injectResults.find(injectResult => injectResult.inject_attack_pattern === attack.attack_pattern_id);
const getInjectResult = (attack: AttackPattern) => {
return injectResults.find(injectResult => injectResult.inject_attack_pattern?.includes(attack.attack_pattern_id));
};
return (
<div style={{ marginBottom: 16 }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { makeStyles } from '@mui/styles';
import * as R from 'ramda';
import { FunctionComponent } from 'react';

import type { AttackPatternStore } from '../../../../actions/attack_patterns/AttackPattern';
import type { AttackPatternHelper } from '../../../../actions/attack_patterns/attackpattern-helper';
import type { InjectExpectationResultsByAttackPatternStore } from '../../../../actions/exercises/Exercise';
import type { KillChainPhaseHelper } from '../../../../actions/kill_chain_phases/killchainphase-helper';
Expand Down Expand Up @@ -52,10 +51,10 @@ const MitreMatrix: FunctionComponent<Props> = ({
.filter(injectResult => !!injectResult.inject_attack_pattern)
.flatMap(injectResult => injectResult.inject_attack_pattern) as unknown as string[],
);
const resultAttackPatterns: AttackPatternStore[] = resultAttackPatternIds.map((attackPatternId: string) => attackPatternMap[attackPatternId])
const resultAttackPatterns: AttackPattern[] = resultAttackPatternIds.map((attackPatternId: string) => attackPatternMap[attackPatternId])
.filter((attackPattern: AttackPattern) => !!attackPattern);
const getAttackPatterns = (killChainPhase: KillChainPhase) => {
return resultAttackPatterns.filter((attackPattern: AttackPatternStore) => attackPattern.attack_pattern_kill_chain_phases?.includes(killChainPhase.phase_id));
return resultAttackPatterns.filter((attackPattern: AttackPattern) => attackPattern.attack_pattern_kill_chain_phases?.includes(killChainPhase.phase_id));
};
// Kill Chain Phase
const resultKillChainPhases = R.uniq(resultAttackPatterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { makeStyles } from '@mui/styles';
import * as R from 'ramda';
import { FunctionComponent } from 'react';

import type { AttackPatternStore } from '../../../../actions/attack_patterns/AttackPattern';
import type { AttackPatternHelper } from '../../../../actions/attack_patterns/attackpattern-helper';
import type { KillChainPhaseHelper } from '../../../../actions/kill_chain_phases/killchainphase-helper';
import { useHelper } from '../../../../store';
Expand Down Expand Up @@ -39,7 +38,7 @@ const MitreMatrixDummy: FunctionComponent = () => {
return (k1.phase_order ?? 0) - (k2.phase_order ?? 0);
};
const getAttackPatterns = (killChainPhase: KillChainPhase) => {
return R.take(random(3, 6), attackPatterns.filter((attackPattern: AttackPatternStore) => attackPattern.attack_pattern_kill_chain_phases?.includes(killChainPhase.phase_id)));
return R.take(random(3, 6), attackPatterns.filter((attackPattern: AttackPattern) => attackPattern.attack_pattern_kill_chain_phases?.includes(killChainPhase.phase_id)));
};
return (
<div className={classes.container}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { makeStyles } from '@mui/styles';
import { useEffect, useState } from 'react';
import * as React from 'react';

import type { AttackPatternStore } from '../../../actions/attack_patterns/AttackPattern';
import MitreFilter, { MITRE_FILTER_KEY } from '../../../admin/components/common/filters/MitreFilter';
import mitreAttack from '../../../static/images/misc/attack.png';
import type { Filter, SearchPaginationInput } from '../../../utils/api-types';
import type { AttackPattern, Filter, SearchPaginationInput } from '../../../utils/api-types';
import { useFormatter } from '../../i18n';
import SearchFilter from '../../SearchFilter';
import Drawer from '../Drawer';
Expand Down Expand Up @@ -52,7 +51,7 @@ interface Props<T> {
availableFilters?: string[];
helpers?: FilterHelpers;
children?: React.ReactElement | null;
attackPatterns?: AttackPatternStore[];
attackPatterns?: AttackPattern[];
}

/**
Expand Down Expand Up @@ -124,7 +123,7 @@ const PaginationComponent = <T extends object>({
(f: Filter) => f.key === MITRE_FILTER_KEY,
)?.[0]?.values?.map(
(externalId: string) => attackPatterns?.find(
(a: AttackPatternStore) => a.attack_pattern_external_id === externalId,
(a: AttackPattern) => a.attack_pattern_external_id === externalId,
)?.attack_pattern_name,
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { makeStyles } from '@mui/styles';
import { useEffect, useState } from 'react';
import * as React from 'react';

import type { AttackPatternStore } from '../../../../actions/attack_patterns/AttackPattern';
import InjectorContractSwitchFilter from '../../../../admin/components/common/filters/InjectorContractSwitchFilter';
import MitreFilter, { MITRE_FILTER_KEY } from '../../../../admin/components/common/filters/MitreFilter';
import mitreAttack from '../../../../static/images/misc/attack.png';
import type { Filter, PropertySchemaDTO, SearchPaginationInput } from '../../../../utils/api-types';
import type { AttackPattern, Filter, PropertySchemaDTO, SearchPaginationInput } from '../../../../utils/api-types';
import { useFormatter } from '../../../i18n';
import ClickableModeChip from '../../chips/ClickableModeChip';
import Drawer from '../../Drawer';
Expand Down Expand Up @@ -48,7 +47,7 @@ interface Props<T> {
availableFilterNames?: string[];
queryableHelpers: QueryableHelpers;
topBarButtons?: React.ReactElement | null;
attackPatterns?: AttackPatternStore[];
attackPatterns?: AttackPattern[];
reloadContentCount?: number;
}

Expand Down Expand Up @@ -108,7 +107,7 @@ const PaginationComponentV2 = <T extends object>({
(f: Filter) => f.key === MITRE_FILTER_KEY,
)?.[0]?.values?.map(
(externalId: string) => attackPatterns?.find(
(a: AttackPatternStore) => a.attack_pattern_external_id === externalId,
(a: AttackPattern) => a.attack_pattern_external_id === externalId,
)?.attack_pattern_name,
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as R from 'ramda';

import type { AttackPatternStore } from '../../actions/attack_patterns/AttackPattern';
import type { InjectorContractOutput } from '../api-types';
import { AttackPattern, InjectorContractOutput } from '../api-types';

const computeAttackPatterns = (contract: InjectorContractOutput, attackPatternsMap: Record<string, AttackPatternStore>) => {
const computeAttackPatterns = (contract: InjectorContractOutput, attackPatternsMap: Record<string, AttackPattern>) => {
const attackPatternParents = (contract.injector_contract_attack_patterns ?? []).flatMap((attackPattern) => {
const attackPatternParentId = attackPatternsMap[attackPattern]?.attack_pattern_parent;
if (attackPatternParentId) {
Expand Down

0 comments on commit 38327f2

Please sign in to comment.