2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2018 IBM.
8 * ===========================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END====================================================
24 package org.onap.aaf.auth.cmd;
26 import java.io.BufferedReader;
28 import java.io.FileReader;
29 import java.io.InputStreamReader;
30 import java.io.OutputStreamWriter;
31 import java.io.PrintWriter;
32 import java.io.Reader;
33 import java.io.Writer;
34 import java.net.HttpURLConnection;
35 import java.util.ArrayList;
36 import java.util.List;
38 import org.onap.aaf.auth.cmd.mgmt.Mgmt;
39 import org.onap.aaf.auth.cmd.ns.NS;
40 import org.onap.aaf.auth.cmd.perm.Perm;
41 import org.onap.aaf.auth.cmd.role.Role;
42 import org.onap.aaf.auth.cmd.user.User;
43 import org.onap.aaf.auth.common.Define;
44 import org.onap.aaf.auth.env.AuthzEnv;
45 import org.onap.aaf.cadi.Access;
46 import org.onap.aaf.cadi.Access.Level;
47 import org.onap.aaf.cadi.CadiException;
48 import org.onap.aaf.cadi.PropAccess;
49 import org.onap.aaf.cadi.SecuritySetter;
50 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
51 import org.onap.aaf.cadi.client.Retryable;
52 import org.onap.aaf.cadi.config.Config;
53 import org.onap.aaf.cadi.config.SecurityInfoC;
54 import org.onap.aaf.cadi.http.HBasicAuthSS;
55 import org.onap.aaf.cadi.http.HMangr;
56 import org.onap.aaf.cadi.sso.AAFSSO;
57 import org.onap.aaf.misc.env.APIException;
59 import jline.console.ConsoleReader;
62 protected static PrintWriter pw;
63 protected HMangr hman;
64 // Storage for last reused client. We can do this
65 // because we're technically "single" threaded calls.
66 public Retryable<?> prevCall;
68 protected SecuritySetter<HttpURLConnection> ss;
69 // protected AuthzEnv env;
70 private boolean close;
71 private List<Cmd> cmds;
74 private ArrayList<Integer> expect = new ArrayList<>();
75 private boolean verbose = true;
77 private SecurityInfoC<HttpURLConnection> si;
78 private boolean request = false;
79 private String force = null;
80 private boolean gui = false;
85 private static int TIMEOUT = Integer.parseInt(Config.AAF_CONN_TIMEOUT_DEF);
86 private static boolean isConsole = false;
87 private static boolean isTest = false;
88 private static boolean showDetails = false;
89 private static boolean ignoreDelay = false;
90 private static int globalDelay=0;
92 // Create when only have Access
93 public AAFcli(Access access, Writer wtr, HMangr hman, SecurityInfoC<HttpURLConnection> si, SecuritySetter<HttpURLConnection> ss) throws APIException, CadiException {
94 this(access,new AuthzEnv(access.getProperties()),wtr,hman, si,ss);
97 public AAFcli(Access access, AuthzEnv env, Writer wtr, HMangr hman, SecurityInfoC<HttpURLConnection> si, SecuritySetter<HttpURLConnection> ss) throws APIException {
103 if (wtr instanceof PrintWriter) {
104 pw = (PrintWriter) wtr;
107 pw = new PrintWriter(wtr);
114 cmds = new ArrayList<>();
116 Role role = new Role(this);
117 cmds.add(new Help(this, cmds));
118 cmds.add(new Version(this));
119 cmds.add(new Perm(role));
121 cmds.add(new User(this));
122 cmds.add(new NS(this));
123 cmds.add(new Mgmt(this));
126 public AuthzEnv env() {
130 public static int timeout() {
134 public void verbose(boolean v) {
138 public void close() {
139 // if (hman != null) {
148 public boolean eval(String line) throws Exception {
149 if (line.length() == 0) {
151 } else if (line.startsWith("#")) {
156 String[] largs = argEval(line);
159 // Variable replacement
160 StringBuilder sb = null;
161 while (idx < largs.length) {
163 for (int v = largs[idx].indexOf("@["); v >= 0; v = largs[idx].indexOf("@[", v + 1)) {
165 sb = new StringBuilder();
167 sb.append(largs[idx], e, v);
168 if ((e = largs[idx].indexOf(']', v)) >= 0) {
169 String p = access.getProperty(largs[idx].substring(v + 2, e),null);
171 p = System.getProperty(largs[idx].substring(v+2,e));
179 if (sb != null && sb.length() > 0) {
180 sb.append(largs[idx], e, largs[idx].length());
181 largs[idx] = sb.toString();
189 while (rv && idx < largs.length) {
190 // Allow Script to change Credential
192 if ("as".equalsIgnoreCase(largs[idx])) {
193 if (largs.length > ++idx) {
194 // get Password from Props with ID as Key
195 String user = largs[idx++];
196 int colon = user.indexOf(':');
199 pass = user.substring(colon + 1);
200 user = user.substring(0, colon);
202 pass = access.getProperty(user, null);
205 pass = access.decrypt(pass, false);
206 access.getProperties().put(user, pass);
207 ss=new HBasicAuthSS(si, user, pass);
208 pw.println("as " + user);
209 } else { // get Pass from System Properties, under name of
211 pw.println("ERROR: No password set for " + user);
216 } else if ("expect".equalsIgnoreCase(largs[idx])) {
218 if (largs.length > idx++) {
219 if (!"nothing".equals(largs[idx])) {
220 for (String str : largs[idx].split(",")) {
222 if ("Exception".equalsIgnoreCase(str)) {
225 expect.add(Integer.parseInt(str));
227 } catch (NumberFormatException e) {
228 throw new CadiException("\"expect\" should be followed by Number");
235 // Sleep, typically for reports, to allow DB to update
238 } else if ("sleep".equalsIgnoreCase(largs[idx])) {
239 Integer t = Integer.parseInt(largs[++idx]);
240 pw.println("sleep " + t);
244 } else if ("delay".equalsIgnoreCase(largs[idx])) {
245 delay = Integer.parseInt(largs[++idx]);
246 pw.println("delay " + delay);
249 } else if ("pause".equalsIgnoreCase(largs[idx])) {
250 pw.println("Press <Return> to continue...");
252 // Sonar insists we do something with the string, though it's only a pause. Not very helpful...
253 String sonar = new BufferedReader(new InputStreamReader(System.in)).readLine();
254 sonar=""; // this useless code brought to you by Sonar.
257 } else if ("exit".equalsIgnoreCase(largs[idx])) {
258 pw.println("Exiting...");
260 } else if ("set".equalsIgnoreCase(largs[idx])) {
261 while (largs.length > ++idx) {
262 int equals = largs[idx].indexOf('=');
266 value = access.getProperty(Config.AAF_APPPASS,null);
270 value = access.decrypt(value, false);
274 access.getProperties().put(tag, value);
275 pw.println("set " + tag + " <encrypted>");
278 tag = largs[idx].substring(0, equals);
279 value = largs[idx].substring(++equals);
280 pw.println("set " + tag + ' ' + value);
282 boolean isTrue = "TRUE".equalsIgnoreCase(value);
283 if ("FORCE".equalsIgnoreCase(tag)) {
285 } else if ("REQUEST".equalsIgnoreCase(tag)) {
287 } else if ("DETAILS".equalsIgnoreCase(tag)) {
288 showDetails = isTrue;
290 access.getProperties().put(tag, value);
294 // Allow Script to indicate if Failure is what is expected
299 if ("REQUEST".equalsIgnoreCase(largs[idx])) {
302 } else if ("FORCE".equalsIgnoreCase(largs[idx])) {
305 } else if ("DETAILS".equalsIgnoreCase(largs[idx])) {
312 if (largs[idx].equalsIgnoreCase(c.getName())) {
315 if (expect.size() > 0) {
316 pw.print("** Expect ");
317 boolean first = true;
318 for (Integer i : expect) {
330 ret = c.exec(++idx, largs);
331 if (delay+globalDelay > 0) {
332 Thread.sleep((long)(delay+globalDelay));
334 } catch (Exception e) {
335 if (expect.contains(-1)) {
336 pw.println(e.getMessage());
342 clearSingleLineProperties();
344 rv = expect.isEmpty() || expect.contains(ret);
349 pw.print("!!! Unexpected Return Code: ");
351 pw.println(", VALIDATE OUTPUT!!!");
357 pw.write("Unknown Instruction \"");
358 pw.write(largs[idx]);
360 idx = largs.length;// always end after one command
365 private String[] argEval(String line) {
366 StringBuilder sb = new StringBuilder();
367 ArrayList<String> arr = new ArrayList<>();
368 boolean start = true;
371 for (int i = 0; i < line.length(); ++i) {
373 if (Character.isWhitespace(ch = line.charAt(i))) {
374 if (start || last==',') {
376 } else if (quote != 0) {
379 arr.add(sb.toString());
383 } else if (ch == '\'' || ch == '"') { // toggle
389 } else if (ch=='|' && quote==0) {
390 arr.add(sb.toString());
399 if (sb.length() > 0) {
400 arr.add(sb.toString());
403 String[] rv = new String[arr.size()];
408 public static void keyboardHelp() {
409 System.out.println("'C-' means hold the ctrl key down while pressing the next key.");
410 System.out.println("'M-' means hold the alt key down while pressing the next key.");
411 System.out.println("For instance, C-b means hold ctrl key and press b, M-b means hold alt and press b\n");
413 System.out.println("Basic Keybindings:");
414 System.out.println("\tC-l - clear screen");
415 System.out.println("\tC-a - beginning of line");
416 System.out.println("\tC-e - end of line");
417 System.out.println("\tC-b - backward character (left arrow also works)");
418 System.out.println("\tM-b - backward word");
419 System.out.println("\tC-f - forward character (right arrow also works)");
420 System.out.println("\tM-f - forward word");
421 System.out.println("\tC-d - delete character under cursor");
422 System.out.println("\tM-d - delete word forward");
423 System.out.println("\tM-backspace - delete word backward");
424 System.out.println("\tC-k - delete from cursor to end of line");
425 System.out.println("\tC-u - delete entire line, regardless of cursor position\n");
427 System.out.println("Command History:");
428 System.out.println("\tC-r - search backward in history (repeating C-r continues the search)");
429 System.out.println("\tC-p - move backwards through history (up arrow also works)");
430 System.out.println("\tC-n - move forwards through history (down arrow also works)\n");
437 public static void main(String[] args) {
441 AAFSSO aafsso = new AAFSSO(args);
442 String noexit = aafsso.access().getProperty("no_exit");
444 PropAccess access = aafsso.access();
448 AuthzEnv env = new AuthzEnv(access);
451 boolean exitOnFailure = true;
453 * Check for "-" options anywhere in command line
455 StringBuilder sb = new StringBuilder();
456 for (int i = 0; i < args.length; ++i) {
457 if ("-i".equalsIgnoreCase(args[i])) {
458 rdr = new InputStreamReader(System.in);
459 // } else if ("-o".equalsIgnoreCase(args[i])) {
460 // // shall we do something different? Output stream is
462 } else if ("-f".equalsIgnoreCase(args[i])) {
463 if (args.length > i + 1) {
464 rdr = new FileReader(args[++i]);
466 } else if ("-a".equalsIgnoreCase(args[i])) {
467 exitOnFailure = false;
468 } else if ("-c".equalsIgnoreCase(args[i])) {
470 } else if ("-s".equalsIgnoreCase(args[i]) && args.length > i + 1) {
471 access.setProperty(Cmd.STARTDATE, args[++i]);
472 } else if ("-e".equalsIgnoreCase(args[i]) && args.length > i + 1) {
473 access.setProperty(Cmd.ENDDATE, args[++i]);
474 } else if ("-t".equalsIgnoreCase(args[i])) {
476 } else if ("-d".equalsIgnoreCase(args[i])) {
478 } else if ("-n".equalsIgnoreCase(args[i])) {
481 if (sb.length() > 0) {
488 AAFConHttp aafcon = new AAFConHttp(access);
490 // SecurityInfoC<?> si = aafcon.securityInfo();
493 aafsso.setLogDefault();
494 aafsso.setStdErrDefault();
496 // Note, with AAF Locator, this may not longer be necessary 3/2018 Jonathan
497 if (!aafsso.loginOnly()) {
499 // loc = new AAFLocator(si,new URI(access.getProperty(Config.AAF_URL)));
500 // } catch (Throwable t) {
501 // aafsso.setStdErrDefault();
504 // // Other Access is done writing to StdOut and StdErr, reset Std out
505 // aafsso.setLogDefault();
508 TIMEOUT = Integer.parseInt(access.getProperty(Config.AAF_CONN_TIMEOUT, Config.AAF_CONN_TIMEOUT_DEF));
509 // HMangr hman = new HMangr(access, loc).readTimeout(TIMEOUT).apiVersion(Config.AAF_DEFAULT_API_VERSION);
511 if (access.getProperty(Config.AAF_DEFAULT_REALM)==null) {
512 access.setProperty(Config.AAF_DEFAULT_REALM, "people.osaaf.org");
513 aafsso.addProp(Config.AAF_DEFAULT_REALM, "people.osaaf.org");
516 AAFcli aafcli = new AAFcli(access,env, new OutputStreamWriter(System.out),
517 aafcon.hman(), aafcon.securityInfo(), aafcon.securityInfo().defSS);
518 // new HBasicAuthSS(si,aafsso.user(), access.decrypt(aafsso.enc_pass(),false)));
521 File delay = new File("aafcli.delay");
522 if (delay.exists()) {
523 BufferedReader br = new BufferedReader(new FileReader(delay));
525 globalDelay = Integer.parseInt(br.readLine());
526 } catch (Exception e) {
527 access.log(Level.DEBUG,e);
535 System.out.println("Type 'help' for short help or 'help -d' for detailed help with aafcli commands");
536 System.out.println("Type '?' for help with command line editing");
537 System.out.println("Type 'q', 'quit', or 'exit' to quit aafcli\n");
539 ConsoleReader reader = new ConsoleReader();
541 reader.setPrompt("aafcli > ");
544 while ((line = reader.readLine()) != null) {
545 showDetails = (line.contains("-d"));
547 if (line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("q") || line.equalsIgnoreCase("exit")) {
549 } else if (line.equalsIgnoreCase("--help -d") || line.equalsIgnoreCase("help -d")
550 || line.equalsIgnoreCase("help")) {
552 } else if (line.equalsIgnoreCase("cls")) {
553 reader.clearScreen();
555 } else if (line.equalsIgnoreCase("?")) {
562 } catch (Exception e) {
563 pw.println(e.getMessage());
570 } else if (rdr != null) {
571 BufferedReader br = new BufferedReader(rdr);
573 while ((line = br.readLine()) != null) {
574 if (!aafcli.eval(line) && exitOnFailure) {
579 } else { // just run the command line
580 aafcli.verbose(false);
581 if (sb.length() == 0) {
584 rv = aafcli.eval(sb.toString()) ? 0 : 1;
590 // Don't close if No Reader, or it's a Reader of Standard In
591 if (rdr != null && !(rdr instanceof InputStreamReader)) {
599 StringBuilder err = aafsso.err();
601 err.append("to continue...");
602 System.err.println(err);
610 } catch (MessageException e) {
611 System.out.println("MessageException caught");
613 System.err.println(e.getMessage());
614 } catch (Exception e) {
615 e.printStackTrace(System.err);
620 public boolean isTest() {
621 return AAFcli.isTest;
624 public boolean isDetailed() {
625 return AAFcli.showDetails;
628 public String typeString(Class<?> cls, boolean json) {
629 return "application/" + cls.getSimpleName() + "+" + (json ? "json" : "xml");//+ ";version=" + hman.apiVersion();
632 public String forceString() {
636 public boolean addRequest() {
640 public void clearSingleLineProperties() {
646 public void gui(boolean b) {