Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Feb 13, 2024
1 parent 9a620d8 commit 103df52
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions modules/bool/src/main/kotlin/com/github/niqdev/todo/sbom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ data class Sbom(
val packages: List<Package>
)

fun Sbom.isVulnerable(catalog: VulnerabilityCatalog): Boolean =
catalog.vulnerabilities.run(VulnerabilityPredicate.evalAnyOf(packages, catalog))

data class VulnerabilityCatalog(
val vulnerabilities: FreeB<VulnerabilityPredicate>
)

// https://osv.dev
// TODO semver
sealed interface VulnerabilityPredicate {
Expand All @@ -37,7 +44,7 @@ sealed interface VulnerabilityPredicate {
data class IsPackage(val type: PackageType) : VulnerabilityPredicate

companion object {
fun eval(pkg: Package): (VulnerabilityPredicate) -> Boolean = {
private fun eval(pkg: Package): (VulnerabilityPredicate) -> Boolean = {
when (it) {
is IsName -> it.name == pkg.name
is IsVersion -> it.version == pkg.version
Expand All @@ -46,17 +53,12 @@ sealed interface VulnerabilityPredicate {
is IsPackage -> it.type == pkg.type
}
}
}
}

data class VulnerabilityCatalog(
val vulnerabilities: FreeB<VulnerabilityPredicate>
)

// TODO extract CatalogPredicate?
fun VulnerabilityCatalog.eval(packages: List<Package>): (VulnerabilityPredicate) -> Boolean = {
packages.fold(false) { isVulnerable, pkg ->
isVulnerable || this.vulnerabilities.run(VulnerabilityPredicate.eval(pkg))
fun evalAnyOf(packages: List<Package>, catalog: VulnerabilityCatalog): (VulnerabilityPredicate) -> Boolean = {
packages.fold(false) { isVulnerable, pkg ->
isVulnerable || catalog.vulnerabilities.run(eval(pkg))
}
}
}
}

Expand Down Expand Up @@ -84,6 +86,5 @@ fun main() {
vulnerabilities = fooVulnerability or barVulnerability
)

val isVulnerable = apkCatalog.vulnerabilities.run(apkCatalog.eval(sbom.packages))
println(isVulnerable)
println(sbom.isVulnerable(apkCatalog))
}

0 comments on commit 103df52

Please sign in to comment.