Skip to content

Commit

Permalink
Add way to insert dlp jobs and query dlp jobs / results / findings. A…
Browse files Browse the repository at this point in the history
…lso add possibility to query dlp findings by field
  • Loading branch information
p4skal committed Jan 9, 2024
1 parent 24a5b5a commit 877479b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/main/kotlin/io/hawk/service/dlp/DlpFinding.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,5 +30,8 @@ class DlpFinding {

@ManyToOne
@JoinColumn
@JsonIdentityReference(alwaysAsId=true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator::class, property="id")
lateinit var result: InspectDlpResult

}
39 changes: 33 additions & 6 deletions src/main/kotlin/io/hawk/service/dlp/DlpInputRestController.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 15 additions & 2 deletions src/main/kotlin/io/hawk/service/dlp/DlpJob.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,6 +26,15 @@ class DlpJob {
@Column(columnDefinition = "TEXT")
var error: String? = null

@OneToMany(mappedBy = "job")
lateinit var results: List<out DlpResult>
@JvmSuppressWildcards
@OneToMany(mappedBy = "job", cascade = [CascadeType.ALL])
@JsonSerialize(using = DlpResultOverviewSerializer::class)
lateinit var results: List<DlpResult>

class DlpResultOverviewSerializer : JsonSerializer<List<DlpResult>>() {
override fun serialize(result: List<DlpResult>, generator: JsonGenerator, provider: SerializerProvider) {
generator.writeArray(result.map { it.id.toString() }.toTypedArray(), 0, result.size)
}

}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/io/hawk/service/dlp/DlpJobRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.hawk.service.dlp

import org.springframework.data.jpa.repository.JpaRepository
import java.util.*

interface DlpJobRepository : JpaRepository<DlpJob, UUID>
19 changes: 18 additions & 1 deletion src/main/kotlin/io/hawk/service/dlp/DlpOutputRestController.kt
Original file line number Diff line number Diff line change
@@ -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<DlpJob> = 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) }
}
6 changes: 6 additions & 0 deletions src/main/kotlin/io/hawk/service/dlp/DlpResultRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.hawk.service.dlp

import org.springframework.data.jpa.repository.JpaRepository
import java.util.*

interface DlpResultRepository : JpaRepository<DlpResult, UUID>
3 changes: 2 additions & 1 deletion src/main/kotlin/io/hawk/service/dlp/InspectDlpResult.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.hawk.service.dlp

import jakarta.persistence.CascadeType
import jakarta.persistence.DiscriminatorValue
import jakarta.persistence.Entity
import jakarta.persistence.OneToMany

@Entity
@DiscriminatorValue("inspect")
class InspectDlpResult : DlpResult() {
@OneToMany(mappedBy = "result")
@OneToMany(mappedBy = "result", cascade = [CascadeType.ALL])
lateinit var findings: List<DlpFinding>
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/io/hawk/service/traffic/field/FieldService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,4 +42,6 @@ class FieldService(
request.obligationToProvide?.also { obligationToProvide = it }
request.consequences?.also { consequences = it }
}

fun showDlp(name: String): List<DlpFinding> = showField(name).findings
}

0 comments on commit 877479b

Please sign in to comment.