Skip to content
Snippets Groups Projects
Verified Commit e4f76e0f authored by Jan-Niclas Strüwer's avatar Jan-Niclas Strüwer
Browse files

first setup with r2dbc database

parent 2e861a6f
No related branches found
No related tags found
No related merge requests found
......@@ -3,20 +3,6 @@ package de.fraunhofer.iem.dataprovider
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
// Taken from https://spring.io/guides/tutorials/spring-webflux-kotlin-rsocket/
//@Configuration
//class Config {
//
// @Bean
// fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {
// val initializer = ConnectionFactoryInitializer()
// initializer.setConnectionFactory(connectionFactory)
// val populator = CompositeDatabasePopulator()
// populator.addPopulators(ResourceDatabasePopulator(ClassPathResource("./sql/schema.sql")))
// initializer.setDatabasePopulator(populator)
// return initializer
// }
//}
@SpringBootApplication
class DataProviderApplication
......
package de.fraunhofer.iem.dataprovider
import io.r2dbc.spi.ConnectionFactory
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.core.io.ClassPathResource
import org.springframework.r2dbc.connection.init.CompositeDatabasePopulator
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer
import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity
import org.springframework.security.config.web.server.ServerHttpSecurity
import org.springframework.security.config.web.server.invoke
......@@ -25,4 +30,16 @@ class SecurityConfiguration {
}
}
}
// Inspired from https://spring.io/guides/tutorials/spring-webflux-kotlin-rsocket/
@Bean
fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {
val initializer = ConnectionFactoryInitializer()
initializer.setConnectionFactory(connectionFactory)
val populator = CompositeDatabasePopulator()
populator.addPopulators(ResourceDatabasePopulator(ClassPathResource("./sql/schema.sql")))
initializer.setDatabasePopulator(populator)
return initializer
}
}
\ No newline at end of file
package de.fraunhofer.iem.dataprovider.sarif
import de.fraunhofer.iem.dataprovider.toolResult.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import java.io.IOException
import java.nio.file.Path
fun Rule.asDbObject(): RuleDb {
return RuleDb(
ruleId = this.id,
name = this.name,
shortDescription = this.shortDescription.text,
helpUri = this.helpUri
)
}
fun Tool.asDbObject(): ToolDb {
return ToolDb(
fullName = this.driver.fullName,
name = this.driver.name,
rules = this.driver.rules.map { it.asDbObject() },
downloadUri = this.driver.downloadUri,
guid = this.driver.guid,
informationUri = this.driver.informationUri,
language = this.driver.language,
organization = this.driver.organization,
semanticVersion = this.driver.semanticVersion,
version = this.driver.version,
)
}
fun Location.asDbObject(): ResultLocationDb {
return ResultLocationDb(
artifactLocationUri = this.physicalLocation.artifactLocation.uri,
endColumn = this.physicalLocation.region.endColumn,
endLine = this.physicalLocation.region.endLine,
startColumn = this.physicalLocation.region.startColumn,
startLine = this.physicalLocation.region.startLine,
)
}
fun Result.asDbObject(): ToolResultDb {
return ToolResultDb(
level = this.level,
locations = this.locations.map { it.asDbObject() },
message = this.message.text,
ruleId = this.ruleId
)
}
fun Sarif.asDbObject(): List<ToolResultsDb> {
return this.runs.map { run ->
ToolResultsDb(
results = run.results.map { it.asDbObject() },
tool = run.tool.asDbObject(),
)
}
}
//fun Rule.asDbObject(): RuleDb {
// return RuleDb(
// ruleId = this.id,
// name = this.name,
// shortDescription = this.shortDescription.text,
// helpUri = this.helpUri
// )
//}
//
//fun Tool.asDbObject(): ToolDb {
// return ToolDb(
// fullName = this.driver.fullName,
// name = this.driver.name,
// rules = this.driver.rules.map { it.asDbObject() },
// downloadUri = this.driver.downloadUri,
// guid = this.driver.guid,
// informationUri = this.driver.informationUri,
// language = this.driver.language,
// organization = this.driver.organization,
// semanticVersion = this.driver.semanticVersion,
// version = this.driver.version,
// )
//}
//
//fun Location.asDbObject(): ResultLocationDb {
// return ResultLocationDb(
// artifactLocationUri = this.physicalLocation.artifactLocation.uri,
// endColumn = this.physicalLocation.region.endColumn,
// endLine = this.physicalLocation.region.endLine,
// startColumn = this.physicalLocation.region.startColumn,
// startLine = this.physicalLocation.region.startLine,
// )
//}
//
//fun Result.asDbObject(): ToolResultDb {
// return ToolResultDb(
// level = this.level,
// locations = this.locations.map { it.asDbObject() },
// message = this.message.text,
// ruleId = this.ruleId
// )
//}
//
//fun Sarif.asDbObject(): List<ToolResultsDb> {
//
//
// return this.runs.map { run ->
// ToolResultsDb(
// results = run.results.map { it.asDbObject() },
// tool = run.tool.asDbObject(),
// )
// }
//}
fun getSarifFromFilePath(resultPath: Path): Sarif {
val resFile = resultPath.toFile()
......
......@@ -2,7 +2,9 @@ package de.fraunhofer.iem.dataprovider.taskManager
import de.fraunhofer.iem.dataprovider.gitlab.GitConfiguration
import de.fraunhofer.iem.dataprovider.logger.getLogger
import de.fraunhofer.iem.dataprovider.sarif.Sarif
import de.fraunhofer.iem.dataprovider.taskManager.tasks.*
import de.fraunhofer.iem.dataprovider.toolResult.ToolResultsPersistenceService
import jakarta.annotation.PreDestroy
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
......@@ -25,13 +27,18 @@ class GitCloneDone(override val taskId: UUID, val outputDirectory: String) : Tas
class GetGitlabProjectDone(override val taskId: UUID, val gitProject: GitProject) : TaskDone()
class ProcessTaskDone(override val taskId: UUID, val message: String) : TaskDone()
class DetektDone(override val taskId: UUID, val sarif: Sarif) : TaskDone()
/**
* The Task Manager takes tasks and distributes them to
* underlying workers. Internally it uses a channel
* to manage incoming tasks.
*/
@Component
class TaskManager(private val config: Config) {
class TaskManager(
private val config: Config,
private val toolResultsPersistenceService: ToolResultsPersistenceService
) {
// The used default dispatcher is ok for CPU-bound workloads. However,
// if they block for a long time it's better to use a custom thread
......@@ -103,6 +110,9 @@ class TaskManager(private val config: Config) {
worker.addTask(DetektTask(event.outputDirectory, analysisResultsPath, ::addEvent))
}
is DetektDone -> {
toolResultsPersistenceService.save(event.sarif)
}
else -> {
logger.info("Received event without special handling associated $event")
......
package de.fraunhofer.iem.dataprovider.taskManager.tasks
import de.fraunhofer.iem.dataprovider.sarif.getSarifFromFilePath
import de.fraunhofer.iem.dataprovider.taskManager.DetektDone
import de.fraunhofer.iem.dataprovider.taskManager.Event
import de.fraunhofer.iem.dataprovider.taskManager.ProcessTaskDone
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource
import java.nio.file.Paths
......@@ -32,6 +33,6 @@ class DetektTask(projectPath: String, outputPath: String, override val responseC
}
}
responseChannel(ProcessTaskDone(taskID, returnMessage))
responseChannel(DetektDone(this.taskID, sarifResult))
}
}
\ No newline at end of file
package de.fraunhofer.iem.dataprovider.toolResult
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.Table
@Table("TOOL_RESULTS")
data class ToolResultsDb(
val results: List<ToolResultDb>,
val tool: ToolDb,
@Column("SARIFRESULT")
val sarifResult: String,
@Id var id: String? = null
)
......
package de.fraunhofer.iem.dataprovider.toolResult
import de.fraunhofer.iem.dataprovider.logger.getLogger
import de.fraunhofer.iem.dataprovider.sarif.Sarif
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.springframework.stereotype.Service
@Service
class ToolResultsPersistenceService(val toolResultsRepository: ToolResultsRepository) {
private val logger = getLogger(javaClass)
suspend fun save(sarif: Sarif) {
// toolResultsRepository.save(sarif.asDbObject())
logger.info("Save sarif request received.")
val sarifString = Json.encodeToString(sarif)
logger.debug("$sarifString")
toolResultsRepository.save(
ToolResultsDb(sarifResult = sarifString)
)
}
}
\ No newline at end of file
CREATE TABLE IF NOT EXISTS tool_results
(
id VARCHAR(60) DEFAULT RANDOM_UUID() PRIMARY KEY,
SARIF_RESULT VARCHAR NOT NULL
);
\ No newline at end of file
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.