Skip to content

[backend] Only show findings of latest simulation for global, scenario, endpoint findings view (#3072) #3239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: release/current
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.openbas.migration;

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

@Component
public class V3_90__add_automatic_finding_hash_column extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {
try (Statement statement = context.getConnection().createStatement()) {

String updateInjectExpectationResult =
"""
-- necessary for hashing functions
CREATE SEQUENCE IF NOT EXISTS exercise_launch_order_seq
AS BIGINT
INCREMENT BY 1
START WITH 1;

ALTER TABLE exercises
ADD COLUMN exercise_launch_order BIGINT;

CREATE OR REPLACE FUNCTION update_launch_order_trigger()
RETURNS TRIGGER AS $$
BEGIN
UPDATE exercises
SET exercise_launch_order = CASE
WHEN NEW.exercise_start_date IS NULL
THEN NULL
ELSE nextval('exercise_launch_order_seq')
END
WHERE exercise_id = NEW.exercise_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER after_insert_exercise
AFTER INSERT ON exercises
FOR EACH ROW
EXECUTE PROCEDURE update_launch_order_trigger();

CREATE OR REPLACE TRIGGER after_update_exercise_start_date
AFTER UPDATE OF exercise_start_date ON exercises
FOR EACH ROW
EXECUTE PROCEDURE update_launch_order_trigger();

-- migration of existing records
UPDATE exercises e
SET exercise_launch_order = migrated_start_order
FROM (SELECT exercise_id, nextval('exercise_launch_order_seq') as migrated_start_order
FROM exercises
WHERE exercise_start_date IS NOT NULL
ORDER BY exercise_start_date DESC NULLS LAST) o
WHERE e.exercise_id = o.exercise_id;
""";

statement.executeUpdate(updateInjectExpectationResult);
}
}
}
32 changes: 22 additions & 10 deletions openbas-api/src/main/java/io/openbas/rest/finding/FindingApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public class FindingApi extends RestBehavior {
@PostMapping("/search")
public Page<FindingOutput> findings(
@RequestBody @Valid final SearchPaginationInput searchPaginationInput) {
return buildPaginationJPA(this.findingRepository::findAll, searchPaginationInput, Finding.class)
return buildPaginationJPA(
(specification, pageable) ->
this.findingRepository.findAll(
FindingSpecification.forLatestSimulations().and(specification), pageable),
searchPaginationInput,
Finding.class)
.map(findingMapper::toFindingOutput);
}

Expand Down Expand Up @@ -79,14 +84,19 @@ public Page<FindingOutput> findingsBySimulation(
public Page<FindingOutput> findingsByScenario(
@PathVariable @NotNull final String scenarioId,
@RequestBody @Valid final SearchPaginationInput searchPaginationInput) {
return buildPaginationJPA(
(Specification<Finding> specification, Pageable pageable) ->
this.findingRepository.findAll(
FindingSpecification.findFindingsForScenario(scenarioId).and(specification),
pageable),
searchPaginationInput,
Finding.class)
.map(findingMapper::toFindingOutput);
Page<FindingOutput> page =
buildPaginationJPA(
(Specification<Finding> specification, Pageable pageable) ->
this.findingRepository.findAll(
FindingSpecification.findFindingsForScenario(scenarioId)
.and(FindingSpecification.forLatestSimulations())
.and(specification),
pageable),
searchPaginationInput,
Finding.class)
.map(findingMapper::toFindingOutput);

return page;
}

@LogExecutionTime
Expand All @@ -98,7 +108,9 @@ public Page<FindingOutput> findingsByEndpoint(
return buildPaginationJPA(
(Specification<Finding> specification, Pageable pageable) ->
this.findingRepository.findAll(
FindingSpecification.findFindingsForEndpoint(endpointId).and(specification),
FindingSpecification.findFindingsForEndpoint(endpointId)
.and(FindingSpecification.forLatestSimulations())
.and(specification),
pageable),
searchPaginationInput,
Finding.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private Specification<T> getDynamicFilterSpecification(
.select(criteriaBuilder.literal(1))
.where(
positiveSpec.toPredicate(assetTable, query, criteriaBuilder),
criteriaBuilder.equal(
finalFrom.get("id"), query.getRoots().stream().findFirst().get().get("id")));
criteriaBuilder.equal(finalFrom.get("id"), root.get("id")));
return criteriaBuilder.exists(subQuery).not();
});
}
Expand All @@ -68,8 +67,7 @@ private Specification<T> getTransitiveTargetingSpecification(
subQuery
.select(criteriaBuilder.literal(1))
.where(
criteriaBuilder.equal(
finalFrom.get("id"), query.getRoots().stream().findFirst().get().get("id")),
criteriaBuilder.equal(finalFrom.get("id"), root.get("id")),
assetGroupTable.get("id").in(assetGroupIds.stream().toList()));
return criteriaBuilder.exists(subQuery).not();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ private Specification<T> getDirectTargetingSpecification(
.select(criteriaBuilder.literal(1))
.where(
criteriaBuilder.equal(injectTable.get("id"), scopedInject.getId()),
criteriaBuilder.equal(
finalFrom.get("id"), query.getRoots().stream().findFirst().get().get("id")));
criteriaBuilder.equal(finalFrom.get("id"), root.get("id")));
return criteriaBuilder.exists(subQuery);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ private Specification<T> getDynamicFilterSpecification(
.select(criteriaBuilder.literal(1))
.where(
positiveSpec.toPredicate(assetTable, query, criteriaBuilder),
criteriaBuilder.equal(
finalJoin.get("id"), query.getRoots().stream().findFirst().get().get("id")));
criteriaBuilder.equal(finalJoin.get("id"), root.get("id")));
return criteriaBuilder.exists(subQuery);
});
}
Expand All @@ -68,8 +67,7 @@ private Specification<T> getTransitiveTargetingSpecification(
subQuery
.select(criteriaBuilder.literal(1))
.where(
criteriaBuilder.equal(
finalJoin.get("id"), query.getRoots().stream().findFirst().get().get("id")),
criteriaBuilder.equal(finalJoin.get("id"), root.get("id")),
assetGroupTable.get("id").in(assetGroupIds));
return criteriaBuilder.exists(subQuery);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.openbas.rest.finding;

import static io.openbas.rest.finding.FindingFixture.*;
import static io.openbas.utils.fixtures.FindingFixture.*;
import static io.openbas.utils.fixtures.InjectFixture.getDefaultInject;
import static org.junit.jupiter.api.Assertions.*;

Expand Down
Loading