Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Create ClassLoaders inside doPrivileged() + Auto-close the WAR file after exploding it #411

Merged
merged 2 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -41,7 +44,7 @@ public class Bootstrap {

@Option(name = "-jv", aliases = { "--jenkins-version"}, usage = "jenkins version to use (only in case 'warDir' is not specified). Defaults to latest LTS.")
public String version;

/**
* Where to load plugins from?
*/
Expand Down Expand Up @@ -108,7 +111,7 @@ public class Bootstrap {

@Option(name = "-v", aliases = { "--version" }, usage = "Prints the current Jenkinsfile Runner version")
public boolean showVersion;

@Option(name = "-h", aliases = { "--help"}, usage = "Prints help information.", help = true, forbids = { "-v", "-w", "-p", "-f", "--runWorkspace" })
public boolean help;

Expand Down Expand Up @@ -237,7 +240,7 @@ public void postConstruct(CmdLineParser parser) throws IOException {
}
}

private String getVersion() throws IOException {
private String getVersion() {
return getClass().getPackage().getImplementationVersion();
}

Expand Down Expand Up @@ -338,12 +341,12 @@ public int run() throws Throwable {
}
}

public ClassLoader createJenkinsWarClassLoader() throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not recall what led to this exception list

return new ClassLoaderBuilder(new SideClassLoader(getPlatformClassloader()))
.collectJars(new File(warDir,"WEB-INF/lib"))
public ClassLoader createJenkinsWarClassLoader() throws PrivilegedActionException {
return AccessController.doPrivileged((PrivilegedExceptionAction<ClassLoader>) () -> new ClassLoaderBuilder(new SideClassLoader(getPlatformClassloader()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jenkins is unlikely to work on advanced security policies . Should be fine though I will need to add Autotests for the resource loading

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pointless. See spotbugs/spotbugs#1515

.collectJars(new File(warDir, "WEB-INF/lib"))
// servlet API needs to be visible to jenkins.war
.collectJars(new File(getAppRepo(),"javax/servlet"))
.make();
.collectJars(new File(getAppRepo(), "javax/servlet"))
.make());
}

public ClassLoader createSetupClassLoader(ClassLoader jenkins) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -43,6 +45,7 @@ public ClassLoaderBuilder collectJars(File dir) throws IOException {
}

public ClassLoader make() {
return new URLClassLoader(jars.toArray(new URL[jars.size()]),parent);
return AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(jars.toArray(
new URL[jars.size()]), parent));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,36 @@
public class Util {

public static File explodeWar(String jarPath) throws IOException {
JarFile jarfile = new JarFile(new File(jarPath));
Enumeration<JarEntry> enu = jarfile.entries();
try (JarFile jarfile = new JarFile(new File(jarPath))) {
Enumeration<JarEntry> enu = jarfile.entries();

// Get current working directory path
Path currentPath = FileSystems.getDefault().getPath("").toAbsolutePath();
//Create Temporary directory
Path path = Files.createTempDirectory(currentPath.toAbsolutePath(), "jenkinsfile-runner");
File destDir = path.toFile();
// Get current working directory path
Path currentPath = FileSystems.getDefault().getPath("").toAbsolutePath();
//Create Temporary directory
Path path = Files.createTempDirectory(currentPath.toAbsolutePath(), "jenkinsfile-runner");
File destDir = path.toFile();

while(enu.hasMoreElements()) {
JarEntry je = enu.nextElement();
File file = new File(destDir, je.getName());
if (!file.exists()) {
file.getParentFile().mkdirs();
file = new File(destDir, je.getName());
}
if (je.isDirectory()) {
continue;
}
InputStream is = jarfile.getInputStream(je);
while (enu.hasMoreElements()) {
JarEntry je = enu.nextElement();
File file = new File(destDir, je.getName());
if (!file.exists()) {
file.getParentFile().mkdirs();
file = new File(destDir, je.getName());
}
if (je.isDirectory()) {
continue;
}
InputStream is = jarfile.getInputStream(je);

try (FileOutputStream fo = new FileOutputStream(file)) {
while (is.available() > 0) {
fo.write(is.read());
try (FileOutputStream fo = new FileOutputStream(file)) {
while (is.available() > 0) {
fo.write(is.read());
}
fo.close();
is.close();
}
fo.close();
is.close();
}
return destDir;
}
return destDir;
}
}