Skip to content

Commit cc4d048

Browse files
committed
[Entitlements] Instrumentation for FileSystemProvider (elastic#122232)
1 parent ed915d3 commit cc4d048

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
@@ -28,6 +28,7 @@
2828
import java.net.Socket;
2929
import java.net.SocketAddress;
3030
import java.net.SocketImplFactory;
31+
import java.net.URI;
3132
import java.net.URL;
3233
import java.net.URLStreamHandler;
3334
import java.net.URLStreamHandlerFactory;
@@ -43,17 +44,24 @@
4344
import java.nio.channels.SocketChannel;
4445
import java.nio.channels.spi.SelectorProvider;
4546
import java.nio.charset.Charset;
47+
import java.nio.file.AccessMode;
48+
import java.nio.file.CopyOption;
49+
import java.nio.file.DirectoryStream;
4650
import java.nio.file.FileStore;
4751
import java.nio.file.LinkOption;
4852
import java.nio.file.OpenOption;
4953
import java.nio.file.Path;
54+
import java.nio.file.attribute.FileAttribute;
5055
import java.nio.file.attribute.UserPrincipal;
5156
import java.nio.file.spi.FileSystemProvider;
5257
import java.security.cert.CertStoreParameters;
5358
import java.util.List;
5459
import java.util.Locale;
60+
import java.util.Map;
5561
import java.util.Properties;
62+
import java.util.Set;
5663
import java.util.TimeZone;
64+
import java.util.concurrent.ExecutorService;
5765

5866
import javax.net.ssl.HostnameVerifier;
5967
import javax.net.ssl.HttpsURLConnection;
@@ -491,8 +499,79 @@ public interface EntitlementChecker {
491499
void check$java_nio_file_Files$$setOwner(Class<?> callerClass, Path path, UserPrincipal principal);
492500

493501
// file system providers
502+
void check$java_nio_file_spi_FileSystemProvider$(Class<?> callerClass);
503+
504+
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, URI uri, Map<String, ?> env);
505+
506+
void checkNewFileSystem(Class<?> callerClass, FileSystemProvider that, Path path, Map<String, ?> env);
507+
494508
void checkNewInputStream(Class<?> callerClass, FileSystemProvider that, Path path, OpenOption... options);
495509

510+
void checkNewOutputStream(Class<?> callerClass, FileSystemProvider that, Path path, OpenOption... options);
511+
512+
void checkNewFileChannel(
513+
Class<?> callerClass,
514+
FileSystemProvider that,
515+
Path path,
516+
Set<? extends OpenOption> options,
517+
FileAttribute<?>... attrs
518+
);
519+
520+
void checkNewAsynchronousFileChannel(
521+
Class<?> callerClass,
522+
FileSystemProvider that,
523+
Path path,
524+
Set<? extends OpenOption> options,
525+
ExecutorService executor,
526+
FileAttribute<?>... attrs
527+
);
528+
529+
void checkNewByteChannel(
530+
Class<?> callerClass,
531+
FileSystemProvider that,
532+
Path path,
533+
Set<? extends OpenOption> options,
534+
FileAttribute<?>... attrs
535+
);
536+
537+
void checkNewDirectoryStream(Class<?> callerClass, FileSystemProvider that, Path dir, DirectoryStream.Filter<? super Path> filter);
538+
539+
void checkCreateDirectory(Class<?> callerClass, FileSystemProvider that, Path dir, FileAttribute<?>... attrs);
540+
541+
void checkCreateSymbolicLink(Class<?> callerClass, FileSystemProvider that, Path link, Path target, FileAttribute<?>... attrs);
542+
543+
void checkCreateLink(Class<?> callerClass, FileSystemProvider that, Path link, Path existing);
544+
545+
void checkDelete(Class<?> callerClass, FileSystemProvider that, Path path);
546+
547+
void checkDeleteIfExists(Class<?> callerClass, FileSystemProvider that, Path path);
548+
549+
void checkReadSymbolicLink(Class<?> callerClass, FileSystemProvider that, Path link);
550+
551+
void checkCopy(Class<?> callerClass, FileSystemProvider that, Path source, Path target, CopyOption... options);
552+
553+
void checkMove(Class<?> callerClass, FileSystemProvider that, Path source, Path target, CopyOption... options);
554+
555+
void checkIsSameFile(Class<?> callerClass, FileSystemProvider that, Path path, Path path2);
556+
557+
void checkIsHidden(Class<?> callerClass, FileSystemProvider that, Path path);
558+
559+
void checkGetFileStore(Class<?> callerClass, FileSystemProvider that, Path path);
560+
561+
void checkCheckAccess(Class<?> callerClass, FileSystemProvider that, Path path, AccessMode... modes);
562+
563+
void checkGetFileAttributeView(Class<?> callerClass, FileSystemProvider that, Path path, Class<?> type, LinkOption... options);
564+
565+
void checkReadAttributes(Class<?> callerClass, FileSystemProvider that, Path path, Class<?> type, LinkOption... options);
566+
567+
void checkReadAttributes(Class<?> callerClass, FileSystemProvider that, Path path, String attributes, LinkOption... options);
568+
569+
void checkReadAttributesIfExists(Class<?> callerClass, FileSystemProvider that, Path path, Class<?> type, LinkOption... options);
570+
571+
void checkSetAttribute(Class<?> callerClass, FileSystemProvider that, Path path, String attribute, Object value, LinkOption... options);
572+
573+
void checkExists(Class<?> callerClass, FileSystemProvider that, Path path, LinkOption... options);
574+
496575
// file store
497576
void checkGetFileStoreAttributeView(Class<?> callerClass, FileStore that, Class<?> type);
498577

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)