diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt b/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt index 456796b..ffaafd3 100644 --- a/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt +++ b/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt @@ -15,6 +15,7 @@ class DlpFinding { @Id var id: UUID = UUID.randomUUID() + @Enumerated(EnumType.STRING) @Column(nullable = false) lateinit var infoType: InfoType @@ -28,10 +29,10 @@ class DlpFinding { @Column(columnDefinition = "jsonb") var additional: Map? = null - @ManyToOne + @ManyToOne(fetch = FetchType.EAGER) @JoinColumn @JsonIdentityReference(alwaysAsId=true) @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property="id") - lateinit var result: InspectDlpResult + var result: InspectDlpResult? = null } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt b/src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt index aa425f5..54808a6 100644 --- a/src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt +++ b/src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt @@ -1,8 +1,6 @@ package io.hawk.service.dlp -import io.hawk.dlp.common.InspectResult import io.hawk.dlp.common.Job -import io.hawk.dlp.common.Result import org.springframework.web.bind.annotation.* import java.util.* @@ -25,20 +23,22 @@ class DlpInputRestController( } @PostMapping("/{jobId}/result/inspect") - fun result(@PathVariable jobId: UUID, result: InspectResult) { + fun result(@PathVariable jobId: UUID, @RequestBody result: InspectDlpResult) { val dlpJob = dlpJobRepository.findById(jobId).orElseThrow { error("Job does not exist") } val dlpResult = InspectDlpResult().apply { id = result.id job = dlpJob timestamp = result.timestamp additional = result.additional - findings = result.findings.map { - DlpFinding().apply { - infoType = it.infoType - likelihood = it.likelihood - occurrences = it.occurrences - additional = it.additional - } + } + dlpResult.findings = result.findings.map { + DlpFinding().apply { + id = it.id + infoType = it.infoType + likelihood = it.likelihood + occurrences = it.occurrences + additional = it.additional + this.result = dlpResult } } dlpResultRepository.save(dlpResult) diff --git a/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt b/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt index 57f9ecd..f8dc18c 100644 --- a/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt +++ b/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt @@ -1,13 +1,10 @@ package io.hawk.service.dlp -import jakarta.persistence.CascadeType -import jakarta.persistence.DiscriminatorValue -import jakarta.persistence.Entity -import jakarta.persistence.OneToMany +import jakarta.persistence.* @Entity @DiscriminatorValue("inspect") class InspectDlpResult : DlpResult() { - @OneToMany(mappedBy = "result", cascade = [CascadeType.ALL]) + @OneToMany(mappedBy = "result", cascade = [CascadeType.ALL], fetch = FetchType.EAGER) lateinit var findings: List } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/traffic/field/Field.kt b/src/main/kotlin/io/hawk/service/traffic/field/Field.kt index e07abe8..b2a1fd7 100644 --- a/src/main/kotlin/io/hawk/service/traffic/field/Field.kt +++ b/src/main/kotlin/io/hawk/service/traffic/field/Field.kt @@ -5,10 +5,8 @@ import com.vladmihalcea.hibernate.type.array.EnumArrayType import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType import com.vladmihalcea.hibernate.type.json.JsonBinaryType import io.hawk.dlp.common.InfoType -import io.hawk.service.dlp.DlpFinding import io.hawk.service.traffic.mapping.MappingField import jakarta.persistence.* -import org.hibernate.annotations.JoinFormula import org.hibernate.annotations.Parameter import org.hibernate.annotations.Type @@ -24,6 +22,7 @@ class Field { var description: String? = null + @Enumerated(EnumType.STRING) @Type(value = EnumArrayType::class, parameters = [ Parameter( @@ -59,13 +58,4 @@ class Field { @JsonIgnore @OneToMany(mappedBy = "field", cascade = [CascadeType.REMOVE]) var mappingFields: MutableList = mutableListOf() - - @JsonIgnore - @ManyToMany - @JoinFormula(value = """ - (SELECT 1 FROM field - WHERE field.id = field_id - AND dlp_finding.info_type = ANY(field.info_types)) - """, referencedColumnName = "id") - var findings: MutableList = mutableListOf() } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt b/src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt index c2f2584..74ce67c 100644 --- a/src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt +++ b/src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt @@ -2,11 +2,13 @@ package io.hawk.service.traffic.field import io.hawk.dlp.common.InfoType import io.hawk.service.dlp.DlpFinding +import jakarta.persistence.EntityManager import org.springframework.stereotype.Service @Service class FieldService( - private val fieldRepository: FieldRepository + private val fieldRepository: FieldRepository, + private val entityManager: EntityManager ) { fun listFields(): List = fieldRepository.findAll() @@ -43,5 +45,12 @@ class FieldService( request.consequences?.also { consequences = it } } - fun showDlp(name: String): List = showField(name).findings + fun showDlp(fieldName: String): List { + return entityManager + .createNativeQuery(""" + select d.id, d.info_type, d.likelihood, d.additional, d.occurrences, d.result_id from dlp_finding d join field on d.info_type = ANY(field.info_types) AND field.name = :field_name + """.trimIndent(), DlpFinding::class.java) + .setParameter("field_name", fieldName) + .resultList as List + } } \ No newline at end of file