Skip to content

Commit

Permalink
VulnerabilityProvider: Add class that will represent each advisor
Browse files Browse the repository at this point in the history
Signed-off-by: Korbinian Singhammer <[email protected]>
  • Loading branch information
Korbinian Singhammer authored and Korbinian Singhammer committed Mar 16, 2021
1 parent 70eb3b1 commit 97dd5df
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions advisor/src/main/kotlin/VulnerabilityProvider.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2020-2021 Bosch.IO GmbH
* Copyright (C) 2021 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.advisor

import java.time.Instant

import org.ossreviewtoolkit.model.AdvisorDetails
import org.ossreviewtoolkit.model.AdvisorResult
import org.ossreviewtoolkit.model.AdvisorSummary
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.createAndLogIssue
import org.ossreviewtoolkit.utils.collectMessagesAsString
import org.ossreviewtoolkit.utils.showStackTrace

/**
* An abstract class that represents a service that can retrieve vulnerability information
* for a list of given [Package]s.
*/
abstract class VulnerabilityProvider(val providerName: String) {

/**
* For a given list of [Package]s, retrieve vulnerability information and return a map
* that associates each package with a list of [AdvisorResult]s. Needs to be implemented
* by child classes.
*/
protected abstract suspend fun retrievePackageVulnerabilities(
packages: List<Package>
): Map<Package, List<AdvisorResult>>

/**
* A generic method that creates a failed [AdvisorResult] for [Package]s if there was an issue
* during the retrieval of vulnerability information.
*/
protected fun createFailedResults(
startTime: Instant,
packages: List<Package>,
t: Throwable
): Map<Package, List<AdvisorResult>> {
val endTime = Instant.now()

t.showStackTrace()

val failedResults = listOf(
AdvisorResult(
vulnerabilities = emptyList(),
advisor = AdvisorDetails(providerName),
summary = AdvisorSummary(
startTime = startTime,
endTime = endTime,
issues = listOf(
createAndLogIssue(
source = providerName,
message = "Failed to retrieve security vulnerabilities from $providerName: " +
t.collectMessagesAsString()
)
)
)
)
)

return packages.associateWith { failedResults }
}
}

0 comments on commit 97dd5df

Please sign in to comment.