-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Increased the scope of File Interceptor to intercept methods from FileSystemProvider.class #17989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0bbe01b
Increased the scope o File Interceptor ro intercept methods from File…
pranu2502 41eec59
Added negative Integ Tests to validate the correct Security Exception…
pranu2502 07da4bf
Fixed failing negative tests because of file exist assertions
pranu2502 ea17a0b
Added a deny default Security Exception for unknown arfumen types in …
pranu2502 7a8ec58
Fixed File Permission substitution for Integ test Log files
pranu2502 1687680
Fixes issue with system property substition in policy file parsing
cwperks fb808c8
Added ChangeLog for newByteChannel Interception
pranu2502 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...nt-sm/agent/src/test/java/org/opensearch/javaagent/FileInterceptorNegativeIntegTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.javaagent; | ||
|
|
||
| import org.opensearch.javaagent.bootstrap.AgentPolicy; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| import java.nio.channels.FileChannel; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.security.PermissionCollection; | ||
| import java.security.Permissions; | ||
| import java.security.Policy; | ||
| import java.security.ProtectionDomain; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.Assert.assertThrows; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| @SuppressWarnings("removal") | ||
| public class FileInterceptorNegativeIntegTests { | ||
| private static Path getTestDir() { | ||
| Path baseDir = Path.of(System.getProperty("user.dir")); | ||
| Path integTestFiles = baseDir.resolve("integ-test-files").normalize(); | ||
| return integTestFiles; | ||
| } | ||
|
|
||
| private String randomAlphaOfLength(int length) { | ||
| // Using UUID to generate random string and taking first 'length' characters | ||
| return UUID.randomUUID().toString().replaceAll("-", "").substring(0, length); | ||
| } | ||
|
|
||
| @BeforeClass | ||
| public static void setUp() throws Exception { | ||
| Policy policy = new Policy() { | ||
| @Override | ||
| public PermissionCollection getPermissions(ProtectionDomain domain) { | ||
| Permissions permissions = new Permissions(); | ||
| return permissions; | ||
| } | ||
| }; | ||
| AgentPolicy.setPolicy(policy); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFileInputStream() { | ||
| Path tmpDir = getTestDir(); | ||
|
|
||
| // Create a unique file name | ||
| String fileName = "test-" + randomAlphaOfLength(8) + ".txt"; | ||
| Path tempPath = tmpDir.resolve(fileName); | ||
|
|
||
| // Verify error when writing Content | ||
| String content = "test content"; | ||
| SecurityException exception = assertThrows("", SecurityException.class, () -> { | ||
| Files.write(tempPath, content.getBytes(StandardCharsets.UTF_8)); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("Denied WRITE access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOpenForReadAndWrite() { | ||
| Path tmpDir = getTestDir(); | ||
|
|
||
| // Create a unique file name | ||
| String fileName = "test-" + randomAlphaOfLength(8) + ".txt"; | ||
| Path tempPath = tmpDir.resolve(fileName); | ||
|
|
||
| // Verify error when opening file | ||
| SecurityException exception = assertThrows("", SecurityException.class, () -> { | ||
| FileChannel.open(tempPath, StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); | ||
| }); | ||
| assertTrue(exception.getMessage().contains("Denied OPEN (read/write) access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopy() { | ||
| Path tmpDir = getTestDir(); | ||
| Path sourcePath = tmpDir.resolve("test-source-" + randomAlphaOfLength(8) + ".txt"); | ||
| Path targetPath = tmpDir.resolve("test-target-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify Error when copying file | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.copy(sourcePath, targetPath)); | ||
| assertTrue(exception.getMessage().contains("Denied COPY (read) access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateFile() { | ||
| Path tmpDir = getTestDir(); | ||
| Path tempPath = tmpDir.resolve("test-create-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify Error when creating file | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.createFile(tempPath)); | ||
| assertTrue(exception.getMessage().contains("Denied WRITE access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMove() { | ||
| Path tmpDir = getTestDir(); | ||
| Path sourcePath = tmpDir.resolve("test-source-" + randomAlphaOfLength(8) + ".txt"); | ||
| Path targetPath = tmpDir.resolve("test-target-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify Error when moving file | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.move(sourcePath, targetPath)); | ||
| assertTrue(exception.getMessage().contains("Denied WRITE access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateLink() throws Exception { | ||
| Path tmpDir = getTestDir(); | ||
| Path originalPath = tmpDir.resolve("test-original-" + randomAlphaOfLength(8) + ".txt"); | ||
| Path linkPath = tmpDir.resolve("test-link-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify Error when creating link | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.createLink(linkPath, originalPath)); | ||
| assertTrue(exception.getMessage().contains("Denied WRITE access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDelete() throws Exception { | ||
| Path tmpDir = getTestDir(); | ||
| Path tempPath = tmpDir.resolve("test-delete-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify Error when deleting file | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.delete(tempPath)); | ||
| assertTrue(exception.getMessage().contains("Denied DELETE access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadAllLines() throws Exception { | ||
| Path tmpDir = getTestDir(); | ||
| Path tempPath = tmpDir.resolve("test-readAllLines-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify Error when reading file | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.readAllLines(tempPath)); | ||
| assertTrue(exception.getMessage().contains("Denied OPEN (read) access to file")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNewOutputStream() throws Exception { | ||
| Path tmpDir = getTestDir(); | ||
| Path tempPath = tmpDir.resolve("test-readAllLines-" + randomAlphaOfLength(8) + ".txt"); | ||
|
|
||
| // Verify error when calling newOutputStream | ||
| SecurityException exception = assertThrows(SecurityException.class, () -> Files.newOutputStream(tempPath)); | ||
| assertTrue(exception.getMessage().contains("Denied OPEN (read/write) access to file")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.