Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConfigLogger |
|
| 2.6666666666666665;2.667 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * | |
5 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.opensource.org/licenses/ecl2.php | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | package org.kuali.rice.core.config; | |
18 | ||
19 | import java.util.*; | |
20 | ||
21 | /** | |
22 | * Logs information about the configuration at the DEBUG level. | |
23 | * | |
24 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
25 | */ | |
26 | 0 | public class ConfigLogger { |
27 | ||
28 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ConfigLogger.class); |
29 | ||
30 | /** | |
31 | * List of keys to suppress the values for and the associated values to print instead. | |
32 | */ | |
33 | 0 | private static final String[][] SECRET_KEYS = |
34 | { | |
35 | {"password", "Top Secret Password"}, | |
36 | {"encryption.key", "Top Secret Encryption Key"} | |
37 | }; | |
38 | ||
39 | /** | |
40 | * These are a list of keys that we know we can display regardless of whether they | |
41 | * are considered "secret" | |
42 | */ | |
43 | 0 | private static final String[] PUBLIC_KEYS = |
44 | { | |
45 | "cas.validate.password" | |
46 | ||
47 | }; | |
48 | ||
49 | ||
50 | public static void logConfig(Config config) { | |
51 | 0 | Map<String, String> safeConfig = getDisplaySafeConfig(config.getProperties()); |
52 | 0 | String props = ""; |
53 | 0 | for (Iterator iter = safeConfig.entrySet().iterator(); iter.hasNext();) { |
54 | 0 | Map.Entry property = (Map.Entry) iter.next(); |
55 | 0 | String key = (String) property.getKey(); |
56 | 0 | String value = (String) property.getValue(); |
57 | 0 | props += key + "=[" + value + "],"; |
58 | 0 | } |
59 | 0 | LOG.debug("Properties used " + props); |
60 | 0 | } |
61 | ||
62 | /** | |
63 | * Returns a value for a parameter that is safe for displaying on screen or in a log file. | |
64 | * @param name the name of the parameter | |
65 | * @param value the parameter value | |
66 | * @return the parameter value if the parameter is non-secret, or a replacement if the parameter is secret | |
67 | */ | |
68 | public static String getDisplaySafeValue(String name, String value) { | |
69 | 0 | String safeValue = value; |
70 | ||
71 | 0 | List<String> l = Arrays.asList(PUBLIC_KEYS); |
72 | ||
73 | 0 | for (String[] secretKey : SECRET_KEYS) { |
74 | 0 | if (name.contains(secretKey[0]) && !l.contains(name)) { |
75 | 0 | safeValue = secretKey[1]; |
76 | 0 | break; |
77 | } | |
78 | } | |
79 | 0 | return safeValue; |
80 | } | |
81 | ||
82 | /** | |
83 | * Returns a Map of configuration paramters that have display-safe values. This allows for the suppression | |
84 | * of sensitive configuration paramters from being displayed (i.e. passwords). | |
85 | */ | |
86 | public static Map<String, String> getDisplaySafeConfig(Map properties) { | |
87 | 0 | Map<String, String> parameters = new HashMap<String, String>(); |
88 | 0 | for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) { |
89 | 0 | Map.Entry property = (Map.Entry) iter.next(); |
90 | 0 | String key = (String) property.getKey(); |
91 | 0 | String value = (String) property.getValue(); |
92 | 0 | String safeValue = getDisplaySafeValue(key, value); |
93 | 0 | parameters.put(key, safeValue); |
94 | 0 | } |
95 | 0 | return parameters; |
96 | } | |
97 | } |