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.configure.ArtifactDir;
44 import org.onap.aaf.cadi.util.MyConsole;
45 import org.onap.aaf.cadi.util.SubStandardConsole;
46 import org.onap.aaf.cadi.util.TheConsole;
49 public static final MyConsole cons = TheConsole.implemented() ? new TheConsole() : new SubStandardConsole();
50 // private static final int EIGHT_HOURS = 8 * 60 * 60 * 1000;
52 private Properties diskprops;
53 private boolean touchDiskprops;
54 private File dot_aaf = null;
55 private File sso = null; // instantiated, if ever, with diskprops
57 boolean removeSSO = false;
58 boolean loginOnly = false;
59 boolean doExit = true;
60 private PropAccess access;
61 private StringBuilder err;
63 private String encrypted_pass;
64 private boolean use_X509;
66 private PrintStream os;
69 private final PrintStream stdOutOrig;
70 private final PrintStream stdErrOrig;
73 public AAFSSO(String[] args) throws IOException, CadiException {
74 this(args,new Properties());
77 public AAFSSO(String[] args, ProcessArgs pa) throws IOException, CadiException {
78 this(args,pa.process(args, new Properties()));
81 public AAFSSO(String[] args, Properties dp) throws IOException, CadiException {
82 stdOutOrig = System.out;
83 stdErrOrig = System.err;
85 List<String> nargs = parseArgs(args);
87 touchDiskprops = false;
89 dot_aaf = new File(System.getProperty("user.home") + "/.aaf");
90 if (!dot_aaf.exists()) {
93 File f = new File(dot_aaf, "sso.out");
94 os = new PrintStream(new FileOutputStream(f, true));
98 sso = new File(dot_aaf, "sso.props");
100 InputStream propStream = new FileInputStream(sso);
102 diskprops.load(propStream);
108 File dot_aaf_kf = new File(dot_aaf, "keyfile");
111 if (dot_aaf_kf.exists()) {
112 dot_aaf_kf.setWritable(true, true);
116 Properties temp = new Properties();
118 for (Entry<Object, Object> es : diskprops.entrySet()) {
119 if (Config.CADI_LATITUDE.equals(es.getKey()) ||
120 Config.CADI_LONGITUDE.equals(es.getKey()) ||
121 Config.AAF_DEFAULT_REALM.equals(es.getKey())) {
122 temp.setProperty(es.getKey().toString(), es.getValue().toString());
126 touchDiskprops = true;
128 String[] naargs = new String[nargs.size()];
129 nargs.toArray(naargs);
130 access = new PropAccess(os, naargs);
133 System.out.println("AAF SSO information removed");
135 // Config.setDefaultRealm(access);
137 if (!dot_aaf_kf.exists()) {
138 // This will create, as required, or reuse
139 ArtifactDir.getSymm(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);
183 diskprops.setProperty(Config.AAF_APPID,appID);
184 if( access.getProperty(Config.AAF_APPPASS)==null) {
185 char[] password = cons.readPassword("Password for %s: ", appID);
186 if(password.length>0) {
187 String app_pass = access.encrypt(new String(password));
188 access.setProperty(Config.AAF_APPPASS,app_pass);
189 diskprops.setProperty(Config.AAF_APPPASS,app_pass);
194 String keystore=access.getProperty(Config.CADI_KEYSTORE);
195 String keystore_pass=access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
197 if (user==null || (alias!=null && (keystore==null || keystore_pass==null))) {
198 String select = null;
200 for (File tsf : dot_aaf.listFiles()) {
201 name = tsf.getName();
202 if (!name.contains("trust") && (name.endsWith(".jks") || name.endsWith(".p12"))) {
204 select = cons.readLine("Use %s for Identity? (y/n): ",tsf.getName());
205 if ("y".equalsIgnoreCase(select)) {
206 keystore = tsf.getCanonicalPath();
207 access.setProperty(Config.CADI_KEYSTORE, keystore);
208 addProp(Config.CADI_KEYSTORE, keystore);
209 char[] password = cons.readPassword("Keystore Password: ");
210 encrypted_pass= access.encrypt(new String(password));
211 access.setProperty(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
212 addProp(Config.CADI_KEYSTORE_PASSWORD, encrypted_pass);
214 // TODO READ Aliases out of Keystore?
215 user = alias = cons.readLine("Keystore alias: ");
216 access.setProperty(Config.CADI_ALIAS, user);
217 addProp(Config.CADI_ALIAS, user);
223 user = appID = cons.readLine(Config.AAF_APPID + ": ");
224 access.setProperty(Config.AAF_APPID, appID);
225 addProp(Config.AAF_APPID, appID);
226 char[] password = cons.readPassword(Config.AAF_APPPASS + ": ");
227 encrypted_pass= access.encrypt(new String(password));
228 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
229 addProp(Config.AAF_APPPASS, encrypted_pass);
232 encrypted_pass = access.getProperty(Config.CADI_KEYSTORE_PASSWORD);
233 if (encrypted_pass == null) {
234 keystore_pass = null;
235 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
237 keystore_pass = encrypted_pass;
246 Symm decryptor = ArtifactDir.getSymm(dot_aaf_kf);
249 String cm_url = access.getProperty(Config.CM_URL); // SSO might overwrite...
250 FileInputStream fos = new FileInputStream(sso);
253 user = access.getProperty(Config.AAF_APPID);
254 encrypted_pass = access.getProperty(Config.AAF_APPPASS);
255 // decrypt with .aaf, and re-encrypt with regular Keyfile
256 access.setProperty(Config.AAF_APPPASS,
257 access.encrypt(decryptor.depass(encrypted_pass)));
258 if (cm_url != null) { //Command line CM_URL Overwrites ssofile.
259 access.setProperty(Config.CM_URL, cm_url);
265 diskprops = new Properties();
266 String realm = Config.getDefaultRealm();
267 // Turn on Console Sysout
268 System.setOut(System.out);
269 user = cons.readLine("aaf_id(%s@%s): ", System.getProperty("user.name"), realm);
271 user = System.getProperty("user.name") + '@' + realm;
272 } else if (user.length() == 0) { //
273 user = System.getProperty("user.name") + '@' + realm;
274 } else if ((user.indexOf('@') < 0) && (realm != null)) {
275 user = user + '@' + realm;
277 access.setProperty(Config.AAF_APPID, user);
278 diskprops.setProperty(Config.AAF_APPID, user);
279 encrypted_pass = new String(cons.readPassword("aaf_password: "));
281 encrypted_pass = Symm.ENC + decryptor.enpass(encrypted_pass);
282 access.setProperty(Config.AAF_APPPASS, encrypted_pass);
283 diskprops.setProperty(Config.AAF_APPPASS, encrypted_pass);
284 diskprops.setProperty(Config.CADI_KEYFILE, access.getProperty(Config.CADI_KEYFILE));
289 err = new StringBuilder("Add -D" + Config.AAF_APPID + "=<id> ");
292 if (encrypted_pass == null && alias == null) {
294 err = new StringBuilder();
298 err.append("-D" + Config.AAF_APPPASS + "=<passwd> ");
301 String locateUrl = access.getProperty(Config.AAF_LOCATE_URL);
302 if (locateUrl==null) {
303 locateUrl=AAFSSO.cons.readLine("AAF Locator URL=https://");
304 if (locateUrl==null || locateUrl.length()==0) {
305 err = new StringBuilder(Config.AAF_LOCATE_URL);
306 err.append(" is required.");
310 locateUrl="https://"+locateUrl;
312 access.setProperty(Config.AAF_LOCATE_URL, locateUrl);
313 addProp(Config.AAF_LOCATE_URL, locateUrl);
316 final String apiVersion = access.getProperty(Config.AAF_API_VERSION, Config.AAF_DEFAULT_API_VERSION);
317 final String aaf_root_ns = access.getProperty(Config.AAF_ROOT_NS);
319 if(aaf_root_ns==null) {
320 locateRoot=Defaults.AAF_ROOT;
322 locateRoot = Defaults.AAF_LOCATE_CONST + '/' + aaf_root_ns;
324 if(access.getProperty(Config.AAF_URL)==null) {
326 access.setProperty(Config.AAF_URL, locateRoot+".service:"+apiVersion);
328 if(access.getProperty(Config.CM_URL)==null) {
329 access.setProperty(Config.CM_URL, locateRoot+".cm:"+apiVersion);
331 String cadiLatitude = access.getProperty(Config.CADI_LATITUDE);
332 if (cadiLatitude==null) {
333 System.out.println("# If you do not know your Global Coordinates, we suggest bing.com/maps");
334 cadiLatitude=AAFSSO.cons.readLine("cadi_latitude[0.000]=");
335 if (cadiLatitude==null || cadiLatitude.isEmpty()) {
336 cadiLatitude="0.000";
338 access.setProperty(Config.CADI_LATITUDE, cadiLatitude);
339 addProp(Config.CADI_LATITUDE, cadiLatitude);
342 String cadiLongitude = access.getProperty(Config.CADI_LONGITUDE);
343 if (cadiLongitude==null) {
344 cadiLongitude=AAFSSO.cons.readLine("cadi_longitude[0.000]=");
345 if (cadiLongitude==null || cadiLongitude.isEmpty()) {
346 cadiLongitude="0.000";
348 access.setProperty(Config.CADI_LONGITUDE, cadiLongitude);
349 addProp(Config.CADI_LONGITUDE, cadiLongitude);
352 String cadi_truststore = access.getProperty(Config.CADI_TRUSTSTORE);
353 if (cadi_truststore==null) {
356 for (File tsf : dot_aaf.listFiles()) {
357 name = tsf.getName();
358 if (name.contains("trust") &&
359 (name.endsWith(".jks") || name.endsWith(".p12"))) {
360 select = cons.readLine("Use %s for TrustStore? (y/n):",tsf.getName());
361 if ("y".equalsIgnoreCase(select)) {
362 cadi_truststore=tsf.getCanonicalPath();
363 access.setProperty(Config.CADI_TRUSTSTORE, cadi_truststore);
364 addProp(Config.CADI_TRUSTSTORE, cadi_truststore);
370 if (cadi_truststore!=null) {
371 if (cadi_truststore.indexOf(File.separatorChar)<0) {
372 cadi_truststore=dot_aaf.getPath()+File.separator+cadi_truststore;
374 String cadi_truststore_password = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD);
375 if (cadi_truststore_password==null) {
376 cadi_truststore_password=AAFSSO.cons.readLine("cadi_truststore_password[%s]=","changeit");
377 cadi_truststore_password = access.encrypt(cadi_truststore_password);
378 access.setProperty(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
379 addProp(Config.CADI_TRUSTSTORE_PASSWORD, cadi_truststore_password);
387 public void setLogDefault() {
388 this.setLogDefault(PropAccess.DEFAULT);
389 System.setOut(stdOutOrig);
392 public void setStdErrDefault() {
393 access.setLogLevel(PropAccess.DEFAULT);
394 System.setErr(stdErrOrig);
397 public void setLogDefault(Level level) {
399 access.setLogLevel(level);
401 System.setOut(stdOutOrig);
404 public boolean loginOnly() {
408 public void addProp(String key, String value) {
409 if (key==null || value==null) {
413 diskprops.setProperty(key, value);
416 public void writeFiles() throws IOException {
417 if (touchDiskprops) {
418 // Store Creds, if they work
419 if (diskprops != null) {
420 if (!dot_aaf.exists()) {
423 FileOutputStream fos = new FileOutputStream(sso);
425 diskprops.store(fos, "AAF Single Signon");
433 sso.setWritable(true, true);
438 public PropAccess access() {
442 public StringBuilder err() {
446 public String user() {
450 public String enc_pass() {
451 return encrypted_pass;
454 public boolean useX509() {
458 public void close() {
462 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
463 // nothing to do here.
469 private List<String> parseArgs(String[] args)
471 List<String> larg = new ArrayList<>(args.length);
473 // Cover for bash's need to escape *.. (\\*)
474 // also, remove SSO if required
475 for (int i = 0; i < args.length; ++i) {
476 if ("\\*".equals(args[i])) {
480 if ("-logout".equalsIgnoreCase(args[i])) {
482 } else if ("-login".equalsIgnoreCase(args[i])) {
484 } else if ("-noexit".equalsIgnoreCase(args[i])) {
493 private void setReadonly(File file) {
494 file.setExecutable(false, false);
495 file.setWritable(false, false);
496 file.setReadable(false, false);
497 file.setReadable(true, true);
500 public boolean ok() {
504 public static interface ProcessArgs {
505 public Properties process(final String[] args, final Properties props);