Skip to content

Commit

Permalink
Merge branch 'master' into renovate/io.hypersistence-hypersistence-ut…
Browse files Browse the repository at this point in the history
…ils-hibernate-63-3.x
  • Loading branch information
RomuDeuxfois authored Aug 19, 2024
2 parents 7f7ba89 + 26709d2 commit f238bef
Show file tree
Hide file tree
Showing 140 changed files with 3,525 additions and 1,595 deletions.
4 changes: 2 additions & 2 deletions openbas-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.openbas</groupId>
<artifactId>openbas-platform</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</parent>

<artifactId>openbas-api</artifactId>
Expand Down Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>io.openbas</groupId>
<artifactId>openbas-framework</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.rabbitmq</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public class InjectHelper {

private List<Team> getInjectTeams(@NotNull final Inject inject) {
Exercise exercise = inject.getExercise();
return inject.isAllTeams() ? exercise.getTeams() : inject.getTeams();
if(inject.isAllTeams()) { // In order to process expectations from players, we also need to load players into teams
exercise.getTeams().forEach(team -> Hibernate.initialize(team.getUsers()));
return exercise.getTeams();
} else {
inject.getTeams().forEach(team -> Hibernate.initialize(team.getUsers()));
return inject.getTeams();
}
}

// -- INJECTION --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ private Challenge createChallenge(JsonNode nodeChallenge, Map<String, Base> base
challenge.setName(nodeChallenge.get("challenge_name").textValue());
challenge.setCategory(nodeChallenge.get("challenge_category").textValue());
challenge.setContent(nodeChallenge.get("challenge_content").textValue());
challenge.setScore(nodeChallenge.get("challenge_score").asInt(0));
challenge.setScore(nodeChallenge.get("challenge_score").asDouble(0.0));
challenge.setMaxAttempts(nodeChallenge.get("challenge_max_attempts").asInt(0));
challenge.setDocuments(
resolveJsonIds(nodeChallenge, "challenge_documents")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ private ContractExpectations expectations() {
Expectation preventionExpectation = new Expectation();
preventionExpectation.setType(PREVENTION);
preventionExpectation.setName("Expect inject to be prevented");
preventionExpectation.setScore(0);
preventionExpectation.setScore(100.0);
// Detection
Expectation detectionExpectation = new Expectation();
detectionExpectation.setType(DETECTION);
detectionExpectation.setName("Expect inject to be detected");
detectionExpectation.setScore(0);
detectionExpectation.setScore(100.0);
return expectationsField("expectations", "Expectations", List.of(preventionExpectation, detectionExpectation));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import io.openbas.injector_contract.ContractorIcon;
import io.openbas.injector_contract.fields.ContractElement;
import io.openbas.database.model.Endpoint;
import io.openbas.injector_contract.fields.ContractExpectations;
import io.openbas.model.inject.form.Expectation;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import static io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE.CHALLENGE;
import static io.openbas.injector_contract.Contract.executableContract;
import static io.openbas.injector_contract.ContractCardinality.Multiple;
import static io.openbas.injector_contract.ContractDef.contractBuilder;
import static io.openbas.injector_contract.fields.ContractChallenge.challengeField;
import static io.openbas.injector_contract.fields.ContractAttachment.attachmentField;
import static io.openbas.injector_contract.fields.ContractExpectations.expectationsField;
import static io.openbas.injector_contract.fields.ContractTeam.teamField;
import static io.openbas.injector_contract.fields.ContractCheckbox.checkboxField;
import static io.openbas.injector_contract.fields.ContractText.textField;
Expand Down Expand Up @@ -61,8 +65,18 @@ public List<Contract> contracts() {
Kind regards,<br />
The animation team
""";
// We include the expectations for challenges
Expectation expectation = new Expectation();
expectation.setType(CHALLENGE);
expectation.setName("Expect targets to complete the challenge(s)");
expectation.setScore(0.0);
ContractExpectations expectationsField = expectationsField(
"expectations", "Expectations", List.of(expectation)
);
List<ContractElement> publishInstance = contractBuilder()
.mandatory(challengeField("challenges", "Challenges", Multiple))
// Contract specific
.optional(expectationsField)
.mandatory(textField("subject", "Subject", "New challenges published for ${user.email}"))
.mandatory(richTextareaField("body", "Body", messageBody))
.optional(checkboxField("encrypted", "Encrypted", false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.openbas.model.ExecutionProcess;
import io.openbas.model.Expectation;
import io.openbas.model.expectation.ChallengeExpectation;
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.model.expectation.ManualExpectation;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.openbas.database.model.InjectStatusExecution.traceError;
import static io.openbas.database.model.InjectStatusExecution.traceSuccess;
Expand Down Expand Up @@ -102,7 +103,21 @@ public ExecutionProcess process(@NotNull final Execution execution, @NotNull fin
});
// Return expectations
List<Expectation> expectations = new ArrayList<>();
challenges.forEach(challenge -> expectations.add(new ChallengeExpectation(challenge.getScore(), challenge)));
if (!content.getExpectations().isEmpty()) {
expectations.addAll(
content.getExpectations()
.stream()
.flatMap((entry) -> switch (entry.getType()) {
case MANUAL -> Stream.of(
(Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription(), entry.isExpectationGroup())
);
case CHALLENGE -> challenges.stream()
.map(challenge -> (Expectation) new ChallengeExpectation(entry.getScore(), challenge, entry.isExpectationGroup()));
default -> Stream.of();
})
.toList()
);
}
return new ExecutionProcess(false, expectations);
} else {
throw new UnsupportedOperationException("Unknown contract " + contract);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.openbas.injectors.email.model.EmailContent;
import io.openbas.model.inject.form.Expectation;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -15,4 +16,7 @@ public class ChallengeContent extends EmailContent {
@JsonProperty("challenges")
private List<String> challenges = new ArrayList<>();

@JsonProperty("expectations")
private List<Expectation> expectations = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public List<Contract> contracts() {
ContractCheckbox emailingField = checkboxField("emailing", "Send email", true);
Expectation expectation = new Expectation();
expectation.setType(ARTICLE);
expectation.setName("Expect teams to read the article(s)");
expectation.setScore(0);
expectation.setName("Expect targets to read the article(s)");
expectation.setScore(0.0);
ContractExpectations expectationsField = expectationsField(
"expectations", "Expectations", List.of(expectation)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public ExecutionProcess process(@NotNull final Execution execution, @NotNull fin
.stream()
.flatMap((entry) -> switch (entry.getType()) {
case MANUAL -> Stream.of(
(Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription())
(Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription(), entry.isExpectationGroup())
);
case ARTICLE -> articles.stream()
.map(article -> (Expectation) new ChannelExpectation(entry.getScore(), article));
.map(article -> (Expectation) new ChannelExpectation(entry.getScore(), article, entry.isExpectationGroup()));
default -> Stream.of();
})
.toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ExecutionProcess process(@NotNull final Execution execution, @NotNull fin
.stream()
.flatMap((entry) -> switch (entry.getType()) {
case MANUAL ->
Stream.of((Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription()));
Stream.of((Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription(), entry.isExpectationGroup()));
default -> Stream.of();
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ExecutionProcess process(@NotNull final Execution execution, @NotNull fin
.stream()
.flatMap((entry) -> switch (entry.getType()) {
case MANUAL ->
Stream.of((Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription()));
Stream.of((Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription(), entry.isExpectationGroup()));
default -> Stream.of();
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ExecutionProcess process(@NotNull final Execution execution, @NotNull fin
.stream()
.flatMap(entry -> switch (entry.getType()) {
case MANUAL ->
Stream.of((Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription()));
Stream.of((Expectation) new ManualExpectation(entry.getScore(), entry.getName(), entry.getDescription(), entry.isExpectationGroup()));
default -> Stream.of();
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static class OvhSmsContentOld {

private String message;
private String expectationType;
private Integer expectationScore;
private Double expectationScore;

OvhSmsContentNew toNewContent() {
OvhSmsContentNew content = new OvhSmsContentNew();
Expand All @@ -99,7 +99,7 @@ public static class EmailContentOld {
private String inReplyTo;
private boolean encrypted;
private String expectationType;
private Integer expectationScore;
private Double expectationScore;

EmailContent toNewContent() {
EmailContent content = new EmailContent();
Expand All @@ -122,7 +122,7 @@ public static class MediaContentOld extends EmailContentOld {

private List<String> articles;
private boolean expectation;
private Integer expectationScore;
private Double expectationScore;
private boolean emailing;

ChannelContent toNewContent() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.openbas.migration;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.springframework.stereotype.Component;

import java.sql.Statement;

@Component
public class V3_30__Score_type extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
Statement select = context.getConnection().createStatement();
select.execute("ALTER TABLE challenges alter column challenge_score type DOUBLE PRECISION;");
select.execute("ALTER TABLE injects_expectations alter column inject_expectation_score type DOUBLE PRECISION;");
select.execute("ALTER TABLE injects_expectations alter column inject_expectation_expected_score type DOUBLE PRECISION;");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.openbas.migration;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.Statement;

@Component
public class V3_31__Add_Injects_tests_statuses extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
Connection connection = context.getConnection();
Statement select = connection.createStatement();
// Create table
select.execute("""
CREATE TABLE injects_tests_statuses (
status_id varchar(255) NOT NULL CONSTRAINT inject_test_status_pkey PRIMARY KEY,
status_name VARCHAR(255) NOT NULL,
status_executions text,
tracking_sent_date timestamp,
tracking_ack_date timestamp,
tracking_end_date timestamp,
tracking_total_execution_time bigint,
tracking_total_count int,
tracking_total_error int,
tracking_total_success int,
status_inject VARCHAR(255) NOT NULL CONSTRAINT inject_test_status_inject_id_fkey REFERENCES injects(inject_id) ON DELETE SET NULL,
status_created_at timestamp not null default now(),
status_updated_at timestamp not null default now()
);
CREATE INDEX idx_inject_test_inject ON injects_tests_statuses(status_inject);
""");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ public Inject tryAtomicTesting(@PathVariable String injectId) {

@GetMapping("/{injectId}/target_results/{targetId}/types/{targetType}")
public List<InjectExpectation> findTargetResult(
@PathVariable String targetId,
@PathVariable String injectId,
@PathVariable String targetType) {
return injectExpectationService.findExpectationsByInjectAndTargetAndTargetType(injectId, targetId, targetType);
@PathVariable String targetId,
@PathVariable String targetType,
@RequestParam(required = false) String parentTargetId ) {
return injectExpectationService.findExpectationsByInjectAndTargetAndTargetType(injectId, targetId, parentTargetId, targetType);
}

@PutMapping("/{injectId}/tags")
Expand Down
Loading

0 comments on commit f238bef

Please sign in to comment.