Mercurial > hg > release > thermostat-1.4
changeset 1749:e997f60ec4d2
Create web.auth in in mongodb credentials setup step
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-August/015109.html
PR2581
author | Anirudhan Mukundan <amukunda@redhat.com> |
---|---|
date | Wed, 12 Aug 2015 13:31:48 -0400 |
parents | c4d1214d3de0 |
children | aa3934b1f7d1 |
files | setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetup.java setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImplTest.java |
diffstat | 4 files changed, 98 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java Wed Sep 02 11:26:48 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/SetupWindow.java Wed Aug 12 13:31:48 2015 -0400 @@ -67,13 +67,16 @@ private StartView startView; private MongoUserSetupView mongoUserSetupView; private UserPropertiesView userPropertiesView; - private String username = null; - private char[] password = null; + private String storageUsername = null; + private char[] storagePassword = null; private boolean showDetailedBlurb = false; private ThermostatSetup thermostatSetup; - private static final String defaultUsername = "mongodevuser"; - private static final String defaultPassword = "mongodevpassword"; + private static final String DEFAULT_AGENT_USER = "agent-tester"; + private static final String DEFAULT_CLIENT_USER = "client-tester"; + private static final String DEFAULT_USER_PASSWORD = "tester"; + private static final String DEFAULT_STORAGE_USER = "mongodevuser"; + private static final String DEFAULT_STORAGE_PASSWORD = "mongodevpassword"; private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer(); private PrintStream out; @@ -105,8 +108,8 @@ } private void cleanup() { - if (password != null) { - Arrays.fill(password, '\0'); + if (storagePassword != null) { + Arrays.fill(storagePassword, '\0'); } } @@ -184,15 +187,15 @@ mongoUserSetupView.getDefaultSetupBtn().addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - mongoUserSetupView.setUsername(defaultUsername); - mongoUserSetupView.setPassword(defaultPassword); + mongoUserSetupView.setUsername(DEFAULT_STORAGE_USER); + mongoUserSetupView.setPassword(DEFAULT_STORAGE_PASSWORD); } }); mongoUserSetupView.getNextBtn().addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - username = mongoUserSetupView.getUsername(); - password = mongoUserSetupView.getPassword(); + storageUsername = mongoUserSetupView.getUsername(); + storagePassword = mongoUserSetupView.getPassword(); runMongoSetup(); if (thermostatSetup.isWebAppInstalled()) { @@ -236,7 +239,7 @@ mongoUserSetupView.disableButtons(); userPropertiesView.disableButtons(); try { - thermostatSetup.createMongodbUser(username, password); + thermostatSetup.createMongodbUser(storageUsername, storagePassword); } catch (MongodbUserSetupException e) { e.printStackTrace(); shutdown(); @@ -294,8 +297,8 @@ UserRoles.WRITE, }; } - thermostatSetup.createThermostatUser(username, password, agentRoles); - thermostatSetup.createThermostatUser(username, password, clientRoles); + thermostatSetup.createThermostatUser(DEFAULT_AGENT_USER, DEFAULT_USER_PASSWORD.toCharArray(), agentRoles); + thermostatSetup.createThermostatUser(DEFAULT_CLIENT_USER, DEFAULT_USER_PASSWORD.toCharArray(), clientRoles); } catch (IOException e) { e.printStackTrace();
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetup.java Wed Sep 02 11:26:48 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetup.java Wed Aug 12 13:31:48 2015 -0400 @@ -41,8 +41,9 @@ public interface ThermostatSetup { /** - * Provided a username and password, - * creates a MongodbUser + * Creates a Mongodb User and a web.auth + * file (if webapp is installed) for the + * provided username and password * * @param username * @param password @@ -51,9 +52,9 @@ void createMongodbUser(String username, char[] password) throws MongodbUserSetupException; /** - * Creates web.auth file and sets - * users.properties and - * roles.properties for a user + * Creates entries in the thermostat-users.properties + * and thermostat-roles.properties for the provided + * username, password and roles * * @param username * @param password
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java Wed Sep 02 11:26:48 2015 +0200 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java Wed Aug 12 13:31:48 2015 -0400 @@ -70,9 +70,6 @@ private static final String USERS_PROPERTIES = "thermostat-users.properties"; private static final String ROLES_PROPERTIES = "thermostat-roles.properties"; private static final String MONGO_INPUT_SCRIPT = "/tmp/mongo-input.js"; - private static final String DEFAULT_AGENT_USER = "agent-tester"; - private static final String DEFAULT_CLIENT_USER = "client-tester"; - private static final String DEFAULT_USER_PASSWORD = "tester"; private static final String THERMOSTAT_AGENT = "thermostat-agent"; private static final String THERMOSTAT_CLIENT = "thermostat-client"; private static final String THERMOSTAT_CMDC = "thermostat-cmdc"; @@ -156,6 +153,10 @@ stopStorage(); + if (isWebAppInstalled()) { + writeStorageCredentialsFile(username, password); + } + File userDoneFile = new File(this.userDoneFile); userDoneFile.createNewFile(); @@ -193,6 +194,22 @@ return mongoProcess.start().waitFor(); } + private void writeStorageCredentialsFile(String username, char[] password) throws MongodbUserSetupException { + File credentialsFile = finder.getConfiguration(WEB_AUTH_FILE); + Properties credentialProps = new Properties(); + credentialProps.setProperty("storage.username", username); + credentialProps.setProperty("storage.password", String.valueOf(password)); + + try { + credentialProps.store(new FileOutputStream(credentialsFile), "Storage Credentials"); + } catch (IOException e) { + throw new MongodbUserSetupException("Storing credentials to file " + WEB_AUTH_FILE + " failed!", e); + } + + credentialsFile.setReadable(true, false); + credentialsFile.setWritable(true, true); + } + private void removeTempStampFile() { if (setupCompleteFile.exists()) { setupCompleteFile.delete(); @@ -201,73 +218,32 @@ @Override public void createThermostatUser(String username, char[] password, String[] roles) throws IOException { - File credentialsFile = finder.getConfiguration(WEB_AUTH_FILE); - try { - Properties credentialProps = new Properties(); - credentialProps.setProperty("storage.username", username); - credentialProps.setProperty("storage.password", String.valueOf(password)); - credentialProps.store(new FileOutputStream(credentialsFile), "Storage Credentials"); - - credentialsFile.setReadable(true, false); - credentialsFile.setWritable(true, true); + List<String> rolesList = Arrays.asList(roles); - List<String> rolesList = Arrays.asList(roles); - - if(rolesList.containsAll(Arrays.asList(UserRoles.CLIENT_ROLES))) { - createClientUser(); - setClientRoles(roles); - } else if(rolesList.containsAll(Arrays.asList(UserRoles.AGENT_ROLES))) { - createAgentUser(); - setAgentRoles(roles); - } - - } catch (IOException e) { - throw new IOException("Automatic substitution of file " + WEB_AUTH_FILE + " failed!", e); + if (rolesList.containsAll(Arrays.asList(UserRoles.CLIENT_ROLES))) { + createClientUser(username, password, roles); + } else if (rolesList.containsAll(Arrays.asList(UserRoles.AGENT_ROLES))) { + createAgentUser(username, password, roles); } } - private void createAgentUser() throws IOException { + private void createClientUser(String username, char[] password, String[] roles) throws IOException { Properties userProps = new Properties(); FileOutputStream userStream = new FileOutputStream(finder.getConfiguration(USERS_PROPERTIES), true); - userProps.setProperty(DEFAULT_AGENT_USER, DEFAULT_USER_PASSWORD); - userProps.store(userStream, "Agent User"); - - setAgentCredentials(); - } + userProps.setProperty(username, String.valueOf(password)); + userProps.store(userStream, "Client User"); - private void setAgentCredentials() throws IOException { - Properties agentProps = new Properties(); - FileOutputStream agentAuthStream = new FileOutputStream(new File(userAgentAuth)); - agentProps.setProperty("username", DEFAULT_AGENT_USER); - agentProps.setProperty("password", DEFAULT_USER_PASSWORD); - agentProps.store(agentAuthStream, "Agent Credentials"); + setClientRoles(username, roles); } - private void createClientUser() throws IOException { - Properties userProps = new Properties(); - FileOutputStream userStream = new FileOutputStream(finder.getConfiguration(USERS_PROPERTIES), true); - userProps.setProperty(DEFAULT_CLIENT_USER, DEFAULT_USER_PASSWORD); - userProps.store(userStream, "Client User"); - } - - private void setAgentRoles(String[] agentRoles) throws IOException { - String[] agentUserRoles = new String[] { - THERMOSTAT_AGENT - }; - setRoleProperty(DEFAULT_AGENT_USER, agentUserRoles); - setRoleProperty(THERMOSTAT_AGENT, agentRoles); - FileOutputStream roleStream = new FileOutputStream(finder.getConfiguration(ROLES_PROPERTIES), true); - roleProps.store(new PropertiesWriter(roleStream), "Thermostat Agent Roles"); - } - - private void setClientRoles(String[] clientRoles) throws IOException { + private void setClientRoles(String username, String[] clientRoles) throws IOException { String[] clientUserRoles = new String[] { THERMOSTAT_CLIENT, THERMOSTAT_CMDC, UserRoles.PURGE }; - String[] cmdcRoles = new String[]{ + String[] cmdcRoles = new String[] { UserRoles.GRANT_CMD_CHANNEL_GARBAGE_COLLECT, UserRoles.GRANT_CMD_CHANNEL_DUMP_HEAP, UserRoles.GRANT_CMD_CHANNEL_GRANT_THREAD_HARVESTER, @@ -277,7 +253,7 @@ UserRoles.GRANT_CMD_CHANNEL_JMX_TOGGLE_NOTIFICATION, }; - setRoleProperty(DEFAULT_CLIENT_USER, clientUserRoles); + setRoleProperty(username, clientUserRoles); setRoleProperty(THERMOSTAT_CLIENT, clientRoles); setRoleProperty(THERMOSTAT_CMDC, cmdcRoles); @@ -285,8 +261,34 @@ roleProps.store(new PropertiesWriter(roleStream), "Thermostat Client Roles"); } + private void createAgentUser(String username, char[] password, String[] roles) throws IOException { + Properties userProps = new Properties(); + FileOutputStream userStream = new FileOutputStream(finder.getConfiguration(USERS_PROPERTIES), true); + userProps.setProperty(username, String.valueOf(password)); + userProps.store(userStream, "Agent User"); + + //set agent credentials + Properties agentProps = new Properties(); + FileOutputStream agentAuthStream = new FileOutputStream(new File(userAgentAuth)); + agentProps.setProperty("username", username); + agentProps.setProperty("password", String.valueOf(password)); + agentProps.store(agentAuthStream, "Agent Credentials"); + + setAgentRoles(username, roles); + } + + private void setAgentRoles(String username, String[] agentRoles) throws IOException { + String[] agentUserRoles = new String[] { + THERMOSTAT_AGENT + }; + setRoleProperty(username, agentUserRoles); + setRoleProperty(THERMOSTAT_AGENT, agentRoles); + FileOutputStream roleStream = new FileOutputStream(finder.getConfiguration(ROLES_PROPERTIES), true); + roleProps.store(new PropertiesWriter(roleStream), "Thermostat Agent Roles"); + } + private void setRoleProperty(String attribute, String[] roles) throws IOException { - if(roleProps == null) { + if (roleProps == null) { roleProps = new Properties(); } if (roles.length > 0) {
--- a/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImplTest.java Wed Sep 02 11:26:48 2015 +0200 +++ b/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImplTest.java Wed Aug 12 13:31:48 2015 -0400 @@ -214,6 +214,11 @@ File userDoneFile = new File(userDataDir.toString() + "/mongodb-user-done.stamp"); File setupCompleteFile = new File(userDataDir.toString() + "/setup-complete.stamp"); + //create path to webapp so web.auth creation is invoked + //when ThermostatSetup.createMongodbUser() is called + Path webAppPath = thermostatSysHome.resolve("webapp"); + Files.createDirectories(webAppPath); + doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { @@ -244,6 +249,12 @@ assertTrue(userDoneFile.exists()); assertTrue(setupCompleteFile.exists()); + + assertTrue(credentialsFile.toFile().exists()); + String credentialsData = new String(Files.readAllBytes(credentialsFile)); + assertTrue(credentialsData.contains("storage.username=" + username)); + assertTrue(credentialsData.contains("storage.password=" + password)); + String setupCompleteData = new String(Files.readAllBytes(setupCompleteFile.toPath())); assertTrue(setupCompleteData.contains("Created by Thermostat Setup")); } @@ -351,6 +362,10 @@ @Test public void testSetupThermostatUser() throws IOException { + String clientUser = "client-tester"; + String agentUser = "agent-tester"; + String userPassword = "tester"; + String[] agentRoles = new String[] { UserRoles.CMD_CHANNEL_VERIFY, UserRoles.LOGIN, @@ -378,14 +393,8 @@ UserRoles.WRITE, }; - tSetup.createThermostatUser(username, password.toCharArray(), agentRoles); - tSetup.createThermostatUser(username, password.toCharArray(), clientRoles); - - //check credentialsFile - assertTrue(credentialsFile.toFile().exists()); - String credentialsData = new String(Files.readAllBytes(credentialsFile)); - assertTrue(credentialsData.contains("storage.username=" + username)); - assertTrue(credentialsData.contains("storage.password=" + password)); + tSetup.createThermostatUser(agentUser, userPassword.toCharArray(), agentRoles); + tSetup.createThermostatUser(clientUser, userPassword.toCharArray(), clientRoles); //check agent credentials file assertTrue(userAgentAuth.toFile().exists());