+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.PrintStream;\r
-import java.text.SimpleDateFormat;\r
-import java.util.ArrayList;\r
-import java.util.Date;\r
-import java.util.List;\r
-import java.util.Map.Entry;\r
-\r
-import org.onap.aaf.cadi.config.Config;\r
-import org.onap.aaf.cadi.config.SecurityInfo;\r
-\r
-import java.util.Properties;\r
-\r
-public class PropAccess implements Access {\r
- private static final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");\r
-\r
- public static Level DEFAULT = Level.AUDIT;\r
- \r
- private Symm symm;\r
- private int level;\r
- private Properties props;\r
- private List<String> recursionProtection = null;\r
- private PrintStream out;\r
- \r
- private String name;\r
-\r
- public PropAccess() {\r
- out=System.out;\r
- init(null);\r
- }\r
- \r
- /**\r
- * This Constructor soley exists to instantiate Servlet Context Based Logging that will call "init" later.\r
- * @param sc\r
- */\r
- protected PropAccess(Object o) {\r
- out=System.out;\r
- props = new Properties();\r
- }\r
- \r
- public PropAccess(String ... args) {\r
- this(System.out,args);\r
- }\r
- \r
- public PropAccess(PrintStream ps, String[] args) {\r
- out=ps==null?System.out:ps;\r
- Properties nprops=new Properties();\r
- int eq;\r
- for(String arg : args) {\r
- if((eq=arg.indexOf('='))>0) {\r
- nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));\r
- }\r
- }\r
- init(nprops);\r
- }\r
-\r
- public PropAccess(Properties p) {\r
- this(System.out,p);\r
- }\r
- \r
- public PropAccess(PrintStream ps, Properties p) {\r
- out=ps==null?System.out:ps;\r
- init(p);\r
- }\r
- \r
- protected void init(Properties p) {\r
- // Make sure these two are set before any changes in Logging\r
- name = "cadi";\r
- level=DEFAULT.maskOf();\r
- \r
- props = new Properties();\r
- // First, load related System Properties\r
- for(Entry<Object,Object> es : System.getProperties().entrySet()) {\r
- String key = es.getKey().toString();\r
- for(String start : new String[] {"cadi_","aaf_","cm_","csp_"}) {\r
- if(key.startsWith(start)) {\r
- props.put(key, es.getValue());\r
- }\r
- } \r
- }\r
- // Second, overlay or fill in with Passed in Props\r
- if(p!=null) {\r
- props.putAll(p);\r
- }\r
- \r
- // Third, load any Chained Property Files\r
- load(props.getProperty(Config.CADI_PROP_FILES));\r
- \r
- String sLevel = props.getProperty(Config.CADI_LOGLEVEL); \r
- if(sLevel!=null) {\r
- level=Level.valueOf(sLevel).maskOf(); \r
- }\r
- // Setup local Symmetrical key encryption\r
- if(symm==null) {\r
- symm = Symm.obtain(this);\r
- }\r
- \r
- name = props.getProperty(Config.CADI_LOGNAME, name);\r
- \r
- // Critical - if no Security Protocols set, then set it. We'll just get messed up if not\r
- if(props.get(Config.CADI_PROTOCOLS)==null) {\r
- props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);\r
- }\r
- }\r
-\r
- private void load(String cadi_prop_files) {\r
- String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);\r
-\r
- if(cadi_prop_files!=null) {\r
- int prev = 0, end = cadi_prop_files.length();\r
- int idx;\r
- String filename;\r
- while(prev<end) {\r
- idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);\r
- if(idx<0) {\r
- idx = end;\r
- }\r
- File file = new File(filename=cadi_prop_files.substring(prev,idx));\r
- if(file.exists()) {\r
- printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());\r
- try {\r
- FileInputStream fis = new FileInputStream(file);\r
- try {\r
- props.load(fis);\r
- // Recursively Load\r
- String chainProp = props.getProperty(Config.CADI_PROP_FILES);\r
- if(chainProp!=null) {\r
- if(recursionProtection==null) {\r
- recursionProtection = new ArrayList<String>();\r
- recursionProtection.add(cadi_prop_files);\r
- }\r
- if(!recursionProtection.contains(chainProp)) {\r
- recursionProtection.add(chainProp);\r
- load(chainProp); // recurse\r
- }\r
- }\r
- } finally {\r
- fis.close();\r
- }\r
- } catch (Exception e) {\r
- log(e,filename,"cannot be opened");\r
- }\r
- } else {\r
- printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());\r
- }\r
- prev = idx+1;\r
- }\r
- }\r
- // Reset Symm if Keyfile Changes:\r
- String newKeyFile = props.getProperty(Config.CADI_KEYFILE);\r
- if((prevKeyFile==null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {\r
- symm = Symm.obtain(this);\r
- prevKeyFile=newKeyFile;\r
- }\r
- \r
- String loglevel = props.getProperty(Config.CADI_LOGLEVEL);\r
- if(loglevel!=null) {\r
- try {\r
- level=Level.valueOf(loglevel).maskOf();\r
- } catch (IllegalArgumentException e) {\r
- printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);\r
- }\r
- }\r
- }\r
- \r
- @Override\r
- public void load(InputStream is) throws IOException {\r
- props.load(is);\r
- load(props.getProperty(Config.CADI_PROP_FILES));\r
- }\r
-\r
- @Override\r
- public void log(Level level, Object ... elements) {\r
- if(willLog(level)) {\r
- StringBuilder sb = buildMsg(level, elements);\r
- out.println(sb);\r
- out.flush();\r
- }\r
- }\r
- \r
- protected StringBuilder buildMsg(Level level, Object[] elements) {\r
- StringBuilder sb = new StringBuilder(iso8601.format(new Date()));\r
- sb.append(' ');\r
- sb.append(level.name());\r
- sb.append(" [");\r
- sb.append(name);\r
- \r
- int end = elements.length;\r
- if(end<=0) {\r
- sb.append("] ");\r
- } else {\r
- int idx = 0;\r
- if(elements[idx] instanceof Integer) {\r
- sb.append('-');\r
- sb.append(elements[idx]);\r
- ++idx;\r
- }\r
- sb.append("] ");\r
- String s;\r
- boolean first = true;\r
- for(Object o : elements) {\r
- if(o!=null) {\r
- s=o.toString();\r
- if(first) {\r
- first = false;\r
- } else {\r
- int l = s.length();\r
- if(l>0) {\r
- switch(s.charAt(l-1)) {\r
- case ' ':\r
- break;\r
- default:\r
- sb.append(' ');\r
- }\r
- }\r
- }\r
- sb.append(s);\r
- }\r
- }\r
- }\r
- return sb;\r
- }\r
-\r
- @Override\r
- public void log(Exception e, Object... elements) {\r
- log(Level.ERROR,e.getMessage(),elements);\r
- e.printStackTrace(System.err);\r
- }\r
-\r
- @Override\r
- public void printf(Level level, String fmt, Object... elements) {\r
- if(willLog(level)) {\r
- log(level,String.format(fmt, elements));\r
- }\r
- }\r
-\r
- @Override\r
- public void setLogLevel(Level level) {\r
- this.level = level.maskOf();\r
- }\r
-\r
- @Override\r
- public boolean willLog(Level level) {\r
- return level.inMask(this.level);\r
- }\r
-\r
- @Override\r
- public ClassLoader classLoader() {\r
- return ClassLoader.getSystemClassLoader();\r
- }\r
-\r
- @Override\r
- public String getProperty(String tag, String def) {\r
- return props.getProperty(tag,def);\r
- }\r
-\r
- @Override\r
- public String decrypt(String encrypted, boolean anytext) throws IOException {\r
- return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))\r
- ? symm.depass(encrypted)\r
- : encrypted;\r
- }\r
- \r
- public String encrypt(String unencrypted) throws IOException {\r
- return Symm.ENC+symm.enpass(unencrypted);\r
- }\r
-\r
- //////////////////\r
- // Additional\r
- //////////////////\r
- public String getProperty(String tag) {\r
- return props.getProperty(tag);\r
- }\r
- \r
-\r
- public Properties getProperties() {\r
- return props;\r
- }\r
-\r
- public void setProperty(String tag, String value) {\r
- if(value!=null) {\r
- props.put(tag, value);\r
- if(Config.CADI_KEYFILE.equals(tag)) {\r
- // reset decryption too\r
- symm = Symm.obtain(this);\r
- }\r
- }\r
- }\r
-\r
- public Properties getDME2Properties() {\r
- return Config.getDME2Props(this);\r
- }\r
-\r
-}\r