de.fraunhofer.iem.spha.adapter.tools.trufflehog.TrufflehogAdapter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spha-adapter Show documentation
Show all versions of spha-adapter Show documentation
SPHA is a collection of libraries to work with hierarchical KPI models.
The newest version!
/*
* Copyright (c) 2024 Fraunhofer IEM. All rights reserved.
*
* Licensed under the MIT license. See LICENSE file in the project root for details.
*
* SPDX-License-Identifier: MIT
* License-Filename: LICENSE
*/
package de.fraunhofer.iem.spha.adapter.tools.trufflehog
import de.fraunhofer.iem.spha.adapter.AdapterResult
import de.fraunhofer.iem.spha.model.adapter.trufflehog.TrufflehogDto
import de.fraunhofer.iem.spha.model.adapter.trufflehog.TrufflehogReportDto
import de.fraunhofer.iem.spha.model.kpi.KpiId
import de.fraunhofer.iem.spha.model.kpi.RawValueKpi
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.InputStream
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.decodeFromStream
object TrufflehogAdapter {
private val logger = KotlinLogging.logger {}
private val jsonParser = Json {
ignoreUnknownKeys = true
explicitNulls = false
}
@OptIn(ExperimentalSerializationApi::class)
fun dtoFromJson(jsonData: InputStream): List {
val rawResult = jsonParser.decodeFromStream(jsonData)
return rawResult.results.mapNotNull {
try {
jsonParser.decodeFromJsonElement(it)
} catch (e: Exception) {
logger.warn { "Decoding of trufflehog result failed for $it with ${e.message}" }
null
}
}
}
fun transformDataToKpi(data: TrufflehogReportDto): Collection {
return transformDataToKpi(listOf(data))
}
fun transformDataToKpi(data: Collection): Collection {
return data.map {
val score = if (it.verifiedSecrets > 0) 0 else 100
AdapterResult.Success.Kpi(RawValueKpi(score = score, kpiId = KpiId.SECRETS.name))
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy