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 2022/12/31 05:19:57 UTC

[shardingsphere] branch master updated: Implement TELEPHONE_RANDOM_REPLACE Algorithm (#23171)

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 ea9cee49978 Implement TELEPHONE_RANDOM_REPLACE Algorithm (#23171)
ea9cee49978 is described below

commit ea9cee499788d26db839c3daa367712bf6011a3b
Author: gxxiong <xi...@foxmail.com>
AuthorDate: Sat Dec 31 13:19:51 2022 +0800

    Implement TELEPHONE_RANDOM_REPLACE Algorithm (#23171)
    
    * Implement TELEPHONE_RANDOM_REPLACE Algorithm
    
    * format code
    
    * format code
    
    * format code and add document
    
    * add document
---
 docs/document/content/dev-manual/mask.cn.md        |  1 +
 docs/document/content/dev-manual/mask.en.md        |  1 +
 .../common-config/builtin-algorithm/mask.cn.md     | 10 ++-
 .../common-config/builtin-algorithm/mask.en.md     | 10 ++-
 .../replace/TelephoneRandomReplaceAlgorithm.java   | 92 ++++++++++++++++++++++
 ...rg.apache.shardingsphere.mask.spi.MaskAlgorithm |  1 +
 .../TelephoneRandomReplaceAlgorithmTest.java       | 63 +++++++++++++++
 7 files changed, 176 insertions(+), 2 deletions(-)

diff --git a/docs/document/content/dev-manual/mask.cn.md b/docs/document/content/dev-manual/mask.cn.md
index 1bf7f3197ef..daeeb21f274 100644
--- a/docs/document/content/dev-manual/mask.cn.md
+++ b/docs/document/content/dev-manual/mask.cn.md
@@ -26,3 +26,4 @@ chapter = true
 | MASK_FROM_X_TO_Y              | 遮盖自 x 至 y 数据脱敏算法 | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_FROM_X_TO_Y`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java)                 |
 | MASK_BEFORE_SPECIAL_CHARS     | 特殊字符前遮盖数据脱敏算法   | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_BEFORE_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java)                 |
 | MASK_AFTER_SPECIAL_CHARS      | 特殊字符后遮盖数据脱敏算法   | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_AFTER_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java)                 |
+| TELEPHONE_RANDOM_REPLACE      | ⼿机号随机替换数据脱敏算法   | [`org.apache.shardingsphere.mask.algorithm.replace.TELEPHONE_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java)                 |
diff --git a/docs/document/content/dev-manual/mask.en.md b/docs/document/content/dev-manual/mask.en.md
index 690ceb81dd2..16b09fe9fc5 100644
--- a/docs/document/content/dev-manual/mask.en.md
+++ b/docs/document/content/dev-manual/mask.en.md
@@ -26,3 +26,4 @@ Data masking algorithm definition
 | MASK_FROM_X_TO_Y              | Mask from x to y data masking algorithm          | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_FROM_X_TO_Y`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java)                 |
 | MASK_BEFORE_SPECIAL_CHARS     | Mask before special chars data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_BEFORE_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java)                 |
 | MASK_AFTER_SPECIAL_CHARS      | Mask after special chars data masking algorithm  | [`org.apache.shardingsphere.mask.algorithm.cover.MASK_AFTER_SPECIAL_CHARS`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java)                 |
+| TELEPHONE_RANDOM_REPLACE      | Random replacement of mobile phone number data masking algorithm  | [`org.apache.shardingsphere.mask.algorithm.replace.TELEPHONE_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java)                 |
diff --git a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md
index d6672b23fa8..67d407e708d 100644
--- a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md
+++ b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.cn.md
@@ -95,7 +95,15 @@ weight = 9
 
 ### 替换脱敏算法
 
-TODO
+#### TELEPHONE_RANDOM_REPLACE 脱敏算法
+
+类型:TELEPHONE_RANDOM_REPLACE
+
+可配置属性:
+
+| *名称*         | *数据类型* | *说明*         |
+|----------------|----------|----------------|
+| network-number | String   | ⽹号(以,分割输入)|
 
 ## 操作步骤
 1. 在脱敏规则中配置脱敏算法;
diff --git a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md
index 465c963596c..daeec814745 100644
--- a/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md
+++ b/docs/document/content/user-manual/common-config/builtin-algorithm/mask.en.md
@@ -95,7 +95,15 @@ Attributes:
 
 ### Replace Data Masking Algorithm
 
-TODO
+#### TELEPHONE_RANDOM_REPLACE Data Masking Algorithm
+
+Type: TELEPHONE_RANDOM_REPLACE
+
+Attributes:
+
+| *Name* | *DataType* | *Description*                    |
+|----------------|--------|----------------------------------------|
+| network-number | String | Network number (separate input with ,) |
 
 ## Operating Procedure
 1. Configure maskAlgorithms in a mask rule.
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java
new file mode 100644
index 00000000000..b1864ad2e66
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithm.java
@@ -0,0 +1,92 @@
+/*
+ * 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.algorithm.replace;
+
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+import java.util.stream.Collectors;
+
+/**
+ * Telephone random replace algorithm.
+ */
+public final class TelephoneRandomReplaceAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String NETWORK_NUMBER = "network-number";
+    
+    private Collection<String> networkNumbers;
+    
+    private List<Integer> networkNumberLength;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        createNetworkNumbers(props);
+    }
+    
+    private void createNetworkNumbers(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, NETWORK_NUMBER, getType());
+        networkNumbers = Splitter.on(",").trimResults().splitToStream(props.getProperty(NETWORK_NUMBER)).map(networkNumber -> isNumeric(networkNumber)).collect(Collectors.toSet());
+        networkNumberLength = networkNumbers.stream().map(networkNumber -> String.valueOf(networkNumber).length()).distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
+    }
+    
+    private String isNumeric(final String networkNumber) {
+        try {
+            Integer.parseInt(networkNumber);
+            return networkNumber;
+        } catch (NumberFormatException e) {
+            throw new MaskAlgorithmInitializationException(getType(), "network-number can only be numbers and ,");
+        }
+    }
+    
+    @Override
+    public String mask(final Object plainValue) {
+        String result = null == plainValue ? null : String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        Random random = new Random();
+        char[] chars = result.toCharArray();
+        for (Integer each : networkNumberLength) {
+            if (networkNumbers.contains(result.substring(0, each))) {
+                for (int i = each; i != -1 && i < chars.length; i++) {
+                    chars[i] = Character.forDigit(random.nextInt(10), 10);
+                }
+                break;
+            }
+        }
+        return new String(chars);
+    }
+    
+    @Override
+    public String getType() {
+        return "TELEPHONE_RANDOM_REPLACE";
+    }
+}
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
index fc69572429b..819502bc85f 100644
--- a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
@@ -22,3 +22,4 @@ org.apache.shardingsphere.mask.algorithm.cover.MaskAfterSpecialCharsAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.MaskBeforeSpecialCharsAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.MaskFirstNLastMMaskAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.MaskFromXToYMaskAlgorithm
+org.apache.shardingsphere.mask.algorithm.replace.TelephoneRandomReplaceAlgorithm
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithmTest.java
new file mode 100644
index 00000000000..fbf99994066
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/TelephoneRandomReplaceAlgorithmTest.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.mask.algorithm.replace;
+
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertNull;
+
+public final class TelephoneRandomReplaceAlgorithmTest {
+    
+    private TelephoneRandomReplaceAlgorithm maskAlgorithm;
+    
+    @Before
+    public void setUp() {
+        maskAlgorithm = new TelephoneRandomReplaceAlgorithm();
+        maskAlgorithm.init(createProperties("130, 130, 155,1702"));
+    }
+    
+    @Test(expected = MaskAlgorithmInitializationException.class)
+    public void assertKeyNotNumber() {
+        maskAlgorithm.init(createProperties("130, x130, 155,1702"));
+    }
+    
+    @Test
+    public void assertMaskWithNullPlaintext() {
+        assertNull(maskAlgorithm.mask(null));
+    }
+    
+    @Test
+    public void assertMask() {
+        assertThat(maskAlgorithm.mask("13012345678").substring(0, 3), is("130"));
+        assertThat(maskAlgorithm.mask("13012345678").substring(3, 11), not("12345678"));
+        assertThat(maskAlgorithm.mask("13012345678"), not("13012345678"));
+    }
+    
+    private Properties createProperties(final String networkNumber) {
+        Properties result = new Properties();
+        result.setProperty("network-number", networkNumber);
+        return result;
+    }
+}