Keep only clean TestCases, remove 2 license issues
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / env / AuthzEnv.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.env;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Properties;
28
29 import org.onap.aaf.cadi.Access;
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.PropAccess;
32 import org.onap.aaf.cadi.PropAccess.LogIt;
33 import org.onap.aaf.cadi.Symm;
34 import org.onap.aaf.cadi.config.Config;
35 import org.onap.aaf.misc.env.APIException;
36 import org.onap.aaf.misc.env.Decryptor;
37 import org.onap.aaf.misc.env.Encryptor;
38 import org.onap.aaf.misc.env.impl.Log4JLogTarget;
39 import org.onap.aaf.misc.env.log4j.LogFileNamer;
40 import org.onap.aaf.misc.rosetta.env.RosettaEnv;
41
42
43 /**
44  * AuthzEnv is the Env tailored to Authz Service
45  * 
46  * Most of it is derived from RosettaEnv, but it also implements Access, which
47  * is an Interface that Allows CADI to interact with Container Logging
48  * 
49  * @author Jonathan
50  *
51  */
52 public class AuthzEnv extends RosettaEnv implements Access {
53         private long[] times = new long[20];
54         private int idx = 0;
55         private PropAccess access;
56
57         public AuthzEnv() {
58                 super();
59                 _init(new PropAccess());
60         }
61
62         public AuthzEnv(String ... args) {
63                 super();
64                 _init(new PropAccess(args));
65         }
66
67         public AuthzEnv(Properties props) {
68                 super();
69                 _init(new PropAccess(props));
70         }
71         
72
73         public AuthzEnv(PropAccess pa) {
74                 super();
75                 _init(pa);
76         }
77         
78         private final void _init(PropAccess pa) { 
79                 access = pa;
80                 times = new long[20];
81                 idx = 0;
82         }
83         
84         private class Log4JLogit implements LogIt {
85                 
86                 @Override
87                 public void push(Level level, Object... elements) {
88                         switch(level) {
89                                 case AUDIT:
90                                         audit.log(elements);
91                                         break;
92                                 case DEBUG:
93                                         debug.log(elements);
94                                         break;
95                                 case ERROR:
96                                         error.log(elements);
97                                         break;
98                                 case INFO:
99                                         info.log(elements);
100                                         break;
101                                 case INIT:
102                                         init.log(elements);
103                                         break;
104                                 case NONE:
105                                         break;
106                                 case WARN:
107                                         warn.log(elements);
108                                         break;
109                         }
110                         
111                 }
112                 
113         }
114
115         @Override
116         public AuthzTransImpl newTrans() {
117                 synchronized(this) {
118                         times[idx]=System.currentTimeMillis();
119                         if(++idx>=times.length)idx=0;
120                 }
121                 return new AuthzTransImpl(this);
122         }
123
124         /**
125          *  Create a Trans, but do not include in Weighted Average
126          * @return
127          */
128         public AuthzTrans newTransNoAvg() {
129                 return new AuthzTransImpl(this);
130         }
131
132         public long transRate() {
133                 int count = 0;
134                 long pot = 0;
135                 long prev = 0;
136                 for(int i=idx;i<times.length;++i) {
137                         if(times[i]>0) {
138                                 if(prev>0) {
139                                         ++count;
140                 pot += times[i]-prev;
141                                 }
142                                 prev = times[i]; 
143                         }
144                 }
145                 for(int i=0;i<idx;++i) {
146                         if(times[i]>0) {
147                                 if(prev>0) {
148                                         ++count;
149                                         pot += times[i]-prev;
150                                 }
151                                 prev = times[i]; 
152                         }
153                 }
154
155                 return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
156         }
157         
158         @Override
159         public ClassLoader classLoader() {
160                 return getClass().getClassLoader();
161         }
162
163         @Override
164         public void load(InputStream is) throws IOException {
165                 access.load(is);
166         }
167
168         @Override
169         public void log(Level lvl, Object... msgs) {
170                 access.log(lvl, msgs);
171         }
172
173         @Override
174         public void log(Exception e, Object... msgs) {
175                 access.log(e,msgs);
176         }
177
178         @Override
179         public void printf(Level level, String fmt, Object... elements) {
180                 access.printf(level, fmt, elements);
181         }
182
183         /* (non-Javadoc)
184          * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
185          */
186         @Override
187         public boolean willLog(Level level) {
188                 return access.willLog(level);
189         }
190
191         @Override
192         public void setLogLevel(Level level) {
193                 access.setLogLevel(level);
194         }
195
196         public void setLog4JNames(String path, String root, String _service, String _audit, String _init, String _trace) throws APIException {
197                 LogFileNamer lfn = new LogFileNamer(root);
198                 if(_service==null) {
199                         throw new APIException("AuthzEnv.setLog4JNames \"_service\" required (as default).  Others can be null");
200                 }
201                 String service=_service=lfn.setAppender(_service); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
202                 String audit=_audit==null?service:lfn.setAppender(_audit);     // is part of the log-file name
203                 String init=_init==null?service:lfn.setAppender(_init);
204                 String trace=_trace==null?service:lfn.setAppender(_trace);
205                 //TODO Validate path on Classpath
206                 lfn.configure(path);
207                 super.fatal = new Log4JLogTarget(service,org.apache.log4j.Level.FATAL);
208                 super.error = new Log4JLogTarget(service,org.apache.log4j.Level.ERROR);
209                 super.warn = new Log4JLogTarget(service,org.apache.log4j.Level.WARN);
210                 super.audit = new Log4JLogTarget(audit,org.apache.log4j.Level.WARN);
211                 super.init = new Log4JLogTarget(init,org.apache.log4j.Level.WARN);
212                 super.info = new Log4JLogTarget(service,org.apache.log4j.Level.INFO);
213                 super.debug = new Log4JLogTarget(service,org.apache.log4j.Level.DEBUG);
214                 super.trace = new Log4JLogTarget(trace,org.apache.log4j.Level.TRACE);
215                 
216                 access.set(new Log4JLogit());
217         }
218         
219         private static final byte[] ENC="enc:".getBytes();
220         public String decrypt(String encrypted, final boolean anytext) throws IOException {
221                 if(encrypted==null) {
222                         throw new IOException("Password to be decrypted is null");
223                 }
224                 if(anytext || encrypted.startsWith("enc:")) {
225                         if(decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
226                                 final Symm s;
227                                 try {
228                                         s = Symm.obtain(this);
229                                 } catch (CadiException e1) {
230                                         throw new IOException(e1);
231                                 }
232                                 decryptor = new Decryptor() {
233                                         private Symm symm = s;
234                                         @Override
235                                         public String decrypt(String encrypted) {
236                                                 try {
237                                                         return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
238                                                                         ? symm.depass(encrypted)
239                                                                         : encrypted;
240                                                 } catch (IOException e) {
241                                                         return "";
242                                                 }
243                                         }
244                                 };
245                                 encryptor = new Encryptor() {
246                                         @Override
247                                         public String encrypt(String data) {
248                                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
249                                                 try {
250                                                         baos.write(ENC);
251                                                         return "enc:"+s.enpass(data);
252                                                 } catch (IOException e) {
253                                                         return "";
254                                                 }
255                                         }
256         
257                                 };
258                         }
259                         return decryptor.decrypt(encrypted);
260                 } else {
261                         return encrypted;
262                 }
263         }
264
265         /* (non-Javadoc)
266          * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String)
267          */
268         @Override
269         public String getProperty(String key) {
270                 return access.getProperty(key);
271         }
272
273         /* (non-Javadoc)
274          * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperties(java.lang.String[])
275          */
276         @Override
277         public Properties getProperties(String... filter) {
278                 return access.getProperties();
279         }
280
281         /* (non-Javadoc)
282          * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String, java.lang.String)
283          */
284         @Override
285         public String getProperty(String key, String defaultValue) {
286                 return access.getProperty(key, defaultValue);
287         }
288
289         /* (non-Javadoc)
290          * @see org.onap.aaf.misc.env.impl.BasicEnv#setProperty(java.lang.String, java.lang.String)
291          */
292         @Override
293         public String setProperty(String key, String value) {
294                 access.setProperty(key, value);
295                 return value;
296         }
297
298         public PropAccess access() {
299                 return access;
300         }
301
302         /* (non-Javadoc)
303          * @see org.onap.aaf.cadi.Access#getProperties()
304          */
305         @Override
306         public Properties getProperties() {
307                 return access.getProperties();
308         };
309         
310 }