Skip to content

Commit

Permalink
Fix dlp result inspect input endpoint and show dlp for field endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijange committed Jan 18, 2024
1 parent 497e3ed commit fb78604
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DlpFinding {
@Id
var id: UUID = UUID.randomUUID()

@Enumerated(EnumType.STRING)
@Column(nullable = false)
lateinit var infoType: InfoType

Expand All @@ -28,10 +29,10 @@ class DlpFinding {
@Column(columnDefinition = "jsonb")
var additional: Map<String, Any?>? = 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

}
20 changes: 10 additions & 10 deletions src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt
Original file line number Diff line number Diff line change
@@ -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.*

Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt
Original file line number Diff line number Diff line change
@@ -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<DlpFinding>
}
12 changes: 1 addition & 11 deletions src/main/kotlin/io/hawk/service/traffic/field/Field.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,6 +22,7 @@ class Field {

var description: String? = null

@Enumerated(EnumType.STRING)
@Type(value = EnumArrayType::class,
parameters = [
Parameter(
Expand Down Expand Up @@ -59,13 +58,4 @@ class Field {
@JsonIgnore
@OneToMany(mappedBy = "field", cascade = [CascadeType.REMOVE])
var mappingFields: MutableList<MappingField> = 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<DlpFinding> = mutableListOf()
}
13 changes: 11 additions & 2 deletions src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Field> = fieldRepository.findAll()

Expand Down Expand Up @@ -43,5 +45,12 @@ class FieldService(
request.consequences?.also { consequences = it }
}

fun showDlp(name: String): List<DlpFinding> = showField(name).findings
fun showDlp(fieldName: String): List<DlpFinding> {
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<DlpFinding>
}
}

0 comments on commit fb78604

Please sign in to comment.