Skip to content
Snippets Groups Projects
Verified Commit 517b35f0 authored by Hutomo Saleh's avatar Hutomo Saleh
Browse files

feat: query from Tool Service API and get Trivy result from it

parent 30d6669c
No related branches found
No related tags found
1 merge request!52feat/trivyEndpoint
package de.fraunhofer.iem.app.tools.trivy.json
import de.fraunhofer.iem.spha.model.adapter.trivy.TrivyDtoV2
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
@Serializable
data class ToolResultsDto(
val trivy: List<TrivyDtoV2>
// Add more tools here.
)
@Serializable
data class RawToolResult(
@SerialName("tool") val tool: String,
@SerialName("output") val output: JsonElement,
)
......@@ -4,6 +4,8 @@ import de.fraunhofer.iem.app.configuration.OpenCodeApiProperties
import de.fraunhofer.iem.app.logger.getLogger
import de.fraunhofer.iem.app.tool.dto.CreateToolDto
import de.fraunhofer.iem.app.tool.enumeration.ToolType
import de.fraunhofer.iem.app.tools.trivy.json.RawToolResult
import de.fraunhofer.iem.app.tools.trivy.json.ToolResultsDto
import de.fraunhofer.iem.app.utilities.ApiException
import de.fraunhofer.iem.app.utilities.HttpClientWrapper
import de.fraunhofer.iem.spha.model.adapter.trivy.TrivyDtoV2
......@@ -11,6 +13,8 @@ import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
import org.springframework.stereotype.Service
@Service
......@@ -25,12 +29,12 @@ class TrivyService(
suspend fun getTrivyResults(projectId: Long): List<TrivyDtoV2> {
val trivyResults =
try {
queryTrivyApi(projectId)
queryToolResultApi(projectId).trivy
} catch (e: Exception) {
logger.error("Query to ORT API failed with exception $e")
logger.error("Query to Tool Service API failed with exception $e")
emptyList()
}
logger.info("Got ${trivyResults.size} ORT results for $projectId.")
logger.info("Got ${trivyResults.size} Tool Service results for $projectId.")
return trivyResults
}
......@@ -38,23 +42,33 @@ class TrivyService(
return CreateToolDto("TRIVY", ToolType.TRIVY)
}
suspend fun queryTrivyApi(projectId: Long): List<TrivyDtoV2> {
logger.info("projectId $projectId: Query ORT API for repo")
// TODO: Move all tool result methods into own service
private suspend fun queryToolResultApi(projectId: Long): ToolResultsDto {
logger.info("projectId $projectId: Query Tool Service API for repo")
val response: HttpResponse = httpClient.get(getToolApiPath(projectId))
val trivyJson = response.body<List<TrivyDtoV2>>()
logger.info("projectId $projectId: Query ORT API returned with ${response.status}")
val toolResults = parseToolResults(response.body<List<RawToolResult>>())
logger.info("projectId $projectId: Query Tool Service API returned with ${response.status}")
if (response.status != HttpStatusCode.OK) {
throw ApiException(
response.status.value,
"projectId $projectId: ORT API returned with code ${response.status}",
"projectId $projectId: Tool Service API returned with code ${response.status}",
)
}
return toolResults
}
return trivyJson
private fun parseToolResults(rawToolResults: List<RawToolResult>): ToolResultsDto {
val trivyResults = mutableListOf<TrivyDtoV2>()
rawToolResults.forEach { result ->
when (result.tool.uppercase()) {
ToolType.TRIVY.name ->
trivyResults.add(Json.decodeFromJsonElement<TrivyDtoV2>(result.output))
}
}
return ToolResultsDto(trivyResults)
}
private fun getToolApiPath(projectId: Long): String {
return "${openCodeApiProperties.toolService}/$projectId/trivy"
return "${openCodeApiProperties.toolService}/$projectId"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment

Consent

On this website, we use the web analytics service Matomo to analyze and review the use of our website. Through the collected statistics, we can improve our offerings and make them more appealing for you. Here, you can decide whether to allow us to process your data and set corresponding cookies for these purposes, in addition to technically necessary cookies. Further information on data protection—especially regarding "cookies" and "Matomo"—can be found in our privacy policy. You can withdraw your consent at any time.