|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.javaagent; |
| 10 | + |
| 11 | +import org.opensearch.javaagent.bootstrap.AgentPolicy; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.nio.channels.FileChannel; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.nio.file.StandardOpenOption; |
| 20 | +import java.security.PermissionCollection; |
| 21 | +import java.security.Permissions; |
| 22 | +import java.security.Policy; |
| 23 | +import java.security.ProtectionDomain; |
| 24 | +import java.util.UUID; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertThrows; |
| 27 | +import static org.junit.Assert.assertTrue; |
| 28 | + |
| 29 | +@SuppressWarnings("removal") |
| 30 | +public class FileInterceptorNegativeIntegTests { |
| 31 | + private static Path getTestDir() { |
| 32 | + Path baseDir = Path.of(System.getProperty("user.dir")); |
| 33 | + Path integTestFiles = baseDir.resolve("integ-test-files").normalize(); |
| 34 | + return integTestFiles; |
| 35 | + } |
| 36 | + |
| 37 | + private String randomAlphaOfLength(int length) { |
| 38 | + // Using UUID to generate random string and taking first 'length' characters |
| 39 | + return UUID.randomUUID().toString().replaceAll("-", "").substring(0, length); |
| 40 | + } |
| 41 | + |
| 42 | + @BeforeClass |
| 43 | + public static void setUp() throws Exception { |
| 44 | + Policy policy = new Policy() { |
| 45 | + @Override |
| 46 | + public PermissionCollection getPermissions(ProtectionDomain domain) { |
| 47 | + Permissions permissions = new Permissions(); |
| 48 | + return permissions; |
| 49 | + } |
| 50 | + }; |
| 51 | + AgentPolicy.setPolicy(policy); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFileInputStream() { |
| 56 | + Path tmpDir = getTestDir(); |
| 57 | + assertTrue("Tmp directory should exist", Files.exists(tmpDir)); |
| 58 | + assertTrue("Tmp directory should be writable", Files.isWritable(tmpDir)); |
| 59 | + |
| 60 | + // Create a unique file name |
| 61 | + String fileName = "test-" + randomAlphaOfLength(8) + ".txt"; |
| 62 | + Path tempPath = tmpDir.resolve(fileName); |
| 63 | + |
| 64 | + // Verify error when writing Content |
| 65 | + String content = "test content"; |
| 66 | + SecurityException exception = assertThrows("", SecurityException.class, () -> { |
| 67 | + Files.write(tempPath, content.getBytes(StandardCharsets.UTF_8)); |
| 68 | + }); |
| 69 | + assertTrue(exception.getMessage().contains("Denied WRITE access to file")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testOpenForReadAndWrite() { |
| 74 | + Path tmpDir = getTestDir(); |
| 75 | + assertTrue("Tmp directory should exist", Files.exists(tmpDir)); |
| 76 | + assertTrue("Tmp directory should be writable", Files.isWritable(tmpDir)); |
| 77 | + |
| 78 | + // Create a unique file name |
| 79 | + String fileName = "test-" + randomAlphaOfLength(8) + ".txt"; |
| 80 | + Path tempPath = tmpDir.resolve(fileName); |
| 81 | + |
| 82 | + // Verify error when opening file |
| 83 | + SecurityException exception = assertThrows("", SecurityException.class, () -> { |
| 84 | + FileChannel.open(tempPath, StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); |
| 85 | + }); |
| 86 | + assertTrue(exception.getMessage().contains("Denied OPEN (read/write) access to file")); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testCopy() { |
| 91 | + Path tmpDir = getTestDir(); |
| 92 | + Path sourcePath = tmpDir.resolve("test-source-" + randomAlphaOfLength(8) + ".txt"); |
| 93 | + Path targetPath = tmpDir.resolve("test-target-" + randomAlphaOfLength(8) + ".txt"); |
| 94 | + |
| 95 | + // Verify Error when copying file |
| 96 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.copy(sourcePath, targetPath)); |
| 97 | + assertTrue(exception.getMessage().contains("Denied COPY (read) access to file")); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testCreateFile() { |
| 102 | + Path tmpDir = getTestDir(); |
| 103 | + Path tempPath = tmpDir.resolve("test-create-" + randomAlphaOfLength(8) + ".txt"); |
| 104 | + |
| 105 | + // Verify Error when creating file |
| 106 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.createFile(tempPath)); |
| 107 | + assertTrue(exception.getMessage().contains("Denied WRITE access to file")); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testMove() { |
| 112 | + Path tmpDir = getTestDir(); |
| 113 | + Path sourcePath = tmpDir.resolve("test-source-" + randomAlphaOfLength(8) + ".txt"); |
| 114 | + Path targetPath = tmpDir.resolve("test-target-" + randomAlphaOfLength(8) + ".txt"); |
| 115 | + |
| 116 | + // Verify Error when moving file |
| 117 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.move(sourcePath, targetPath)); |
| 118 | + assertTrue(exception.getMessage().contains("Denied WRITE access to file")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testCreateLink() throws Exception { |
| 123 | + Path tmpDir = getTestDir(); |
| 124 | + Path originalPath = tmpDir.resolve("test-original-" + randomAlphaOfLength(8) + ".txt"); |
| 125 | + Path linkPath = tmpDir.resolve("test-link-" + randomAlphaOfLength(8) + ".txt"); |
| 126 | + |
| 127 | + // Verify Error when creating link |
| 128 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.createLink(linkPath, originalPath)); |
| 129 | + assertTrue(exception.getMessage().contains("Denied WRITE access to file")); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testDelete() throws Exception { |
| 134 | + Path tmpDir = getTestDir(); |
| 135 | + Path tempPath = tmpDir.resolve("test-delete-" + randomAlphaOfLength(8) + ".txt"); |
| 136 | + |
| 137 | + // Verify Error when deleting file |
| 138 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.delete(tempPath)); |
| 139 | + assertTrue(exception.getMessage().contains("Denied DELETE access to file")); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testReadAllLines() throws Exception { |
| 144 | + Path tmpDir = getTestDir(); |
| 145 | + Path tempPath = tmpDir.resolve("test-readAllLines-" + randomAlphaOfLength(8) + ".txt"); |
| 146 | + |
| 147 | + // Verify Error when reading file |
| 148 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.readAllLines(tempPath)); |
| 149 | + assertTrue(exception.getMessage().contains("Denied READ access to file")); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testNewOutputStream() throws Exception { |
| 154 | + Path tmpDir = getTestDir(); |
| 155 | + Path tempPath = tmpDir.resolve("test-readAllLines-" + randomAlphaOfLength(8) + ".txt"); |
| 156 | + |
| 157 | + // Verify error when calling newOutputStream |
| 158 | + SecurityException exception = assertThrows(SecurityException.class, () -> Files.newOutputStream(tempPath)); |
| 159 | + assertTrue(exception.getMessage().contains("Denied OPEN (read/write) access to file")); |
| 160 | + } |
| 161 | +} |
0 commit comments