This is an automated email from the ASF dual-hosted git repository.
panjuan 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 5d70df3 Add create encrypt rule handler (#10399)
5d70df3 is described below
commit 5d70df3e48ee4cf50edbc9bf7f9e6e828381bd00
Author: Haoran Meng <[email protected]>
AuthorDate: Thu May 20 17:44:51 2021 +0800
Add create encrypt rule handler (#10399)
* Add create encrypt rule handler
* Add create encrypt rule handler
Co-authored-by: menghaoranss <[email protected]>
---
.../db/protocol/error/CommonErrorCode.java | 8 +-
.../parser/segment/rdl/EncryptRuleSegment.java | 2 +
.../shardingsphere-encrypt-common/pom.xml | 5 +
.../yaml/converter/EncryptRuleConverter.java | 85 +++++++++++++++
.../yaml/converter/EncryptRuleConverterTest.java | 63 +++++++++++
.../exception/EncryptRuleExistsException.java | 13 +--
.../exception/InvalidEncryptorsException.java | 15 ++-
.../text/distsql/rdl/RDLBackendHandlerFactory.java | 5 +
.../rdl/impl/CreateEncryptRuleBackendHandler.java | 80 ++++++++++++++
.../impl/CreateEncryptRuleBackendHandlerTest.java | 116 +++++++++++++++++++++
.../frontend/mysql/err/MySQLErrPacketFactory.java | 8 ++
11 files changed, 382 insertions(+), 18 deletions(-)
diff --git
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
index d7ab5fa..8599d32 100644
---
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
+++
b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
@@ -55,7 +55,7 @@ public enum CommonErrorCode implements SQLErrorCode {
DUPLICATE_TABLE(1113, "C1113", "Duplicate table names %s."),
- SHARDING_BROADCAST_EXIST(1114, "C1114", "Sharding broadcast table rules
already exist in schema %s."),
+ SHARDING_BROADCAST_EXIST(1114, "C1114", "Sharding broadcast table rules
already exists in schema %s."),
SHARDING_BINDING_TABLE_RULES_NOT_EXIST(1115, "C1115", "Sharding binding
table rules not exist in schema %s."),
@@ -63,7 +63,7 @@ public enum CommonErrorCode implements SQLErrorCode {
INVALID_LOAD_BALANCERS(1117, "C1117", "Invalid load balancers %s."),
- DATABASE_DISCOVERY_RULE_EXIST(1118, "C1118", "Database discovery rule
already exist in schema %s."),
+ DATABASE_DISCOVERY_RULE_EXIST(1118, "C1118", "Database discovery rule
already exists in schema %s."),
INVALID_DATABASE_DISCOVERY_TYPES(1119, "C1119", "Invalid database
discovery types %s."),
@@ -71,6 +71,10 @@ public enum CommonErrorCode implements SQLErrorCode {
DATABASE_DISCOVERY_RULE_DATASOURCE_NOT_EXIST(1121, "C1121", "Database
discovery rules %s do not exist."),
+ ENCRYPT_RULE_EXIST(1122, "C1122", "Encrypt rule already exists in schema
%s."),
+
+ INVALID_ENCRYPTORS(1123, "C1123", "Invalid encryptors %s."),
+
SCALING_JOB_NOT_EXIST(1201, "C1201", "Scaling job %s does not exist."),
SCALING_OPERATE_FAILED(1209, "C1209", "Scaling Operate Failed: [%s]"),
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
index 105be1c..69806b8 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.distsql.parser.segment.rdl;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import lombok.Setter;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Collection;
@@ -28,6 +29,7 @@ import java.util.Collection;
*/
@RequiredArgsConstructor
@Getter
+@Setter
public final class EncryptRuleSegment implements ASTNode {
private final String tableName;
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/pom.xml
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/pom.xml
index 7f659f8..18ed7dd 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/pom.xml
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/pom.xml
@@ -34,5 +34,10 @@
<artifactId>shardingsphere-encrypt-api</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-distsql-parser-statement</artifactId>
+ <version>${project.version}</version>
+ </dependency>
</dependencies>
</project>
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/yaml/converter/EncryptRuleConverter.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/yaml/converter/EncryptRuleConverter.java
new file mode 100644
index 0000000..4c6123f
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/yaml/converter/EncryptRuleConverter.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.yaml.converter;
+
+import
org.apache.shardingsphere.distsql.parser.segment.rdl.EncryptColumnSegment;
+import org.apache.shardingsphere.distsql.parser.segment.rdl.EncryptRuleSegment;
+import
org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptColumnRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
+import
org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Encrypt rule converter.
+ */
+public final class EncryptRuleConverter {
+
+ /**
+ * Convert collection of encrypt rule segments to YAML encrypt rule
configuration.
+ *
+ * @param encryptRules collection of encrypt rule segments
+ * @return YAML encrypt rule configuration
+ */
+ public static YamlEncryptRuleConfiguration convert(final
Collection<EncryptRuleSegment> encryptRules) {
+ YamlEncryptRuleConfiguration result = new
YamlEncryptRuleConfiguration();
+
result.getTables().putAll(encryptRules.stream().map(EncryptRuleConverter::buildYamlEncryptTableRuleConfiguration)
+
.collect(Collectors.toMap(YamlEncryptTableRuleConfiguration::getName, each ->
each)));
+ for (EncryptRuleSegment encryptRuleSegment : encryptRules) {
+
result.getEncryptors().putAll(buildYamlShardingSphereAlgorithmConfigurations(encryptRuleSegment));
+ }
+ return result;
+ }
+
+ private static YamlEncryptTableRuleConfiguration
buildYamlEncryptTableRuleConfiguration(final EncryptRuleSegment
encryptRuleSegment) {
+ YamlEncryptTableRuleConfiguration result = new
YamlEncryptTableRuleConfiguration();
+ result.setName(encryptRuleSegment.getTableName());
+ result.getColumns().putAll(encryptRuleSegment.getColumns().stream()
+ .map(each ->
buildYamlEncryptColumnRuleConfiguration(encryptRuleSegment.getTableName(),
each))
+
.collect(Collectors.toMap(YamlEncryptColumnRuleConfiguration::getLogicColumn,
each -> each)));
+ return result;
+ }
+
+ private static YamlEncryptColumnRuleConfiguration
buildYamlEncryptColumnRuleConfiguration(final String tableName, final
EncryptColumnSegment encryptColumnSegment) {
+ YamlEncryptColumnRuleConfiguration result = new
YamlEncryptColumnRuleConfiguration();
+ result.setLogicColumn(encryptColumnSegment.getName());
+ result.setCipherColumn(encryptColumnSegment.getCipherColumn());
+ result.setPlainColumn(encryptColumnSegment.getPlainColumn());
+ result.setEncryptorName(getEncryptorName(tableName,
encryptColumnSegment.getName(),
encryptColumnSegment.getEncryptor().getAlgorithmName()));
+ return result;
+ }
+
+ private static Map<String, YamlShardingSphereAlgorithmConfiguration>
buildYamlShardingSphereAlgorithmConfigurations(final EncryptRuleSegment
encryptRuleSegment) {
+ return
encryptRuleSegment.getColumns().stream().collect(Collectors.toMap(EncryptColumnSegment::getName,
each -> buildYamlShardingSphereAlgorithmConfiguration(each)))
+ .entrySet().stream().collect(Collectors.toMap(entry ->
getEncryptorName(encryptRuleSegment.getTableName(), entry.getKey(),
entry.getValue().getType()), entry -> entry.getValue()));
+ }
+
+ private static YamlShardingSphereAlgorithmConfiguration
buildYamlShardingSphereAlgorithmConfiguration(final EncryptColumnSegment
encryptColumnSegment) {
+ YamlShardingSphereAlgorithmConfiguration result = new
YamlShardingSphereAlgorithmConfiguration();
+ result.setType(encryptColumnSegment.getEncryptor().getAlgorithmName());
+
result.setProps(encryptColumnSegment.getEncryptor().getAlgorithmProps());
+ return result;
+ }
+
+ private static String getEncryptorName(final String tableName, final
String columnName, final String encryptorAlgorithmName) {
+ return String.format("%s_%s_%s", tableName, columnName,
encryptorAlgorithmName);
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/yaml/converter/EncryptRuleConverterTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/yaml/converter/EncryptRuleConverterTest.java
new file mode 100644
index 0000000..11da81e
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/yaml/converter/EncryptRuleConverterTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.yaml.converter;
+
+import org.apache.shardingsphere.distsql.parser.segment.FunctionSegment;
+import
org.apache.shardingsphere.distsql.parser.segment.rdl.EncryptColumnSegment;
+import org.apache.shardingsphere.distsql.parser.segment.rdl.EncryptRuleSegment;
+import
org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+
+public final class EncryptRuleConverterTest {
+
+ @Test
+ public void assertCovert() {
+ YamlEncryptRuleConfiguration encryptRuleConfiguration =
EncryptRuleConverter.convert(Collections
+ .singleton(new EncryptRuleSegment("t_encrypt",
buildColumns())));
+ assertNotNull(encryptRuleConfiguration);
+ assertThat(encryptRuleConfiguration.getTables().keySet(),
is(Collections.singleton("t_encrypt")));
+
assertThat(encryptRuleConfiguration.getTables().get("t_encrypt").getName(),
is("t_encrypt"));
+
assertThat(encryptRuleConfiguration.getTables().get("t_encrypt").getColumns().keySet(),
is(Collections.singleton("user_id")));
+
assertThat(encryptRuleConfiguration.getTables().get("t_encrypt").getColumns().get("user_id").getLogicColumn(),
is("user_id"));
+
assertThat(encryptRuleConfiguration.getTables().get("t_encrypt").getColumns().get("user_id").getCipherColumn(),
is("user_cipher"));
+
assertThat(encryptRuleConfiguration.getTables().get("t_encrypt").getColumns().get("user_id").getPlainColumn(),
is("user_plain"));
+
assertThat(encryptRuleConfiguration.getTables().get("t_encrypt").getColumns().get("user_id").getEncryptorName(),
is("t_encrypt_user_id_MD5"));
+ }
+
+ private Collection<EncryptColumnSegment> buildColumns() {
+ EncryptColumnSegment encryptColumnSegment = new EncryptColumnSegment();
+ encryptColumnSegment.setName("user_id");
+ encryptColumnSegment.setPlainColumn("user_plain");
+ encryptColumnSegment.setCipherColumn("user_cipher");
+ FunctionSegment functionSegment = new FunctionSegment();
+ functionSegment.setAlgorithmName("MD5");
+ Properties properties = new Properties();
+ properties.setProperty("MD5-key", "MD5-value");
+ functionSegment.setAlgorithmProps(properties);
+ encryptColumnSegment.setEncryptor(functionSegment);
+ return Collections.singleton(encryptColumnSegment);
+ }
+}
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/EncryptRuleExistsException.java
similarity index 72%
copy from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
copy to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/EncryptRuleExistsException.java
index 105be1c..39ef700 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/EncryptRuleExistsException.java
@@ -15,22 +15,19 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.segment.rdl;
+package org.apache.shardingsphere.proxy.backend.exception;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
-
-import java.util.Collection;
/**
- * Encrypt rule segment.
+ * Encrypt rule exists exception.
*/
@RequiredArgsConstructor
@Getter
-public final class EncryptRuleSegment implements ASTNode {
+public final class EncryptRuleExistsException extends BackendException {
- private final String tableName;
+ private static final long serialVersionUID = -1673568903990383982L;
- private final Collection<EncryptColumnSegment> columns;
+ private final String schemaName;
}
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/InvalidEncryptorsException.java
similarity index 70%
copy from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
copy to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/InvalidEncryptorsException.java
index 105be1c..b6120c9 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/EncryptRuleSegment.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/InvalidEncryptorsException.java
@@ -15,22 +15,21 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.distsql.parser.segment.rdl;
+package org.apache.shardingsphere.proxy.backend.exception;
+import lombok.AllArgsConstructor;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Collection;
/**
- * Encrypt rule segment.
+ * Invalid encryptors exception.
*/
-@RequiredArgsConstructor
+@AllArgsConstructor
@Getter
-public final class EncryptRuleSegment implements ASTNode {
+public final class InvalidEncryptorsException extends BackendException {
- private final String tableName;
+ private static final long serialVersionUID = 3175781664028568140L;
- private final Collection<EncryptColumnSegment> columns;
+ private final Collection<String> encryptors;
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
index ae8b14d..b12a41d 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/RDLBackendHandlerFactory.java
@@ -26,6 +26,7 @@ import
org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterShardin
import
org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterShardingTableRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AddResourceStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateDatabaseDiscoveryRuleStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateEncryptRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateReadwriteSplittingRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingBroadcastTableRulesStatement;
@@ -49,6 +50,7 @@ import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.AlterShardi
import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.AlterShardingTableRuleBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateDatabaseBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateDatabaseDiscoveryRuleBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateEncryptRuleBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateReadwriteSplittingRuleBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateShardingBindingTableRulesBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl.CreateShardingBroadcastTableRulesBackendHandler;
@@ -157,6 +159,9 @@ public final class RDLBackendHandlerFactory {
if (sqlStatement instanceof DropDatabaseDiscoveryRuleStatement) {
return Optional.of(new
DropDatabaseDiscoveryRuleBackendHandler((DropDatabaseDiscoveryRuleStatement)
sqlStatement, backendConnection));
}
+ if (sqlStatement instanceof CreateEncryptRuleStatement) {
+ return Optional.of(new
CreateEncryptRuleBackendHandler((CreateEncryptRuleStatement) sqlStatement,
backendConnection));
+ }
return Optional.empty();
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java
new file mode 100644
index 0000000..81747b9
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandler.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
+
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateEncryptRuleStatement;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import
org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.converter.EncryptRuleConverter;
+import
org.apache.shardingsphere.governance.core.registry.listener.event.rule.RuleConfigurationsAlteredEvent;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+import org.apache.shardingsphere.infra.spi.typed.TypedSPIRegistry;
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import
org.apache.shardingsphere.proxy.backend.exception.EncryptRuleExistsException;
+import
org.apache.shardingsphere.proxy.backend.exception.InvalidEncryptorsException;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.text.SchemaRequiredBackendHandler;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * Create encrypt rule backend handler.
+ */
+public final class CreateEncryptRuleBackendHandler extends
SchemaRequiredBackendHandler<CreateEncryptRuleStatement> {
+
+ public CreateEncryptRuleBackendHandler(final CreateEncryptRuleStatement
sqlStatement, final BackendConnection backendConnection) {
+ super(sqlStatement, backendConnection);
+ }
+
+ @Override
+ public ResponseHeader execute(final String schemaName, final
CreateEncryptRuleStatement sqlStatement) {
+ check(schemaName, sqlStatement);
+ YamlEncryptRuleConfiguration config =
EncryptRuleConverter.convert(sqlStatement.getEncryptRules());
+ Collection<RuleConfiguration> rules = new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(config));
+ post(schemaName, rules);
+ return new UpdateResponseHeader(sqlStatement);
+ }
+
+ private void check(final String schemaName, final
CreateEncryptRuleStatement sqlStatement) {
+ if
(ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream().anyMatch(each
-> each instanceof EncryptRuleConfiguration)) {
+ throw new EncryptRuleExistsException(schemaName);
+ }
+ // TODO check resource
+ Collection<String> encryptors = new LinkedHashSet<>();
+ sqlStatement.getEncryptRules().stream().forEach(each ->
encryptors.addAll(each.getColumns().stream()
+ .map(column ->
column.getEncryptor().getAlgorithmName()).collect(Collectors.toSet())));
+ Collection<String> invalidEncryptors = encryptors.stream().filter(each
-> !TypedSPIRegistry.findRegisteredService(EncryptAlgorithm.class, each, new
Properties()).isPresent())
+ .collect(Collectors.toList());
+ if (!invalidEncryptors.isEmpty()) {
+ throw new InvalidEncryptorsException(invalidEncryptors);
+ }
+ }
+
+ private void post(final String schemaName, final
Collection<RuleConfiguration> rules) {
+ ShardingSphereEventBus.getInstance().post(new
RuleConfigurationsAlteredEvent(schemaName, rules));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandlerTest.java
new file mode 100644
index 0000000..77e4085
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateEncryptRuleBackendHandlerTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
+
+import com.google.common.collect.Maps;
+import org.apache.shardingsphere.distsql.parser.segment.FunctionSegment;
+import
org.apache.shardingsphere.distsql.parser.segment.rdl.EncryptColumnSegment;
+import org.apache.shardingsphere.distsql.parser.segment.rdl.EncryptRuleSegment;
+import
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateEncryptRuleStatement;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import
org.apache.shardingsphere.proxy.backend.exception.EncryptRuleExistsException;
+import
org.apache.shardingsphere.proxy.backend.exception.InvalidEncryptorsException;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class CreateEncryptRuleBackendHandlerTest {
+
+ @Mock
+ private BackendConnection backendConnection;
+
+ @Mock
+ private CreateEncryptRuleStatement sqlStatement;
+
+ @Mock
+ private MetaDataContexts metaDataContexts;
+
+ @Mock
+ private TransactionContexts transactionContexts;
+
+ @Mock
+ private ShardingSphereMetaData shardingSphereMetaData;
+
+ @Mock
+ private ShardingSphereRuleMetaData ruleMetaData;
+
+ private CreateEncryptRuleBackendHandler handler = new
CreateEncryptRuleBackendHandler(sqlStatement, backendConnection);
+
+ @Before
+ public void setUp() {
+ ShardingSphereServiceLoader.register(EncryptAlgorithm.class);
+ ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
+
when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singletonList("test"));
+
when(metaDataContexts.getMetaData(eq("test"))).thenReturn(shardingSphereMetaData);
+
when(shardingSphereMetaData.getRuleMetaData()).thenReturn(ruleMetaData);
+ }
+
+ @Test
+ public void assertExecute() {
+ EncryptRuleSegment encryptRuleSegment = new
EncryptRuleSegment("t_encrypt", buildColumns("MD5"));
+
when(sqlStatement.getEncryptRules()).thenReturn(Collections.singletonList(encryptRuleSegment));
+ ResponseHeader responseHeader = handler.execute("test", sqlStatement);
+ assertNotNull(responseHeader);
+ assertTrue(responseHeader instanceof UpdateResponseHeader);
+ }
+
+ @Test(expected = EncryptRuleExistsException.class)
+ public void assertExecuteWithExistEncryptRule() {
+
when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new
EncryptRuleConfiguration(Collections.emptyList(), Maps.newHashMap())));
+ handler.execute("test", sqlStatement);
+ }
+
+ @Test(expected = InvalidEncryptorsException.class)
+ public void assertExecuteWithInvalidEncryptors() {
+ EncryptRuleSegment encryptRuleSegment = new
EncryptRuleSegment("t_encrypt", buildColumns("notExistEncryptor"));
+
when(sqlStatement.getEncryptRules()).thenReturn(Collections.singletonList(encryptRuleSegment));
+ handler.execute("test", sqlStatement);
+ }
+
+ private Collection<EncryptColumnSegment> buildColumns(final String
encryptorName) {
+ EncryptColumnSegment encryptColumnSegment = new EncryptColumnSegment();
+ encryptColumnSegment.setName("user_id");
+ encryptColumnSegment.setPlainColumn("user_plain");
+ encryptColumnSegment.setCipherColumn("user_cipher");
+ FunctionSegment functionSegment = new FunctionSegment();
+ functionSegment.setAlgorithmName(encryptorName);
+ encryptColumnSegment.setEncryptor(functionSegment);
+ return Collections.singleton(encryptColumnSegment);
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
index 05e96b0..46eccd8 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
@@ -32,7 +32,9 @@ import
org.apache.shardingsphere.proxy.backend.exception.DatabaseDiscoveryRuleEx
import
org.apache.shardingsphere.proxy.backend.exception.DatabaseDiscoveryRuleNotExistedException;
import
org.apache.shardingsphere.proxy.backend.exception.DuplicateResourceException;
import
org.apache.shardingsphere.proxy.backend.exception.DuplicateTablesException;
+import
org.apache.shardingsphere.proxy.backend.exception.EncryptRuleExistsException;
import
org.apache.shardingsphere.proxy.backend.exception.InvalidDatabaseDiscoveryTypesException;
+import
org.apache.shardingsphere.proxy.backend.exception.InvalidEncryptorsException;
import
org.apache.shardingsphere.proxy.backend.exception.InvalidLoadBalancersException;
import
org.apache.shardingsphere.proxy.backend.exception.InvalidResourceException;
import
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
@@ -198,6 +200,12 @@ public final class MySQLErrPacketFactory {
if (cause instanceof
DatabaseDiscoveryRuleDataSourcesNotExistedException) {
return new MySQLErrPacket(1,
CommonErrorCode.DATABASE_DISCOVERY_RULE_DATASOURCE_NOT_EXIST,
((DatabaseDiscoveryRuleDataSourcesNotExistedException) cause).getRuleNames());
}
+ if (cause instanceof EncryptRuleExistsException) {
+ return new MySQLErrPacket(1, CommonErrorCode.ENCRYPT_RULE_EXIST,
((EncryptRuleExistsException) cause).getSchemaName());
+ }
+ if (cause instanceof InvalidEncryptorsException) {
+ return new MySQLErrPacket(1, CommonErrorCode.INVALID_ENCRYPTORS,
((InvalidEncryptorsException) cause).getEncryptors());
+ }
return new MySQLErrPacket(1, CommonErrorCode.UNKNOWN_EXCEPTION,
cause.getMessage());
}
}