Hi,
I have coded up a subclass of Apache commons configuration PropertiesConfiguration that will encrypt plain text entries and re-save the property file on load up.
This get around the administrator having to know/use the jasypt CLI tools: instead the administrator of can just type the properties, and they will be encrypted/obfuscated when they are loaded by the application the first time.
It integrates with the encryptable spring property place holder well.
Hope it's useful :-)
Craig
Spring bean config:
<code>
<bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg>
<ref bean="stringEncryptor"/>
</constructor-arg>
<property name="properties">
<ref bean="properties"/>
</property>
</bean>
<bean id="stringEncryptor"
class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="config">
<ref bean="encryptorConfig"/>
</property>
</bean>
<bean id="encryptorConfig"
class="org.jasypt.encryption.pbe.config.EnvironmentPBEConfig">
<property name="passwordSysPropertyName">
<value>jasypt.hash</value>
</property>
</bean>
<bean id="properties" class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
<constructor-arg>
<ref bean="config"/>
</constructor-arg>
</bean>
<bean id="config" factory-bean="&configuration" factory-method="getConfiguration"/>
<!-- Composite configuration -->
<bean id="configuration"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<property name="configurations">
<list>
<!-- System properties -->
<bean class="org.apache.commons.configuration.SystemConfiguration"/>
<bean class="org.jasypt.commons.configuration.EncryptingPropertiesConfiguration">
<constructor-arg type="java.lang.String" value="jasypt.encrypted.properties"/>
<property name="encryptor">
<ref bean="stringEncryptor"/>
</property>
</bean>
</list>
</property>
</bean>
</code>
java file:
<code>
package org.jasypt.commons.configuration;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import org.jasypt.encryption.pbe.PBEStringEncryptor;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.ConfigurationException;
public class EncryptingPropertiesConfiguration extends PropertiesConfiguration {
private PBEStringEncryptor encryptor = null;
public EncryptingPropertiesConfiguration()
{
super();
}
/**
* Creates and loads the extended properties from the specified file.
* The specified file can contain "include = " properties which then
* are loaded and merged into the properties.
*
* @param fileName The name of the properties file to load.
* @throws ConfigurationException Error while loading the properties file
*/
public EncryptingPropertiesConfiguration(String key) throws ConfigurationException
{
super(System.getProperty(key));
}
/**
* Creates and loads the extended properties from the specified file.
* The specified file can contain "include = " properties which then
* are loaded and merged into the properties. If the file does not exist,
* an empty configuration will be created. Later the <code>save()</code>
* method can be called to save the properties to the specified file.
*
* @param file The properties file to load.
* @throws ConfigurationException Error while loading the properties file
*/
public EncryptingPropertiesConfiguration(File file) throws ConfigurationException
{
super(file);
}
/**
* Creates and loads the extended properties from the specified URL.
* The specified file can contain "include = " properties which then
* are loaded and merged into the properties.
*
* @param url The location of the properties file to load.
* @throws ConfigurationException Error while loading the properties file
*/
public EncryptingPropertiesConfiguration(URL url) throws ConfigurationException
{
super(url);
}
public void setEncryptor(PBEStringEncryptor encryptor) throws ConfigurationException {
if (encryptor == null){
return;
}
this.encryptor = encryptor;
// iterate over keys, encrypting if not and save if
// any changed
Iterator i = getKeys();
ArrayList keys = new ArrayList();
boolean changed = false;
while(i.hasNext()){
keys.add(i.next());
}
i = keys.iterator();
while(i.hasNext()){
String key = (String)i.next();
Object property = getProperty(key);
if (property instanceof String){
String string = (String)property;
if (!(string.startsWith("ENC(") && string.endsWith(")"))){
setProperty(key,"ENC("+encryptor.encrypt(string)+")");
changed = true;
}
}
}
if (changed){
save();
}
}
}
</code>