-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathSpdxResolvedDocument.kt
More file actions
428 lines (376 loc) · 17.2 KB
/
SpdxResolvedDocument.kt
File metadata and controls
428 lines (376 loc) · 17.2 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (C) 2021 The ORT Project Copyright Holders <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>
*
* 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
*/
@file:Suppress("TooManyFunctions")
package org.ossreviewtoolkit.plugins.packagemanagers.spdxdocumentfile.utils
import java.io.File
import java.net.URI
import org.apache.logging.log4j.kotlin.logger
import org.ossreviewtoolkit.model.Hash
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.model.createAndLogIssue
import org.ossreviewtoolkit.plugins.packagemanagers.spdxdocumentfile.SpdxDocumentFile
import org.ossreviewtoolkit.plugins.packagemanagers.spdxdocumentfile.SpdxDocumentFileFactory
import org.ossreviewtoolkit.utils.authentication.requestPasswordAuthentication
import org.ossreviewtoolkit.utils.common.collectMessages
import org.ossreviewtoolkit.utils.common.safeDeleteRecursively
import org.ossreviewtoolkit.utils.ort.OkHttpClientHelper
import org.ossreviewtoolkit.utils.ort.addBasicAuthorization
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
import org.ossreviewtoolkit.utils.ort.downloadFile
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxDocument
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxExternalDocumentReference
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxPackage
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxRelationship
/**
* A data class storing information about a root SPDX document and all the documents referenced by it.
*
* This class is used by [SpdxDocumentFile] to get a combined view on all packages and their relations defined in a set
* of SPDX files.
*/
internal data class SpdxResolvedDocument(
/**
* The root document. This is the starting point, from which all external references have been traversed.
*/
val rootDocument: ResolvedSpdxDocument,
/**
* Holds a map with all [ResolvedSpdxDocument]s that are referenced directly or indirectly from the root document,
* using the external reference objects as keys.
*/
val referencedDocuments: Map<SpdxExternalDocumentReference, ResolvedSpdxDocument>,
/**
* Holds a list with the accumulated [SpdxRelationship]s from all documents referenced directly or indirectly from
* the root document.
*/
val relationships: List<SpdxRelationship>,
/**
* A map allowing direct access to the packages declared in one of the contained documents. For the packages of the
* root document, the key is the package's identifiers. For other packages, the identifier needs to be prefixed
* with the document identifier of the reference.
*/
private val packagesById: Map<String, SpdxPackage>,
/**
* A map storing issues that were encountered when resolving external document references. These issues are also
* assigned to [SpdxPackage]s defined in the corresponding external documents.
*/
private val issuesByReferenceId: Map<String, Issue>
) {
companion object {
fun load(cache: SpdxDocumentCache, rootDocumentFile: File): SpdxResolvedDocument {
val rootDocument = cache.load(rootDocumentFile).getOrThrow()
val references = mutableMapOf<SpdxExternalDocumentReference, ResolvedSpdxDocument>()
val issues = mutableMapOf<String, Issue>()
resolveAllReferences(
cache,
rootDocument,
rootDocumentFile.toURI(),
references,
issues,
mutableSetOf()
)
val resolvedRootDocument = ResolvedSpdxDocument(rootDocument, rootDocumentFile.toURI())
// Note: The identifiers from packages defined in external documents are qualified with the relation name,
// while package identifiers from the root document are not qualified. Thus, there can be no clash.
val packages = collectPackages(references) + rootDocument.getPackages()
val relations = collectAndQualifyRelations(references) + rootDocument.relationships
return SpdxResolvedDocument(resolvedRootDocument, references, relations, packages, issues)
}
}
/**
* Get the [SpdxPackage] for the given [identifier] by resolving against packages or external document references
* contained in this document. If the package cannot be resolved, add an issue to [issues].
*/
fun getSpdxPackageForId(identifier: String, issues: MutableList<Issue>): SpdxPackage? {
val pkg = packagesById[identifier]
val issue = issuesByReferenceId[identifier.substringBefore(':', "")]
if (pkg != null) {
issue?.also { issues += it }
} else {
issues += issue ?: createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
message = "'$identifier' could neither be resolved to a 'package' nor to an 'externalDocumentRef'."
)
}
return pkg
}
/**
* Retrieve the issues from [issuesByReferenceId] that are not associated with [any package][packagesById]. These
* issues can be related to general issues within the SPDX document.
*/
fun getIssuesWithoutSpdxPackage() =
issuesByReferenceId.mapNotNull { (id, issue) ->
if (packagesById[id] == null) issue else null
}
/**
* Return the local definition file in which the package with the given [identifier] is declared. If the package
* cannot be resolved or if it has not been declared in a local file, return *null*.
*/
fun getDefinitionFile(identifier: String): File? {
if (identifier !in packagesById) return null
val reference = identifier.substringBefore(':', "")
.takeUnless { it.isEmpty() }
?.let { refId -> referencedDocuments.entries.find { it.key.externalDocumentId == refId } }
return reference?.value?.definitionFile() ?: rootDocument.definitionFile()
}
}
/**
* A data class storing information about an SPDX document and the URL from which it was loaded. The latter is
* required to generate the VCS information in the analyzer result.
*/
internal data class ResolvedSpdxDocument(
/** The actual SPDX document. */
val document: SpdxDocument,
/** The URL from which this document was loaded. */
val url: URI
) {
/**
* Return the local definition file from which this document was loaded if there is one. Return *null* if this
* document was loaded from the internet.
*/
fun definitionFile(): File? = url.toDefinitionFile()
}
/**
* Resolve all external references to SPDX documents contained in [document], and recursively in all referenced
* documents. Use [cache] to load documents. Resolve relative URLs against [baseUri]. Store all encountered references
* and the documents they point to in [references]. Store issues encountered when resolving references in [issues]. Use
* [knownUris] to detect cycles.
*/
private fun resolveAllReferences(
cache: SpdxDocumentCache,
document: SpdxDocument,
baseUri: URI,
references: MutableMap<SpdxExternalDocumentReference, ResolvedSpdxDocument>,
issues: MutableMap<String, Issue>,
knownUris: MutableSet<URI>
) {
document.resolveReferences(cache, baseUri).forEach { (ref, resolvedDoc) ->
resolvedDoc.document?.let { resolvedDocument ->
references += ref to ResolvedSpdxDocument(resolvedDocument, resolvedDoc.uri)
if (knownUris.add(resolvedDoc.uri)) {
resolveAllReferences(
cache,
resolvedDocument,
resolvedDoc.uri,
references,
issues,
knownUris
)
}
}
resolvedDoc.issue?.let { issues += ref.externalDocumentId to it }
}
}
/**
* Return a map with all [SpdxPackage]s found in one of the given [references] using qualified identifiers as keys.
*/
private fun collectPackages(
references: MutableMap<SpdxExternalDocumentReference, ResolvedSpdxDocument>
): Map<String, SpdxPackage> {
val allPackages = mutableMapOf<String, SpdxPackage>()
references.forEach { (reference, resolvedDocument) ->
allPackages += resolvedDocument.document.getPackages("${reference.externalDocumentId}:")
}
return allPackages
}
/**
* Return a list with all [SpdxRelationship]s found in the given [references]. Qualify the identifiers used in these
* relationships, so that they are compatible with the keys used to access the aggregated packages.
*/
private fun collectAndQualifyRelations(
references: MutableMap<SpdxExternalDocumentReference, ResolvedSpdxDocument>
): List<SpdxRelationship> =
references.flatMap { (reference, resolvedSpdxDocument) ->
resolvedSpdxDocument.document.relationships.map { it.qualify(reference) }
}
/**
* A data class to hold the result of an operation to resolve an [SpdxDocument] from an external reference. Resolving
* of the document may fail, then the document is *null*, and a corresponding [Issue] is present.
*/
internal data class ResolutionResult(
/**
* The document the reference points to, if it could be resolved successfully.
*/
val document: SpdxDocument?,
/** The URI pointing to the document. */
val uri: URI,
/**
* An issue that occurred while resolving the document. If the document could not be resolved, this gives details
* about the underlying error. It could also be a warning.
*/
val issue: Issue?
)
/**
* Check whether this URI points to a local definition file.
*/
private fun URI.isLocalDefinitionFile(): Boolean = scheme.equals("file", ignoreCase = true) || !isAbsolute
/**
* Convert this URI to a local definition file if possible. Otherwise, return *null*.
*/
private fun URI.toDefinitionFile(): File? =
takeIf { isLocalDefinitionFile() }?.let { File(it.path).absoluteFile.normalize() }?.takeIf { it.isFile }
/**
* Return the [SpdxDocument] this [SpdxExternalDocumentReference]'s [SpdxDocument] refers to. Use [cache] to parse
* the document, and [baseUri] to resolve relative references.
*/
internal fun SpdxExternalDocumentReference.resolve(cache: SpdxDocumentCache, baseUri: URI): ResolutionResult {
val uri = runCatching {
val resolvedUri = baseUri.resolve(spdxDocument)
resolvedUri.takeUnless { baseUri.query != null } ?: URI("$resolvedUri?${baseUri.query}")
}.getOrElse {
return ResolutionResult(
document = null,
uri = baseUri,
issue = createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
message = "The SPDX document at '$spdxDocument' cannot be resolved as a URI (referred from $baseUri " +
"as part of '$externalDocumentId')."
)
)
}
return if (uri.isLocalDefinitionFile()) {
resolveFromFile(uri, cache, baseUri)
} else {
resolveFromDownload(uri, cache, baseUri)
}
}
/**
* Resolve this [SpdxExternalDocumentReference] from [uri] if it points to a file on the local file system. Use
* [cache] to load the file. In case of a failure, create an [Issue] whose message includes [baseUri].
*/
private fun SpdxExternalDocumentReference.resolveFromFile(
uri: URI,
cache: SpdxDocumentCache,
baseUri: URI
): ResolutionResult {
val file = uri.toDefinitionFile() ?: return ResolutionResult(
document = null,
uri = baseUri,
issue = createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
message = "The file pointed to by '$uri' in reference '$externalDocumentId' does not exist."
)
)
val document = cache.load(file).getOrElse {
return ResolutionResult(
document = null,
uri = uri,
issue = createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
message = "Failed to parse the SPDX document pointed to by '$uri' in reference " +
"'$externalDocumentId': ${it.message}"
)
)
}
return ResolutionResult(document, uri, verifyChecksum(file, baseUri))
}
/**
* Resolve this [SpdxExternalDocumentReference] from [uri] if it requires a download from a server. Use [cache] to
* parse the document after it has been downloaded. In case of a failure, create an [Issue] whose message includes
* [baseUri].
*/
private fun SpdxExternalDocumentReference.resolveFromDownload(
uri: URI,
cache: SpdxDocumentCache,
baseUri: URI
): ResolutionResult {
logger.info {
"Downloading SPDX document from $uri (referred from $baseUri as part of '$externalDocumentId')."
}
val tempDir = createOrtTempDir()
return try {
val client = OkHttpClientHelper.buildClient {
// Use the authenticator also to request preemptive authentication.
val auth = requestPasswordAuthentication(uri)
if (auth != null) {
addBasicAuthorization(auth.userName, String(auth.password))
}
}
val file = client.downloadFile(uri.toString(), tempDir).getOrElse {
return ResolutionResult(
document = null,
uri = uri,
issue = createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
message = "Failed to download SPDX document from $uri (referred from $baseUri as part of " +
"'$externalDocumentId'): ${it.collectMessages()}"
)
)
}
val document = cache.load(file).getOrElse {
return ResolutionResult(
document = null,
uri = uri,
issue = createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
message = "Failed to parse SPDX document from $uri (referred from $baseUri as part of " +
"'$externalDocumentId'): ${it.message}"
)
)
}
ResolutionResult(document, uri, verifyChecksum(file, baseUri))
} finally {
tempDir.safeDeleteRecursively()
}
}
/**
* Verify that the resolved or downloaded [file] this [SpdxExternalDocumentReference] refers to matches the expected
* checksum. If not, return an [Issue] based on the document [uri].
*/
private fun SpdxExternalDocumentReference.verifyChecksum(file: File, uri: URI): Issue? {
val hash = Hash(checksum.checksumValue, checksum.algorithm.name)
if (hash.verify(file)) return null
return SpdxResolvedDocument.createAndLogIssue(
source = SpdxDocumentFileFactory.descriptor.displayName,
severity = Severity.WARNING,
message = "The SPDX document at '$spdxDocument' does not match the expected $hash (referred from $uri as " +
"part of '$externalDocumentId')."
)
}
/**
* Load all documents referenced by external references in this [SpdxDocument] using [cache]. Resolve relative paths
* based on [documentUri].
*/
private fun SpdxDocument.resolveReferences(
cache: SpdxDocumentCache,
documentUri: URI
): Map<SpdxExternalDocumentReference, ResolutionResult> =
externalDocumentRefs.associateWith { it.resolve(cache, documentUri) }
/**
* Return a map with all the SPDX packages contained in this document. Keys are the identifiers of the packages,
* optionally with the given [idPrefix]. The prefix is used to assign packages to external references.
*/
private fun SpdxDocument.getPackages(idPrefix: String? = null): Map<String, SpdxPackage> =
packages.associateBy { pkg -> idPrefix?.let { "$it${pkg.spdxId}" } ?: pkg.spdxId }
/**
* Transform the identifiers of the packages referenced by this relation to qualified identifiers if necessary. When
* combining the relationships from multiple SPDX documents, packages must always be referenced with qualified
* identifiers (including the ID of the [reference] that points to the document), so that they can be resolved
* correctly.
*/
private fun SpdxRelationship.qualify(reference: SpdxExternalDocumentReference): SpdxRelationship {
val qualifiedElementId = ensureQualified(spdxElementId, reference)
val qualifiedRelatedId = ensureQualified(relatedSpdxElement, reference)
return takeIf { spdxElementId == qualifiedElementId && relatedSpdxElement == qualifiedRelatedId }
?: copy(spdxElementId = qualifiedElementId, relatedSpdxElement = qualifiedRelatedId)
}
/**
* Transform the given [spdxId] to a qualified identifier based on [reference] unless it is already qualified.
*/
private fun ensureQualified(spdxId: String, reference: SpdxExternalDocumentReference): String =
spdxId.takeIf { ':' in it } ?: "${reference.externalDocumentId}:$spdxId"