1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.authz.env;
\r
26 import java.io.ByteArrayOutputStream;
\r
27 import java.io.IOException;
\r
28 import java.io.InputStream;
\r
29 import java.util.Map.Entry;
\r
30 import java.util.Properties;
\r
32 import com.att.cadi.Access;
\r
33 import com.att.cadi.Symm;
\r
34 import com.att.cadi.config.Config;
\r
35 import com.att.inno.env.APIException;
\r
36 import com.att.inno.env.Decryptor;
\r
37 import com.att.inno.env.Encryptor;
\r
38 import com.att.inno.env.impl.Log4JLogTarget;
\r
39 import com.att.inno.env.log4j.LogFileNamer;
\r
40 import com.att.rosetta.env.RosettaEnv;
\r
44 * AuthzEnv is the Env tailored to Authz Service
\r
46 * Most of it is derived from RosettaEnv, but it also implements Access, which
\r
47 * is an Interface that Allows CADI to interact with Container Logging
\r
51 public class AuthzEnv extends RosettaEnv implements Access {
\r
52 private long[] times = new long[20];
\r
53 private int idx = 0;
\r
54 //private int mask = Level.AUDIT.maskOf();
\r
60 public AuthzEnv(String ... args) {
\r
64 public AuthzEnv(Properties props) {
\r
65 super(Config.CADI_PROP_FILES,props);
\r
70 public AuthzTransImpl newTrans() {
\r
71 synchronized(this) {
\r
72 times[idx]=System.currentTimeMillis();
\r
73 if(++idx>=times.length)idx=0;
\r
75 return new AuthzTransImpl(this);
\r
79 * Create a Trans, but do not include in Weighted Average
\r
82 public AuthzTrans newTransNoAvg() {
\r
83 return new AuthzTransImpl(this);
\r
86 public long transRate() {
\r
90 for(int i=idx;i<times.length;++i) {
\r
94 pot += times[i]-prev;
\r
99 for(int i=0;i<idx;++i) {
\r
103 pot += times[i]-prev;
\r
109 return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
\r
113 public ClassLoader classLoader() {
\r
114 return getClass().getClassLoader();
\r
118 public void load(InputStream is) throws IOException {
\r
119 Properties props = new Properties();
\r
121 for(Entry<Object, Object> es : props.entrySet()) {
\r
122 String key = es.getKey().toString();
\r
123 String value =es.getValue().toString();
\r
124 put(staticSlot(key==null?null:key.trim()),value==null?null:value.trim());
\r
129 public void log(Level lvl, Object... msgs) {
\r
130 // if(lvl.inMask(mask)) {
\r
133 // init().log(msgs);
\r
136 // audit().log(msgs);
\r
139 // debug().log(msgs);
\r
142 // error().log(msgs);
\r
145 // info().log(msgs);
\r
148 // warn().log(msgs);
\r
157 public void log(Exception e, Object... msgs) {
\r
158 error().log(e,msgs);
\r
162 public void printf(Level level, String fmt, Object... elements) {
\r
163 if(willLog(level)) {
\r
164 log(level,String.format(fmt, elements));
\r
169 * @see com.att.cadi.Access#willLog(com.att.cadi.Access.Level)
\r
172 public boolean willLog(Level level) {
\r
174 // if(level.inMask(mask)) {
\r
177 // return init().isLoggable();
\r
179 // return audit().isLoggable();
\r
181 // return debug().isLoggable();
\r
183 // return error().isLoggable();
\r
185 // return info().isLoggable();
\r
187 // return warn().isLoggable();
\r
196 public void setLogLevel(Level level) {
\r
197 super.debug().isLoggable();
\r
198 //level.toggle(mask);
\r
201 public void setLog4JNames(String path, String root, String _service, String _audit, String _init, String _trace) throws APIException {
\r
202 LogFileNamer lfn = new LogFileNamer(root);
\r
203 if(_service==null) {
\r
204 throw new APIException("AuthzEnv.setLog4JNames \"_service\" required (as default). Others can be null");
\r
206 String service=_service=lfn.setAppender(_service); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
\r
207 String audit=_audit==null?service:lfn.setAppender(_audit); // is part of the log-file name
\r
208 String init=_init==null?service:lfn.setAppender(_init);
\r
209 String trace=_trace==null?service:lfn.setAppender(_trace);
\r
210 //TODO Validate path on Classpath
\r
211 lfn.configure(path);
\r
212 super.fatal = new Log4JLogTarget(service,org.apache.log4j.Level.FATAL);
\r
213 super.error = new Log4JLogTarget(service,org.apache.log4j.Level.ERROR);
\r
214 super.warn = new Log4JLogTarget(service,org.apache.log4j.Level.WARN);
\r
215 super.audit = new Log4JLogTarget(audit,org.apache.log4j.Level.WARN);
\r
216 super.init = new Log4JLogTarget(init,org.apache.log4j.Level.WARN);
\r
217 super.info = new Log4JLogTarget(service,org.apache.log4j.Level.INFO);
\r
218 super.debug = new Log4JLogTarget(service,org.apache.log4j.Level.DEBUG);
\r
219 super.trace = new Log4JLogTarget(trace,org.apache.log4j.Level.TRACE);
\r
222 private static final byte[] ENC="enc:???".getBytes();
\r
223 public String decrypt(String encrypted, final boolean anytext) throws IOException {
\r
224 if(encrypted==null) {
\r
225 throw new IOException("Password to be decrypted is null");
\r
227 if(anytext || encrypted.startsWith("enc:")) {
\r
228 if(decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
\r
229 final Symm s = Symm.obtain(this);
\r
230 decryptor = new Decryptor() {
\r
231 private Symm symm = s;
\r
233 public String decrypt(String encrypted) {
\r
235 return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
\r
236 ? symm.depass(encrypted)
\r
238 } catch (IOException e) {
\r
243 encryptor = new Encryptor() {
\r
245 public String encrypt(String data) {
\r
246 ByteArrayOutputStream baos = new ByteArrayOutputStream();
\r
249 return "enc:???"+s.enpass(data);
\r
250 } catch (IOException e) {
\r
257 return decryptor.decrypt(encrypted);
\r