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;
35 import org.onap.aaf.cadi.config.Config;
36 import org.onap.aaf.cadi.config.SecurityInfo;
38 import java.util.Properties;
40 public class PropAccess implements Access {
41 private static final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
43 public static 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);
72 Properties nprops=new Properties();
74 for(String arg : args) {
75 if((eq=arg.indexOf('='))>0) {
76 nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
82 public PropAccess(LogIt logit, String[] args) {
86 public PropAccess(Properties p) {
90 public PropAccess(PrintStream ps, Properties p) {
91 logIt = new StreamLogIt(ps==null?System.out:ps);
95 protected void init(Properties p) {
96 // Make sure these two are set before any changes in Logging
98 level=DEFAULT.maskOf();
100 props = new Properties();
101 // First, load related System Properties
102 for(Entry<Object,Object> es : System.getProperties().entrySet()) {
103 String key = es.getKey().toString();
104 for(String start : new String[] {"cadi_","aaf_","cm_"}) {
105 if(key.startsWith(start)) {
106 props.put(key, es.getValue());
110 // Second, overlay or fill in with Passed in Props
115 // Third, load any Chained Property Files
116 load(props.getProperty(Config.CADI_PROP_FILES));
118 String sLevel = props.getProperty(Config.CADI_LOGLEVEL);
120 level=Level.valueOf(sLevel).maskOf();
122 // Setup local Symmetrical key encryption
125 symm = Symm.obtain(this);
126 } catch (CadiException e) {
127 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
128 e.printStackTrace(System.err);
133 name = props.getProperty(Config.CADI_LOGNAME, name);
135 specialConversions();
138 private void specialConversions() {
139 // Critical - if no Security Protocols set, then set it. We'll just get messed up if not
140 if(props.get(Config.CADI_PROTOCOLS)==null) {
141 props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
145 temp=props.get(Config.CADI_PROTOCOLS);
146 if(props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) {
147 props.put(Config.HTTPS_PROTOCOLS, temp);
151 if("1.7".equals(System.getProperty("java.specification.version"))
152 && (temp==null || (temp instanceof String && ((String)temp).contains("TLSv1.2")))) {
153 System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
158 private void load(String cadi_prop_files) {
159 if(cadi_prop_files==null) {
162 String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
163 int prev = 0, end = cadi_prop_files.length();
167 idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);
171 File file = new File(filename=cadi_prop_files.substring(prev,idx));
173 printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
175 FileInputStream fis = new FileInputStream(file);
179 String chainProp = props.getProperty(Config.CADI_PROP_FILES);
180 if(chainProp!=null) {
181 if(recursionProtection==null) {
182 recursionProtection = new ArrayList<String>();
183 recursionProtection.add(cadi_prop_files);
185 if(!recursionProtection.contains(chainProp)) {
186 recursionProtection.add(chainProp);
187 load(chainProp); // recurse
193 } catch (Exception e) {
194 log(e,filename,"cannot be opened");
197 printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());
203 for(Entry<Object, Object> es : props.entrySet()) {
204 Object value = es.getValue();
205 if(value instanceof String) {
206 String trim = ((String)value).trim();
207 if(trim!=value) { // Yes, I want OBJECT equals
208 props.setProperty((String)es.getKey(), trim);
212 // Reset Symm if Keyfile Changes:
213 String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
214 if((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
216 symm = Symm.obtain(this);
217 } catch (CadiException e) {
218 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
219 e.printStackTrace(System.err);
223 prevKeyFile=newKeyFile;
226 String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
229 level=Level.valueOf(loglevel).maskOf();
230 } catch (IllegalArgumentException e) {
231 printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);
235 specialConversions();
239 public void load(InputStream is) throws IOException {
241 load(props.getProperty(Config.CADI_PROP_FILES));
245 public void log(Level level, Object ... elements) {
247 logIt.push(level,elements);
251 protected StringBuilder buildMsg(Level level, Object[] elements) {
252 StringBuilder sb = new StringBuilder(iso8601.format(new Date()));
254 sb.append(level.name());
258 int end = elements.length;
263 if(elements[idx] instanceof Integer) {
265 sb.append(elements[idx]);
270 boolean first = true;
271 for(Object o : elements) {
279 switch(s.charAt(l-1)) {
295 public void log(Exception e, Object... elements) {
296 log(Level.ERROR,e.getMessage(),elements);
297 e.printStackTrace(System.err);
301 public void printf(Level level, String fmt, Object... elements) {
303 log(level,String.format(fmt, elements));
308 public void setLogLevel(Level level) {
309 this.level = level.maskOf();
313 public boolean willLog(Level level) {
314 return level.inMask(this.level);
318 public ClassLoader classLoader() {
319 return ClassLoader.getSystemClassLoader();
323 public String getProperty(String tag, String def) {
324 return props.getProperty(tag,def);
328 public String decrypt(String encrypted, boolean anytext) throws IOException {
329 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
330 ? symm.depass(encrypted)
334 public String encrypt(String unencrypted) throws IOException {
335 return Symm.ENC+symm.enpass(unencrypted);
341 public String getProperty(String tag) {
342 return props.getProperty(tag);
346 public Properties getProperties() {
350 public void setProperty(String tag, String value) {
352 props.put(tag, value);
353 if(Config.CADI_KEYFILE.equals(tag)) {
354 // reset decryption too
356 symm = Symm.obtain(this);
357 } catch (CadiException e) {
358 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
359 e.printStackTrace(System.err);
366 public interface LogIt {
367 public void push(Level level, Object ... elements) ;
370 private class StreamLogIt implements LogIt {
371 private PrintStream ps;
373 public StreamLogIt(PrintStream ps) {
377 public void push(Level level, Object ... elements) {
378 ps.println(buildMsg(level,elements));
384 public void set(LogIt logit) {