Mercurial > hg > release > thermostat-1.4
changeset 1750:aa3934b1f7d1
Create credentials files in USER_THERMOSTAT_HOME when THERMOSTAT_HOME is read-only
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-August/015170.html
PR2581
author | Anirudhan Mukundan <amukunda@redhat.com> |
---|---|
date | Fri, 14 Aug 2015 12:15:56 -0400 |
parents | e997f60ec4d2 |
children | bdde8b47dd93 |
files | setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/CredentialFinder.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/CredentialFinderTest.java |
diffstat | 3 files changed, 121 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/CredentialFinder.java Wed Aug 12 13:31:48 2015 -0400 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/CredentialFinder.java Fri Aug 14 12:15:56 2015 -0400 @@ -39,6 +39,7 @@ import com.redhat.thermostat.shared.config.CommonPaths; import java.io.File; +import java.io.IOException; public class CredentialFinder { @@ -48,7 +49,7 @@ this.paths = paths; } - public File getConfiguration(String name) { + public File getConfiguration(String name) throws IOException { File systemFile = getConfigurationFile(paths.getSystemConfigurationDirectory(), name); if (isUsable(systemFile)) { return systemFile; @@ -60,11 +61,20 @@ return null; } + //package-private for testing File getConfigurationFile(File directory, String name) { return new File(directory, name); } - private boolean isUsable(File file) { - return file.isFile() && file.canRead(); + //package-private for testing + boolean isUsable(File file) throws IOException { + if (file.exists()) { + return file.isFile() && file.canRead() && file.canWrite(); + } else { + //Check that the parent directory is not read-only, + //so file can be created + File canonicalFile = file.getCanonicalFile(); + return canonicalFile.getParentFile().canWrite(); + } } }
--- a/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java Wed Aug 12 13:31:48 2015 -0400 +++ b/setup-command/command/src/main/java/com/redhat/thermostat/setup/command/internal/ThermostatSetupImpl.java Fri Aug 14 12:15:56 2015 -0400 @@ -195,19 +195,18 @@ } 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 { + File credentialsFile = finder.getConfiguration(WEB_AUTH_FILE); + Properties credentialProps = new Properties(); + credentialProps.setProperty("storage.username", username); + credentialProps.setProperty("storage.password", String.valueOf(password)); + credentialProps.store(new FileOutputStream(credentialsFile), "Storage Credentials"); - try { - credentialProps.store(new FileOutputStream(credentialsFile), "Storage Credentials"); + credentialsFile.setReadable(true, false); + credentialsFile.setWritable(true, true); } 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() {
--- a/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/CredentialFinderTest.java Wed Aug 12 13:31:48 2015 -0400 +++ b/setup-command/command/src/test/java/com/redhat/thermostat/setup/command/internal/CredentialFinderTest.java Fri Aug 14 12:15:56 2015 -0400 @@ -46,25 +46,31 @@ import java.nio.file.Files; import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class CredentialFinderTest { - private File systemConfigDir = mock(File.class); - private File userConfigDir = mock(File.class); + private File systemConfigDir; + private File userConfigDir; private File realFile1; private File realFile2; + private File fileToCreate1; + private File fileToCreate2; private CommonPaths paths; @Before public void setUp() throws IOException { - realFile1 = Files.createTempFile("credentialfinder-unit-test", null).toFile(); - realFile1.createNewFile(); + systemConfigDir = Files.createTempDirectory("system-config-dir").toFile(); + userConfigDir = Files.createTempDirectory("user-config-dir").toFile(); + fileToCreate1 = new File(systemConfigDir.toString(), "does-not-exist"); + fileToCreate2 = new File(userConfigDir.toString(), "does-not-exist"); + realFile1 = Files.createTempFile("credentialfinder-unit-test", null).toFile(); realFile2 = Files.createTempFile("credentialfinder-unit-test", null).toFile(); - realFile2.createNewFile(); paths = mock(CommonPaths.class); when(paths.getSystemConfigurationDirectory()).thenReturn(systemConfigDir); @@ -73,12 +79,14 @@ @After public void tearDown() { + systemConfigDir.delete(); + userConfigDir.delete(); realFile1.delete(); realFile2.delete(); } @Test - public void verifyFileFromUserHomeIsUsedIfSystemHomeIsNotUsable() throws Exception { + public void verifyFileFromUserHomeIsUsedIfSystemHomeIsNotUsable() throws IOException { CredentialFinder finder = new CredentialFinder(paths) { @Override File getConfigurationFile(File directory, String name) { @@ -88,7 +96,7 @@ return realFile1; } throw new AssertionError("Unknown test case"); - }; + } }; File config = finder.getConfiguration("web.auth"); @@ -109,7 +117,7 @@ return userFile; } throw new AssertionError("Unknown test case"); - }; + } }; File config = finder.getConfiguration("web.auth"); @@ -127,10 +135,93 @@ return new File("/does-not-exist/really-it-doesn't"); } throw new AssertionError("Unknown test case"); - }; + } }; File config = finder.getConfiguration("web.auth"); assertEquals(realFile1, config); } + + + @Test + public void verifyFileFromSystemHomeIsUsedIfFileDoesNotExist() throws IOException { + CredentialFinder finder = new CredentialFinder(paths) { + @Override + File getConfigurationFile(File directory, String name) { + if (directory == systemConfigDir) { + return fileToCreate1; + } else if (directory == userConfigDir) { + return fileToCreate2; + } + throw new AssertionError("Unknown test case"); + } + }; + + File config = finder.getConfiguration("web.auth"); + assertEquals(fileToCreate1, config); + } + + @Test + public void verifyFileFromUserHomeIsUsedIfSystemHomeIsNotUsableAndFileDoesNotExist() throws IOException { + systemConfigDir.setReadOnly(); + + CredentialFinder finder = new CredentialFinder(paths) { + @Override + File getConfigurationFile(File directory, String name) { + if (directory == systemConfigDir) { + return fileToCreate1; + } else if (directory == userConfigDir) { + return fileToCreate2; + } + throw new AssertionError("Unknown test case"); + } + }; + + File config = finder.getConfiguration("web.auth"); + assertEquals(fileToCreate2, config); + } + + @Test + public void verifyIsNotUsableWhenNotIsFile() throws IOException { + File mockFile = mock(File.class); + when(mockFile.exists()).thenReturn(true); + when(mockFile.isFile()).thenReturn(false); + when(mockFile.canRead()).thenReturn(true); + when(mockFile.canWrite()).thenReturn(true); + CredentialFinder finder = new CredentialFinder(paths); + + assertFalse(finder.isUsable(mockFile)); + } + + @Test + public void verifyIsNotUsableWhenNotCanRead() throws IOException { + File mockFile = mock(File.class); + when(mockFile.exists()).thenReturn(true); + when(mockFile.isFile()).thenReturn(true); + when(mockFile.canRead()).thenReturn(false); + when(mockFile.canWrite()).thenReturn(true); + CredentialFinder finder = new CredentialFinder(paths); + + assertFalse(finder.isUsable(mockFile)); + } + + @Test + public void verifyIsNotUsableWhenNotCanWrite() throws IOException { + File mockFile = mock(File.class); + when(mockFile.exists()).thenReturn(true); + when(mockFile.isFile()).thenReturn(true); + when(mockFile.canRead()).thenReturn(true); + when(mockFile.canWrite()).thenReturn(false); + CredentialFinder finder = new CredentialFinder(paths); + + assertFalse(finder.isUsable(mockFile)); + } + + @Test + public void verifyIsUsableWhenFileDoesNotExistAndHasNoParent() throws IOException { + File fileToCreate = new File("does-not-exist"); + CredentialFinder finder = new CredentialFinder(paths); + + assertTrue(finder.isUsable(fileToCreate)); + } }