forked from scanoss/ort_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Advisor: Add Sonatype Nexus IQ as a security advisor
Add Sonatype Nexus IQ Server [1] as a security advisor module. [1] https://help.sonatype.com/iqserver Resolves oss-review-toolkit#3268. Signed-off-by: Marcel Bochtler <[email protected]>
- Loading branch information
1 parent
6a4a90e
commit e311617
Showing
5 changed files
with
208 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright (C) 2020 Bosch.IO GmbH | ||
* | ||
* 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.advisors | ||
|
||
import java.io.IOException | ||
import java.net.URL | ||
import java.time.Instant | ||
import java.util.concurrent.Executors | ||
|
||
import kotlinx.coroutines.asCoroutineDispatcher | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.withContext | ||
|
||
import org.ossreviewtoolkit.advisor.AbstractAdvisorFactory | ||
import org.ossreviewtoolkit.advisor.Advisor | ||
import org.ossreviewtoolkit.model.AdvisorDetails | ||
import org.ossreviewtoolkit.model.AdvisorResult | ||
import org.ossreviewtoolkit.model.AdvisorSummary | ||
import org.ossreviewtoolkit.model.Identifier | ||
import org.ossreviewtoolkit.model.Package | ||
import org.ossreviewtoolkit.model.Vulnerability | ||
import org.ossreviewtoolkit.model.config.AdvisorConfiguration | ||
import org.ossreviewtoolkit.model.config.NexusIqConfiguration | ||
import org.ossreviewtoolkit.model.createAndLogIssue | ||
import org.ossreviewtoolkit.nexusiq.NexusIqService | ||
import org.ossreviewtoolkit.utils.NamedThreadFactory | ||
import org.ossreviewtoolkit.utils.OkHttpClientHelper | ||
import org.ossreviewtoolkit.utils.collectMessagesAsString | ||
import org.ossreviewtoolkit.utils.log | ||
import org.ossreviewtoolkit.utils.showStackTrace | ||
|
||
import retrofit2.Call | ||
|
||
/** | ||
* A wrapper for [Nexus IQ Server](https://help.sonatype.com/iqserver) security vulnerability data. | ||
*/ | ||
class NexusIq( | ||
name: String, | ||
config: AdvisorConfiguration | ||
) : Advisor(name, config) { | ||
class Factory : AbstractAdvisorFactory<NexusIq>("NexusIQ") { | ||
override fun create(config: AdvisorConfiguration) = NexusIq(advisorName, config) | ||
} | ||
|
||
data class NexusIqVulnerability( | ||
override val id: String, | ||
override val severity: Float, | ||
|
||
// URL to http://mitre.org for more information about the CVE. | ||
val url: URL | ||
) : Vulnerability | ||
|
||
private val nexusIqConfig = config as NexusIqConfiguration | ||
|
||
override suspend fun retrievePackageVulnerabilities(packages: List<Package>): Map<Package, List<AdvisorResult>> { | ||
val service = NexusIqService.create( | ||
nexusIqConfig.serverUrl, | ||
nexusIqConfig.username, | ||
nexusIqConfig.password, | ||
OkHttpClientHelper.buildClient() | ||
) | ||
|
||
val advisorDispatcher = | ||
Executors.newSingleThreadExecutor(NamedThreadFactory(advisorName)).asCoroutineDispatcher() | ||
|
||
return coroutineScope { | ||
val startTime = Instant.now() | ||
|
||
val components = packages.map { pkg -> | ||
val packageUrl = buildString { | ||
append(pkg.purl) | ||
val purlType = pkg.id.getPurlType() | ||
if (purlType == Identifier.PurlType.MAVEN) append("?type=jar") | ||
if (purlType == Identifier.PurlType.PYPI) append("?extension=tar.gz") | ||
} | ||
|
||
NexusIqService.Component(packageUrl) | ||
} | ||
|
||
try { | ||
val componentDetails = | ||
execute(withContext(advisorDispatcher) { | ||
service.getComponentDetails(NexusIqService.ComponentsWrapper(components)) | ||
}).componentDetails | ||
.associateBy { it.component.packageUrl.substringBefore("?") } | ||
|
||
val endTime = Instant.now() | ||
|
||
packages.mapNotNullTo(mutableListOf()) { pkg -> | ||
componentDetails[pkg.id.toPurl()]?.takeUnless { | ||
it.securityData.securityIssues.isEmpty() | ||
}?.let { details -> | ||
pkg to listOf( | ||
AdvisorResult( | ||
details.securityData.securityIssues.mapToVulnerabilities(), | ||
AdvisorDetails(advisorName), | ||
AdvisorSummary(startTime, endTime) | ||
) | ||
) | ||
} | ||
}.toMap() | ||
} catch (e: IOException) { | ||
e.showStackTrace() | ||
|
||
val now = Instant.now() | ||
packages.associateWith { | ||
listOf( | ||
AdvisorResult( | ||
vulnerability = emptyList(), | ||
advisor = AdvisorDetails(advisorName), | ||
summary = AdvisorSummary( | ||
startTime = now, | ||
endTime = now, | ||
issues = listOf( | ||
createAndLogIssue( | ||
source = advisorName, | ||
message = "Failed to retrieve security vulnerabilities from $advisorName: " + | ||
e.collectMessagesAsString() | ||
) | ||
) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun Collection<NexusIqService.SecurityIssue>.mapToVulnerabilities() = | ||
map { | ||
NexusIqVulnerability( | ||
it.reference, | ||
it.severity, | ||
it.url | ||
) | ||
} | ||
|
||
/** | ||
* Execute an HTTP request specified by the given [call]. The response status is checked. If everything went | ||
* well, the marshalled body of the request is returned; otherwise, the function throws an exception. | ||
*/ | ||
private fun <T> execute(call: Call<T>): T { | ||
val request = "${call.request().method} on ${call.request().url}" | ||
log.debug { "Executing HTTP $request." } | ||
|
||
val response = call.execute() | ||
log.debug { "HTTP response is (status ${response.code()}): ${response.message()}" } | ||
|
||
val body = response.body() | ||
if (!response.isSuccessful || body == null) { | ||
throw IOException( | ||
"Failed HTTP $request with status ${response.code()} and error: ${response.errorBody()?.string()}." | ||
) | ||
} | ||
|
||
return body | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
advisor/src/main/resources/META-INF/services/org.ossreviewtoolkit.advisor.AdvisorFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.ossreviewtoolkit.advisor.advisors.NexusIq$Factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters