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/01/09 01:55:16 UTC

[shardingsphere] branch master updated: Implement LANDLINE_NUMBER_RANDOM_REPLACE Algorithm (#23404)

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 40afcdac9a2 Implement LANDLINE_NUMBER_RANDOM_REPLACE Algorithm (#23404)
40afcdac9a2 is described below

commit 40afcdac9a29e00b1a7a32bdc82b4f04485d5f92
Author: lushaorong <ww...@163.com>
AuthorDate: Mon Jan 9 09:55:00 2023 +0800

    Implement LANDLINE_NUMBER_RANDOM_REPLACE Algorithm (#23404)
    
    * Implement LANDLINE_NUMBER_RANDOM_REPLACE Algorithm
    
    Signed-off-by: lushaorong <ww...@163.com>
    
    * add final
    
    Signed-off-by: lushaorong <ww...@163.com>
    
    * fix code style
    
    Signed-off-by: lushaorong <ww...@163.com>
    
    * update mask algorithm document
    
    Signed-off-by: lushaorong <ww...@163.com>
    
    Signed-off-by: lushaorong <ww...@163.com>
---
 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/LandlineNumberRandomAlgorithm.java     | 76 ++++++++++++++++++++++
 ...rg.apache.shardingsphere.mask.spi.MaskAlgorithm |  1 +
 .../replace/LandlineNumberRandomAlgorithmTest.java | 45 +++++++++++++
 7 files changed, 144 insertions(+)

diff --git a/docs/document/content/dev-manual/mask.cn.md b/docs/document/content/dev-manual/mask.cn.md
index 77b2da728d9..ddad58fe578 100644
--- a/docs/document/content/dev-manual/mask.cn.md
+++ b/docs/document/content/dev-manual/mask.cn.md
@@ -29,3 +29,4 @@ chapter = true
 | PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE | 身份证号随机替换数据脱敏算法   | [`org.apache.shardingsphere.mask.algorithm.replace.PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java)                 |
 | MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE | 军官证随机替换数据脱敏算法    | [`org.apache.shardingsphere.mask.algorithm.replace.MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.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)                 |
+| LANDLINE_NUMBER_RANDOM_REPLACE          | 座机号码随机替换            | [`org.apache.shardingsphere.mask.algorithm.replace.LANDLINE_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java)                 |
diff --git a/docs/document/content/dev-manual/mask.en.md b/docs/document/content/dev-manual/mask.en.md
index 00c00b466a8..d633049f1be 100644
--- a/docs/document/content/dev-manual/mask.en.md
+++ b/docs/document/content/dev-manual/mask.en.md
@@ -29,3 +29,4 @@ Data masking algorithm definition
 | PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE | Personal identity number random replace data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.replace.PERSONAL_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java)                 |
 | MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE | Military identity number random replace data masking algorithm | [`org.apache.shardingsphere.mask.algorithm.replace.MILITARY_IDENTITY_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithm.java)                 |
 | TELEPHONE_RANDOM_REPLACE                | Telephone random replace 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)                 |
+| LANDLINE_NUMBER_RANDOM_REPLACE          | Landline number random replace data masking algorithm            | [`org.apache.shardingsphere.mask.algorithm.replace.LANDLINE_NUMBER_RANDOM_REPLACE`](https://github.com/apache/shardingsphere/blob/master/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.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 435e566db16..d5f33970e0d 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
@@ -125,6 +125,16 @@ weight = 9
 |-----------------|----------|-------------|
 | network-numbers | String   | ⽹号(以英文逗号分隔) |
 
+#### 座机号码随机替换
+
+类型:LANDLINE_NUMBER_RANDOM_REPLACE
+
+可配置属性:
+
+| *名称*            | *数据类型* | *说明*        |
+|-----------------|----------|-------------|
+| landline-numbers | String   | 座机号码(以英文逗号分隔) |
+
 ## 操作步骤
 1. 在脱敏规则中配置脱敏算法;
 2. 为脱敏算法指定脱敏算法类型。
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 55729daa47a..d07b906113e 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
@@ -125,6 +125,16 @@ Attributes:
 |-----------------|--------|---------------------------------------|
 | network-numbers | String | Network numbers (separate with comma) |
 
+#### Landline Number Random Replace Data Masking Algorithm
+
+Type: LANDLINE_NUMBER_RANDOM_REPLACE
+
+Attributes:
+
+| *Name*          | *DataType* | *Description*                         |
+|-----------------|--------|---------------------------------------|
+| landline-numbers | String | Landline numbers (separate with comma) |
+
 ## Operating Procedure
 1. Configure maskAlgorithms in a mask rule.
 2. Use relevant algorithm types in maskAlgorithms.
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java
new file mode 100644
index 00000000000..06e38bbab12
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java
@@ -0,0 +1,76 @@
+/*
+ * 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.spi.MaskAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.Random;
+
+/**
+ * Landline number replace algorithm.
+ */
+public final class LandlineNumberRandomAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String LANDLINE_NUMBERS = "landline-numbers";
+    
+    private List<String> landLineNumbers;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        this.landLineNumbers = createLandLineNumbers(props);
+    }
+    
+    private List<String> createLandLineNumbers(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, LANDLINE_NUMBERS, getType());
+        return Splitter.on(",").trimResults().splitToList(props.getProperty(LANDLINE_NUMBERS));
+    }
+    
+    @Override
+    public String mask(final Object plainValue) {
+        String result = null == plainValue ? null : String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        return landLineNumbers.stream().filter(result::startsWith).findFirst().map(each -> createRandValue(result, each)).orElse(result);
+    }
+    
+    private String createRandValue(final String plainValue, final String landLineNumber) {
+        Random random = new Random();
+        StringBuilder result = new StringBuilder();
+        result.append(landLineNumbers.get(random.nextInt(landLineNumbers.size())));
+        for (int i = landLineNumber.length(); i < plainValue.length(); i++) {
+            result.append(Character.forDigit(random.nextInt(10), 10));
+        }
+        return result.toString();
+    }
+    
+    @Override
+    public String getType() {
+        return "LANDLINE_NUMBER_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 22178d046a5..6984bea317d 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
@@ -25,3 +25,4 @@ org.apache.shardingsphere.mask.algorithm.cover.MaskFromXToYMaskAlgorithm
 org.apache.shardingsphere.mask.algorithm.replace.TelephoneRandomReplaceAlgorithm
 org.apache.shardingsphere.mask.algorithm.replace.PersonalIdentityNumberRandomReplaceAlgorithm
 org.apache.shardingsphere.mask.algorithm.replace.MilitaryIdentityNumberRandomReplaceAlgorithm
+org.apache.shardingsphere.mask.algorithm.replace.LandlineNumberRandomAlgorithm
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java
new file mode 100644
index 00000000000..2dc627178b5
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class LandlineNumberRandomAlgorithmTest {
+    
+    private LandlineNumberRandomAlgorithm maskAlgorithm;
+    
+    @Before
+    public void setUp() {
+        maskAlgorithm = new LandlineNumberRandomAlgorithm();
+        maskAlgorithm.init(PropertiesBuilder.build(new Property("landline-numbers", "025, 027, 028, 029, 0310, 0311, 0313")));
+    }
+    
+    @Test
+    public void assertMask() {
+        assertThat(maskAlgorithm.mask(""), is(""));
+        assertThat(maskAlgorithm.mask("0251234567"), not("0251234567"));
+        assertThat(maskAlgorithm.mask("03101234567"), not("03101234567"));
+    }
+}