diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/PropertyUserStore.java b/jetty-security/src/main/java/org/eclipse/jetty/security/PropertyUserStore.java index 0c418ea65a6..4630ae6cfd9 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/PropertyUserStore.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/PropertyUserStore.java @@ -18,17 +18,8 @@ package org.eclipse.jetty.security; -import org.eclipse.jetty.util.PathWatcher; -import org.eclipse.jetty.util.PathWatcher.PathWatchEvent; -import org.eclipse.jetty.util.StringUtil; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; -import org.eclipse.jetty.util.resource.JarResource; -import org.eclipse.jetty.util.resource.PathResource; -import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.security.Credential; - import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -41,6 +32,17 @@ import java.util.Map; import java.util.Properties; import java.util.Set; +import org.eclipse.jetty.toolchain.test.IO; +import org.eclipse.jetty.util.PathWatcher; +import org.eclipse.jetty.util.PathWatcher.PathWatchEvent; +import org.eclipse.jetty.util.StringUtil; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.util.resource.JarFileResource; +import org.eclipse.jetty.util.resource.PathResource; +import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.util.security.Credential; + /** * PropertyUserStore *
@@ -59,13 +61,10 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
{
private static final Logger LOG = Log.getLogger(PropertyUserStore.class);
- private static final String JAR_FILE = "jar:file:";
-
protected Path _configPath;
- protected Resource _configResource;
- protected PathWatcher pathWatcher;
- protected boolean hotReload = false; // default is not to reload
+ protected PathWatcher _pathWatcher;
+ protected boolean _hotReload = false; // default is not to reload
protected boolean _firstLoad = true; // true if first load, false from that point on
protected Listjar:file:
*/
+ @Deprecated
public void setConfigPath(String configFile)
{
- if (configFile == null)
- {
- _configPath = null;
- }
- else if (new File( configFile ).exists())
- {
- _configPath = new File(configFile).toPath();
- }
- if ( !new File( configFile ).exists() && configFile.startsWith( JAR_FILE ))
- {
- // format of the url is jar:file:/foo/bar/beer.jar!/mountain_goat/pale_ale.txt
- // ideally we'd like to extract this to Resource class?
- try
- {
- _configPath = extractPackedFile( configFile );
- }
- catch ( IOException e )
- {
- throw new RuntimeException( "cannot extract file from url:" + configFile, e );
- }
- }
+ setConfig(configFile);
}
- private Path extractPackedFile( String configFile )
+ private Path extractPackedFile( JarFileResource configResource )
throws IOException
{
- int fileIndex = configFile.indexOf( "!" );
- String entryPath = configFile.substring( fileIndex + 1, configFile.length() );
+ String uri = configResource.getURI().toASCIIString();
+ int colon = uri.lastIndexOf(":");
+ int bang_slash = uri.indexOf("!/");
+ if (colon<0 || bang_slash<0 || colon>bang_slash)
+ throw new IllegalArgumentException("Not resolved JarFile resource: "+uri);
+ String entry_path = uri.substring(colon+2).replace("!/","__").replace('/','_').replace('.','_');
Path tmpDirectory = Files.createTempDirectory( "users_store" );
- Path extractedPath = Paths.get(tmpDirectory.toString(), entryPath);
- // delete if exists as copyTo do not overwrite
+ tmpDirectory.toFile().deleteOnExit();
+ Path extractedPath = Paths.get(tmpDirectory.toString(), entry_path);
Files.deleteIfExists( extractedPath );
- // delete on shutdown
extractedPath.toFile().deleteOnExit();
- JarResource.newResource( configFile ).copyTo( tmpDirectory.toFile() );
+ IO.copy(configResource.getInputStream(),new FileOutputStream(extractedPath.toFile()));
+ if (isHotReload())
+ {
+ LOG.warn("Cannot hot reload from packed configuration: {}",configResource);
+ setHotReload(false);
+ }
return extractedPath;
}
@@ -162,7 +160,17 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
* Set the Config Path from a {@link File} reference
* @param configFile the config file
*/
+ @Deprecated
public void setConfigPath(File configFile)
+ {
+ setConfigFile(configFile);
+ }
+
+ /**
+ * Set the Config Path from a {@link File} reference
+ * @param configFile the config file
+ */
+ public void setConfigFile(File configFile)
{
if(configFile == null)
{
@@ -182,19 +190,15 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
_configPath = configPath;
}
- /* ------------------------------------------------------------ */
/**
* @return the resource associated with the configured properties file, creating it if necessary
* @throws IOException if unable to get the resource
*/
public Resource getConfigResource() throws IOException
{
- if (_configResource == null)
- {
- _configResource = new PathResource(_configPath);
- }
-
- return _configResource;
+ if (_configPath==null)
+ return null;
+ return new PathResource(_configPath);
}
/**
@@ -204,7 +208,7 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
*/
public boolean isHotReload()
{
- return hotReload;
+ return _hotReload;
}
/**
@@ -218,7 +222,7 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
{
throw new IllegalStateException("Cannot set hot reload while user store is running");
}
- this.hotReload = enable;
+ this._hotReload = enable;
}
@@ -247,8 +251,9 @@ public class PropertyUserStore extends UserStore implements PathWatcher.Listener
}
Properties properties = new Properties();
- if (getConfigResource().exists())
- properties.load(getConfigResource().getInputStream());
+ Resource config = getConfigResource();
+ if (config!=null && config.exists())
+ properties.load(config.getInputStream());
Set