PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/domain-management/src/main/java/org/jboss/as/domain/management/security/AddPropertiesUser.java

#
Java | 189 lines | 128 code | 32 blank | 29 comment | 30 complexity | 6e27d83fd3af41bad89043dbaf7fd311 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.domain.management.security;
  23. import org.jboss.as.domain.management.security.state.PropertyFileFinder;
  24. import org.jboss.as.domain.management.security.state.PropertyFilePrompt;
  25. import org.jboss.as.domain.management.security.state.State;
  26. import org.jboss.as.domain.management.security.state.StateValues;
  27. import java.io.Closeable;
  28. import java.io.IOException;
  29. import java.io.StringReader;
  30. import java.util.LinkedList;
  31. import java.util.List;
  32. import java.util.Properties;
  33. import static org.jboss.as.domain.management.DomainManagementMessages.MESSAGES;
  34. /**
  35. * A command line utility to add new users to the mgmt-users.properties files.
  36. *
  37. * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
  38. */
  39. public class AddPropertiesUser {
  40. public static final String[] BAD_USER_NAMES = {"admin", "administrator", "root"};
  41. public static final String SERVER_BASE_DIR = "jboss.server.base.dir";
  42. public static final String SERVER_CONFIG_DIR = "jboss.server.config.dir";
  43. public static final String SERVER_CONFIG_USER_DIR = "jboss.server.config.user.dir";
  44. public static final String DOMAIN_BASE_DIR = "jboss.domain.base.dir";
  45. public static final String DOMAIN_CONFIG_DIR = "jboss.domain.config.dir";
  46. public static final String DOMAIN_CONFIG_USER_DIR = "jboss.domain.config.user.dir";
  47. public static final String DEFAULT_MANAGEMENT_REALM = "ManagementRealm";
  48. public static final String DEFAULT_APPLICATION_REALM = "ApplicationRealm";
  49. public static final String MGMT_USERS_PROPERTIES = "mgmt-users.properties";
  50. public static final String APPLICATION_USERS_PROPERTIES = "application-users.properties";
  51. public static final String APPLICATION_ROLES_PROPERTIES = "application-roles.properties";
  52. public static final String APPLICATION_USERS_SWITCH = "-a";
  53. public static final String DOMAIN_CONFIG_DIR_USERS_SWITCH = "-dc";
  54. public static final String SERVER_CONFIG_DIR_USERS_SWITCH = "-sc";
  55. private static final char CARRIAGE_RETURN_CHAR = '\r';
  56. public static final String NEW_LINE = "\n";
  57. public static final String SPACE = " ";
  58. private static final Properties argsCliProps = new Properties();
  59. private ConsoleWrapper theConsole;
  60. protected State nextState;
  61. protected AddPropertiesUser() {
  62. theConsole = new JavaConsole();
  63. StateValues stateValues = new StateValues();
  64. stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
  65. if (theConsole.getConsole() == null) {
  66. throw MESSAGES.noConsoleAvailable();
  67. }
  68. nextState = new PropertyFilePrompt(theConsole, stateValues);
  69. }
  70. protected AddPropertiesUser(ConsoleWrapper console) {
  71. this.theConsole = console;
  72. StateValues stateValues = new StateValues();
  73. stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
  74. nextState = new PropertyFilePrompt(theConsole,stateValues);
  75. }
  76. private AddPropertiesUser(final boolean management, final String user, final char[] password, final String realm) {
  77. boolean silent = false;
  78. StateValues stateValues = new StateValues();
  79. stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
  80. String valueSilent = argsCliProps.getProperty("silent");
  81. if (valueSilent != null) {
  82. silent = Boolean.valueOf(valueSilent);
  83. }
  84. if (silent) {
  85. stateValues.setHowInteractive(Interactiveness.SILENT);
  86. } else {
  87. stateValues.setHowInteractive(Interactiveness.NON_INTERACTIVE);
  88. }
  89. if ((theConsole == null) && (stateValues.isSilent() == false)) {
  90. throw MESSAGES.noConsoleAvailable();
  91. }
  92. stateValues.setUserName(user);
  93. stateValues.setPassword(password);
  94. stateValues.setRealm(realm);
  95. stateValues.setManagement(management);
  96. nextState = new PropertyFileFinder(theConsole, stateValues);
  97. }
  98. private AddPropertiesUser(boolean management, final String user, final char[] password) {
  99. this(management, user, password, management ? DEFAULT_MANAGEMENT_REALM : DEFAULT_APPLICATION_REALM);
  100. }
  101. protected void run() {
  102. while ((nextState = nextState.execute()) != null) {
  103. }
  104. }
  105. /**
  106. * @param args
  107. */
  108. public static void main(String[] args) {
  109. List<String> argsList = new LinkedList<String>();
  110. String[] argsArray = null;
  111. StringReader stringReader = null;
  112. boolean management = true;
  113. int realArgsLength;
  114. if (args.length >= 1) {
  115. for (String temp : args) {
  116. if (temp.startsWith("--")) {
  117. try {
  118. stringReader = new StringReader(temp.substring(2));
  119. argsCliProps.load(stringReader);
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. } finally {
  123. safeClose(stringReader);
  124. }
  125. } else if (temp.equals(APPLICATION_USERS_SWITCH)) {
  126. management = false;
  127. } else if (temp.indexOf(DOMAIN_CONFIG_DIR_USERS_SWITCH)>=0) {
  128. System.setProperty(DOMAIN_CONFIG_DIR,temp.substring(3));
  129. } else if (temp.indexOf(SERVER_CONFIG_DIR)>=0) {
  130. System.setProperty(SERVER_CONFIG_DIR,temp.substring(3));
  131. } else {
  132. argsList.add(temp);
  133. }
  134. }
  135. }
  136. argsArray = argsList.toArray(new String[0]);
  137. realArgsLength = argsArray.length;
  138. if (realArgsLength == 3) {
  139. new AddPropertiesUser(management, argsArray[0], argsArray[1].toCharArray(), argsArray[2]).run();
  140. } else if (realArgsLength == 2) {
  141. new AddPropertiesUser(management, argsArray[0], argsArray[1].toCharArray()).run();
  142. } else {
  143. new AddPropertiesUser().run();
  144. }
  145. }
  146. private static void safeClose(Closeable c) {
  147. if (c != null) {
  148. try {
  149. c.close();
  150. } catch (IOException e) {
  151. }
  152. }
  153. }
  154. public enum Interactiveness {
  155. SILENT, NON_INTERACTIVE, INTERACTIVE
  156. }
  157. }