|
28 | 28 | import static org.mockito.Mockito.*;
|
29 | 29 |
|
30 | 30 | import java.io.IOException;
|
| 31 | +import java.net.InetAddress; |
31 | 32 | import java.net.InetSocketAddress;
|
32 | 33 | import java.nio.charset.StandardCharsets;
|
33 | 34 | import java.util.stream.Stream;
|
34 | 35 |
|
35 | 36 | import javax.security.auth.Subject;
|
36 | 37 |
|
37 | 38 | import org.dcache.nfs.ExportFile;
|
| 39 | +import org.dcache.nfs.ExportTable; |
38 | 40 | import org.dcache.nfs.FsExport;
|
39 | 41 | import org.dcache.nfs.status.AccessException;
|
40 | 42 | import org.dcache.nfs.status.NoEntException;
|
@@ -743,4 +745,106 @@ public void testPrivilegedClient() throws IOException {
|
743 | 745 | pseudoFs = new PseudoFs(vfs, mockedRpc, mockedExportFile);
|
744 | 746 | pseudoFs.getattr(fsRoot);
|
745 | 747 | }
|
| 748 | + |
| 749 | + @Test |
| 750 | + public void testAllowListingByExportedClient() throws IOException { |
| 751 | + InetSocketAddress clientEndpoint = new InetSocketAddress("192.168.1.1", 314); |
| 752 | + |
| 753 | + Subject subject = toSubject(17, 17); |
| 754 | + Inode parent = vfs.mkdir(fsRoot, "exports", subject, 0755); |
| 755 | + vfs.mkdir(parent, "data", subject, 0755); |
| 756 | + |
| 757 | + given(mockedTransport.getRemoteSocketAddress()).willReturn(clientEndpoint); |
| 758 | + given(mockedAuth.getSubject()).willReturn(ROOT); |
| 759 | + given(mockedAuth.type()).willReturn(RpcAuthType.UNIX); |
| 760 | + given(mockedRpc.getTransport()).willReturn(mockedTransport); |
| 761 | + given(mockedRpc.getCredential()).willReturn(mockedAuth); |
| 762 | + |
| 763 | + FsExport export1 = new FsExport.FsExportBuilder() |
| 764 | + .rw() |
| 765 | + .trusted() |
| 766 | + .withoutAcl() |
| 767 | + .withSec(FsExport.Sec.SYS) |
| 768 | + .forClient("192.168.1.0/24") |
| 769 | + .build("/exports/data"); |
| 770 | + |
| 771 | + |
| 772 | + var exportTable = new ExportTable() { |
| 773 | + |
| 774 | + @Override |
| 775 | + public Stream<FsExport> exports() { |
| 776 | + throw new RuntimeException("not used in this test"); |
| 777 | + } |
| 778 | + |
| 779 | + @Override |
| 780 | + public Stream<FsExport> exports(InetAddress client) { |
| 781 | + return export1.isAllowed(client) ? Stream.of(export1) : Stream.empty(); |
| 782 | + } |
| 783 | + |
| 784 | + @Override |
| 785 | + public FsExport getExport(String path, InetAddress client) { |
| 786 | + return export1.getPath().equals(path) && export1.isAllowed(client)? export1 : null; |
| 787 | + } |
| 788 | + |
| 789 | + @Override |
| 790 | + public FsExport getExport(int index, InetAddress client) { |
| 791 | + return export1.getIndex() == index && export1.isAllowed(client) ? export1 : null; |
| 792 | + } |
| 793 | + }; |
| 794 | + |
| 795 | + pseudoFs = new PseudoFs(vfs, mockedRpc, exportTable); |
| 796 | + Inode pseudoRoot = pseudoFs.getRootInode(); |
| 797 | + pseudoFs.list(pseudoRoot, DirectoryStream.ZERO_VERIFIER, 0); |
| 798 | + } |
| 799 | + |
| 800 | + |
| 801 | + @Test(expected = AccessException.class) |
| 802 | + public void testRejectListing() throws IOException { |
| 803 | + InetSocketAddress clientEndpoint = new InetSocketAddress("192.168.2.1", 314); |
| 804 | + |
| 805 | + Subject subject = toSubject(17, 17); |
| 806 | + Inode parent = vfs.mkdir(fsRoot, "exports", subject, 0755); |
| 807 | + vfs.mkdir(parent, "data", subject, 0755); |
| 808 | + |
| 809 | + given(mockedTransport.getRemoteSocketAddress()).willReturn(clientEndpoint); |
| 810 | + given(mockedAuth.getSubject()).willReturn(ROOT); |
| 811 | + given(mockedAuth.type()).willReturn(RpcAuthType.UNIX); |
| 812 | + given(mockedRpc.getTransport()).willReturn(mockedTransport); |
| 813 | + given(mockedRpc.getCredential()).willReturn(mockedAuth); |
| 814 | + |
| 815 | + FsExport export1 = new FsExport.FsExportBuilder() |
| 816 | + .rw() |
| 817 | + .trusted() |
| 818 | + .withoutAcl() |
| 819 | + .withSec(FsExport.Sec.SYS) |
| 820 | + .forClient("192.168.1.0/24") |
| 821 | + .build("/exports/data"); |
| 822 | + |
| 823 | + var exportTable = new ExportTable() { |
| 824 | + |
| 825 | + @Override |
| 826 | + public Stream<FsExport> exports() { |
| 827 | + throw new RuntimeException("not used in this test"); |
| 828 | + } |
| 829 | + |
| 830 | + @Override |
| 831 | + public Stream<FsExport> exports(InetAddress client) { |
| 832 | + return export1.isAllowed(client) ? Stream.of(export1) : Stream.empty(); |
| 833 | + } |
| 834 | + |
| 835 | + @Override |
| 836 | + public FsExport getExport(String path, InetAddress client) { |
| 837 | + return export1.getPath().equals(path) && export1.isAllowed(client)? export1 : null; |
| 838 | + } |
| 839 | + |
| 840 | + @Override |
| 841 | + public FsExport getExport(int index, InetAddress client) { |
| 842 | + return export1.getIndex() == index && export1.isAllowed(client) ? export1 : null; |
| 843 | + } |
| 844 | + }; |
| 845 | + |
| 846 | + pseudoFs = new PseudoFs(vfs, mockedRpc, exportTable); |
| 847 | + Inode pseudoRoot = pseudoFs.getRootInode(); |
| 848 | + pseudoFs.list(pseudoRoot, DirectoryStream.ZERO_VERIFIER, 0); |
| 849 | + } |
746 | 850 | }
|
0 commit comments