Skip to content

Commit

Permalink
[AS7-6464] Re-working of the add-user utility states for: -
Browse files Browse the repository at this point in the history
 1 - Quicker reporting of a problem to the user i.e. Don't ask for three values then tell them the first one is rejected.
 2 - Improved workflow regarding validation to prevent checks from being skipped should a previous check fail.
  • Loading branch information
darranl authored and bstansberry committed Feb 8, 2013
1 parent 712dfd5 commit 5401ec6
Show file tree
Hide file tree
Showing 20 changed files with 654 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,22 @@ public interface DomainManagementMessages {
@Message(id = Message.NONE, value = "n")
String shortNo();

/**
* Message to check if an alternative realm is really desired.
*
* @return the message.
*/
@Message(id = Message.NONE, value = "The realm name supplied must match the name used by the server configuration which by default would be '%s'")
String alternativeRealm(final String defaultRealm);

/**
* Confirmation of realm choice.
*
* @return the message.
*/
@Message(id = Message.NONE, value = "Are you sure you want to set the realm to '%s'")
String realmConfirmation(final String chosenRealm);

/*
* Logging IDs 15200 to 15299 are reserved for domain management, the file DomainManagementLogger also contains messages in
* this range commencing 15200.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class AddPropertiesUser {
protected AddPropertiesUser(ConsoleWrapper console) {
theConsole = console;
StateValues stateValues = new StateValues();
stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
stateValues.setJBossHome(System.getenv("JBOSS_HOME"));

if (theConsole.getConsole() == null) {
throw MESSAGES.noConsoleAvailable();
Expand All @@ -80,7 +80,7 @@ protected AddPropertiesUser(ConsoleWrapper console) {

private AddPropertiesUser(ConsoleWrapper console, final boolean management, final String user, final String password, final String realm) {
StateValues stateValues = new StateValues();
stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
stateValues.setJBossHome(System.getenv("JBOSS_HOME"));

final Interactiveness howInteractive;
boolean silent = Boolean.valueOf(argsCliProps.getProperty(CommandLineArgument.SILENT.key()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.domain.management.security.state;

import java.util.Collection;
import java.util.Iterator;

/**
* Where multiple stages of validation need to be performed this {@link State} provides a way to coordinate ensuring each stage
* of the validation is performed.
*
* One instance of this state should be created when the validation begins and as each step passes this instance should be used
* as the next state. Should validation fail then the instance of this State should be discarded so that validation can commence
* from the beginning if alternative values are provided.
*
* @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
*/
public abstract class AbstractValidationState implements State {

private Iterator<State> stateIterator;

@Override
public State execute() {
if (stateIterator == null) {
stateIterator = getValidationStates().iterator();
}

if (stateIterator.hasNext()) {
return stateIterator.next();
}

return getSuccessState();
}

/**
* Get a {@link Collection} containing all states required to perform the validation needed.
*
* On initialisation an Iterator will be created for this collection - each time this state is called the next state
* returned by the Iterator will be called to perform validation. Once the Iterator is exhausted the success state will be
* returned instead.
*
* If validation fails then it is expected that the individual validation states will transition away from this state, this
* is why there is no error or failure state.
*/
protected abstract Collection<State> getValidationStates();

/**
* Get the state to transition to once all validation is complete.
*
* @return The state to transition to once all validation is complete.
*/
protected abstract State getSuccessState();

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class ConfirmationChoice implements State {

private ConsoleWrapper theConsole;
private final String message;
private final String[] messageLines;
private final String prompt;
private final State yesState;
private final State noState;
Expand All @@ -49,9 +49,9 @@ public class ConfirmationChoice implements State {
private static final int NO = 1;
private static final int INVALID = 2;

public ConfirmationChoice(ConsoleWrapper theConsole,final String message, final String prompt, final State yesState, final State noState) {
public ConfirmationChoice(ConsoleWrapper theConsole,final String[] messageLines, final String prompt, final State yesState, final State noState) {
this.theConsole = theConsole;
this.message = message;
this.messageLines = messageLines;
this.prompt = prompt;
this.yesState = yesState;
this.noState = noState;
Expand All @@ -60,11 +60,18 @@ public ConfirmationChoice(ConsoleWrapper theConsole,final String message, final
}
}

public ConfirmationChoice(ConsoleWrapper theConsole, final String message, final String prompt, final State yesState,
final State noState) {
this(theConsole, new String[] { message }, prompt, yesState, noState);
}

@Override
public State execute() {
if (message != null) {
theConsole.printf(message);
theConsole.printf(NEW_LINE);
if (messageLines != null) {
for (String message : messageLines) {
theConsole.printf(message);
theConsole.printf(NEW_LINE);
}
}

theConsole.printf(prompt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
import org.jboss.as.domain.management.security.ConsoleWrapper;

/**
* State to check that the user is not already defined in any of the resolved
* properties files.
* State to branch between adding and updating the user and outputting summary information if not running in silent mode.
*
*/
public class DuplicateUserCheckState implements State {
public class PreModificationState implements State {

private final ConsoleWrapper theConsole;
private StateValues stateValues;

public DuplicateUserCheckState(final ConsoleWrapper theConsole,final StateValues stateValues) {
public PreModificationState(final ConsoleWrapper theConsole, final StateValues stateValues) {
this.theConsole = theConsole;
this.stateValues = stateValues;
if (stateValues.isSilent() == false && theConsole.getConsole() == null) {
Expand All @@ -56,13 +56,12 @@ public State execute() {
String message = MESSAGES.aboutToAddUser(stateValues.getUserName(), stateValues.getRealm());
String prompt = MESSAGES.isCorrectPrompt() + " " + MESSAGES.yes() + "/" + MESSAGES.no() + "?";

continuingState = new ConfirmationChoice(theConsole,message, prompt, addState, new PromptNewUserState(theConsole, stateValues));
continuingState = new ConfirmationChoice(theConsole, message, prompt, addState, new PromptNewUserState(
theConsole, stateValues));
}
}

return continuingState;
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
package org.jboss.as.domain.management.security.state;

import static org.jboss.as.domain.management.DomainManagementMessages.MESSAGES;
import static org.jboss.as.domain.management.security.AddPropertiesUser.DEFAULT_MANAGEMENT_REALM;
import static org.jboss.as.domain.management.security.AddPropertiesUser.NEW_LINE;

import org.jboss.as.domain.management.security.ConsoleWrapper;

Expand All @@ -36,12 +34,6 @@ public class PromptNewUserState implements State {
private final StateValues stateValues;
private ConsoleWrapper theConsole;

public PromptNewUserState(ConsoleWrapper theConsole) {
this.theConsole = theConsole;
stateValues = new StateValues();
stateValues.setRealm(DEFAULT_MANAGEMENT_REALM);
}

public PromptNewUserState(ConsoleWrapper theConsole, final StateValues stateValues) {
this.theConsole = theConsole;
this.stateValues = stateValues;
Expand All @@ -52,38 +44,18 @@ public PromptNewUserState(ConsoleWrapper theConsole, final StateValues stateValu

@Override
public State execute() {
State continuingState = new PromptPasswordState(theConsole, stateValues);
State continuingState = new ValidateUserState(theConsole, stateValues);
if (stateValues.isSilentOrNonInteractive() == false) {
theConsole.printf(NEW_LINE);
theConsole.printf(MESSAGES.enterNewUserDetails());
theConsole.printf(NEW_LINE);
stateValues.setPassword(null); // If interactive we want to be sure to capture this.

/*
* Prompt for realm.
*/
theConsole.printf(MESSAGES.realmPrompt(stateValues.getRealm()));
String temp = theConsole.readLine(" : ");
if (temp == null) {
/*
* This will return user to the command prompt so add a new line to
* ensure the command prompt is on the next line.
*/
theConsole.printf(NEW_LINE);
return null;
}
if (temp.length() > 0) {
stateValues.setRealm(temp);
}

/*
* Prompt for username.
*/
String existingUsername = stateValues.getUserName();
String usernamePrompt = existingUsername == null ? MESSAGES.usernamePrompt() :
MESSAGES.usernamePrompt(existingUsername);
theConsole.printf(usernamePrompt);
temp = theConsole.readLine(" : ");
String temp = theConsole.readLine(" : ");
if (temp != null && temp.length() > 0) {
existingUsername = temp;
}
Expand All @@ -92,19 +64,6 @@ public State execute() {
return new ErrorState(theConsole,MESSAGES.noUsernameExiting());
}
stateValues.setUserName(existingUsername);

if (stateValues.getKnownUsers().contains(stateValues.getUserName())) {
State duplicateContinuing = stateValues.isSilentOrNonInteractive() ? null : new PromptNewUserState(theConsole, stateValues);
if (stateValues.isSilentOrNonInteractive()) {
continuingState = new ErrorState(theConsole, MESSAGES.duplicateUser(stateValues.getUserName()), duplicateContinuing, stateValues);
} else {
String message = MESSAGES.aboutToUpdateUser(stateValues.getUserName());
String prompt = MESSAGES.isCorrectPrompt() + " " + MESSAGES.yes() + "/" + MESSAGES.no() + "?";

stateValues.setExistingUser(true);
continuingState = new ConfirmationChoice(theConsole,message, prompt, continuingState, duplicateContinuing);
}
}
}

return continuingState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,58 @@
/**
* State to prompt the user for a password
* <p/>
* This state handles password validation by let the user re-enter the password
* in case of the password mismatch the user will be present for an error
* and will re-enter the PromptPasswordState again
* This state handles password validation by let the user re-enter the password in case of the password mismatch the user will
* be present for an error and will re-enter the PromptPasswordState again
*/
public class PromptPasswordState implements State {

private ConsoleWrapper theConsole;
private StateValues stateValues;
private final ConsoleWrapper theConsole;
private final StateValues stateValues;
private final boolean rePrompt;

public PromptPasswordState(ConsoleWrapper theConsole, StateValues stateValues) {
public PromptPasswordState(ConsoleWrapper theConsole, StateValues stateValues, boolean rePrompt) {
this.theConsole = theConsole;
this.stateValues = stateValues;
this.rePrompt = rePrompt;
if ((stateValues != null && stateValues.isSilent() == false) && theConsole.getConsole() == null) {
throw MESSAGES.noConsoleAvailable();
}
}

@Override
public State execute() {
State continuingState = new WeakCheckState(theConsole, stateValues);
if (stateValues.isSilentOrNonInteractive() == false) {
/*
* Prompt for password.
*/
theConsole.printf(MESSAGES.passwordPrompt());
char[] tempChar = theConsole.readPassword(" : ");
if (tempChar == null || tempChar.length == 0) {
return new ErrorState(theConsole,MESSAGES.noPasswordExiting());
}
if (rePrompt == false) {
/*
* Prompt for password.
*/
theConsole.printf(MESSAGES.passwordPrompt());
char[] tempChar = theConsole.readPassword(" : ");
if (tempChar == null || tempChar.length == 0) {
return new ErrorState(theConsole, MESSAGES.noPasswordExiting());
}
stateValues.setPassword(tempChar);

theConsole.printf(MESSAGES.passwordConfirmationPrompt());
char[] secondTempChar = theConsole.readPassword(" : ");
if (secondTempChar == null) {
secondTempChar = new char[0]; // If re-entry missed allow fall through to comparison.
}
return new ValidatePasswordState(theConsole, stateValues);
} else {

if (Arrays.equals(tempChar, secondTempChar) == false) {
return new ErrorState(theConsole, MESSAGES.passwordMisMatch(), this);
}
stateValues.setPassword(tempChar);
theConsole.printf(MESSAGES.passwordConfirmationPrompt());
char[] secondTempChar = theConsole.readPassword(" : ");
if (secondTempChar == null) {
secondTempChar = new char[0]; // If re-entry missed allow fall through to comparison.
}

if (!stateValues.isManagement()) {
theConsole.printf(MESSAGES.rolesPrompt());
String userRoles = stateValues.getKnownRoles().get(stateValues.getUserName());
stateValues.setRoles(theConsole.readLine("[%1$2s]: ", (userRoles == null?"":userRoles)));
if (Arrays.equals(stateValues.getPassword(), secondTempChar) == false) {
// Start again at the first password.
return new ErrorState(theConsole, MESSAGES.passwordMisMatch(), new PromptPasswordState(theConsole, stateValues, false));
}

// As long as it matches the actual value has already been validated.
return stateValues.isManagement() ? new PreModificationState(theConsole, stateValues) : new PromptRolesState(
theConsole, stateValues);
}
}

return continuingState;

return new ValidatePasswordState(theConsole, stateValues);
}
}

Loading

0 comments on commit 5401ec6

Please sign in to comment.