You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/03/23 06:11:52 UTC

[GitHub] [incubator-shardingsphere] terrymanu commented on a change in pull request #4871: Fixed issue- #4645 | Use YAML configuration for java AbstractEncrypt…

terrymanu commented on a change in pull request #4871: Fixed issue- #4645 | Use YAML configuration for java  AbstractEncrypt…
URL: https://github.com/apache/incubator-shardingsphere/pull/4871#discussion_r396229707
 
 

 ##########
 File path: sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/common/base/AbstractEncryptJDBCDatabaseAndTableTest.java
 ##########
 @@ -17,87 +17,90 @@
 
 package org.apache.shardingsphere.shardingjdbc.common.base;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import javax.sql.DataSource;
 import org.apache.shardingsphere.encrypt.api.EncryptColumnRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.EncryptTableRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.EncryptorRuleConfiguration;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptColumnRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptorRuleConfiguration;
 import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
 import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
 import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
 import org.h2.tools.RunScript;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
 
-import javax.sql.DataSource;
-import java.io.InputStreamReader;
-import java.sql.SQLException;
-import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
 public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQLTest {
-    
+
     private static EncryptDataSource encryptDataSource;
-    
+
     private static EncryptDataSource encryptDataSourceWithProps;
-    
+
     private static final List<String> ENCRYPT_DB_NAMES = Collections.singletonList("encrypt");
-    
+
+    private static YamlEncryptRuleConfiguration encryptRuleConfig;
+
     @BeforeClass
-    public static void initEncryptDataSource() throws SQLException {
+    public static void initEncryptDataSource() throws SQLException, IOException {
+        encryptRuleConfig = getEncryptRuleConfig(getFile("encrypt_config.yaml"));
+        System.out.println(encryptRuleConfig);
         if (null != encryptDataSource && null != encryptDataSourceWithProps) {
             return;
         }
         Map<String, DataSource> dataSources = getDataSources();
         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(ConfigurationPropertyKey.SQL_SHOW.getKey(), true);
         result.put(ConfigurationPropertyKey.QUERY_WITH_CIPHER_COLUMN.getKey(), false);
         return result;
     }
-    
+
     private static Map<String, DataSource> getDataSources() {
         return Maps.filterKeys(getDatabaseTypeMap().values().iterator().next(), ENCRYPT_DB_NAMES::contains);
     }
-    
+
     private static EncryptRuleConfiguration createEncryptRuleConfiguration() {
         EncryptRuleConfiguration result = new EncryptRuleConfiguration();
-        result.getEncryptors().put("test", new EncryptorRuleConfiguration("test", new Properties()));
-        result.getEncryptors().put("assistedTest", new EncryptorRuleConfiguration("assistedTest", new Properties()));
-        result.getTables().put("t_encrypt", createEncryptTableRule());
-        result.getTables().put("t_query_encrypt", createQueryEncryptTableRule());
-        result.getTables().put("t_encrypt_contains_column", createEncryptContainsColumnTableRule());
+        for (Map.Entry<String, YamlEncryptorRuleConfiguration> entry : encryptRuleConfig.getEncryptors().entrySet()) {
+            result.getEncryptors().put(entry.getKey(), new EncryptorRuleConfiguration(entry.getValue().getType(), entry.getValue().getProps()));
+        }
+
+        for (Map.Entry<String, YamlEncryptTableRuleConfiguration> tableMap : encryptRuleConfig.getTables().entrySet()) {
+            Map<String, EncryptColumnRuleConfiguration> columns = new LinkedHashMap<>(2, 1);
+            for (Map.Entry<String, YamlEncryptColumnRuleConfiguration> columnMap : tableMap.getValue().getColumns().entrySet()) {
+                YamlEncryptColumnRuleConfiguration yamlConfig = columnMap.getValue();
+                EncryptColumnRuleConfiguration columnRuleConfiguration = new EncryptColumnRuleConfiguration(yamlConfig.getPlainColumn(), yamlConfig.getCipherColumn(),
+                    yamlConfig.getAssistedQueryColumn(),
+                    yamlConfig.getEncryptor());
+                columns.put(columnMap.getKey(), columnRuleConfiguration);
+            }
+            result.getTables().put(tableMap.getKey(), new EncryptTableRuleConfiguration(columns));
+        }
+
         return result;
     }
 
 Review comment:
   You may use `YamlEncryptDataSourceFactory.createDataSource()` to load YAML directly.
   We can just make sure the format of YAML file are correct and then use ShardingSphere's public API to load yaml and create EncryptDataSource.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services