Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -9,13 +9,17 @@

package org.elasticsearch.entitlement.runtime.policy;

import org.elasticsearch.core.PathUtils;

import java.nio.file.Path;
import java.util.stream.Stream;

/**
* Resolves paths for known directories checked by entitlements.
*/
public interface PathLookup {
Class<?> DEFAULT_FILESYSTEM_CLASS = PathUtils.getDefaultFileSystem().getClass();

enum BaseDir {
USER_HOME,
CONFIG,
Expand All @@ -37,4 +41,6 @@ enum BaseDir {
* paths of the given {@code baseDir}.
*/
Stream<Path> resolveSettingPaths(BaseDir baseDir, String settingName);

boolean isPathOnDefaultFilesystem(Path path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ public Stream<Path> resolveSettingPaths(BaseDir baseDir, String settingName) {
.toList();
return getBaseDirPaths(baseDir).flatMap(path -> relativePaths.stream().map(path::resolve));
}

@Override
public boolean isPathOnDefaultFilesystem(Path path) {
return path.getFileSystem().getClass() == DEFAULT_FILESYSTEM_CLASS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

package org.elasticsearch.entitlement.runtime.policy;

import org.elasticsearch.core.PathUtils;
import org.elasticsearch.core.Strings;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.entitlement.instrumentation.InstrumentationService;
Expand Down Expand Up @@ -58,7 +57,7 @@
*/
@SuppressForbidden(reason = "Explicitly checking APIs that are forbidden")
public class PolicyCheckerImpl implements PolicyChecker {
static final Class<?> DEFAULT_FILESYSTEM_CLASS = PathUtils.getDefaultFileSystem().getClass();

protected final Set<Package> suppressFailureLogPackages;
/**
* Frames originating from this module are ignored in the permission logic.
Expand All @@ -81,15 +80,14 @@ public PolicyCheckerImpl(
this.pathLookup = pathLookup;
}

private static boolean isPathOnDefaultFilesystem(Path path) {
var pathFileSystemClass = path.getFileSystem().getClass();
if (path.getFileSystem().getClass() != DEFAULT_FILESYSTEM_CLASS) {
private boolean isPathOnDefaultFilesystem(Path path) {
if (pathLookup.isPathOnDefaultFilesystem(path) == false) {
PolicyManager.generalLogger.trace(
() -> Strings.format(
"File entitlement trivially allowed: path [%s] is for a different FileSystem class [%s], default is [%s]",
path.toString(),
pathFileSystemClass.getName(),
DEFAULT_FILESYSTEM_CLASS.getName()
path.getFileSystem().getClass().getName(),
PathLookup.DEFAULT_FILESYSTEM_CLASS.getName()
)
);
return false;
Expand Down Expand Up @@ -217,7 +215,7 @@ public void checkFileRead(Class<?> callerClass, Path path) {

@Override
public void checkFileRead(Class<?> callerClass, Path path, boolean followLinks) throws NoSuchFileException {
if (PolicyCheckerImpl.isPathOnDefaultFilesystem(path) == false) {
if (isPathOnDefaultFilesystem(path) == false) {
return;
}
var requestingClass = requestingClass(callerClass);
Expand Down Expand Up @@ -265,7 +263,7 @@ public void checkFileWrite(Class<?> callerClass, File file) {

@Override
public void checkFileWrite(Class<?> callerClass, Path path) {
if (PolicyCheckerImpl.isPathOnDefaultFilesystem(path) == false) {
if (isPathOnDefaultFilesystem(path) == false) {
return;
}
var requestingClass = requestingClass(callerClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

package org.elasticsearch.entitlement.runtime.policy;

import org.apache.lucene.tests.mockfile.FilterFileSystem;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -37,4 +39,14 @@ public Stream<Path> resolveSettingPaths(BaseDir baseDir, String settingName) {
return Stream.empty();
}

@Override
public boolean isPathOnDefaultFilesystem(Path path) {
var fileSystem = path.getFileSystem();
if (fileSystem.getClass() != DEFAULT_FILESYSTEM_CLASS) {
while (fileSystem instanceof FilterFileSystem ffs) {
fileSystem = ffs.getDelegate();
}
}
return fileSystem.getClass() == DEFAULT_FILESYSTEM_CLASS;
}
}