/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
23package org.jboss.as.domain.management.security;
24
25import org.jboss.as.domain.management.security.state.PropertyFileFinder;
26import org.jboss.as.domain.management.security.state.PropertyFilePrompt;
27import org.jboss.as.domain.management.security.state.State;
28import org.jboss.as.domain.management.security.state.StateValues;
29
30import java.io.Closeable;
31import java.io.IOException;
32import java.io.StringReader;
33import java.util.LinkedList;
34import java.util.List;
35import java.util.Properties;
36
37import static org.jboss.as.domain.management.DomainManagementMessages.MESSAGES;
38
39/**
40 * A command line utility to add new users to the mgmt-users.properties files.
41 *
42 * @author <a href="mailto:darran.lofthouse@jboss.com">Darran Lofthouse</a>
43 */
44public class AddPropertiesUser {
45
46 public static final String[] BAD_USER_NAMES = {"admin", "administrator", "root"};
47
48 public static final String SERVER_BASE_DIR = "jboss.server.base.dir";
49 public static final String SERVER_CONFIG_DIR = "jboss.server.config.dir";
50 public static final String SERVER_CONFIG_USER_DIR = "jboss.server.config.user.dir";
51 public static final String DOMAIN_BASE_DIR = "jboss.domain.base.dir";
52 public static final String DOMAIN_CONFIG_DIR = "jboss.domain.config.dir";
53 public static final String DOMAIN_CONFIG_USER_DIR = "jboss.domain.config.user.dir";
54
55 public static final String DEFAULT_MANAGEMENT_REALM = "ManagementRealm";
56 public static final String DEFAULT_APPLICATION_REALM = "ApplicationRealm";
57 public static final String MGMT_USERS_PROPERTIES = "mgmt-users.properties";
58 public static final String APPLICATION_USERS_PROPERTIES = "application-users.properties";
59 public static final String APPLICATION_ROLES_PROPERTIES = "application-roles.properties";
60 public static final String APPLICATION_USERS_SWITCH = "-a";
61
62 public static final String DOMAIN_CONFIG_DIR_USERS_SWITCH = "-dc";
63 public static final String SERVER_CONFIG_DIR_USERS_SWITCH = "-sc";
64
65 private static final char CARRIAGE_RETURN_CHAR = '\r';
66
67 public static final String NEW_LINE = "\n";
68 public static final String SPACE = " ";
69
70 private static final Properties argsCliProps = new Properties();
71
72
73 private ConsoleWrapper theConsole;
74
75
76 protected State nextState;
77
78 protected AddPropertiesUser() {
79 theConsole = new JavaConsole();
80 StateValues stateValues = new StateValues();
81 stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
82
83 if (theConsole.getConsole() == null) {
84 throw MESSAGES.noConsoleAvailable();
85 }
86 nextState = new PropertyFilePrompt(theConsole, stateValues);
87 }
88
89 protected AddPropertiesUser(ConsoleWrapper console) {
90 this.theConsole = console;
91 StateValues stateValues = new StateValues();
92 stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
93 nextState = new PropertyFilePrompt(theConsole,stateValues);
94 }
95
96 private AddPropertiesUser(final boolean management, final String user, final char[] password, final String realm) {
97 boolean silent = false;
98 StateValues stateValues = new StateValues();
99 stateValues.setJbossHome(System.getenv("JBOSS_HOME"));
100 String valueSilent = argsCliProps.getProperty("silent");
101
102 if (valueSilent != null) {
103 silent = Boolean.valueOf(valueSilent);
104 }
105 if (silent) {
106 stateValues.setHowInteractive(Interactiveness.SILENT);
107 } else {
108 stateValues.setHowInteractive(Interactiveness.NON_INTERACTIVE);
109 }
110
111 if ((theConsole == null) && (stateValues.isSilent() == false)) {
112 throw MESSAGES.noConsoleAvailable();
113 }
114 stateValues.setUserName(user);
115 stateValues.setPassword(password);
116 stateValues.setRealm(realm);
117 stateValues.setManagement(management);
118
119 nextState = new PropertyFileFinder(theConsole, stateValues);
120 }
121
122 private AddPropertiesUser(boolean management, final String user, final char[] password) {
123 this(management, user, password, management ? DEFAULT_MANAGEMENT_REALM : DEFAULT_APPLICATION_REALM);
124 }
125
126 protected void run() {
127 while ((nextState = nextState.execute()) != null) {
128 }
129 }
130
131 /**
132 * @param args
133 */
134 public static void main(String[] args) {
135
136 List<String> argsList = new LinkedList<String>();
137 String[] argsArray = null;
138 StringReader stringReader = null;
139 boolean management = true;
140
141 int realArgsLength;
142
143 if (args.length >= 1) {
144
145 for (String temp : args) {
146 if (temp.startsWith("--")) {
147 try {
148 stringReader = new StringReader(temp.substring(2));
149 argsCliProps.load(stringReader);
150 } catch (IOException e) {
151 e.printStackTrace();
152 } finally {
153 safeClose(stringReader);
154 }
155 } else if (temp.equals(APPLICATION_USERS_SWITCH)) {
156 management = false;
157 } else if (temp.indexOf(DOMAIN_CONFIG_DIR_USERS_SWITCH)>=0) {
158 System.setProperty(DOMAIN_CONFIG_DIR,temp.substring(3));
159 } else if (temp.indexOf(SERVER_CONFIG_DIR)>=0) {
160 System.setProperty(SERVER_CONFIG_DIR,temp.substring(3));
161 } else {
162 argsList.add(temp);
163 }
164 }
165 }
166 argsArray = argsList.toArray(new String[0]);
167 realArgsLength = argsArray.length;
168 if (realArgsLength == 3) {
169 new AddPropertiesUser(management, argsArray[0], argsArray[1].toCharArray(), argsArray[2]).run();
170 } else if (realArgsLength == 2) {
171 new AddPropertiesUser(management, argsArray[0], argsArray[1].toCharArray()).run();
172 } else {
173 new AddPropertiesUser().run();
174 }
175 }
176
177 private static void safeClose(Closeable c) {
178 if (c != null) {
179 try {
180 c.close();
181 } catch (IOException e) {
182 }
183 }
184 }
185
186 public enum Interactiveness {
187 SILENT, NON_INTERACTIVE, INTERACTIVE
188 }
189}