forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaMavenProvider.java
More file actions
474 lines (439 loc) · 17.8 KB
/
Copy pathJavaMavenProvider.java
File metadata and controls
474 lines (439 loc) · 17.8 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* Copyright © 2023 Red Hat, Inc.
*
* 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.
*/
package com.redhat.exhort.providers;
import static com.redhat.exhort.impl.ExhortApi.debugLoggingIsNeeded;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.redhat.exhort.Api;
import com.redhat.exhort.Provider;
import com.redhat.exhort.logging.LoggersFactory;
import com.redhat.exhort.sbom.Sbom;
import com.redhat.exhort.sbom.SbomFactory;
import com.redhat.exhort.tools.Ecosystem.Type;
import com.redhat.exhort.tools.Operations;
import com.redhat.exhort.utils.Environment;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
/**
* Concrete implementation of the {@link Provider} used for converting dependency trees for Java
* Maven projects (pom.xml) into a content Dot Graphs for Stack analysis or Json for Component
* analysis.
*/
public final class JavaMavenProvider extends BaseJavaProvider {
private static final String PROP_JAVA_HOME = "JAVA_HOME";
private static final Logger log = LoggersFactory.getLogger(JavaMavenProvider.class.getName());
private final String mvnExecutable;
private static final String MVN = Operations.isWindows() ? "mvn.cmd" : "mvn";
private static final String ARG_VERSION = "-v";
public JavaMavenProvider(Path manifest) {
super(Type.MAVEN, manifest);
// check for custom mvn executable mvn or mvn wrapper
this.mvnExecutable = selectMvnRuntime(manifest);
}
@Override
public Content provideStack() throws IOException {
var mvnCleanCmd =
new String[] {mvnExecutable, "clean", "-f", manifest.toString(), "--batch-mode", "-q"};
var mvnEnvs = getMvnExecEnvs();
// execute the clean command
Operations.runProcess(manifest.getParent(), mvnCleanCmd, mvnEnvs);
// create a temp file for storing the dependency tree in
var tmpFile = Files.createTempFile("exhort_dot_graph_", null);
// the tree command will build the project and create the dependency tree in the temp file
var mvnTreeCmd =
new ArrayList<String>() {
{
add(mvnExecutable);
add("org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree");
add("-Dverbose");
add("-DoutputType=text");
add(String.format("-DoutputFile=%s", tmpFile.toString()));
add("-f");
add(manifest.toString());
add("--batch-mode");
add("-q");
}
};
// if we have dependencies marked as ignored, exclude them from the tree command
var ignored =
getDependencies(manifest).stream()
.filter(d -> d.ignored)
.map(DependencyAggregator::toPurl)
.map(PackageURL::getCoordinates)
.collect(Collectors.toList());
// execute the tree command
Operations.runProcess(manifest.getParent(), mvnTreeCmd.toArray(String[]::new), mvnEnvs);
if (debugLoggingIsNeeded()) {
String stackAnalysisDependencyTree = Files.readString(tmpFile);
log.info(
String.format(
"Package Manager Maven Stack Analysis Dependency Tree Output: %s %s",
System.lineSeparator(), stackAnalysisDependencyTree));
}
var sbom = buildSbomFromTextFormat(tmpFile);
// build and return content for constructing request to the backend
return new Content(
sbom.filterIgnoredDeps(ignored).getAsJsonString().getBytes(), Api.CYCLONEDX_MEDIA_TYPE);
}
private Sbom buildSbomFromTextFormat(Path textFormatFile) throws IOException {
var sbom = SbomFactory.newInstance(Sbom.BelongingCondition.PURL, "sensitive");
List<String> lines = Files.readAllLines(textFormatFile);
var root = lines.get(0);
var rootPurl = parseDep(root);
sbom.addRoot(rootPurl);
lines.remove(0);
String[] array = new String[lines.size()];
lines.toArray(array);
// createSbomIteratively(lines,sbom);
parseDependencyTree(root, 0, array, sbom, null);
return sbom;
}
@Override
public Content provideComponent() throws IOException {
// build effective pom command
return generateSbomFromEffectivePom();
}
private Content generateSbomFromEffectivePom() throws IOException {
var tmpEffPom = Files.createTempFile("exhort_eff_pom_", ".xml");
var mvnEffPomCmd =
new String[] {
mvnExecutable,
"clean",
"help:effective-pom",
String.format("-Doutput=%s", tmpEffPom.toString()),
"-f",
manifest.toString(),
"--batch-mode",
"-q"
};
// execute the effective pom command
Operations.runProcess(manifest.getParent(), mvnEffPomCmd, getMvnExecEnvs());
if (debugLoggingIsNeeded()) {
String CaEffectivePoM = Files.readString(tmpEffPom);
log.info(
String.format(
"Package Manager Maven Component Analysis Effective POM Output : %s %s",
System.lineSeparator(), CaEffectivePoM));
}
// if we have dependencies marked as ignored grab ignored dependencies from the original pom
// the effective-pom goal doesn't carry comments
List<DependencyAggregator> dependencies = getDependencies(manifest);
var ignored =
dependencies.stream()
.filter(d -> d.ignored)
.map(DependencyAggregator::toPurl)
.collect(Collectors.toSet());
var testsDeps =
dependencies.stream()
.filter(DependencyAggregator::isTestDependency)
.collect(Collectors.toSet());
var deps = getDependencies(tmpEffPom);
var sbom = SbomFactory.newInstance().addRoot(getRoot(tmpEffPom));
deps.stream()
.filter(dep -> !testsDeps.contains(dep))
.map(DependencyAggregator::toPurl)
.filter(dep -> ignored.stream().noneMatch(artifact -> artifact.isCoordinatesEquals(dep)))
.forEach(d -> sbom.addDependency(sbom.getRoot(), d, null));
// build and return content for constructing request to the backend
return new Content(sbom.getAsJsonString().getBytes(), Api.CYCLONEDX_MEDIA_TYPE);
}
private PackageURL getRoot(final Path manifestPath) throws IOException {
XMLStreamReader reader = null;
try {
reader =
XMLInputFactory.newInstance().createXMLStreamReader(Files.newInputStream(manifestPath));
DependencyAggregator dependencyAggregator = null;
boolean isRoot = false;
while (reader.hasNext()) {
reader.next(); // get the next event
if (reader.isStartElement() && "project".equals(reader.getLocalName())) {
isRoot = true;
dependencyAggregator = new DependencyAggregator();
continue;
}
if (!Objects.isNull(dependencyAggregator)) {
if (reader.isStartElement()) {
switch (reader.getLocalName()) {
case "groupId": // starting "groupId" tag, get next event and set to aggregator
reader.next();
dependencyAggregator.groupId = reader.getText();
break;
case "artifactId": // starting "artifactId" tag, get next event and set to aggregator
reader.next();
dependencyAggregator.artifactId = reader.getText();
break;
case "version": // starting "version" tag, get next event and set to aggregator
reader.next();
dependencyAggregator.version = reader.getText();
break;
}
}
if (dependencyAggregator.isValid()) {
return dependencyAggregator.toPurl();
}
}
}
} catch (XMLStreamException exc) {
throw new IOException(exc);
} finally {
if (!Objects.isNull(reader)) {
try {
reader.close(); // close stream if open
} catch (XMLStreamException e) {
//
}
}
}
throw new IllegalStateException("Unable to retrieve Root dependency from effective pom");
}
private List<DependencyAggregator> getDependencies(final Path manifestPath) throws IOException {
List<DependencyAggregator> deps = new ArrayList<>();
XMLStreamReader reader = null;
try {
// get a xml stream reader for the manifest file
reader =
XMLInputFactory.newInstance().createXMLStreamReader(Files.newInputStream(manifestPath));
// the following dependencyIgnore object is used to aggregate dependency data over iterations
// when a "dependency" tag starts, it will be initiated,
// when a "dependency" tag ends, it will be parsed, act upon, and reset
DependencyAggregator dependencyAggregator = null;
boolean insideDependencyManagement = false;
boolean insideExclusions = false;
boolean insidePlugins = false;
while (reader.hasNext()) {
reader.next(); // get the next event
if (reader.isStartElement() && "dependencyManagement".equals(reader.getLocalName())) {
insideDependencyManagement = true;
continue;
}
if (reader.isEndElement() && "dependencyManagement".equals(reader.getLocalName())) {
insideDependencyManagement = false;
continue;
}
if (reader.isStartElement() && "plugins".equals(reader.getLocalName())) {
insidePlugins = true;
continue;
}
if (reader.isEndElement() && "plugins".equals(reader.getLocalName())) {
insidePlugins = false;
continue;
}
if (reader.isStartElement() && "exclusions".equals(reader.getLocalName())) {
insideExclusions = true;
continue;
}
if (reader.isEndElement() && "exclusions".equals(reader.getLocalName())) {
insideExclusions = false;
continue;
}
if (reader.isStartElement() && "dependency".equals(reader.getLocalName())) {
// starting "dependency" tag, initiate aggregator only if not inside dependencyManagement
// or plugins
if (!insideDependencyManagement && !insidePlugins) {
dependencyAggregator = new DependencyAggregator();
}
continue;
}
// if dependency aggregator haven't been initiated,
// we're currently not iterating over a "dependency" tag - no need for further parsing
if (!Objects.isNull(dependencyAggregator)) {
// if we hit an ignore comment, mark aggregator to be ignored
if (reader.getEventType() == XMLStreamConstants.COMMENT
&& "exhortignore".equals(reader.getText().strip())) {
dependencyAggregator.ignored = true;
continue;
}
if (reader.isStartElement() && !insideExclusions) {
// NOTE if we want to include "scope" tags in ignore,
// add a case here and a property in DependencyIgnore
// Only process these elements if we're not inside exclusions
switch (reader.getLocalName()) {
case "groupId": // starting "groupId" tag, get next event and set to aggregator
reader.next();
dependencyAggregator.groupId = reader.getText();
break;
case "artifactId": // starting "artifactId" tag, get next event and set to aggregator
reader.next();
dependencyAggregator.artifactId = reader.getText();
break;
case "scope":
reader.next();
dependencyAggregator.scope =
reader.getText() != null ? reader.getText().trim() : "*";
break;
case "version": // starting "version" tag, get next event and set to aggregator
reader.next();
dependencyAggregator.version = reader.getText();
break;
}
}
if (reader.isEndElement() && "dependency".equals(reader.getLocalName())) {
// add object to list and reset dependency aggregator only if not inside
// dependencyManagement or plugins
if (!insideDependencyManagement && !insidePlugins && dependencyAggregator != null) {
deps.add(dependencyAggregator);
}
dependencyAggregator = null;
}
}
}
} catch (XMLStreamException exc) {
throw new IOException(exc);
} finally {
if (!Objects.isNull(reader)) {
try {
reader.close(); // close stream if open
} catch (XMLStreamException e) {
//
}
}
}
return deps;
}
Map<String, String> getMvnExecEnvs() {
var javaHome = Environment.get(PROP_JAVA_HOME);
if (javaHome != null && !javaHome.isBlank()) {
return Collections.singletonMap(PROP_JAVA_HOME, javaHome);
}
return null;
}
// NOTE if we want to include "scope" tags in ignore,
// add property here and a case in the start-element-switch in the getIgnored method
/** Aggregator class for aggregating Dependency data over stream iterations, * */
private static final class DependencyAggregator {
private String scope = "*";
private String groupId;
private String artifactId;
private String version;
boolean ignored = false;
/**
* Get the string representation of the dependency to use as excludes
*
* @return an exclude string for the dependency:tree plugin, i.e. group-id:artifact-id:*:version
*/
@Override
public String toString() {
// NOTE if you add scope, don't forget to replace the * with its value
return String.format("%s:%s:%s:%s", groupId, artifactId, scope, version);
}
public boolean isValid() {
return Objects.nonNull(groupId) && Objects.nonNull(artifactId) && Objects.nonNull(version);
}
public boolean isTestDependency() {
return scope.trim().equals("test");
}
public PackageURL toPurl() {
try {
return new PackageURL(
Type.MAVEN.getType(),
groupId,
artifactId,
version,
this.scope.equals("*") ? null : new TreeMap<>(Map.of("scope", this.scope)),
null);
} catch (MalformedPackageURLException e) {
throw new IllegalArgumentException("Unable to parse PackageURL", e);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DependencyAggregator)) return false;
var that = (DependencyAggregator) o;
// NOTE we do not compare the ignored field
// This is required for comparing pom.xml with effective_pom.xml as the latter doesn't
// contain comments indicating ignore
return Objects.equals(this.groupId, that.groupId)
&& Objects.equals(this.artifactId, that.artifactId)
&& Objects.equals(this.version, that.version);
}
@Override
public int hashCode() {
return Objects.hash(groupId, artifactId, version);
}
}
private String selectMvnRuntime(final Path manifestPath) {
boolean preferWrapper = Operations.getWrapperPreference(MVN);
if (preferWrapper) {
String wrapperName = Operations.isWindows() ? "mvnw.cmd" : "mvnw";
String mvnw = traverseForMvnw(wrapperName, manifestPath.toString());
if (mvnw != null) {
try {
// verify maven wrapper is accessible
Operations.runProcess(manifest.getParent(), mvnw, ARG_VERSION);
if (debugLoggingIsNeeded()) {
log.info(String.format("using maven wrapper from : %s", mvnw));
}
return mvnw;
} catch (Exception e) {
log.warning(
"Failed to check for mvnw due to: " + e.getMessage() + " Fall back to use mvn");
}
}
}
// If maven wrapper is not requested or not accessible, fall back to use mvn
String mvn = Operations.getExecutable(MVN, ARG_VERSION);
if (debugLoggingIsNeeded()) {
log.info(String.format("using mvn executable from : %s", mvn));
}
return mvn;
}
private String traverseForMvnw(String wrapperName, String startingManifest) {
return traverseForMvnw(wrapperName, startingManifest, null);
}
public static String traverseForMvnw(
String wrapperName, String startingManifest, String repoRoot) {
String normalizedManifest = normalizePath(startingManifest);
Path path = Paths.get(normalizedManifest);
if (repoRoot == null) {
repoRoot =
Operations.getGitRootDir(path.getParent().toString())
.orElse(path.toAbsolutePath().getRoot().toString());
}
Path wrapperPath = path.getParent().resolve(wrapperName).toAbsolutePath();
if (Files.isExecutable(wrapperPath)) {
return wrapperPath.toString();
} else {
String currentDir = path.getParent().toAbsolutePath().toString();
if (currentDir.equals(repoRoot)) {
return null;
}
return traverseForMvnw(wrapperName, path.getParent().toString(), repoRoot);
}
}
public static String normalizePath(String thePath) {
Path normalized = Paths.get(thePath).toAbsolutePath().normalize();
String result = normalized.toString();
if (Operations.isWindows()) {
result = result.toLowerCase();
}
return result;
}
}