HealthCheckUtils { public static void maskPasswords(final Map<String, Object> map) { map.entrySet().forEach(entry -> { String key = entry.getKey(); if (entry.getValue() instanceof String) { if (key.toLowerCase().contains("password")) { LOGGER.debug("Masking entry with key {}", key); map.put(key, "****"); } else if (key.toLowerCase().contains("apikey") || key.toLowerCase().contains("token")) { LOGGER.debug("Masking entry with key {}", key); String oldValue = (String) entry.getValue(); if (oldValue.length() <= 8) { map.put(key, "****"); } else { String newValue = oldValue.substring(0, 4) + "****" + oldValue.substring(oldValue.length() - 4); map.put(key, newValue); } } } else if (entry.getValue() instanceof Map) { maskPasswords((Map<String, Object>) entry.getValue()); } else if (entry.getValue() instanceof List) { for (Object item : (List<Object>)entry.getValue()) { if (item instanceof Map) { maskPasswords((Map<String, Object>) item); } } } }); }  static String validateHttpConnection(final String httpMethod, final String urlStr); static Map<String, Object> beanToMap(final Object object); static void maskPasswords(final Map<String, Object> map);  }
@Test void maskPasswords() { Map<String, Object> root = new HashMap<>(); Map<String, Object> subMap = new HashMap<>(); root.put("xxx", 1); root.put("yyy", "abc"); root.put("abcPassword", "1234"); root.put("passwordDEF", "1234"); root.put("aToken", "1234"); root.put("aLongerToken", "1234567890"); root.put("aApiKey", "1234"); root.put("aLongerApiKey", "1234567890"); subMap.put("xxx", 1); subMap.put("yyy", "abc"); subMap.put("abcPassword", "1234"); subMap.put("passwordDEF", "1234"); root.put("submap", subMap); HealthCheckUtils.maskPasswords(root); Assertions.assertThat(root.get("abcPassword")).isEqualTo("****"); Assertions.assertThat(root.get("passwordDEF")).isEqualTo("****"); Assertions.assertThat(root.get("aToken")).isEqualTo("****"); Assertions.assertThat(root.get("aLongerToken")).isEqualTo("1234****7890"); Assertions.assertThat(root.get("aApiKey")).isEqualTo("****"); Assertions.assertThat(root.get("aLongerApiKey")).isEqualTo("1234****7890"); Assertions.assertThat(((Map)root.get("submap")).get("abcPassword")).isEqualTo("****"); Assertions.assertThat(((Map)root.get("submap")).get("passwordDEF")).isEqualTo("****"); LOGGER.info("root map: {}", root); System.out.println(root.toString()); }