-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathThirdPartyAuditTask.java
416 lines (365 loc) · 16.9 KB
/
ThirdPartyAuditTask.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.gradle.precommit;
import de.thetaphi.forbiddenapis.cli.CliMain;
import org.apache.commons.io.output.NullOutputStream;
import org.opensearch.gradle.LoggedExec;
import org.opensearch.gradle.OS;
import org.opensearch.gradle.dependencies.CompileOnlyResolvePlugin;
import org.gradle.api.DefaultTask;
import org.gradle.api.JavaVersion;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.provider.Property;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.CompileClasspath;
import org.gradle.api.tasks.IgnoreEmptyDirectories;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.ExecResult;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@CacheableTask
public class ThirdPartyAuditTask extends DefaultTask {
private static final Pattern MISSING_CLASS_PATTERN = Pattern.compile(
"WARNING: Class '(.*)' cannot be loaded \\(.*\\)\\. Please fix the classpath!"
);
private static final Pattern VIOLATION_PATTERN = Pattern.compile("\\s\\sin ([a-zA-Z0-9$.]+) \\(.*\\)");
private static final int SIG_KILL_EXIT_VALUE = 137;
private static final List<Integer> EXPECTED_EXIT_CODES = Arrays.asList(
CliMain.EXIT_SUCCESS,
CliMain.EXIT_VIOLATION,
CliMain.EXIT_UNSUPPORTED_JDK
);
private static final String JDK_JAR_HELL_MAIN_CLASS = "org.opensearch.bootstrap.JdkJarHellCheck";
private Set<String> missingClassExcludes = new TreeSet<>();
private Set<String> violationsExcludes = new TreeSet<>();
private Set<String> jdkJarHellExcludes = new TreeSet<>();
private File signatureFile;
private String javaHome;
private FileCollection jdkJarHellClasspath;
private final Property<JavaVersion> targetCompatibility = getProject().getObjects().property(JavaVersion.class);
@Input
public Property<JavaVersion> getTargetCompatibility() {
return targetCompatibility;
}
@InputFiles
@PathSensitive(PathSensitivity.NAME_ONLY)
public Configuration getForbiddenAPIsConfiguration() {
return getProject().getConfigurations().getByName("forbiddenApisCliJar");
}
@InputFile
@PathSensitive(PathSensitivity.NONE)
public File getSignatureFile() {
return signatureFile;
}
public void setSignatureFile(File signatureFile) {
this.signatureFile = signatureFile;
}
@Input
@Optional
public String getJavaHome() {
return javaHome;
}
public void setJavaHome(String javaHome) {
this.javaHome = javaHome;
}
@Internal
public File getJarExpandDir() {
return new File(new File(getProject().getBuildDir(), "precommit/thirdPartyAudit"), getName());
}
@OutputFile
public File getSuccessMarker() {
return new File(getProject().getBuildDir(), "markers/" + getName());
}
// We use compile classpath normalization here because class implementation changes are irrelevant for the purposes of jdk jar hell.
// We only care about the runtime classpath ABI here.
@CompileClasspath
public FileCollection getJdkJarHellClasspath() {
return jdkJarHellClasspath.filter(File::exists);
}
public void setJdkJarHellClasspath(FileCollection jdkJarHellClasspath) {
this.jdkJarHellClasspath = jdkJarHellClasspath;
}
public void ignoreMissingClasses(String... classesOrPackages) {
if (classesOrPackages.length == 0) {
missingClassExcludes = null;
return;
}
if (missingClassExcludes == null) {
missingClassExcludes = new TreeSet<>();
}
for (String each : classesOrPackages) {
missingClassExcludes.add(each);
}
}
public void ignoreViolations(String... violatingClasses) {
for (String each : violatingClasses) {
violationsExcludes.add(each);
}
}
public void ignoreJarHellWithJDK(String... classes) {
for (String each : classes) {
jdkJarHellExcludes.add(each);
}
}
@Input
public Set<String> getJdkJarHellExcludes() {
return jdkJarHellExcludes;
}
@Input
@Optional
public Set<String> getMissingClassExcludes() {
return missingClassExcludes;
}
@Classpath
@SkipWhenEmpty
@IgnoreEmptyDirectories
public Set<File> getJarsToScan() {
// These are SelfResolvingDependency, and some of them backed by file collections, like the Gradle API files,
// or dependencies added as `files(...)`, we can't be sure if those are third party or not.
// err on the side of scanning these to make sure we don't miss anything
Spec<Dependency> reallyThirdParty = dep -> dep.getGroup() != null && dep.getGroup().startsWith("org.opensearch") == false;
Set<File> jars = getRuntimeConfiguration().getResolvedConfiguration().getFiles(reallyThirdParty);
Set<File> compileOnlyConfiguration = getProject().getConfigurations()
.getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
.getResolvedConfiguration()
.getFiles(reallyThirdParty);
// don't scan provided dependencies that we already scanned, e.x. don't scan cores dependencies for every plugin
if (compileOnlyConfiguration != null) {
jars.removeAll(compileOnlyConfiguration);
}
return jars;
}
@TaskAction
public void runThirdPartyAudit() throws IOException {
Set<File> jars = getJarsToScan();
extractJars(jars);
final String forbiddenApisOutput = runForbiddenAPIsCli();
final Set<String> missingClasses = new TreeSet<>();
Matcher missingMatcher = MISSING_CLASS_PATTERN.matcher(forbiddenApisOutput);
while (missingMatcher.find()) {
missingClasses.add(missingMatcher.group(1));
}
final Set<String> violationsClasses = new TreeSet<>();
Matcher violationMatcher = VIOLATION_PATTERN.matcher(forbiddenApisOutput);
while (violationMatcher.find()) {
violationsClasses.add(violationMatcher.group(1));
}
Set<String> jdkJarHellClasses = runJdkJarHellCheck();
if (missingClassExcludes != null) {
assertNoPointlessExclusions("are not missing", missingClassExcludes, missingClasses);
long bogousExcludesCount = Stream.concat(missingClassExcludes.stream(), violationsExcludes.stream())
.filter(each -> missingClasses.contains(each) == false)
.filter(each -> violationsClasses.contains(each) == false)
.count();
if (bogousExcludesCount != 0 && bogousExcludesCount == missingClassExcludes.size() + violationsExcludes.size()) {
logForbiddenAPIsOutput(forbiddenApisOutput);
throw new IllegalStateException(
"All excluded classes seem to have no issues. " + "This is sometimes an indication that the check silently failed"
);
}
missingClasses.removeAll(missingClassExcludes);
}
assertNoPointlessExclusions("have no violations", violationsExcludes, violationsClasses);
assertNoPointlessExclusions("do not generate jar hell with the JDK", jdkJarHellExcludes, jdkJarHellClasses);
if (missingClassExcludes == null && (missingClasses.isEmpty() == false)) {
getLogger().info("Found missing classes, but task is configured to ignore all of them:\n {}", formatClassList(missingClasses));
missingClasses.clear();
}
violationsClasses.removeAll(violationsExcludes);
if (missingClasses.isEmpty() && violationsClasses.isEmpty()) {
getLogger().info("Third party audit passed successfully");
} else {
logForbiddenAPIsOutput(forbiddenApisOutput);
if (missingClasses.isEmpty() == false) {
getLogger().error("Missing classes:\n{}", formatClassList(missingClasses));
}
if (violationsClasses.isEmpty() == false) {
getLogger().error("Classes with violations:\n{}", formatClassList(violationsClasses));
}
throw new IllegalStateException("Audit of third party dependencies failed");
}
assertNoJarHell(jdkJarHellClasses);
// Mark successful third party audit check
getSuccessMarker().getParentFile().mkdirs();
Files.write(getSuccessMarker().toPath(), new byte[] {});
}
private void logForbiddenAPIsOutput(String forbiddenApisOutput) {
getLogger().error("Forbidden APIs output:\n{}==end of forbidden APIs==", forbiddenApisOutput);
}
private void extractJars(Set<File> jars) {
File jarExpandDir = getJarExpandDir();
// We need to clean up to make sure old dependencies don't linger
getProject().delete(jarExpandDir);
jars.forEach(jar -> {
FileTree jarFiles = getProject().zipTree(jar);
getProject().copy(spec -> {
spec.from(jarFiles);
spec.into(jarExpandDir);
// exclude classes from multi release jars
spec.exclude("META-INF/versions/**");
});
// Deal with multi release jars:
// The order is important, we iterate here so we don't depend on the order in which Gradle executes the spec
// We extract multi release jar classes ( if these exist ) going from 9 - the first to support them, to the
// current `targetCompatibility` version.
// Each extract will overwrite the top level classes that existed before it, the result is that we end up
// with a single version of the class in `jarExpandDir`.
// This will be the closes version to `targetCompatibility`, the same class that would be loaded in a JVM
// that has `targetCompatibility` version.
// This means we only scan classes that would be loaded into `targetCompatibility`, and don't look at any
// pther version specific implementation of said classes.
IntStream.rangeClosed(
Integer.parseInt(JavaVersion.VERSION_1_9.getMajorVersion()),
Integer.parseInt(targetCompatibility.get().getMajorVersion())
).forEach(majorVersion -> getProject().copy(spec -> {
spec.from(getProject().zipTree(jar));
spec.into(jarExpandDir);
String metaInfPrefix = "META-INF/versions/" + majorVersion;
spec.include(metaInfPrefix + "/**");
// Drop the version specific prefix
spec.eachFile(details -> details.setPath(details.getPath().replace(metaInfPrefix, "")));
spec.setIncludeEmptyDirs(false);
}));
});
}
private void assertNoJarHell(Set<String> jdkJarHellClasses) {
jdkJarHellClasses.removeAll(jdkJarHellExcludes);
if (jdkJarHellClasses.isEmpty() == false) {
throw new IllegalStateException(
"Audit of third party dependencies failed:\n" + " Jar Hell with the JDK:\n" + formatClassList(jdkJarHellClasses)
);
}
}
private void assertNoPointlessExclusions(String specifics, Set<String> excludes, Set<String> problematic) {
String notMissing = excludes.stream()
.filter(each -> problematic.contains(each) == false)
.map(each -> " * " + each)
.collect(Collectors.joining("\n"));
if (notMissing.isEmpty() == false) {
getLogger().error("Unnecessary exclusions, following classes " + specifics + ":\n {}", notMissing);
throw new IllegalStateException("Third party audit task is not configured correctly");
}
}
private String formatClassList(Set<String> classList) {
return classList.stream().map(name -> " * " + name).sorted().collect(Collectors.joining("\n"));
}
private String runForbiddenAPIsCli() throws IOException {
ByteArrayOutputStream errorOut = new ByteArrayOutputStream();
ExecResult result = getProject().javaexec(spec -> {
if (javaHome != null) {
spec.setExecutable(javaHome + "/bin/java");
}
spec.classpath(
getForbiddenAPIsConfiguration(),
getRuntimeConfiguration(),
getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
);
spec.jvmArgs("-Xmx1g");
spec.jvmArgs(LoggedExec.shortLivedArgs());
spec.getMainClass().set("de.thetaphi.forbiddenapis.cli.CliMain");
spec.args("-f", getSignatureFile().getAbsolutePath(), "-d", getJarExpandDir(), "--allowmissingclasses");
spec.setErrorOutput(errorOut);
if (getLogger().isInfoEnabled() == false) {
spec.setStandardOutput(new NullOutputStream());
}
spec.setIgnoreExitValue(true);
});
if (OS.current().equals(OS.LINUX) && result.getExitValue() == SIG_KILL_EXIT_VALUE) {
throw new IllegalStateException("Third party audit was killed buy SIGKILL, could be a victim of the Linux OOM killer");
}
final String forbiddenApisOutput;
try (ByteArrayOutputStream outputStream = errorOut) {
forbiddenApisOutput = outputStream.toString(StandardCharsets.UTF_8.name());
}
if (EXPECTED_EXIT_CODES.contains(result.getExitValue()) == false) {
throw new IllegalStateException("Forbidden APIs cli failed: " + forbiddenApisOutput);
}
return forbiddenApisOutput;
}
private Set<String> runJdkJarHellCheck() throws IOException {
ByteArrayOutputStream standardOut = new ByteArrayOutputStream();
ExecResult execResult = getProject().javaexec(spec -> {
spec.classpath(
jdkJarHellClasspath,
getRuntimeConfiguration(),
getProject().getConfigurations().getByName(CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME)
);
spec.getMainClass().set(JDK_JAR_HELL_MAIN_CLASS);
spec.args(getJarExpandDir());
spec.setIgnoreExitValue(true);
if (javaHome != null) {
spec.setExecutable(javaHome + "/bin/java");
}
spec.setStandardOutput(standardOut);
});
if (execResult.getExitValue() == 0) {
return Collections.emptySet();
}
final String jdkJarHellCheckList;
try (ByteArrayOutputStream outputStream = standardOut) {
jdkJarHellCheckList = outputStream.toString(StandardCharsets.UTF_8.name());
}
return new TreeSet<>(Arrays.asList(jdkJarHellCheckList.split("\\r?\\n")));
}
private Configuration getRuntimeConfiguration() {
Configuration runtime = getProject().getConfigurations().findByName("runtimeClasspath");
if (runtime == null) {
return getProject().getConfigurations().getByName("testCompileClasspath");
}
return runtime;
}
}