-
Couldn't load subscription status.
- Fork 9.1k
HADOOP-19733. S3A: load credentials providers using configured classloader #8048
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
Draft
brandonvin
wants to merge
2
commits into
apache:trunk
Choose a base branch
from
brandonvin:HADOOP-19733-s3a-classloader-isolation
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−15
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
|
|
||
|
|
@@ -28,6 +29,14 @@ | |
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.s3a.impl.InstantiationIOException; | ||
|
|
||
| import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
| import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
|
|
||
| import static org.mockito.Mockito.doReturn; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Checks that classloader isolation for loading extension classes is applied | ||
|
|
@@ -37,10 +46,33 @@ | |
| */ | ||
| public class ITestS3AFileSystemIsolatedClassloader extends AbstractS3ATestBase { | ||
|
|
||
| private static String customClassName = "custom.class.name"; | ||
|
|
||
| private static class CustomCredentialsProvider implements AwsCredentialsProvider { | ||
|
|
||
| public CustomCredentialsProvider() { | ||
| } | ||
|
|
||
| @Override | ||
| public AwsCredentials resolveCredentials() { | ||
| return null; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private static class CustomClassLoader extends ClassLoader { | ||
| } | ||
|
|
||
| private final ClassLoader customClassLoader = new CustomClassLoader(); | ||
| private final ClassLoader customClassLoader = spy(new CustomClassLoader()); | ||
| { | ||
| try { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a nice way to simulate classloader pain. |
||
| doReturn(CustomCredentialsProvider.class) | ||
| .when(customClassLoader) | ||
| .loadClass(customClassName); | ||
| } catch (ClassNotFoundException ex) { | ||
| throw new RuntimeException(ex); | ||
| } | ||
| } | ||
|
|
||
| private S3AFileSystem createNewTestFs(Configuration conf) throws IOException { | ||
| S3AFileSystem fs = new S3AFileSystem(); | ||
|
|
@@ -77,19 +109,9 @@ private void assertInNewFilesystem(Map<String, String> confToSet, Consumer<FileS | |
| } | ||
| } | ||
|
|
||
| private Map<String, String> mapOf() { | ||
| return new HashMap<>(); | ||
| } | ||
|
|
||
| private Map<String, String> mapOf(String key, String value) { | ||
| HashMap<String, String> m = new HashMap<>(); | ||
| m.put(key, value); | ||
| return m; | ||
| } | ||
brandonvin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Test | ||
| public void defaultIsolatedClassloader() throws IOException { | ||
| assertInNewFilesystem(mapOf(), (fs) -> { | ||
| assertInNewFilesystem(Map.of(), (fs) -> { | ||
| Assertions.assertThat(fs.getConf().getClassLoader()) | ||
| .describedAs("The classloader used to load s3a fs extensions") | ||
| .isNotEqualTo(Thread.currentThread().getContextClassLoader()) | ||
|
|
@@ -100,11 +122,26 @@ public void defaultIsolatedClassloader() throws IOException { | |
| .isEqualTo(fs.getClass().getClassLoader()) | ||
| .describedAs("the classloader that loaded the fs"); | ||
| }); | ||
|
|
||
| Throwable thrown = Assertions.catchThrowable(() -> { | ||
brandonvin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assertInNewFilesystem( | ||
| Map.of(Constants.AWS_CREDENTIALS_PROVIDER, customClassName), | ||
| (fs) -> {}); | ||
| }); | ||
|
|
||
| Assertions.assertThat(thrown) | ||
| .describedAs("thrown") | ||
| .isInstanceOf(InstantiationIOException.class); | ||
|
|
||
| Assertions.assertThat(thrown.getCause()) | ||
| .describedAs("cause") | ||
| .isInstanceOf(ClassNotFoundException.class) | ||
| .hasMessageContaining(customClassName); | ||
| } | ||
|
|
||
| @Test | ||
| public void isolatedClassloader() throws IOException { | ||
| assertInNewFilesystem(mapOf(Constants.AWS_S3_CLASSLOADER_ISOLATION, "true"), (fs) -> { | ||
| assertInNewFilesystem(Map.of(Constants.AWS_S3_CLASSLOADER_ISOLATION, "true"), (fs) -> { | ||
| Assertions.assertThat(fs.getConf().getClassLoader()) | ||
| .describedAs("The classloader used to load s3a fs extensions") | ||
| .isNotEqualTo(Thread.currentThread().getContextClassLoader()) | ||
|
|
@@ -115,11 +152,31 @@ public void isolatedClassloader() throws IOException { | |
| .isEqualTo(fs.getClass().getClassLoader()) | ||
| .describedAs("the classloader that loaded the fs"); | ||
| }); | ||
|
|
||
| Throwable thrown = Assertions.catchThrowable(() -> { | ||
brandonvin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assertInNewFilesystem( | ||
| Map.of(Constants.AWS_S3_CLASSLOADER_ISOLATION, "true", | ||
| Constants.AWS_CREDENTIALS_PROVIDER, customClassName), | ||
| (fs) -> {}); | ||
| }); | ||
|
|
||
| Assertions.assertThat(thrown) | ||
| .describedAs("thrown") | ||
| .isInstanceOf(InstantiationIOException.class); | ||
|
|
||
| Assertions.assertThat(thrown.getCause()) | ||
| .describedAs("cause") | ||
| .isInstanceOf(ClassNotFoundException.class) | ||
| .hasMessageContaining(customClassName); | ||
| } | ||
|
|
||
| @Test | ||
| public void notIsolatedClassloader() throws IOException { | ||
| assertInNewFilesystem(mapOf(Constants.AWS_S3_CLASSLOADER_ISOLATION, "false"), (fs) -> { | ||
| Map<String, String> confToSet = Map.of( | ||
| Constants.AWS_S3_CLASSLOADER_ISOLATION, "false", | ||
| Constants.AWS_CREDENTIALS_PROVIDER, customClassName); | ||
|
|
||
| assertInNewFilesystem(confToSet, (fs) -> { | ||
| Assertions.assertThat(fs.getConf().getClassLoader()) | ||
| .describedAs("The classloader used to load s3a fs extensions") | ||
| .isEqualTo(Thread.currentThread().getContextClassLoader()) | ||
|
|
@@ -129,6 +186,16 @@ public void notIsolatedClassloader() throws IOException { | |
| .describedAs("The classloader used to load s3a fs extensions") | ||
| .isNotEqualTo(fs.getClass().getClassLoader()) | ||
| .describedAs("the classloader that loaded the fs"); | ||
|
|
||
| S3AFileSystem s3a = (S3AFileSystem) fs; | ||
| List<AwsCredentialsProvider> providers = | ||
| s3a.getS3AInternals().shareCredentials("test").getProviders(); | ||
| Assertions.assertThat(providers) | ||
| .describedAs("providers") | ||
| .hasSize(1); | ||
| Assertions.assertThat(providers.get(0)) | ||
| .describedAs("provider") | ||
| .isInstanceOf(CustomCredentialsProvider.class); | ||
| }); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: put the amazon imports in the same group as the junit ones