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 access.log(Level.ERROR, msgs);
109 public boolean isLoggable() {
110 return access.willLog(level);
114 public void printf(String fmt, Object... vars) {
115 access.printf(level, fmt, vars);
120 public AuthzTransImpl newTrans() {
122 times[idx]=System.currentTimeMillis();
123 if(++idx>=times.length)idx=0;
125 return new AuthzTransImpl(this);
129 * Create a Trans, but do not include in Weighted Average
132 public AuthzTrans newTransNoAvg() {
133 return new AuthzTransImpl(this);
136 public long transRate() {
140 for(int i=idx;i<times.length;++i) {
144 pot += times[i]-prev;
149 for(int i=0;i<idx;++i) {
153 pot += times[i]-prev;
159 return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
163 public ClassLoader classLoader() {
164 return getClass().getClassLoader();
168 public void load(InputStream is) throws IOException {
173 public void log(Level lvl, Object... msgs) {
174 access.log(lvl, msgs);
178 public void log(Exception e, Object... msgs) {
183 public void printf(Level level, String fmt, Object... elements) {
184 access.printf(level, fmt, elements);
188 * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
191 public boolean willLog(Level level) {
192 return access.willLog(level);
196 public void setLogLevel(Level level) {
197 access.setLogLevel(level);
200 private static final byte[] ENC="enc:".getBytes();
201 public String decrypt(String encrypted, final boolean anytext) throws IOException {
202 if(encrypted==null) {
203 throw new IOException("Password to be decrypted is null");
205 if(anytext || encrypted.startsWith("enc:")) {
206 if(decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
209 s = Symm.obtain(this);
210 } catch (CadiException e1) {
211 throw new IOException(e1);
213 decryptor = new Decryptor() {
214 private Symm symm = s;
216 public String decrypt(String encrypted) {
218 return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
219 ? symm.depass(encrypted)
221 } catch (IOException e) {
226 encryptor = new Encryptor() {
228 public String encrypt(String data) {
229 ByteArrayOutputStream baos = new ByteArrayOutputStream();
232 return "enc:"+s.enpass(data);
233 } catch (IOException e) {
240 return decryptor.decrypt(encrypted);
247 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String)
250 public String getProperty(String key) {
251 return access.getProperty(key);
255 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperties(java.lang.String[])
258 public Properties getProperties(String... filter) {
259 return access.getProperties();
263 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String, java.lang.String)
266 public String getProperty(String key, String defaultValue) {
267 return access.getProperty(key, defaultValue);
271 * @see org.onap.aaf.misc.env.impl.BasicEnv#setProperty(java.lang.String, java.lang.String)
274 public String setProperty(String key, String value) {
275 access.setProperty(key, value);
279 public PropAccess access() {
284 * @see org.onap.aaf.cadi.Access#getProperties()
287 public Properties getProperties() {
288 return access.getProperties();