From 2993a3eb06e33b7cc5530d612f6f0a2ace0bb1fe Mon Sep 17 00:00:00 2001 From: Elia Renzoni Date: Wed, 18 Nov 2026 16:32:32 +0100 Subject: [PATCH 1/2] RATIS-1654. Add a StorageState for insufficient privilege Signed-off-by: Elia Renzoni --- .../storage/RaftStorageDirectoryImpl.java | 11 +++- .../ratis/server/storage/RaftStorageImpl.java | 7 ++- .../ratis/server/storage/TestRaftStorage.java | 55 +++++++++++++++++++ 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java index 5647e89d3f..cc5218f652 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.AccessDeniedException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -42,7 +43,8 @@ enum StorageState { NON_EXISTENT, NOT_FORMATTED, NO_SPACE, - NORMAL + NORMAL, + INSUFFICIENT_PRIVILEGE } private final File root; // root directory @@ -144,11 +146,14 @@ StorageState analyzeStorage(boolean toLock) throws IOException { } if (!Files.isWritable(root.toPath())) { LOG.warn("The storage directory {} is not writable.", rootPath); - return StorageState.NON_EXISTENT; + return StorageState.INSUFFICIENT_PRIVILEGE; } } catch(SecurityException ex) { LOG.warn("Cannot access storage directory " + rootPath, ex); - return StorageState.NON_EXISTENT; + return StorageState.INSUFFICIENT_PRIVILEGE; + } catch(AccessDeniedException ex) { + LOG.warn("Insufficient privilege to create storage directory " + rootPath, ex); + return StorageState.INSUFFICIENT_PRIVILEGE; } if (toLock) { diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java index 33dc333bba..21886b8c86 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java @@ -57,8 +57,9 @@ public class RaftStorageImpl implements RaftStorage { public void initialize() throws IOException { try { if (startupOption == StartupOption.FORMAT) { - if (storageDir.analyzeStorage(false) == StorageState.NON_EXISTENT) { - throw new IOException("Cannot format " + storageDir); + final StorageState analyzed = storageDir.analyzeStorage(false); + if (analyzed == StorageState.NON_EXISTENT || analyzed == StorageState.INSUFFICIENT_PRIVILEGE) { + throw new IOException("Cannot format " + storageDir + ": " + analyzed); } storageDir.lock(); format(); @@ -108,7 +109,7 @@ private StorageState analyzeAndRecoverStorage(boolean toLock) throws IOException StorageState storageState = storageDir.analyzeStorage(toLock); // Existence of raft-meta.tmp means the change of votedFor/term has not // been committed. Thus we should delete the tmp file. - if (storageState != StorageState.NON_EXISTENT) { + if (storageState != StorageState.NON_EXISTENT && storageState != StorageState.INSUFFICIENT_PRIVILEGE) { cleanMetaTmpFile(); } if (storageState == StorageState.NORMAL) { diff --git a/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java b/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java index 5c31bb7aaf..f08a644b7e 100644 --- a/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java +++ b/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java @@ -43,6 +43,7 @@ import org.apache.ratis.util.SizeInBytes; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -746,4 +747,58 @@ public void testNotEnoughSpace() throws IOException { StorageState state = sd.analyzeStorage(false); Assertions.assertEquals(StorageState.NO_SPACE, state); } + + /** The storage directory exists but is not writable. */ + @Test + public void testInsufficientPrivilegeExistingDir() throws IOException { + FileUtils.createDirectories(storageDir); + Assertions.assertTrue(storageDir.setWritable(false)); + try { + assumeNotWritable(storageDir); + final RaftStorageDirectoryImpl sd = new RaftStorageDirectoryImpl(storageDir, SizeInBytes.ZERO); + Assertions.assertEquals(StorageState.INSUFFICIENT_PRIVILEGE, sd.analyzeStorage(false)); + } finally { + Assertions.assertTrue(storageDir.setWritable(true)); + } + } + + /** The storage directory does not exist and its parent is not writable, so it cannot be created. */ + @Test + public void testInsufficientPrivilegeCannotCreateDir() throws IOException { + final File parent = storageDir.getParentFile(); + FileUtils.createDirectories(parent); + Assertions.assertFalse(storageDir.exists()); + Assertions.assertTrue(parent.setWritable(false)); + try { + assumeNotWritable(parent); + final RaftStorageDirectoryImpl sd = new RaftStorageDirectoryImpl(storageDir, SizeInBytes.ZERO); + Assertions.assertEquals(StorageState.INSUFFICIENT_PRIVILEGE, sd.analyzeStorage(false)); + Assertions.assertFalse(storageDir.exists()); + } finally { + Assertions.assertTrue(parent.setWritable(true)); + } + } + + /** RaftStorage initialization should fail with a message containing INSUFFICIENT_PRIVILEGE. */ + @Test + public void testInsufficientPrivilegeInitialize() throws IOException { + FileUtils.createDirectories(storageDir); + Assertions.assertTrue(storageDir.setWritable(false)); + try { + assumeNotWritable(storageDir); + final IOException e = Assertions.assertThrows(IOException.class, () -> newRaftStorage(storageDir)); + Assertions.assertTrue(e.getMessage().contains(StorageState.INSUFFICIENT_PRIVILEGE.name()), e.getMessage()); + + final IOException f = Assertions.assertThrows(IOException.class, () -> formatRaftStorage(storageDir)); + Assertions.assertTrue(f.getMessage().contains("Cannot format"), f.getMessage()); + } finally { + Assertions.assertTrue(storageDir.setWritable(true)); + } + } + + /** Skip the test if permissions are not enforced, e.g. when running as root. */ + private static void assumeNotWritable(File dir) { + Assumptions.assumeFalse(Files.isWritable(dir.toPath()), + () -> "Permissions are not enforced for " + dir + " (running as root?)"); + } } From 422172f6b80d8c12a1147e9017a520e081839b42 Mon Sep 17 00:00:00 2001 From: Elia Renzoni Date: Thu, 19 Nov 2026 07:42:02 +0100 Subject: [PATCH 2/2] refactor: throw exceptions instead of enums Signed-off-by: Elia Renzoni --- .../storage/RaftStorageDirectoryImpl.java | 19 ++++++---------- .../ratis/server/storage/RaftStorageImpl.java | 9 ++------ .../ratis/server/storage/TestRaftStorage.java | 22 +++++++------------ 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java index cc5218f652..e1af81816c 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectoryImpl.java @@ -27,6 +27,7 @@ import java.nio.file.AccessDeniedException; import java.nio.file.DirectoryStream; import java.nio.file.Files; +import java.nio.file.NotDirectoryException; import java.nio.file.Path; import java.util.Objects; @@ -40,11 +41,9 @@ class RaftStorageDirectoryImpl implements RaftStorageDirectory { enum StorageState { UNINITIALIZED, - NON_EXISTENT, NOT_FORMATTED, NO_SPACE, - NORMAL, - INSUFFICIENT_PRIVILEGE + NORMAL } private final File root; // root directory @@ -141,19 +140,15 @@ StorageState analyzeStorage(boolean toLock) throws IOException { } // or is inaccessible if (!root.isDirectory()) { - LOG.warn("{} is not a directory", rootPath); - return StorageState.NON_EXISTENT; + throw new NotDirectoryException(rootPath); } if (!Files.isWritable(root.toPath())) { - LOG.warn("The storage directory {} is not writable.", rootPath); - return StorageState.INSUFFICIENT_PRIVILEGE; + throw new AccessDeniedException(rootPath, null, "not writable"); } } catch(SecurityException ex) { - LOG.warn("Cannot access storage directory " + rootPath, ex); - return StorageState.INSUFFICIENT_PRIVILEGE; - } catch(AccessDeniedException ex) { - LOG.warn("Insufficient privilege to create storage directory " + rootPath, ex); - return StorageState.INSUFFICIENT_PRIVILEGE; + final AccessDeniedException ade = new AccessDeniedException(rootPath); + ade.initCause(ex); + throw ade; } if (toLock) { diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java index 21886b8c86..44ea3c31f9 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageImpl.java @@ -57,10 +57,7 @@ public class RaftStorageImpl implements RaftStorage { public void initialize() throws IOException { try { if (startupOption == StartupOption.FORMAT) { - final StorageState analyzed = storageDir.analyzeStorage(false); - if (analyzed == StorageState.NON_EXISTENT || analyzed == StorageState.INSUFFICIENT_PRIVILEGE) { - throw new IOException("Cannot format " + storageDir + ": " + analyzed); - } + storageDir.analyzeStorage(false); storageDir.lock(); format(); state = storageDir.analyzeStorage(false); @@ -109,9 +106,7 @@ private StorageState analyzeAndRecoverStorage(boolean toLock) throws IOException StorageState storageState = storageDir.analyzeStorage(toLock); // Existence of raft-meta.tmp means the change of votedFor/term has not // been committed. Thus we should delete the tmp file. - if (storageState != StorageState.NON_EXISTENT && storageState != StorageState.INSUFFICIENT_PRIVILEGE) { - cleanMetaTmpFile(); - } + cleanMetaTmpFile(); if (storageState == StorageState.NORMAL) { final File f = storageDir.getMetaFile(); if (!f.exists()) { diff --git a/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java b/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java index f08a644b7e..3e15535c8a 100644 --- a/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java +++ b/ratis-test/src/test/java/org/apache/ratis/server/storage/TestRaftStorage.java @@ -51,7 +51,9 @@ import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.AccessDeniedException; import java.nio.file.Files; +import java.nio.file.NotDirectoryException; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; @@ -119,12 +121,7 @@ public void testNotExistent() throws IOException { storage.close(); FileUtils.deleteFully(storageDir); Assertions.assertTrue(storageDir.createNewFile()); - try (RaftStorage ignored = newRaftStorage(storageDir)) { - Assertions.fail(); - } catch (IOException e) { - Assertions.assertTrue( - e.getMessage().contains(StorageState.NON_EXISTENT.name())); - } + Assertions.assertThrows(NotDirectoryException.class, () -> newRaftStorage(storageDir)); } /** @@ -756,7 +753,7 @@ public void testInsufficientPrivilegeExistingDir() throws IOException { try { assumeNotWritable(storageDir); final RaftStorageDirectoryImpl sd = new RaftStorageDirectoryImpl(storageDir, SizeInBytes.ZERO); - Assertions.assertEquals(StorageState.INSUFFICIENT_PRIVILEGE, sd.analyzeStorage(false)); + Assertions.assertThrows(AccessDeniedException.class, () -> sd.analyzeStorage(false)); } finally { Assertions.assertTrue(storageDir.setWritable(true)); } @@ -772,25 +769,22 @@ public void testInsufficientPrivilegeCannotCreateDir() throws IOException { try { assumeNotWritable(parent); final RaftStorageDirectoryImpl sd = new RaftStorageDirectoryImpl(storageDir, SizeInBytes.ZERO); - Assertions.assertEquals(StorageState.INSUFFICIENT_PRIVILEGE, sd.analyzeStorage(false)); + Assertions.assertThrows(AccessDeniedException.class, () -> sd.analyzeStorage(false)); Assertions.assertFalse(storageDir.exists()); } finally { Assertions.assertTrue(parent.setWritable(true)); } } - /** RaftStorage initialization should fail with a message containing INSUFFICIENT_PRIVILEGE. */ + /** RaftStorage initialization (RECOVER and FORMAT) should fail with AccessDeniedException. */ @Test public void testInsufficientPrivilegeInitialize() throws IOException { FileUtils.createDirectories(storageDir); Assertions.assertTrue(storageDir.setWritable(false)); try { assumeNotWritable(storageDir); - final IOException e = Assertions.assertThrows(IOException.class, () -> newRaftStorage(storageDir)); - Assertions.assertTrue(e.getMessage().contains(StorageState.INSUFFICIENT_PRIVILEGE.name()), e.getMessage()); - - final IOException f = Assertions.assertThrows(IOException.class, () -> formatRaftStorage(storageDir)); - Assertions.assertTrue(f.getMessage().contains("Cannot format"), f.getMessage()); + Assertions.assertThrows(AccessDeniedException.class, () -> newRaftStorage(storageDir)); + Assertions.assertThrows(AccessDeniedException.class, () -> formatRaftStorage(storageDir)); } finally { Assertions.assertTrue(storageDir.setWritable(true)); }