SensitiveInfoUtils { public static List<String> filterSensitiveIps(final List<String> result) { final Map<String, String> fakeIpMap = new HashMap<>(); final String fakeIpSample = "ip"; final AtomicInteger step = new AtomicInteger(); Function<String, String> func = new Function<String, String>() { @Override public String apply(final String line) { final String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; final Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(line); String result = line; while (matcher.find()) { String realIp = matcher.group(); String fakeIp = fakeIpMap.get(realIp); if (Strings.isNullOrEmpty(fakeIp)) { fakeIp = fakeIpSample + step.incrementAndGet(); fakeIpMap.put(realIp, fakeIp); } result = result.replace(realIp, fakeIp); } return result; } }; return Lists.transform(result, func); }  static List<String> filterSensitiveIps(final List<String> result);  }
@Test public void assertFilterContentWithoutIp() { List<String> content = new ArrayList<>(); content.add("/simpleElasticDemoJob/servers"); content.add("/simpleElasticDemoJob/leader"); assertThat(SensitiveInfoUtils.filterSensitiveIps(content), is(content)); }
@Test public void assertFilterContentWithSensitiveIp() { List<String> content = new ArrayList<>(2); content.add("/simpleElasticDemoJob/servers/127.0.0.1"); content.add("/simpleElasticDemoJob/servers/192.168.0.1/hostName | 192.168.0.1"); List<String> expected = new ArrayList<>(2); expected.add("/simpleElasticDemoJob/servers/ip1"); expected.add("/simpleElasticDemoJob/servers/ip2/hostName | ip2"); assertThat(SensitiveInfoUtils.filterSensitiveIps(content), is(expected)); }
@Test public void assertFilterContentWithSensitiveIp2() { List<String> content = new ArrayList<>(2); content.add("/simpleElasticDemoJob/servers/127.0.0.1"); content.add("/simpleElasticDemoJob/servers/192.168.0.1/desc | 127.0.0.1"); List<String> expected = new ArrayList<>(2); expected.add("/simpleElasticDemoJob/servers/ip1"); expected.add("/simpleElasticDemoJob/servers/ip2/desc | ip1"); assertThat(SensitiveInfoUtils.filterSensitiveIps(content), is(expected)); }