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