This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 0c331684442 Refactor EncryptRuleConfigurationChecker (#33639)
0c331684442 is described below
commit 0c3316844422a35037ab694e9be757c56143b1e8
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 13 16:47:33 2024 +0800
Refactor EncryptRuleConfigurationChecker (#33639)
---
.../config/EncryptRuleConfigurationChecker.java | 42 +++++------------
.../EncryptRuleConfigurationCheckerTest.java | 52 +++++++++++++++++++---
2 files changed, 56 insertions(+), 38 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationChecker.java
index 598973bc788..1a3956b8cb1 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationChecker.java
@@ -62,39 +62,19 @@ public final class EncryptRuleConfigurationChecker
implements RuleConfigurationC
}
private void checkColumn(final String databaseName, final String
tableName, final EncryptColumnRuleConfiguration columnRuleConfig, final
Map<String, AlgorithmConfiguration> encryptors) {
- checkCipherColumn(databaseName, tableName, columnRuleConfig.getName(),
columnRuleConfig.getCipher(), encryptors);
- columnRuleConfig.getAssistedQuery().ifPresent(optional ->
checkAssistColumn(databaseName, tableName, columnRuleConfig.getName(),
optional, encryptors));
- columnRuleConfig.getLikeQuery().ifPresent(optional ->
checkLikeColumn(databaseName, tableName, columnRuleConfig.getName(), optional,
encryptors));
+ checkEncryptColumnItem(databaseName, tableName,
columnRuleConfig.getName(), columnRuleConfig.getCipher(), encryptors, "Cipher");
+ columnRuleConfig.getAssistedQuery().ifPresent(optional ->
checkEncryptColumnItem(databaseName, tableName, columnRuleConfig.getName(),
optional, encryptors, "Assist Query"));
+ columnRuleConfig.getLikeQuery().ifPresent(optional ->
checkEncryptColumnItem(databaseName, tableName, columnRuleConfig.getName(),
optional, encryptors, "Like Query"));
}
- private void checkCipherColumn(final String databaseName, final String
tableName, final String logicColumnName,
- final EncryptColumnItemRuleConfiguration
cipherColumnConfig, final Map<String, AlgorithmConfiguration> encryptors) {
- ShardingSpherePreconditions.checkNotEmpty(cipherColumnConfig.getName(),
- () -> new MissingRequiredEncryptColumnException("Cipher", new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
-
ShardingSpherePreconditions.checkNotEmpty(cipherColumnConfig.getEncryptorName(),
- () -> new MissingRequiredAlgorithmException("Standard
encrypt", new SQLExceptionIdentifier(databaseName, tableName,
logicColumnName)));
- ShardingSpherePreconditions.checkContainsKey(encryptors,
cipherColumnConfig.getEncryptorName(),
- () -> new UnregisteredAlgorithmException("Standard encrypt",
cipherColumnConfig.getEncryptorName(), new SQLExceptionIdentifier(databaseName,
tableName, logicColumnName)));
- }
-
- private void checkAssistColumn(final String databaseName, final String
tableName, final String logicColumnName,
- final EncryptColumnItemRuleConfiguration
assistedQueryColumnConfig, final Map<String, AlgorithmConfiguration>
encryptors) {
-
ShardingSpherePreconditions.checkNotEmpty(assistedQueryColumnConfig.getName(),
- () -> new MissingRequiredEncryptColumnException("Assisted
query", new SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
-
ShardingSpherePreconditions.checkNotEmpty(assistedQueryColumnConfig.getEncryptorName(),
- () -> new MissingRequiredAlgorithmException("Assist query
encrypt", new SQLExceptionIdentifier(databaseName, tableName,
logicColumnName)));
- ShardingSpherePreconditions.checkContainsKey(encryptors,
assistedQueryColumnConfig.getEncryptorName(),
- () -> new UnregisteredAlgorithmException("Assist query
encrypt", assistedQueryColumnConfig.getEncryptorName(), new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
- }
-
- private void checkLikeColumn(final String databaseName, final String
tableName, final String logicColumnName,
- final EncryptColumnItemRuleConfiguration
likeQueryColumnConfig, final Map<String, AlgorithmConfiguration> encryptors) {
-
ShardingSpherePreconditions.checkNotEmpty(likeQueryColumnConfig.getName(),
- () -> new MissingRequiredEncryptColumnException("Like", new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
-
ShardingSpherePreconditions.checkNotEmpty(likeQueryColumnConfig.getEncryptorName(),
- () -> new MissingRequiredAlgorithmException("Like query", new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
- ShardingSpherePreconditions.checkContainsKey(encryptors,
likeQueryColumnConfig.getEncryptorName(),
- () -> new UnregisteredAlgorithmException("Like query encrypt",
likeQueryColumnConfig.getEncryptorName(), new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
+ private void checkEncryptColumnItem(final String databaseName, final
String tableName, final String logicColumnName,
+ final
EncryptColumnItemRuleConfiguration columnItem, final Map<String,
AlgorithmConfiguration> encryptors, final String itemType) {
+ ShardingSpherePreconditions.checkNotEmpty(columnItem.getName(),
+ () -> new MissingRequiredEncryptColumnException(itemType, new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
+
ShardingSpherePreconditions.checkNotEmpty(columnItem.getEncryptorName(),
+ () -> new MissingRequiredAlgorithmException(itemType + "
encrypt", new SQLExceptionIdentifier(databaseName, tableName,
logicColumnName)));
+ ShardingSpherePreconditions.checkContainsKey(encryptors,
columnItem.getEncryptorName(),
+ () -> new UnregisteredAlgorithmException(itemType + "
encrypt", columnItem.getEncryptorName(), new
SQLExceptionIdentifier(databaseName, tableName, logicColumnName)));
}
@Override
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
index 1b5d97f2bbc..555e4c50cd1 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
@@ -21,7 +21,9 @@ import
org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
import
org.apache.shardingsphere.encrypt.config.rule.EncryptColumnItemRuleConfiguration;
import
org.apache.shardingsphere.encrypt.config.rule.EncryptColumnRuleConfiguration;
import
org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.exception.metadata.MissingRequiredEncryptColumnException;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.exception.MissingRequiredAlgorithmException;
import
org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException;
import
org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
@@ -33,6 +35,8 @@ import java.util.Collections;
import java.util.Map;
import java.util.Properties;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class EncryptRuleConfigurationCheckerTest {
@@ -61,18 +65,46 @@ class EncryptRuleConfigurationCheckerTest {
@SuppressWarnings("unchecked")
@Test
- void assertCheckWithInvalidCipherColumn() {
- EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithInvalidCipherColumn();
- assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
+ void assertCheckWithEmptyCipherColumnName() {
+ EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithEmptyCipherColumnName();
+ assertThrows(MissingRequiredEncryptColumnException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
}
- private EncryptRuleConfiguration
createRuleConfigurationWithInvalidCipherColumn() {
- EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
+ private EncryptRuleConfiguration
createRuleConfigurationWithEmptyCipherColumnName() {
+ EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("", "aes_encryptor"));
Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
return new EncryptRuleConfiguration(tables, encryptors);
}
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertCheckWithEmptyCipherEncryptorName() {
+ EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithEmptyCipherEncryptorName();
+ assertThrows(MissingRequiredAlgorithmException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
+ }
+
+ private EncryptRuleConfiguration
createRuleConfigurationWithEmptyCipherEncryptorName() {
+ EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", ""));
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
+ return new EncryptRuleConfiguration(tables, encryptors);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertCheckWithUnregisteredCipherEncryptor() {
+ EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithUnregisteredCipherEncryptor();
+ assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
+ }
+
+ private EncryptRuleConfiguration
createRuleConfigurationWithUnregisteredCipherEncryptor() {
+ EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "no_encryptor"));
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.singletonMap("aes_encryptor", new AlgorithmConfiguration("MD5", new
Properties()));
+ return new EncryptRuleConfiguration(tables, encryptors);
+ }
+
@SuppressWarnings("unchecked")
@Test
void assertCheckWithInvalidAssistColumn() {
@@ -84,7 +116,7 @@ class EncryptRuleConfigurationCheckerTest {
EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
columnRuleConfig.setAssistedQuery(new
EncryptColumnItemRuleConfiguration("user_assisted", "aes_assisted_encryptor"));
Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
- Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.singletonMap("aes_encryptor", new AlgorithmConfiguration("MD5", new
Properties()));
return new EncryptRuleConfiguration(tables, encryptors);
}
@@ -99,7 +131,13 @@ class EncryptRuleConfigurationCheckerTest {
EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
columnRuleConfig.setLikeQuery(new
EncryptColumnItemRuleConfiguration("user_like", "like_cn_encryptor"));
Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
- Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.singletonMap("aes_encryptor", new AlgorithmConfiguration("MD5", new
Properties()));
return new EncryptRuleConfiguration(tables, encryptors);
}
+
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertGetTableNames() {
+ assertThat(checker.getTableNames(createValidRuleConfiguration()),
is(Collections.singletonList("t_encrypt")));
+ }
}