forked from oss-review-toolkit/ort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanResultsStorage.kt
385 lines (326 loc) · 16.3 KB
/
ScanResultsStorage.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright (C) 2017-2021 HERE Europe B.V.
* Copyright (C) 2019 Bosch Software Innovations 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
*
* https://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.scanner
import kotlin.time.measureTimedValue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
import org.ossreviewtoolkit.model.AccessStatistics
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.UnknownProvenance
import org.ossreviewtoolkit.model.config.ClearlyDefinedStorageConfiguration
import org.ossreviewtoolkit.model.config.FileBasedStorageConfiguration
import org.ossreviewtoolkit.model.config.PostgresStorageConfiguration
import org.ossreviewtoolkit.model.config.ScanStorageConfiguration
import org.ossreviewtoolkit.model.config.ScannerConfiguration
import org.ossreviewtoolkit.model.config.Sw360StorageConfiguration
import org.ossreviewtoolkit.model.utils.DatabaseUtils
import org.ossreviewtoolkit.scanner.experimental.NestedProvenance
import org.ossreviewtoolkit.scanner.experimental.NestedProvenanceScanResult
import org.ossreviewtoolkit.scanner.experimental.PackageBasedScanStorage
import org.ossreviewtoolkit.scanner.experimental.ScanStorageException
import org.ossreviewtoolkit.scanner.experimental.toNestedProvenanceScanResult
import org.ossreviewtoolkit.scanner.storages.*
import org.ossreviewtoolkit.utils.ort.logger
import org.ossreviewtoolkit.utils.ort.ortDataDirectory
import org.ossreviewtoolkit.utils.ort.storage.HttpFileStorage
import org.ossreviewtoolkit.utils.ort.storage.LocalFileStorage
import org.ossreviewtoolkit.utils.ort.storage.XZCompressedLocalFileStorage
/**
* The abstract class that storage backends for scan results need to implement.
*/
abstract class ScanResultsStorage : PackageBasedScanStorage {
/**
* A companion object that allow to configure the globally used storage backend.
*/
companion object {
/**
* A successful [Result] with an empty list of [ScanResult]s.
*/
val EMPTY_RESULT = Result.success<List<ScanResult>>(emptyList())
/**
* The scan result storage in use. Needs to be set via the corresponding [configure] function.
*/
var storage: ScanResultsStorage = NoStorage()
/**
* Configure the [ScanResultsStorage]. If [config] does not contain a storage configuration by default a
* [FileBasedStorage] using a [XZCompressedLocalFileStorage] as backend is configured.
*/
fun configure(config: ScannerConfiguration): ScanResultsStorage {
// Unfortunately, the smart cast does not work when moving this to a capturing "when" subject.
val configuredStorages = config.storages
storage = when {
configuredStorages.isNullOrEmpty() -> createDefaultStorage()
configuredStorages.size == 1 -> createStorage(configuredStorages.values.first())
else -> createCompositeStorage(config)
}
logger.info { "ScanResultStorage has been configured to ${storage.name}." }
return storage
}
/**
* Create a [FileBasedStorage] to be used as default if no other storage has been configured.
*/
private fun createDefaultStorage(): ScanResultsStorage {
val localFileStorage = XZCompressedLocalFileStorage(ortDataDirectory.resolve("$TOOL_NAME/results"))
return FileBasedStorage(localFileStorage)
}
/**
* Create a [CompositeStorage] that manages all storages defined in the given [config].
*/
private fun createCompositeStorage(config: ScannerConfiguration): ScanResultsStorage {
val storages = config.storages.orEmpty().mapValues { createStorage(it.value) }
fun resolve(name: String): ScanResultsStorage =
requireNotNull(storages[name]) {
"Could not resolve storage '$name'."
}
val readers = config.storageReaders.orEmpty().map { resolve(it) }
val writers = config.storageWriters.orEmpty().map { resolve(it) }
logger.info {
"Using composite storage with readers ${readers.joinToString { it.name }} and writers " +
"${writers.joinToString { it.name }}."
}
return CompositeStorage(readers, writers)
}
/**
* Create the concrete [ScanResultsStorage] implementation based on the [config] passed in.
*/
private fun createStorage(config: ScanStorageConfiguration): ScanResultsStorage =
when (config) {
is FileBasedStorageConfiguration -> createFileBasedStorage(config)
is PostgresStorageConfiguration -> createPostgresStorage(config)
is ClearlyDefinedStorageConfiguration -> createClearlyDefinedStorage(config)
is Sw360StorageConfiguration -> createSw360Storage(config)
}
/**
* Create a [FileBasedStorage] based on the [config] passed in.
*/
private fun createFileBasedStorage(config: FileBasedStorageConfiguration): ScanResultsStorage {
val backend = config.backend.createFileStorage()
when (backend) {
is HttpFileStorage -> logger.info { "Using file based storage with HTTP backend '${backend.url}'." }
is LocalFileStorage -> logger.info {
"Using file based storage with local directory '${backend.directory.invariantSeparatorsPath}'."
}
}
return FileBasedStorage(backend)
}
/**
* Create a [PostgresStorage] based on the [config] passed in.
*/
private fun createPostgresStorage(config: PostgresStorageConfiguration): ScanResultsStorage {
val dataSource = DatabaseUtils.createHikariDataSource(
config = config.connection,
applicationNameSuffix = TOOL_NAME,
// Use a value slightly higher than the number of transactions accessing the storage.
maxPoolSize = config.connection.parallelTransactions + 3
)
logger.info {
"Using Postgres storage with URL '${config.connection.url}' and schema '${config.connection.schema}'."
}
return PostgresStorage(dataSource, config.connection.parallelTransactions)
}
/**
* Create a [ClearlyDefinedStorage] based on the [config] passed in.
*/
private fun createClearlyDefinedStorage(config: ClearlyDefinedStorageConfiguration): ScanResultsStorage =
ClearlyDefinedStorage(config).also {
logger.info { "Using ClearlyDefined storage with URL '${config.serverUrl}'." }
}
/**
* Configure a [Sw360Storage] as the current storage backend.
*/
private fun createSw360Storage(config: Sw360StorageConfiguration): ScanResultsStorage =
Sw360Storage(config).also {
logger.info {
"Using SW360 storage with auth URL '${config.authUrl}' and REST URL '${config.restUrl}'."
}
}
}
/**
* The name to refer to this storage implementation.
*/
open val name: String = javaClass.simpleName
/**
* The access statistics for the scan result storage.
*/
val stats = AccessStatistics()
/**
* Return all [ScanResult]s contained in this [ScanResultsStorage] corresponding to the package denoted by the given
* [id] wrapped in a [Result].
*/
fun read(id: Identifier): Result<List<ScanResult>> {
val (result, duration) = measureTimedValue { readInternal(id) }
stats.numReads.incrementAndGet()
result.onSuccess { results ->
if (results.isNotEmpty()) {
stats.numHits.incrementAndGet()
}
logger.info {
"Read ${results.size} scan result(s) for '${id.toCoordinates()}' from ${javaClass.simpleName} in " +
"$duration."
}
}
return result
}
/**
* Return all [ScanResult]s contained in this [ScanResultsStorage] corresponding to the given [package][pkg] that
* are [compatible][ScannerCriteria.matches] with the provided [scannerCriteria] wrapped in a [Result]. Also,
* [Package.sourceArtifact], [Package.vcs], and [Package.vcsProcessed] are used to check if the scan result matches
* the expected source code location. That check is important to find the correct results when different revisions
* of a package using the same version name are used (e.g. multiple scans of a "1.0-SNAPSHOT" version during
* development).
*/
fun read(pkg: Package, scannerCriteria: ScannerCriteria): Result<List<ScanResult>> {
val (result, duration) = measureTimedValue { readInternal(pkg, scannerCriteria) }
stats.numReads.incrementAndGet()
result.onSuccess { results ->
if (results.isNotEmpty()) {
stats.numHits.incrementAndGet()
}
logger.info {
"Read ${results.size} scan result(s) for '${pkg.id.toCoordinates()}' from ${javaClass.simpleName} in " +
"$duration."
}
}
return result
}
/**
* Return all [ScanResult]s contained in this [ScanResultsStorage] corresponding to the given [packages] that
* are [compatible][ScannerCriteria.matches] with the provided [scannerCriteria] wrapped in a [Result]. Also,
* [Package.sourceArtifact], [Package.vcs], and [Package.vcsProcessed] are used to check if the scan result matches
* the expected source code location. That check is important to find the correct results when different revisions
* of a package using the same version name are used (e.g. multiple scans of a "1.0-SNAPSHOT" version during
* development).
*/
fun read(
packages: Collection<Package>,
scannerCriteria: ScannerCriteria
): Result<Map<Identifier, List<ScanResult>>> {
val (result, duration) = measureTimedValue { readInternal(packages, scannerCriteria) }
stats.numReads.addAndGet(packages.size)
result.onSuccess { results ->
stats.numHits.addAndGet(results.count { (_, results) -> results.isNotEmpty() })
logger.info {
"Read ${results.values.sumOf { it.size }} scan result(s) from ${javaClass.simpleName} in $duration."
}
}
return result
}
/**
* Add the given [scanResult] to the stored [ScanResult]s for the scanned [Package] with the provided [id].
* Depending on the storage implementation this might first read any existing [ScanResult]s and write the new
* [ScanResult]s to the storage again, implicitly deleting the original storage entry by overwriting it.
* Return a [Result] describing whether the operation was successful.
*/
fun add(id: Identifier, scanResult: ScanResult): Result<Unit> {
// Do not store scan results without provenance information, because they cannot be assigned to the revision of
// the package source code later.
if (scanResult.provenance is UnknownProvenance) {
val message =
"Not storing scan result for '${id.toCoordinates()}' because no provenance information is available."
logger.info { message }
return Result.failure(ScanStorageException(message))
}
val (result, duration) = measureTimedValue { addInternal(id, scanResult) }
logger.info { "Added scan result for '${id.toCoordinates()}' to ${javaClass.simpleName} in $duration." }
return result
}
/**
* Internal version of [read] that does not update the [access statistics][stats].
*/
protected abstract fun readInternal(id: Identifier): Result<List<ScanResult>>
/**
* Internal version of [read] that does not update the [access statistics][stats]. Implementations may want to
* override this function if they can filter for the wanted [scannerCriteria] in a more efficient way.
*/
protected open fun readInternal(pkg: Package, scannerCriteria: ScannerCriteria): Result<List<ScanResult>> =
readInternal(pkg.id).map { results ->
if (results.isEmpty()) {
results
} else {
val scanResults = results.toMutableList()
// Only keep scan results whose provenance information matches the package information.
scanResults.retainAll { it.provenance.matches(pkg) }
if (scanResults.isEmpty()) {
logger.debug {
"No stored scan results found for $pkg. The following entries with non-matching provenance " +
"have been ignored: ${scanResults.map { it.provenance }}"
}
} else {
// Only keep scan results from compatible scanners.
scanResults.retainAll { scannerCriteria.matches(it.scanner) }
if (scanResults.isEmpty()) {
logger.debug {
"No stored scan results found for $scannerCriteria. The following entries with " +
"incompatible scanners have been ignored: ${scanResults.map { it.scanner }}"
}
}
}
scanResults
}
}
/**
* Internal version of [read] that does not update the [access statistics][stats]. The default implementation uses
* [Dispatchers.IO] to run requests for individual packages in parallel. Implementations may want to override this
* function if they can filter for the wanted [scannerCriteria] or fetch results for multiple packages in a more
* efficient way.
*/
protected open fun readInternal(
packages: Collection<Package>,
scannerCriteria: ScannerCriteria
): Result<Map<Identifier, List<ScanResult>>> {
val results = runBlocking(Dispatchers.IO) {
packages.map { async { it.id to readInternal(it, scannerCriteria) } }.awaitAll()
}.associate { it }
val successfulResults = results.mapNotNull { (id, scanResults) ->
scanResults.getOrNull()?.let { id to it }
}.toMap()
return if (successfulResults.isEmpty()) {
Result.failure(ScanStorageException("Could not read any scan results from ${javaClass.simpleName}."))
} else {
Result.success(successfulResults)
}
}
/**
* Internal version of [add] that skips common sanity checks.
*/
protected abstract fun addInternal(id: Identifier, scanResult: ScanResult): Result<Unit>
override fun read(pkg: Package, nestedProvenance: NestedProvenance): List<NestedProvenanceScanResult> =
read(pkg.id).toNestedProvenanceScanResult(nestedProvenance)
override fun read(
pkg: Package,
nestedProvenance: NestedProvenance,
scannerCriteria: ScannerCriteria
): List<NestedProvenanceScanResult> =
read(pkg, scannerCriteria).toNestedProvenanceScanResult(nestedProvenance)
private fun Result<List<ScanResult>>.toNestedProvenanceScanResult(nestedProvenance: NestedProvenance) =
map { scanResults ->
scanResults.filter { it.provenance == nestedProvenance.root }
.map { it.toNestedProvenanceScanResult(nestedProvenance) }
}.getOrThrow()
override fun write(pkg: Package, nestedProvenanceScanResult: NestedProvenanceScanResult) {
nestedProvenanceScanResult.merge().forEach { scanResult ->
add(pkg.id, scanResult).getOrThrow()
}
}
}