From 1cc116cb60e91374c7dd7977e95a99bf1de15b96 Mon Sep 17 00:00:00 2001 From: Gael Leblan Date: Tue, 19 Nov 2024 17:34:24 +0100 Subject: [PATCH 1/4] [backend] Upgrading to latest spring boot version --- openbas-api/pom.xml | 4 ++++ .../rest/exercise/service/ExerciseService.java | 7 +++++-- .../java/io/openbas/service/ScenarioService.java | 11 ++++++----- .../src/main/resources/application.properties | 2 +- .../src/main/java/io/openbas/utils/JpaUtils.java | 13 ++++++------- openbas-model/pom.xml | 2 +- .../java/io/openbas/database/model/Endpoint.java | 2 +- pom.xml | 2 +- 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/openbas-api/pom.xml b/openbas-api/pom.xml index 6fcd464626..beba108b84 100644 --- a/openbas-api/pom.xml +++ b/openbas-api/pom.xml @@ -146,6 +146,10 @@ org.flywaydb flyway-core + + org.flywaydb + flyway-database-postgresql + commons-codec commons-codec diff --git a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java index 921eb1d16b..4ffacc790a 100644 --- a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java +++ b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java @@ -42,6 +42,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import lombok.RequiredArgsConstructor; +import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; @@ -383,11 +384,13 @@ private void select( // Array aggregations Join exerciseTagsJoin = exerciseRoot.join("tags", JoinType.LEFT); joinMap.put("tags", exerciseTagsJoin); - Expression tagIdsExpression = arrayAggOnId(cb, exerciseTagsJoin); + Expression tagIdsExpression = + arrayAggOnId((HibernateCriteriaBuilder) cb, exerciseTagsJoin); Join injectsJoin = exerciseRoot.join("injects", JoinType.LEFT); joinMap.put("injects", injectsJoin); - Expression injectIdsExpression = arrayAggOnId(cb, injectsJoin); + Expression injectIdsExpression = + arrayAggOnId((HibernateCriteriaBuilder) cb, injectsJoin); // SELECT cq.multiselect( exerciseRoot.get("id").alias("exercise_id"), diff --git a/openbas-api/src/main/java/io/openbas/service/ScenarioService.java b/openbas-api/src/main/java/io/openbas/service/ScenarioService.java index 675b983058..680c2c309b 100644 --- a/openbas-api/src/main/java/io/openbas/service/ScenarioService.java +++ b/openbas-api/src/main/java/io/openbas/service/ScenarioService.java @@ -58,6 +58,7 @@ import lombok.extern.java.Log; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.function.TriFunction; +import org.hibernate.query.criteria.HibernateCriteriaBuilder; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; @@ -183,12 +184,12 @@ private Page findAllWithCriteriaBuilder( // Join on TAG Join scenarioTagsJoin = scenarioRoot.join("tags", JoinType.LEFT); joinMap.put("tags", scenarioTagsJoin); + Expression nullString = cb.nullLiteral(String.class); + Expression arr = + ((HibernateCriteriaBuilder) cb).arrayAgg(null, scenarioTagsJoin.get("id")); Expression tagIdsExpression = - cb.function( - "array_remove", - String[].class, - cb.function("array_agg", String[].class, scenarioTagsJoin.get("id")), - cb.nullLiteral(String.class)); + ((HibernateCriteriaBuilder) cb).arrayRemove(arr, nullString); + // Join on INJECT and INJECTOR CONTRACT Join injectsJoin = scenarioRoot.join("injects", JoinType.LEFT); joinMap.put("injects", injectsJoin); diff --git a/openbas-api/src/main/resources/application.properties b/openbas-api/src/main/resources/application.properties index 932e095734..6470e9fac4 100644 --- a/openbas-api/src/main/resources/application.properties +++ b/openbas-api/src/main/resources/application.properties @@ -97,7 +97,7 @@ spring.datasource.url=jdbc:postgresql://... spring.datasource.username=openbas spring.datasource.password= spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -spring.jpa.hibernate.ddl-auto=validate +spring.jpa.hibernate.ddl-auto=none # spring.jpa.show-sql=true # spring.jpa.properties.hibernate.format_sql=true spring.flyway.url=${spring.datasource.url} diff --git a/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java b/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java index 8202e3a80f..b62fdf47cf 100644 --- a/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java +++ b/openbas-framework/src/main/java/io/openbas/utils/JpaUtils.java @@ -7,6 +7,7 @@ import jakarta.persistence.criteria.*; import jakarta.validation.constraints.NotNull; import java.util.Map; +import org.hibernate.query.criteria.HibernateCriteriaBuilder; public class JpaUtils { @@ -89,12 +90,10 @@ public static Expression toPath( // -- FUNCTION -- public static Expression arrayAggOnId( - @NotNull final CriteriaBuilder cb, @NotNull final Join join) { - return cb.function( - "array_remove", - String[].class, - cb.function("array_agg", String[].class, join.get("id")), - cb.nullLiteral(String.class)); + @NotNull final HibernateCriteriaBuilder cb, @NotNull final Join join) { + Expression nullString = cb.nullLiteral(String.class); + Expression arr = cb.arrayAgg(null, join.get("id")); + return cb.arrayRemove(arr, nullString); } // -- JOIN -- @@ -106,6 +105,6 @@ public static Join createLeftJoin(Root root, String attributeNam public static Expression createJoinArrayAggOnId( CriteriaBuilder cb, Root root, String attributeName) { Join join = createLeftJoin(root, attributeName); - return arrayAggOnId(cb, join); + return arrayAggOnId((HibernateCriteriaBuilder) cb, join); } } diff --git a/openbas-model/pom.xml b/openbas-model/pom.xml index e17c06f407..e5f4e6ecc1 100644 --- a/openbas-model/pom.xml +++ b/openbas-model/pom.xml @@ -46,7 +46,7 @@ io.hypersistence hypersistence-utils-hibernate-63 - 3.7.0 + 3.9.0 org.postgresql diff --git a/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java b/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java index f11f3ba660..3e91ab0ec2 100644 --- a/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java +++ b/openbas-model/src/main/java/io/openbas/database/model/Endpoint.java @@ -53,7 +53,7 @@ public enum PLATFORM_TYPE { @NotEmpty @Ipv4OrIpv6Constraint @Type(StringArrayType.class) - @Column(name = "endpoint_ips") + @Column(name = "endpoint_ips", columnDefinition = "text[]") @JsonProperty("endpoint_ips") private String[] ips; diff --git a/pom.xml b/pom.xml index 50c944c502..491c77f276 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.0 + 3.3.5 io.openbas From b23ae3ff22ca65dae2807d829b1f49b3237d23a6 Mon Sep 17 00:00:00 2001 From: Gael Leblan Date: Thu, 21 Nov 2024 14:05:25 +0100 Subject: [PATCH 2/4] [backend] Upgrading to latest spring boot version --- .../V3_48__Adding_wrapper_functions.java | 32 +++++++++++++++++++ .../io/openbas/utils/OperationUtilsJpa.java | 4 +-- pom.xml | 5 +++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java diff --git a/openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java b/openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java new file mode 100644 index 0000000000..d0754ef3f5 --- /dev/null +++ b/openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java @@ -0,0 +1,32 @@ +package io.openbas.migration; + +import java.sql.SQLException; +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_48__Adding_wrapper_functions extends BaseJavaMigration { + + @Override + public void migrate(Context context) throws SQLException { + Statement migrator = context.getConnection().createStatement(); + migrator.execute( + """ + CREATE FUNCTION array_to_string_wrapper(a anyelement, b text) + RETURNS TEXT AS + $$ + SELECT array_to_string(a, b) + $$ LANGUAGE SQL; + """); + migrator.execute( + """ + CREATE FUNCTION array_position_wrapper(a anyelement, b text) + RETURNS INT AS + $$ + SELECT array_position(a, b) + $$ LANGUAGE SQL; + """); + } +} diff --git a/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java b/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java index a8bc1b0a15..f34bd8a320 100644 --- a/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java +++ b/openbas-framework/src/main/java/io/openbas/utils/OperationUtilsJpa.java @@ -295,7 +295,7 @@ private static Expression lowerArray(Expression paths, CriteriaBuil private static Expression arrayPosition( Expression paths, CriteriaBuilder cb, Expression text) { - return cb.function("array_position", Boolean.class, paths, text); + return cb.function("array_position_wrapper", Boolean.class, paths, text); } private static Expression lower(Expression paths, CriteriaBuilder cb) { @@ -307,7 +307,7 @@ private static Expression stringToArray(Expression paths, Crit } private static Expression arrayToString(Expression paths, CriteriaBuilder cb) { - return cb.function("array_to_string", String.class, paths, cb.literal(" && ")); + return cb.function("array_to_string_wrapper", String.class, paths, cb.literal(" && ")); } private static Expression avals(Expression paths, CriteriaBuilder cb) { diff --git a/pom.xml b/pom.xml index 491c77f276..922e383d87 100644 --- a/pom.xml +++ b/pom.xml @@ -64,6 +64,11 @@ lombok provided + + org.hibernate.orm + hibernate-core + 6.6.2.Final + From 3710d70b45ac3eddc9a9ceabd12b25c3e98243ea Mon Sep 17 00:00:00 2001 From: Gael Leblan Date: Thu, 21 Nov 2024 14:16:17 +0100 Subject: [PATCH 3/4] [backend] Upgrading to latest spring boot version --- ..._functions.java => V3_49__Adding_wrapper_functions.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename openbas-api/src/main/java/io/openbas/migration/{V3_48__Adding_wrapper_functions.java => V3_49__Adding_wrapper_functions.java} (77%) diff --git a/openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java b/openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java similarity index 77% rename from openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java rename to openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java index d0754ef3f5..7210df4f9f 100644 --- a/openbas-api/src/main/java/io/openbas/migration/V3_48__Adding_wrapper_functions.java +++ b/openbas-api/src/main/java/io/openbas/migration/V3_49__Adding_wrapper_functions.java @@ -7,14 +7,14 @@ import org.springframework.stereotype.Component; @Component -public class V3_48__Adding_wrapper_functions extends BaseJavaMigration { +public class V3_49__Adding_wrapper_functions extends BaseJavaMigration { @Override public void migrate(Context context) throws SQLException { Statement migrator = context.getConnection().createStatement(); migrator.execute( """ - CREATE FUNCTION array_to_string_wrapper(a anyelement, b text) + CREATE OR REPLACE FUNCTION array_to_string_wrapper(a anyelement, b text) RETURNS TEXT AS $$ SELECT array_to_string(a, b) @@ -22,7 +22,7 @@ SELECT array_to_string(a, b) """); migrator.execute( """ - CREATE FUNCTION array_position_wrapper(a anyelement, b text) + CREATE OR REPLACE FUNCTION array_position_wrapper(a anyelement, b text) RETURNS INT AS $$ SELECT array_position(a, b) From 695d8364bea524ab88f5a1f14a9fb50b0139782f Mon Sep 17 00:00:00 2001 From: Gael Leblan Date: Thu, 21 Nov 2024 15:36:10 +0100 Subject: [PATCH 4/4] [backend] Upgrading to latest spring boot version --- pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pom.xml b/pom.xml index 922e383d87..491c77f276 100644 --- a/pom.xml +++ b/pom.xml @@ -64,11 +64,6 @@ lombok provided - - org.hibernate.orm - hibernate-core - 6.6.2.Final -