Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
a0eb7411
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
a0eb7411
编写于
7月 29, 2019
作者:
L
Liang Zhang
提交者:
GitHub
7月 29, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2775 from tristaZero/dev
Add jdbc tests for encryptiion
上级
501f55ea
0cfced15
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
134 addition
and
69 deletion
+134
-69
sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java
...java/org/apache/shardingsphere/core/rule/EncryptRule.java
+14
-0
sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/encrypt/EncryptTable.java
...he/shardingsphere/core/strategy/encrypt/EncryptTable.java
+1
-3
sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultMetaData.java
.../core/execute/sql/execute/result/QueryResultMetaData.java
+8
-1
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/encrypt/segment/EncryptInsertColumns.java
...e/core/optimize/encrypt/segment/EncryptInsertColumns.java
+4
-3
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/segment/insert/ShardingInsertColumns.java
...timize/sharding/segment/insert/ShardingInsertColumns.java
+4
-3
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/common/base/AbstractEncryptJDBCDatabaseAndTableTest.java
.../common/base/AbstractEncryptJDBCDatabaseAndTableTest.java
+21
-9
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatementTest.java
...dbc/jdbc/core/statement/EncryptPreparedStatementTest.java
+25
-20
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatementTest.java
...hardingjdbc/jdbc/core/statement/EncryptStatementTest.java
+51
-24
sharding-jdbc/sharding-jdbc-core/src/test/resources/encrypt_data.sql
...bc/sharding-jdbc-core/src/test/resources/encrypt_data.sql
+4
-4
sharding-jdbc/sharding-jdbc-core/src/test/resources/jdbc_init.sql
...-jdbc/sharding-jdbc-core/src/test/resources/jdbc_init.sql
+2
-2
未找到文件。
sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java
浏览文件 @
a0eb7411
...
@@ -182,6 +182,20 @@ public final class EncryptRule implements BaseRule {
...
@@ -182,6 +182,20 @@ public final class EncryptRule implements BaseRule {
return
tables
.
get
(
logicTable
).
getCipherColumns
();
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.
* Get assisted query column.
*
*
...
...
sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/strategy/encrypt/EncryptTable.java
浏览文件 @
a0eb7411
...
@@ -130,9 +130,7 @@ public final class EncryptTable {
...
@@ -130,9 +130,7 @@ public final class EncryptTable {
public
Collection
<
String
>
getCipherColumns
()
{
public
Collection
<
String
>
getCipherColumns
()
{
Collection
<
String
>
result
=
new
LinkedList
<>();
Collection
<
String
>
result
=
new
LinkedList
<>();
for
(
EncryptColumn
each
:
columns
.
values
())
{
for
(
EncryptColumn
each
:
columns
.
values
())
{
if
(
each
.
getPlainColumn
().
isPresent
())
{
result
.
add
(
each
.
getCipherColumn
());
result
.
add
(
each
.
getCipherColumn
());
}
}
}
return
result
;
return
result
;
}
}
...
...
sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/result/QueryResultMetaData.java
浏览文件 @
a0eb7411
...
@@ -139,7 +139,8 @@ public final class QueryResultMetaData {
...
@@ -139,7 +139,8 @@ public final class QueryResultMetaData {
*/
*/
@SneakyThrows
@SneakyThrows
public
Optional
<
ShardingEncryptor
>
getShardingEncryptor
(
final
int
columnIndex
)
{
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
{
private
String
getTableName
(
final
int
columnIndex
)
throws
SQLException
{
...
@@ -150,4 +151,10 @@ public final class QueryResultMetaData {
...
@@ -150,4 +151,10 @@ public final class QueryResultMetaData {
Optional
<
TableRule
>
tableRule
=
shardingRule
.
findTableRuleByActualTable
(
actualTableName
);
Optional
<
TableRule
>
tableRule
=
shardingRule
.
findTableRuleByActualTable
(
actualTableName
);
return
tableRule
.
isPresent
()
?
tableRule
.
get
().
getLogicTable
()
:
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
;
}
}
}
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/encrypt/segment/EncryptInsertColumns.java
浏览文件 @
a0eb7411
...
@@ -55,11 +55,12 @@ public final class EncryptInsertColumns implements InsertColumns {
...
@@ -55,11 +55,12 @@ public final class EncryptInsertColumns implements InsertColumns {
Collection
<
String
>
result
=
new
LinkedHashSet
<>(
allColumnNames
.
size
()
-
assistedQueryAndPlainColumnNames
.
size
());
Collection
<
String
>
result
=
new
LinkedHashSet
<>(
allColumnNames
.
size
()
-
assistedQueryAndPlainColumnNames
.
size
());
String
tableName
=
insertStatement
.
getTable
().
getTableName
();
String
tableName
=
insertStatement
.
getTable
().
getTableName
();
for
(
String
each
:
allColumnNames
)
{
for
(
String
each
:
allColumnNames
)
{
if
(!
isAssistedQueryAndPlainColumns
(
each
))
{
result
.
add
(
each
);
}
if
(
isCipherColumn
(
encryptRule
,
tableName
,
each
))
{
if
(
isCipherColumn
(
encryptRule
,
tableName
,
each
))
{
result
.
add
(
getLogicColumn
(
encryptRule
,
tableName
,
each
));
result
.
add
(
getLogicColumn
(
encryptRule
,
tableName
,
each
));
continue
;
}
if
(!
isAssistedQueryAndPlainColumns
(
each
))
{
result
.
add
(
each
);
}
}
}
}
return
result
;
return
result
;
...
...
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/segment/insert/ShardingInsertColumns.java
浏览文件 @
a0eb7411
...
@@ -58,11 +58,12 @@ public final class ShardingInsertColumns implements InsertColumns {
...
@@ -58,11 +58,12 @@ public final class ShardingInsertColumns implements InsertColumns {
Collection
<
String
>
result
=
new
LinkedHashSet
<>(
allColumnNames
.
size
()
-
assistedQueryAndPlainColumnNames
.
size
());
Collection
<
String
>
result
=
new
LinkedHashSet
<>(
allColumnNames
.
size
()
-
assistedQueryAndPlainColumnNames
.
size
());
String
tableName
=
insertStatement
.
getTable
().
getTableName
();
String
tableName
=
insertStatement
.
getTable
().
getTableName
();
for
(
String
each
:
allColumnNames
)
{
for
(
String
each
:
allColumnNames
)
{
if
(!
isAssistedQueryAndPlainColumns
(
each
))
{
result
.
add
(
each
);
}
if
(
isCipherColumn
(
encryptRule
,
tableName
,
each
))
{
if
(
isCipherColumn
(
encryptRule
,
tableName
,
each
))
{
result
.
add
(
getLogicColumn
(
encryptRule
,
tableName
,
each
));
result
.
add
(
getLogicColumn
(
encryptRule
,
tableName
,
each
));
continue
;
}
if
(!
isAssistedQueryAndPlainColumns
(
each
))
{
result
.
add
(
each
);
}
}
}
}
if
(
isGenerateKeyFromMetaData
(
allColumnNames
,
insertStatement
.
getValueSize
()))
{
if
(
isGenerateKeyFromMetaData
(
allColumnNames
,
insertStatement
.
getValueSize
()))
{
...
...
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/common/base/AbstractEncryptJDBCDatabaseAndTableTest.java
浏览文件 @
a0eb7411
...
@@ -44,17 +44,25 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
...
@@ -44,17 +44,25 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
private
static
EncryptDataSource
encryptDataSource
;
private
static
EncryptDataSource
encryptDataSource
;
private
static
EncryptDataSource
encryptDataSourceWithProps
;
private
static
final
List
<
String
>
ENCRYPT_DB_NAMES
=
Collections
.
singletonList
(
"encrypt"
);
private
static
final
List
<
String
>
ENCRYPT_DB_NAMES
=
Collections
.
singletonList
(
"encrypt"
);
@BeforeClass
@BeforeClass
public
static
void
initEncryptDataSource
()
throws
SQLException
{
public
static
void
initEncryptDataSource
()
throws
SQLException
{
if
(
null
!=
encryptDataSource
)
{
if
(
null
!=
encryptDataSource
&&
null
!=
encryptDataSourceWithProps
)
{
return
;
return
;
}
}
Map
<
String
,
DataSource
>
dataSources
=
getDataSources
();
Map
<
String
,
DataSource
>
dataSources
=
getDataSources
();
Properties
props
=
new
Properties
();
encryptDataSource
=
new
EncryptDataSource
(
dataSources
.
values
().
iterator
().
next
(),
new
EncryptRule
(
createEncryptRuleConfiguration
()),
new
Properties
());
props
.
put
(
ShardingPropertiesConstant
.
SQL_SHOW
.
getKey
(),
true
);
encryptDataSourceWithProps
=
new
EncryptDataSource
(
dataSources
.
values
().
iterator
().
next
(),
new
EncryptRule
(
createEncryptRuleConfiguration
()),
createProperties
());
encryptDataSource
=
new
EncryptDataSource
(
dataSources
.
values
().
iterator
().
next
(),
new
EncryptRule
(
createEncryptRuleConfiguration
()),
props
);
}
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
()
{
private
static
Map
<
String
,
DataSource
>
getDataSources
()
{
...
@@ -70,9 +78,9 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
...
@@ -70,9 +78,9 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
private
static
EncryptRuleConfiguration
createEncryptRuleConfiguration
()
{
private
static
EncryptRuleConfiguration
createEncryptRuleConfiguration
()
{
EncryptorRuleConfiguration
encryptorConfig
=
new
EncryptorRuleConfiguration
(
"test"
,
new
Properties
());
EncryptorRuleConfiguration
encryptorConfig
=
new
EncryptorRuleConfiguration
(
"test"
,
new
Properties
());
EncryptorRuleConfiguration
encryptorQueryConfig
=
new
EncryptorRuleConfiguration
(
"assistedTest"
,
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
));
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
));
EncryptTableRuleConfiguration
tableConfig2
=
new
EncryptTableRuleConfiguration
(
Collections
.
singletonMap
(
"pwd"
,
columnConfig2
));
EncryptRuleConfiguration
result
=
new
EncryptRuleConfiguration
();
EncryptRuleConfiguration
result
=
new
EncryptRuleConfiguration
();
result
.
getEncryptors
().
put
(
"test"
,
encryptorConfig
);
result
.
getEncryptors
().
put
(
"test"
,
encryptorConfig
);
...
@@ -92,9 +100,13 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
...
@@ -92,9 +100,13 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
ex
.
printStackTrace
();
ex
.
printStackTrace
();
}
}
}
}
protected
final
EncryptDataSource
getEncryptDataSource
()
{
protected
final
EncryptConnection
getEncryptConnection
()
throws
SQLException
{
return
encryptDataSource
;
return
encryptDataSource
.
getConnection
();
}
protected
final
EncryptConnection
getEncryptConnectionWithProps
()
throws
SQLException
{
return
encryptDataSourceWithProps
.
getConnection
();
}
}
@AfterClass
@AfterClass
...
...
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatementTest.java
浏览文件 @
a0eb7411
...
@@ -19,13 +19,12 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
...
@@ -19,13 +19,12 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.shardingjdbc.common.base.AbstractEncryptJDBCDatabaseAndTableTest
;
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
org.junit.Test
;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.Statement
;
...
@@ -46,23 +45,16 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -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_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
static
final
String
SELECT_ALL_SQL
=
"select id, cipher_pwd, assist_pwd from t_query_encrypt"
;
private
EncryptConnection
encryptConnection
;
@Before
public
void
setUp
()
throws
SQLException
{
encryptConnection
=
getEncryptDataSource
().
getConnection
();
}
@Test
@Test
public
void
assertSqlShow
()
{
public
void
assertSqlShow
()
throws
SQLException
{
assertTrue
(
encryptConnection
.
getRuntimeContext
().
getProps
().<
Boolean
>
getValue
(
ShardingPropertiesConstant
.
SQL_SHOW
));
assertTrue
(
getEncryptConnectionWithProps
()
.
getRuntimeContext
().
getProps
().<
Boolean
>
getValue
(
ShardingPropertiesConstant
.
SQL_SHOW
));
}
}
@Test
@Test
public
void
assertInsertWithExecute
()
throws
SQLException
{
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
(
1
,
2
);
statement
.
setObject
(
2
,
'b'
);
statement
.
setObject
(
2
,
'b'
);
statement
.
execute
();
statement
.
execute
();
...
@@ -72,7 +64,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -72,7 +64,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
@Test
public
void
assertInsertWithBatchExecute
()
throws
SQLException
{
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
(
1
,
3
);
statement
.
setObject
(
2
,
'c'
);
statement
.
setObject
(
2
,
'c'
);
statement
.
addBatch
();
statement
.
addBatch
();
...
@@ -86,7 +78,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -86,7 +78,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
@Test
public
void
assertInsertWithExecuteWithGeneratedKey
()
throws
SQLException
{
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
();
statement
.
execute
();
ResultSet
resultSet
=
statement
.
getGeneratedKeys
();
ResultSet
resultSet
=
statement
.
getGeneratedKeys
();
assertTrue
(
resultSet
.
next
());
assertTrue
(
resultSet
.
next
());
...
@@ -98,7 +90,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -98,7 +90,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
@Test
public
void
assertDeleteWithExecute
()
throws
SQLException
{
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
(
1
,
'a'
);
statement
.
setObject
(
2
,
1
);
statement
.
setObject
(
2
,
1
);
statement
.
execute
();
statement
.
execute
();
...
@@ -109,7 +101,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -109,7 +101,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
@Test
public
void
assertUpdateWithExecuteUpdate
()
throws
SQLException
{
public
void
assertUpdateWithExecuteUpdate
()
throws
SQLException
{
int
result
;
int
result
;
try
(
PreparedStatement
statement
=
encryptConnection
.
prepareStatement
(
UPDATE_SQL
))
{
try
(
PreparedStatement
statement
=
getEncryptConnection
()
.
prepareStatement
(
UPDATE_SQL
))
{
statement
.
setObject
(
1
,
'f'
);
statement
.
setObject
(
1
,
'f'
);
statement
.
setObject
(
2
,
'a'
);
statement
.
setObject
(
2
,
'a'
);
result
=
statement
.
executeUpdate
();
result
=
statement
.
executeUpdate
();
...
@@ -120,7 +112,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -120,7 +112,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
@Test
@Test
public
void
assertSelectWithExecuteQuery
()
throws
SQLException
{
public
void
assertSelectWithExecuteQuery
()
throws
SQLException
{
try
(
PreparedStatement
statement
=
encryptConnection
.
prepareStatement
(
SELECT_SQL
))
{
try
(
PreparedStatement
statement
=
getEncryptConnection
()
.
prepareStatement
(
SELECT_SQL
))
{
statement
.
setObject
(
1
,
'a'
);
statement
.
setObject
(
1
,
'a'
);
ResultSet
resultSet
=
statement
.
executeQuery
();
ResultSet
resultSet
=
statement
.
executeQuery
();
assertTrue
(
resultSet
.
next
());
assertTrue
(
resultSet
.
next
());
...
@@ -132,9 +124,22 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -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
@Test
public
void
assertSelectWithExecuteWithProperties
()
throws
SQLException
{
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
();
Boolean
result
=
statement
.
execute
();
assertTrue
(
result
);
assertTrue
(
result
);
assertThat
(
statement
.
getResultSetType
(),
is
(
ResultSet
.
TYPE_FORWARD_ONLY
));
assertThat
(
statement
.
getResultSetType
(),
is
(
ResultSet
.
TYPE_FORWARD_ONLY
));
...
@@ -150,7 +155,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
...
@@ -150,7 +155,7 @@ public final class EncryptPreparedStatementTest extends AbstractEncryptJDBCDatab
int
count
=
1
;
int
count
=
1
;
while
(
resultSet
.
next
())
{
while
(
resultSet
.
next
())
{
if
(
id
==
count
)
{
if
(
id
==
count
)
{
assertThat
(
pwd
,
is
(
resultSet
.
getObject
(
"pwd"
)));
assertThat
(
pwd
,
is
(
resultSet
.
getObject
(
"
cipher_
pwd"
)));
assertThat
(
assistPwd
,
is
(
resultSet
.
getObject
(
"assist_pwd"
)));
assertThat
(
assistPwd
,
is
(
resultSet
.
getObject
(
"assist_pwd"
)));
}
}
count
+=
1
;
count
+=
1
;
...
...
sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatementTest.java
浏览文件 @
a0eb7411
...
@@ -19,14 +19,15 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
...
@@ -19,14 +19,15 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant
;
import
org.apache.shardingsphere.shardingjdbc.common.base.AbstractEncryptJDBCDatabaseAndTableTest
;
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
org.junit.Test
;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.ResultSet
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.Statement
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
...
@@ -43,63 +44,60 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
...
@@ -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
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
private
static
final
String
SELECT_SQL_TO_ASSERT
=
"select id, cipher_pwd, plain_pwd from t_encrypt"
;
public
void
setUp
()
throws
SQLException
{
encryptConnection
=
getEncryptDataSource
().
getConnection
();
}
@Test
@Test
public
void
assertSqlShow
()
{
public
void
assertSqlShow
()
throws
SQLException
{
assertTrue
(
encryptConnection
.
getRuntimeContext
().
getProps
().<
Boolean
>
getValue
(
ShardingPropertiesConstant
.
SQL_SHOW
));
assertTrue
(
getEncryptConnectionWithProps
()
.
getRuntimeContext
().
getProps
().<
Boolean
>
getValue
(
ShardingPropertiesConstant
.
SQL_SHOW
));
}
}
@Test
@Test
public
void
assertInsertWithExecute
()
throws
SQLException
{
public
void
assertInsertWithExecute
()
throws
SQLException
{
try
(
Statement
statement
=
encryptConnection
.
createStatement
())
{
try
(
Statement
statement
=
getEncryptConnection
()
.
createStatement
())
{
statement
.
execute
(
INSERT_SQL
);
statement
.
execute
(
INSERT_SQL
);
}
}
assertResultSet
(
3
,
2
,
"encryptValue"
);
assertResultSet
(
3
,
2
,
"encryptValue"
,
"b"
);
}
}
@Test
@Test
public
void
assertInsertWithExecuteWithGeneratedKey
()
throws
SQLException
{
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
);
statement
.
execute
(
INSERT_GENERATED_KEY_SQL
,
Statement
.
RETURN_GENERATED_KEYS
);
ResultSet
resultSet
=
statement
.
getGeneratedKeys
();
ResultSet
resultSet
=
statement
.
getGeneratedKeys
();
assertTrue
(
resultSet
.
next
());
assertTrue
(
resultSet
.
next
());
assertThat
(
resultSet
.
getInt
(
1
),
is
(
6
));
assertThat
(
resultSet
.
getInt
(
1
),
is
(
6
));
assertFalse
(
resultSet
.
next
());
assertFalse
(
resultSet
.
next
());
}
}
assertResultSet
(
3
,
2
,
"encryptValue
"
);
assertResultSet
(
3
,
6
,
"encryptValue"
,
"b
"
);
}
}
@Test
@Test
public
void
assertDeleteWithExecute
()
throws
SQLException
{
public
void
assertDeleteWithExecute
()
throws
SQLException
{
try
(
Statement
statement
=
encryptConnection
.
createStatement
())
{
try
(
Statement
statement
=
getEncryptConnection
()
.
createStatement
())
{
statement
.
execute
(
DELETE_SQL
);
statement
.
execute
(
DELETE_SQL
);
}
}
assertResultSet
(
1
,
5
,
"encryptValue"
);
assertResultSet
(
1
,
5
,
"encryptValue"
,
"b"
);
}
}
@Test
@Test
public
void
assertUpdateWithExecuteUpdate
()
throws
SQLException
{
public
void
assertUpdateWithExecuteUpdate
()
throws
SQLException
{
int
result
;
int
result
;
try
(
Statement
statement
=
encryptConnection
.
createStatement
())
{
try
(
Statement
statement
=
getEncryptConnection
()
.
createStatement
())
{
result
=
statement
.
executeUpdate
(
UPDATE_SQL
);
result
=
statement
.
executeUpdate
(
UPDATE_SQL
);
}
}
assertThat
(
result
,
is
(
2
));
assertThat
(
result
,
is
(
2
));
assertResultSet
(
2
,
1
,
"encryptValue"
);
assertResultSet
(
2
,
1
,
"encryptValue"
,
"f"
);
}
}
@Test
@Test
public
void
assertSelectWithExecuteQuery
()
throws
SQLException
{
public
void
assertSelectWithExecuteQuery
()
throws
SQLException
{
try
(
Statement
statement
=
encryptConnection
.
createStatement
())
{
try
(
Statement
statement
=
getEncryptConnection
()
.
createStatement
())
{
ResultSet
resultSet
=
statement
.
executeQuery
(
SELECT_SQL
);
ResultSet
resultSet
=
statement
.
executeQuery
(
SELECT_SQL
);
assertTrue
(
resultSet
.
next
());
assertTrue
(
resultSet
.
next
());
assertThat
(
resultSet
.
getInt
(
1
),
is
(
1
));
assertThat
(
resultSet
.
getInt
(
1
),
is
(
1
));
...
@@ -112,7 +110,7 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
...
@@ -112,7 +110,7 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
@Test
@Test
public
void
assertSelectWithExecuteWithProperties
()
throws
SQLException
{
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
};
int
[]
columnIndexes
=
{
1
,
2
};
Boolean
result
=
statement
.
execute
(
SELECT_SQL
,
columnIndexes
);
Boolean
result
=
statement
.
execute
(
SELECT_SQL
,
columnIndexes
);
assertTrue
(
result
);
assertTrue
(
result
);
...
@@ -122,14 +120,43 @@ public final class EncryptStatementTest extends AbstractEncryptJDBCDatabaseAndTa
...
@@ -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
();
try
(
Connection
conn
=
getDatabaseTypeMap
().
values
().
iterator
().
next
().
values
().
iterator
().
next
().
getConnection
();
Statement
stmt
=
conn
.
createStatement
())
{
Statement
stmt
=
conn
.
createStatement
())
{
ResultSet
resultSet
=
stmt
.
executeQuery
(
SELECT_
ALL_SQL
);
ResultSet
resultSet
=
stmt
.
executeQuery
(
SELECT_
SQL_TO_ASSERT
);
int
count
=
1
;
int
count
=
1
;
while
(
resultSet
.
next
())
{
while
(
resultSet
.
next
())
{
if
(
id
==
count
)
{
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
;
count
+=
1
;
}
}
...
...
sharding-jdbc/sharding-jdbc-core/src/test/resources/encrypt_data.sql
浏览文件 @
a0eb7411
...
@@ -17,7 +17,7 @@
...
@@ -17,7 +17,7 @@
DELETE
FROM
t_encrypt
;
DELETE
FROM
t_encrypt
;
DELETE
FROM
t_query_encrypt
;
DELETE
FROM
t_query_encrypt
;
INSERT
INTO
t_encrypt
VALUES
(
1
,
'encryptValue'
);
INSERT
INTO
t_encrypt
VALUES
(
1
,
'plainValue'
);
INSERT
INTO
t_encrypt
VALUES
(
5
,
'encryptValue'
);
INSERT
INTO
t_encrypt
VALUES
(
5
,
'plainValue'
);
INSERT
INTO
t_query_encrypt
VALUES
(
1
,
'encryptValue'
);
INSERT
INTO
t_query_encrypt
VALUES
(
1
,
'plainValue'
);
INSERT
INTO
t_query_encrypt
VALUES
(
5
,
'encryptValue'
);
INSERT
INTO
t_query_encrypt
VALUES
(
5
,
'plainValue'
);
\ No newline at end of file
\ No newline at end of file
sharding-jdbc/sharding-jdbc-core/src/test/resources/jdbc_init.sql
浏览文件 @
a0eb7411
...
@@ -21,5 +21,5 @@ CREATE TABLE IF NOT EXISTS t_order_item_0 (item_id INT NOT NULL, order_id INT NO
...
@@ -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_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_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_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_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
,
pwd
VARCHAR
(
45
)
NULL
,
assist_pwd
VARCHAR
(
45
)
NULL
,
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
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录