forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseJavaProvider.java
More file actions
217 lines (197 loc) · 7.46 KB
/
BaseJavaProvider.java
File metadata and controls
217 lines (197 loc) · 7.46 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
/*
* 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 com.redhat.exhort.providers;
import com.github.packageurl.MalformedPackageURLException;
import com.github.packageurl.PackageURL;
import com.redhat.exhort.Provider;
import com.redhat.exhort.sbom.Sbom;
import com.redhat.exhort.tools.Ecosystem;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
public abstract class BaseJavaProvider extends Provider {
protected BaseJavaProvider(Ecosystem.Type ecosystem, Path manifest) {
super(ecosystem, manifest);
}
void parseDependencyTree(String src, int srcDepth, String[] lines, Sbom sbom, String scope) {
if (lines.length == 0) {
return;
}
if (lines.length == 1 && lines[0].trim().isEmpty()) {
return;
}
int index = 0;
String target = lines[index];
int targetDepth = getDepth(target);
while (targetDepth > srcDepth && index < lines.length) {
if (targetDepth == srcDepth + 1) {
PackageURL from = parseDep(src);
PackageURL to = parseDep(target);
if (dependencyIsNotTestScope(from) && dependencyIsNotTestScope(to)) {
sbom.addDependency(from, to, scope);
}
} else {
String[] modifiedLines = Arrays.copyOfRange(lines, index, lines.length);
parseDependencyTree(
lines[index - 1], getDepth(lines[index - 1]), modifiedLines, sbom, scope);
}
if (index < lines.length - 1) {
target = lines[++index];
targetDepth = getDepth(target);
} else {
index++;
}
}
}
static boolean dependencyIsNotTestScope(PackageURL artifact) {
return (Objects.nonNull(artifact.getQualifiers())
&& !artifact.getQualifiers().get("scope").equals("test"))
|| Objects.isNull(artifact.getQualifiers());
}
PackageURL parseDep(String dep) {
// root package
DependencyAggregator dependencyAggregator = new DependencyAggregator();
// in case line in dependency tree text starts with a letter ( for root artifact).
if (dep.matches("^\\w.*")) {
dependencyAggregator = new DependencyAggregator();
String[] parts = dep.split(":");
dependencyAggregator.groupId = parts[0];
dependencyAggregator.artifactId = parts[1];
dependencyAggregator.version = parts[3];
return dependencyAggregator.toPurl();
}
int firstDash = dep.indexOf("-");
String dependency = dep.substring(++firstDash).trim();
if (dependency.startsWith("(")) {
dependency = dependency.substring(1);
}
dependency = dependency.replace(":runtime", ":compile").replace(":provided", ":compile");
int endIndex = Math.max(dependency.indexOf(":compile"), dependency.indexOf(":test"));
int scopeLength;
if (dependency.contains(":compile")) {
scopeLength = ":compile".length();
} else {
scopeLength = ":test".length();
}
dependency = dependency.substring(0, endIndex + scopeLength);
String[] parts = dependency.split(":");
// contains only GAV + packaging + scope
if (parts.length == 5) {
dependencyAggregator.groupId = parts[0];
dependencyAggregator.artifactId = parts[1];
dependencyAggregator.version = parts[3];
String conflictMessage = "omitted for conflict with";
if (dep.contains(conflictMessage)) {
dependencyAggregator.version =
dep.substring(dep.indexOf(conflictMessage) + conflictMessage.length())
.replace(")", "")
.trim();
}
}
// In case there are 6 parts, there is also a classifier for artifact (version suffix)
// contains GAV + packaging + classifier + scope
else if (parts.length == 6) {
dependencyAggregator.groupId = parts[0];
dependencyAggregator.artifactId = parts[1];
dependencyAggregator.version = String.format("%s-%s", parts[4], parts[3]);
String conflictMessage = "omitted for conflict with";
if (dep.contains(conflictMessage)) {
dependencyAggregator.version =
dep.substring(dep.indexOf(conflictMessage) + conflictMessage.length())
.replace(")", "")
.trim();
}
} else {
throw new RuntimeException(
String.format("Cannot parse dependency into PackageUrl from line = \"%s\"", dep));
}
if (parts[parts.length - 1].matches(".*[a-z]$")) {
dependencyAggregator.scope = parts[parts.length - 1];
} else {
int endOfLine =
Integer.min(parts[parts.length - 1].indexOf(""), parts[parts.length - 1].indexOf("-"));
dependencyAggregator.scope = parts[parts.length - 1].substring(0, endOfLine).trim();
}
return dependencyAggregator.toPurl();
}
int getDepth(String line) {
if (line == null || line.trim().isEmpty()) {
return -1;
}
if (line.matches("^\\w.*")) {
return 0;
}
return ((line.indexOf('-') - 1) / 3) + 1;
}
// 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, */
static final class DependencyAggregator {
String scope = "*";
String groupId;
String artifactId;
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);
}
boolean isValid() {
return Objects.nonNull(groupId) && Objects.nonNull(artifactId) && Objects.nonNull(version);
}
boolean isTestDependency() {
return scope.trim().equals("test");
}
PackageURL toPurl() {
try {
return new PackageURL(
Ecosystem.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);
}
}
}