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.text.SimpleDateFormat;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Properties;
36 import org.onap.aaf.cadi.config.Config;
37 import org.onap.aaf.cadi.config.SecurityInfo;
39 public class PropAccess implements Access {
40 // Sonar says cannot be static... it's ok. not too many PropAccesses created.
41 private final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
43 public static final Level DEFAULT = Level.AUDIT;
47 private Properties props;
48 private List<String> recursionProtection = null;
53 logIt = new StreamLogIt(System.out);
58 * This Constructor soly exists to instantiate Servlet Context Based Logging that will call "init" later.
61 protected PropAccess(Object o) {
62 logIt = new StreamLogIt(System.out);
63 props = new Properties();
66 public PropAccess(String ... args) {
67 this(System.out,args);
70 public PropAccess(PrintStream ps, String[] args) {
71 logIt = new StreamLogIt(ps==null?System.out:ps);
75 public PropAccess(LogIt logit, String[] args) {
79 public PropAccess(Properties p) {
83 public PropAccess(PrintStream ps, Properties p) {
84 logIt = new StreamLogIt(ps==null?System.out:ps);
88 protected void init(final LogIt logIt, final String[] args) {
90 Properties nprops=new Properties();
92 for(String arg : args) {
93 if((eq=arg.indexOf('='))>0) {
94 nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
100 protected void init(Properties p) {
101 // Make sure these two are set before any changes in Logging
103 level=DEFAULT.maskOf();
105 props = new Properties();
106 // First, load related System Properties
107 for(Entry<Object,Object> es : System.getProperties().entrySet()) {
108 String key = es.getKey().toString();
109 for(String start : new String[] {"cadi_","aaf_","cm_"}) {
110 if(key.startsWith(start)) {
111 props.put(key, es.getValue());
115 // Second, overlay or fill in with Passed in Props
120 // Third, load any Chained Property Files
121 load(props.getProperty(Config.CADI_PROP_FILES));
123 String sLevel = props.getProperty(Config.CADI_LOGLEVEL);
125 level=Level.valueOf(sLevel).maskOf();
127 // Setup local Symmetrical key encryption
130 symm = Symm.obtain(this);
131 } catch (CadiException e) {
132 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
133 e.printStackTrace(System.err);
138 name = props.getProperty(Config.CADI_LOGNAME, name);
140 specialConversions();
143 private void specialConversions() {
144 // Critical - if no Security Protocols set, then set it. We'll just get messed up if not
145 if(props.get(Config.CADI_PROTOCOLS)==null) {
146 props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
150 temp=props.get(Config.CADI_PROTOCOLS);
151 if(props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) {
152 props.put(Config.HTTPS_PROTOCOLS, temp);
156 if("1.7".equals(System.getProperty("java.specification.version"))
157 && (temp==null || (temp instanceof String && ((String)temp).contains("TLSv1.2")))) {
158 System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
163 private void load(String cadi_prop_files) {
164 if(cadi_prop_files==null) {
167 String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
168 int prev = 0, end = cadi_prop_files.length();
172 idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);
176 File file = new File(filename=cadi_prop_files.substring(prev,idx));
178 printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
180 FileInputStream fis = new FileInputStream(file);
184 String chainProp = props.getProperty(Config.CADI_PROP_FILES);
185 if(chainProp!=null) {
186 if(recursionProtection==null) {
187 recursionProtection = new ArrayList<>();
188 recursionProtection.add(cadi_prop_files);
190 if(!recursionProtection.contains(chainProp)) {
191 recursionProtection.add(chainProp);
192 load(chainProp); // recurse
198 } catch (Exception e) {
199 log(e,filename,"cannot be opened");
202 printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());
208 for(Entry<Object, Object> es : props.entrySet()) {
209 Object value = es.getValue();
210 if(value instanceof String) {
211 String trim = ((String)value).trim();
212 if(trim!=value) { // Yes, I want OBJECT equals
213 props.setProperty((String)es.getKey(), trim);
217 // Reset Symm if Keyfile Changes:
218 String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
219 if((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
221 symm = Symm.obtain(this);
222 } catch (CadiException e) {
223 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
224 e.printStackTrace(System.err);
228 prevKeyFile=newKeyFile;
231 String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
234 level=Level.valueOf(loglevel).maskOf();
235 } catch (IllegalArgumentException e) {
236 printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);
240 specialConversions();
244 public void load(InputStream is) throws IOException {
246 load(props.getProperty(Config.CADI_PROP_FILES));
250 public void log(Level level, Object ... elements) {
252 logIt.push(level,elements);
256 protected StringBuilder buildMsg(Level level, Object[] elements) {
257 return buildMsg(name,iso8601,level,elements);
260 public static StringBuilder buildMsg(final String name, final SimpleDateFormat sdf, Level level, Object[] elements) {
261 StringBuilder sb = new StringBuilder(sdf.format(new Date()));
263 sb.append(level.name());
267 int end = elements.length;
272 if(elements[idx] instanceof Integer) {
274 sb.append(elements[idx]);
279 boolean first = true;
280 for(Object o : elements) {
288 switch(s.charAt(l-1)) {
304 public void log(Exception e, Object... elements) {
305 log(Level.ERROR,e.getMessage(),elements);
306 e.printStackTrace(System.err);
310 public void printf(Level level, String fmt, Object... elements) {
312 log(level,String.format(fmt, elements));
317 public void setLogLevel(Level level) {
318 this.level = level.maskOf();
322 public boolean willLog(Level level) {
323 return level.inMask(this.level);
327 public ClassLoader classLoader() {
328 return ClassLoader.getSystemClassLoader();
332 public String getProperty(String tag, String def) {
333 return props.getProperty(tag,def);
337 public String decrypt(String encrypted, boolean anytext) throws IOException {
338 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
339 ? symm.depass(encrypted)
343 public String encrypt(String unencrypted) throws IOException {
344 return Symm.ENC+symm.enpass(unencrypted);
350 public String getProperty(String tag) {
351 return props.getProperty(tag);
355 public Properties getProperties() {
359 public void setProperty(String tag, String value) {
361 props.put(tag, value);
362 if(Config.CADI_KEYFILE.equals(tag)) {
363 // reset decryption too
365 symm = Symm.obtain(this);
366 } catch (CadiException e) {
367 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
368 e.printStackTrace(System.err);
375 public interface LogIt {
376 public void push(Level level, Object ... elements) ;
379 private class StreamLogIt implements LogIt {
380 private PrintStream ps;
382 public StreamLogIt(PrintStream ps) {
386 public void push(Level level, Object ... elements) {
387 ps.println(buildMsg(level,elements));
393 public void set(LogIt logit) {