resolve cipher api dependency problems

This commit is contained in:
Sebastian Sdorra
2011-09-03 17:28:57 +02:00
parent 9d52d43061
commit 8c0324de73
7 changed files with 120 additions and 42 deletions

View File

@@ -33,15 +33,78 @@
package sonia.scm.security; package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import sonia.scm.SCMContext;
import sonia.scm.util.ServiceUtil;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
* @since 1.7
*/ */
public class CipherSingleton public class CipherUtil
{ {
/** Field description */ /** Field description */
private static CipherHandler cipherHandler; private static volatile CipherUtil instance;
//~--- constructors ---------------------------------------------------------
private KeyGenerator keyGenerator;
/**
* Constructs ...
*
*/
private CipherUtil()
{
keyGenerator = ServiceUtil.getService(KeyGenerator.class);
if (keyGenerator == null)
{
keyGenerator = new UUIDKeyGenerator();
}
cipherHandler = ServiceUtil.getService(CipherHandler.class);
if (cipherHandler == null)
{
cipherHandler = new DefaultCipherHandler(SCMContext.getContext(),
keyGenerator);
}
}
public KeyGenerator getKeyGenerator()
{
return keyGenerator;
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public static CipherUtil getInstance()
{
if (instance == null)
{
synchronized (CipherUtil.class)
{
if (instance == null)
{
instance = new CipherUtil();
}
}
}
return instance;
}
//~--- methods -------------------------------------------------------------- //~--- methods --------------------------------------------------------------
@@ -53,13 +116,8 @@ public class CipherSingleton
* *
* @return * @return
*/ */
public static String decode(String value) public String decode(String value)
{ {
if (cipherHandler == null)
{
throw new CipherException("CipherSingleton is not initialized");
}
return cipherHandler.decode(value); return cipherHandler.decode(value);
} }
@@ -71,24 +129,26 @@ public class CipherSingleton
* *
* @return * @return
*/ */
public static String encode(String value) public String encode(String value)
{ {
if (cipherHandler == null)
{
throw new CipherException("CipherSingleton is not initialized");
}
return cipherHandler.encode(value); return cipherHandler.encode(value);
} }
//~--- get methods ----------------------------------------------------------
/** /**
* Method description * Method description
* *
* *
* @param handler * @return
*/ */
public static void init(CipherHandler handler) public CipherHandler getCipherHandler()
{ {
cipherHandler = handler; return cipherHandler;
} }
//~--- fields ---------------------------------------------------------------
/** Field description */
private CipherHandler cipherHandler;
} }

View File

@@ -35,9 +35,6 @@ package sonia.scm.security;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -69,8 +66,8 @@ import javax.crypto.spec.SecretKeySpec;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
* @since 1.7
*/ */
@Singleton
public class DefaultCipherHandler implements CipherHandler public class DefaultCipherHandler implements CipherHandler
{ {
@@ -117,18 +114,17 @@ public class DefaultCipherHandler implements CipherHandler
* @param keyGenerator * @param keyGenerator
* *
* *
* @throws IOException
*/ */
@Inject
public DefaultCipherHandler(SCMContextProvider context, public DefaultCipherHandler(SCMContextProvider context,
KeyGenerator keyGenerator) KeyGenerator keyGenerator)
throws IOException
{ {
File configDirectory = new File(context.getBaseDirectory(), "config"); File configDirectory = new File(context.getBaseDirectory(), "config");
IOUtil.mkdirs(configDirectory); IOUtil.mkdirs(configDirectory);
cipherKeyFile = new File(configDirectory, CIPHERKEY_FILENAME); cipherKeyFile = new File(configDirectory, CIPHERKEY_FILENAME);
try
{
if (cipherKeyFile.exists()) if (cipherKeyFile.exists())
{ {
loadKey(); loadKey();
@@ -139,6 +135,11 @@ public class DefaultCipherHandler implements CipherHandler
storeKey(); storeKey();
} }
} }
catch (IOException ex)
{
throw new CipherException("could not create CipherHandler", ex);
}
}
//~--- methods -------------------------------------------------------------- //~--- methods --------------------------------------------------------------

View File

@@ -36,6 +36,7 @@ package sonia.scm.security;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.util.UUID; import java.util.UUID;
import sonia.scm.security.KeyGenerator;
/** /**
* *

View File

@@ -35,7 +35,7 @@ package sonia.scm.xml;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import sonia.scm.security.CipherSingleton; import sonia.scm.security.CipherUtil;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
@@ -62,7 +62,7 @@ public class XmlCipherStringAdapter extends XmlAdapter<String, String>
@Override @Override
public String marshal(String v) throws Exception public String marshal(String v) throws Exception
{ {
return CipherSingleton.encode(v); return CipherUtil.getInstance().encode(v);
} }
/** /**
@@ -78,6 +78,6 @@ public class XmlCipherStringAdapter extends XmlAdapter<String, String>
@Override @Override
public String unmarshal(String v) throws Exception public String unmarshal(String v) throws Exception
{ {
return CipherSingleton.decode(v); return CipherUtil.getInstance().decode(v);
} }
} }

View File

@@ -58,7 +58,7 @@ import java.util.List;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import sonia.scm.security.CipherHandler; import sonia.scm.security.CipherHandler;
import sonia.scm.security.CipherSingleton; import sonia.scm.security.CipherUtil;
/** /**
* *
@@ -146,10 +146,6 @@ public class ScmContextListener extends GuiceServletContextListener
SCMContextProvider context = SCMContext.getContext(); SCMContextProvider context = SCMContext.getContext();
// init CipherSingleton
CipherHandler ch = injector.getInstance(CipherHandler.class);
CipherSingleton.init(ch);
// init StoreFactory // init StoreFactory
injector.getInstance(StoreFactory.class).init(context); injector.getInstance(StoreFactory.class).init(context);

View File

@@ -65,12 +65,11 @@ import sonia.scm.repository.RepositoryBrowserUtil;
import sonia.scm.repository.RepositoryManager; import sonia.scm.repository.RepositoryManager;
import sonia.scm.repository.xml.XmlRepositoryManager; import sonia.scm.repository.xml.XmlRepositoryManager;
import sonia.scm.security.CipherHandler; import sonia.scm.security.CipherHandler;
import sonia.scm.security.DefaultCipherHandler; import sonia.scm.security.CipherUtil;
import sonia.scm.security.EncryptionHandler; import sonia.scm.security.EncryptionHandler;
import sonia.scm.security.KeyGenerator; import sonia.scm.security.KeyGenerator;
import sonia.scm.security.MessageDigestEncryptionHandler; import sonia.scm.security.MessageDigestEncryptionHandler;
import sonia.scm.security.SecurityContext; import sonia.scm.security.SecurityContext;
import sonia.scm.security.UUIDKeyGenerator;
import sonia.scm.store.JAXBStoreFactory; import sonia.scm.store.JAXBStoreFactory;
import sonia.scm.store.StoreFactory; import sonia.scm.store.StoreFactory;
import sonia.scm.template.FreemarkerTemplateHandler; import sonia.scm.template.FreemarkerTemplateHandler;
@@ -196,14 +195,15 @@ public class ScmServletModule extends ServletModule
bind(SCMContextProvider.class).toInstance(context); bind(SCMContextProvider.class).toInstance(context);
ScmConfiguration config = getScmConfiguration(context); ScmConfiguration config = getScmConfiguration(context);
CipherUtil cu = CipherUtil.getInstance();
bind(StoreFactory.class).to(JAXBStoreFactory.class); bind(StoreFactory.class).to(JAXBStoreFactory.class);
bind(ScmConfiguration.class).toInstance(config); bind(ScmConfiguration.class).toInstance(config);
bind(PluginLoader.class).toInstance(pluginLoader); bind(PluginLoader.class).toInstance(pluginLoader);
bind(PluginManager.class).to(DefaultPluginManager.class); bind(PluginManager.class).to(DefaultPluginManager.class);
bind(KeyGenerator.class).to(UUIDKeyGenerator.class); bind(KeyGenerator.class).toInstance(cu.getKeyGenerator());
bind(CipherHandler.class).toInstance(cu.getCipherHandler());
bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class); bind(EncryptionHandler.class).to(MessageDigestEncryptionHandler.class);
bind(CipherHandler.class).to(DefaultCipherHandler.class);
bindExtProcessor.bindExtensions(binder()); bindExtProcessor.bindExtensions(binder());
Class<? extends FileSystem> fileSystem = Class<? extends FileSystem> fileSystem =

View File

@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
import sonia.scm.ConfigurationException; import sonia.scm.ConfigurationException;
import sonia.scm.SCMContext; import sonia.scm.SCMContext;
import sonia.scm.config.ScmConfiguration; import sonia.scm.config.ScmConfiguration;
import sonia.scm.security.CipherUtil;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
@@ -131,6 +132,13 @@ public class ScmConfigurationUtil
Unmarshaller unmarshaller = context.createUnmarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller();
ScmConfiguration loadedConfig = ScmConfiguration loadedConfig =
(ScmConfiguration) unmarshaller.unmarshal(file); (ScmConfiguration) unmarshaller.unmarshal(file);
String password = loadedConfig.getProxyPassword();
if (Util.isNotEmpty(password))
{
password = CipherUtil.getInstance().decode(password);
loadedConfig.setProxyPassword(password);
}
if (loadedConfig != null) if (loadedConfig != null)
{ {
@@ -168,10 +176,22 @@ public class ScmConfigurationUtil
IOUtil.mkdirs(file.getParentFile()); IOUtil.mkdirs(file.getParentFile());
} }
ScmConfiguration config = new ScmConfiguration();
config.load(configuration);
String password = config.getProxyPassword();
if (Util.isNotEmpty(password))
{
password = CipherUtil.getInstance().encode(password);
config.setProxyPassword(password);
}
Marshaller marshaller = context.createMarshaller(); Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(configuration, file); marshaller.marshal(config, file);
configuration.fireChangeEvent(); configuration.fireChangeEvent();
} }
catch (Exception ex) catch (Exception ex)