ARTEMIS-786 checking for inputs and some reorg of the class model on user actions

This commit is contained in:
Clebert Suconic 2016-11-02 14:58:48 -04:00
parent cd7b838952
commit 119476ddcc
6 changed files with 107 additions and 67 deletions

View File

@ -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.");
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}

View File

@ -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.");
}
}

View File

@ -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");
}
}

View File

@ -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())) {