forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradleProvider.java
More file actions
584 lines (514 loc) · 21.9 KB
/
Copy pathGradleProvider.java
File metadata and controls
584 lines (514 loc) · 21.9 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* Copyright 2023-2025 Trustify Dependency Analytics Authors
*
* 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 io.github.guacsec.trustifyda.providers;
import static io.github.guacsec.trustifyda.impl.ExhortApi.debugLoggingIsNeeded;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import io.github.guacsec.trustifyda.Api;
import io.github.guacsec.trustifyda.Provider;
import io.github.guacsec.trustifyda.logging.LoggersFactory;
import io.github.guacsec.trustifyda.sbom.Sbom;
import io.github.guacsec.trustifyda.sbom.SbomFactory;
import io.github.guacsec.trustifyda.tools.Ecosystem.Type;
import io.github.guacsec.trustifyda.tools.Operations;
import io.github.guacsec.trustifyda.utils.IgnorePatternDetector;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.tomlj.Toml;
import org.tomlj.TomlParseResult;
import org.tomlj.TomlTable;
/**
* Concrete implementation of the {@link Provider} used for converting dependency trees for Gradle
* projects (gradle.build / gradle.build.kts) into a content Dot Graphs for Stack analysis or Json
* for Component analysis.
*/
public final class GradleProvider extends BaseJavaProvider {
public static String RUNTIME_CLASSPATH = "runtimeClasspath";
public static String COMPILE_CLASSPATH = "compileClasspath";
public static String REQUIRED = "required";
public static String OPTIONAL = "optional";
private static final Logger log = LoggersFactory.getLogger(GradleProvider.class.getName());
private final String gradleExecutable = Operations.getExecutable("gradle", "--version");
public GradleProvider(Path manifest) {
super(Type.GRADLE, manifest);
}
@Override
public Content provideStack() throws IOException {
Path tempFile = getDependencies(manifest);
if (debugLoggingIsNeeded()) {
String stackAnalysisDependencyTree = Files.readString(tempFile);
log.info(
String.format(
"Package Manager Gradle Stack Analysis Dependency Tree Output: %s %s",
System.lineSeparator(), stackAnalysisDependencyTree));
}
Map<String, String> propertiesMap = extractProperties(manifest);
var sbom = buildSbomFromTextFormat(tempFile, propertiesMap, AnalysisType.STACK);
var ignored = getIgnoredDeps(manifest);
return new Content(
sbom.filterIgnoredDeps(ignored).getAsJsonString().getBytes(), Api.CYCLONEDX_MEDIA_TYPE);
}
private List<String> getIgnoredDeps(Path manifestPath) throws IOException {
List<String> buildGradleLines = Files.readAllLines(manifestPath);
List<String> ignored = new ArrayList<>();
var ignoredLines =
buildGradleLines.stream()
.filter(this::isIgnoredLine)
.map(this::extractPackageName)
.collect(Collectors.toList());
// Process each ignored dependency
for (String dependency : ignoredLines) {
String ignoredDepInfo;
if (depHasLibsNotation(dependency)) {
ignoredDepInfo = getDepFromNotation(dependency, manifestPath);
} else {
ignoredDepInfo = getDepInfo(dependency);
}
if (ignoredDepInfo != null) {
ignored.add(ignoredDepInfo);
}
}
return ignored;
}
private String getDepInfo(String dependencyLine) {
// Check if the line contains "group:", "name:", and "version:"
if (dependencyLine.contains("group:")
&& dependencyLine.contains("name:")
&& dependencyLine.contains("version:")) {
Pattern pattern = Pattern.compile("(group|name|version):\\s*['\"](.*?)['\"]");
Matcher matcher = pattern.matcher(dependencyLine);
String groupId = null, artifactId = null, version = null;
while (matcher.find()) {
String key = matcher.group(1);
String value = matcher.group(2);
switch (key) {
case "group":
groupId = value;
break;
case "name":
artifactId = value;
break;
case "version":
version = value;
break;
}
}
if (groupId != null && artifactId != null && version != null) {
PackageURL ignoredPackageUrl = toPurl(groupId, artifactId, version);
return ignoredPackageUrl.getCoordinates();
}
} else {
// Regular expression pattern to capture content inside single or double quotes
Pattern pattern = Pattern.compile("['\"](.*?)['\"]");
Matcher matcher = pattern.matcher(dependencyLine);
// Check if the matcher finds a match
if (matcher.find()) {
// Get the matched string inside single or double quotes
String dependency = matcher.group(1);
String[] dependencyParts = dependency.split(":");
if (dependencyParts.length == 3) {
// Extract groupId, artifactId, and version
String groupId = dependencyParts[0];
String artifactId = dependencyParts[1];
String version = dependencyParts[2];
PackageURL ignoredPackageUrl = toPurl(groupId, artifactId, version);
return ignoredPackageUrl.getCoordinates();
}
}
}
return null;
}
private String getDepFromNotation(String dependency, Path manifestPath) throws IOException {
// Extract everything after "libs."
String alias = dependency.substring(dependency.indexOf("libs.") + "libs.".length()).trim();
alias = alias.replace(".", "-").replace(")", "");
// Read and parse the TOML file
TomlParseResult toml = Toml.parse(getLibsVersionsTomlPath(manifestPath));
TomlTable librariesTable = toml.getTable("libraries");
TomlTable dependencyTable = librariesTable.getTable(alias);
if (dependencyTable != null) {
String groupId = dependencyTable.getString("module").split(":")[0];
String artifactId = dependencyTable.getString("module").split(":")[1];
String version =
toml.getTable("versions").getString(dependencyTable.getString("version.ref"));
PackageURL ignoredPackageUrl = toPurl(groupId, artifactId, version);
return ignoredPackageUrl.getCoordinates();
}
return null;
}
private Path getLibsVersionsTomlPath(Path manifestPath) {
return manifestPath.getParent().resolve("gradle/libs.versions.toml");
}
public PackageURL toPurl(String groupId, String artifactId, String version) {
try {
return new PackageURL(Type.MAVEN.getType(), groupId, artifactId, version, null, null);
} catch (MalformedPackageURLException e) {
throw new IllegalArgumentException("Unable to parse PackageURL", e);
}
}
private boolean depHasLibsNotation(String depToBeIgnored) {
Pattern pattern = Pattern.compile(":");
Matcher matcher = pattern.matcher(depToBeIgnored.trim());
return (depToBeIgnored.trim().startsWith("library(") || depToBeIgnored.trim().contains("libs."))
&& (matcher.results().count() <= 1);
}
private boolean isIgnoredLine(String line) {
return IgnorePatternDetector.containsIgnorePattern(line);
}
private String extractPackageName(String line) {
String packageName = line.trim();
// Extract the package name before the comment
int commentIndex = packageName.indexOf("//");
if (commentIndex != -1) {
packageName = packageName.substring(0, commentIndex).trim();
}
// Remove any other trailing comments or spaces
commentIndex = packageName.indexOf("/*");
if (commentIndex != -1) {
packageName = packageName.substring(0, commentIndex).trim();
}
return packageName;
}
private Path getDependencies(Path manifestPath) throws IOException {
// create a temp file for storing the dependency tree in
var tempFile = Files.createTempFile("TRUSTIFY_DA_graph_", null);
// the command will create the dependency tree in the temp file
String gradleCommand = gradleExecutable + " dependencies";
String[] cmdList = gradleCommand.split("\\s+");
String gradleOutput =
Operations.runProcessGetOutput(Path.of(manifestPath.getParent().toString()), cmdList);
Files.writeString(tempFile, gradleOutput);
return tempFile;
}
private Path getProperties(Path manifestPath) throws IOException {
Path propsTempFile = Files.createTempFile("propsfile", ".txt");
String propCmd = gradleExecutable + " properties";
String[] propCmdList = propCmd.split("\\s+");
String properties =
Operations.runProcessGetOutput(Path.of(manifestPath.getParent().toString()), propCmdList);
// Create a temporary file
Files.writeString(propsTempFile, properties);
return propsTempFile;
}
/**
* Builds a Software Bill of Materials (SBOM) from a text format file based on the specified
* analysis type.
*
* <p>This method initializes an SBOM, extracts the root dependency, and processes runtime and
* compile configuration lines from the provided text format file. Depending on the {@code
* analysisType}:
*
* <ul>
* <li>For {@code AnalysisType.STACK}, it uses {@code prepareLinesForParsingDependencyTree} to
* prepare lines and parses the full dependency tree for both runtime (as {@code REQUIRED})
* and compile (as {@code OPTIONAL}) configurations.
* <li>For {@code AnalysisType.COMPONENT}, it uses {@code
* prepareLinesForParsingDirectDependency} to prepare lines, filters for lines with a depth
* of 1 using {@code ProcessedLine}, and parses the filtered dependencies for runtime (as
* {@code REQUIRED}) and compile (as {@code OPTIONAL}) configurations.
* </ul>
*
* The resulting SBOM includes the root dependency and parsed dependencies with appropriate
* scopes.
*
* @param textFormatFile the path to the text format file containing dependency information
* @param propertiesMap a map of properties used to extract the root dependency
* @param analysisType the type of analysis to perform ({@code STACK} or {@code COMPONENT})
* @return the constructed {@code Sbom} object with parsed dependencies
* @throws IOException if an I/O error occurs while reading the text format file
*/
private Sbom buildSbomFromTextFormat(
Path textFormatFile, Map<String, String> propertiesMap, AnalysisType analysisType)
throws IOException {
Sbom sbom = SbomFactory.newInstance(Sbom.BelongingCondition.PURL, "sensitive");
String root = getRoot(textFormatFile, propertiesMap);
PackageURL rootPurl = parseDep(root);
sbom.addRoot(rootPurl);
List<String> runtimeConfig = extractLines(textFormatFile, RUNTIME_CLASSPATH);
List<String> compileConfig = extractLines(textFormatFile, COMPILE_CLASSPATH);
if (analysisType == AnalysisType.STACK) {
List<String> runtimePreparedLines = prepareLinesForParsingDependencyTree(runtimeConfig);
List<String> compilePreparedLines = prepareLinesForParsingDependencyTree(compileConfig);
parseDependencyTree(root, 0, runtimePreparedLines.toArray(new String[0]), sbom, REQUIRED);
parseDependencyTree(root, 0, compilePreparedLines.toArray(new String[0]), sbom, OPTIONAL);
} else {
List<ProcessedLine> runtimePreparedLines =
prepareLinesForParsingDirectDependency(runtimeConfig);
List<ProcessedLine> compilePreparedLines =
prepareLinesForParsingDirectDependency(compileConfig);
List<String> runtimeArrayForSbom = new ArrayList<>();
for (ProcessedLine line : runtimePreparedLines) {
if (line.getDepth() == 1) {
runtimeArrayForSbom.add(line.getLine());
}
}
parseDependencyTree(root, 0, runtimeArrayForSbom.toArray(new String[0]), sbom, REQUIRED);
List<String> compileArrayForSbom = new ArrayList<>();
for (ProcessedLine line : compilePreparedLines) {
if (line.getDepth() == 1) {
compileArrayForSbom.add(line.getLine());
}
}
parseDependencyTree(root, 0, compileArrayForSbom.toArray(new String[0]), sbom, OPTIONAL);
}
return sbom;
}
/**
* A class representing a processed line of text with an associated indentation depth.
*
* <p>This class encapsulates a line of text and its corresponding depth level, typically used to
* represent hierarchical or structured data such as dependencies. The class provides methods to
* access the line and depth, and overrides the {@code toString} method to provide a meaningful
* string representation.
*/
static class ProcessedLine {
/** The text content of the processed line. */
public final String line;
/** The indentation depth associated with the line. */
public final int depth;
/**
* Constructs a new {@code ProcessedLine} with the specified line and depth.
*
* @param line the text content of the line
* @param depth the indentation depth of the line
*/
public ProcessedLine(String line, int depth) {
this.line = line;
this.depth = depth;
}
public String getLine() {
return line;
}
public int getDepth() {
return depth;
}
@Override
public String toString() {
return "Dependency{line='" + line + "', depth=" + depth + "}";
}
}
/**
* Prepares a list of strings for parsing direct dependencies by transforming and filtering lines
* into {@code ProcessedLine} objects.
*
* <p>This method processes each input line, skipping empty lines or those ending with " FAILED".
* For valid lines, it:
*
* <ul>
* <li>Calculates the indentation depth using {@code getIndentationLevel}.
* <li>Applies transformations to the line using {@code replaceLine}.
* <li>Checks if the transformed line contains a version using {@code containsVersion}.
* <li>If a version is present, creates a new {@code ProcessedLine} with the transformed line
* appended with ":compile" and the calculated depth, and adds it to the result list.
* </ul>
*
* The resulting list contains only lines that meet the version criteria, encapsulated as {@code
* ProcessedLine} objects.
*
* @param lines the list of input strings to process
* @return a list of {@code ProcessedLine} objects representing valid, transformed lines with
* their indentation depths
*/
private List<ProcessedLine> prepareLinesForParsingDirectDependency(List<String> lines) {
List<ProcessedLine> result = new ArrayList<>();
for (String line : lines) {
if (line.trim().isEmpty() || line.endsWith(" FAILED")) {
continue;
}
int depth = getIndentationLevel(line);
line = replaceLine(line);
if (containsVersion(line)) {
result.add(new ProcessedLine(line + ":compile", depth));
}
}
return result;
}
/**
* Prepares a list of strings for parsing a dependency tree by transforming and filtering lines.
*
* <p>This method processes each input line, skipping empty lines or those ending with " FAILED".
* For valid lines, it:
*
* <ul>
* <li>Applies transformations to the line using {@code replaceLine}.
* <li>Checks if the transformed line contains a version using {@code containsVersion}.
* <li>If a version is present, appends ":compile" to the line and adds it to the result list.
* </ul>
*
* The resulting list contains only transformed lines that meet the version criteria, suitable for
* dependency tree parsing.
*
* @param lines the list of input strings to process
* @return a list of transformed strings, each appended with ":compile", representing valid
* dependency lines
*/
private List<String> prepareLinesForParsingDependencyTree(List<String> lines) {
List<String> result = new ArrayList<>();
for (String line : lines) {
if (line.trim().isEmpty() || line.endsWith(" FAILED")) {
continue;
}
line = replaceLine(line);
if (containsVersion(line)) {
result.add(line + ":compile");
}
}
return result;
}
/**
* Processes a given line of text by applying a series of string replacements to standardize its
* format.
*
* <p>This method performs multiple transformations on the input line, including:
*
* <ul>
* <li>Replacing triple dashes ("---") with a single dash ("-") and four spaces with two spaces.
* <li>Simplifying colon-separated patterns of the form ":X:Y -> Z" to ":X:Z".
* <li>Transforming colon-separated patterns of the form "X:Y:Z" to "X:Y:jar:Z".
* <li>Removing trailing annotations such as " (n)", " (c)", or " (*)".
* <li>Appending ":compile" to the end of the line.
* </ul>
*
* The transformed line is then returned.
*
* @param line the input string to be processed
* @return the transformed string after applying all replacements
*/
private String replaceLine(String line) {
line = line.replaceAll("---", "-").replaceAll(" ", " ");
line = line.replaceAll(":(.*):(.*) -> (.*)$", ":$1:$3");
line = line.replaceAll("(.*):(.*):(.*)$", "$1:$2:jar:$3");
line = line.replaceAll(" \\(n\\)$", "");
line = line.replaceAll(" \\(c\\)$", "");
line = line.replaceAll(" \\(\\*\\)", "");
return line;
}
/**
* Determines the indentation level of a given line of text based on specific patterns.
*
* <p>This method analyzes the input line to calculate its indentation level. If the line starts
* with a '+' or '\', it is considered to have an indentation level of 1. Otherwise, it counts
* occurrences of specific indentation patterns ("| " or " ") to determine the level. If no such
* patterns are found, it returns -1; otherwise, it returns the count of patterns plus 1.
*
* @param line the input string to analyze for indentation
* @return the indentation level of the line; returns 1 if the line starts with '+' or '\', the
* count of indentation patterns plus 1 if patterns are found, or -1 if no patterns are found
*/
public int getIndentationLevel(String line) {
if (line.matches("^[\\\\+].*")) {
return 1;
}
Pattern pattern = Pattern.compile("\\| {4}| {5}");
Matcher matcher = pattern.matcher(line);
int count = 0;
while (matcher.find()) {
count++;
}
return count == 0 ? -1 : count + 1;
}
private boolean containsVersion(String line) {
String lineStripped = line.replace("(n)", "").trim();
Pattern pattern1 =
Pattern.compile("\\W*[a-z0-9.-]+:[a-z0-9.-]+:[0-9]+[.][0-9]+(.[0-9]+)?(.*)?.*");
Pattern pattern2 = Pattern.compile(".*version:\\s?(')?[0-9]+[.][0-9]+(.[0-9]+)?(')?");
Matcher matcher1 = pattern1.matcher(lineStripped);
Matcher matcher2 = pattern2.matcher(lineStripped);
return (matcher1.find() || matcher2.find()) && !lineStripped.contains("libs.");
}
private String getRoot(Path textFormatFile, Map<String, String> propertiesMap)
throws IOException {
String group = propertiesMap.get("group");
String version = propertiesMap.get("version");
String rootName = extractRootProjectValue(textFormatFile);
return group + ':' + rootName + ':' + "jar" + ':' + version;
}
private String extractRootProjectValue(Path inputFilePath) throws IOException {
List<String> lines = Files.readAllLines(inputFilePath);
for (String line : lines) {
if (line.contains("Root project")) {
Pattern pattern = Pattern.compile("Root project '(.+)'");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
return matcher.group(1);
}
}
}
return null;
}
private Map<String, String> extractProperties(Path manifestPath) throws IOException {
Path propsTempFile = getProperties(manifestPath);
String content = Files.readString(propsTempFile);
// Define the regular expression pattern for key-value pairs
Pattern pattern = Pattern.compile("([^:]+):\\s+(.+)");
Matcher matcher = pattern.matcher(content);
// Create a Map to store key-value pairs
Map<String, String> keyValueMap = new HashMap<>();
// Iterate through matches and add them to the map
while (matcher.find()) {
String key = matcher.group(1).trim();
String value = matcher.group(2).trim();
keyValueMap.put(key, value);
}
// Check if any key-value pairs were found
if (!keyValueMap.isEmpty()) {
return keyValueMap;
} else {
return Collections.emptyMap();
}
}
private List<String> extractLines(Path inputFilePath, String startMarker) throws IOException {
List<String> lines = Files.readAllLines(inputFilePath);
List<String> extractedLines = new ArrayList<>();
boolean startFound = false;
for (String line : lines) {
// If the start marker is found, set startFound to true
if (line.startsWith(startMarker)) {
startFound = true;
continue; // Skip the line containing the startMarker
}
// If startFound is true and the line is not empty, add it to the extractedLines list
if (startFound && !line.trim().isEmpty()) {
extractedLines.add(line);
}
// If an empty line is encountered, break out of the loop
if (startFound && line.trim().isEmpty()) {
break;
}
}
return extractedLines;
}
@Override
public Content provideComponent() throws IOException {
Path tempFile = getDependencies(manifest);
Map<String, String> propertiesMap = extractProperties(manifest);
Sbom sbom = buildSbomFromTextFormat(tempFile, propertiesMap, AnalysisType.COMPONENT);
var ignored = getIgnoredDeps(manifest);
return new Content(
sbom.filterIgnoredDeps(ignored).getAsJsonString().getBytes(), Api.CYCLONEDX_MEDIA_TYPE);
}
}