forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonPipProvider.java
More file actions
299 lines (271 loc) · 11.9 KB
/
PythonPipProvider.java
File metadata and controls
299 lines (271 loc) · 11.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
/*
* 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.fasterxml.jackson.core.JsonProcessingException;
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;
import io.github.guacsec.trustifyda.tools.Operations;
import io.github.guacsec.trustifyda.utils.Environment;
import io.github.guacsec.trustifyda.utils.IgnorePatternDetector;
import io.github.guacsec.trustifyda.utils.PythonControllerBase;
import io.github.guacsec.trustifyda.utils.PythonControllerRealEnv;
import io.github.guacsec.trustifyda.utils.PythonControllerVirtualEnv;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public final class PythonPipProvider extends Provider {
private static final Logger log = LoggersFactory.getLogger(PythonPipProvider.class.getName());
private static final String DEFAULT_PIP_ROOT_COMPONENT_NAME = "default-pip-root";
private static final String DEFAULT_PIP_ROOT_COMPONENT_VERSION = "0.0.0";
public void setPythonController(PythonControllerBase pythonController) {
this.pythonController = pythonController;
}
private PythonControllerBase pythonController;
public PythonPipProvider(Path manifest) {
super(Ecosystem.Type.PYTHON, manifest);
}
@Override
public Content provideStack() throws IOException {
PythonControllerBase pythonController = getPythonController();
List<Map<String, Object>> dependencies =
pythonController.getDependencies(manifest.toString(), true);
printDependenciesTree(dependencies);
Sbom sbom = SbomFactory.newInstance(Sbom.BelongingCondition.PURL, "sensitive");
sbom.addRoot(toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION));
for (Map<String, Object> component : dependencies) {
addAllDependencies(sbom.getRoot(), component, sbom);
}
byte[] requirementsFile = Files.readAllBytes(manifest);
handleIgnoredDependencies(new String(requirementsFile), sbom);
return new Content(
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
}
private void addAllDependencies(PackageURL source, Map<String, Object> component, Sbom sbom) {
PackageURL packageURL =
toPurl((String) component.get("name"), (String) component.get("version"));
sbom.addDependency(source, packageURL, null);
List<Map<String, Object>> directDeps =
(List<Map<String, Object>>) component.get("dependencies");
if (directDeps != null) {
for (Map<String, Object> dep : directDeps) {
addAllDependencies(packageURL, dep, sbom);
}
}
}
@Override
public Content provideComponent() throws IOException {
PythonControllerBase pythonController = getPythonController();
List<Map<String, Object>> dependencies =
pythonController.getDependencies(manifest.toString(), false);
printDependenciesTree(dependencies);
Sbom sbom = SbomFactory.newInstance();
sbom.addRoot(toPurl(DEFAULT_PIP_ROOT_COMPONENT_NAME, DEFAULT_PIP_ROOT_COMPONENT_VERSION));
dependencies.forEach(
(component) ->
sbom.addDependency(
sbom.getRoot(),
toPurl((String) component.get("name"), (String) component.get("version")),
null));
var manifestContent = Files.readString(manifest);
handleIgnoredDependencies(manifestContent, sbom);
return new Content(
sbom.getAsJsonString().getBytes(StandardCharsets.UTF_8), Api.CYCLONEDX_MEDIA_TYPE);
}
private void printDependenciesTree(List<Map<String, Object>> dependencies)
throws JsonProcessingException {
if (debugLoggingIsNeeded()) {
String pythonControllerTree =
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(dependencies);
log.info(
String.format(
"Python Generated Dependency Tree in Json Format: %s %s %s",
System.lineSeparator(), pythonControllerTree, System.lineSeparator()));
}
}
private void handleIgnoredDependencies(String manifestContent, Sbom sbom) {
Set<PackageURL> ignoredDeps = getIgnoredDependencies(manifestContent);
Set<String> ignoredDepsVersions =
ignoredDeps.stream()
.filter(dep -> !dep.getVersion().trim().equals("*"))
.map(PackageURL::getCoordinates)
.collect(Collectors.toSet());
Set<String> ignoredDepsNoVersions =
ignoredDeps.stream()
.filter(dep -> dep.getVersion().trim().equals("*"))
.map(PackageURL::getCoordinates)
.collect(Collectors.toSet());
// filter out by name only from sbom all exhortignore dependencies that their version will be
// resolved by pip.
sbom.setBelongingCriteriaBinaryAlgorithm(Sbom.BelongingCondition.NAME);
sbom.filterIgnoredDeps(ignoredDepsNoVersions);
boolean matchManifestVersions = Environment.getBoolean(PROP_MATCH_MANIFEST_VERSIONS, true);
// filter out by purl from sbom all exhortignore dependencies that their version hardcoded in
// requirements.txt -
// in case all versions in manifest matching installed versions of packages in environment.
if (matchManifestVersions) {
sbom.setBelongingCriteriaBinaryAlgorithm(Sbom.BelongingCondition.PURL);
sbom.filterIgnoredDeps(ignoredDepsVersions);
} else {
// in case version mismatch is possible (MATCH_MANIFEST_VERSIONS=false) , need to parse the
// name of package
// from the purl, and remove the package name from sbom according to name only
Set<String> deps =
ignoredDepsVersions.stream()
.map(
purlString -> {
try {
return new PackageURL(purlString).getName();
} catch (MalformedPackageURLException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toSet());
sbom.setBelongingCriteriaBinaryAlgorithm(Sbom.BelongingCondition.NAME);
sbom.filterIgnoredDeps(deps);
}
}
/**
* Checks if a text line contains a Python pip ignore pattern. Handles both '#exhortignore' and
* '#trustify-da-ignore' with optional spacing.
*
* @param line the line to check
* @return true if the line contains a Python pip ignore pattern
*/
private boolean containsPythonIgnorePattern(String line) {
return line.contains("#" + IgnorePatternDetector.IGNORE_PATTERN)
|| line.contains("# " + IgnorePatternDetector.IGNORE_PATTERN)
|| line.contains("#" + IgnorePatternDetector.LEGACY_IGNORE_PATTERN)
|| line.contains("# " + IgnorePatternDetector.LEGACY_IGNORE_PATTERN);
}
private Set<PackageURL> getIgnoredDependencies(String requirementsDeps) {
String[] requirementsLines = requirementsDeps.split(System.lineSeparator());
Set<PackageURL> collected =
Arrays.stream(requirementsLines)
.filter(this::containsPythonIgnorePattern)
.map(PythonPipProvider::extractDepFull)
.map(this::splitToNameVersion)
.map(dep -> toPurl(dep[0], dep[1]))
// .map(packageURL -> packageURL.getCoordinates())
.collect(Collectors.toSet());
return collected;
}
private String[] splitToNameVersion(String nameVersion) {
String[] result;
if (nameVersion.matches(
"[a-zA-Z0-9-_()]+={2}[0-9]{1,4}[.][0-9]{1,4}(([.][0-9]{1,4})|([.][a-zA-Z0-9]+)|([a-zA-Z0-9]+)|([.][a-zA-Z0-9]+[.][a-z-A-Z0-9]+))?")) {
result = nameVersion.split("==");
} else {
String dependencyName = PythonControllerBase.getDependencyName(nameVersion);
result = new String[] {dependencyName, "*"};
}
return result;
}
private static String extractDepFull(String requirementLine) {
return requirementLine.substring(0, requirementLine.indexOf("#")).trim();
}
private PackageURL toPurl(String name, String version) {
try {
return new PackageURL(Ecosystem.Type.PYTHON.getType(), null, name, version, null, null);
} catch (MalformedPackageURLException e) {
throw new RuntimeException(e);
}
}
private PythonControllerBase getPythonController() {
String pythonPipBinaries;
boolean useVirtualPythonEnv;
if (!Environment.get(PythonControllerBase.PROP_TRUSTIFY_DA_PIP_SHOW, "").trim().isEmpty()
&& !Environment.get(PythonControllerBase.PROP_TRUSTIFY_DA_PIP_FREEZE, "")
.trim()
.isEmpty()) {
pythonPipBinaries = "python;;pip";
useVirtualPythonEnv = false;
} else {
pythonPipBinaries = getExecutable("python", "--version");
useVirtualPythonEnv =
Environment.getBoolean(PythonControllerBase.PROP_TRUSTIFY_DA_PYTHON_VIRTUAL_ENV, false);
}
String[] parts = pythonPipBinaries.split(";;");
var python = parts[0];
var pip = parts[1];
PythonControllerBase pythonController;
if (this.pythonController == null) {
if (useVirtualPythonEnv) {
pythonController = new PythonControllerVirtualEnv(python);
} else {
pythonController = new PythonControllerRealEnv(python, pip);
}
} else {
pythonController = this.pythonController;
}
return pythonController;
}
private String getExecutable(String command, String args) {
String python = Operations.getCustomPathOrElse("python3");
String pip = Operations.getCustomPathOrElse("pip3");
try {
Operations.runProcess(python, args);
Operations.runProcess(pip, args);
} catch (Exception e) {
python = Operations.getCustomPathOrElse("python");
pip = Operations.getCustomPathOrElse("pip");
try {
Process process = new ProcessBuilder(command, args).redirectErrorStream(true).start();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException(
"Python executable found, but it exited with error code " + exitCode);
}
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(
String.format(
"Unable to find or run Python executable '%s'. Please ensure Python is installed"
+ " and available in your PATH.",
command),
ex);
}
try {
Process process = new ProcessBuilder("pip", args).redirectErrorStream(true).start();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException("Pip executable found, but it exited with error code " + exitCode);
}
} catch (IOException | InterruptedException ex) {
throw new RuntimeException(
String.format(
"Unable to find or run Pip executable '%s'. Please ensure Pip is installed and"
+ " available in your PATH.",
command),
ex);
}
}
return String.format("%s;;%s", python, pip);
}
}