From 2da8f5f1ac26805183b7dec2847999f158bd7efd Mon Sep 17 00:00:00 2001 From: Tom Quarendon Date: Fri, 6 Oct 2017 07:39:12 +0100 Subject: [PATCH] [KARAF-5411] Allow for users.properties file not existing, or empty --- .../org/apache/karaf/client/ClientConfig.java | 50 +++++++++++-------- .../java/org/apache/karaf/client/Main.java | 20 ++++++-- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/client/src/main/java/org/apache/karaf/client/ClientConfig.java b/client/src/main/java/org/apache/karaf/client/ClientConfig.java index 4d06fd0baa0..e071aae86b8 100644 --- a/client/src/main/java/org/apache/karaf/client/ClientConfig.java +++ b/client/src/main/java/org/apache/karaf/client/ClientConfig.java @@ -183,29 +183,31 @@ public ClientConfig(String[] args) throws IOException { command = commandBuilder.toString(); Map usersCfg = new LinkedHashMap<>(); - loadProps(new File(System.getProperty("karaf.etc") + "/users.properties"), usersCfg); - if (!usersCfg.isEmpty()) { - Set users = new LinkedHashSet<>(); - for (String user : usersCfg.keySet()) { - if (!user.startsWith(GROUP_PREFIX)) { - users.add(user); - } - } - if (user == null) { - if (users.iterator().hasNext()) { - user = users.iterator().next(); - } - } - if (interactiveMode && !inputPassword) { - password = null; - } else if (!inputPassword) { - password = usersCfg.get(user); - if (password != null && password.contains(ROLE_DELIMITER)) { - password = password.substring(0, password.indexOf(ROLE_DELIMITER)); - } - } + File userPropertiesFile = new File(System.getProperty("karaf.etc") + "/users.properties"); + if (userPropertiesFile.exists()) { + loadProps(userPropertiesFile, usersCfg); + if (!usersCfg.isEmpty()) { + Set users = new LinkedHashSet<>(); + for (String user : usersCfg.keySet()) { + if (!user.startsWith(GROUP_PREFIX)) { + users.add(user); + } + } + if (user == null) { + if (users.iterator().hasNext()) { + user = users.iterator().next(); + } + } + if (interactiveMode && !inputPassword) { + password = null; + } else if (!inputPassword) { + password = usersCfg.get(user); + if (password != null && password.contains(ROLE_DELIMITER)) { + password = password.substring(0, password.indexOf(ROLE_DELIMITER)); + } + } + } } - } private static void showHelp() { @@ -298,6 +300,10 @@ public int getPort() { public String getUser() { return user; } + + public void setUser(String user) { + this.user = user; + } public String getPassword() { return password; diff --git a/client/src/main/java/org/apache/karaf/client/Main.java b/client/src/main/java/org/apache/karaf/client/Main.java index 14b09917c8a..e9206b08d67 100644 --- a/client/src/main/java/org/apache/karaf/client/Main.java +++ b/client/src/main/java/org/apache/karaf/client/Main.java @@ -149,14 +149,28 @@ public String getUpdatedPassword(ClientSession session, String prompt, String la } }); } + + if (config.getUser()==null || config.getUser().isEmpty()) { + while (true) { + String user = console.readLine("Enter user : "); + if (user==null || user.isEmpty()) { + System.err.println("User must not be empty!"); + } + else { + config.setUser(user); + break; + } + } + } + else if (console != null) { + console.printf("Logging in as %s\n", config.getUser()); + } + setupAgent(config.getUser(), config.getKeyFile(), client, passwordProvider); client.getProperties().put(FactoryManager.IDLE_TIMEOUT, String.valueOf(config.getIdleTimeout())); // TODO: remove the line below when SSHD-732 is fixed client.setKeyPairProvider(new FileKeyPairProvider()); client.start(); - if (console != null) { - console.printf("Logging in as %s\n", config.getUser()); - } ClientSession session = connectWithRetries(client, config); if (config.getPassword() != null) { session.addPasswordIdentity(config.getPassword());