diff --git a/README.md b/README.md index 34e16b57..c482c3a5 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ The shell performs the following Linux like commands: set - set environment variable with arg value in the following format: key=value touch - create empty member if does not exist, arg represents a member or dataset(member) uname - show current connected host name + usermod - modify username or password of current connection, arg can be -u or -p" vi - where arg is a sequential dataset or member name, arg will be downloaded and displayed for editing, use save command to save changes whoami - show current connected user name @@ -150,14 +151,18 @@ You can override the default file name and its location by setting the following The configuration file consists of JSON data. The configuration JSON string is defined as a JSON array structure. The array will consist of one or more profile(s). -A profile is a one-to-one relationship of Profile.java file within the project. It contains variables as a placeholder for configuration information, such as z/OSMF and ssh connection information and properties to control the Window environment. +A profile is a one-to-one relationship of Profile.java file within the project. It contains variables as a placeholder for configuration information, such as z/OSMF and SSH connection information and properties to control the Window environment. -In addition, each profile contains path variable to control location for download directory. - -The first JSON array entry in the example below shows all the attributes defined to be read by the application. +In addition, each profile contains path variable to control location for the download directory. -The other JSON array entries shows that you don't need to specify all attributes and its values. The attributes most critical are those that specify a z/OSMF connection: hostname, zosmfport, username, and password. +The first JSON array entry in the example below shows all the attributes defined to be read by the application. +The other JSON array entries shows that you don't need to specify all attributes and its values. The attributes most critical are those that specify a z/OSMF connection: hostname and zosmfport. + +The username and password entries are optional. It is recommended to not specify those settings. When not specified, the application will prompt end user for username and password for the current connection. + +For further details on username and password usage see [here](https://github.com/Zowe-Java-SDK/ZosShell/issues/178) + Example of config.json: [ @@ -165,8 +170,8 @@ Example of config.json: "hostname": "xxxxxxxxx", "zosmfport": "xxxx", "sshport" : "xxxx", - "username": "xxxxx", - "password": "xxxxxx", + "username": "", + "password": "", "downloadpath": "/ZosShell", "consolename": "", "window": { @@ -180,8 +185,6 @@ Example of config.json: "hostname": "xxxxxxxxx", "zosmfport": "xxxx", "sshport" : "xxxx", - "username": "xxxxx", - "password": "xxxxxx", "downloadpath": "C:\\ZosShell3", "consolename": "", "window": {} @@ -190,8 +193,6 @@ Example of config.json: "hostname": "xxxxxxxxx", "zosmfport": "xxxx", "sshport" : "xxxx", - "username": "xxxxx", - "password": "xxxxxx", "downloadpath": "C:\\ZosShell", "consolename": "", "window": {} diff --git a/src/main/java/zos/shell/ZosShell.java b/src/main/java/zos/shell/ZosShell.java index 596278d0..611378ca 100644 --- a/src/main/java/zos/shell/ZosShell.java +++ b/src/main/java/zos/shell/ZosShell.java @@ -15,6 +15,8 @@ import zos.shell.singleton.configuration.ConfigSingleton; import zos.shell.utility.PromptUtil; import zos.shell.utility.StrUtil; +import zowe.client.sdk.core.SshConnection; +import zowe.client.sdk.core.ZosConnection; import zowe.client.sdk.utility.ValidateUtils; import java.io.IOException; @@ -53,7 +55,7 @@ public static void main(String[] args) { // initialize ConnSingleton object var connSingleton = ConnSingleton.getInstance(); - connSingleton.setCurrZosConnection(ConfigSingleton.getInstance().getZosConnectionByIndex(0)); + connSingleton.setCurrZosConnection(ConfigSingleton.getInstance().getZosConnectionByIndex(0), 0); connSingleton.setCurrSshConnection(ConfigSingleton.getInstance().getSshConnectionByIndex(0)); var title = Constants.APP_TITLE + " - " + connSingleton.getCurrSshConnection().getHost().toUpperCase(); TerminalSingleton.getInstance().getMainTerminal().setPaneTitle(title); @@ -86,13 +88,37 @@ public void accept(TextIO textIO, RunnerData runnerData) { // local variables copies from singletons TextTerminal terminal = TerminalSingleton.getInstance().getTerminal(); var currConnection = ConnSingleton.getInstance().getCurrZosConnection(); + var currSshConnection = ConnSingleton.getInstance().getCurrSshConnection(); - // display information on initial first connection defined and set for usage + // setup initial first connection definition and prompt for username and password if applicable. try { - ValidateUtils.checkConnection(currConnection); var host = currConnection.getHost(); - var user = currConnection.getUser(); - terminal.println("Connected to " + host + " as user " + user + "."); + var zosmfport = currConnection.getZosmfPort(); + var username = currConnection.getUser(); + var password = currConnection.getPassword(); + if (host.isBlank() || zosmfport.isBlank() || "0".equals(zosmfport)) { + throw new IllegalStateException("Error: Hostname or z/OSMF port value(s) missing\n" + + "Check configuration file and try again..."); + } + + if (username.isBlank() || password.isBlank()) { + terminal.println("Enter username and password for host " + host); + username = PromptUtil.getPromptInfo("username:", false); + password = PromptUtil.getPromptInfo("password:", true); + + currConnection = new ZosConnection(host, zosmfport, username, password); + + ValidateUtils.checkConnection(currConnection); + ConfigSingleton.getInstance().setZosConnectionByIndex(currConnection, 0); + ConnSingleton.getInstance().setCurrZosConnection(currConnection, 0); + currSshConnection = new SshConnection(host, currSshConnection.getPort(), username, password); + ConfigSingleton.getInstance().setSshConnectionByIndex(currSshConnection, 0); + ConnSingleton.getInstance().setCurrSshConnection(currSshConnection); + } + var msg = String.format("Connection defined:\nhost:%s\nuser:%s\nzosmfport:%s\nsshport:%s", + currConnection.getHost(), currConnection.getUser(), currConnection.getZosmfPort(), + currSshConnection.getPort()); + terminal.println(msg); } catch (Exception e) { var mainTerminal = TerminalSingleton.getInstance().getMainTerminal(); mainTerminal.println("ERROR: Default connection invalid, try again..."); diff --git a/src/main/java/zos/shell/command/CommandRouter.java b/src/main/java/zos/shell/command/CommandRouter.java index d9ffba9f..2305f677 100644 --- a/src/main/java/zos/shell/command/CommandRouter.java +++ b/src/main/java/zos/shell/command/CommandRouter.java @@ -122,13 +122,25 @@ public void routeCommand(String[] params) { if (isParamsExceeded(2, params)) { return; } + int changeIndex; + try { + changeIndex = Integer.parseInt(params[1]); + } catch (NumberFormatException e) { + terminal.println(Constants.INVALID_COMMAND); + return; + } + var previousCurrConnection = currConnection; var changeConnController = controllerContainer.getChangeConnController(terminal); currConnection = changeConnController.changeZosConnection(currConnection, params); - ConnSingleton.getInstance().setCurrZosConnection(currConnection); + ConnSingleton.getInstance().setCurrZosConnection(currConnection, changeIndex); currSshConnection = changeConnController.changeSshConnection(currSshConnection, params); ConnSingleton.getInstance().setCurrSshConnection(currSshConnection); - var msg = "Connected to " + currConnection.getHost() + " as user " + currConnection.getUser() + "."; - terminal.println(msg); + if (previousCurrConnection != currConnection) { + var msg = String.format("Connection changed:\nhost:%s\nuser:%s\nzosmfport:%s\nsshport:%s", + currConnection.getHost(), currConnection.getUser(), currConnection.getZosmfPort(), + currSshConnection.getPort()); + terminal.println(msg); + } TerminalSingleton.getInstance().getMainTerminal() .setPaneTitle(Constants.APP_TITLE + " - " + currConnection.getHost().toUpperCase()); break; @@ -633,6 +645,19 @@ public void routeCommand(String[] params) { String unameResult = unameController.uname(currConnection); terminal.println(unameResult); break; + case "usermod": + if (isParamsMissing(1, params)) { + return; + } + if (isParamsExceeded(2, params)) { + return; + } + var usermodController = controllerContainer.getUsermodController(currConnection, + ConnSingleton.getInstance().getCurrZosConnectionIndex()); + var flag = params[1]; + String usermodResult = usermodController.change(flag); + terminal.println(usermodResult); + break; case "ussh": if (isParamsMissing(1, params)) { return; diff --git a/src/main/java/zos/shell/controller/UsermodController.java b/src/main/java/zos/shell/controller/UsermodController.java new file mode 100644 index 00000000..e5103393 --- /dev/null +++ b/src/main/java/zos/shell/controller/UsermodController.java @@ -0,0 +1,31 @@ +package zos.shell.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import zos.shell.constants.Constants; +import zos.shell.service.usermod.UsermodService; + +public class UsermodController { + + private static final Logger LOG = LoggerFactory.getLogger(UsermodController.class); + + private final UsermodService usermodService; + + public UsermodController(final UsermodService usermodService) { + LOG.debug("*** UsermodController ***"); + this.usermodService = usermodService; + } + + public String change(final String flag) { + LOG.debug("*** change ***"); + if ("-u".equalsIgnoreCase(flag)) { + return usermodService.changeUsername(); + } + if ("-p".equalsIgnoreCase(flag)) { + return usermodService.changePassword(); + } else { + return Constants.INVALID_COMMAND; + } + } + +} diff --git a/src/main/java/zos/shell/controller/container/ControllerFactoryContainer.java b/src/main/java/zos/shell/controller/container/ControllerFactoryContainer.java index 1997f5bd..afde3532 100644 --- a/src/main/java/zos/shell/controller/container/ControllerFactoryContainer.java +++ b/src/main/java/zos/shell/controller/container/ControllerFactoryContainer.java @@ -34,6 +34,7 @@ import zos.shell.service.rename.RenameService; import zos.shell.service.search.SearchCacheService; import zos.shell.service.tso.TsoService; +import zos.shell.service.usermod.UsermodService; import zowe.client.sdk.core.SshConnection; import zowe.client.sdk.core.ZosConnection; import zowe.client.sdk.zosconsole.method.IssueConsole; @@ -503,6 +504,12 @@ public UnameController getUnameController(final ZosConnection connection, final return this.unameController; } + public UsermodController getUsermodController(final ZosConnection connection, final int index) { + LOG.debug("*** getUsermodController ***"); + var usermodService = new UsermodService(connection, index); + return new UsermodController(usermodService); + } + public UssController getUssController(final SshConnection connection) { LOG.debug("*** getUssController ***"); if (this.ussController == null || diff --git a/src/main/java/zos/shell/service/autocomplete/SearchCommandService.java b/src/main/java/zos/shell/service/autocomplete/SearchCommandService.java index ef3f754d..d43a5a57 100644 --- a/src/main/java/zos/shell/service/autocomplete/SearchCommandService.java +++ b/src/main/java/zos/shell/service/autocomplete/SearchCommandService.java @@ -12,8 +12,8 @@ public final class SearchCommandService { private static final String[] commands = new String[]{"browsejob", "cancel", "cat", "cd", "change", "clear", "color", "connections", "count", "copy", "download", "downloadjob", "end", "env", "files", "grep", "help", "history", "hostname", "ls", "mkdir", "mvs", "purge", "ps", "pwd", "rename", "rm", "rn", "save", - "search", "set", "stop", "submit", "tail", "timeout", "touch", "tso", "uname", "ussh", "vi", "visited", - "whoami"}; + "search", "set", "stop", "submit", "tail", "timeout", "touch", "tso", "uname", "usermod", "ussh", "vi", + "visited", "whoami"}; private final TriePreFix dictionary = new TriePreFix(commands); diff --git a/src/main/java/zos/shell/service/change/ChangeConnService.java b/src/main/java/zos/shell/service/change/ChangeConnService.java index 6b92ebb2..a560eace 100644 --- a/src/main/java/zos/shell/service/change/ChangeConnService.java +++ b/src/main/java/zos/shell/service/change/ChangeConnService.java @@ -6,6 +6,7 @@ import zos.shell.constants.Constants; import zos.shell.singleton.configuration.ConfigSingleton; import zos.shell.singleton.configuration.record.ConfigSettings; +import zos.shell.utility.PromptUtil; import zowe.client.sdk.core.SshConnection; import zowe.client.sdk.core.ZosConnection; @@ -33,7 +34,29 @@ public ZosConnection changeZosConnection(final ZosConnection zosConnection, fina var profile = configSingleton.getProfileByIndex(index); ConfigSingleton.getInstance().setConfigSettings(new ConfigSettings(profile.getDownloadpath(), profile.getConsolename(), profile.getWindow())); + + var zosConnectionByIndex = configSingleton.getZosConnectionByIndex(index); + var host = zosConnectionByIndex.getHost(); + var username = zosConnectionByIndex.getUser(); + var password = zosConnectionByIndex.getPassword(); + var zosmfport = zosConnectionByIndex.getZosmfPort(); + + if (host.isBlank() || zosmfport.isBlank()) { + terminal.println("Error: Hostname or z/OSMF port value(s) missing"); + terminal.println("Check configuration file and try again..."); + return zosConnection; + } + + if (!username.isBlank() && !password.isBlank()) { + ConfigSingleton.getInstance().updateWindowSittings(terminal); + return zosConnectionByIndex; + } + + terminal.println("Enter username and password for host " + host); + username = PromptUtil.getPromptInfo("username:", false); + password = PromptUtil.getPromptInfo("password:", true); ConfigSingleton.getInstance().updateWindowSittings(terminal); + configSingleton.setZosConnectionByIndex(new ZosConnection(host, zosmfport, username, password), index); return configSingleton.getZosConnectionByIndex(index); } @@ -43,14 +66,26 @@ public SshConnection changeSshConnection(final SshConnection sshConnection, fina if (index-- > configSingleton.getZosConnections().size()) { return sshConnection; } + + var zosConnectionByIndex = configSingleton.getZosConnectionByIndex(index); + var zosUsername = zosConnectionByIndex.getUser(); + var zosPassword = zosConnectionByIndex.getPassword(); + + var sshConnectionByIndex = configSingleton.getSshConnectionByIndex(index); + var sshHost = sshConnectionByIndex.getHost(); + var sshPort = sshConnectionByIndex.getPort(); + + configSingleton.setSshConnectionByIndex(new SshConnection(sshHost, sshPort, zosUsername, zosPassword), index); return configSingleton.getSshConnectionByIndex(index); } public void displayConnections() { LOG.debug("*** displayConnections ***"); var i = new AtomicInteger(1); - configSingleton.getZosConnections().forEach(c -> terminal.println(i.getAndIncrement() + " " + "hostname: " + - c.getHost() + ", port: " + c.getZosmfPort() + ", user: " + c.getUser())); + configSingleton.getZosConnections().forEach( + c -> terminal.println(i.getAndIncrement() + " " + "hostname: " + + (c.getHost().isBlank() ? "n\\a" : c.getHost()) + ", zosmfport: " + + (c.getZosmfPort().isBlank() ? "n\\a" : c.getZosmfPort()))); if (configSingleton.getZosConnections().isEmpty()) { terminal.println(Constants.NO_CONNECTION_INFO); } diff --git a/src/main/java/zos/shell/service/help/HelpService.java b/src/main/java/zos/shell/service/help/HelpService.java index 87f44e13..6d7e5db6 100644 --- a/src/main/java/zos/shell/service/help/HelpService.java +++ b/src/main/java/zos/shell/service/help/HelpService.java @@ -67,6 +67,7 @@ public class HelpService { "touch - create empty member if does not exist, arg represents a member or dataset(member)", "tso - execute a tso command where arg is a command string within double quotes", "uname - show current connected host name", + "usermod - modify username or password of current connection, arg can be -u or -p", "ussh - execute USS/UNIX command via SSH connection where arg is a command string within double quotes", "vi - where arg is a sequential dataset or member name, arg will be downloaded", " and displayed for editing, use save command to save changes", diff --git a/src/main/java/zos/shell/service/usermod/UsermodService.java b/src/main/java/zos/shell/service/usermod/UsermodService.java new file mode 100644 index 00000000..11472a57 --- /dev/null +++ b/src/main/java/zos/shell/service/usermod/UsermodService.java @@ -0,0 +1,57 @@ +package zos.shell.service.usermod; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import zos.shell.singleton.ConnSingleton; +import zos.shell.singleton.configuration.ConfigSingleton; +import zos.shell.utility.PromptUtil; +import zowe.client.sdk.core.SshConnection; +import zowe.client.sdk.core.ZosConnection; + +public class UsermodService { + + private static final Logger LOG = LoggerFactory.getLogger(UsermodService.class); + + private final String host; + private final String zosmfPort; + private final String username; + private final String password; + private final int sshPort; + + private final int index; + + public UsermodService(final ZosConnection connection, final int index) { + LOG.debug("*** UsermodService ***"); + this.host = connection.getHost(); + this.zosmfPort = connection.getZosmfPort(); + this.username = connection.getUser(); + this.password = connection.getPassword(); + this.sshPort = ConfigSingleton.getInstance().getSshConnectionByIndex(index).getPort(); + this.index = index; + } + + public String changePassword() { + LOG.debug("*** changePassword ***"); + var password = PromptUtil.getPromptInfo("password:", true); + var zosConnection = new ZosConnection(this.host, this.zosmfPort, this.username, password); + var sshConnection = new SshConnection(this.host, this.sshPort, this.username, password); + ConfigSingleton.getInstance().setZosConnectionByIndex(zosConnection, index); + ConnSingleton.getInstance().setCurrZosConnection(zosConnection, index); + ConfigSingleton.getInstance().setSshConnectionByIndex(sshConnection, index); + ConnSingleton.getInstance().setCurrSshConnection(sshConnection); + return "password changed"; + } + + public String changeUsername() { + LOG.debug("*** changeUsername ***"); + var username = PromptUtil.getPromptInfo("username:", false); + var zosConnection = new ZosConnection(this.host, this.zosmfPort, username, this.password); + var sshConnection = new SshConnection(this.host, this.sshPort, username, this.password); + ConfigSingleton.getInstance().setZosConnectionByIndex(zosConnection, index); + ConnSingleton.getInstance().setCurrZosConnection(zosConnection, index); + ConfigSingleton.getInstance().setSshConnectionByIndex(sshConnection, index); + ConnSingleton.getInstance().setCurrSshConnection(sshConnection); + return "username changed"; + } + +} diff --git a/src/main/java/zos/shell/singleton/ConnSingleton.java b/src/main/java/zos/shell/singleton/ConnSingleton.java index 4f1f61d9..5bc93e75 100644 --- a/src/main/java/zos/shell/singleton/ConnSingleton.java +++ b/src/main/java/zos/shell/singleton/ConnSingleton.java @@ -11,6 +11,7 @@ public class ConnSingleton { private ZosConnection currZosConnection; private SshConnection currSshConnection; + private int connectionIndex; private static class Holder { private static final ConnSingleton instance = new ConnSingleton(); @@ -30,9 +31,10 @@ public ZosConnection getCurrZosConnection() { return currZosConnection; } - public void setCurrZosConnection(final ZosConnection currZosConnection) { + public void setCurrZosConnection(final ZosConnection currZosConnection, int index) { LOG.debug("*** setCurrZosConnection ***"); this.currZosConnection = currZosConnection; + this.connectionIndex = index; } public SshConnection getCurrSshConnection() { @@ -45,4 +47,9 @@ public void setCurrSshConnection(final SshConnection currSshConnection) { this.currSshConnection = currSshConnection; } + public int getCurrZosConnectionIndex() { + LOG.debug("*** getCurrZosConnectionIndex ***"); + return connectionIndex; + } + } diff --git a/src/main/java/zos/shell/singleton/configuration/ConfigSingleton.java b/src/main/java/zos/shell/singleton/configuration/ConfigSingleton.java index b5aacf81..edcbe4f0 100644 --- a/src/main/java/zos/shell/singleton/configuration/ConfigSingleton.java +++ b/src/main/java/zos/shell/singleton/configuration/ConfigSingleton.java @@ -92,13 +92,23 @@ private void initialConfigSettings() { private void createZosConnections() { LOG.debug("*** createZosConnections ***"); profiles.forEach(profile -> zosConnections.add(new ZosConnection(profile.getHostname(), - profile.getZosmfport(), profile.getUsername(), profile.getPassword()))); + profile.getZosmfport() != null ? profile.getZosmfport() : "0", + profile.getUsername() != null ? profile.getUsername() : "", + profile.getPassword() != null ? profile.getPassword() : ""))); } private void createSshConnections() { LOG.debug("*** createSshConnections ***"); - profiles.forEach(profile -> shhConnections.add(new SshConnection(profile.getHostname(), - Integer.parseInt(profile.getSshport()), profile.getUsername(), profile.getPassword()))); + profiles.forEach(profile -> { + int sshport = 0; + try { + sshport = Integer.parseInt(profile.getSshport()); + } catch (NumberFormatException ignored) { + } + shhConnections.add(new SshConnection(profile.getHostname(), + sshport, profile.getUsername() != null ? profile.getUsername() : "", + profile.getPassword() != null ? profile.getPassword() : "")); + }); } public void updateWindowSittings(final TextTerminal terminal) { @@ -145,6 +155,10 @@ public SshConnection getSshConnectionByIndex(int index) { return shhConnections.get(index); } + public void setSshConnectionByIndex(final SshConnection sshConnection, final int index) { + this.shhConnections.set(index, sshConnection); + } + public ZosConnection getZosConnectionByIndex(int index) { LOG.debug("*** getZosConnectionByIndex ***"); if (zosConnections.isEmpty()) { @@ -153,6 +167,10 @@ public ZosConnection getZosConnectionByIndex(int index) { return zosConnections.get(index); } + public void setZosConnectionByIndex(final ZosConnection zosConnection, final int index) { + this.zosConnections.set(index, zosConnection); + } + public ConfigSettings getConfigSettings() { LOG.debug("*** getConfigSettings ***"); return configSettings; diff --git a/src/main/java/zos/shell/utility/PromptUtil.java b/src/main/java/zos/shell/utility/PromptUtil.java index ddc7b175..a33198e0 100644 --- a/src/main/java/zos/shell/utility/PromptUtil.java +++ b/src/main/java/zos/shell/utility/PromptUtil.java @@ -3,6 +3,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import zos.shell.constants.Constants; +import zos.shell.singleton.TerminalSingleton; public final class PromptUtil { @@ -18,4 +19,17 @@ public static String getPrompt() { return Constants.DEFAULT_PROMPT; } + public static String getPromptInfo(final String promptMsg, final boolean isMask) { + LOG.debug("*** getPrompt ***"); + TerminalSingleton.getInstance().setDisableKeys(true); + var result = TerminalSingleton.getInstance() + .getMainTextIO() + .newStringInputReader() + .withMaxLength(80) + .withInputMasking(isMask) + .read(promptMsg); + TerminalSingleton.getInstance().setDisableKeys(false); + return result; + } + }