diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt b/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt index 0dfb16d..456796b 100644 --- a/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt +++ b/src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt @@ -1,5 +1,8 @@ package io.hawk.service.dlp +import com.fasterxml.jackson.annotation.JsonIdentityInfo +import com.fasterxml.jackson.annotation.JsonIdentityReference +import com.fasterxml.jackson.annotation.ObjectIdGenerators import com.vladmihalcea.hibernate.type.json.JsonBinaryType import io.hawk.dlp.common.InfoType import io.hawk.dlp.common.Occurrence @@ -27,5 +30,8 @@ class DlpFinding { @ManyToOne @JoinColumn + @JsonIdentityReference(alwaysAsId=true) + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property="id") lateinit var result: InspectDlpResult + } \ 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 43e2dc1..93bcfa3 100644 --- a/src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt +++ b/src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt @@ -1,15 +1,42 @@ package io.hawk.service.dlp +import io.hawk.dlp.common.InspectResult import io.hawk.dlp.common.Job import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController -class DlpInputRestController { - - @PostMapping("/dlp/input") - fun test(job: Job) { - - +@RequestMapping("/api/dlp") +class DlpInputRestController( + private val dlpJobRepository: DlpJobRepository +) { + @PostMapping + fun input(job: Job) { + val dlpJob = DlpJob().apply dlp@{ + id = job.id + created = job.created + status = job.status + error = job.error?.replace("\u0000", "") + results = job.results?.values?.mapNotNull { + if(it is InspectResult) { + InspectDlpResult().apply { + id = it.id + this.job = this@dlp + timestamp = it.timestamp + additional = it.additional + findings = it.findings.map { + DlpFinding().apply { + infoType = it.infoType + likelihood = it.likelihood + occurrences = it.occurrences + additional = it.additional + } + } + } + } else null + } ?: emptyList() + } + dlpJobRepository.save(dlpJob) } } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpJob.kt b/src/main/kotlin/io/hawk/service/dlp/DlpJob.kt index 46ff135..222fd03 100644 --- a/src/main/kotlin/io/hawk/service/dlp/DlpJob.kt +++ b/src/main/kotlin/io/hawk/service/dlp/DlpJob.kt @@ -1,5 +1,9 @@ package io.hawk.service.dlp +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import com.fasterxml.jackson.databind.annotation.JsonSerialize import io.hawk.dlp.common.JobStatus import jakarta.persistence.* import java.time.LocalDateTime @@ -22,6 +26,15 @@ class DlpJob { @Column(columnDefinition = "TEXT") var error: String? = null - @OneToMany(mappedBy = "job") - lateinit var results: List + @JvmSuppressWildcards + @OneToMany(mappedBy = "job", cascade = [CascadeType.ALL]) + @JsonSerialize(using = DlpResultOverviewSerializer::class) + lateinit var results: List + + class DlpResultOverviewSerializer : JsonSerializer>() { + override fun serialize(result: List, generator: JsonGenerator, provider: SerializerProvider) { + generator.writeArray(result.map { it.id.toString() }.toTypedArray(), 0, result.size) + } + + } } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpJobRepository.kt b/src/main/kotlin/io/hawk/service/dlp/DlpJobRepository.kt new file mode 100644 index 0000000..b7f2468 --- /dev/null +++ b/src/main/kotlin/io/hawk/service/dlp/DlpJobRepository.kt @@ -0,0 +1,6 @@ +package io.hawk.service.dlp + +import org.springframework.data.jpa.repository.JpaRepository +import java.util.* + +interface DlpJobRepository : JpaRepository \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpOutputRestController.kt b/src/main/kotlin/io/hawk/service/dlp/DlpOutputRestController.kt index 790e2f5..0a86e96 100644 --- a/src/main/kotlin/io/hawk/service/dlp/DlpOutputRestController.kt +++ b/src/main/kotlin/io/hawk/service/dlp/DlpOutputRestController.kt @@ -1,7 +1,24 @@ package io.hawk.service.dlp +import org.springframework.data.domain.Sort +import org.springframework.http.HttpStatus +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController +import org.springframework.web.server.ResponseStatusException +import java.util.* @RestController -class DlpOutputRestController { +@RequestMapping("/api/dlp") +class DlpOutputRestController( + private val dlpJobRepository: DlpJobRepository, + private val dlpResultRepository: DlpResultRepository +) { + @GetMapping + fun list(): List = dlpJobRepository.findAll(Sort.by(Sort.Direction.DESC, "created")) + + @GetMapping("/results/{id}") + fun show(@PathVariable id: UUID): DlpResult = + dlpResultRepository.findById(id).orElseThrow { ResponseStatusException(HttpStatus.NOT_FOUND) } } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/dlp/DlpResultRepository.kt b/src/main/kotlin/io/hawk/service/dlp/DlpResultRepository.kt new file mode 100644 index 0000000..4eaf1c8 --- /dev/null +++ b/src/main/kotlin/io/hawk/service/dlp/DlpResultRepository.kt @@ -0,0 +1,6 @@ +package io.hawk.service.dlp + +import org.springframework.data.jpa.repository.JpaRepository +import java.util.* + +interface DlpResultRepository : JpaRepository \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt b/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt index b641aac..57f9ecd 100644 --- a/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt +++ b/src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt @@ -1,5 +1,6 @@ package io.hawk.service.dlp +import jakarta.persistence.CascadeType import jakarta.persistence.DiscriminatorValue import jakarta.persistence.Entity import jakarta.persistence.OneToMany @@ -7,6 +8,6 @@ import jakarta.persistence.OneToMany @Entity @DiscriminatorValue("inspect") class InspectDlpResult : DlpResult() { - @OneToMany(mappedBy = "result") + @OneToMany(mappedBy = "result", cascade = [CascadeType.ALL]) lateinit var findings: List } \ No newline at end of file diff --git a/src/main/kotlin/io/hawk/service/traffic/field/FieldController.kt b/src/main/kotlin/io/hawk/service/traffic/field/FieldController.kt index df8ad3b..29320c6 100644 --- a/src/main/kotlin/io/hawk/service/traffic/field/FieldController.kt +++ b/src/main/kotlin/io/hawk/service/traffic/field/FieldController.kt @@ -1,10 +1,10 @@ package io.hawk.service.traffic.field import io.swagger.v3.oas.annotations.Operation +import jakarta.validation.Valid import org.springframework.http.HttpStatus import org.springframework.validation.annotation.Validated import org.springframework.web.bind.annotation.* -import jakarta.validation.Valid @Validated @RestController @@ -19,6 +19,9 @@ class FieldController(private val fieldService: FieldService) { @Operation(description = "Get single existing field") fun show(@PathVariable name: String) = fieldService.showField(name) + @GetMapping("/{name}/dlp") + fun showDlp(@PathVariable name: String) = fieldService.showDlp(name) + @PostMapping @Operation(description = "Create single field") fun create(@Valid @RequestBody request: FieldRequest) = fieldService.insertField(request) 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 29edf27..c2f2584 100644 --- a/src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt +++ b/src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt @@ -1,6 +1,7 @@ package io.hawk.service.traffic.field import io.hawk.dlp.common.InfoType +import io.hawk.service.dlp.DlpFinding import org.springframework.stereotype.Service @Service @@ -41,4 +42,6 @@ class FieldService( request.obligationToProvide?.also { obligationToProvide = it } request.consequences?.also { consequences = it } } + + fun showDlp(name: String): List = showField(name).findings } \ No newline at end of file