SensitiveConfigScrubber { public Map<String, Object> filterSensitiveOptions(final Map<String, Object> config, final Cluster cluster) { Objects.requireNonNull(config); final Map<String, Object> copy = new HashMap<>(config); final String[] sensitiveKeys = new String[] { SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, }; Arrays.stream(sensitiveKeys) .filter(copy::containsKey) .forEach((key) -> copy.put(key, "**HIDDEN**")); if (copy.containsKey(SaslConfigs.SASL_JAAS_CONFIG) && cluster != null) { final SaslProperties saslProperties = saslUtility.decodeProperties(cluster); if (saslProperties.getPlainPassword() != null && !saslProperties.getPlainPassword().isEmpty()) { String jaasConfig = (String) copy.get(SaslConfigs.SASL_JAAS_CONFIG); jaasConfig = jaasConfig.replaceAll(saslProperties.getPlainPassword(), "**HIDDEN**"); copy.put(SaslConfigs.SASL_JAAS_CONFIG, jaasConfig); } } return copy; } @Autowired  SensitiveConfigScrubber(final SaslUtility saslUtility); Map<String, Object> filterSensitiveOptions(final Map<String, Object> config, final Cluster cluster);  }
@Test public void smokeTest() { final String secretPhrase = "secret"; final SaslProperties saslProperties = new SaslProperties("Username", secretPhrase, "Anything", "Something"); final Cluster cluster = new Cluster(); cluster.setName("My Test Cluster"); cluster.setSaslConfig(saslUtility.encryptProperties(saslProperties)); cluster.setSaslEnabled(true); final Map<String, Object> mockConfig = new HashMap<>(); mockConfig.put("Key1", "Value1"); mockConfig.put("Key2", "Value2"); mockConfig.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, secretPhrase); mockConfig.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, secretPhrase); mockConfig.put(SaslConfigs.SASL_JAAS_CONFIG, "Anything anything " + secretPhrase + " something"); final Map<String, Object> scrubbed = scrubber.filterSensitiveOptions(mockConfig, cluster); assertNotSame("Should be different instances", scrubbed, mockConfig); assertEquals(scrubbed.get("Key1"), mockConfig.get("Key1")); assertEquals(scrubbed.get("Key2"), mockConfig.get("Key2")); assertNotEquals(scrubbed.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG), mockConfig.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG)); assertEquals("**HIDDEN**", scrubbed.get(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG)); assertNotEquals(scrubbed.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG), mockConfig.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG)); assertEquals("**HIDDEN**", scrubbed.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG)); assertNotEquals(scrubbed.get(SaslConfigs.SASL_JAAS_CONFIG), mockConfig.get(SaslConfigs.SASL_JAAS_CONFIG)); assertFalse( ((String)scrubbed.get(SaslConfigs.SASL_JAAS_CONFIG)).contains(secretPhrase) ); }