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.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.List;
35 import java.util.Map.Entry;
36 import java.util.Properties;
38 import org.onap.aaf.cadi.config.Config;
39 import org.onap.aaf.cadi.config.SecurityInfo;
41 public class PropAccess implements Access {
42 // Sonar says cannot be static... it's ok. not too many PropAccesses created.
43 private final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
45 public static final Level DEFAULT = Level.AUDIT;
49 private Properties props;
50 private List<String> recursionProtection = null;
55 logIt = new StreamLogIt(System.out);
60 * This Constructor soly exists to instantiate Servlet Context Based Logging that will call "init" later.
63 protected PropAccess(Object o) {
64 logIt = new StreamLogIt(System.out);
65 props = new Properties();
68 public PropAccess(String ... args) {
69 this(System.out,args);
72 public PropAccess(PrintStream ps, String[] args) {
73 logIt = new StreamLogIt(ps==null?System.out:ps);
77 public PropAccess(LogIt logit, String[] args) {
81 public PropAccess(Properties p) {
85 public PropAccess(PrintStream ps, Properties p) {
86 logIt = new StreamLogIt(ps==null?System.out:ps);
90 protected void init(final LogIt logIt, final String[] args) {
92 Properties nprops=new Properties();
94 for (String arg : args) {
95 if ((eq=arg.indexOf('='))>0) {
96 nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
102 protected void init(Properties p) {
103 // Make sure these two are set before any changes in Logging
105 level=DEFAULT.maskOf();
107 props = new Properties();
108 // First, load related System Properties
109 for (Entry<Object,Object> es : System.getProperties().entrySet()) {
110 String key = es.getKey().toString();
111 for (String start : new String[] {"HOSTNAME","cadi_","aaf_","cm_"}) {
112 if (key.startsWith(start)) {
113 props.put(key, es.getValue());
117 // Second, overlay or fill in with Passed in Props
122 // Third, load any Chained Property Files
123 load(props.getProperty(Config.CADI_PROP_FILES));
125 String sLevel = props.getProperty(Config.CADI_LOGLEVEL);
127 level=Level.valueOf(sLevel).maskOf();
129 // Setup local Symmetrical key encryption
132 symm = Symm.obtain(this);
133 } catch (CadiException e) {
134 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
135 e.printStackTrace(System.err);
140 name = props.getProperty(Config.CADI_LOGNAME, name);
142 specialConversions();
145 private void specialConversions() {
146 // Critical - if no Security Protocols set, then set it. We'll just get messed up if not
147 if (props.get(Config.CADI_PROTOCOLS)==null) {
148 props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
152 temp=props.get(Config.CADI_PROTOCOLS);
153 if (props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) {
154 props.put(Config.HTTPS_PROTOCOLS, temp);
158 if ("1.7".equals(System.getProperty("java.specification.version"))
159 && (temp==null || (temp instanceof String && ((String)temp).contains("TLSv1.2")))) {
160 System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
165 private void load(String cadi_prop_files) {
166 if (cadi_prop_files==null) {
169 String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
170 int prev = 0, end = cadi_prop_files.length();
174 idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);
178 File file = new File(filename=cadi_prop_files.substring(prev,idx));
180 printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
182 FileInputStream fis = new FileInputStream(file);
186 String chainProp = props.getProperty(Config.CADI_PROP_FILES);
187 if (chainProp!=null) {
188 if (recursionProtection==null) {
189 recursionProtection = new ArrayList<>();
190 recursionProtection.add(cadi_prop_files);
192 if (!recursionProtection.contains(chainProp)) {
193 recursionProtection.add(chainProp);
194 load(chainProp); // recurse
200 } catch (Exception e) {
201 log(e,filename,"cannot be opened");
204 printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());
210 for (Entry<Object, Object> es : props.entrySet()) {
211 Object value = es.getValue();
212 if (value instanceof String) {
213 String trim = ((String)value).trim();
214 // Remove Beginning/End Quotes, which might be there if mixed with Bash Props
215 int s = 0, e=trim.length()-1;
216 if (s<e && trim.charAt(s)=='"' && trim.charAt(e)=='"') {
217 trim=trim.substring(s+1,e);
219 if (trim!=value) { // Yes, I want OBJECT equals
220 props.setProperty((String)es.getKey(), trim);
224 // Reset Symm if Keyfile Changes:
225 String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
226 if ((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
228 symm = Symm.obtain(this);
229 } catch (CadiException e) {
230 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
231 e.printStackTrace(System.err);
235 prevKeyFile=newKeyFile;
238 String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
239 if (loglevel!=null) {
241 level=Level.valueOf(loglevel).maskOf();
242 } catch (IllegalArgumentException e) {
243 printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);
247 specialConversions();
251 public void load(InputStream is) throws IOException {
253 load(props.getProperty(Config.CADI_PROP_FILES));
257 public void log(Level level, Object ... elements) {
258 if (willLog(level)) {
259 logIt.push(level,elements);
263 protected StringBuilder buildMsg(Level level, Object[] elements) {
264 return buildMsg(name,iso8601,level,elements);
267 public static StringBuilder buildMsg(final String name, final SimpleDateFormat sdf, Level level, Object[] elements) {
268 StringBuilder sb = new StringBuilder(sdf.format(new Date()));
270 sb.append(level.name());
274 int end = elements.length;
279 if(elements[idx]!=null &&
280 elements[idx] instanceof Integer) {
282 sb.append(elements[idx]);
286 write(true,sb,elements);
291 private static boolean write(boolean first, StringBuilder sb, Object[] elements) {
293 for (Object o : elements) {
295 if(o.getClass().isArray()) {
296 first = write(first,sb,(Object[])o);
304 switch(s.charAt(l-1)) {
320 public void log(Exception e, Object... elements) {
321 StringWriter sw = new StringWriter();
322 PrintWriter pw = new PrintWriter(sw);
324 e.printStackTrace(pw);
325 log(Level.ERROR,elements,sw.toString());
329 public void printf(Level level, String fmt, Object... elements) {
330 if (willLog(level)) {
331 log(level,String.format(fmt, elements));
336 public void setLogLevel(Level level) {
337 this.level = level.maskOf();
341 public boolean willLog(Level level) {
342 return level.inMask(this.level);
346 public ClassLoader classLoader() {
347 return ClassLoader.getSystemClassLoader();
351 public String getProperty(String tag, String def) {
352 return props.getProperty(tag,def);
356 public String decrypt(String encrypted, boolean anytext) throws IOException {
357 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
358 ? symm.depass(encrypted)
362 public String encrypt(String unencrypted) throws IOException {
363 return Symm.ENC+symm.enpass(unencrypted);
369 public String getProperty(String tag) {
370 return props.getProperty(tag);
374 public Properties getProperties() {
378 public void setProperty(String tag, String value) {
380 props.put(tag, value);
381 if (Config.CADI_KEYFILE.equals(tag)) {
382 // reset decryption too
384 symm = Symm.obtain(this);
385 } catch (CadiException e) {
386 System.err.append("FATAL ERROR: Cannot obtain Key Information.");
387 e.printStackTrace(System.err);
394 public interface LogIt {
395 public void push(Level level, Object ... elements) ;
398 private class StreamLogIt implements LogIt {
399 private PrintStream ps;
401 public StreamLogIt(PrintStream ps) {
405 public void push(Level level, Object ... elements) {
406 ps.println(buildMsg(level,elements));
411 public void set(LogIt logit) {
415 public void setStreamLogIt(PrintStream ps) {
416 logIt = new StreamLogIt(ps);
419 public String toString() {
420 return props.toString();