Remove Tabs, per Jococo
[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             Object[] nm = new Object[msgs.length+1];
106             System.arraycopy(msgs, 0, nm, 1, msgs.length);
107             nm[0]=e;
108             access.log(Level.ERROR, nm);
109         }
110
111         @Override
112         public boolean isLoggable() {
113             return access.willLog(level);
114         }
115
116         @Override
117         public void printf(String fmt, Object... vars) {
118             access.printf(level, fmt, vars);
119         }
120         
121     }
122     @Override
123     public AuthzTransImpl newTrans() {
124         synchronized(this) {
125             times[idx]=System.currentTimeMillis();
126             if (++idx>=times.length)idx=0;
127         }
128         return new AuthzTransImpl(this);
129     }
130
131     /**
132      *  Create a Trans, but do not include in Weighted Average
133      * @return
134      */
135     public AuthzTrans newTransNoAvg() {
136         return new AuthzTransImpl(this);
137     }
138
139     public long transRate() {
140         int count = 0;
141         long pot = 0;
142         long prev = 0;
143         for (int i=idx;i<times.length;++i) {
144             if (times[i]>0) {
145                 if (prev>0) {
146                     ++count;
147         pot += times[i]-prev;
148                 }
149                 prev = times[i]; 
150             }
151         }
152         for (int i=0;i<idx;++i) {
153             if (times[i]>0) {
154                 if (prev>0) {
155                     ++count;
156                     pot += times[i]-prev;
157                 }
158                 prev = times[i]; 
159             }
160         }
161
162         return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
163     }
164     
165     @Override
166     public ClassLoader classLoader() {
167         return getClass().getClassLoader();
168     }
169
170     @Override
171     public void load(InputStream is) throws IOException {
172         access.load(is);
173     }
174
175     @Override
176     public void log(Level lvl, Object... msgs) {
177         access.log(lvl, msgs);
178     }
179
180     @Override
181     public void log(Exception e, Object... msgs) {
182         access.log(e,msgs);
183     }
184
185     @Override
186     public void printf(Level level, String fmt, Object... elements) {
187         access.printf(level, fmt, elements);
188     }
189
190     /* (non-Javadoc)
191      * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
192      */
193     @Override
194     public boolean willLog(Level level) {
195         return access.willLog(level);
196     }
197
198     @Override
199     public void setLogLevel(Level level) {
200         access.setLogLevel(level);
201     }
202     
203     private static final byte[] ENC="enc:".getBytes();
204     public String decrypt(String encrypted, final boolean anytext) throws IOException {
205         if (encrypted==null) {
206             throw new IOException("Password to be decrypted is null");
207         }
208         if (anytext || encrypted.startsWith("enc:")) {
209             if (decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
210                 final Symm s;
211                 try {
212                     s = Symm.obtain(this);
213                 } catch (CadiException e1) {
214                     throw new IOException(e1);
215                 }
216                 decryptor = new Decryptor() {
217                     private Symm symm = s;
218                     @Override
219                     public String decrypt(String encrypted) {
220                         try {
221                             return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
222                                     ? symm.depass(encrypted)
223                                     : encrypted;
224                         } catch (IOException e) {
225                             return "";
226                         }
227                     }
228                 };
229                 encryptor = new Encryptor() {
230                     @Override
231                     public String encrypt(String data) {
232                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
233                         try {
234                             baos.write(ENC);
235                             return "enc:"+s.enpass(data);
236                         } catch (IOException e) {
237                             return "";
238                         }
239                     }
240     
241                 };
242             }
243             return decryptor.decrypt(encrypted);
244         } else {
245             return encrypted;
246         }
247     }
248
249     /* (non-Javadoc)
250      * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String)
251      */
252     @Override
253     public String getProperty(String key) {
254         return access.getProperty(key);
255     }
256
257     /* (non-Javadoc)
258      * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperties(java.lang.String[])
259      */
260     @Override
261     public Properties getProperties(String... filter) {
262         return access.getProperties();
263     }
264
265     /* (non-Javadoc)
266      * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String, java.lang.String)
267      */
268     @Override
269     public String getProperty(String key, String defaultValue) {
270         return access.getProperty(key, defaultValue);
271     }
272
273     /* (non-Javadoc)
274      * @see org.onap.aaf.misc.env.impl.BasicEnv#setProperty(java.lang.String, java.lang.String)
275      */
276     @Override
277     public String setProperty(String key, String value) {
278         access.setProperty(key, value);
279         return value;
280     }
281
282     public PropAccess access() {
283         return access;
284     }
285
286     /* (non-Javadoc)
287      * @see org.onap.aaf.cadi.Access#getProperties()
288      */
289     @Override
290     public Properties getProperties() {
291         return access.getProperties();
292     };
293     
294 }