ARTEMIS-786 checking for inputs and some reorg of the class model on user actions
Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/119476dd Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/119476dd Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/119476dd Branch: refs/heads/ARTEMIS-780 Commit: 119476ddcc77cfc7a2192f8ba87101dcbd7b44b1 Parents: cd7b838 Author: Clebert Suconic <[email protected]> Authored: Wed Nov 2 14:58:48 2016 -0400 Committer: Clebert Suconic <[email protected]> Committed: Wed Nov 2 15:17:31 2016 -0400 ---------------------------------------------------------------------- .../artemis/cli/commands/user/AddUser.java | 32 ++++++------ .../artemis/cli/commands/user/ListUser.java | 15 ++++++ .../cli/commands/user/PasswordAction.java | 37 ++++++++++++++ .../artemis/cli/commands/user/RemoveUser.java | 9 ++++ .../artemis/cli/commands/user/ResetUser.java | 27 +++++----- .../artemis/cli/commands/user/UserAction.java | 54 ++++++-------------- 6 files changed, 107 insertions(+), 67 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/119476dd/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java index cbb8f60..caa32a7 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/AddUser.java @@ -20,6 +20,7 @@ import io.airlift.airline.Command; import io.airlift.airline.Option; import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.util.HashUtil; +import org.apache.activemq.artemis.util.FileBasedSecStoreConfig; import org.apache.commons.lang3.StringUtils; /** @@ -27,13 +28,7 @@ import org.apache.commons.lang3.StringUtils; * ./artemis user add --username guest --role admin --password *** */ @Command(name = "add", description = "Add a new user") -public class AddUser extends UserAction { - - @Option(name = "--password", description = "the password (Default: input)") - String password; - - @Option(name = "--role", description = "user's role(s), comma separated", required = true) - String role; +public class AddUser extends PasswordAction { @Option(name = "--plaintext", description = "using plaintext (Default false)") boolean plaintext = false; @@ -42,9 +37,9 @@ public class AddUser extends UserAction { public Object execute(ActionContext context) throws Exception { super.execute(context); - if (password == null) { - password = inputPassword("--password", "Please provide the password:", null); - } + checkInputUser(); + checkInputPassword(); + checkInputRole(); String hash = plaintext ? password : HashUtil.tryHash(context, password); add(hash, StringUtils.split(role, ",")); @@ -52,11 +47,16 @@ public class AddUser extends UserAction { return null; } - public void setPassword(String password) { - this.password = password; - } - - public void setRole(String role) { - this.role = role; + /** + * Adding a new user + * @param hash the password + * @param role the role + * @throws IllegalArgumentException if user exists + */ + protected void add(String hash, String... role) throws Exception { + FileBasedSecStoreConfig config = getConfiguration(); + config.addNewUser(username, hash, role); + config.save(); + context.out.println("User added successfully."); } } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/119476dd/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java index cb3ff39..136a417 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ListUser.java @@ -16,8 +16,11 @@ */ package org.apache.activemq.artemis.cli.commands.user; +import java.util.List; + import io.airlift.airline.Command; import org.apache.activemq.artemis.cli.commands.ActionContext; +import org.apache.activemq.artemis.util.FileBasedSecStoreConfig; /** * list existing users, example: @@ -35,4 +38,16 @@ public class ListUser extends UserAction { return null; } + /** + * list a single user or all users + * if username is not specified + */ + protected void list() throws Exception { + FileBasedSecStoreConfig config = getConfiguration(); + List<String> result = config.listUser(username); + for (String str : result) { + context.out.println(str); + } + } + } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/119476dd/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/PasswordAction.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/PasswordAction.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/PasswordAction.java new file mode 100644 index 0000000..2260488 --- /dev/null +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/PasswordAction.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.activemq.artemis.cli.commands.user; + +import io.airlift.airline.Option; + +public class PasswordAction extends UserAction { + + @Option(name = "--password", description = "the password (Default: input)") + String password; + + protected void checkInputPassword() { + if (password == null) { + password = inputPassword("--password", "Please provide the password:", null); + } + } + + public void setPassword(String password) { + this.password = password; + } + +} http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/119476dd/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java index 172a76d..70167da 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/RemoveUser.java @@ -18,6 +18,7 @@ package org.apache.activemq.artemis.cli.commands.user; import io.airlift.airline.Command; import org.apache.activemq.artemis.cli.commands.ActionContext; +import org.apache.activemq.artemis.util.FileBasedSecStoreConfig; /** * Remove a user, example: @@ -29,8 +30,16 @@ public class RemoveUser extends UserAction { @Override public Object execute(ActionContext context) throws Exception { super.execute(context); + checkInputUser(); remove(); return null; } + protected void remove() throws Exception { + FileBasedSecStoreConfig config = getConfiguration(); + config.removeUser(username); + config.save(); + context.out.println("User removed."); + } + } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/119476dd/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java index 27da6c7..c219ef5 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/ResetUser.java @@ -20,6 +20,7 @@ import io.airlift.airline.Command; import io.airlift.airline.Option; import org.apache.activemq.artemis.cli.commands.ActionContext; import org.apache.activemq.artemis.cli.commands.util.HashUtil; +import org.apache.activemq.artemis.util.FileBasedSecStoreConfig; import org.apache.commons.lang3.StringUtils; /** @@ -27,13 +28,7 @@ import org.apache.commons.lang3.StringUtils; * ./artemis user reset --username guest --role admin --password *** */ @Command(name = "reset", description = "Reset user's password or roles") -public class ResetUser extends UserAction { - - @Option(name = "--password", description = "the password (Default: input)") - String password; - - @Option(name = "--role", description = "user's role(s), comma separated") - String role; +public class ResetUser extends PasswordAction { @Option(name = "--plaintext", description = "using plaintext (Default false)") boolean plaintext = false; @@ -42,6 +37,9 @@ public class ResetUser extends UserAction { public Object execute(ActionContext context) throws Exception { super.execute(context); + checkInputUser(); + checkInputPassword(); + if (password != null) { password = plaintext ? password : HashUtil.tryHash(context, password); } @@ -55,11 +53,14 @@ public class ResetUser extends UserAction { return null; } - public void setPassword(String password) { - this.password = password; - } - - public void setRole(String role) { - this.role = role; + protected void reset(String password, String[] roles) throws Exception { + if (password == null && roles == null) { + context.err.println("Nothing to update."); + return; + } + FileBasedSecStoreConfig config = getConfiguration(); + config.updateUser(username, password, roles); + config.save(); + context.out.println("User updated"); } } http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/119476dd/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java ---------------------------------------------------------------------- diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java index 918338e..2f7c77f 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/user/UserAction.java @@ -24,63 +24,41 @@ import org.apache.activemq.artemis.util.FileBasedSecStoreConfig; import javax.security.auth.login.AppConfigurationEntry; import javax.security.auth.login.Configuration; import java.io.File; -import java.util.List; import static org.apache.activemq.artemis.spi.core.security.jaas.PropertiesLoginModule.ROLE_FILE_PROP_NAME; import static org.apache.activemq.artemis.spi.core.security.jaas.PropertiesLoginModule.USER_FILE_PROP_NAME; public abstract class UserAction extends InputAbstract { - @Option(name = "--user", description = "The user name") + @Option(name = "--role", description = "user's role(s), comma separated") + String role; + + @Option(name = "--user", description = "The user name (Default: input)") String username = null; - /** - * Adding a new user - * @param hash the password - * @param role the role - * @throws IllegalArgumentException if user exists - */ - protected void add(String hash, String... role) throws Exception { - FileBasedSecStoreConfig config = getConfiguration(); - config.addNewUser(username, hash, role); - config.save(); - context.out.println("User added successfully."); - } + @Option(name = "--entry", description = "The appConfigurationEntry (default: activemq)") + String entry = "activemq"; - /** - * list a single user or all users - * if username is not specified - */ - protected void list() throws Exception { - FileBasedSecStoreConfig config = getConfiguration(); - List<String> result = config.listUser(username); - for (String str : result) { - context.out.println(str); + protected void checkInputUser() { + if (username == null) { + username = input("--user", "Please provider the userName:", null); } } - protected void remove() throws Exception { - FileBasedSecStoreConfig config = getConfiguration(); - config.removeUser(username); - config.save(); - context.out.println("User removed."); + public void setRole(String role) { + this.role = role; } - protected void reset(String password, String[] roles) throws Exception { - if (password == null && roles == null) { - context.err.println("Nothing to update."); - return; + public void checkInputRole() { + if (role == null) { + role = input("--role", "type a comma separated list of roles", null); } - FileBasedSecStoreConfig config = getConfiguration(); - config.updateUser(username, password, roles); - config.save(); - context.out.println("User updated"); } - private FileBasedSecStoreConfig getConfiguration() throws Exception { + protected FileBasedSecStoreConfig getConfiguration() throws Exception { Configuration securityConfig = Configuration.getConfiguration(); - AppConfigurationEntry[] entries = securityConfig.getAppConfigurationEntry("activemq"); + AppConfigurationEntry[] entries = securityConfig.getAppConfigurationEntry(entry); for (AppConfigurationEntry entry : entries) { if (entry.getLoginModuleName().equals(PropertiesLoginModule.class.getName())) {
