2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.PrintStream;
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31 import java.text.DateFormat;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.Date;
35 import java.util.List;
36 import java.util.Map.Entry;
37 import java.util.Properties;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.config.SecurityInfo;
41 import org.onap.aaf.cadi.util.Split;
43 public class PropAccess implements Access {
44 // Sonar says cannot be static... it's ok. not too many PropAccesses created.
45 private final SimpleDateFormat iso8601 = newISO8601();
47 public static SimpleDateFormat newISO8601() {
48 return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
51 public static final Level DEFAULT = Level.AUDIT;
55 private Properties props;
56 private List<String> recursionProtection = null;
61 logIt = new StreamLogIt(System.out);
66 * This Constructor soly exists to instantiate Servlet Context Based Logging that will call "init" later.
69 protected PropAccess(Object o) {
70 logIt = new StreamLogIt(System.out);
71 props = new Properties();
74 public PropAccess(String ... args) {
75 this(System.out,args);
78 public PropAccess(PrintStream ps, String[] args) {
79 logIt = new StreamLogIt(ps==null?System.out:ps);
83 public PropAccess(LogIt logit, String[] args) {
87 public PropAccess(Properties p) {
91 public PropAccess(PrintStream ps, Properties p) {
92 logIt = new StreamLogIt(ps==null?System.out:ps);
96 protected void init(final LogIt logIt, final String[] args) {
98 Properties nprops=new Properties();
100 for (String arg : args) {
101 if ((eq=arg.indexOf('='))>0) {
102 nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
108 protected synchronized void init(Properties p) {
109 // Make sure these two are set before any changes in Logging
112 props = new Properties();
113 // First, load related System Properties
114 for (Entry<Object,Object> es : System.getProperties().entrySet()) {
115 String key = es.getKey().toString();
116 for (String start : new String[] {"cadi_","aaf_","cm_"}) {
117 if (key.startsWith(start)) {
118 props.put(key, es.getValue());
122 // Second, overlay or fill in with Passed in Props
128 String sLevel = props.getProperty(Config.CADI_LOGLEVEL);
129 // Third, load any Chained Property Files
130 load(props.getProperty(Config.CADI_PROP_FILES));
132 if(sLevel==null) { // if LogLev wasn't set before, check again after Chained Load
133 sLevel = props.getProperty(Config.CADI_LOGLEVEL);
135 level=DEFAULT.maskOf();
137 level=Level.valueOf(sLevel).maskOf();
140 // Setup local Symmetrical key encryption
143 symm = Symm.obtain(this);
144 } catch (CadiException e) {
145 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
146 e.printStackTrace(System.err);
151 name = props.getProperty(Config.CADI_LOGNAME, name);
153 SecurityInfo.setHTTPProtocols(this);
158 private void load(String cadi_prop_files) {
159 if (cadi_prop_files==null) {
162 String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
165 for(String filename : Split.splitTrim(File.pathSeparatorChar, cadi_prop_files)) {
166 Properties fileProps = new Properties();
167 File file = new File(filename);
169 printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
171 FileInputStream fis = new FileInputStream(file);
174 // Only load props from recursion which are not already in props
175 // meaning top Property file takes precedence
176 for(Entry<Object, Object> es : fileProps.entrySet()) {
177 if(props.get(es.getKey())==null) {
178 String key = es.getKey().toString();
179 String value = es.getValue().toString();
180 props.put(key, value);
181 if(key.contains("pass")) {
184 printf(Level.DEBUG," %s=%s",key,value);
188 String chainProp = fileProps.getProperty(Config.CADI_PROP_FILES);
189 if (chainProp!=null) {
190 if (recursionProtection==null) {
191 recursionProtection = new ArrayList<>();
192 recursionProtection.add(cadi_prop_files);
194 if (!recursionProtection.contains(chainProp)) {
195 recursionProtection.add(chainProp);
196 load(chainProp); // recurse
202 } catch (Exception e) {
203 log(e,filename,"cannot be opened");
206 printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());
211 for (Entry<Object, Object> es : props.entrySet()) {
212 Object value = es.getValue();
213 if (value instanceof String) {
214 String trim = ((String)value).trim();
215 // Remove Beginning/End Quotes, which might be there if mixed with Bash Props
216 int s = 0, e=trim.length()-1;
217 if (s<e && trim.charAt(s)=='"' && trim.charAt(e)=='"') {
218 trim=trim.substring(s+1,e);
220 if (trim!=value) { // Yes, I want OBJECT equals
221 props.setProperty((String)es.getKey(), trim);
225 // Reset Symm if Keyfile Changes:
226 String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
227 if ((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
229 symm = Symm.obtain(this);
230 } catch (CadiException e) {
231 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
232 e.printStackTrace(System.err);
236 prevKeyFile=newKeyFile;
239 String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
240 if (loglevel!=null) {
242 level=Level.valueOf(loglevel).maskOf();
243 } catch (IllegalArgumentException e) {
244 printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);
250 public void load(InputStream is) throws IOException {
252 load(props.getProperty(Config.CADI_PROP_FILES));
256 public void log(Level level, Object ... elements) {
257 if (willLog(level)) {
258 logIt.push(level,elements);
262 public StringBuilder buildMsg(Level level, Object[] elements) {
263 return buildMsg(name,iso8601,level,elements);
267 * Need to pass in DateFormat per thread, because not marked as thread safe
269 public static StringBuilder buildMsg(final String name, final DateFormat sdf, Level level, Object[] elements) {
270 final StringBuilder sb;
271 int end = elements.length;
273 sb = new StringBuilder();
274 write(true,sb,elements);
276 sb = new StringBuilder(
277 sdf.format(new Date())
280 sb.append(level.name());
287 if(elements[idx]!=null &&
288 elements[idx] instanceof Integer) {
290 sb.append(elements[idx]);
294 write(true,sb,elements);
300 private static boolean write(boolean first, StringBuilder sb, Object[] elements) {
302 for (Object o : elements) {
304 if(o.getClass().isArray()) {
305 first = write(first,sb,(Object[])o);
313 switch(s.charAt(l-1)) {
329 public void log(Exception e, Object... elements) {
330 StringWriter sw = new StringWriter();
331 PrintWriter pw = new PrintWriter(sw);
333 e.printStackTrace(pw);
334 log(Level.ERROR,elements,sw.toString());
338 public void printf(Level level, String fmt, Object... elements) {
339 if (willLog(level)) {
340 log(level,String.format(fmt, elements));
345 public void setLogLevel(Level level) {
346 this.level = level.maskOf();
350 public boolean willLog(Level level) {
351 return level.inMask(this.level);
355 public ClassLoader classLoader() {
356 return ClassLoader.getSystemClassLoader();
360 public String getProperty(String tag, String def) {
361 return props.getProperty(tag,def);
365 public String decrypt(String encrypted, boolean anytext) throws IOException {
366 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
367 ? symm.depass(encrypted)
371 public String encrypt(String unencrypted) throws IOException {
372 return Symm.ENC+symm.enpass(unencrypted);
378 public String getProperty(String tag) {
379 return props.getProperty(tag);
383 public Properties getProperties() {
387 public void setProperty(String tag, String value) {
389 props.put(tag, value);
390 if (Config.CADI_KEYFILE.equals(tag)) {
391 // reset decryption too
393 symm = Symm.obtain(this);
394 } catch (CadiException e) {
395 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
396 e.printStackTrace(System.err);
403 public interface LogIt {
404 public void push(Level level, Object ... elements) ;
407 private class StreamLogIt implements LogIt {
408 private PrintStream ps;
410 public StreamLogIt(PrintStream ps) {
414 public void push(Level level, Object ... elements) {
415 ps.println(buildMsg(level,elements));
420 public void set(LogIt logit) {
424 public void setStreamLogIt(PrintStream ps) {
425 logIt = new StreamLogIt(ps);
428 public String toString() {
429 return props.toString();