SLING-10505 Improve exceptions for nonreadable password files
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 @@
     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 @@
 
     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 @@
         }
     }
 
-    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 @@
         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 @@
         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();
+    }
+
 }