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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/**
* ============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.cadi;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import org.onap.aaf.cadi.config.Config;
import org.onap.aaf.cadi.config.SecurityInfo;
public class PropAccess implements Access {
// Sonar says cannot be static... it's ok. not too many PropAccesses created.
private final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
public static final Level DEFAULT = Level.AUDIT;
private Symm symm;
private int level;
private Properties props;
private List<String> recursionProtection = null;
private LogIt logIt;
private String name;
public PropAccess() {
logIt = new StreamLogIt(System.out);
init(null);
}
/**
* This Constructor soly exists to instantiate Servlet Context Based Logging that will call "init" later.
* @param sc
*/
protected PropAccess(Object o) {
logIt = new StreamLogIt(System.out);
props = new Properties();
}
public PropAccess(String ... args) {
this(System.out,args);
}
public PropAccess(PrintStream ps, String[] args) {
logIt = new StreamLogIt(ps==null?System.out:ps);
init(logIt,args);
}
public PropAccess(LogIt logit, String[] args) {
init(logit, args);
}
public PropAccess(Properties p) {
this(System.out,p);
}
public PropAccess(PrintStream ps, Properties p) {
logIt = new StreamLogIt(ps==null?System.out:ps);
init(p);
}
protected void init(final LogIt logIt, final String[] args) {
this.logIt = logIt;
Properties nprops=new Properties();
int eq;
for(String arg : args) {
if((eq=arg.indexOf('='))>0) {
nprops.setProperty(arg.substring(0, eq),arg.substring(eq+1));
}
}
init(nprops);
}
protected void init(Properties p) {
// Make sure these two are set before any changes in Logging
name = "cadi";
level=DEFAULT.maskOf();
props = new Properties();
// First, load related System Properties
for(Entry<Object,Object> es : System.getProperties().entrySet()) {
String key = es.getKey().toString();
for(String start : new String[] {"cadi_","aaf_","cm_"}) {
if(key.startsWith(start)) {
props.put(key, es.getValue());
}
}
}
// Second, overlay or fill in with Passed in Props
if(p!=null) {
props.putAll(p);
}
// Third, load any Chained Property Files
load(props.getProperty(Config.CADI_PROP_FILES));
String sLevel = props.getProperty(Config.CADI_LOGLEVEL);
if(sLevel!=null) {
level=Level.valueOf(sLevel).maskOf();
}
// Setup local Symmetrical key encryption
if(symm==null) {
try {
symm = Symm.obtain(this);
} catch (CadiException e) {
System.err.append("FATAL ERROR: Cannot obtain Key Information.");
e.printStackTrace(System.err);
System.exit(1);
}
}
name = props.getProperty(Config.CADI_LOGNAME, name);
specialConversions();
}
private void specialConversions() {
// Critical - if no Security Protocols set, then set it. We'll just get messed up if not
if(props.get(Config.CADI_PROTOCOLS)==null) {
props.setProperty(Config.CADI_PROTOCOLS, SecurityInfo.HTTPS_PROTOCOLS_DEFAULT);
}
Object temp;
temp=props.get(Config.CADI_PROTOCOLS);
if(props.get(Config.HTTPS_PROTOCOLS)==null && temp!=null) {
props.put(Config.HTTPS_PROTOCOLS, temp);
}
if(temp!=null) {
if("1.7".equals(System.getProperty("java.specification.version"))
&& (temp==null || (temp instanceof String && ((String)temp).contains("TLSv1.2")))) {
System.setProperty(Config.HTTPS_CIPHER_SUITES, Config.HTTPS_CIPHER_SUITES_DEFAULT);
}
}
}
private void load(String cadi_prop_files) {
if(cadi_prop_files==null) {
return;
}
String prevKeyFile = props.getProperty(Config.CADI_KEYFILE);
int prev = 0, end = cadi_prop_files.length();
int idx;
String filename;
while(prev<end) {
idx = cadi_prop_files.indexOf(File.pathSeparatorChar,prev);
if(idx<0) {
idx = end;
}
File file = new File(filename=cadi_prop_files.substring(prev,idx));
if(file.exists()) {
printf(Level.INIT,"Loading CADI Properties from %s",file.getAbsolutePath());
try {
FileInputStream fis = new FileInputStream(file);
try {
props.load(fis);
// Recursively Load
String chainProp = props.getProperty(Config.CADI_PROP_FILES);
if(chainProp!=null) {
if(recursionProtection==null) {
recursionProtection = new ArrayList<>();
recursionProtection.add(cadi_prop_files);
}
if(!recursionProtection.contains(chainProp)) {
recursionProtection.add(chainProp);
load(chainProp); // recurse
}
}
} finally {
fis.close();
}
} catch (Exception e) {
log(e,filename,"cannot be opened");
}
} else {
printf(Level.WARN,"Warning: recursive CADI Property %s does not exist",file.getAbsolutePath());
}
prev = idx+1;
}
// Trim
for(Entry<Object, Object> es : props.entrySet()) {
Object value = es.getValue();
if(value instanceof String) {
String trim = ((String)value).trim();
// Remove Beginning/End Quotes, which might be there if mixed with Bash Props
int s = 0, e=trim.length()-1;
if(s<e && trim.charAt(s)=='"' && trim.charAt(e)=='"') {
trim=trim.substring(s+1,e);
}
if(trim!=value) { // Yes, I want OBJECT equals
props.setProperty((String)es.getKey(), trim);
}
}
}
// Reset Symm if Keyfile Changes:
String newKeyFile = props.getProperty(Config.CADI_KEYFILE);
if((prevKeyFile!=null && newKeyFile!=null) || (newKeyFile!=null && !newKeyFile.equals(prevKeyFile))) {
try {
symm = Symm.obtain(this);
} catch (CadiException e) {
System.err.append("FATAL ERROR: Cannot obtain Key Information.");
e.printStackTrace(System.err);
System.exit(1);
}
prevKeyFile=newKeyFile;
}
String loglevel = props.getProperty(Config.CADI_LOGLEVEL);
if(loglevel!=null) {
try {
level=Level.valueOf(loglevel).maskOf();
} catch (IllegalArgumentException e) {
printf(Level.ERROR,"%s=%s is an Invalid Log Level",Config.CADI_LOGLEVEL,loglevel);
}
}
specialConversions();
}
@Override
public void load(InputStream is) throws IOException {
props.load(is);
load(props.getProperty(Config.CADI_PROP_FILES));
}
@Override
public void log(Level level, Object ... elements) {
if(willLog(level)) {
logIt.push(level,elements);
}
}
protected StringBuilder buildMsg(Level level, Object[] elements) {
return buildMsg(name,iso8601,level,elements);
}
public static StringBuilder buildMsg(final String name, final SimpleDateFormat sdf, Level level, Object[] elements) {
StringBuilder sb = new StringBuilder(sdf.format(new Date()));
sb.append(' ');
sb.append(level.name());
sb.append(" [");
sb.append(name);
int end = elements.length;
if(end<=0) {
sb.append("] ");
} else {
int idx = 0;
if(elements[idx] instanceof Integer) {
sb.append('-');
sb.append(elements[idx]);
++idx;
}
sb.append("] ");
String s;
boolean first = true;
for(Object o : elements) {
if(o!=null) {
s=o.toString();
if(first) {
first = false;
} else {
int l = s.length();
if(l>0) {
switch(s.charAt(l-1)) {
case ' ':
break;
default:
sb.append(' ');
}
}
}
sb.append(s);
}
}
}
return sb;
}
@Override
public void log(Exception e, Object... elements) {
log(Level.ERROR,e.getMessage(),elements);
e.printStackTrace(System.err);
}
@Override
public void printf(Level level, String fmt, Object... elements) {
if(willLog(level)) {
log(level,String.format(fmt, elements));
}
}
@Override
public void setLogLevel(Level level) {
this.level = level.maskOf();
}
@Override
public boolean willLog(Level level) {
return level.inMask(this.level);
}
@Override
public ClassLoader classLoader() {
return ClassLoader.getSystemClassLoader();
}
@Override
public String getProperty(String tag, String def) {
return props.getProperty(tag,def);
}
@Override
public String decrypt(String encrypted, boolean anytext) throws IOException {
return (encrypted!=null && (anytext==true || encrypted.startsWith(Symm.ENC)))
? symm.depass(encrypted)
: encrypted;
}
public String encrypt(String unencrypted) throws IOException {
return Symm.ENC+symm.enpass(unencrypted);
}
//////////////////
// Additional
//////////////////
public String getProperty(String tag) {
return props.getProperty(tag);
}
public Properties getProperties() {
return props;
}
public void setProperty(String tag, String value) {
if(value!=null) {
props.put(tag, value);
if(Config.CADI_KEYFILE.equals(tag)) {
// reset decryption too
try {
symm = Symm.obtain(this);
} catch (CadiException e) {
System.err.append("FATAL ERROR: Cannot obtain Key Information.");
e.printStackTrace(System.err);
System.exit(1);
}
}
}
}
public interface LogIt {
public void push(Level level, Object ... elements) ;
}
private class StreamLogIt implements LogIt {
private PrintStream ps;
public StreamLogIt(PrintStream ps) {
this.ps = ps;
}
@Override
public void push(Level level, Object ... elements) {
ps.println(buildMsg(level,elements));
ps.flush();
}
}
public void set(LogIt logit) {
logIt = logit;
}
}
|