1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package org.onap.ccsdk.sli.core.sli;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SecurePrinter {
private static final Logger LOG = LoggerFactory.getLogger(SecurePrinter.class);
private static final String DEFAULT_FILTER = "password,pass,pswd";
private static final String REDACTED = "***REDACTED***";
private static final String FILTER_PROPERTY = "NODE_STRING_FILTER";
private static final String SEPERATOR = " = ";
private static final String COMMON_ERROR_MESSAGE = "Failed to print properties";
private static String[] filterArray;
public SecurePrinter() {
String filterProperty = System.getProperty(FILTER_PROPERTY);
if (filterProperty != null && !filterProperty.isEmpty() && filterProperty.contains(",")) {
filterArray = filterProperty.split(",");
} else {
filterArray = DEFAULT_FILTER.split(",");
}
}
private String filterValue(String key, String value) {
String normalizedKey = key.toLowerCase();
for (String restrictedKey : filterArray) {
if (normalizedKey.contains(restrictedKey)) {
return REDACTED;
}
}
return value;
}
public void printAttributes(HashMap<String, String> attributes) {
if (LOG.isDebugEnabled()) {
for (Entry<String, String> attribute : attributes.entrySet()) {
String value = filterValue(attribute.getKey(), attribute.getValue());
LOG.debug(attribute.getKey() + SEPERATOR + value);
}
}
}
public void printAttributes(HashMap<String, String> attributes, String subpath) {
if (LOG.isDebugEnabled()) {
for (Entry<String, String> attribute : attributes.entrySet()) {
if (attribute.getKey().startsWith(subpath)) {
String value = filterValue(attribute.getKey(), attribute.getValue());
LOG.debug(attribute.getKey() + SEPERATOR + value);
}
}
}
}
public void printProperties(Properties props) {
if (LOG.isDebugEnabled()) {
try {
for (Entry<Object, Object> property : props.entrySet()) {
String keyString = (String) property.getKey();
String valueString = (String) property.getValue();
String value = filterValue(keyString, valueString);
LOG.debug(keyString + SEPERATOR + value);
}
} catch (Exception e) {
LOG.error(COMMON_ERROR_MESSAGE, e);
}
}
}
public void printProperties(Properties props, String subpath) {
if (LOG.isDebugEnabled()) {
try {
for (Entry<Object, Object> property : props.entrySet()) {
String keyString = (String) property.getKey();
if (keyString.startsWith(subpath)) {
String valueString = (String) property.getValue();
String value = filterValue(keyString, valueString);
LOG.debug(keyString + SEPERATOR + value);
}
}
} catch (Exception e) {
LOG.error(COMMON_ERROR_MESSAGE, e);
}
}
}
public void printPropertiesAlphabetically(Properties props) {
if (LOG.isDebugEnabled()) {
TreeMap<String, String> sortedMap = new TreeMap(props);
try {
for (Entry<String, String> entry : sortedMap.entrySet()) {
String value = filterValue(entry.getKey(), entry.getValue());
LOG.debug(entry.getKey() + SEPERATOR + value);
}
} catch (Exception e) {
LOG.error(COMMON_ERROR_MESSAGE, e);
}
}
}
public void printAttributesToFile(HashMap<String, String> attributes, String fileName) {
try (FileOutputStream fstr = new FileOutputStream(new File(fileName));
PrintStream pstr = new PrintStream(fstr, true);) {
pstr.println("#######################################");
for (Entry<String, String> entry : attributes.entrySet()) {
String value = filterValue(entry.getKey(), entry.getValue());
pstr.println(entry.getKey() + SEPERATOR + value);
}
} catch (Exception e) {
LOG.error("Cannot write context to file.", e);
}
}
}
|