X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fcore%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2FPropAccess.java;h=df2c0764aee6284792317e02b966ece15183e8c5;hb=refs%2Fchanges%2F65%2F99865%2F1;hp=a35777f8fd68a83f0634423599f6e0cdd8fb5a9e;hpb=4b5a7d721d994a49057e9bfb403c7bff1b376660;p=aaf%2Fauthz.git diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/PropAccess.java b/cadi/core/src/main/java/org/onap/aaf/cadi/PropAccess.java index a35777f8..df2c0764 100644 --- a/cadi/core/src/main/java/org/onap/aaf/cadi/PropAccess.java +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/PropAccess.java @@ -3,13 +3,15 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright (C) 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,11 +23,15 @@ package org.onap.aaf.cadi; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -35,14 +41,13 @@ import java.util.Properties; import org.onap.aaf.cadi.config.Config; import org.onap.aaf.cadi.config.SecurityInfo; +import org.onap.aaf.cadi.util.Split; public class PropAccess implements Access { // Sonar says cannot be static... it's ok. not too many PropAccesses created. - private final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); - - public static final Level DEFAULT = Level.AUDIT; - + private final SimpleDateFormat iso8601 = newISO8601(); private Symm symm; + public static final Level DEFAULT = Level.AUDIT; private int level; private Properties props; private List recursionProtection = null; @@ -53,7 +58,7 @@ public class PropAccess implements Access { logIt = new StreamLogIt(System.out); init(null); } - + /** * This Constructor soly exists to instantiate Servlet Context Based Logging that will call "init" later. * @param sc @@ -62,70 +67,79 @@ public class PropAccess implements Access { logIt = new StreamLogIt(System.out); props = new Properties(); } - + public PropAccess(String ... args) { this(System.out,args); } - + public PropAccess(PrintStream ps, String[] args) { logIt = new StreamLogIt(ps==null?System.out:ps); init(logIt,args); } - + public PropAccess(LogIt logit, String[] args) { init(logit, args); } - + public PropAccess(Properties p) { this(System.out,p); } - + public PropAccess(PrintStream ps, Properties p) { logIt = new StreamLogIt(ps==null?System.out:ps); init(p); } - + protected void init(final LogIt logIt, final String[] args) { this.logIt = logIt; Properties nprops=new Properties(); int eq; - for(String arg : args) { - if((eq=arg.indexOf('='))>0) { + for (String arg : args) { + if ((eq=arg.indexOf('='))>0) { nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1)); } } init(nprops); } - - protected void init(Properties p) { + + public static SimpleDateFormat newISO8601() { + return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + } + + protected synchronized void init(Properties p) { // Make sure these two are set before any changes in Logging name = "cadi"; - level=DEFAULT.maskOf(); - + props = new Properties(); // First, load related System Properties - for(Entry es : System.getProperties().entrySet()) { + for (Entry es : System.getProperties().entrySet()) { String key = es.getKey().toString(); - for(String start : new String[] {"cadi_","aaf_","cm_"}) { - if(key.startsWith(start)) { + for (String start : new String[] {"cadi_","aaf_","cm_"}) { + if (key.startsWith(start)) { props.put(key, es.getValue()); } - } + } } // Second, overlay or fill in with Passed in Props - if(p!=null) { + if (p!=null) { props.putAll(p); } - + + // Preset LogLevel + String sLevel = props.getProperty(Config.CADI_LOGLEVEL); // Third, load any Chained Property Files load(props.getProperty(Config.CADI_PROP_FILES)); - - String sLevel = props.getProperty(Config.CADI_LOGLEVEL); - if(sLevel!=null) { - level=Level.valueOf(sLevel).maskOf(); + + if(sLevel==null) { // if LogLev wasn't set before, check again after Chained Load + sLevel = props.getProperty(Config.CADI_LOGLEVEL); + if (sLevel==null) { + level=DEFAULT.maskOf(); + } else { + level=Level.valueOf(sLevel).maskOf(); + } } // Setup local Symmetrical key encryption - if(symm==null) { + if (symm==null) { try { symm = Symm.obtain(this); } catch (CadiException e) { @@ -134,60 +148,51 @@ public class PropAccess implements Access { System.exit(1); } } - + name = props.getProperty(Config.CADI_LOGNAME, name); - - specialConversions(); - } - private void specialConversions() { - // Critical - if no Security Protocols set, then set it. We'll just get messed up if not - if(props.get(Config.CADI_PROTOCOLS)==null) { - props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT); - } - - Object temp; - temp=props.get(Config.CADI_PROTOCOLS); - if(props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) { - props.put(Config.HTTPS_PROTOCOLS, temp); - } - - if(temp!=null) { - if("1.7".equals(System.getProperty("java.specification.version")) - && (temp==null || (temp instanceof String && ((String)temp).contains("TLSv1.2")))) { - System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT); - } - } + SecurityInfo.setHTTPProtocols(this); + } + private void load(String cadi_prop_files) { - if(cadi_prop_files==null) { + if (cadi_prop_files==null) { return; } String prevKeyFile = props.getProperty(Config.CADI_KEYFILE); - int prev = 0, end = cadi_prop_files.length(); - int idx; - String filename; - while(prev es : fileProps.entrySet()) { + if(props.get(es.getKey())==null) { + String key = es.getKey().toString(); + String value = es.getValue().toString(); + props.put(key, value); + if(key.contains("pass")) { + value = "vi XX"; + } + printf(Level.DEBUG," %s=%s",key,value); + } + } // Recursively Load - String chainProp = props.getProperty(Config.CADI_PROP_FILES); - if(chainProp!=null) { - if(recursionProtection==null) { + String chainProp = fileProps.getProperty(Config.CADI_PROP_FILES); + if (chainProp!=null) { + if (recursionProtection==null) { recursionProtection = new ArrayList<>(); recursionProtection.add(cadi_prop_files); } - if(!recursionProtection.contains(chainProp)) { + if (!recursionProtection.contains(chainProp)) { recursionProtection.add(chainProp); load(chainProp); // recurse } @@ -201,27 +206,26 @@ public class PropAccess implements Access { } else { printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath()); } - prev = idx+1; } - - // Trim - for(Entry es : props.entrySet()) { + + // Trim + for (Entry es : props.entrySet()) { Object value = es.getValue(); - if(value instanceof String) { + if (value instanceof String) { String trim = ((String)value).trim(); // Remove Beginning/End Quotes, which might be there if mixed with Bash Props int s = 0, e=trim.length()-1; - if(s0) { + if (l>0) { switch(s.charAt(l-1)) { case ' ': break; @@ -302,18 +328,21 @@ public class PropAccess implements Access { } } } - return sb; + return first; } @Override public void log(Exception e, Object... elements) { - log(Level.ERROR,e.getMessage(),elements); - e.printStackTrace(System.err); + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + pw.println(); + e.printStackTrace(pw); + log(Level.ERROR,elements,sw.toString()); } @Override public void printf(Level level, String fmt, Object... elements) { - if(willLog(level)) { + if (willLog(level)) { log(level,String.format(fmt, elements)); } } @@ -344,7 +373,7 @@ public class PropAccess implements Access { ? symm.depass(encrypted) : encrypted; } - + public String encrypt(String unencrypted) throws IOException { return Symm.ENC+symm.enpass(unencrypted); } @@ -355,16 +384,16 @@ public class PropAccess implements Access { public String getProperty(String tag) { return props.getProperty(tag); } - + public Properties getProperties() { return props; } public void setProperty(String tag, String value) { - if(value!=null) { + if (value!=null) { props.put(tag, value); - if(Config.CADI_KEYFILE.equals(tag)) { + if (Config.CADI_KEYFILE.equals(tag)) { // reset decryption too try { symm = Symm.obtain(this); @@ -380,10 +409,10 @@ public class PropAccess implements Access { public interface LogIt { public void push(Level level, Object ... elements) ; } - + private class StreamLogIt implements LogIt { private PrintStream ps; - + public StreamLogIt(PrintStream ps) { this.ps = ps; } @@ -392,10 +421,17 @@ public class PropAccess implements Access { ps.println(buildMsg(level,elements)); ps.flush(); } - } public void set(LogIt logit) { logIt = logit; } + + public void setStreamLogIt(PrintStream ps) { + logIt = new StreamLogIt(ps); + } + + public String toString() { + return props.toString(); + } }