Skip to content

Commit

Permalink
[backend/frontend] Improvement for performances (#772)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Paris <[email protected]>
Co-authored-by: Romuald Lemesle <[email protected]>
  • Loading branch information
3 people authored Apr 28, 2024
1 parent 8609ae8 commit 36bd9cf
Show file tree
Hide file tree
Showing 71 changed files with 184 additions and 365 deletions.
12 changes: 8 additions & 4 deletions openbas-api/src/main/java/io/openbas/rest/inject/InjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.openbas.atomic_testing.AtomicTestingMapper.toDto;
import static io.openbas.config.SessionHelper.currentUser;
import static io.openbas.database.model.User.ROLE_ADMIN;
import static io.openbas.database.specification.CommunicationSpecification.fromInject;
Expand Down Expand Up @@ -240,7 +244,7 @@ public Inject updateInject(
@PreAuthorize("isExerciseObserver(#exerciseId)")
public Iterable<Inject> exerciseInjects(@PathVariable String exerciseId) {
return injectRepository.findAll(InjectSpecification.fromExercise(exerciseId)).stream()
.sorted(Inject.executionComparator).toList();
.sorted(Inject.executionComparator).toList();
}

@GetMapping("/api/exercises/{exerciseId}/injects/{injectId}")
Expand Down Expand Up @@ -399,9 +403,9 @@ public Inject createInjectForScenario(
@PreAuthorize("isScenarioObserver(#scenarioId)")
public Iterable<Inject> scenarioInjects(@PathVariable @NotBlank final String scenarioId) {
return this.injectRepository.findAll(InjectSpecification.fromScenario(scenarioId))
.stream()
.sorted(Inject.executionComparator)
.toList();
.stream()
.sorted(Inject.executionComparator)
.toList();
}

@GetMapping(SCENARIO_URI + "/{scenarioId}/injects/{injectId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class InjectHelperTest {
@Autowired
private UserRepository userRepository;

@Autowired
private InjectorContractRepository injectorContractRepository;

@Disabled
@DisplayName("Retrieve simple inject to run")
@Test
Expand Down Expand Up @@ -75,7 +78,7 @@ void injectsToRunTest() {
Inject inject = new Inject();
inject.setTitle("Test inject");
inject.setType(TYPE);
inject.setContract(EMAIL_DEFAULT);
inject.setInjectorContract(injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setEnabled(true);
inject.setExercise(exerciseSaved);
inject.setTeams(List.of(team));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.openbas.injectors;
package io.openbas.injects;

import io.openbas.database.model.Exercise;
import io.openbas.database.model.Inject;
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectorContractRepository;
import io.openbas.injectors.email.EmailContract;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,6 +25,8 @@ public class InjectCrudTest {

@Autowired
private ExerciseRepository exerciseRepository;
@Autowired
private InjectorContractRepository injectorContractRepository;

@DisplayName("Test inject creation with non null depends duration")
@Test
Expand All @@ -36,7 +40,7 @@ void createInjectSuccess() {
Inject inject = new Inject();
inject.setTitle("test");
inject.setType(TYPE);
inject.setContract(EMAIL_DEFAULT);
inject.setInjectorContract(injectorContractRepository.findById(EmailContract.EMAIL_DEFAULT).orElseThrow());
inject.setExercise(exerciseCreated);
inject.setDependsDuration(0L);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package io.openbas.injectors.email;
package io.openbas.injects.email;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.openbas.database.model.Execution;
import io.openbas.database.model.Inject;
import io.openbas.database.model.InjectExpectation;
import io.openbas.database.model.User;
import io.openbas.database.repository.InjectorContractRepository;
import io.openbas.database.repository.UserRepository;
import io.openbas.execution.ExecutableInject;
import io.openbas.execution.ExecutionContext;
import io.openbas.execution.ExecutionContextService;
import io.openbas.injectors.email.EmailContract;
import io.openbas.injectors.email.EmailExecutor;
import io.openbas.injectors.email.model.EmailContent;
import io.openbas.model.ExecutionProcess;
import io.openbas.model.inject.form.Expectation;
Expand All @@ -31,6 +34,8 @@ public class EmailExecutorTest {
@Autowired
private UserRepository userRepository;
@Autowired
private InjectorContractRepository injectorContractRepository;
@Autowired
private ExecutionContextService executionContextService;
@Resource
protected ObjectMapper mapper;
Expand All @@ -48,7 +53,7 @@ void process() throws Exception {
content.setExpectations(List.of(expectation));
Inject inject = new Inject();
inject.setType(EmailContract.TYPE);
inject.setContract(EmailContract.EMAIL_DEFAULT);
inject.setInjectorContract(injectorContractRepository.findById(EmailContract.EMAIL_DEFAULT).orElseThrow());
inject.setContent(this.mapper.valueToTree(content));
Iterable<User> users = this.userRepository.findAll();
List<ExecutionContext> userInjectContexts = fromIterable(users).stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.openbas.injectors.manual;
package io.openbas.injects.manual;

import io.openbas.injectors.manual.ManualExecutor;
import org.junit.jupiter.api.Assertions;
Expand Down
5 changes: 4 additions & 1 deletion openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.openbas.database.model.Inject;
import io.openbas.database.model.Scenario;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectorContractRepository;
import io.openbas.database.repository.ScenarioRepository;
import io.openbas.rest.inject.form.InjectInput;
import io.openbas.service.ScenarioService;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class InjectApiTest {
private ScenarioRepository scenarioRepository;
@Autowired
private InjectRepository injectRepository;
@Autowired
private InjectorContractRepository injectorContractRepository;

static String SCENARIO_ID;
static String INJECT_ID;
Expand All @@ -67,7 +70,7 @@ void addInjectForScenarioTest() throws Exception {
InjectInput input = new InjectInput();
input.setTitle("Test inject");
input.setType(TYPE);
input.setContract(EMAIL_DEFAULT);
input.setInjectorContract(EMAIL_DEFAULT);
input.setDependsDuration(0L);

// -- EXECUTE --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class ExerciseExpectationServiceTest {
@Autowired
private InjectExpectationRepository injectExpectationRepository;

@Autowired
private InjectorContractRepository injectorContractRepository;

static String EXERCISE_ID;

@BeforeAll
Expand All @@ -61,7 +64,7 @@ void beforeAll() {
Inject inject = new Inject();
inject.setTitle("test");
inject.setType(TYPE);
inject.setContract(EMAIL_DEFAULT);
inject.setInjectorContract(injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setExercise(exerciseCreated);
inject.setDependsDuration(0L);
Inject injectCreated = this.injectRepository.save(inject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static Inject getInjectForEmailContract() {
Inject inject = new Inject();
inject.setTitle(INJECT_EMAIL_NAME);
inject.setType(TYPE);
inject.setContract(EMAIL_DEFAULT);
inject.setEnabled(true);
inject.setDependsDuration(0L);
return inject;
Expand Down
27 changes: 1 addition & 26 deletions openbas-front/src/actions/InjectorContracts.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
import * as schema from './Schema';
import { getReferential, putReferential, postReferential, delReferential, simplePostCall, buildUri } from '../utils/Action';
import * as Constants from '../constants/ActionTypes';
import { api } from '../network';

export const fetchInjectorContracts = () => async (dispatch) => {
const uri = '/api/injector_contracts';
try {
const response = await api(schema.arrayOfInjectorContracts)
.get(buildUri(uri));
response.data.result.forEach((id) => {
const parsedContent = JSON.parse(response.data.entities.injector_contracts[id].injector_contract_content);
response.data.entities.injector_contracts[id] = {
...response.data.entities.injector_contracts[id],
...parsedContent,
};
});
dispatch({
type: Constants.DATA_FETCH_SUCCESS,
payload: response.data,
});
return response.data;
} catch (error) {
dispatch({ type: Constants.DATA_FETCH_ERROR, payload: error });
throw error;
}
};
import { getReferential, putReferential, postReferential, delReferential, simplePostCall } from '../utils/Action';

export const fetchInjectorContract = (injectorContractId) => (dispatch) => {
const uri = `/api/injector_contracts/${injectorContractId}`;
Expand Down
39 changes: 23 additions & 16 deletions openbas-front/src/actions/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ const entities = (key, state) => Object.values(maps(key, state));
const entity = (id, key, state) => state.referential.entities[key][id]?.asMutable({ deep: true });
const me = (state) => state.referential.entities.users[R.path(['logged', 'user'], state.app)];

const getInjectWithParsedInjectorContractContent = (i) => {
if (!i) {
return i;
}
return ({
...i,
inject_injector_contract: {
...i.inject_injector_contract,
injector_contract_content_parsed: JSON.parse(i.inject_injector_contract.injector_contract_content),
},
});
};
const getInjectsWithParsedInjectorContractContent = (injects) => {
if (R.isEmpty(injects)) {
return injects;
}
return injects.map(getInjectWithParsedInjectorContractContent);
};

export const storeHelper = (state) => ({
logged: () => state.app.logged,
getMe: () => me(state),
Expand All @@ -287,7 +306,7 @@ export const storeHelper = (state) => ({
getExerciseTeams: (id) => entities('teams', state).filter((i) => i.team_exercises.includes(id)),
getExerciseVariables: (id) => entities('variables', state).filter((i) => i.variable_exercise === id),
getExerciseArticles: (id) => entities('articles', state).filter((i) => i.article_exercise === id),
getExerciseInjects: (id) => entities('injects', state).filter((i) => i.inject_exercise === id),
getExerciseInjects: (id) => getInjectsWithParsedInjectorContractContent(entities('injects', state).filter((i) => i.inject_exercise === id)),
getExerciseCommunications: (id) => entities('communications', state).filter(
(i) => i.communication_exercise === id,
),
Expand Down Expand Up @@ -354,12 +373,12 @@ export const storeHelper = (state) => ({
getTags: () => entities('tags', state),
getTagsMap: () => maps('tags', state),
// injects
getInject: (id) => entity(id, 'injects', state),
getInject: (id) => getInjectWithParsedInjectorContractContent(entity(id, 'injects', state)),
getAtomicTesting: (id) => entity(id, 'atomics', state),
getAtomicTestingDetail: (id) => entity(id, 'atomicdetails', state),
getAtomicTestings: () => entities('atomics', state),
getTargetResults: (id, injectId) => entities('targetresults', state).filter((r) => (r.target_id === id) && (r.target_inject_id === injectId)),
getInjectsMap: () => maps('injects', state),
getInjectsMap: () => getInjectsWithParsedInjectorContractContent(maps('injects', state)),
getNextInjects: () => {
const sortFn = (a, b) => new Date(a.inject_date).getTime() - new Date(b.inject_date).getTime();
const injects = entities('injects', state).filter(
Expand Down Expand Up @@ -417,18 +436,6 @@ export const storeHelper = (state) => ({
getInjectorsMap: () => maps('injectors', state),
// injectors contracts
getInjectorContract: (id) => entity(id, 'injector_contracts', state),
getInjectorContracts: () => entities('injector_contracts', state),
getInjectorContractsMap: () => maps('injector_contracts', state),
getInjectorContractsMapByType: () => R.indexBy(R.path(['config', 'type']), entities('injector_contracts', state)),
getInjectorContractsWithNoTeams: () => R.uniq(
entities('injector_contracts', state)
.map((t) => ({
hasTeams: t.fields.filter((f) => f.key === 'teams').length > 0,
...t,
}))
.filter((t) => !t.hasTeams)
.map((t) => t.config.type),
),
// collectors
getCollector: (id) => entity(id, 'collectors', state),
getCollectors: () => entities('collectors', state),
Expand Down Expand Up @@ -476,5 +483,5 @@ export const storeHelper = (state) => ({
getScenarioVariables: (id) => entities('variables', state).filter((i) => i.variable_scenario === id),
getScenarioArticles: (id) => entities('articles', state).filter((i) => i.article_scenario === id),
getScenarioChallenges: (id) => entities('challenges', state).filter((c) => c.challenge_scenarios.includes(id)),
getScenarioInjects: (id) => entities('injects', state).filter((i) => i.inject_scenario === id),
getScenarioInjects: (id) => getInjectsWithParsedInjectorContractContent(entities('injects', state).filter((i) => i.inject_scenario === id)),
});
7 changes: 6 additions & 1 deletion openbas-front/src/actions/injects/Inject.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ export type InjectInput = {
inject_depends_duration_seconds: number;
};

export type InjectStore = Omit<Inject, 'inject_tags' | 'inject_content'> & {
export type InjectStore = Omit<Inject, 'inject_tags' | 'inject_content' | 'inject_injector_contract'> & {
inject_tags: string[] | undefined;
inject_content: { expectationScore: number, challenges: string[] | undefined }
inject_injector_contract: {
// as we don't know the type of the content of a contract we need to put any here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
injector_contract_content_parsed: any
} & Inject['inject_injector_contract']
};

export type InjectExpectationStore = Omit<InjectExpectation, 'inject_expectation_team', 'inject_expectation_inject'> & {
Expand Down
2 changes: 1 addition & 1 deletion openbas-front/src/actions/injects/inject-helper.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Exercise, Inject, Scenario } from '../../utils/api-types';
import type { Exercise, Inject, Scenario, InjectExpectation } from '../../utils/api-types';

export interface InjectHelper {
getExerciseInjects: (exerciseId: Exercise['exercise_id']) => Inject[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AtomicTestingHelper } from '../../../../actions/atomic_testings/at
import AtomicTestingPopover from './AtomicTestingPopover';
import { useFormatter } from '../../../../components/i18n';
import Transition from '../../../../components/common/Transition';
import { AtomicTestingResultContext } from '../../components/Context';
import { AtomicTestingResultContext } from '../../common/Context';
import StatusChip from './StatusChip';
import type { AtomicTestingOutput } from '../../../../utils/api-types';
import useDataLoader from '../../../../utils/ServerSideEvent';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ButtonPopover, { ButtonPopoverEntry } from '../../../../components/common
import DialogDelete from '../../../../components/common/DialogDelete';
import { deleteAtomicTesting, fetchAtomicTestingForUpdate, updateAtomicTesting } from '../../../../actions/atomic_testings/atomic-testing-actions';
import { useHelper } from '../../../../store';
import { AtomicTestingResultContext } from '../../components/Context';
import { AtomicTestingResultContext } from '../../common/Context';
import useDataLoader from '../../../../utils/ServerSideEvent';
import type { AtomicTestingHelper } from '../../../../actions/atomic_testings/atomic-testing-helper';
import UpdateInject from '../../common/injects/UpdateInject';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import AtomicTestingHeader from './AtomicTestingHeader';
import { fetchAtomicTesting } from '../../../../actions/atomic_testings/atomic-testing-actions';
import type { AtomicTestingOutput } from '../../../../utils/api-types';
import type { AtomicTestingHelper } from '../../../../actions/atomic_testings/atomic-testing-helper';
import { AtomicTestingResultContext, AtomicTestingResultContextType } from '../../components/Context';
import { AtomicTestingResultContext, AtomicTestingResultContextType } from '../../common/Context';

const AtomicTesting = lazy(() => import('./AtomicTesting'));
const AtomicTestingDetail = lazy(() => import('./AtomicTestingDetail'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ const Createinject: FunctionComponent<Props> = ({ title, onCreateInject, isAtomi
setActiveStep(0);
};

const submitCreation = async (data: Inject) => {
handleReset();
await onCreateInject(data);
};

// Fetching data
const { attackPatterns, attackPatternsMap } = useHelper((helper: AttackPatternHelper) => ({
attackPatterns: helper.getAttackPatterns(),
Expand Down Expand Up @@ -229,7 +224,7 @@ const Createinject: FunctionComponent<Props> = ({ title, onCreateInject, isAtomi
handleClose={() => setOpen(false)}
handleBack={handleBack}
handleReset={handleReset}
onCreateInject={submitCreation}
onCreateInject={onCreateInject}
isAtomic={isAtomic}
{...props}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FunctionComponent, useContext } from 'react';
import { Button } from '@mui/material';
import InjectDefinition from './InjectDefinition';
import { PermissionsContext } from '../../components/Context';
import { PermissionsContext } from '..';
import type { Inject, Tag } from '../../../../utils/api-types';
import { useHelper } from '../../../../store';
import type { InjectHelper } from '../../../../actions/injects/inject-helper';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Transition from '../../../../components/common/Transition';
import ChannelIcon from '../../components/channels/ChannelIcon';
import type { Theme } from '../../../../components/Theme';
import { useAppDispatch } from '../../../../utils/hooks';
import { PermissionsContext } from '../../components/Context';
import { PermissionsContext } from '../Context';
import useDataLoader from '../../../../utils/ServerSideEvent';
import { useHelper } from '../../../../store';
import type { ChannelsHelper } from '../../../../actions/channels/channel-helper';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import useDataLoader from '../../../../utils/ServerSideEvent';
import { useAppDispatch } from '../../../../utils/hooks';
import { useHelper } from '../../../../store';
import type { ChallengesHelper } from '../../../../actions/helper';
import { PermissionsContext } from '../../components/Context';
import { PermissionsContext } from '../Context';
import type { Challenge } from '../../../../utils/api-types';

const useStyles = makeStyles((theme: Theme) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useAppDispatch } from '../../../../utils/hooks';
import type { Theme } from '../../../../components/Theme';
import { useHelper } from '../../../../store';
import type { DocumentsHelper, UsersHelper } from '../../../../actions/helper';
import { PermissionsContext } from '../../components/Context';
import { PermissionsContext } from '../Context';
import type { Document } from '../../../../utils/api-types';

const useStyles = makeStyles((theme: Theme) => ({
Expand Down
Loading

0 comments on commit 36bd9cf

Please sign in to comment.