2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.env;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Properties;
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;
44 * AuthzEnv is the Env tailored to Authz Service
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
52 public class AuthzEnv extends RosettaEnv implements Access {
53 private long[] times = new long[20];
55 private PropAccess access;
59 _init(new PropAccess());
62 public AuthzEnv(String ... args) {
64 _init(new PropAccess(args));
67 public AuthzEnv(Properties props) {
69 _init(new PropAccess(props));
73 public AuthzEnv(PropAccess pa) {
78 private final void _init(PropAccess pa) {
84 private class Log4JLogit implements LogIt {
87 public void push(Level level, Object... elements) {
116 public AuthzTransImpl newTrans() {
118 times[idx]=System.currentTimeMillis();
119 if(++idx>=times.length)idx=0;
121 return new AuthzTransImpl(this);
125 * Create a Trans, but do not include in Weighted Average
128 public AuthzTrans newTransNoAvg() {
129 return new AuthzTransImpl(this);
132 public long transRate() {
136 for(int i=idx;i<times.length;++i) {
140 pot += times[i]-prev;
145 for(int i=0;i<idx;++i) {
149 pot += times[i]-prev;
155 return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
159 public ClassLoader classLoader() {
160 return getClass().getClassLoader();
164 public void load(InputStream is) throws IOException {
169 public void log(Level lvl, Object... msgs) {
170 access.log(lvl, msgs);
174 public void log(Exception e, Object... msgs) {
179 public void printf(Level level, String fmt, Object... elements) {
180 access.printf(level, fmt, elements);
184 * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
187 public boolean willLog(Level level) {
188 return access.willLog(level);
192 public void setLogLevel(Level level) {
193 access.setLogLevel(level);
196 public void setLog4JNames(String path, String root, String _service, String _audit, String _init, String _trace) throws APIException {
197 LogFileNamer lfn = new LogFileNamer(root);
199 throw new APIException("AuthzEnv.setLog4JNames \"_service\" required (as default). Others can be null");
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
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);
216 access.set(new Log4JLogit());
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");
224 if(anytext || encrypted.startsWith("enc:")) {
225 if(decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
228 s = Symm.obtain(this);
229 } catch (CadiException e1) {
230 throw new IOException(e1);
232 decryptor = new Decryptor() {
233 private Symm symm = s;
235 public String decrypt(String encrypted) {
237 return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
238 ? symm.depass(encrypted)
240 } catch (IOException e) {
245 encryptor = new Encryptor() {
247 public String encrypt(String data) {
248 ByteArrayOutputStream baos = new ByteArrayOutputStream();
251 return "enc:"+s.enpass(data);
252 } catch (IOException e) {
259 return decryptor.decrypt(encrypted);
266 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String)
269 public String getProperty(String key) {
270 return access.getProperty(key);
274 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperties(java.lang.String[])
277 public Properties getProperties(String... filter) {
278 return access.getProperties();
282 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String, java.lang.String)
285 public String getProperty(String key, String defaultValue) {
286 return access.getProperty(key, defaultValue);
290 * @see org.onap.aaf.misc.env.impl.BasicEnv#setProperty(java.lang.String, java.lang.String)
293 public String setProperty(String key, String value) {
294 access.setProperty(key, value);
298 public PropAccess access() {
303 * @see org.onap.aaf.cadi.Access#getProperties()
306 public Properties getProperties() {
307 return access.getProperties();