Skip to content

Commit

Permalink
replace unknown ip with string IP
Browse files Browse the repository at this point in the history
  • Loading branch information
317787106 committed Nov 14, 2024
1 parent a154dcb commit 7bd057d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public static void addSensitive(String key, String value) {
sensitiveCache.put(key, value);
}

public String desensitization(String content) {
private String desensitization(String content) {
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String key = matcher.group();
String value = sensitiveCache.getIfPresent(key);
if (value != null) {
content = content.replaceAll(key, value);
} else {
content = content.replaceAll(key, "unknown");
content = content.replaceAll(key, "IP");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package org.tron.common.logsfilter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Assert;
import org.junit.Test;

public class DesensitizedConverterTest {

@Test
public void testReplace() {
public void testReplace()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
DesensitizedConverter converter = new DesensitizedConverter();
DesensitizedConverter.addSensitive("/192.168.1.10", "address1");
DesensitizedConverter.addSensitive("/197.168.1.10", "address2");
DesensitizedConverter.addSensitive("192.168.1.10", "address1");
DesensitizedConverter.addSensitive("197.168.1.10", "address2");

Method method = converter.getClass().getDeclaredMethod(
"desensitization", String.class);
method.setAccessible(true);

String logStr1 = "This is test log /192.168.1.10:100, /197.168.1.10:200, /197.168.1.10:100";
Assert.assertEquals("This is test log address1:100, address2:200, address2:100",
converter.desensitization(logStr1));
String result1 = (String) method.invoke(converter, logStr1);
Assert.assertEquals("This is test log /address1:100, /address2:200, /address2:100",
result1);

String logStr2 = "This is test log /192.168.1.100:100, /197.168.1.10:200, /197.168.1.10:100";
Assert.assertEquals("This is test log unknown:100, address2:200, address2:100",
converter.desensitization(logStr2));
String result2 = (String) method.invoke(converter, logStr2);
Assert.assertEquals("This is test log /IP:100, /address2:200, /address2:100",
result2);
}
}

0 comments on commit 7bd057d

Please sign in to comment.