aboutsummaryrefslogtreecommitdiffstats
path: root/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SecurePrinter.java
blob: d12729c42f9e9cfe7d34f8b7c0074e67842e43dd (plain)
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
package org.onap.ccsdk.sli.core.sli;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;

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 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() + " = " + 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() + " = " + 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 + " = " + value);
                }
            } catch (Exception e) {
                LOG.error("Failed to print properties", 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 + " = " + value);
                    }
                }
            } catch (Exception e) {
                LOG.error("Failed to print properties", e);
            }
        }
    }
    
}