You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/12/28 15:58:43 UTC
[shardingsphere] branch master updated: Fix DistSQL ALTER ENCRYPT RULE (#23142)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 21b8475108b Fix DistSQL ALTER ENCRYPT RULE (#23142)
21b8475108b is described below
commit 21b8475108b86e796c767ce81716aa00c0c30c4b
Author: Chen Jiahao <37...@users.noreply.github.com>
AuthorDate: Wed Dec 28 23:58:30 2022 +0800
Fix DistSQL ALTER ENCRYPT RULE (#23142)
---
.../update/AlterEncryptRuleStatementUpdater.java | 22 ++++++++++++----
.../AlterEncryptRuleStatementUpdaterTest.java | 29 +++++++++++++++++++++-
2 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdater.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdater.java
index 47bfaf5d848..8682b45040f 100644
--- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdater.java
+++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdater.java
@@ -18,7 +18,12 @@
package org.apache.shardingsphere.encrypt.distsql.handler.update;
import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
+import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
+import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
+import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionAlterUpdater;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.distsql.handler.converter.EncryptRuleStatementConverter;
import org.apache.shardingsphere.encrypt.distsql.parser.segment.EncryptColumnSegment;
@@ -26,10 +31,6 @@ import org.apache.shardingsphere.encrypt.distsql.parser.segment.EncryptRuleSegme
import org.apache.shardingsphere.encrypt.distsql.parser.statement.AlterEncryptRuleStatement;
import org.apache.shardingsphere.encrypt.factory.EncryptAlgorithmFactory;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
-import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
-import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
-import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionAlterUpdater;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
@@ -97,6 +98,7 @@ public final class AlterEncryptRuleStatementUpdater implements RuleDefinitionAlt
public void updateCurrentRuleConfiguration(final EncryptRuleConfiguration currentRuleConfig, final EncryptRuleConfiguration toBeAlteredRuleConfig) {
dropRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig);
addRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig);
+ dropUnusedEncryptor(currentRuleConfig);
}
private void dropRuleConfiguration(final EncryptRuleConfiguration currentRuleConfig, final EncryptRuleConfiguration toBeAlteredRuleConfig) {
@@ -104,7 +106,6 @@ public final class AlterEncryptRuleStatementUpdater implements RuleDefinitionAlt
Optional<EncryptTableRuleConfiguration> toBeRemovedTableRuleConfig = currentRuleConfig.getTables().stream().filter(tableRule -> tableRule.getName().equals(each.getName())).findAny();
Preconditions.checkState(toBeRemovedTableRuleConfig.isPresent());
currentRuleConfig.getTables().remove(toBeRemovedTableRuleConfig.get());
- toBeRemovedTableRuleConfig.get().getColumns().forEach(column -> currentRuleConfig.getEncryptors().remove(column.getEncryptorName()));
}
}
@@ -113,6 +114,17 @@ public final class AlterEncryptRuleStatementUpdater implements RuleDefinitionAlt
currentRuleConfig.getEncryptors().putAll(toBeAlteredRuleConfig.getEncryptors());
}
+ private void dropUnusedEncryptor(final EncryptRuleConfiguration currentRuleConfig) {
+ Collection<String> inUsedEncryptors = currentRuleConfig.getTables().stream().flatMap(each -> each.getColumns().stream()).map(EncryptColumnRuleConfiguration::getEncryptorName)
+ .collect(Collectors.toSet());
+ inUsedEncryptors.addAll(currentRuleConfig.getTables().stream().flatMap(each -> each.getColumns().stream()).map(EncryptColumnRuleConfiguration::getAssistedQueryEncryptorName)
+ .collect(Collectors.toSet()));
+ inUsedEncryptors.addAll(currentRuleConfig.getTables().stream().flatMap(each -> each.getColumns().stream()).map(EncryptColumnRuleConfiguration::getLikeQueryEncryptorName)
+ .collect(Collectors.toSet()));
+ Collection<String> unusedEncryptors = currentRuleConfig.getEncryptors().keySet().stream().filter(each -> !inUsedEncryptors.contains(each)).collect(Collectors.toSet());
+ unusedEncryptors.forEach(each -> currentRuleConfig.getEncryptors().remove(each));
+ }
+
@Override
public Class<EncryptRuleConfiguration> getRuleConfigurationClass() {
return EncryptRuleConfiguration.class;
diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java
index 05003025d66..5960c11c444 100644
--- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java
+++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.encrypt.distsql.handler.update;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.distsql.parser.segment.EncryptColumnSegment;
import org.apache.shardingsphere.encrypt.distsql.parser.segment.EncryptRuleSegment;
@@ -26,6 +27,7 @@ import org.apache.shardingsphere.encrypt.distsql.parser.statement.AlterEncryptRu
import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -33,10 +35,15 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
+import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
+import java.util.Map;
import java.util.Properties;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
@RunWith(MockitoJUnitRunner.class)
public final class AlterEncryptRuleStatementUpdaterTest {
@@ -61,7 +68,7 @@ public final class AlterEncryptRuleStatementUpdaterTest {
}
@Test(expected = InvalidRuleConfigurationException.class)
- public void assertCheckSQLStatementWithIncompleteDataType() {
+ public void assertUpdateCurrentRuleConfigurationWithInUsedEncryptor() {
EncryptColumnSegment columnSegment = new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", "like_column",
"int varchar(10)", null, null, null, null, new AlgorithmSegment("test", new Properties()),
new AlgorithmSegment("test", new Properties()),
@@ -71,6 +78,13 @@ public final class AlterEncryptRuleStatementUpdaterTest {
updater.checkSQLStatement(database, statement, createCurrentRuleConfiguration());
}
+ @Test
+ public void assertCheckSQLStatementWithIncompleteDataType() {
+ EncryptRuleConfiguration currentRuleConfiguration = createCurrentRuleConfigurationWithMultipleTableRules();
+ updater.updateCurrentRuleConfiguration(currentRuleConfiguration, createToBeAlteredRuleConfiguration());
+ assertThat(currentRuleConfiguration.getEncryptors().size(), is(1));
+ }
+
private AlterEncryptRuleStatement createSQLStatement(final String encryptorName) {
EncryptColumnSegment columnSegment = new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", "like_column",
new AlgorithmSegment(encryptorName, new Properties()),
@@ -84,4 +98,17 @@ public final class AlterEncryptRuleStatementUpdaterTest {
EncryptTableRuleConfiguration tableRuleConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.emptyList(), null);
return new EncryptRuleConfiguration(new LinkedList<>(Collections.singleton(tableRuleConfig)), Collections.emptyMap());
}
+
+ private EncryptRuleConfiguration createCurrentRuleConfigurationWithMultipleTableRules() {
+ EncryptColumnRuleConfiguration columnRuleConfig = new EncryptColumnRuleConfiguration("user_id", "user_cipher", "", "", "user_plain", "t_encrypt_user_id_MD5", null);
+ EncryptTableRuleConfiguration tableRuleConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singleton(columnRuleConfig), null);
+ Map<String, AlgorithmConfiguration> encryptors = Collections.singletonMap("t_encrypt_user_id_MD5", new AlgorithmConfiguration("TEST", new Properties()));
+ return new EncryptRuleConfiguration(new LinkedList<>(Arrays.asList(tableRuleConfig,
+ new EncryptTableRuleConfiguration("t_encrypt_another", Collections.singleton(columnRuleConfig), null))), encryptors, true);
+ }
+
+ private EncryptRuleConfiguration createToBeAlteredRuleConfiguration() {
+ EncryptTableRuleConfiguration tableRuleConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.emptyList(), null);
+ return new EncryptRuleConfiguration(new LinkedList<>(Collections.singleton(tableRuleConfig)), Collections.emptyMap());
+ }
}