summaryrefslogtreecommitdiffstats
path: root/auth/auth-core/src/main/java/org/onap/aaf/auth/env/AuthzEnv.java
blob: 0bbe079ec304292b03fc890e8e1137df28063c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.env;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.onap.aaf.cadi.Access;
import org.onap.aaf.cadi.CadiException;
import org.onap.aaf.cadi.PropAccess;
import org.onap.aaf.cadi.Symm;
import org.onap.aaf.cadi.PropAccess.LogIt;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.misc.env.APIException;
import org.onap.aaf.misc.env.Decryptor;
import org.onap.aaf.misc.env.Encryptor;
import org.onap.aaf.misc.env.impl.Log4JLogTarget;
import org.onap.aaf.misc.env.log4j.LogFileNamer;
import org.onap.aaf.misc.rosetta.env.RosettaEnv;


/**
 * AuthzEnv is the Env tailored to Authz Service
 * 
 * Most of it is derived from RosettaEnv, but it also implements Access, which
 * is an Interface that Allows CADI to interact with Container Logging
 * 
 * @author Jonathan
 *
 */
public class AuthzEnv extends RosettaEnv implements Access {
	private long[] times = new long[20];
	private int idx = 0;
	private PropAccess access;

	public AuthzEnv() {
		super();
		_init(new PropAccess());
	}

	public AuthzEnv(String ... args) {
		super();
		_init(new PropAccess(args));
	}

	public AuthzEnv(Properties props) {
		super();
		_init(new PropAccess(props));
	}
	

	public AuthzEnv(PropAccess pa) {
		super();
		_init(pa);
	}
	
	private final void _init(PropAccess pa) { 
		access = pa;
		times = new long[20];
		idx = 0;
	}
	
	private class Log4JLogit implements LogIt {
		
		@Override
		public void push(Level level, Object... elements) {
			switch(level) {
				case AUDIT:
					audit.log(elements);
					break;
				case DEBUG:
					debug.log(elements);
					break;
				case ERROR:
					error.log(elements);
					break;
				case INFO:
					info.log(elements);
					break;
				case INIT:
					init.log(elements);
					break;
				case NONE:
					break;
				case WARN:
					warn.log(elements);
					break;
			}
			
		}
		
	}

	@Override
	public AuthzTransImpl newTrans() {
		synchronized(this) {
			times[idx]=System.currentTimeMillis();
			if(++idx>=times.length)idx=0;
		}
		return new AuthzTransImpl(this);
	}

	/**
	 *  Create a Trans, but do not include in Weighted Average
	 * @return
	 */
	public AuthzTrans newTransNoAvg() {
		return new AuthzTransImpl(this);
	}

	public long transRate() {
		int count = 0;
		long pot = 0;
		long prev = 0;
		for(int i=idx;i<times.length;++i) {
			if(times[i]>0) {
				if(prev>0) {
					++count;
		pot += times[i]-prev;
				}
				prev = times[i]; 
			}
		}
		for(int i=0;i<idx;++i) {
			if(times[i]>0) {
				if(prev>0) {
					++count;
					pot += times[i]-prev;
				}
				prev = times[i]; 
			}
		}

		return count==0?300000L:pot/count; // Return Weighted Avg, or 5 mins, if none avail.
	}
	
	@Override
	public ClassLoader classLoader() {
		return getClass().getClassLoader();
	}

	@Override
	public void load(InputStream is) throws IOException {
		access.load(is);
	}

	@Override
	public void log(Level lvl, Object... msgs) {
		access.log(lvl, msgs);
	}

	@Override
	public void log(Exception e, Object... msgs) {
		access.log(e,msgs);
	}

	@Override
	public void printf(Level level, String fmt, Object... elements) {
		access.printf(level, fmt, elements);
	}

	/* (non-Javadoc)
	 * @see org.onap.aaf.cadi.Access#willLog(org.onap.aaf.cadi.Access.Level)
	 */
	@Override
	public boolean willLog(Level level) {
		return access.willLog(level);
	}

	@Override
	public void setLogLevel(Level level) {
		access.setLogLevel(level);
	}

	public void setLog4JNames(String path, String root, String _service, String _audit, String _init, String _trace) throws APIException {
		LogFileNamer lfn = new LogFileNamer(root);
		if(_service==null) {
			throw new APIException("AuthzEnv.setLog4JNames \"_service\" required (as default).  Others can be null");
		}
		String service=_service=lfn.setAppender(_service); // when name is split, i.e. authz|service, the Appender is "authz", and "service"
		String audit=_audit==null?service:lfn.setAppender(_audit);     // is part of the log-file name
		String init=_init==null?service:lfn.setAppender(_init);
		String trace=_trace==null?service:lfn.setAppender(_trace);
		//TODO Validate path on Classpath
		lfn.configure(path);
		super.fatal = new Log4JLogTarget(service,org.apache.log4j.Level.FATAL);
		super.error = new Log4JLogTarget(service,org.apache.log4j.Level.ERROR);
		super.warn = new Log4JLogTarget(service,org.apache.log4j.Level.WARN);
		super.audit = new Log4JLogTarget(audit,org.apache.log4j.Level.WARN);
		super.init = new Log4JLogTarget(init,org.apache.log4j.Level.WARN);
		super.info = new Log4JLogTarget(service,org.apache.log4j.Level.INFO);
		super.debug = new Log4JLogTarget(service,org.apache.log4j.Level.DEBUG);
		super.trace = new Log4JLogTarget(trace,org.apache.log4j.Level.TRACE);
		
		access.set(new Log4JLogit());
	}
	
	private static final byte[] ENC="enc:".getBytes();
	public String decrypt(String encrypted, final boolean anytext) throws IOException {
		if(encrypted==null) {
			throw new IOException("Password to be decrypted is null");
		}
		if(anytext || encrypted.startsWith("enc:")) {
			if(decryptor.equals(Decryptor.NULL) && getProperty(Config.CADI_KEYFILE)!=null) {
				final Symm s;
				try {
					s = Symm.obtain(this);
				} catch (CadiException e1) {
					throw new IOException(e1);
				}
				decryptor = new Decryptor() {
					private Symm symm = s;
					@Override
					public String decrypt(String encrypted) {
						try {
							return (encrypted!=null && (anytext || encrypted.startsWith(Symm.ENC)))
									? symm.depass(encrypted)
									: encrypted;
						} catch (IOException e) {
							return "";
						}
					}
				};
				encryptor = new Encryptor() {
					@Override
					public String encrypt(String data) {
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						try {
							baos.write(ENC);
							return "enc:"+s.enpass(data);
						} catch (IOException e) {
							return "";
						}
					}
	
				};
			}
			return decryptor.decrypt(encrypted);
		} else {
			return encrypted;
		}
	}

	/* (non-Javadoc)
	 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String)
	 */
	@Override
	public String getProperty(String key) {
		return access.getProperty(key);
	}

	/* (non-Javadoc)
	 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperties(java.lang.String[])
	 */
	@Override
	public Properties getProperties(String... filter) {
		return access.getProperties();
	}

	/* (non-Javadoc)
	 * @see org.onap.aaf.misc.env.impl.BasicEnv#getProperty(java.lang.String, java.lang.String)
	 */
	@Override
	public String getProperty(String key, String defaultValue) {
		return access.getProperty(key, defaultValue);
	}

	/* (non-Javadoc)
	 * @see org.onap.aaf.misc.env.impl.BasicEnv#setProperty(java.lang.String, java.lang.String)
	 */
	@Override
	public String setProperty(String key, String value) {
		access.setProperty(key, value);
		return value;
	}

	public PropAccess access() {
		return access;
	}

	/* (non-Javadoc)
	 * @see org.onap.aaf.cadi.Access#getProperties()
	 */
	@Override
	public Properties getProperties() {
		return access.getProperties();
	};
	
}