You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/07/24 07:13:55 UTC

[shardingsphere] branch master updated: Add mask rule checker (#27414)

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

duanzhengqiang 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 456d5588fae Add  mask rule checker (#27414)
456d5588fae is described below

commit 456d5588fae1fedc5add0338ded8bb8b6e5bb7d7
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Mon Jul 24 15:13:48 2023 +0800

    Add  mask rule checker (#27414)
    
    * Add  mask rule checker
    
    * Add final
    
    * Add document
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../mask/checker/MaskRuleConfigurationChecker.java | 65 ++++++++++++++++++++++
 .../checker/InvalidMaskAlgorithmNameException.java | 33 +++++++++++
 ...ra.config.rule.checker.RuleConfigurationChecker | 19 +++++++
 5 files changed, 119 insertions(+)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index ef786510acb..0a05dda66a5 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -279,6 +279,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | SQL State | Vendor Code | 错误信息                                                        |
 |-----------|-------------|-------------------------------------------------------------|
 | HY000     | 20980       | Mask algorithm \`%s\` initialization failed, reason is: %s. |
+| 42S02     | 20990       | Invalid mask algorithm \`%s\` in database \`%s\`.           |
 
 ## 其他异常
 
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 7fe1c61e5ab..4a6f5e7df06 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -294,6 +294,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | SQL State | Vendor Code | Reason                                                      |
 |-----------|-------------|-------------------------------------------------------------|
 | HY000     | 20980       | Mask algorithm \`%s\` initialization failed, reason is: %s. |
+| 42S02     | 20990       | Invalid mask algorithm \`%s\` in database \`%s\`.           |
 
 ## Other Exception
 
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java
new file mode 100644
index 00000000000..37cd8376b2a
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java
@@ -0,0 +1,65 @@
+/*
+ * 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.mask.checker;
+
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.constant.MaskOrder;
+import org.apache.shardingsphere.mask.exception.checker.InvalidMaskAlgorithmNameException;
+
+import javax.sql.DataSource;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Mask rule configuration checker.
+ */
+public final class MaskRuleConfigurationChecker implements RuleConfigurationChecker<MaskRuleConfiguration> {
+    
+    @Override
+    public void check(final String databaseName, final MaskRuleConfiguration config, final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules) {
+        checkTables(databaseName, config.getTables(), config.getMaskAlgorithms());
+    }
+    
+    private void checkTables(final String databaseName, final Collection<MaskTableRuleConfiguration> tables, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
+        for (MaskTableRuleConfiguration each : tables) {
+            checkColumns(databaseName, each.getColumns(), maskAlgorithms);
+        }
+    }
+    
+    private void checkColumns(final String databaseName, final Collection<MaskColumnRuleConfiguration> columns, final Map<String, AlgorithmConfiguration> maskAlgorithms) {
+        for (MaskColumnRuleConfiguration each : columns) {
+            ShardingSpherePreconditions.checkState(maskAlgorithms.containsKey(each.getMaskAlgorithm()), () -> new InvalidMaskAlgorithmNameException(databaseName, each.getMaskAlgorithm()));
+        }
+    }
+    
+    @Override
+    public int getOrder() {
+        return MaskOrder.ORDER;
+    }
+    
+    @Override
+    public Class<MaskRuleConfiguration> getTypeClass() {
+        return MaskRuleConfiguration.class;
+    }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/checker/InvalidMaskAlgorithmNameException.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/checker/InvalidMaskAlgorithmNameException.java
new file mode 100644
index 00000000000..1599d976b30
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/checker/InvalidMaskAlgorithmNameException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.mask.exception.checker;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.mask.exception.MaskSQLException;
+
+/**
+ * Invalid mask algorithm name exception.
+ */
+public final class InvalidMaskAlgorithmNameException extends MaskSQLException {
+    
+    private static final long serialVersionUID = 1586639735142430235L;
+    
+    public InvalidMaskAlgorithmNameException(final String databaseName, final String maskAlgorithmName) {
+        super(XOpenSQLState.NOT_FOUND, 90, "Invalid mask algorithm `%s` in database `%s`.", maskAlgorithmName, databaseName);
+    }
+}
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
new file mode 100644
index 00000000000..c59d97271b1
--- /dev/null
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.mask.checker.MaskRuleConfigurationChecker
+