2 package org.onap.ccsdk.sli.core.sli;
5 import java.io.FileOutputStream;
6 import java.io.PrintStream;
7 import java.util.HashMap;
8 import java.util.Map.Entry;
9 import java.util.Properties;
10 import java.util.TreeMap;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 public class SecurePrinter {
16 private static final Logger LOG = LoggerFactory.getLogger(SecurePrinter.class);
17 private static final String DEFAULT_FILTER = "password,pass,pswd";
18 private static final String REDACTED = "***REDACTED***";
19 private static final String FILTER_PROPERTY = "NODE_STRING_FILTER";
20 private static final String SEPERATOR = " = ";
21 private static final String COMMON_ERROR_MESSAGE = "Failed to print properties";
23 private static String[] filterArray;
25 public SecurePrinter() {
26 String filterProperty = System.getProperty(FILTER_PROPERTY);
27 if (filterProperty != null && !filterProperty.isEmpty() && filterProperty.contains(",")) {
28 filterArray = filterProperty.split(",");
30 filterArray = DEFAULT_FILTER.split(",");
34 private String filterValue(String key, String value) {
35 String normalizedKey = key.toLowerCase();
36 for (String restrictedKey : filterArray) {
37 if (normalizedKey.contains(restrictedKey)) {
44 public void printAttributes(HashMap<String, String> attributes) {
45 if (LOG.isDebugEnabled()) {
46 for (Entry<String, String> attribute : attributes.entrySet()) {
47 String value = filterValue(attribute.getKey(), attribute.getValue());
48 LOG.debug(attribute.getKey() + SEPERATOR + value);
53 public void printAttributes(HashMap<String, String> attributes, String subpath) {
54 if (LOG.isDebugEnabled()) {
55 for (Entry<String, String> attribute : attributes.entrySet()) {
56 if (attribute.getKey().startsWith(subpath)) {
57 String value = filterValue(attribute.getKey(), attribute.getValue());
58 LOG.debug(attribute.getKey() + SEPERATOR + value);
64 public void printProperties(Properties props) {
65 if (LOG.isDebugEnabled()) {
67 for (Entry<Object, Object> property : props.entrySet()) {
68 String keyString = (String) property.getKey();
69 String valueString = (String) property.getValue();
70 String value = filterValue(keyString, valueString);
71 LOG.debug(keyString + SEPERATOR + value);
73 } catch (Exception e) {
74 LOG.error(COMMON_ERROR_MESSAGE, e);
79 public void printProperties(Properties props, String subpath) {
80 if (LOG.isDebugEnabled()) {
82 for (Entry<Object, Object> property : props.entrySet()) {
83 String keyString = (String) property.getKey();
84 if (keyString.startsWith(subpath)) {
85 String valueString = (String) property.getValue();
86 String value = filterValue(keyString, valueString);
87 LOG.debug(keyString + SEPERATOR + value);
90 } catch (Exception e) {
91 LOG.error(COMMON_ERROR_MESSAGE, e);
96 public void printPropertiesAlphabetically(Properties props) {
97 if (LOG.isDebugEnabled()) {
98 TreeMap<String, String> sortedMap = new TreeMap(props);
100 for (Entry<String, String> entry : sortedMap.entrySet()) {
101 String value = filterValue(entry.getKey(), entry.getValue());
102 LOG.debug(entry.getKey() + SEPERATOR + value);
104 } catch (Exception e) {
105 LOG.error(COMMON_ERROR_MESSAGE, e);
110 public void printAttributesToFile(HashMap<String, String> attributes, String fileName) {
111 try (FileOutputStream fstr = new FileOutputStream(new File(fileName));
112 PrintStream pstr = new PrintStream(fstr, true);) {
113 pstr.println("#######################################");
114 for (Entry<String, String> entry : attributes.entrySet()) {
115 String value = filterValue(entry.getKey(), entry.getValue());
116 pstr.println(entry.getKey() + SEPERATOR + value);
118 } catch (Exception e) {
119 LOG.error("Cannot write context to file.", e);