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.authz.env;
\r
25 import java.io.ByteArrayOutputStream;
\r
26 import java.io.IOException;
\r
27 import java.io.InputStream;
\r
28 import java.util.Map.Entry;
\r
29 import java.util.Properties;
\r
31 import org.onap.aaf.cadi.Access;
\r
32 import org.onap.aaf.cadi.Symm;
\r
33 import org.onap.aaf.cadi.config.Config;
\r
34 import org.onap.aaf.inno.env.APIException;
\r
35 import org.onap.aaf.inno.env.Decryptor;
\r
36 import org.onap.aaf.inno.env.Encryptor;
\r
37 import org.onap.aaf.inno.env.impl.Log4JLogTarget;
\r
38 import org.onap.aaf.inno.env.log4j.LogFileNamer;
\r
39 import org.onap.aaf.rosetta.env.RosettaEnv;
\r
43 * AuthzEnv is the Env tailored to Authz Service
\r
45 * Most of it is derived from RosettaEnv, but it also implements Access, which
\r
46 * is an Interface that Allows CADI to interact with Container Logging
\r
50 public class AuthzEnv extends RosettaEnv implements Access {
\r
51 private long[] times = new long[20];
\r
52 private int idx = 0;
\r
53 //private int mask = Level.AUDIT.maskOf();
\r
59 public AuthzEnv(String ... args) {
\r
63 public AuthzEnv(Properties props) {
\r
64 super(Config.CADI_PROP_FILES,props);
\r
69 public AuthzTransImpl newTrans() {
\r
70 synchronized(this) {
\r
71 times[idx]=System.currentTimeMillis();
\r
72 if(++idx>=times.length)idx=0;
\r
74 return new AuthzTransImpl(this);
\r
78 * Create a Trans, but do not include in Weighted Average
\r
81 public AuthzTrans newTransNoAvg() {
\r
82 return new AuthzTransImpl(this);
\r
85 public long transRate() {
\r
89 for(int i=idx;i<times.length;++i) {
\r
93 pot += times[i]-prev;
\r
98 for(int i=0;i<idx;++i) {
\r
102 pot += times[i]-prev;
\r
108 return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
\r
112 public ClassLoader classLoader() {
\r
113 return getClass().getClassLoader();
\r
117 public void load(InputStream is) throws IOException {
\r
118 Properties props = new Properties();
\r
120 for(Entry<Object, Object> es : props.entrySet()) {
\r
121 String key = es.getKey().toString();
\r
122 String value =es.getValue().toString();
\r
123 put(staticSlot(key==null?null:key.trim()),value==null?null:value.trim());
\r
128 public void log(Level lvl, Object... msgs) {
\r
129 // if(lvl.inMask(mask)) {
\r
132 // init().log(msgs);
\r
135 // audit().log(msgs);
\r
138 // debug().log(msgs);
\r
141 // error().log(msgs);
\r
144 // info().log(msgs);
\r
147 // warn().log(msgs);
\r
156 public void log(Exception e, Object... msgs) {
\r
157 error().log(e,msgs);
\r
161 public void printf(Level level, String fmt, Object... elements) {
\r
162 if(willLog(level)) {
\r
163 log(level,String.format(fmt, elements));
\r
168 * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
\r
171 public boolean willLog(Level level) {
\r
173 // if(level.inMask(mask)) {
\r
176 // return init().isLoggable();
\r
178 // return audit().isLoggable();
\r
180 // return debug().isLoggable();
\r
182 // return error().isLoggable();
\r
184 // return info().isLoggable();
\r
186 // return warn().isLoggable();
\r
195 public void setLogLevel(Level level) {
\r
196 super.debug().isLoggable();
\r
197 //level.toggle(mask);
\r
200 public void setLog4JNames(String path, String root, String _service, String _audit, String _init, String _trace) throws APIException {
\r
201 LogFileNamer lfn = new LogFileNamer(root);
\r
202 if(_service==null) {
\r
203 throw new APIException("AuthzEnv.setLog4JNames \"_service\" required (as default). Others can be null");
\r
205 String service=_service=lfn.setAppender(_service); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
\r
206 String audit=_audit==null?service:lfn.setAppender(_audit); // is part of the log-file name
\r
207 String init=_init==null?service:lfn.setAppender(_init);
\r
208 String trace=_trace==null?service:lfn.setAppender(_trace);
\r
209 //TODO Validate path on Classpath
\r
210 lfn.configure(path);
\r
211 super.fatal = new Log4JLogTarget(service,org.apache.log4j.Level.FATAL);
\r
212 super.error = new Log4JLogTarget(service,org.apache.log4j.Level.ERROR);
\r
213 super.warn = new Log4JLogTarget(service,org.apache.log4j.Level.WARN);
\r
214 super.audit = new Log4JLogTarget(audit,org.apache.log4j.Level.WARN);
\r
215 super.init = new Log4JLogTarget(init,org.apache.log4j.Level.WARN);
\r
216 super.info = new Log4JLogTarget(service,org.apache.log4j.Level.INFO);
\r
217 super.debug = new Log4JLogTarget(service,org.apache.log4j.Level.DEBUG);
\r
218 super.trace = new Log4JLogTarget(trace,org.apache.log4j.Level.TRACE);
\r
221 private static final byte[] ENC="enc:???".getBytes();
\r
222 public String decrypt(String encrypted, final boolean anytext) throws IOException {
\r
223 if(encrypted==null) {
\r
224 throw new IOException("Password to be decrypted is null");
\r
226 if(anytext || encrypted.startsWith("enc:")) {
\r
227 if(decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
\r
228 final Symm s = Symm.obtain(this);
\r
229 decryptor = new Decryptor() {
\r
230 private Symm symm = s;
\r
232 public String decrypt(String encrypted) {
\r
234 return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
\r
235 ? symm.depass(encrypted)
\r
237 } catch (IOException e) {
\r
242 encryptor = new Encryptor() {
\r
244 public String encrypt(String data) {
\r
245 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
248 return "enc:???"+s.enpass(data);
\r
249 } catch (IOException e) {
\r
256 return decryptor.decrypt(encrypted);
\r