Skip to content

Commit

Permalink
[backend] wip
Browse files Browse the repository at this point in the history
  • Loading branch information
savacano28 committed Nov 7, 2024
1 parent f7b6118 commit f0717a0
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.openbas.database.model.ExecutionStatus;
import io.openbas.utils.InjectMapper;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.time.Instant;
Expand Down Expand Up @@ -57,5 +57,5 @@ public InjectStatusSimple getStatus() {
private List<TargetSimple> targets = new ArrayList<>();

@JsonProperty("inject_expectation_results")
private List<InjectMapper.ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();
private List<ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.openbas.database.model.InjectStatusCommandLine;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import java.time.Instant;
Expand Down Expand Up @@ -40,21 +40,14 @@ public class InjectResultOverviewOutput {
@JsonProperty("inject_commands_lines")
private InjectStatusCommandLine commandsLines;

@JsonProperty("inject_expectations")
private List<InjectExpectationSimple> expectations;

@JsonProperty("inject_type")
private String type;

@Schema(description = "Kill Chain Phases")
@JsonProperty("inject_kill_chain_phases")
@NotNull
private List<KillChainPhaseSimple> killChainPhases;
@JsonProperty("injects_tags")
private List<String> tagIds;

@Schema(description = "Attack Patterns")
@JsonProperty("inject_attack_patterns")
@NotNull
private List<AttackPatternSimple> attackPatterns;
@JsonProperty("injects_documents")
private List<String> documentIds;

@Schema(description = "Full contract")
@JsonProperty("inject_injector_contract")
Expand All @@ -64,21 +57,33 @@ public class InjectResultOverviewOutput {
@JsonProperty("inject_status")
private InjectStatusSimple status;

@Default
@Schema(description = "Result of expectations")
@JsonProperty("inject_expectation_results")
@NotNull
private List<ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();
@JsonProperty("inject_expectations")
private List<InjectExpectationSimple> expectations;

@JsonProperty("injects_tags")
private List<String> tagIds;
@Schema(description = "Kill Chain Phases")
@JsonProperty("inject_kill_chain_phases")
@NotNull
private List<KillChainPhaseSimple> killChainPhases;

@JsonProperty("injects_documents")
private List<String> documentIds;
@Schema(description = "Attack Patterns")
@JsonProperty("inject_attack_patterns")
@NotNull
private List<AttackPatternSimple> attackPatterns;

@JsonProperty("inject_ready")
private Boolean isReady;

@JsonProperty("inject_updated_at")
private Instant updatedAt;

// -- PROCESSED ATTRIBUTES --

@Default
@Schema(description = "Result of expectations")
@JsonProperty("inject_expectation_results")
@NotNull
private List<ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();

@JsonProperty("inject_targets")
private List<InjectTargetWithResult> targets;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.openbas.atomic_testing.TargetType;
import io.openbas.database.model.Endpoint.PLATFORM_TYPE;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import io.openbas.rest.helper.TeamHelper;
import io.openbas.rest.inject.form.InjectExpectationResultsByAttackPattern;
import io.openbas.service.*;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import io.openbas.utils.ResultUtils;
import io.openbas.utils.pagination.SearchPaginationInput;
import jakarta.persistence.criteria.Join;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.openbas.database.model.Tag;
import io.openbas.helper.MultiIdSetDeserializer;
import io.openbas.rest.atomic_testing.form.TargetSimple;
import io.openbas.utils.InjectMapper;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -63,5 +63,5 @@ public class ExerciseSimple {
private List<TargetSimple> targets = new ArrayList<>();

@JsonProperty("exercise_global_score")
private List<InjectMapper.ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();
private List<ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import io.openbas.atomic_testing.TargetType;
import io.openbas.config.OpenBASConfig;
import io.openbas.database.model.*;
import io.openbas.database.raw.*;
import io.openbas.database.raw.RawExerciseSimple;
import io.openbas.database.raw.RawInjectExpectation;
import io.openbas.database.repository.*;
import io.openbas.rest.atomic_testing.form.TargetSimple;
import io.openbas.rest.exception.ElementNotFoundException;
Expand All @@ -25,6 +26,7 @@
import io.openbas.service.TeamService;
import io.openbas.service.VariableService;
import io.openbas.utils.AtomicTestingUtils;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import io.openbas.utils.ExerciseMapper;
import io.openbas.utils.InjectMapper;
import io.openbas.utils.ResultUtils;
Expand Down Expand Up @@ -65,10 +67,10 @@ public class ExerciseService {
private final ExerciseMapper exerciseMapper;
private final InjectMapper injectMapper;
private final ResultUtils resultUtils;

private final AssetRepository assetRepository;
private final AssetGroupRepository assetGroupRepository;
private final InjectExpectationRepository injectExpectationRepository;

private final ArticleRepository articleRepository;
private final ExerciseRepository exerciseRepository;
private final TeamRepository teamRepository;
Expand Down Expand Up @@ -489,7 +491,7 @@ public Iterable<ExerciseSimple> scenarioExercises(@NotBlank String scenarioId) {
return exerciseMapper.getExerciseSimples(exercises);
}

public List<InjectMapper.ExpectationResultsByType> getGlobalResults(@NotBlank String exerciseId) {
public List<ExpectationResultsByType> getGlobalResults(@NotBlank String exerciseId) {
return resultUtils.getResultsByTypes(exerciseRepository.findInjectsByExercise(exerciseId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.openbas.database.model.Inject;
import io.openbas.helper.MonoIdDeserializer;
import io.openbas.utils.AtomicTestingUtils;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.openbas.rest.statistic.response.PlatformStatistic;
import io.openbas.rest.statistic.response.StatisticElement;
import io.openbas.utils.AtomicTestingUtils;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.openbas.rest.inject.form.InjectExpectationResultsByAttackPattern;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType;
import java.time.Instant;
import java.util.List;
import java.util.Map;
Expand Down
103 changes: 49 additions & 54 deletions openbas-api/src/main/java/io/openbas/utils/AtomicTestingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import io.openbas.database.raw.*;
import io.openbas.expectation.ExpectationType;
import io.openbas.rest.atomic_testing.form.InjectTargetWithResult;
import io.openbas.utils.InjectMapper.ExpectationResultsByType;
import io.openbas.utils.InjectMapper.ResultDistribution;
import jakarta.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -228,7 +226,7 @@ public static List<InjectTargetWithResult> getTargetsWithResultsFromRaw(
entry.getValue(),
playerExpectations.isEmpty()
? List.of()
: calculateResultsforPlayersFromRaw(
: calculateResultsForPlayersFromRaw(
groupedByTeamAndUser.get(entry.getKey()), rawUserMap),
null))
.toList());
Expand Down Expand Up @@ -377,7 +375,7 @@ public static List<InjectTargetWithResult> getTargetsWithResultsFromRaw(
}

// -- PRE CALCULATED RESULTS FOR PLAYERS --
private static List<InjectTargetWithResult> calculateResultsforPlayersFromRaw(
private static List<InjectTargetWithResult> calculateResultsForPlayersFromRaw(
Map<String, List<RawInjectExpectation>> expectationsByUser, Map<String, RawUser> rawUserMap) {
if (expectationsByUser == null) {
return new ArrayList<>();
Expand All @@ -402,72 +400,74 @@ private static List<InjectTargetWithResult> calculateResultsforPlayersFromRaw(

// -- RESULTS BY EXPECTATION TYPE --
@NotNull
public static List<ExpectationResultsByType> getExpectationResultByTypes(
final List<InjectExpectation> expectations) {
List<Double> preventionScores = getScores(List.of(EXPECTATION_TYPE.PREVENTION), expectations);
List<Double> detectionScores = getScores(List.of(EXPECTATION_TYPE.DETECTION), expectations);
public static List<ExpectationResultsByType> getExpectationResultByTypesFromRaw(
List<RawInjectExpectation> expectations) {
List<Double> preventionScores =
getScoresFromRaw(List.of(EXPECTATION_TYPE.PREVENTION), expectations);
List<Double> detectionScores =
getScoresFromRaw(List.of(EXPECTATION_TYPE.DETECTION), expectations);
List<Double> humanScores =
getScores(
getScoresFromRaw(
List.of(EXPECTATION_TYPE.ARTICLE, EXPECTATION_TYPE.CHALLENGE, EXPECTATION_TYPE.MANUAL),
expectations);

List<ExpectationResultsByType> resultAvgOfExpectations = new ArrayList<>();

getExpectationByType(ExpectationType.PREVENTION, preventionScores)
.map(resultAvgOfExpectations::add);
.ifPresent(resultAvgOfExpectations::add);
getExpectationByType(ExpectationType.DETECTION, detectionScores)
.map(resultAvgOfExpectations::add);
.ifPresent(resultAvgOfExpectations::add);
getExpectationByType(ExpectationType.HUMAN_RESPONSE, humanScores)
.map(resultAvgOfExpectations::add);
.ifPresent(resultAvgOfExpectations::add);

return resultAvgOfExpectations;
}

@NotNull
public static List<ExpectationResultsByType> getExpectationResultByTypesFromRaw(
List<RawInjectExpectation> expectations) {
List<Double> preventionScores =
getScoresFromRaw(List.of(EXPECTATION_TYPE.PREVENTION), expectations);
List<Double> detectionScores =
getScoresFromRaw(List.of(EXPECTATION_TYPE.DETECTION), expectations);
public static List<ExpectationResultsByType> getExpectationResultByTypes(
final List<InjectExpectation> expectations) {
List<Double> preventionScores = getScores(List.of(EXPECTATION_TYPE.PREVENTION), expectations);
List<Double> detectionScores = getScores(List.of(EXPECTATION_TYPE.DETECTION), expectations);
List<Double> humanScores =
getScoresFromRaw(
getScores(
List.of(EXPECTATION_TYPE.ARTICLE, EXPECTATION_TYPE.CHALLENGE, EXPECTATION_TYPE.MANUAL),
expectations);

List<ExpectationResultsByType> resultAvgOfExpectations = new ArrayList<>();

getExpectationByType(ExpectationType.PREVENTION, preventionScores)
.ifPresent(resultAvgOfExpectations::add);
.map(resultAvgOfExpectations::add);
getExpectationByType(ExpectationType.DETECTION, detectionScores)
.ifPresent(resultAvgOfExpectations::add);
.map(resultAvgOfExpectations::add);
getExpectationByType(ExpectationType.HUMAN_RESPONSE, humanScores)
.ifPresent(resultAvgOfExpectations::add);
.map(resultAvgOfExpectations::add);

return resultAvgOfExpectations;
}

// -- NORMALIZED SCORES --
public static List<Double> getScores(
final List<EXPECTATION_TYPE> types, final List<InjectExpectation> expectations) {
public static List<Double> getScoresFromRaw(
List<EXPECTATION_TYPE> types, List<RawInjectExpectation> expectations) {
return expectations.stream()
.filter(e -> types.contains(e.getType()))
.filter(e -> types.contains(EXPECTATION_TYPE.valueOf(e.getInject_expectation_type())))
.map(
injectExpectation -> {
if (injectExpectation.getScore() == null) {
rawInjectExpectation -> {
if (rawInjectExpectation.getInject_expectation_score() == null) {
return null;
}
if (injectExpectation.getTeam() != null) {
if (injectExpectation.getScore() >= injectExpectation.getExpectedScore()) {
if (rawInjectExpectation.getTeam_id() != null) {
if (rawInjectExpectation.getInject_expectation_score()
>= rawInjectExpectation.getInject_expectation_expected_score()) {
return 1.0;
} else {
return 0.0;
}
} else {
if (injectExpectation.getScore() >= injectExpectation.getExpectedScore()) {
if (rawInjectExpectation.getInject_expectation_score()
>= rawInjectExpectation.getInject_expectation_expected_score()) {
return 1.0;
}
if (injectExpectation.getScore() == 0) {
if (rawInjectExpectation.getInject_expectation_score() == 0) {
return 0.0;
}
return 0.5;
Expand All @@ -476,28 +476,26 @@ public static List<Double> getScores(
.toList();
}

public static List<Double> getScoresFromRaw(
List<EXPECTATION_TYPE> types, List<RawInjectExpectation> expectations) {
public static List<Double> getScores(
final List<EXPECTATION_TYPE> types, final List<InjectExpectation> expectations) {
return expectations.stream()
.filter(e -> types.contains(EXPECTATION_TYPE.valueOf(e.getInject_expectation_type())))
.filter(e -> types.contains(e.getType()))
.map(
rawInjectExpectation -> {
if (rawInjectExpectation.getInject_expectation_score() == null) {
injectExpectation -> {
if (injectExpectation.getScore() == null) {
return null;
}
if (rawInjectExpectation.getTeam_id() != null) {
if (rawInjectExpectation.getInject_expectation_score()
>= rawInjectExpectation.getInject_expectation_expected_score()) {
if (injectExpectation.getTeam() != null) {
if (injectExpectation.getScore() >= injectExpectation.getExpectedScore()) {
return 1.0;
} else {
return 0.0;
}
} else {
if (rawInjectExpectation.getInject_expectation_score()
>= rawInjectExpectation.getInject_expectation_expected_score()) {
if (injectExpectation.getScore() >= injectExpectation.getExpectedScore()) {
return 1.0;
}
if (rawInjectExpectation.getInject_expectation_score() == 0) {
if (injectExpectation.getScore() == 0) {
return 0.0;
}
return 0.5;
Expand Down Expand Up @@ -537,18 +535,6 @@ public static Optional<ExpectationResultsByType> getExpectationByType(
type, InjectExpectation.EXPECTATION_STATUS.PENDING, getResultDetail(type, scores)));
}

public static List<InjectExpectation> getRefinedExpectations(
Inject inject, List<String> targetIds) {
return inject.getExpectations().stream()
.filter(expectation -> targetIds.contains(expectation.getTargetId()))
.filter(
expectation ->
expectation.getUser()
== null) // Filter expectations linked to players. For global results, We use
// Team expectations
.toList();
}

public static InjectExpectation.EXPECTATION_STATUS getResult(final OptionalDouble avg) {
Double avgAsDouble = avg.getAsDouble();
return avgAsDouble == 0.0
Expand Down Expand Up @@ -579,4 +565,13 @@ public static List<ResultDistribution> getResultDetail(
private static List<InjectTargetWithResult> sortResults(List<InjectTargetWithResult> targets) {
return targets.stream().sorted(Comparator.comparing(InjectTargetWithResult::getName)).toList();
}

// -- RECORDS --
public record ExpectationResultsByType(
@NotNull ExpectationType type,
@NotNull InjectExpectation.EXPECTATION_STATUS avgResult,
@NotNull List<ResultDistribution> distribution) {}

public record ResultDistribution(
@NotNull String id, @NotNull String label, @NotNull Integer value) {}
}
Loading

0 comments on commit f0717a0

Please sign in to comment.