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 {
73 List<String> nargs = parseArgs(args);
74 diskprops = new Properties();
75 touchDiskprops = false;
77 dot_aaf = new File(System.getProperty("user.home") + "/.aaf");
78 if (!dot_aaf.exists()) {
81 stdOutOrig = System.out;
82 stdErrOrig = System.err;
83 File f = new File(dot_aaf, "sso.out");
84 os = new PrintStream(new FileOutputStream(f, true));
88 sso = new File(dot_aaf, "sso.props");
90 InputStream propStream = new FileInputStream(sso);
92 diskprops.load(propStream);
98 // String keyfile = diskprops.getProperty(Config.CADI_KEYFILE);
99 // if(keyfile==null) {
100 // keyfile = dot_aaf.getCanonicalPath()+".keyfile";
101 // touchDiskprops=true;
103 File dot_aaf_kf = new File(dot_aaf, "keyfile");
106 if (dot_aaf_kf.exists()) {
107 dot_aaf_kf.setWritable(true, true);
111 Properties temp = new Properties();
113 for(Entry<Object, Object> es : diskprops.entrySet()) {
114 if(Config.CADI_LATITUDE.equals(es.getKey()) ||
115 Config.CADI_LONGITUDE.equals(es.getKey()) ||
116 Config.AAF_DEFAULT_REALM.equals(es.getKey())) {
117 temp.setProperty(es.getKey().toString(), es.getValue().toString());
121 touchDiskprops = true;
123 String[] naargs = new String[nargs.size()];
124 nargs.toArray(naargs);
125 access = new PropAccess(os, naargs);
128 System.out.println("AAF SSO information removed");
130 // Config.setDefaultRealm(access);
132 if (!dot_aaf_kf.exists()) {
133 FileOutputStream fos = new FileOutputStream(dot_aaf_kf);
135 fos.write(Symm.keygen());
136 setReadonly(dot_aaf_kf);
142 for(Entry<Object, Object> es : diskprops.entrySet()) {
143 nargs.add(es.getKey().toString() + '=' + es.getValue().toString());
145 String[] naargs = new String[nargs.size()];
146 nargs.toArray(naargs);
147 access = new PropAccess(os, naargs);
150 for(String tag : new String[] {Config.AAF_APPID, Config.AAF_APPPASS,
151 Config.CADI_ALIAS, Config.CADI_KEYSTORE,Config.CADI_KEYSTORE_PASSWORD,Config.CADI_KEY_PASSWORD}) {
152 access.getProperties().remove(tag);
153 diskprops.remove(tag);
156 // TODO Do we want to require reset of Passwords at least every Eight Hours.
157 // } else if (sso.lastModified() > (System.currentTimeMillis() - EIGHT_HOURS)) {
158 // for(String tag : new String[] {Config.AAF_APPPASS,Config.CADI_KEYSTORE_PASSWORD,Config.CADI_KEY_PASSWORD}) {
159 // access.getProperties().remove(tag);
160 // diskprops.remove(tag);
162 // touchDiskprops=true;
165 String keyfile = access.getProperty(Config.CADI_KEYFILE); // in case its CertificateMan props
166 if (keyfile == null) {
167 access.setProperty(Config.CADI_KEYFILE, dot_aaf_kf.getAbsolutePath());
168 addProp(Config.CADI_KEYFILE,dot_aaf_kf.getAbsolutePath());
173 alias = access.getProperty(Config.CADI_ALIAS);
175 appID = access.getProperty(Config.AAF_APPID);
182 String keystore=access.getProperty(Config.CADI_KEYSTORE);
183 String keystore_pass=access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
185 if(user==null || (alias!=null && (keystore==null || keystore_pass==null))) {
186 String select = null;
188 for (File tsf : dot_aaf.listFiles()) {
189 name = tsf.getName();
190 if (!name.contains("trust") && (name.endsWith(".jks") || name.endsWith(".p12"))) {
191 select = cons.readLine("Use %s for Identity? (y/n): ",tsf.getName());
192 if("y".equalsIgnoreCase(select)) {
193 keystore = tsf.getCanonicalPath();
194 access.setProperty(Config.CADI_KEYSTORE, keystore);
195 addProp(Config.CADI_KEYSTORE, keystore);
196 char[] password = cons.readPassword("Keystore Password: ");
197 encrypted_pass= access.encrypt(new String(password));
198 access.setProperty(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
199 addProp(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
201 // TODO READ Aliases out of Keystore?
202 user = alias = cons.readLine("Keystore alias: ");
203 access.setProperty(Config.CADI_ALIAS, user);
204 addProp(Config.CADI_ALIAS, user);
210 user = appID = cons.readLine(Config.AAF_APPID + ": ");
211 access.setProperty(Config.AAF_APPID, appID);
212 addProp(Config.AAF_APPID, appID);
213 char[] password = cons.readPassword(Config.AAF_APPPASS + ": ");
214 encrypted_pass= access.encrypt(new String(password));
215 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
216 addProp(Config.AAF_APPPASS, encrypted_pass);
219 encrypted_pass = access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
220 if(encrypted_pass == null) {
221 keystore_pass = null;
222 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
224 keystore_pass = encrypted_pass;
233 Symm decryptor = Symm.obtain(dot_aaf_kf);
236 String cm_url = access.getProperty(Config.CM_URL); // SSO might overwrite...
237 FileInputStream fos = new FileInputStream(sso);
240 user = access.getProperty(Config.AAF_APPID);
241 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
242 // decrypt with .aaf, and re-encrypt with regular Keyfile
243 access.setProperty(Config.AAF_APPPASS,
244 access.encrypt(decryptor.depass(encrypted_pass)));
245 if (cm_url != null) { //Command line CM_URL Overwrites ssofile.
246 access.setProperty(Config.CM_URL, cm_url);
252 diskprops = new Properties();
253 String realm = Config.getDefaultRealm();
254 // Turn on Console Sysout
255 System.setOut(System.out);
256 user = cons.readLine("aaf_id(%s@%s): ", System.getProperty("user.name"), realm);
258 user = System.getProperty("user.name") + '@' + realm;
259 } else if (user.length() == 0) { //
260 user = System.getProperty("user.name") + '@' + realm;
261 } else if ((user.indexOf('@') < 0) && (realm != null)) {
262 user = user + '@' + realm;
264 access.setProperty(Config.AAF_APPID, user);
265 diskprops.setProperty(Config.AAF_APPID, user);
266 encrypted_pass = new String(cons.readPassword("aaf_password: "));
268 encrypted_pass = Symm.ENC + decryptor.enpass(encrypted_pass);
269 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
270 diskprops.setProperty(Config.AAF_APPPASS, encrypted_pass);
271 diskprops.setProperty(Config.CADI_KEYFILE, access.getProperty(Config.CADI_KEYFILE));
276 err = new StringBuilder("Add -D" + Config.AAF_APPID + "=<id> ");
279 if (encrypted_pass == null && alias == null) {
281 err = new StringBuilder();
285 err.append("-D" + Config.AAF_APPPASS + "=<passwd> ");
288 String locateUrl = access.getProperty(Config.AAF_LOCATE_URL);
289 if(locateUrl==null) {
290 locateUrl=AAFSSO.cons.readLine("AAF Locator FQDN/machine[:port]=https://");
291 if(locateUrl==null || locateUrl.length()==0) {
292 err = new StringBuilder(Config.AAF_LOCATE_URL);
293 err.append(" is required.");
297 locateUrl="https://"+locateUrl+"/locate";
299 access.setProperty(Config.AAF_LOCATE_URL, locateUrl);
300 addProp(Config.AAF_LOCATE_URL, locateUrl);
303 String aafUrl = "https://AAF_LOCATE_URL/AAF_NS.service/2.0";
304 access.setProperty(Config.AAF_URL, aafUrl);
305 access.setProperty(Config.CM_URL, "https://AAF_LOCATE_URL/AAF_NS.cm/2.0");
306 String cadiLatitude = access.getProperty(Config.CADI_LATITUDE);
307 if(cadiLatitude==null) {
308 System.out.println("# If you do not know your Global Coordinates, we suggest bing.com/maps");
309 cadiLatitude=AAFSSO.cons.readLine("cadi_latitude[0.000]=");
310 if(cadiLatitude==null || cadiLatitude.isEmpty()) {
311 cadiLatitude="0.000";
313 access.setProperty(Config.CADI_LATITUDE, cadiLatitude);
314 addProp(Config.CADI_LATITUDE, cadiLatitude);
317 String cadiLongitude = access.getProperty(Config.CADI_LONGITUDE);
318 if(cadiLongitude==null) {
319 cadiLongitude=AAFSSO.cons.readLine("cadi_longitude[0.000]=");
320 if(cadiLongitude==null || cadiLongitude.isEmpty()) {
321 cadiLongitude="0.000";
323 access.setProperty(Config.CADI_LONGITUDE, cadiLongitude);
324 addProp(Config.CADI_LONGITUDE, cadiLongitude);
327 String cadi_truststore = access.getProperty(Config.CADI_TRUSTSTORE);
328 if(cadi_truststore==null) {
331 for (File tsf : dot_aaf.listFiles()) {
332 name = tsf.getName();
333 if (name.contains("trust") &&
334 (name.endsWith(".jks") || name.endsWith(".p12"))) {
335 select = cons.readLine("Use %s for TrustStore? (y/n):",tsf.getName());
336 if("y".equalsIgnoreCase(select)) {
337 cadi_truststore=tsf.getCanonicalPath();
338 access.setProperty(Config.CADI_TRUSTSTORE, cadi_truststore);
339 addProp(Config.CADI_TRUSTSTORE, cadi_truststore);
345 if(cadi_truststore!=null) {
346 if(cadi_truststore.indexOf(File.separatorChar)<0) {
347 cadi_truststore=dot_aaf.getPath()+File.separator+cadi_truststore;
349 String cadi_truststore_password = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD);
350 if(cadi_truststore_password==null) {
351 cadi_truststore_password=AAFSSO.cons.readLine("cadi_truststore_password[%s]=","changeit");
352 cadi_truststore_password = access.encrypt(cadi_truststore_password);
353 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
354 addProp(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
362 public void setLogDefault() {
363 this.setLogDefault(PropAccess.DEFAULT);
364 System.setOut(stdOutOrig);
367 public void setStdErrDefault() {
368 access.setLogLevel(PropAccess.DEFAULT);
369 System.setOut(stdErrOrig);
372 public void setLogDefault(Level level) {
374 access.setLogLevel(level);
376 System.setOut(stdOutOrig);
379 public boolean loginOnly() {
383 public void addProp(String key, String value) {
384 if(key==null || value==null) {
388 diskprops.setProperty(key, value);
391 public void writeFiles() throws IOException {
393 // Store Creds, if they work
394 if (diskprops != null) {
395 if (!dot_aaf.exists()) {
398 FileOutputStream fos = new FileOutputStream(sso);
400 diskprops.store(fos, "AAF Single Signon");
408 sso.setWritable(true, true);
413 public PropAccess access() {
417 public StringBuilder err() {
421 public String user() {
425 public String enc_pass() {
426 return encrypted_pass;
429 public boolean useX509() {
433 public void close() {
437 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
438 // nothing to do here.
444 private List<String> parseArgs(String[] args)
446 List<String> larg = new ArrayList<String>(args.length);
448 // Cover for bash's need to escape *.. (\\*)
449 // also, remove SSO if required
450 for (int i = 0; i < args.length; ++i) {
451 if ("\\*".equals(args[i])) {
455 if ("-logout".equalsIgnoreCase(args[i])) {
457 } else if ("-login".equalsIgnoreCase(args[i])) {
459 } else if ("-noexit".equalsIgnoreCase(args[i])) {
468 private void setReadonly(File file) {
469 file.setExecutable(false, false);
470 file.setWritable(false, false);
471 file.setReadable(false, false);
472 file.setReadable(true, true);
475 public boolean ok() {