mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor ConfigFilter
This commit is contained in:
@@ -73,7 +73,7 @@ public class ConfigFilterTest extends ConfigFileGenerator {
|
||||
dataSource.setUsername("test");
|
||||
dataSource.setPassword(encryptedString);
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=RSA");
|
||||
dataSource.setConnectionProperties("config.decrypt=true");
|
||||
|
||||
try {
|
||||
dataSource.init();
|
||||
@@ -84,32 +84,11 @@ public class ConfigFilterTest extends ConfigFileGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitInvalidDecrypt() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setDriver(MockDriver.instance);
|
||||
dataSource.setUrl("");
|
||||
dataSource.setUsername("test");
|
||||
dataSource.setPassword(encryptedString);
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=ABCDEF");
|
||||
|
||||
Exception error = null;
|
||||
try {
|
||||
dataSource.init();
|
||||
} catch (IllegalArgumentException e) {
|
||||
error = e;
|
||||
} finally {
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
Assert.assertNotNull(error);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitRemoteConfigAndDecrypt() throws SQLException {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setFilters("config");
|
||||
dataSource.setConnectionProperties("config.decrypt=RSA;config.file=" + "file://" + this.filePath);
|
||||
dataSource.setConnectionProperties("config.decrypt=true;config.file=" + "file://" + this.filePath);
|
||||
try {
|
||||
dataSource.init();
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.druid.filter.config.security.decrypter.DecryptException;
|
||||
import com.alibaba.druid.filter.config.security.decrypter.DecrypterFactory;
|
||||
import com.alibaba.druid.filter.config.security.decrypter.RsaDecrypter;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class DecrypterFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testGetDecrypter() throws DecryptException {
|
||||
Assert.assertNotNull(DecrypterFactory.getDecrypter("RSA"));
|
||||
Assert.assertEquals(DecrypterFactory.getDecrypter("RSA").getClass(), RsaDecrypter.class);
|
||||
|
||||
Assert.assertNull(DecrypterFactory.getDecrypter("ajfdjksfklds;"));
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
package com.alibaba.druid.bvt.filter.config;
|
||||
|
||||
import com.alibaba.druid.filter.config.security.decrypter.DecryptException;
|
||||
import com.alibaba.druid.filter.config.security.decrypter.RsaDecrypter;
|
||||
import com.alibaba.druid.filter.config.security.decrypter.SensitiveParameters;
|
||||
import com.alibaba.druid.util.Base64;
|
||||
import junit.framework.Assert;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.security.PublicKey;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author Jonas Yang
|
||||
*/
|
||||
public class RsaDecrypterTest {
|
||||
String publicKeyContent = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2YdTlkEw7JfRNItCosRf4QO8vOL54ZEdj0LZm" +
|
||||
"tKhNFnNRTtHrLDFT44BPYqFSLVv3TOhLxJcEs31kyQhXEC5b9ozpZBvYrJDSt8QQMoYDAMLSojs+" +
|
||||
"K4vTJavgbRKKads0di+DuoMmJ5g5dcbDNd1Kb4kgWhrZtUwhfSjQklaiIwIDAQAB";
|
||||
|
||||
String encryptedString = "Zaa9qpyB9snnBUz7UBvcaHcNfWMKEWvhnx0dbWmwxBOJ9vkeZNXjV9MEJezSzL+6yQh7aO29vD67" +
|
||||
"qedtD9ublVwwf+jTPuO1NjzAKy0+yWVQ1KdR2KaQuvCxFWiM1n6nbpkUGB6OVpoQgFnFzdKlyC9D" +
|
||||
"KU3Q6evGa1hlZ8jnHcA=";
|
||||
|
||||
String keyFilePath;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
File tmp = File.createTempFile("druid_public_key", Long.toString(System.currentTimeMillis()));
|
||||
byte[] keyContent = Base64.base64ToByteArray(this.publicKeyContent);
|
||||
|
||||
FileOutputStream out = null;
|
||||
try {
|
||||
out = new FileOutputStream(tmp);
|
||||
out.write(keyContent);
|
||||
} finally {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
this.keyFilePath = tmp.getAbsolutePath();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
File tmp = new File(this.keyFilePath);
|
||||
if (tmp.exists()) {
|
||||
tmp.delete();
|
||||
}
|
||||
|
||||
this.keyFilePath = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKeyByString() {
|
||||
RsaDecrypter rsaDecrypter = new RsaDecrypter();
|
||||
PublicKey key = rsaDecrypter.getPublicKeyByString(this.publicKeyContent);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
|
||||
key = rsaDecrypter.getPublicKeyByString(null);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
try {
|
||||
key = rsaDecrypter.getPublicKeyByString("abcdef");
|
||||
Assert.assertTrue("It is here", false);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKeyByPublicKeyFile() {
|
||||
RsaDecrypter rsaDecrypter = new RsaDecrypter();
|
||||
PublicKey key = rsaDecrypter.getPublicKeyByPublicKeyFile(this.keyFilePath);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
|
||||
key = rsaDecrypter.getPublicKeyByPublicKeyFile(null);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
try {
|
||||
key = rsaDecrypter.getPublicKeyByPublicKeyFile("abcdef");
|
||||
Assert.assertTrue("It is here", false);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKeyByX509File() {
|
||||
//No test
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPublicKey() {
|
||||
RsaDecrypter rsaDecrypter = new RsaDecrypter();
|
||||
PublicKey key = rsaDecrypter.getPublicKey(null);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(RsaDecrypter.DEFAULT_PUBLIC_KEY_STRING, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
Properties info = new Properties();
|
||||
info.put(RsaDecrypter.KEY, this.publicKeyContent);
|
||||
key = rsaDecrypter.getPublicKey(info);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(this.publicKeyContent, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
|
||||
info = new Properties();
|
||||
info.put(RsaDecrypter.KEY_FILE, this.keyFilePath);
|
||||
key = rsaDecrypter.getPublicKey(info);
|
||||
Assert.assertNotNull("It is null", key);
|
||||
Assert.assertEquals(this.publicKeyContent, Base64.byteArrayToBase64(key.getEncoded()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecrypt() throws DecryptException {
|
||||
RsaDecrypter decrypter = new RsaDecrypter();
|
||||
Properties info = new Properties();
|
||||
info.put(RsaDecrypter.KEY_FILE, this.keyFilePath);
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", encryptedString), info);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithEmptyKey() throws DecryptException {
|
||||
RsaDecrypter decrypter = new RsaDecrypter();
|
||||
SensitiveParameters parameters = decrypter.decrypt(new SensitiveParameters("", "", "OJfUm6WCHi7EuXqE6aEc+Po2xFrAGBeSNy8O2jWhV2FTG8/5kbRRr2rjNKhptlevm/03Y0048P7h88gdUOXAYg=="), (Properties) null);
|
||||
|
||||
Assert.assertEquals("It is not same.", "xiaoyu", parameters.getPassword());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.alibaba.druid.filter.config;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -32,11 +34,7 @@ public class ConfigFileGenerator {
|
||||
} catch (IOException e) {
|
||||
Assert.assertNull("Failed to init resource.", e);
|
||||
} finally {
|
||||
if (out != null) {
|
||||
try {
|
||||
out.close();
|
||||
} catch(Exception e){}
|
||||
}
|
||||
JdbcUtils.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user