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.config.Config;
42 import org.onap.aaf.cadi.util.MyConsole;
43 import org.onap.aaf.cadi.util.SubStandardConsole;
44 import org.onap.aaf.cadi.util.TheConsole;
47 public static final MyConsole cons = TheConsole.implemented() ? new TheConsole() : new SubStandardConsole();
48 // private static final int EIGHT_HOURS = 8 * 60 * 60 * 1000;
50 private Properties diskprops;
51 private boolean touchDiskprops;
52 private File dot_aaf = null;
53 private File sso = null; // instantiated, if ever, with diskprops
55 boolean removeSSO = false;
56 boolean loginOnly = false;
57 boolean doExit = true;
58 private PropAccess access;
59 private StringBuilder err;
61 private String encrypted_pass;
62 private boolean use_X509;
64 private PrintStream os;
67 private final PrintStream stdOutOrig;
68 private final PrintStream stdErrOrig;
71 public AAFSSO(String[] args) throws IOException, CadiException {
72 this(args,new Properties());
75 public AAFSSO(String[] args, ProcessArgs pa) throws IOException, CadiException {
76 this(args,pa.process(args, new Properties()));
79 public AAFSSO(String[] args, Properties dp) throws IOException, CadiException {
80 stdOutOrig = System.out;
81 stdErrOrig = System.err;
83 List<String> nargs = parseArgs(args);
85 touchDiskprops = false;
87 dot_aaf = new File(System.getProperty("user.home") + "/.aaf");
88 if (!dot_aaf.exists()) {
91 File f = new File(dot_aaf, "sso.out");
92 os = new PrintStream(new FileOutputStream(f, true));
96 sso = new File(dot_aaf, "sso.props");
98 InputStream propStream = new FileInputStream(sso);
100 diskprops.load(propStream);
106 File dot_aaf_kf = new File(dot_aaf, "keyfile");
109 if (dot_aaf_kf.exists()) {
110 dot_aaf_kf.setWritable(true, true);
114 Properties temp = new Properties();
116 for(Entry<Object, Object> es : diskprops.entrySet()) {
117 if(Config.CADI_LATITUDE.equals(es.getKey()) ||
118 Config.CADI_LONGITUDE.equals(es.getKey()) ||
119 Config.AAF_DEFAULT_REALM.equals(es.getKey())) {
120 temp.setProperty(es.getKey().toString(), es.getValue().toString());
124 touchDiskprops = true;
126 String[] naargs = new String[nargs.size()];
127 nargs.toArray(naargs);
128 access = new PropAccess(os, naargs);
131 System.out.println("AAF SSO information removed");
133 // Config.setDefaultRealm(access);
135 if (!dot_aaf_kf.exists()) {
136 FileOutputStream fos = new FileOutputStream(dot_aaf_kf);
138 fos.write(Symm.keygen());
139 setReadonly(dot_aaf_kf);
145 for(Entry<Object, Object> es : diskprops.entrySet()) {
146 nargs.add(es.getKey().toString() + '=' + es.getValue().toString());
148 String[] naargs = new String[nargs.size()];
149 nargs.toArray(naargs);
150 access = new PropAccess(os, naargs);
153 for(String tag : new String[] {Config.AAF_APPID, Config.AAF_APPPASS,
154 Config.CADI_ALIAS, Config.CADI_KEYSTORE,Config.CADI_KEYSTORE_PASSWORD,Config.CADI_KEY_PASSWORD}) {
155 access.getProperties().remove(tag);
156 diskprops.remove(tag);
159 // TODO Do we want to require reset of Passwords at least every Eight Hours.
160 // } else if (sso.lastModified() > (System.currentTimeMillis() - EIGHT_HOURS)) {
161 // for(String tag : new String[] {Config.AAF_APPPASS,Config.CADI_KEYSTORE_PASSWORD,Config.CADI_KEY_PASSWORD}) {
162 // access.getProperties().remove(tag);
163 // diskprops.remove(tag);
165 // touchDiskprops=true;
168 String keyfile = access.getProperty(Config.CADI_KEYFILE); // in case its CertificateMan props
169 if (keyfile == null) {
170 access.setProperty(Config.CADI_KEYFILE, dot_aaf_kf.getAbsolutePath());
171 addProp(Config.CADI_KEYFILE,dot_aaf_kf.getAbsolutePath());
176 alias = access.getProperty(Config.CADI_ALIAS);
178 appID = access.getProperty(Config.AAF_APPID);
185 if(appID!=null && access.getProperty(Config.AAF_APPPASS)==null) {
186 char[] password = cons.readPassword("Password for %s: ", appID);
187 String app_pass = access.encrypt(new String(password));
188 access.setProperty(Config.AAF_APPPASS,app_pass);
191 String keystore=access.getProperty(Config.CADI_KEYSTORE);
192 String keystore_pass=access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
194 if(user==null || (alias!=null && (keystore==null || keystore_pass==null))) {
195 String select = null;
197 for (File tsf : dot_aaf.listFiles()) {
198 name = tsf.getName();
199 if (!name.contains("trust") && (name.endsWith(".jks") || name.endsWith(".p12"))) {
201 select = cons.readLine("Use %s for Identity? (y/n): ",tsf.getName());
202 if("y".equalsIgnoreCase(select)) {
203 keystore = tsf.getCanonicalPath();
204 access.setProperty(Config.CADI_KEYSTORE, keystore);
205 addProp(Config.CADI_KEYSTORE, keystore);
206 char[] password = cons.readPassword("Keystore Password: ");
207 encrypted_pass= access.encrypt(new String(password));
208 access.setProperty(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
209 addProp(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
211 // TODO READ Aliases out of Keystore?
212 user = alias = cons.readLine("Keystore alias: ");
213 access.setProperty(Config.CADI_ALIAS, user);
214 addProp(Config.CADI_ALIAS, user);
220 user = appID = cons.readLine(Config.AAF_APPID + ": ");
221 access.setProperty(Config.AAF_APPID, appID);
222 addProp(Config.AAF_APPID, appID);
223 char[] password = cons.readPassword(Config.AAF_APPPASS + ": ");
224 encrypted_pass= access.encrypt(new String(password));
225 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
226 addProp(Config.AAF_APPPASS, encrypted_pass);
229 encrypted_pass = access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
230 if(encrypted_pass == null) {
231 keystore_pass = null;
232 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
234 keystore_pass = encrypted_pass;
243 Symm decryptor = Symm.obtain(dot_aaf_kf);
246 String cm_url = access.getProperty(Config.CM_URL); // SSO might overwrite...
247 FileInputStream fos = new FileInputStream(sso);
250 user = access.getProperty(Config.AAF_APPID);
251 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
252 // decrypt with .aaf, and re-encrypt with regular Keyfile
253 access.setProperty(Config.AAF_APPPASS,
254 access.encrypt(decryptor.depass(encrypted_pass)));
255 if (cm_url != null) { //Command line CM_URL Overwrites ssofile.
256 access.setProperty(Config.CM_URL, cm_url);
262 diskprops = new Properties();
263 String realm = Config.getDefaultRealm();
264 // Turn on Console Sysout
265 System.setOut(System.out);
266 user = cons.readLine("aaf_id(%s@%s): ", System.getProperty("user.name"), realm);
268 user = System.getProperty("user.name") + '@' + realm;
269 } else if (user.length() == 0) { //
270 user = System.getProperty("user.name") + '@' + realm;
271 } else if ((user.indexOf('@') < 0) && (realm != null)) {
272 user = user + '@' + realm;
274 access.setProperty(Config.AAF_APPID, user);
275 diskprops.setProperty(Config.AAF_APPID, user);
276 encrypted_pass = new String(cons.readPassword("aaf_password: "));
278 encrypted_pass = Symm.ENC + decryptor.enpass(encrypted_pass);
279 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
280 diskprops.setProperty(Config.AAF_APPPASS, encrypted_pass);
281 diskprops.setProperty(Config.CADI_KEYFILE, access.getProperty(Config.CADI_KEYFILE));
286 err = new StringBuilder("Add -D" + Config.AAF_APPID + "=<id> ");
289 if (encrypted_pass == null && alias == null) {
291 err = new StringBuilder();
295 err.append("-D" + Config.AAF_APPPASS + "=<passwd> ");
298 String locateUrl = access.getProperty(Config.AAF_LOCATE_URL);
299 if(locateUrl==null) {
300 locateUrl=AAFSSO.cons.readLine("AAF Locator URL=https://");
301 if(locateUrl==null || locateUrl.length()==0) {
302 err = new StringBuilder(Config.AAF_LOCATE_URL);
303 err.append(" is required.");
307 locateUrl="https://"+locateUrl;
309 access.setProperty(Config.AAF_LOCATE_URL, locateUrl);
310 addProp(Config.AAF_LOCATE_URL, locateUrl);
313 String aafUrl = "https://AAF_LOCATE_URL/AAF_NS.service:2.0";
314 access.setProperty(Config.AAF_URL, aafUrl);
315 access.setProperty(Config.CM_URL, "https://AAF_LOCATE_URL/AAF_NS.cm:2.0");
316 String cadiLatitude = access.getProperty(Config.CADI_LATITUDE);
317 if(cadiLatitude==null) {
318 System.out.println("# If you do not know your Global Coordinates, we suggest bing.com/maps");
319 cadiLatitude=AAFSSO.cons.readLine("cadi_latitude[0.000]=");
320 if(cadiLatitude==null || cadiLatitude.isEmpty()) {
321 cadiLatitude="0.000";
323 access.setProperty(Config.CADI_LATITUDE, cadiLatitude);
324 addProp(Config.CADI_LATITUDE, cadiLatitude);
327 String cadiLongitude = access.getProperty(Config.CADI_LONGITUDE);
328 if(cadiLongitude==null) {
329 cadiLongitude=AAFSSO.cons.readLine("cadi_longitude[0.000]=");
330 if(cadiLongitude==null || cadiLongitude.isEmpty()) {
331 cadiLongitude="0.000";
333 access.setProperty(Config.CADI_LONGITUDE, cadiLongitude);
334 addProp(Config.CADI_LONGITUDE, cadiLongitude);
337 String cadi_truststore = access.getProperty(Config.CADI_TRUSTSTORE);
338 if(cadi_truststore==null) {
341 for (File tsf : dot_aaf.listFiles()) {
342 name = tsf.getName();
343 if (name.contains("trust") &&
344 (name.endsWith(".jks") || name.endsWith(".p12"))) {
345 select = cons.readLine("Use %s for TrustStore? (y/n):",tsf.getName());
346 if("y".equalsIgnoreCase(select)) {
347 cadi_truststore=tsf.getCanonicalPath();
348 access.setProperty(Config.CADI_TRUSTSTORE, cadi_truststore);
349 addProp(Config.CADI_TRUSTSTORE, cadi_truststore);
355 if(cadi_truststore!=null) {
356 if(cadi_truststore.indexOf(File.separatorChar)<0) {
357 cadi_truststore=dot_aaf.getPath()+File.separator+cadi_truststore;
359 String cadi_truststore_password = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD);
360 if(cadi_truststore_password==null) {
361 cadi_truststore_password=AAFSSO.cons.readLine("cadi_truststore_password[%s]=","changeit");
362 cadi_truststore_password = access.encrypt(cadi_truststore_password);
363 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
364 addProp(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
372 public void setLogDefault() {
373 this.setLogDefault(PropAccess.DEFAULT);
374 System.setOut(stdOutOrig);
377 public void setStdErrDefault() {
378 access.setLogLevel(PropAccess.DEFAULT);
379 System.setErr(stdErrOrig);
382 public void setLogDefault(Level level) {
384 access.setLogLevel(level);
386 System.setOut(stdOutOrig);
389 public boolean loginOnly() {
393 public void addProp(String key, String value) {
394 if(key==null || value==null) {
398 diskprops.setProperty(key, value);
401 public void writeFiles() throws IOException {
403 // Store Creds, if they work
404 if (diskprops != null) {
405 if (!dot_aaf.exists()) {
408 FileOutputStream fos = new FileOutputStream(sso);
410 diskprops.store(fos, "AAF Single Signon");
418 sso.setWritable(true, true);
423 public PropAccess access() {
427 public StringBuilder err() {
431 public String user() {
435 public String enc_pass() {
436 return encrypted_pass;
439 public boolean useX509() {
443 public void close() {
447 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
448 // nothing to do here.
454 private List<String> parseArgs(String[] args)
456 List<String> larg = new ArrayList<String>(args.length);
458 // Cover for bash's need to escape *.. (\\*)
459 // also, remove SSO if required
460 for (int i = 0; i < args.length; ++i) {
461 if ("\\*".equals(args[i])) {
465 if ("-logout".equalsIgnoreCase(args[i])) {
467 } else if ("-login".equalsIgnoreCase(args[i])) {
469 } else if ("-noexit".equalsIgnoreCase(args[i])) {
478 private void setReadonly(File file) {
479 file.setExecutable(false, false);
480 file.setWritable(false, false);
481 file.setReadable(false, false);
482 file.setReadable(true, true);
485 public boolean ok() {
489 public static interface ProcessArgs {
490 public Properties process(final String[] args, final Properties props);