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.cadi.sso;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.PrintStream;
30 import java.lang.reflect.InvocationTargetException;
31 import java.lang.reflect.Method;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.Properties;
37 import org.onap.aaf.cadi.Access.Level;
38 import org.onap.aaf.cadi.CadiException;
39 import org.onap.aaf.cadi.PropAccess;
40 import org.onap.aaf.cadi.Symm;
41 import org.onap.aaf.cadi.aaf.Defaults;
42 import org.onap.aaf.cadi.config.Config;
43 import org.onap.aaf.cadi.util.MyConsole;
44 import org.onap.aaf.cadi.util.SubStandardConsole;
45 import org.onap.aaf.cadi.util.TheConsole;
48 public static final MyConsole cons = TheConsole.implemented() ? new TheConsole() : new SubStandardConsole();
49 // private static final int EIGHT_HOURS = 8 * 60 * 60 * 1000;
51 private Properties diskprops;
52 private boolean touchDiskprops;
53 private File dot_aaf = null;
54 private File sso = null; // instantiated, if ever, with diskprops
56 boolean removeSSO = false;
57 boolean loginOnly = false;
58 boolean doExit = true;
59 private PropAccess access;
60 private StringBuilder err;
62 private String encrypted_pass;
63 private boolean use_X509;
65 private PrintStream os;
68 private final PrintStream stdOutOrig;
69 private final PrintStream stdErrOrig;
72 public AAFSSO(String[] args) throws IOException, CadiException {
73 this(args,new Properties());
76 public AAFSSO(String[] args, ProcessArgs pa) throws IOException, CadiException {
77 this(args,pa.process(args, new Properties()));
80 public AAFSSO(String[] args, Properties dp) throws IOException, CadiException {
81 stdOutOrig = System.out;
82 stdErrOrig = System.err;
84 List<String> nargs = parseArgs(args);
86 touchDiskprops = false;
88 dot_aaf = new File(System.getProperty("user.home") + "/.aaf");
89 if (!dot_aaf.exists()) {
92 File f = new File(dot_aaf, "sso.out");
93 os = new PrintStream(new FileOutputStream(f, true));
97 sso = new File(dot_aaf, "sso.props");
99 InputStream propStream = new FileInputStream(sso);
101 diskprops.load(propStream);
107 File dot_aaf_kf = new File(dot_aaf, "keyfile");
110 if (dot_aaf_kf.exists()) {
111 dot_aaf_kf.setWritable(true, true);
115 Properties temp = new Properties();
117 for (Entry<Object, Object> es : diskprops.entrySet()) {
118 if (Config.CADI_LATITUDE.equals(es.getKey()) ||
119 Config.CADI_LONGITUDE.equals(es.getKey()) ||
120 Config.AAF_DEFAULT_REALM.equals(es.getKey())) {
121 temp.setProperty(es.getKey().toString(), es.getValue().toString());
125 touchDiskprops = true;
127 String[] naargs = new String[nargs.size()];
128 nargs.toArray(naargs);
129 access = new PropAccess(os, naargs);
132 System.out.println("AAF SSO information removed");
134 // Config.setDefaultRealm(access);
136 if (!dot_aaf_kf.exists()) {
137 FileOutputStream fos = new FileOutputStream(dot_aaf_kf);
139 fos.write(Symm.keygen());
140 setReadonly(dot_aaf_kf);
146 for (Entry<Object, Object> es : diskprops.entrySet()) {
147 nargs.add(es.getKey().toString() + '=' + es.getValue().toString());
149 String[] naargs = new String[nargs.size()];
150 nargs.toArray(naargs);
151 access = new PropAccess(os, naargs);
154 for (String tag : new String[] {Config.AAF_APPID, Config.AAF_APPPASS,
155 Config.CADI_ALIAS, Config.CADI_KEYSTORE,Config.CADI_KEYSTORE_PASSWORD,Config.CADI_KEY_PASSWORD}) {
156 access.getProperties().remove(tag);
157 diskprops.remove(tag);
160 // TODO Do we want to require reset of Passwords at least every Eight Hours.
161 // } else if (sso.lastModified() > (System.currentTimeMillis() - EIGHT_HOURS)) {
162 // for (String tag : new String[] {Config.AAF_APPPASS,Config.CADI_KEYSTORE_PASSWORD,Config.CADI_KEY_PASSWORD}) {
163 // access.getProperties().remove(tag);
164 // diskprops.remove(tag);
166 // touchDiskprops=true;
169 String keyfile = access.getProperty(Config.CADI_KEYFILE); // in case its CertificateMan props
170 if (keyfile == null) {
171 access.setProperty(Config.CADI_KEYFILE, dot_aaf_kf.getAbsolutePath());
172 addProp(Config.CADI_KEYFILE,dot_aaf_kf.getAbsolutePath());
177 alias = access.getProperty(Config.CADI_ALIAS);
179 appID = access.getProperty(Config.AAF_APPID);
186 if (appID!=null && access.getProperty(Config.AAF_APPPASS)==null) {
187 char[] password = cons.readPassword("Password for %s: ", appID);
188 String app_pass = access.encrypt(new String(password));
189 access.setProperty(Config.AAF_APPPASS,app_pass);
190 diskprops.setProperty(Config.AAF_APPPASS, app_pass);
193 String keystore=access.getProperty(Config.CADI_KEYSTORE);
194 String keystore_pass=access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
196 if (user==null || (alias!=null && (keystore==null || keystore_pass==null))) {
197 String select = null;
199 for (File tsf : dot_aaf.listFiles()) {
200 name = tsf.getName();
201 if (!name.contains("trust") && (name.endsWith(".jks") || name.endsWith(".p12"))) {
203 select = cons.readLine("Use %s for Identity? (y/n): ",tsf.getName());
204 if ("y".equalsIgnoreCase(select)) {
205 keystore = tsf.getCanonicalPath();
206 access.setProperty(Config.CADI_KEYSTORE, keystore);
207 addProp(Config.CADI_KEYSTORE, keystore);
208 char[] password = cons.readPassword("Keystore Password: ");
209 encrypted_pass= access.encrypt(new String(password));
210 access.setProperty(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
211 addProp(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
213 // TODO READ Aliases out of Keystore?
214 user = alias = cons.readLine("Keystore alias: ");
215 access.setProperty(Config.CADI_ALIAS, user);
216 addProp(Config.CADI_ALIAS, user);
222 user = appID = cons.readLine(Config.AAF_APPID + ": ");
223 access.setProperty(Config.AAF_APPID, appID);
224 addProp(Config.AAF_APPID, appID);
225 char[] password = cons.readPassword(Config.AAF_APPPASS + ": ");
226 encrypted_pass= access.encrypt(new String(password));
227 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
228 addProp(Config.AAF_APPPASS, encrypted_pass);
231 encrypted_pass = access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
232 if (encrypted_pass == null) {
233 keystore_pass = null;
234 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
236 keystore_pass = encrypted_pass;
245 Symm decryptor = Symm.obtain(dot_aaf_kf);
248 String cm_url = access.getProperty(Config.CM_URL); // SSO might overwrite...
249 FileInputStream fos = new FileInputStream(sso);
252 user = access.getProperty(Config.AAF_APPID);
253 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
254 // decrypt with .aaf, and re-encrypt with regular Keyfile
255 access.setProperty(Config.AAF_APPPASS,
256 access.encrypt(decryptor.depass(encrypted_pass)));
257 if (cm_url != null) { //Command line CM_URL Overwrites ssofile.
258 access.setProperty(Config.CM_URL, cm_url);
264 diskprops = new Properties();
265 String realm = Config.getDefaultRealm();
266 // Turn on Console Sysout
267 System.setOut(System.out);
268 user = cons.readLine("aaf_id(%s@%s): ", System.getProperty("user.name"), realm);
270 user = System.getProperty("user.name") + '@' + realm;
271 } else if (user.length() == 0) { //
272 user = System.getProperty("user.name") + '@' + realm;
273 } else if ((user.indexOf('@') < 0) && (realm != null)) {
274 user = user + '@' + realm;
276 access.setProperty(Config.AAF_APPID, user);
277 diskprops.setProperty(Config.AAF_APPID, user);
278 encrypted_pass = new String(cons.readPassword("aaf_password: "));
280 encrypted_pass = Symm.ENC + decryptor.enpass(encrypted_pass);
281 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
282 diskprops.setProperty(Config.AAF_APPPASS, encrypted_pass);
283 diskprops.setProperty(Config.CADI_KEYFILE, access.getProperty(Config.CADI_KEYFILE));
288 err = new StringBuilder("Add -D" + Config.AAF_APPID + "=<id> ");
291 if (encrypted_pass == null && alias == null) {
293 err = new StringBuilder();
297 err.append("-D" + Config.AAF_APPPASS + "=<passwd> ");
300 String locateUrl = access.getProperty(Config.AAF_LOCATE_URL);
301 if (locateUrl==null) {
302 locateUrl=AAFSSO.cons.readLine("AAF Locator URL=https://");
303 if (locateUrl==null || locateUrl.length()==0) {
304 err = new StringBuilder(Config.AAF_LOCATE_URL);
305 err.append(" is required.");
309 locateUrl="https://"+locateUrl;
311 access.setProperty(Config.AAF_LOCATE_URL, locateUrl);
312 addProp(Config.AAF_LOCATE_URL, locateUrl);
315 access.setProperty(Config.AAF_URL, Defaults.AAF_URL);
316 access.setProperty(Config.CM_URL, Defaults.CM_URL);
317 String cadiLatitude = access.getProperty(Config.CADI_LATITUDE);
318 if (cadiLatitude==null) {
319 System.out.println("# If you do not know your Global Coordinates, we suggest bing.com/maps");
320 cadiLatitude=AAFSSO.cons.readLine("cadi_latitude[0.000]=");
321 if (cadiLatitude==null || cadiLatitude.isEmpty()) {
322 cadiLatitude="0.000";
324 access.setProperty(Config.CADI_LATITUDE, cadiLatitude);
325 addProp(Config.CADI_LATITUDE, cadiLatitude);
328 String cadiLongitude = access.getProperty(Config.CADI_LONGITUDE);
329 if (cadiLongitude==null) {
330 cadiLongitude=AAFSSO.cons.readLine("cadi_longitude[0.000]=");
331 if (cadiLongitude==null || cadiLongitude.isEmpty()) {
332 cadiLongitude="0.000";
334 access.setProperty(Config.CADI_LONGITUDE, cadiLongitude);
335 addProp(Config.CADI_LONGITUDE, cadiLongitude);
338 String cadi_truststore = access.getProperty(Config.CADI_TRUSTSTORE);
339 if (cadi_truststore==null) {
342 for (File tsf : dot_aaf.listFiles()) {
343 name = tsf.getName();
344 if (name.contains("trust") &&
345 (name.endsWith(".jks") || name.endsWith(".p12"))) {
346 select = cons.readLine("Use %s for TrustStore? (y/n):",tsf.getName());
347 if ("y".equalsIgnoreCase(select)) {
348 cadi_truststore=tsf.getCanonicalPath();
349 access.setProperty(Config.CADI_TRUSTSTORE, cadi_truststore);
350 addProp(Config.CADI_TRUSTSTORE, cadi_truststore);
356 if (cadi_truststore!=null) {
357 if (cadi_truststore.indexOf(File.separatorChar)<0) {
358 cadi_truststore=dot_aaf.getPath()+File.separator+cadi_truststore;
360 String cadi_truststore_password = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD);
361 if (cadi_truststore_password==null) {
362 cadi_truststore_password=AAFSSO.cons.readLine("cadi_truststore_password[%s]=","changeit");
363 cadi_truststore_password = access.encrypt(cadi_truststore_password);
364 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
365 addProp(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
373 public void setLogDefault() {
374 this.setLogDefault(PropAccess.DEFAULT);
375 System.setOut(stdOutOrig);
378 public void setStdErrDefault() {
379 access.setLogLevel(PropAccess.DEFAULT);
380 System.setErr(stdErrOrig);
383 public void setLogDefault(Level level) {
385 access.setLogLevel(level);
387 System.setOut(stdOutOrig);
390 public boolean loginOnly() {
394 public void addProp(String key, String value) {
395 if (key==null || value==null) {
399 diskprops.setProperty(key, value);
402 public void writeFiles() throws IOException {
403 if (touchDiskprops) {
404 // Store Creds, if they work
405 if (diskprops != null) {
406 if (!dot_aaf.exists()) {
409 FileOutputStream fos = new FileOutputStream(sso);
411 diskprops.store(fos, "AAF Single Signon");
419 sso.setWritable(true, true);
424 public PropAccess access() {
428 public StringBuilder err() {
432 public String user() {
436 public String enc_pass() {
437 return encrypted_pass;
440 public boolean useX509() {
444 public void close() {
448 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
449 // nothing to do here.
455 private List<String> parseArgs(String[] args)
457 List<String> larg = new ArrayList<>(args.length);
459 // Cover for bash's need to escape *.. (\\*)
460 // also, remove SSO if required
461 for (int i = 0; i < args.length; ++i) {
462 if ("\\*".equals(args[i])) {
466 if ("-logout".equalsIgnoreCase(args[i])) {
468 } else if ("-login".equalsIgnoreCase(args[i])) {
470 } else if ("-noexit".equalsIgnoreCase(args[i])) {
479 private void setReadonly(File file) {
480 file.setExecutable(false, false);
481 file.setWritable(false, false);
482 file.setReadable(false, false);
483 file.setReadable(true, true);
486 public boolean ok() {
490 public static interface ProcessArgs {
491 public Properties process(final String[] args, final Properties props);