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 9a4da7546a4 Add new encrypt rule configuration swapper (#25984)
9a4da7546a4 is described below
commit 9a4da7546a443411278fae83bc20d5e01bd26e03
Author: ChenJiaHao <[email protected]>
AuthorDate: Thu Jun 1 19:21:25 2023 +0800
Add new encrypt rule configuration swapper (#25984)
* Add new encrypt rule configuration swapper
* Change converter to static
---
features/encrypt/core/pom.xml | 5 ++
.../NewYamlEncryptRuleConfigurationSwapper.java | 79 ++++++++++++++++++++++
...NewYamlEncryptRuleConfigurationSwapperTest.java | 63 +++++++++++++++++
.../config/encrypt/EncryptNodeConverter.java | 53 +++++++++++++++
4 files changed, 200 insertions(+)
diff --git a/features/encrypt/core/pom.xml b/features/encrypt/core/pom.xml
index 8904d2683b0..d6367d6e57d 100644
--- a/features/encrypt/core/pom.xml
+++ b/features/encrypt/core/pom.xml
@@ -68,6 +68,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-metadata-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>commons-codec</groupId>
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapper.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..b2022d17357
--- /dev/null
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapper.java
@@ -0,0 +1,79 @@
+/*
+ * 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.swapper;
+
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.constant.EncryptOrder;
+import
org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import
org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
+import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.NewYamlRuleConfigurationSwapper;
+import
org.apache.shardingsphere.metadata.persist.node.metadata.config.encrypt.EncryptNodeConverter;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Map.Entry;
+
+/**
+ * TODO Rename to YamlEncryptRuleConfigurationSwapper when metadata structure
adjustment completed.
+ * New YAML encrypt rule configuration swapper.
+ */
+public final class NewYamlEncryptRuleConfigurationSwapper implements
NewYamlRuleConfigurationSwapper<EncryptRuleConfiguration> {
+
+ // TODO to be used
+ private final YamlEncryptTableRuleConfigurationSwapper tableSwapper = new
YamlEncryptTableRuleConfigurationSwapper();
+
+ private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new
YamlAlgorithmConfigurationSwapper();
+
+ @Override
+ public Collection<YamlDataNode> swapToDataNodes(final
EncryptRuleConfiguration data) {
+ Collection<YamlDataNode> result = new LinkedHashSet<>();
+ for (EncryptTableRuleConfiguration each : data.getTables()) {
+ result.add(new
YamlDataNode(EncryptNodeConverter.getTableNamePath(each.getName()),
YamlEngine.marshal(each)));
+ }
+ for (Entry<String, AlgorithmConfiguration> entry :
data.getEncryptors().entrySet()) {
+ result.add(new
YamlDataNode(EncryptNodeConverter.getEncryptorPath(entry.getKey()),
YamlEngine.marshal(entry.getValue())));
+ }
+ return result;
+ }
+
+ @Override
+ public EncryptRuleConfiguration swapToObject(final
Collection<YamlDataNode> dataNodes) {
+ // TODO to be completed
+ return new EncryptRuleConfiguration(Collections.emptyList(),
Collections.emptyMap());
+ }
+
+ @Override
+ public Class<EncryptRuleConfiguration> getTypeClass() {
+ return EncryptRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "ENCRYPT";
+ }
+
+ @Override
+ public int getOrder() {
+ return EncryptOrder.ORDER;
+ }
+}
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapperTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..42775eff7e0
--- /dev/null
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/NewYamlEncryptRuleConfigurationSwapperTest.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.swapper;
+
+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;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.datanode.YamlDataNode;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class NewYamlEncryptRuleConfigurationSwapperTest {
+
+ private final NewYamlEncryptRuleConfigurationSwapper swapper = new
NewYamlEncryptRuleConfigurationSwapper();
+
+ @Test
+ void assertSwapEmptyConfigToDataNodes() {
+ EncryptRuleConfiguration config = new
EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap());
+ Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+ assertThat(result.size(), is(0));
+ }
+
+ @Test
+ void assertSwapFullConfigToDataNodes() {
+ EncryptRuleConfiguration config = createMaximumEncryptRule();
+ Collection<YamlDataNode> result = swapper.swapToDataNodes(config);
+ assertThat(result.size(), is(2));
+ Iterator<YamlDataNode> iterator = result.iterator();
+ assertThat(iterator.next().getKey(), is("tables/foo"));
+ assertThat(iterator.next().getKey(), is("encryptors/FOO"));
+ }
+
+ private EncryptRuleConfiguration createMaximumEncryptRule() {
+ Collection<EncryptTableRuleConfiguration> tables = new LinkedList<>();
+ tables.add(new EncryptTableRuleConfiguration("foo",
Collections.singleton(new EncryptColumnRuleConfiguration("foo_column", new
EncryptColumnItemRuleConfiguration("FIXTURE")))));
+ return new EncryptRuleConfiguration(tables,
Collections.singletonMap("FOO", new AlgorithmConfiguration("FOO", new
Properties())));
+ }
+}
diff --git
a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/encrypt/EncryptNodeConverter.java
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/encrypt/EncryptNodeConverter.java
new file mode 100644
index 00000000000..8c5df783d06
--- /dev/null
+++
b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/metadata/config/encrypt/EncryptNodeConverter.java
@@ -0,0 +1,53 @@
+/*
+ * 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.metadata.persist.node.metadata.config.encrypt;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * TODO move to features module
+ * Encrypt node converter.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class EncryptNodeConverter {
+
+ private static final String TABLES = "tables";
+
+ private static final String ENCRYPTORS = "encryptors";
+
+ /**
+ * Get table name path.
+ *
+ * @param tableName table name
+ * @return table name path
+ */
+ public static String getTableNamePath(final String tableName) {
+ return String.join("/", TABLES, tableName);
+ }
+
+ /**
+ * Get encryptor path.
+ *
+ * @param encryptorName encryptor name
+ * @return encryptor path
+ */
+ public static String getEncryptorPath(final String encryptorName) {
+ return String.join("/", ENCRYPTORS, encryptorName);
+ }
+}