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.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;
41 * AuthzEnv is the Env tailored to Authz Service
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
49 public class AuthzEnv extends RosettaEnv implements Access {
50 private long[] times = new long[20];
52 private PropAccess access;
56 _init(new PropAccess());
59 public AuthzEnv(String ... args) {
61 _init(new PropAccess(args));
64 public AuthzEnv(Properties props) {
66 _init(new PropAccess(props));
70 public AuthzEnv(PropAccess pa) {
75 private final void _init(PropAccess pa) {
79 fatal = new AccessLogTarget(access, Level.ERROR);
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);
89 private class AccessLogTarget implements LogTarget {
90 private final Level level;
91 private final Access access;
93 public AccessLogTarget(final Access access, final Level level) {
99 public void log(Object... msgs) {
100 access.log(level, msgs);
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);
108 access.log(Level.ERROR, nm);
112 public boolean isLoggable() {
113 return access.willLog(level);
117 public void printf(String fmt, Object... vars) {
118 access.printf(level, fmt, vars);
123 public AuthzTransImpl newTrans() {
125 times[idx]=System.currentTimeMillis();
126 if (++idx>=times.length)idx=0;
128 return new AuthzTransImpl(this);
132 * Create a Trans, but do not include in Weighted Average
135 public AuthzTrans newTransNoAvg() {
136 return new AuthzTransImpl(this);
139 public long transRate() {
143 for (int i=idx;i<times.length;++i) {
147 pot += times[i]-prev;
152 for (int i=0;i<idx;++i) {
156 pot += times[i]-prev;
162 return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
166 public ClassLoader classLoader() {
167 return getClass().getClassLoader();
171 public void load(InputStream is) throws IOException {
176 public void log(Level lvl, Object... msgs) {
177 access.log(lvl, msgs);
181 public void log(Exception e, Object... msgs) {
186 public void printf(Level level, String fmt, Object... elements) {
187 access.printf(level, fmt, elements);
191 * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
194 public boolean willLog(Level level) {
195 return access.willLog(level);
199 public void setLogLevel(Level level) {
200 access.setLogLevel(level);
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");
208 if (anytext || encrypted.startsWith("enc:")) {
209 if (decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
212 s = Symm.obtain(this);
213 } catch (CadiException e1) {
214 throw new IOException(e1);
216 decryptor = new Decryptor() {
217 private Symm symm = s;
219 public String decrypt(String encrypted) {
221 return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
222 ? symm.depass(encrypted)
224 } catch (IOException e) {
229 encryptor = new Encryptor() {
231 public String encrypt(String data) {
232 ByteArrayOutputStream baos = new ByteArrayOutputStream();
235 return "enc:"+s.enpass(data);
236 } catch (IOException e) {
243 return decryptor.decrypt(encrypted);
250 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String)
253 public String getProperty(String key) {
254 return access.getProperty(key);
258 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperties(java.lang.String[])
261 public Properties getProperties(String... filter) {
262 return access.getProperties();
266 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String, java.lang.String)
269 public String getProperty(String key, String defaultValue) {
270 return access.getProperty(key, defaultValue);
274 * @see org.onap.aaf.misc.env.impl.BasicEnv#setProperty(java.lang.String, java.lang.String)
277 public String setProperty(String key, String value) {
278 access.setProperty(key, value);
282 public PropAccess access() {
287 * @see org.onap.aaf.cadi.Access#getProperties()
290 public Properties getProperties() {
291 return access.getProperties();