Skip to content

Commit bd242cc

Browse files
authored
[Entitlements] Instrumentation for FileSystemProvider (#122232)
1 parent 2c846e7 commit bd242cc

File tree

8 files changed

+729
-18
lines changed

8 files changed

+729
-18
lines changed

libs/entitlement/bridge/src/main/java/org/elasticsearch/entitlement/bridge/EntitlementChecker.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.net.Socket;
3636
import java.net.SocketAddress;
3737
import java.net.SocketImplFactory;
38+
import java.net.URI;
3839
import java.net.URL;
3940
import java.net.URLStreamHandler;
4041
import java.net.URLStreamHandlerFactory;
@@ -50,17 +51,24 @@
5051
import java.nio.channels.SocketChannel;
5152
import java.nio.channels.spi.SelectorProvider;
5253
import java.nio.charset.Charset;
54+
import java.nio.file.AccessMode;
55+
import java.nio.file.CopyOption;
56+
import java.nio.file.DirectoryStream;
5357
import java.nio.file.FileStore;
5458
import java.nio.file.LinkOption;
5559
import java.nio.file.OpenOption;
5660
import java.nio.file.Path;
61+
import java.nio.file.attribute.FileAttribute;
5762
import java.nio.file.attribute.UserPrincipal;
5863
import java.nio.file.spi.FileSystemProvider;
5964
import java.security.cert.CertStoreParameters;
6065
import java.util.List;
6166
import java.util.Locale;
67+
import java.util.Map;
6268
import java.util.Properties;
69+
import java.util.Set;
6370
import java.util.TimeZone;
71+
import java.util.concurrent.ExecutorService;
6472
import java.util.function.Consumer;
6573

6674
import javax.net.ssl.HostnameVerifier;
@@ -553,8 +561,79 @@ public interface EntitlementChecker {
553561
void check$java_nio_file_Files$$setOwner(Class<?> callerClass, Path path, UserPrincipal principal);
554562

555563
// file system providers
564+
void check$java_nio_file_spi_FileSystemProvider$(Class<?> callerClass);
565+
566+
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, URI uri, Map<String, ?> env);
567+
568+
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, Path path, Map<String, ?> env);
569+
556570
void checkNewInputStream(Class<?> callerClass, FileSystemProvider that, Path path, OpenOption... options);
557571

572+
void checkNewOutputStream(Class<?> callerClass, FileSystemProvider that, Path path, OpenOption... options);
573+
574+
void checkNewFileChannel(
575+
Class<?> callerClass,
576+
FileSystemProvider that,
577+
Path path,
578+
Set<? extends OpenOption> options,
579+
FileAttribute<?>... attrs
580+
);
581+
582+
void checkNewAsynchronousFileChannel(
583+
Class<?> callerClass,
584+
FileSystemProvider that,
585+
Path path,
586+
Set<? extends OpenOption> options,
587+
ExecutorService executor,
588+
FileAttribute<?>... attrs
589+
);
590+
591+
void checkNewByteChannel(
592+
Class<?> callerClass,
593+
FileSystemProvider that,
594+
Path path,
595+
Set<? extends OpenOption> options,
596+
FileAttribute<?>... attrs
597+
);
598+
599+
void checkNewDirectoryStream(Class<?> callerClass, FileSystemProvider that, Path dir, DirectoryStream.Filter<? super Path> filter);
600+
601+
void checkCreateDirectory(Class<?> callerClass, FileSystemProvider that, Path dir, FileAttribute<?>... attrs);
602+
603+
void checkCreateSymbolicLink(Class<?> callerClass, FileSystemProvider that, Path link, Path target, FileAttribute<?>... attrs);
604+
605+
void checkCreateLink(Class<?> callerClass, FileSystemProvider that, Path link, Path existing);
606+
607+
void checkDelete(Class<?> callerClass, FileSystemProvider that, Path path);
608+
609+
void checkDeleteIfExists(Class<?> callerClass, FileSystemProvider that, Path path);
610+
611+
void checkReadSymbolicLink(Class<?> callerClass, FileSystemProvider that, Path link);
612+
613+
void checkCopy(Class<?> callerClass, FileSystemProvider that, Path source, Path target, CopyOption... options);
614+
615+
void checkMove(Class<?> callerClass, FileSystemProvider that, Path source, Path target, CopyOption... options);
616+
617+
void checkIsSameFile(Class<?> callerClass, FileSystemProvider that, Path path, Path path2);
618+
619+
void checkIsHidden(Class<?> callerClass, FileSystemProvider that, Path path);
620+
621+
void checkGetFileStore(Class<?> callerClass, FileSystemProvider that, Path path);
622+
623+
void checkCheckAccess(Class<?> callerClass, FileSystemProvider that, Path path, AccessMode... modes);
624+
625+
void checkGetFileAttributeView(Class<?> callerClass, FileSystemProvider that, Path path, Class<?> type, LinkOption... options);
626+
627+
void checkReadAttributes(Class<?> callerClass, FileSystemProvider that, Path path, Class<?> type, LinkOption... options);
628+
629+
void checkReadAttributes(Class<?> callerClass, FileSystemProvider that, Path path, String attributes, LinkOption... options);
630+
631+
void checkReadAttributesIfExists(Class<?> callerClass, FileSystemProvider that, Path path, Class<?> type, LinkOption... options);
632+
633+
void checkSetAttribute(Class<?> callerClass, FileSystemProvider that, Path path, String attribute, Object value, LinkOption... options);
634+
635+
void checkExists(Class<?> callerClass, FileSystemProvider that, Path path, LinkOption... options);
636+
558637
// file store
559638
void checkGetFileStoreAttributeView(Class<?> callerClass, FileStore that, Class<?> type);
560639

libs/entitlement/qa/entitled-plugin/src/main/java/org/elasticsearch/entitlement/qa/entitled/EntitledActions.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,26 @@
1414
import java.io.IOException;
1515
import java.nio.file.Files;
1616
import java.nio.file.Path;
17+
import java.nio.file.Paths;
1718
import java.nio.file.attribute.UserPrincipal;
19+
import java.security.SecureRandom;
1820

21+
@SuppressForbidden(reason = "Exposes forbidden APIs for testing purposes")
1922
public final class EntitledActions {
2023
private EntitledActions() {}
2124

22-
@SuppressForbidden(reason = "Exposes forbidden APIs for testing purposes")
25+
private static final SecureRandom random = new SecureRandom();
26+
27+
private static final Path testRootDir = Paths.get(System.getProperty("es.entitlements.testdir"));
28+
29+
private static Path readDir() {
30+
return testRootDir.resolve("read_dir");
31+
}
32+
33+
private static Path readWriteDir() {
34+
return testRootDir.resolve("read_write_dir");
35+
}
36+
2337
static void System_clearProperty(String key) {
2438
System.clearProperty(key);
2539
}
@@ -31,4 +45,20 @@ public static UserPrincipal getFileOwner(Path path) throws IOException {
3145
public static void createFile(Path path) throws IOException {
3246
Files.createFile(path);
3347
}
48+
49+
public static Path createTempFileForRead() throws IOException {
50+
return Files.createFile(readDir().resolve("entitlements-" + random.nextLong() + ".tmp"));
51+
}
52+
53+
public static Path createTempFileForWrite() throws IOException {
54+
return Files.createFile(readWriteDir().resolve("entitlements-" + random.nextLong() + ".tmp"));
55+
}
56+
57+
public static Path createTempDirectoryForWrite() throws IOException {
58+
return Files.createDirectory(readWriteDir().resolve("entitlements-dir-" + random.nextLong()));
59+
}
60+
61+
public static Path createTempSymbolicLink() throws IOException {
62+
return Files.createSymbolicLink(readDir().resolve("entitlements-link-" + random.nextLong()), readWriteDir());
63+
}
3464
}

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/DummyImplementations.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,32 @@
2323
import java.net.SocketAddress;
2424
import java.net.SocketException;
2525
import java.net.SocketImpl;
26+
import java.net.URI;
2627
import java.nio.channels.AsynchronousChannelGroup;
2728
import java.nio.channels.AsynchronousServerSocketChannel;
2829
import java.nio.channels.AsynchronousSocketChannel;
2930
import java.nio.channels.DatagramChannel;
3031
import java.nio.channels.Pipe;
32+
import java.nio.channels.SeekableByteChannel;
3133
import java.nio.channels.ServerSocketChannel;
3234
import java.nio.channels.SocketChannel;
3335
import java.nio.channels.spi.AbstractSelector;
3436
import java.nio.channels.spi.AsynchronousChannelProvider;
3537
import java.nio.channels.spi.SelectorProvider;
3638
import java.nio.charset.Charset;
3739
import java.nio.charset.spi.CharsetProvider;
40+
import java.nio.file.AccessMode;
41+
import java.nio.file.CopyOption;
42+
import java.nio.file.DirectoryStream;
43+
import java.nio.file.FileStore;
44+
import java.nio.file.FileSystem;
45+
import java.nio.file.LinkOption;
46+
import java.nio.file.OpenOption;
47+
import java.nio.file.Path;
48+
import java.nio.file.attribute.BasicFileAttributes;
49+
import java.nio.file.attribute.FileAttribute;
50+
import java.nio.file.attribute.FileAttributeView;
51+
import java.nio.file.spi.FileSystemProvider;
3852
import java.security.cert.Certificate;
3953
import java.text.BreakIterator;
4054
import java.text.Collator;
@@ -51,6 +65,7 @@
5165
import java.util.Iterator;
5266
import java.util.Locale;
5367
import java.util.Map;
68+
import java.util.Set;
5469
import java.util.concurrent.ExecutorService;
5570
import java.util.concurrent.ThreadFactory;
5671
import java.util.spi.CalendarDataProvider;
@@ -568,4 +583,97 @@ public Charset charsetForName(String charsetName) {
568583
return null;
569584
}
570585
}
586+
587+
static class DummyFileSystemProvider extends FileSystemProvider {
588+
@Override
589+
public String getScheme() {
590+
return "";
591+
}
592+
593+
@Override
594+
public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
595+
return null;
596+
}
597+
598+
@Override
599+
public FileSystem getFileSystem(URI uri) {
600+
return null;
601+
}
602+
603+
@Override
604+
public Path getPath(URI uri) {
605+
return null;
606+
}
607+
608+
@Override
609+
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)
610+
throws IOException {
611+
return null;
612+
}
613+
614+
@Override
615+
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
616+
return null;
617+
}
618+
619+
@Override
620+
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
621+
622+
}
623+
624+
@Override
625+
public void delete(Path path) throws IOException {
626+
627+
}
628+
629+
@Override
630+
public void copy(Path source, Path target, CopyOption... options) throws IOException {
631+
632+
}
633+
634+
@Override
635+
public void move(Path source, Path target, CopyOption... options) throws IOException {
636+
637+
}
638+
639+
@Override
640+
public boolean isSameFile(Path path, Path path2) throws IOException {
641+
return false;
642+
}
643+
644+
@Override
645+
public boolean isHidden(Path path) throws IOException {
646+
return false;
647+
}
648+
649+
@Override
650+
public FileStore getFileStore(Path path) throws IOException {
651+
return null;
652+
}
653+
654+
@Override
655+
public void checkAccess(Path path, AccessMode... modes) throws IOException {
656+
657+
}
658+
659+
@Override
660+
public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
661+
return null;
662+
}
663+
664+
@Override
665+
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
666+
return null;
667+
}
668+
669+
@Override
670+
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException {
671+
return Map.of();
672+
}
673+
674+
@Override
675+
public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
676+
677+
}
678+
}
571679
}

0 commit comments

Comments
 (0)