This is an automated email from the ASF dual-hosted git repository.

olli pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-crypto.git

commit c6799957c7b3cb2e80f5780dd89524c77cce0b98
Author: Oliver Lietz <o...@apache.org>
AuthorDate: Sun Jun 20 12:12:33 2021 +0200

    SLING-10505 Improve exceptions for nonreadable password files
---
 .../crypto/internal/FilePasswordProvider.java      | 18 ++++++++-----
 .../crypto/internal/FilePasswordProviderTest.java  | 31 ++++++++++++++++++++++
 2 files changed, 42 insertions(+), 7 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/commons/crypto/internal/FilePasswordProvider.java
 
b/src/main/java/org/apache/sling/commons/crypto/internal/FilePasswordProvider.java
index 92d59a6..f4c785e 100644
--- 
a/src/main/java/org/apache/sling/commons/crypto/internal/FilePasswordProvider.java
+++ 
b/src/main/java/org/apache/sling/commons/crypto/internal/FilePasswordProvider.java
@@ -63,14 +63,14 @@ public class FilePasswordProvider implements 
PasswordProvider {
     protected void activate(final FilePasswordProviderConfiguration 
configuration) throws IOException {
         logger.debug("activating");
         this.configuration = configuration;
-        checkConfiguration();
+        checkConfiguration(configuration);
     }
 
     @Modified
     protected void modified(final FilePasswordProviderConfiguration 
configuration) throws IOException {
         logger.debug("modifying");
         this.configuration = configuration;
-        checkConfiguration();
+        checkConfiguration(configuration);
     }
 
     @Deactivate
@@ -80,6 +80,7 @@ public class FilePasswordProvider implements PasswordProvider 
{
 
     private char[] readPassword(final String path, final boolean 
fixPosixNewline) throws IOException {
         final File file = new File(path);
+        checkPasswordFile(file);
         final char[] buffer = new char[(int) file.length()];
         try (final BufferedReader reader = 
Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
             final int size = reader.read(buffer);
@@ -96,11 +97,14 @@ public class FilePasswordProvider implements 
PasswordProvider {
         }
     }
 
-    private void checkConfiguration() throws IOException {
-        final String path = configuration.path();
-        final File file = new File(path);
+    private void checkConfiguration(final FilePasswordProviderConfiguration 
configuration) throws IOException {
+        final File file = new File(configuration.path());
+        checkPasswordFile(file);
+    }
+
+    private void checkPasswordFile(final File file) throws IOException {
         if (!file.canRead()) {
-            final String message = String.format("Unable to read password file 
'%s'", path);
+            final String message = String.format("Unable to read password file 
'%s'", file.getAbsolutePath());
             throw new IOException(message);
         }
     }
@@ -112,7 +116,7 @@ public class FilePasswordProvider implements 
PasswordProvider {
         try {
             return readPassword(configuration.path(), 
configuration.fix_posixNewline());
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new RuntimeException(e.getMessage(), e);
         }
     }
 
diff --git 
a/src/test/java/org/apache/sling/commons/crypto/internal/FilePasswordProviderTest.java
 
b/src/test/java/org/apache/sling/commons/crypto/internal/FilePasswordProviderTest.java
index bf38ed2..1ce0cf0 100644
--- 
a/src/test/java/org/apache/sling/commons/crypto/internal/FilePasswordProviderTest.java
+++ 
b/src/test/java/org/apache/sling/commons/crypto/internal/FilePasswordProviderTest.java
@@ -18,7 +18,9 @@
  */
 package org.apache.sling.commons.crypto.internal;
 
+import java.io.File;
 import java.io.IOException;
+import java.util.UUID;
 
 import org.junit.Rule;
 import org.junit.Test;
@@ -118,4 +120,33 @@ public class FilePasswordProviderTest {
         assertThat(provider.getPassword()).isEqualTo(PASSWORD_ASCII_NEWLINE);
     }
 
+    @Test
+    public void testPasswordFileNotReadableDuringConfigurationCheck() throws 
IOException {
+        final FilePasswordProvider provider = new FilePasswordProvider();
+        final String path = String.format("%s%s", 
System.getProperty("java.io.tmpdir"), UUID.randomUUID());
+        final FilePasswordProviderConfiguration configuration = 
mock(FilePasswordProviderConfiguration.class);
+        when(configuration.path()).thenReturn(path);
+        when(configuration.fix_posixNewline()).thenReturn(false);
+        exception.expect(IOException.class);
+        final String message = String.format("Unable to read password file 
'%s'", path);
+        exception.expectMessage(message);
+        provider.activate(configuration);
+    }
+
+    @Test
+    public void testPasswordFileNotReadableAfterConfigurationCheck() throws 
IOException {
+        final FilePasswordProvider provider = new FilePasswordProvider();
+        final File file = File.createTempFile(UUID.randomUUID().toString(), 
null);
+        final String path = file.getPath();
+        final FilePasswordProviderConfiguration configuration = 
mock(FilePasswordProviderConfiguration.class);
+        when(configuration.path()).thenReturn(path);
+        when(configuration.fix_posixNewline()).thenReturn(false);
+        provider.activate(configuration);
+        file.delete();
+        exception.expect(RuntimeException.class);
+        final String message = String.format("Unable to read password file 
'%s'", path);
+        exception.expectMessage(message);
+        provider.getPassword();
+    }
+
 }

Reply via email to