PropertiesEncryption { public void encryptAndSave(String input, Set<String> mustBeEncrypted) { modifyAndSave(input, mustBeEncrypted, this::encryptIfNot); }  void encryptAndSave(String input, Set<String> mustBeEncrypted); void decryptAndSave(String input, Set<String> mustBeDecrypted);  }
@Test public void shouldEncryptAndSaveAllOccurrences() throws Exception { String propertyKey = "admin.password"; String propertyValue = "5ecr3t"; String propertyEncodedValue = "JP6lC6hVeu3wRZA1Tzigyg=="; String mongoPropName = "mongodb.uri"; String mongoPropValue = "mongodb: String mongoPropValueEncrypted = "mongodb: Path tempFile = Files.createTempFile("dataprep-PropertiesEncryptionTest.", ".properties"); try (BufferedWriter bufferedWriter = Files.newBufferedWriter(tempFile, Charsets.UTF_8)) { for (int i = 0; i < 5; i++) { bufferedWriter.write(propertyKey + "=" + propertyValue); bufferedWriter.newLine(); } bufferedWriter.write(mongoPropName + "=" + mongoPropValue); } new PropertiesEncryption().encryptAndSave(tempFile.toString(), Sets.newHashSet(propertyKey, mongoPropName)); Properties properties = new Properties(); properties.load(new FileInputStream(tempFile.toFile())); assertEquals(propertyEncodedValue, properties.getProperty(propertyKey)); assertEquals(mongoPropValueEncrypted, properties.getProperty(mongoPropName)); }
@Test public void shouldNotEncryptCommentedProperties() throws Exception { String commentedLines = "# admin.password = administrator password"; Path tempFile = Files.createTempFile("dataprep-PropertiesEncryptionTest.", ".properties"); try (BufferedWriter bufferedWriter = Files.newBufferedWriter(tempFile, Charsets.UTF_8)) { bufferedWriter.write("# file.password = the file password"); bufferedWriter.newLine(); bufferedWriter.write("file = /tmp"); bufferedWriter.newLine(); } new PropertiesEncryption().encryptAndSave(tempFile.toString(), Sets.newHashSet("file.password")); try (BufferedReader reader = Files.newBufferedReader(tempFile)) { String line = reader.readLine(); assertEquals("# file.password = the file password", line); line = reader.readLine(); assertEquals("file = /tmp", line); } }
@Test public void encryptAndSave_doesNotBreakLayout() throws Exception { Path tempFile = Files.createTempFile("dataprep-PropertiesEncryptionTest.", ".properties"); try (InputStream refInStream = getClass().getResourceAsStream("keep-layout-test-in.properties")) { Files.copy(refInStream, tempFile, StandardCopyOption.REPLACE_EXISTING); } new PropertiesEncryption().encryptAndSave(tempFile.toString(), Sets.newHashSet("admin.password")); assertEquals(Resources.toString(getClass().getResource("keep-layout-test-expected.properties"), Charsets.UTF_8), com.google.common.io.Files.toString(tempFile.toFile(), Charsets.UTF_8)); }
@Test public void shouldEncryptAllKeysButMissingOnes() throws Exception { String propertyKey = "admin.password"; String missingPropertyKeyOne = "missingOne"; String missingPropertyKeyTwo = "missingTwo"; String propertyValue = "5ecr3t"; String propertyEncodedValue = "JP6lC6hVeu3wRZA1Tzigyg=="; Path tempFile = Files.createTempFile("dataprep-PropertiesEncryptionTest.", ".properties"); try (BufferedWriter bufferedWriter = Files.newBufferedWriter(tempFile, Charsets.UTF_8)) { for (int i = 0; i < 5; i++) { bufferedWriter.write(propertyKey + "=" + propertyValue); bufferedWriter.newLine(); } } new PropertiesEncryption().encryptAndSave(tempFile.toString(), Sets.newHashSet(propertyKey, missingPropertyKeyOne, missingPropertyKeyTwo)); final String expectedLine = propertyKey + "=" + propertyEncodedValue; verifyContent(expectedLine, tempFile); }