This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 bf46a83c6f4 Fix EncryptRuleConfiguration defaultProperties. (#30990)
bf46a83c6f4 is described below

commit bf46a83c6f4a7f3950fe0e9da2381d900387c3e9
Author: Cong Hu <[email protected]>
AuthorDate: Tue Apr 23 12:28:15 2024 +0800

    Fix EncryptRuleConfiguration defaultProperties. (#30990)
---
 .../encrypt/api/config/EncryptRuleConfiguration.java | 20 ++++++++++++++++----
 .../shardingsphere/encrypt/rule/EncryptRuleTest.java | 16 ++++++++++++++++
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git 
a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
 
b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
index 0ead0f4f5a1..41ade93cd10 100644
--- 
a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
+++ 
b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/config/EncryptRuleConfiguration.java
@@ -27,7 +27,10 @@ import 
org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfigurati
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
 
 /**
  * Encrypt rule configuration.
@@ -41,11 +44,20 @@ public final class EncryptRuleConfiguration implements 
DatabaseRuleConfiguration
     
     public EncryptRuleConfiguration(final 
Collection<EncryptTableRuleConfiguration> tables, final Map<String, 
AlgorithmConfiguration> encryptors) {
         this.tables = tables;
-        this.encryptors = encryptors;
-        for (AlgorithmConfiguration each : encryptors.values()) {
-            TypedSPILoader.findUninitedService(EncryptAlgorithm.class, 
each.getType()).map(EncryptAlgorithm::getMetaData).map(EncryptAlgorithmMetaData::getDefaultProps)
-                    .ifPresent(each.getProps()::putAll);
+        this.encryptors = rebuildEncryptorsWithDefaultProperties(encryptors);
+    }
+    
+    private Map<String, AlgorithmConfiguration> 
rebuildEncryptorsWithDefaultProperties(final Map<String, 
AlgorithmConfiguration> encryptors) {
+        Map<String, AlgorithmConfiguration> result = new HashMap<>();
+        for (Entry<String, AlgorithmConfiguration> entry : 
encryptors.entrySet()) {
+            Properties props = new Properties();
+            props.putAll(entry.getValue().getProps());
+            Properties defaultProps = 
TypedSPILoader.findUninitedService(EncryptAlgorithm.class, 
entry.getValue().getType()).map(EncryptAlgorithm::getMetaData)
+                    
.map(EncryptAlgorithmMetaData::getDefaultProps).orElseGet(Properties::new);
+            defaultProps.forEach(props::putIfAbsent);
+            result.put(entry.getKey(), new 
AlgorithmConfiguration(entry.getValue().getType(), props));
         }
+        return result;
     }
     
     @Override
diff --git 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
index d2cbaa71476..e8520c4ec6f 100644
--- 
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
+++ 
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.encrypt.rule;
 
+import org.apache.commons.codec.digest.MessageDigestAlgorithms;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration;
 import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
@@ -40,11 +41,14 @@ import java.util.stream.Stream;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class EncryptRuleTest {
     
+    private static final String DIGEST_ALGORITHM_NAME = 
"digest-algorithm-name";
+    
     @Test
     void assertFindEncryptTable() {
         assertTrue(new EncryptRule("foo_db", 
createEncryptRuleConfiguration()).findEncryptTable("t_encrypt").isPresent());
@@ -86,6 +90,18 @@ class EncryptRuleTest {
         assertThat(pwdColumnConfig.getLikeQuery().get().getEncryptorName(), 
is("like_query_test_encryptor"));
     }
     
+    @Test
+    void assertAESEncryptRuleDefaultProps() {
+        EncryptRuleConfiguration defaultPropsEncryptRuleConfig = new 
EncryptRuleConfiguration(Collections.emptyList(),
+                Collections.singletonMap("aes_encryptor", new 
AlgorithmConfiguration("AES", new Properties())));
+        assertEquals(MessageDigestAlgorithms.SHA_1, 
defaultPropsEncryptRuleConfig.getEncryptors().get("aes_encryptor").getProps().getProperty(DIGEST_ALGORITHM_NAME));
+        Properties props = new Properties();
+        props.put(DIGEST_ALGORITHM_NAME, MessageDigestAlgorithms.SHA_256);
+        EncryptRuleConfiguration sha256EncryptRuleConfig = new 
EncryptRuleConfiguration(Collections.emptyList(),
+                Collections.singletonMap("aes_encryptor", new 
AlgorithmConfiguration("AES", props)));
+        assertEquals(MessageDigestAlgorithms.SHA_256, 
sha256EncryptRuleConfig.getEncryptors().get("aes_encryptor").getProps().getProperty(DIGEST_ALGORITHM_NAME));
+    }
+    
     private Map<String, AlgorithmConfiguration> getEncryptors(final 
AlgorithmConfiguration standardEncryptConfig, final AlgorithmConfiguration 
queryAssistedEncryptConfig,
                                                               final 
AlgorithmConfiguration queryLikeEncryptConfig) {
         Map<String, AlgorithmConfiguration> result = new HashMap<>(3, 1F);

Reply via email to