-
-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for EncryptableProperties #49
Comments
👍 That is indeed very interesting. It happened many times I have deployed application in shared environment and some sensitive information was exposed in the configuration files. And that was not desirable, of course. I would like to add this feature. |
@jpurnomosidi I think that it should be possible for the user to implement some encryption using the For instance: import org.aeonbits.owner.Config;
import org.aeonbits.owner.ConfigFactory;
import org.aeonbits.owner.Converter;
import sun.misc.BASE64Decoder;
import java.io.IOException;
import java.lang.reflect.Method;
/**
* To encrypt the password:
* System.out.println(new BASE64Encoder().encode(xor("tiger".getBytes(), "secret".getBytes())));
*/
public class EncryptedPropertiesExample {
interface EncryptedConfiguration extends Config {
@ConverterClass(DecryptConverter.class)
@DefaultValue("BwwEFxc=")
String scottPassword();
}
public static class DecryptConverter implements Converter {
public Object convert(Method method, String input) {
try {
String key = System.getProperty("example.encryption.key");
return new String(xor(new BASE64Decoder().decodeBuffer(input), key.getBytes()));
} catch (IOException e) {
throw new UnsupportedOperationException(e);
}
}
}
public static void main(String[] args) {
System.setProperty("example.encryption.key", "secret");
EncryptedConfiguration example = ConfigFactory.create(EncryptedConfiguration.class);
System.out.println(example.scottPassword());
}
private static byte[] xor(final byte[] input, final byte[] secret) {
final byte[] output = new byte[input.length];
if (secret.length == 0) {
throw new IllegalArgumentException("empty encryption key");
}
int spos = 0;
for (int pos = 0; pos < input.length; ++pos) {
output[pos] = (byte) (input[pos] ^ secret[spos]);
++spos;
if (spos >= secret.length) {
spos = 0;
}
}
return output;
}
} When you run the above code, it will print The above code runs fine with version 1.0.5-SNAPSHOT (on master branch), but possibly it doesn't work with version 1.0.4.1 and prior (all released versions). If you need this to work, I can backport a fix I made for 1.0.5-SNAPSHOT into 1.0.4 branch and release a bugfix release 1.0.4.2 so you can exploit this solution. Let me know. By the way, I would like to implement some strong property encryption mechanism inside owner itself; in the meantime, this solution can possibly fix urgencies? |
1.0.4.2 is ok. Btw, it's better not to use Sun base64 encoder/decoder (http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html). Better use apache common codec or javax.xml.bind.DatatypeConverter. See http://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-got-error-in-eclipse |
I know, that was just for an example ;) |
I removed the sun base64 and added commons-codec as test dependency. I will implement this feature more appropriately in the next versions. Today I released version 1.0.5 so you can use the solution I proposed as example if you update to version 1.0.5. At the moment of writing, version 1.0.5 is not yet on maven central repository yet, but it should be there soon. |
public static class EncryptedValueConverter implements Converter<String> {
@Override
public String convert(Method method, String input) {
return (PropertyValueEncryptionUtils.isEncryptedValue(input) ? PropertyValueEncryptionUtils.decrypt(input, getEncryptor()) : input);
}
private StringEncryptor getEncryptor() {
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setAlgorithm("PBEWITHMD5ANDDES");
config.setPasswordEnvName("PASSWORD");
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
standardPBEStringEncryptor.setConfig(config);
return standardPBEStringEncryptor;
}
} The code is roughly illustrative of how we've used it with Jaspyt. |
Thanks @rajatvig for the example. |
@lviggiano: @DecrypterManagerClass(SampleDecrypter.class)
public interface SampleConfig extends Config {
@Key("password")
@EncryptedKey
@DefaultValue("LEXKJVu5XG/0fSgAROlfSG+a5TfXz4HeJ/tbjUuM8cg=")
String password();
@Key("myapp.username")
String username; @DecrypterManagerClass is a simple annotation to set the class that decrypts all of the passwords in this configuration. The following code is implemented in the new code, and avoids the commons-codec library. Following is an example about using javax.crypto: import java.io.UnsupportedEncodingException;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
public class AESEncryption {
public static final String ALGORITHM = "AES";
private final String encoding;
private final byte[] keyValue;
public String getAlgorithm() {
return AESEncryption.ALGORITHM;
}
public AESEncryption( String keyValue )
throws UnsupportedEncodingException {
this ( keyValue, "UTF-8" );
}
public AESEncryption( String keyValue, String encoding )
throws UnsupportedEncodingException {
this.encoding = encoding;
this.keyValue = keyValue.getBytes( encoding );
}
public String encrypt(String plainData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal( plainData.getBytes( this.encoding ) );
String encryptedValue = DatatypeConverter.printBase64Binary( encVal );
return encryptedValue;
}
public String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance( ALGORITHM );
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = DatatypeConverter.parseBase64Binary( encryptedData );
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue, this.encoding );
return decryptedValue;
}
private Key generateKey() throws Exception {
return new SecretKeySpec( this.keyValue, this.getAlgorithm() );
}
} A simple test: import java.io.UnsupportedEncodingException;
import org.junit.Assert;
import org.junit.Test;
public class AESEncryptionTestCase {
@Test
public void encriptionDecriptionTest()
throws UnsupportedEncodingException, Exception {
String cipherKey = "ABCDEFGH12345678";
String expectedValue = "This is a decoded value";
AESEncryption aes = new AESEncryption( cipherKey );
String encriptedValue = aes.encrypt( expectedValue );
System.out.println( "Encripted=" + encriptedValue );
String decriptedValue = aes.decrypt( encriptedValue );
System.out.println( "Decripted=" + decriptedValue);
Assert.assertEquals("The decripted value is not the same as original value", expectedValue, decriptedValue);
}
} |
Encryptable properties support (Issue #49)
Owner is very cool framework in my opinion. However, how about the security of storing password in properties? Can owner support for encryptable properties like in Jasypt http://www.jasypt.org/encrypting-configuration.html ?
Thanks
The text was updated successfully, but these errors were encountered: