94 lines
3.5 KiB
Kotlin
94 lines
3.5 KiB
Kotlin
package com.securityc4po.api.pentest
|
|
|
|
import com.securityc4po.api.configuration.BC_BAD_CAST_TO_ABSTRACT_COLLECTION
|
|
import com.securityc4po.api.extensions.getLoggerFor
|
|
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
|
|
import com.securityc4po.api.ResponseBody
|
|
import com.securityc4po.api.finding.FindingRequestBody
|
|
import com.securityc4po.api.finding.FindingService
|
|
import com.securityc4po.api.finding.toFindingResponseBody
|
|
import org.springframework.http.ResponseEntity
|
|
import org.springframework.http.ResponseEntity.noContent
|
|
import org.springframework.web.bind.annotation.*
|
|
import reactor.core.publisher.Mono
|
|
|
|
@RestController
|
|
@RequestMapping("/pentests")
|
|
@CrossOrigin(
|
|
origins = [],
|
|
allowCredentials = "false",
|
|
allowedHeaders = ["*"],
|
|
methods = [RequestMethod.GET, RequestMethod.DELETE, RequestMethod.POST, RequestMethod.PATCH]
|
|
)
|
|
@SuppressFBWarnings(BC_BAD_CAST_TO_ABSTRACT_COLLECTION)
|
|
class PentestController(private val pentestService: PentestService, private val findingService: FindingService) {
|
|
|
|
var logger = getLoggerFor<PentestController>()
|
|
|
|
@GetMapping
|
|
fun getPentestsByProjectIdAndCategory(
|
|
@RequestParam("projectId") projectId: String,
|
|
@RequestParam("category") category: String
|
|
): Mono<ResponseEntity<List<ResponseBody>>> {
|
|
return pentestService.getPentestsForCategory(projectId, PentestCategory.valueOf(category)).map { pentestList ->
|
|
pentestList.map {
|
|
it.toPentestResponseBody()
|
|
}
|
|
}.map {
|
|
if (it.isEmpty()) noContent().build()
|
|
else ResponseEntity.ok(it)
|
|
}
|
|
}
|
|
|
|
/* Todo: Add API
|
|
@GetMapping
|
|
fun getPentestById(
|
|
@RequestParam("pentestId") pentestId: String
|
|
): Mono<ResponseEntity<List<ResponseBody>>> {
|
|
return pentestService.getPentest(pentestId).map {
|
|
ResponseEntity.ok(it)
|
|
}
|
|
}*/
|
|
|
|
@PostMapping("/{projectId}")
|
|
fun savePentest(
|
|
@PathVariable(value = "projectId") projectId: String,
|
|
@RequestBody body: PentestRequestBody
|
|
): Mono<ResponseEntity<ResponseBody>> {
|
|
return this.pentestService.savePentest(projectId, body).map {
|
|
ResponseEntity.accepted().body(it.toPentestResponseBody())
|
|
}
|
|
}
|
|
|
|
@PatchMapping("/{pentestId}")
|
|
fun updatePentest(
|
|
@PathVariable(value = "pentestId") pentestId: String,
|
|
@RequestBody body: PentestRequestBody
|
|
): Mono<ResponseEntity<ResponseBody>> {
|
|
return this.pentestService.updatePentest(pentestId, body).map {
|
|
ResponseEntity.accepted().body(it.toPentestResponseBody())
|
|
}
|
|
}
|
|
|
|
@GetMapping("/{pentestId}/findings")
|
|
fun getFindings(@PathVariable(value = "pentestId") pentestId: String): Mono<ResponseEntity<List<ResponseBody>>> {
|
|
return this.pentestService.getFindingIdsByPentestId(pentestId).flatMap { findingIds: List<String> ->
|
|
this.findingService.getFindingsByIds(findingIds).map { findingList ->
|
|
findingList.map { it.toFindingResponseBody() }
|
|
}
|
|
}.map {
|
|
if (it.isEmpty()) ResponseEntity.noContent().build()
|
|
else ResponseEntity.ok(it)
|
|
}
|
|
}
|
|
|
|
@PostMapping("/{pentestId}/finding")
|
|
fun saveFinding(
|
|
@PathVariable(value = "pentestId") pentestId: String,
|
|
@RequestBody body: FindingRequestBody
|
|
): Mono<ResponseEntity<ResponseBody>> {
|
|
return this.findingService.saveFinding(pentestId, body).map {
|
|
ResponseEntity.accepted().body(it.toFindingResponseBody())
|
|
}
|
|
}
|
|
} |