GUACAMOLE-1508: Automatically delete temporary files on webapp shutdown.

This commit is contained in:
Michael Jumper
2022-01-20 16:03:17 -08:00
parent 3fb309bbcf
commit 27c4935e36
4 changed files with 103 additions and 19 deletions

View File

@@ -19,6 +19,7 @@
package org.apache.guacamole; package org.apache.guacamole;
import com.google.common.collect.Lists;
import org.apache.guacamole.tunnel.TunnelModule; import org.apache.guacamole.tunnel.TunnelModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
@@ -117,6 +118,14 @@ public class GuacamoleServletContextListener extends GuiceServletContextListener
@Inject @Inject
private List<AuthenticationProvider> authProviders; private List<AuthenticationProvider> authProviders;
/**
* All temporary files that should be deleted upon application shutdown, in
* reverse order of desired deletion. This will typically simply be the
* order that each file was created.
*/
@Inject
private List<File> temporaryFiles;
/** /**
* Internal reference to the Guice injector that was lazily created when * Internal reference to the Guice injector that was lazily created when
* getInjector() was first invoked. * getInjector() was first invoked.
@@ -194,20 +203,49 @@ public class GuacamoleServletContextListener extends GuiceServletContextListener
}); });
} }
/**
* Deletes the given temporary file/directory, if possible. If the deletion
* operation fails, a warning is logged noting the failure. If the given
* file is a directory, it will only be deleted if empty.
*
* @param temp
* The temporary file to delete.
*/
private void deleteTemporaryFile(File temp) {
if (!temp.delete()) {
logger.warn("Temporary file/directory \"{}\" could not be "
+ "deleted. The file may remain until the JVM exits, or "
+ "may need to be manually deleted.", temp);
}
else
logger.debug("Deleted temporary file/directory \"{}\".", temp);
}
@Override @Override
public void contextDestroyed(ServletContextEvent servletContextEvent) { public void contextDestroyed(ServletContextEvent servletContextEvent) {
try {
// Clean up reference to Guice injector
servletContextEvent.getServletContext().removeAttribute(GUICE_INJECTOR);
// Clean up reference to Guice injector // Shutdown TokenSessionMap
servletContextEvent.getServletContext().removeAttribute(GUICE_INJECTOR); if (sessionMap != null)
sessionMap.shutdown();
// Shutdown TokenSessionMap // Unload all extensions
if (sessionMap != null) if (authProviders != null) {
sessionMap.shutdown(); for (AuthenticationProvider authProvider : authProviders)
authProvider.shutdown();
}
}
finally {
// Regardless of what may succeed/fail here, always attempt to
// clean up ALL temporary files
if (temporaryFiles != null)
Lists.reverse(temporaryFiles).stream().forEachOrdered(this::deleteTemporaryFile);
// Unload all extensions
if (authProviders != null) {
for (AuthenticationProvider authProvider : authProviders)
authProvider.shutdown();
} }
// Continue any Guice-specific cleanup // Continue any Guice-specific cleanup

View File

@@ -27,6 +27,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipException; import java.util.zip.ZipException;
@@ -355,12 +356,18 @@ public class Extension {
* @param file * @param file
* The file to load as an extension. * The file to load as an extension.
* *
* @param temporaryFiles
* A modifiable List that should be populated with all temporary files
* created for this extension. These files should be deleted on
* application shutdown in reverse order.
*
* @throws GuacamoleException * @throws GuacamoleException
* If the provided file is not a .jar file, does not contain the * If the provided file is not a .jar file, does not contain the
* guac-manifest.json, or if guac-manifest.json is invalid and cannot * guac-manifest.json, or if guac-manifest.json is invalid and cannot
* be parsed. * be parsed.
*/ */
public Extension(final ClassLoader parent, final File file) throws GuacamoleException { public Extension(final ClassLoader parent, final File file,
final List<File> temporaryFiles) throws GuacamoleException {
// Associate extension abstraction with original file // Associate extension abstraction with original file
this.file = file; this.file = file;
@@ -390,7 +397,7 @@ public class Extension {
} }
// Create isolated classloader for this extension // Create isolated classloader for this extension
classLoader = ExtensionClassLoader.getInstance(file, parent); classLoader = ExtensionClassLoader.getInstance(file, temporaryFiles, parent);
} }

View File

@@ -84,6 +84,11 @@ public class ExtensionClassLoader extends URLClassLoader {
* @param extension * @param extension
* The extension .jar file from which classes should be loaded. * The extension .jar file from which classes should be loaded.
* *
* @param temporaryFiles
* A modifiable List that should be populated with all temporary files
* created for the given extension. These files should be deleted on
* application shutdown in reverse order.
*
* @param parent * @param parent
* The ClassLoader to use if class resolution through the extension * The ClassLoader to use if class resolution through the extension
* .jar fails. * .jar fails.
@@ -97,7 +102,8 @@ public class ExtensionClassLoader extends URLClassLoader {
* file cannot be read. * file cannot be read.
*/ */
public static ExtensionClassLoader getInstance(final File extension, public static ExtensionClassLoader getInstance(final File extension,
final ClassLoader parent) throws GuacamoleException { final List<File> temporaryFiles, final ClassLoader parent)
throws GuacamoleException {
try { try {
// Attempt to create classloader which loads classes from the given // Attempt to create classloader which loads classes from the given
@@ -106,7 +112,7 @@ public class ExtensionClassLoader extends URLClassLoader {
@Override @Override
public ExtensionClassLoader run() throws GuacamoleException { public ExtensionClassLoader run() throws GuacamoleException {
return new ExtensionClassLoader(extension, parent); return new ExtensionClassLoader(extension, temporaryFiles, parent);
} }
}); });
@@ -194,6 +200,11 @@ public class ExtensionClassLoader extends URLClassLoader {
* @param extension * @param extension
* The extension .jar file to generate URLs for. * The extension .jar file to generate URLs for.
* *
* @param temporaryFiles
* A modifiable List that should be populated with all temporary files
* created for the given extension. These files should be deleted on
* application shutdown in reverse order.
*
* @return * @return
* An array of all URLs relevant to the given extension .jar. * An array of all URLs relevant to the given extension .jar.
* *
@@ -202,8 +213,8 @@ public class ExtensionClassLoader extends URLClassLoader {
* cannot be read, or any necessary temporary files/directories cannot * cannot be read, or any necessary temporary files/directories cannot
* be created. * be created.
*/ */
private static URL[] getExtensionURLs(File extension) private static URL[] getExtensionURLs(File extension,
throws GuacamoleException { List<File> temporaryFiles) throws GuacamoleException {
JarFile extensionJar; JarFile extensionJar;
try { try {
@@ -238,6 +249,7 @@ public class ExtensionClassLoader extends URLClassLoader {
try { try {
if (extensionTempLibDir == null) { if (extensionTempLibDir == null) {
extensionTempLibDir = Files.createTempDirectory(EXTENSION_TEMP_DIR_PREFIX); extensionTempLibDir = Files.createTempDirectory(EXTENSION_TEMP_DIR_PREFIX);
temporaryFiles.add(extensionTempLibDir.toFile());
extensionTempLibDir.toFile().deleteOnExit(); extensionTempLibDir.toFile().deleteOnExit();
} }
} }
@@ -252,6 +264,7 @@ public class ExtensionClassLoader extends URLClassLoader {
File tempLibrary; File tempLibrary;
try { try {
tempLibrary = Files.createTempFile(extensionTempLibDir, EXTENSION_TEMP_LIB_PREFIX, ".jar").toFile(); tempLibrary = Files.createTempFile(extensionTempLibDir, EXTENSION_TEMP_LIB_PREFIX, ".jar").toFile();
temporaryFiles.add(tempLibrary);
tempLibrary.deleteOnExit(); tempLibrary.deleteOnExit();
} }
catch (IOException e) { catch (IOException e) {
@@ -295,6 +308,11 @@ public class ExtensionClassLoader extends URLClassLoader {
* @param extension * @param extension
* The extension .jar file from which classes should be loaded. * The extension .jar file from which classes should be loaded.
* *
* @param temporaryFiles
* A modifiable List that should be populated with all temporary files
* created for the given extension. These files should be deleted on
* application shutdown in reverse order.
*
* @param parent * @param parent
* The ClassLoader to use if class resolution through the extension * The ClassLoader to use if class resolution through the extension
* .jar fails. * .jar fails.
@@ -303,9 +321,9 @@ public class ExtensionClassLoader extends URLClassLoader {
* If the given file is not actually a file, or the contents of the * If the given file is not actually a file, or the contents of the
* file cannot be read. * file cannot be read.
*/ */
private ExtensionClassLoader(File extension, ClassLoader parent) private ExtensionClassLoader(File extension, List<File> temporaryFiles,
throws GuacamoleException { ClassLoader parent) throws GuacamoleException {
super(getExtensionURLs(extension), null); super(getExtensionURLs(extension, temporaryFiles), null);
this.parent = parent; this.parent = parent;
} }

View File

@@ -140,6 +140,13 @@ public class ExtensionModule extends ServletModule {
private final List<Listener> boundListeners = private final List<Listener> boundListeners =
new ArrayList<Listener>(); new ArrayList<Listener>();
/**
* All temporary files that should be deleted upon application shutdown, in
* reverse order of desired deletion. This will typically simply be the
* order that each file was created.
*/
private final List<File> temporaryFiles = new ArrayList<>();
/** /**
* Service for adding and retrieving language resources. * Service for adding and retrieving language resources.
*/ */
@@ -261,6 +268,20 @@ public class ExtensionModule extends ServletModule {
return Collections.unmodifiableList(boundAuthenticationProviders); return Collections.unmodifiableList(boundAuthenticationProviders);
} }
/**
* Returns a list of all temporary files that should be deleted upon
* application shutdown, in reverse order of desired deletion. This will
* typically simply be the order that each file was created.
*
* @return
* A List of all temporary files that should be deleted upon
* application shutdown. The List is not modifiable.
*/
@Provides
public List<File> getTemporaryFiles() {
return Collections.unmodifiableList(temporaryFiles);
}
/** /**
* Binds the given provider class such that a listener is bound for each * Binds the given provider class such that a listener is bound for each
* listener interface implemented by the provider and such that all bound * listener interface implemented by the provider and such that all bound
@@ -479,7 +500,7 @@ public class ExtensionModule extends ServletModule {
try { try {
// Load extension from file // Load extension from file
Extension extension = new Extension(getParentClassLoader(), extensionFile); Extension extension = new Extension(getParentClassLoader(), extensionFile, temporaryFiles);
// Validate Guacamole version of extension // Validate Guacamole version of extension
if (!isCompatible(extension.getGuacamoleVersion())) { if (!isCompatible(extension.getGuacamoleVersion())) {