You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by la...@apache.org on 2022/02/16 10:10:27 UTC

[shardingsphere] 01/01: `show encrypt rules` syntax add display of data type

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

lanchengxiang pushed a commit to branch show_encrypt_rule
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git

commit d52ee4fa9aefa75cc01a0cb1fa6c164cb787a2eb
Author: lanchengx <la...@qq.com>
AuthorDate: Wed Feb 16 17:31:29 2022 +0800

    `show encrypt rules` syntax add display of data type
---
 .../rule/YamlEncryptColumnRuleConfiguration.java   |  8 ++++
 .../EncryptColumnRuleConfigurationYamlSwapper.java | 10 ++++-
 .../handler/query/EncryptRuleQueryResultSet.java   | 50 +++++++++++-----------
 .../query/EncryptRuleQueryResultSetTest.java       |  9 ++--
 4 files changed, 46 insertions(+), 31 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/config/rule/YamlEncryptColumnRuleConfiguration.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/config/rule/YamlEncryptColumnRuleConfiguration.java
index d6a6e6f..71e68f1 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/config/rule/YamlEncryptColumnRuleConfiguration.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/config/rule/YamlEncryptColumnRuleConfiguration.java
@@ -30,11 +30,19 @@ public final class YamlEncryptColumnRuleConfiguration implements YamlConfigurati
     
     private String logicColumn;
     
+    private String logicDataType;
+    
     private String cipherColumn;
     
+    private String cipherDataType;
+    
     private String assistedQueryColumn;
     
+    private String assistedQueryDataType;
+    
     private String plainColumn;
     
+    private String plainDataType;
+    
     private String encryptorName;
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/rule/EncryptColumnRuleConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/rule/EncryptColumnRuleConfigurationYamlSwapper.java
index a95f41d..8f96135 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/rule/EncryptColumnRuleConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/rule/EncryptColumnRuleConfigurationYamlSwapper.java
@@ -33,12 +33,18 @@ public final class EncryptColumnRuleConfigurationYamlSwapper implements YamlConf
         result.setCipherColumn(data.getCipherColumn());
         result.setAssistedQueryColumn(data.getAssistedQueryColumn());
         result.setEncryptorName(data.getEncryptorName());
+        result.setLogicDataType(data.getLogicDataType());
+        result.setPlainDataType(data.getPlainDataType());
+        result.setCipherDataType(data.getCipherDataType());
+        result.setAssistedQueryDataType(data.getAssistedQueryDataType());
         return result;
     }
     
     @Override
     public EncryptColumnRuleConfiguration swapToObject(final YamlEncryptColumnRuleConfiguration yamlConfig) {
-        return new EncryptColumnRuleConfiguration(
-                yamlConfig.getLogicColumn(), yamlConfig.getCipherColumn(), yamlConfig.getAssistedQueryColumn(), yamlConfig.getPlainColumn(), yamlConfig.getEncryptorName());
+        return new EncryptColumnRuleConfiguration(yamlConfig.getLogicColumn(), yamlConfig.getLogicDataType(),
+                yamlConfig.getCipherColumn(), yamlConfig.getCipherDataType(),
+                yamlConfig.getAssistedQueryColumn(), yamlConfig.getAssistedQueryDataType(),
+                yamlConfig.getPlainColumn(), yamlConfig.getPlainDataType(), yamlConfig.getEncryptorName());
     }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSet.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSet.java
index 0381e6c..d86ef95 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSet.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSet.java
@@ -18,22 +18,20 @@
 package org.apache.shardingsphere.encrypt.distsql.handler.query;
 
 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.statement.ShowEncryptRulesStatement;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.properties.PropertiesConverter;
-import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Collectors;
@@ -43,36 +41,38 @@ import java.util.stream.Collectors;
  */
 public final class EncryptRuleQueryResultSet implements DistSQLResultSet {
     
-    private Iterator<Entry<String, EncryptColumnRuleConfiguration>> data;
-    
-    private Map<String, ShardingSphereAlgorithmConfiguration> encryptors;
+    private Iterator<Collection<Object>> data = Collections.emptyIterator();
     
     @Override
     public void init(final ShardingSphereMetaData metaData, final SQLStatement sqlStatement) {
-        Optional<EncryptRuleConfiguration> ruleConfig = metaData.getRuleMetaData().getConfigurations()
-                .stream().filter(each -> each instanceof EncryptRuleConfiguration).map(each -> (EncryptRuleConfiguration) each).findAny();
-        data = ruleConfig.map(optional -> getAllEncryptColumns(optional, ((ShowEncryptRulesStatement) sqlStatement).getTableName()).entrySet().iterator()).orElse(Collections.emptyIterator());
-        encryptors = ruleConfig.map(EncryptRuleConfiguration::getEncryptors).orElseGet(Collections::emptyMap);
+        Optional<EncryptRuleConfiguration> ruleConfiguration = metaData.getRuleMetaData().findRuleConfiguration(EncryptRuleConfiguration.class).stream().findAny();
+        ruleConfiguration.ifPresent(op -> data = buildData(op, (ShowEncryptRulesStatement) sqlStatement).iterator());
     }
     
-    private Map<String, EncryptColumnRuleConfiguration> getAllEncryptColumns(final EncryptRuleConfiguration encryptRuleConfig, final String tableName) {
-        Map<String, EncryptColumnRuleConfiguration> result = new HashMap<>();
-        if (Objects.nonNull(tableName)) {
-            encryptRuleConfig.getTables().stream().filter(each -> tableName.equalsIgnoreCase(each.getName()))
-                    .findAny().ifPresent(each -> result.putAll(buildEncryptColumnRuleConfigurationMap(each)));
-        } else {
-            encryptRuleConfig.getTables().forEach(each -> result.putAll(buildEncryptColumnRuleConfigurationMap(each)));
-        }
-        return result;
+    private Collection<Collection<Object>> buildData(final EncryptRuleConfiguration configuration, final ShowEncryptRulesStatement sqlStatement) {
+        return configuration.getTables().stream().filter(each -> Objects.isNull(sqlStatement.getTableName()) || each.getName().equals(sqlStatement.getTableName()))
+                .map(each -> buildColumnData(each, configuration.getEncryptors())).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedList::new));
     }
     
-    private Map<String, EncryptColumnRuleConfiguration> buildEncryptColumnRuleConfigurationMap(final EncryptTableRuleConfiguration encryptTableRuleConfig) {
-        return encryptTableRuleConfig.getColumns().stream().collect(Collectors.toMap(each -> String.join(".", encryptTableRuleConfig.getName(), each.getLogicColumn()), each -> each));
+    private Collection<Collection<Object>> buildColumnData(final EncryptTableRuleConfiguration tableRuleConfiguration, final Map<String, ShardingSphereAlgorithmConfiguration> algorithmMap) {
+        Collection<Collection<Object>> result = new LinkedList<>();
+        tableRuleConfiguration.getColumns().forEach(each -> {
+            ShardingSphereAlgorithmConfiguration algorithmConfiguration = algorithmMap.get(each.getEncryptorName());
+            result.add(Arrays.asList(tableRuleConfiguration.getName(), each.getLogicColumn(), each.getLogicDataType(), each.getCipherColumn(), each.getCipherDataType(),
+                    each.getPlainColumn(), each.getPlainDataType(), each.getAssistedQueryColumn(), each.getAssistedQueryDataType(),
+                    algorithmConfiguration.getType(), PropertiesConverter.convert(algorithmConfiguration.getProps()),
+                    Objects.isNull(tableRuleConfiguration.getQueryWithCipherColumn()) ? Boolean.FALSE.toString() : tableRuleConfiguration.getQueryWithCipherColumn().toString()));
+        });
+        return result;
     }
     
     @Override
     public Collection<String> getColumnNames() {
-        return Arrays.asList("table", "logic_column", "cipher_column", "plain_column", "encryptor_type", "encryptor_props");
+        return Arrays.asList("table", "logic_column", "logic_data_type",
+                "cipher_column", "cipher_data_type",
+                "plain_column", "plain_data_type",
+                "assisted_query_column", "assisted_query_data_type",
+                "encryptor_type", "encryptor_props", "query_with_cipher_column");
     }
     
     @Override
@@ -82,9 +82,7 @@ public final class EncryptRuleQueryResultSet implements DistSQLResultSet {
     
     @Override
     public Collection<Object> getRowData() {
-        Entry<String, EncryptColumnRuleConfiguration> entry = data.next();
-        return Arrays.asList(entry.getKey().split("\\.")[0], entry.getValue().getLogicColumn(), entry.getValue().getCipherColumn(), entry.getValue().getPlainColumn(), 
-                encryptors.get(entry.getValue().getEncryptorName()).getType(), PropertiesConverter.convert(encryptors.get(entry.getValue().getEncryptorName()).getProps()));
+        return data.next();
     }
     
     @Override
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSetTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSetTest.java
index 3548408..3d8d86a 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSetTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-distsql/shardingsphere-encrypt-distsql-handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleQueryResultSetTest.java
@@ -34,6 +34,7 @@ import java.util.Properties;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -43,20 +44,22 @@ public final class EncryptRuleQueryResultSetTest {
     @Test
     public void assertGetRowData() {
         ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
-        when(metaData.getRuleMetaData().getConfigurations()).thenReturn(Collections.singleton(getRuleConfiguration()));
+        when(metaData.getRuleMetaData().findRuleConfiguration(any())).thenReturn(Collections.singleton(getRuleConfiguration()));
         DistSQLResultSet resultSet = new EncryptRuleQueryResultSet();
         resultSet.init(metaData, mock(ShowEncryptRulesStatement.class));
         Collection<Object> actual = resultSet.getRowData();
-        assertThat(actual.size(), is(6));
+        assertThat(actual.size(), is(12));
         assertTrue(actual.contains("t_encrypt"));
         assertTrue(actual.contains("user_id"));
         assertTrue(actual.contains("user_cipher"));
         assertTrue(actual.contains("user_plain"));
         assertTrue(actual.contains("md5"));
+        assertTrue(actual.contains("varchar(10)"));
     }
     
     private RuleConfiguration getRuleConfiguration() {
-        EncryptColumnRuleConfiguration encryptColumnRuleConfig = new EncryptColumnRuleConfiguration("user_id", "user_cipher", null, "user_plain", "test");
+        EncryptColumnRuleConfiguration encryptColumnRuleConfig = new EncryptColumnRuleConfiguration("user_id", "varchar(10)", "user_cipher", "varchar(10)", null, null,
+                "user_plain", "varchar(10)", "test");
         EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singleton(encryptColumnRuleConfig), null);
         ShardingSphereAlgorithmConfiguration shardingSphereAlgorithmConfig = new ShardingSphereAlgorithmConfiguration("md5", new Properties());
         return new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), Collections.singletonMap("test", shardingSphereAlgorithmConfig));