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/13 01:06:50 UTC
[shardingsphere] branch master updated: Implement MASK_FIRST_N_LAST_M and MASK_FROM_X_TO_Y (#22808)
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 69fc6d534d7 Implement MASK_FIRST_N_LAST_M and MASK_FROM_X_TO_Y (#22808)
69fc6d534d7 is described below
commit 69fc6d534d7b6f6155031d2df01562bcdd86e703
Author: lushaorong <ww...@163.com>
AuthorDate: Tue Dec 13 09:06:40 2022 +0800
Implement MASK_FIRST_N_LAST_M and MASK_FROM_X_TO_Y (#22808)
* shardingsphere-22759 Implement new data masking feature for users with security needs
Signed-off-by: lushaorong <ww...@163.com>
* fix code style
Signed-off-by: lushaorong <ww...@163.com>
Signed-off-by: lushaorong <ww...@163.com>
---
.../cover/MaskFirstNLastMMaskAlgorithm.java | 91 ++++++++++++++++++++++
.../algorithm/cover/MaskFromXToYMaskAlgorithm.java | 87 +++++++++++++++++++++
.../cover/MaskFirstNLastMMaskAlgorithmTest.java | 60 ++++++++++++++
.../cover/MaskFromXToYMaskAlgorithmTest.java | 61 +++++++++++++++
4 files changed, 299 insertions(+)
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java
new file mode 100644
index 00000000000..114667614df
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java
@@ -0,0 +1,91 @@
+/*
+ * 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.cover;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * Mask first n last m mask algorithm.
+ */
+public final class MaskFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String FIRST_N = "first-n";
+
+ private static final String LAST_M = "last-m";
+
+ private static final String REPLACE_CHAR = "replace-char";
+
+ private Integer firstN;
+
+ private Integer lastM;
+
+ private Character replaceChar;
+
+ @Getter
+ private Properties props;
+
+ @Override
+ public String mask(final Object plainValue) {
+ String result = String.valueOf(plainValue);
+ if (Strings.isNullOrEmpty(result)) {
+ return result;
+ }
+ char[] chars = result.toCharArray();
+ for (int i = 0, len = Math.min(firstN, chars.length); i < len; i++) {
+ chars[i] = replaceChar;
+ }
+ for (int i = chars.length - Math.min(lastM, chars.length); i < chars.length; i++) {
+ chars[i] = replaceChar;
+ }
+ return new String(chars);
+ }
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ this.firstN = getN(props);
+ this.lastM = getM(props);
+ this.replaceChar = getReplaceChar(props);
+ }
+
+ private Integer getN(final Properties props) {
+ Preconditions.checkArgument(props.containsKey(FIRST_N), "%s can not be null.", FIRST_N);
+ return Integer.parseInt(props.getProperty(FIRST_N));
+ }
+
+ private Integer getM(final Properties props) {
+ Preconditions.checkArgument(props.containsKey(LAST_M), "%s can not be null.", LAST_M);
+ return Integer.parseInt(props.getProperty(LAST_M));
+ }
+
+ private Character getReplaceChar(final Properties props) {
+ Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
+ Preconditions.checkArgument(1 == props.getProperty(REPLACE_CHAR).length(), "%s length must be 1.", REPLACE_CHAR);
+ return props.getProperty(REPLACE_CHAR).charAt(0);
+ }
+
+ @Override
+ public String getType() {
+ return "MASK_FIRST_N_LAST_M";
+ }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
new file mode 100644
index 00000000000..3e33e0fcb66
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
@@ -0,0 +1,87 @@
+/*
+ * 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.cover;
+
+import com.google.common.base.Preconditions;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * Mask from x to y mask algorithm.
+ */
+public final class MaskFromXToYMaskAlgorithm implements MaskAlgorithm<Object, String> {
+
+ private static final String FROM_X = "from-x";
+
+ private static final String TO_Y = "to-y";
+
+ private static final String REPLACE_CHAR = "replace-char";
+
+ private Integer fromX;
+
+ private Integer toY;
+
+ private Character replaceChar;
+
+ @Getter
+ private Properties props;
+
+ @Override
+ public String mask(final Object plainValue) {
+ String value = plainValue == null ? "" : plainValue.toString();
+ if ("".equals(value) || value.length() <= fromX || toY < fromX) {
+ return value;
+ }
+ char[] chars = value.toCharArray();
+ for (int i = fromX, len = Math.min(toY, chars.length - 1); i <= len; i++) {
+ chars[i] = replaceChar;
+ }
+ return new String(chars);
+ }
+
+ @Override
+ public void init(final Properties props) {
+ this.props = props;
+ this.fromX = getX(props);
+ this.toY = getY(props);
+ this.replaceChar = getReplaceChar(props);
+ }
+
+ private Integer getX(final Properties props) {
+ Preconditions.checkArgument(props.containsKey(FROM_X), "%s can not be null.", FROM_X);
+ return Integer.parseInt(props.getProperty(FROM_X));
+ }
+
+ private Integer getY(final Properties props) {
+ Preconditions.checkArgument(props.containsKey(TO_Y), "%s can not be null.", TO_Y);
+ return Integer.parseInt(props.getProperty(TO_Y));
+ }
+
+ private Character getReplaceChar(final Properties props) {
+ Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
+ Preconditions.checkArgument(1 == props.getProperty(REPLACE_CHAR).length(), "%s length must be 1.", REPLACE_CHAR);
+ return props.getProperty(REPLACE_CHAR).charAt(0);
+ }
+
+ @Override
+ public String getType() {
+ return "MASK_FROM_X_TO_Y";
+ }
+}
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
new file mode 100644
index 00000000000..3741b9391ee
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.cover;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class MaskFirstNLastMMaskAlgorithmTest {
+
+ private MaskFirstNLastMMaskAlgorithm algorithm;
+
+ @Before
+ public void setUp() {
+ algorithm = new MaskFirstNLastMMaskAlgorithm();
+ algorithm.init(createProperties("3", "5", "*"));
+ }
+
+ @Test
+ public void assertMask() {
+ assertThat(algorithm.mask("abc12345678"), is("***123*****"));
+ }
+
+ @Test
+ public void assertMaskWithShortPlainValue() {
+ assertThat(algorithm.mask("ab"), is("**"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void assertMaskWithInvalidProperties() {
+ algorithm.init(createProperties("", "5", "*"));
+ }
+
+ private Properties createProperties(final String firstN, final String lastM, final String replaceChar) {
+ Properties result = new Properties();
+ result.setProperty("first-n", firstN);
+ result.setProperty("last-m", lastM);
+ result.setProperty("replace-char", replaceChar);
+ return result;
+ }
+}
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
new file mode 100644
index 00000000000..748b17670a4
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.cover;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class MaskFromXToYMaskAlgorithmTest {
+
+ private MaskFromXToYMaskAlgorithm algorithm;
+
+ @Before
+ public void setUp() {
+ algorithm = new MaskFromXToYMaskAlgorithm();
+ algorithm.init(createProperties("3", "5", "*"));
+ }
+
+ @Test
+ public void assertMask() {
+ assertThat(algorithm.mask("abc12345"), is("abc***45"));
+ }
+
+ @Test
+ public void assertMaskWithShortPlainValue() {
+ assertThat(algorithm.mask("ab"), is("ab"));
+ assertThat(algorithm.mask("abc1"), is("abc*"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void assertMaskWithInvalidProperties() {
+ algorithm.init(createProperties("5", "", "*"));
+ }
+
+ private Properties createProperties(final String fromX, final String toY, final String replaceChar) {
+ Properties result = new Properties();
+ result.setProperty("from-x", fromX);
+ result.setProperty("to-y", toY);
+ result.setProperty("replace-char", replaceChar);
+ return result;
+ }
+}