Skip to content

Commit

Permalink
[backend/frontend] Sanitize
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelHassine committed May 11, 2024
1 parent 6b1791e commit db74f2e
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 122 deletions.
2 changes: 1 addition & 1 deletion openbas-collectors
Original file line number Diff line number Diff line change
Expand Up @@ -26,129 +26,137 @@
@Service
public class InjectExpectationService {

private final InjectExpectationRepository injectExpectationRepository;

// -- CRUD --

public void addResultExpectation(
@NotNull final InjectExpectation expectation,
@NotBlank final String sourceId,
@NotBlank final String sourceName,
@NotBlank final String result) {
computeResult(expectation, sourceId, sourceName, result);
this.update(expectation);
}

public void computeExpectation(
@NotNull final InjectExpectation expectation,
@NotBlank final String sourceId,
@NotBlank final String sourceName,
@NotBlank final String result,
@NotBlank final boolean success) {
computeResult(expectation, sourceId, sourceName, result);
expectation.setScore(success ? expectation.getExpectedScore() : 0);
this.update(expectation);
}

public void computeExpectationGroup(
@NotNull final InjectExpectation expectationAssetGroup,
@NotNull final List<InjectExpectation> expectationAssets,
@NotBlank final String sourceId,
@NotBlank final String sourceName) {
boolean success;
if (expectationAssetGroup.isExpectationGroup()) {
success = expectationAssets.stream().anyMatch((e) -> e.getExpectedScore().equals(e.getScore()));
} else {
success = expectationAssets.stream().allMatch((e) -> e.getExpectedScore().equals(e.getScore()));
private final InjectExpectationRepository injectExpectationRepository;

// -- CRUD --

public void addResultExpectation(
@NotNull final InjectExpectation expectation,
@NotBlank final String sourceId,
@NotBlank final String sourceName,
@NotBlank final String result) {
computeResult(expectation, sourceId, sourceName, result);
this.update(expectation);
}

public void computeExpectation(
@NotNull final InjectExpectation expectation,
@NotBlank final String sourceId,
@NotBlank final String sourceName,
@NotBlank final String result,
@NotBlank final boolean success) {
computeResult(expectation, sourceId, sourceName, result);
expectation.setScore(success ? expectation.getExpectedScore() : 0);
this.update(expectation);
}

public void computeExpectationGroup(
@NotNull final InjectExpectation expectationAssetGroup,
@NotNull final List<InjectExpectation> expectationAssets,
@NotBlank final String sourceId,
@NotBlank final String sourceName) {
boolean success;
if (expectationAssetGroup.isExpectationGroup()) {
success = expectationAssets.stream().anyMatch((e) -> e.getExpectedScore().equals(e.getScore()));
} else {
success = expectationAssets.stream().allMatch((e) -> e.getExpectedScore().equals(e.getScore()));
}
computeResult(expectationAssetGroup, sourceId, sourceName, success ? "VALIDATED" : "FAILED");
expectationAssetGroup.setScore(success ? expectationAssetGroup.getExpectedScore() : 0);
this.update(expectationAssetGroup);
}

public void update(@NotNull InjectExpectation injectExpectation) {
injectExpectation.setUpdatedAt(now());
this.injectExpectationRepository.save(injectExpectation);
}

// -- PREVENTION --

public InjectExpectation preventionExpectationForAsset(
@NotNull final Inject inject,
@NotBlank final String assetId) {
return this.injectExpectationRepository.findPreventionExpectationForAsset(
inject.getId(), assetId
);
}

public List<InjectExpectation> preventionExpectationForAssets(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
List<String> assetIds = assetGroup.getAssets().stream().map(Asset::getId).toList();
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(PREVENTION))
.and(InjectExpectationSpecification.fromAssets(inject.getId(), assetIds))
);
}

public InjectExpectation preventionExpectationForAssetGroup(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
return this.injectExpectationRepository.findPreventionExpectationForAssetGroup(
inject.getId(), assetGroup.getId()
);
}

public List<InjectExpectation> preventionExpectationsNotFill(@NotBlank final String source) {
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(PREVENTION))
)
.stream()
.filter(e -> e.getResults().stream().noneMatch(r -> source.equals(r.getSourceId())))
.toList();
}

// -- DETECTION --

public List<InjectExpectation> expectationsForAssets(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup,
@NotNull final InjectExpectation.EXPECTATION_TYPE expectationType) {
List<String> assetIds = assetGroup.getAssets().stream().map(Asset::getId).toList();
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(expectationType))
.and(InjectExpectationSpecification.fromAssets(inject.getId(), assetIds))
);
}

public List<InjectExpectation> detectionExpectationsNotFill(@NotBlank final String source) {
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(DETECTION))
)
.stream()
.filter(e -> e.getResults().stream().noneMatch(r -> source.equals(r.getSourceId())))
.toList();
}
computeResult(expectationAssetGroup, sourceId, sourceName, success ? "VALIDATED" : "FAILED");
expectationAssetGroup.setScore(success ? expectationAssetGroup.getExpectedScore() : 0);
this.update(expectationAssetGroup);
}

public void update(@NotNull InjectExpectation injectExpectation) {
injectExpectation.setUpdatedAt(now());
this.injectExpectationRepository.save(injectExpectation);
}

// -- PREVENTION --

public InjectExpectation preventionExpectationForAsset(
@NotNull final Inject inject,
@NotBlank final String assetId) {
return this.injectExpectationRepository.findPreventionExpectationForAsset(
inject.getId(), assetId
);
}

public List<InjectExpectation> preventionExpectationForAssets(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
List<String> assetIds = assetGroup.getAssets().stream().map(Asset::getId).toList();
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(PREVENTION))
.and(InjectExpectationSpecification.fromAssets(inject.getId(), assetIds))
);
}

public InjectExpectation preventionExpectationForAssetGroup(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
return this.injectExpectationRepository.findPreventionExpectationForAssetGroup(
inject.getId(), assetGroup.getId()
);
}

public List<InjectExpectation> preventionExpectationsNotFillFrom(
@NotNull final Instant date,
@NotBlank final String source) {
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(PREVENTION))
.and(InjectExpectationSpecification.from(date))
)
.stream()
.filter(e -> e.getResults().stream().noneMatch(r -> source.equals(r.getSourceId())))
.toList();
}

// -- DETECTION --

public List<InjectExpectation> detectionExpectationsNotFill(@NotBlank final String source) {
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(DETECTION))
)
.stream()
.filter(e -> e.getResults().stream().noneMatch(r -> source.equals(r.getSourceId())))
.toList();
}

public List<InjectExpectation> detectionExpectationsForAssets(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
List<String> assetIds = assetGroup.getAssets().stream().map(Asset::getId).toList();
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(DETECTION))
.and(InjectExpectationSpecification.fromAssets(inject.getId(), assetIds))
);
}

// -- BY TARGET TYPE

public List<InjectExpectation> findExpectationsByInjectAndTargetAndTargetType(
@NotBlank final String injectId,
@NotBlank final String targetId,
@NotBlank final String targetType) {
try {
TargetType targetTypeEnum = TargetType.valueOf(targetType);
return switch (targetTypeEnum) {
case TEAMS -> injectExpectationRepository.findAllByInjectAndTeam(injectId, targetId);
case ASSETS -> injectExpectationRepository.findAllByInjectAndAsset(injectId, targetId);
case ASSETS_GROUPS -> injectExpectationRepository.findAllByInjectAndAssetGroup(injectId, targetId);
};
} catch (IllegalArgumentException e) {
return Collections.emptyList();

public List<InjectExpectation> detectionExpectationsForAssets(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
List<String> assetIds = assetGroup.getAssets().stream().map(Asset::getId).toList();
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(DETECTION))
.and(InjectExpectationSpecification.fromAssets(inject.getId(), assetIds))
);
}

}
// -- BY TARGET TYPE

public List<InjectExpectation> findExpectationsByInjectAndTargetAndTargetType(
@NotBlank final String injectId,
@NotBlank final String targetId,
@NotBlank final String targetType) {
try {
TargetType targetTypeEnum = TargetType.valueOf(targetType);
return switch (targetTypeEnum) {
case TEAMS -> injectExpectationRepository.findAllByInjectAndTeam(injectId, targetId);
case ASSETS -> injectExpectationRepository.findAllByInjectAndAsset(injectId, targetId);
case ASSETS_GROUPS -> injectExpectationRepository.findAllByInjectAndAssetGroup(injectId, targetId);
};
} catch (IllegalArgumentException e) {
return Collections.emptyList();
}

}

}

0 comments on commit db74f2e

Please sign in to comment.