未验证 提交 a0eb7411 编写于 作者: L Liang Zhang 提交者: GitHub

Merge pull request #2775 from tristaZero/dev

Add jdbc tests for encryptiion
......@@ -182,6 +182,20 @@ public final class EncryptRule implements BaseRule {
return tables.get(logicTable).getCipherColumns();
}
/**
* Is cipher column or not.
*
* @param columnName column name
* @return cipher column or not
*/
public boolean isCipherColumn(final String columnName) {
Collection<String> result = new LinkedList<>();
for (EncryptTable each : tables.values()) {
result.addAll(each.getCipherColumns());
}
return result.contains(columnName);
}
/**
* Get assisted query column.
*
......
......@@ -130,9 +130,7 @@ public final class EncryptTable {
public Collection<String> getCipherColumns() {
Collection<String> result = new LinkedList<>();
for (EncryptColumn each : columns.values()) {
if (each.getPlainColumn().isPresent()) {
result.add(each.getCipherColumn());
}
result.add(each.getCipherColumn());
}
return result;
}
......
......@@ -139,7 +139,8 @@ public final class QueryResultMetaData {
*/
@SneakyThrows
public Optional<ShardingEncryptor> getShardingEncryptor(final int columnIndex) {
return encryptRule.getShardingEncryptor(getTableName(columnIndex), resultSetMetaData.getColumnName(columnIndex));
String logicTable = getTableName(columnIndex);
return encryptRule.getShardingEncryptor(logicTable, getLogicColumn(logicTable, columnIndex));
}
private String getTableName(final int columnIndex) throws SQLException {
......@@ -150,4 +151,10 @@ public final class QueryResultMetaData {
Optional<TableRule> tableRule = shardingRule.findTableRuleByActualTable(actualTableName);
return tableRule.isPresent() ? tableRule.get().getLogicTable() : actualTableName;
}
@SneakyThrows
private String getLogicColumn(final String tableName, final int columnIndex) {
String columnLabel = resultSetMetaData.getColumnName(columnIndex);
return encryptRule.isCipherColumn(columnLabel) ? encryptRule.getLogicColumn(tableName, columnLabel) : columnLabel;
}
}
......@@ -55,11 +55,12 @@ public final class EncryptInsertColumns implements InsertColumns {
Collection<String> result = new LinkedHashSet<>(allColumnNames.size() - assistedQueryAndPlainColumnNames.size());
String tableName = insertStatement.getTable().getTableName();
for (String each : allColumnNames) {
if (!isAssistedQueryAndPlainColumns(each)) {
result.add(each);
}
if (isCipherColumn(encryptRule, tableName, each)) {
result.add(getLogicColumn(encryptRule, tableName, each));
continue;
}
if (!isAssistedQueryAndPlainColumns(each)) {
result.add(each);
}
}
return result;
......
......@@ -58,11 +58,12 @@ public final class ShardingInsertColumns implements InsertColumns {
Collection<String> result = new LinkedHashSet<>(allColumnNames.size() - assistedQueryAndPlainColumnNames.size());
String tableName = insertStatement.getTable().getTableName();
for (String each : allColumnNames) {
if (!isAssistedQueryAndPlainColumns(each)) {
result.add(each);
}
if (isCipherColumn(encryptRule, tableName, each)) {
result.add(getLogicColumn(encryptRule, tableName, each));
continue;
}
if (!isAssistedQueryAndPlainColumns(each)) {
result.add(each);
}
}
if (isGenerateKeyFromMetaData(allColumnNames, insertStatement.getValueSize())) {
......
......@@ -44,17 +44,25 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
private static EncryptDataSource encryptDataSource;
private static EncryptDataSource encryptDataSourceWithProps;
private static final List<String> ENCRYPT_DB_NAMES = Collections.singletonList("encrypt");
@BeforeClass
public static void initEncryptDataSource() throws SQLException {
if (null != encryptDataSource) {
if (null != encryptDataSource && null != encryptDataSourceWithProps) {
return;
}
Map<String, DataSource> dataSources = getDataSources();
Properties props = new Properties();
props.put(ShardingPropertiesConstant.SQL_SHOW.getKey(), true);
encryptDataSource = new EncryptDataSource(dataSources.values().iterator().next(), new EncryptRule(createEncryptRuleConfiguration()), props);
encryptDataSource = new EncryptDataSource(dataSources.values().iterator().next(), new EncryptRule(createEncryptRuleConfiguration()), new Properties());
encryptDataSourceWithProps = new EncryptDataSource(dataSources.values().iterator().next(), new EncryptRule(createEncryptRuleConfiguration()), createProperties());
}
private static Properties createProperties() {
Properties result = new Properties();
result.put(ShardingPropertiesConstant.SQL_SHOW.getKey(), true);
result.put(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN, false);
return result;
}
private static Map<String, DataSource> getDataSources() {
......@@ -70,9 +78,9 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
private static EncryptRuleConfiguration createEncryptRuleConfiguration() {
EncryptorRuleConfiguration encryptorConfig = new EncryptorRuleConfiguration("test", new Properties());
EncryptorRuleConfiguration encryptorQueryConfig = new EncryptorRuleConfiguration("assistedTest", new Properties());
EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("", "pwd", "", "test");
EncryptColumnRuleConfiguration columnConfig1 = new EncryptColumnRuleConfiguration("plain_pwd", "cipher_pwd", "", "test");
EncryptTableRuleConfiguration tableConfig1 = new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnConfig1));
EncryptColumnRuleConfiguration columnConfig2 = new EncryptColumnRuleConfiguration("", "pwd", "assist_pwd", "assistedTest");
EncryptColumnRuleConfiguration columnConfig2 = new EncryptColumnRuleConfiguration("", "cipher_pwd", "assist_pwd", "assistedTest");
EncryptTableRuleConfiguration tableConfig2 = new EncryptTableRuleConfiguration(Collections.singletonMap("pwd", columnConfig2));
EncryptRuleConfiguration result = new EncryptRuleConfiguration();
result.getEncryptors().put("test", encryptorConfig);
......@@ -92,9 +100,13 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
ex.printStackTrace();
}
}
protected final EncryptDataSource getEncryptDataSource() {
return encryptDataSource;
protected final EncryptConnection getEncryptConnection() throws SQLException {
return encryptDataSource.getConnection();
}
protected final EncryptConnection getEncryptConnectionWithProps() throws SQLException {
return encryptDataSourceWithProps.getConnection();
}
@AfterClass
......
......@@ -19,13 +19,12 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.shardingjdbc.common.base.AbstractEncryptJDBCDatabaseAndTableTest;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
......@@ -46,23 +45,16 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
private static final String SELECT_SQL = "select * from t_query_encrypt where pwd = ? ";
private static final String SELECT_ALL_SQL = "select id, pwd, assist_pwd from t_query_encrypt";
private EncryptConnection encryptConnection;
@Before
public void setUp() throws SQLException {
encryptConnection = getEncryptDataSource().getConnection();
}
private static final String SELECT_ALL_SQL = "select id, cipher_pwd, assist_pwd from t_query_encrypt";
@Test
public void assertSqlShow() {
assertTrue(encryptConnection.getRuntimeContext().getProps().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
public void assertSqlShow() throws SQLException {
assertTrue(getEncryptConnectionWithProps().getRuntimeContext().getProps().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
}
@Test
public void assertInsertWithExecute() throws SQLException {
try (PreparedStatement statement = encryptConnection.prepareStatement(INSERT_SQL)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(INSERT_SQL)) {
statement.setObject(1, 2);
statement.setObject(2, 'b');
statement.execute();
......@@ -72,7 +64,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
public void assertInsertWithBatchExecute() throws SQLException {
try (PreparedStatement statement = encryptConnection.prepareStatement(INSERT_SQL)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(INSERT_SQL)) {
statement.setObject(1, 3);
statement.setObject(2, 'c');
statement.addBatch();
......@@ -86,7 +78,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
public void assertInsertWithExecuteWithGeneratedKey() throws SQLException {
try (PreparedStatement statement = encryptConnection.prepareStatement(INSERT_GENERATED_KEY_SQL, Statement.RETURN_GENERATED_KEYS)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(INSERT_GENERATED_KEY_SQL, Statement.RETURN_GENERATED_KEYS)) {
statement.execute();
ResultSet resultSet = statement.getGeneratedKeys();
assertTrue(resultSet.next());
......@@ -98,7 +90,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
public void assertDeleteWithExecute() throws SQLException {
try (PreparedStatement statement = encryptConnection.prepareStatement(DELETE_SQL)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(DELETE_SQL)) {
statement.setObject(1, 'a');
statement.setObject(2, 1);
statement.execute();
......@@ -109,7 +101,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
public void assertUpdateWithExecuteUpdate() throws SQLException {
int result;
try (PreparedStatement statement = encryptConnection.prepareStatement(UPDATE_SQL)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(UPDATE_SQL)) {
statement.setObject(1, 'f');
statement.setObject(2, 'a');
result = statement.executeUpdate();
......@@ -120,7 +112,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
public void assertSelectWithExecuteQuery() throws SQLException {
try (PreparedStatement statement = encryptConnection.prepareStatement(SELECT_SQL)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(SELECT_SQL)) {
statement.setObject(1, 'a');
ResultSet resultSet = statement.executeQuery();
assertTrue(resultSet.next());
......@@ -132,9 +124,22 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
}
}
@Test
public void assertSelectWithMetaData() throws SQLException {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(SELECT_SQL)) {
statement.setObject(1, 'a');
ResultSetMetaData metaData = statement.executeQuery().getMetaData();
assertThat(metaData.getColumnCount(), is(2));
for (int i = 0; i < metaData.getColumnCount(); i++) {
assertThat(metaData.getColumnLabel(1), is("id"));
assertThat(metaData.getColumnLabel(2), is("pwd"));
}
}
}
@Test
public void assertSelectWithExecuteWithProperties() throws SQLException {
try (PreparedStatement statement = encryptConnection.prepareStatement(SELECT_ALL_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
try (PreparedStatement statement = getEncryptConnection().prepareStatement(SELECT_ALL_SQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
Boolean result = statement.execute();
assertTrue(result);
assertThat(statement.getResultSetType(), is(ResultSet.TYPE_FORWARD_ONLY));
......@@ -150,7 +155,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
int count = 1;
while (resultSet.next()) {
if (id == count) {
assertThat(pwd, is(resultSet.getObject("pwd")));
assertThat(pwd, is(resultSet.getObject("cipher_pwd")));
assertThat(assistPwd, is(resultSet.getObject("assist_pwd")));
}
count += 1;
......
......@@ -19,14 +19,15 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.shardingjdbc.common.base.AbstractEncryptJDBCDatabaseAndTableTest;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
......@@ -43,63 +44,60 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
private static final String UPDATE_SQL = "update t_encrypt set pwd ='f' where pwd = 'a'";
private static final String SELECT_SQL = "select * from t_encrypt where pwd = 'a' ";
private static final String SELECT_SQL = "select id, pwd from t_encrypt where pwd = 'a'";
private static final String SELECT_ALL_SQL = "select id, pwd from t_encrypt";
private static final String SELECT_SQL_WITH_STAR = "select * from t_encrypt where pwd = 'a'";
private EncryptConnection encryptConnection;
private static final String SELECT_SQL_WITH_PLAIN = "select id, pwd from t_encrypt where pwd = 'plainValue'";
@Before
public void setUp() throws SQLException {
encryptConnection = getEncryptDataSource().getConnection();
}
private static final String SELECT_SQL_TO_ASSERT = "select id, cipher_pwd, plain_pwd from t_encrypt";
@Test
public void assertSqlShow() {
assertTrue(encryptConnection.getRuntimeContext().getProps().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
public void assertSqlShow() throws SQLException {
assertTrue(getEncryptConnectionWithProps().getRuntimeContext().getProps().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
}
@Test
public void assertInsertWithExecute() throws SQLException {
try (Statement statement = encryptConnection.createStatement()) {
try (Statement statement = getEncryptConnection().createStatement()) {
statement.execute(INSERT_SQL);
}
assertResultSet(3, 2, "encryptValue");
assertResultSet(3, 2, "encryptValue", "b");
}
@Test
public void assertInsertWithExecuteWithGeneratedKey() throws SQLException {
try (Statement statement = encryptConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
try (Statement statement = getEncryptConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
statement.execute(INSERT_GENERATED_KEY_SQL, Statement.RETURN_GENERATED_KEYS);
ResultSet resultSet = statement.getGeneratedKeys();
assertTrue(resultSet.next());
assertThat(resultSet.getInt(1), is(6));
assertFalse(resultSet.next());
}
assertResultSet(3, 2, "encryptValue");
assertResultSet(3, 6, "encryptValue", "b");
}
@Test
public void assertDeleteWithExecute() throws SQLException {
try (Statement statement = encryptConnection.createStatement()) {
try (Statement statement = getEncryptConnection().createStatement()) {
statement.execute(DELETE_SQL);
}
assertResultSet(1, 5, "encryptValue");
assertResultSet(1, 5, "encryptValue", "b");
}
@Test
public void assertUpdateWithExecuteUpdate() throws SQLException {
int result;
try (Statement statement = encryptConnection.createStatement()) {
try (Statement statement = getEncryptConnection().createStatement()) {
result = statement.executeUpdate(UPDATE_SQL);
}
assertThat(result, is(2));
assertResultSet(2, 1, "encryptValue");
assertResultSet(2, 1, "encryptValue", "f");
}
@Test
public void assertSelectWithExecuteQuery() throws SQLException {
try (Statement statement = encryptConnection.createStatement()) {
try (Statement statement = getEncryptConnection().createStatement()) {
ResultSet resultSet = statement.executeQuery(SELECT_SQL);
assertTrue(resultSet.next());
assertThat(resultSet.getInt(1), is(1));
......@@ -112,7 +110,7 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
@Test
public void assertSelectWithExecuteWithProperties() throws SQLException {
try (Statement statement = encryptConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
try (Statement statement = getEncryptConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT)) {
int[] columnIndexes = {1, 2};
Boolean result = statement.execute(SELECT_SQL, columnIndexes);
assertTrue(result);
......@@ -122,14 +120,43 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
}
}
private void assertResultSet(final int resultSetCount, final int id, final Object pwd) throws SQLException {
@Test
public void assertSelectWithMetaData() throws SQLException {
try (Statement statement = getEncryptConnection().createStatement()) {
ResultSetMetaData metaData = statement.executeQuery(SELECT_SQL_WITH_STAR).getMetaData();
assertThat(metaData.getColumnCount(), is(3));
for (int i = 0; i < metaData.getColumnCount(); i++) {
assertThat(metaData.getColumnLabel(1), is("id"));
assertThat(metaData.getColumnLabel(2), is("pwd"));
assertThat(metaData.getColumnLabel(3), is("plain_pwd"));
}
}
}
@Test
public void assertSelectWithPlainColumn() throws SQLException {
try (Statement statement = getEncryptConnectionWithProps().createStatement()) {
ResultSet resultSet = statement.executeQuery(SELECT_SQL_WITH_PLAIN);
int count = 1;
List<Object> ids = Arrays.asList((Object) 1, 5);
while (resultSet.next()) {
assertThat(resultSet.getObject("id"), is(ids.get(count - 1)));
assertThat(resultSet.getObject("pwd"), is((Object) "decryptValue"));
count += 1;
}
assertThat(count - 1, is(ids.size()));
}
}
private void assertResultSet(final int resultSetCount, final int id, final Object pwd, final Object plain) throws SQLException {
try (Connection conn = getDatabaseTypeMap().values().iterator().next().values().iterator().next().getConnection();
Statement stmt = conn.createStatement()) {
ResultSet resultSet = stmt.executeQuery(SELECT_ALL_SQL);
ResultSet resultSet = stmt.executeQuery(SELECT_SQL_TO_ASSERT);
int count = 1;
while (resultSet.next()) {
if (id == count) {
assertThat(pwd, is(resultSet.getObject("pwd")));
assertThat(resultSet.getObject("cipher_pwd"), is(pwd));
assertThat(resultSet.getObject("plain_pwd"), is(plain));
}
count += 1;
}
......
......@@ -17,7 +17,7 @@
DELETE FROM t_encrypt;
DELETE FROM t_query_encrypt;
INSERT INTO t_encrypt VALUES(1, 'encryptValue');
INSERT INTO t_encrypt VALUES(5, 'encryptValue');
INSERT INTO t_query_encrypt VALUES(1, 'encryptValue');
INSERT INTO t_query_encrypt VALUES(5, 'encryptValue');
\ No newline at end of file
INSERT INTO t_encrypt VALUES(1, 'plainValue');
INSERT INTO t_encrypt VALUES(5, 'plainValue');
INSERT INTO t_query_encrypt VALUES(1, 'plainValue');
INSERT INTO t_query_encrypt VALUES(5, 'plainValue');
\ No newline at end of file
......@@ -21,5 +21,5 @@ CREATE TABLE IF NOT EXISTS t_order_item_0 (item_id INT NOT NULL, order_id INT NO
CREATE TABLE IF NOT EXISTS t_order_item_1 (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (item_id));
CREATE TABLE IF NOT EXISTS t_config (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS t_global (id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS t_encrypt (id INT NOT NULL AUTO_INCREMENT, pwd VARCHAR(45) NULL, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS t_query_encrypt (id INT NOT NULL AUTO_INCREMENT, pwd VARCHAR(45) NULL, assist_pwd VARCHAR(45) NULL, PRIMARY KEY (id));
\ No newline at end of file
CREATE TABLE IF NOT EXISTS t_encrypt (id INT NOT NULL AUTO_INCREMENT, cipher_pwd VARCHAR(45) NULL, plain_pwd VARCHAR(45), PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS t_query_encrypt (id INT NOT NULL AUTO_INCREMENT, cipher_pwd VARCHAR(45) NULL, assist_pwd VARCHAR(45) NULL, PRIMARY KEY (id));
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册