1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi;
\r
25 import java.io.File;
\r
26 import java.io.FileInputStream;
\r
27 import java.io.IOException;
\r
28 import java.io.InputStream;
\r
29 import java.io.PrintStream;
\r
30 import java.text.SimpleDateFormat;
\r
31 import java.util.ArrayList;
\r
32 import java.util.Date;
\r
33 import java.util.List;
\r
34 import java.util.Map.Entry;
\r
36 import org.onap.aaf.cadi.config.Config;
\r
37 import org.onap.aaf.cadi.config.SecurityInfo;
\r
39 import java.util.Properties;
\r
41 public class PropAccess implements Access {
\r
42 private static final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
\r
44 public static Level DEFAULT = Level.AUDIT;
\r
48 private Properties props;
\r
49 private List<String> recursionProtection = null;
\r
50 private PrintStream out;
\r
52 private String name;
\r
54 public PropAccess() {
\r
60 * This Constructor soley exists to instantiate Servlet Context Based Logging that will call "init" later.
\r
63 protected PropAccess(Object o) {
\r
65 props = new Properties();
\r
68 public PropAccess(String ... args) {
\r
69 this(System.out,args);
\r
72 public PropAccess(PrintStream ps, String[] args) {
\r
73 out=ps==null?System.out:ps;
\r
74 Properties nprops=new Properties();
\r
76 for(String arg : args) {
\r
77 if((eq=arg.indexOf('='))>0) {
\r
78 nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
\r
84 public PropAccess(Properties p) {
\r
88 public PropAccess(PrintStream ps, Properties p) {
\r
89 out=ps==null?System.out:ps;
\r
93 protected void init(Properties p) {
\r
94 // Make sure these two are set before any changes in Logging
\r
96 level=DEFAULT.maskOf();
\r
98 props = new Properties();
\r
99 // First, load related System Properties
\r
100 for(Entry<Object,Object> es : System.getProperties().entrySet()) {
\r
101 String key = es.getKey().toString();
\r
102 for(String start : new String[] {"cadi_","aaf_","cm_","csp_"}) {
\r
103 if(key.startsWith(start)) {
\r
104 props.put(key, es.getValue());
\r
108 // Second, overlay or fill in with Passed in Props
\r
113 // Third, load any Chained Property Files
\r
114 load(props.getProperty(Config.CADI_PROP_FILES));
\r
116 String sLevel = props.getProperty(Config.CADI_LOGLEVEL);
\r
118 level=Level.valueOf(sLevel).maskOf();
\r
120 // Setup local Symmetrical key encryption
\r
122 symm = Symm.obtain(this);
\r
125 name = props.getProperty(Config.CADI_LOGNAME, name);
\r
127 // Critical - if no Security Protocols set, then set it. We'll just get messed up if not
\r
128 if(props.get(Config.CADI_PROTOCOLS)==null) {
\r
129 props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
\r
133 private void load(String cadi_prop_files) {
\r
134 String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
\r
136 if(cadi_prop_files!=null) {
\r
137 int prev = 0, end = cadi_prop_files.length();
\r
141 idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);
\r
145 File file = new File(filename=cadi_prop_files.substring(prev,idx));
\r
146 if(file.exists()) {
\r
147 printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
\r
149 FileInputStream fis = new FileInputStream(file);
\r
152 // Recursively Load
\r
153 String chainProp = props.getProperty(Config.CADI_PROP_FILES);
\r
154 if(chainProp!=null) {
\r
155 if(recursionProtection==null) {
\r
156 recursionProtection = new ArrayList<String>();
\r
157 recursionProtection.add(cadi_prop_files);
\r
159 if(!recursionProtection.contains(chainProp)) {
\r
160 recursionProtection.add(chainProp);
\r
161 load(chainProp); // recurse
\r
167 } catch (Exception e) {
\r
168 log(e,filename,"cannot be opened");
\r
171 printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());
\r
176 // Reset Symm if Keyfile Changes:
\r
177 String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
\r
178 if((prevKeyFile==null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
\r
179 symm = Symm.obtain(this);
\r
180 prevKeyFile=newKeyFile;
\r
183 String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
\r
184 if(loglevel!=null) {
\r
186 level=Level.valueOf(loglevel).maskOf();
\r
187 } catch (IllegalArgumentException e) {
\r
188 printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);
\r
194 public void load(InputStream is) throws IOException {
\r
196 load(props.getProperty(Config.CADI_PROP_FILES));
\r
200 public void log(Level level, Object ... elements) {
\r
201 if(willLog(level)) {
\r
202 StringBuilder sb = buildMsg(level, elements);
\r
208 protected StringBuilder buildMsg(Level level, Object[] elements) {
\r
209 StringBuilder sb = new StringBuilder(iso8601.format(new Date()));
\r
211 sb.append(level.name());
\r
215 int end = elements.length;
\r
220 if(elements[idx] instanceof Integer) {
\r
222 sb.append(elements[idx]);
\r
227 boolean first = true;
\r
228 for(Object o : elements) {
\r
234 int l = s.length();
\r
236 switch(s.charAt(l-1)) {
\r
252 public void log(Exception e, Object... elements) {
\r
253 log(Level.ERROR,e.getMessage(),elements);
\r
254 e.printStackTrace(System.err);
\r
258 public void printf(Level level, String fmt, Object... elements) {
\r
259 if(willLog(level)) {
\r
260 log(level,String.format(fmt, elements));
\r
265 public void setLogLevel(Level level) {
\r
266 this.level = level.maskOf();
\r
270 public boolean willLog(Level level) {
\r
271 return level.inMask(this.level);
\r
275 public ClassLoader classLoader() {
\r
276 return ClassLoader.getSystemClassLoader();
\r
280 public String getProperty(String tag, String def) {
\r
281 return props.getProperty(tag,def);
\r
285 public String decrypt(String encrypted, boolean anytext) throws IOException {
\r
286 return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
\r
287 ? symm.depass(encrypted)
\r
291 public String encrypt(String unencrypted) throws IOException {
\r
292 return Symm.ENC+symm.enpass(unencrypted);
\r
298 public String getProperty(String tag) {
\r
299 return props.getProperty(tag);
\r
303 public Properties getProperties() {
\r
307 public void setProperty(String tag, String value) {
\r
309 props.put(tag, value);
\r
310 if(Config.CADI_KEYFILE.equals(tag)) {
\r
311 // reset decryption too
\r
312 symm = Symm.obtain(this);
\r
317 public Properties getDME2Properties() {
\r
318 return Config.getDME2Props(this);
\r