Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

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.NotDirectoryException;
import java.nio.file.Path;
import java.util.Objects;

Expand All @@ -39,7 +41,6 @@ class RaftStorageDirectoryImpl implements RaftStorageDirectory {

enum StorageState {
UNINITIALIZED,
NON_EXISTENT,
NOT_FORMATTED,
NO_SPACE,
NORMAL
Expand Down Expand Up @@ -139,16 +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.NON_EXISTENT;
throw new AccessDeniedException(rootPath, null, "not writable");
}
} catch(SecurityException ex) {
LOG.warn("Cannot access storage directory " + rootPath, ex);
return StorageState.NON_EXISTENT;
final AccessDeniedException ade = new AccessDeniedException(rootPath);
ade.initCause(ex);
throw ade;
}

if (toLock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ 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);
}
storageDir.analyzeStorage(false);
storageDir.lock();
format();
state = storageDir.analyzeStorage(false);
Expand Down Expand Up @@ -108,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) {
cleanMetaTmpFile();
}
cleanMetaTmpFile();
if (storageState == StorageState.NORMAL) {
final File f = storageDir.getMetaFile();
if (!f.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@
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;

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;
Expand Down Expand Up @@ -118,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));
}

/**
Expand Down Expand Up @@ -746,4 +744,55 @@ 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.assertThrows(AccessDeniedException.class, () -> 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.assertThrows(AccessDeniedException.class, () -> sd.analyzeStorage(false));
Assertions.assertFalse(storageDir.exists());
} finally {
Assertions.assertTrue(parent.setWritable(true));
}
}

/** 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);
Assertions.assertThrows(AccessDeniedException.class, () -> newRaftStorage(storageDir));
Assertions.assertThrows(AccessDeniedException.class, () -> formatRaftStorage(storageDir));
} 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?)");
}
}