-
Notifications
You must be signed in to change notification settings - Fork 1k
[apache5-client] Fail fast when SecurityManager lacks TCP_KEEPIDLE/TCP_KEEPINTERVAL/TCP_KEEPCOUNT permissions. #6992
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
9 commits
Select commit
Hold shift + click to select a range
90fc833
Fail fast at Apache5HttpClient construction when SecurityManager is a…
joviegas 5049b73
Update Junits
joviegas 697b152
update junit test cases and handle case where we can get security exc…
joviegas bcefb09
Update for loop
joviegas a6ea0eb
Handle PR comemnt to add test case with actual request
joviegas bb45965
checkstyle
joviegas c56ae8e
Handle review comment
joviegas fc4a27e
Merge branch 'master' into joviegas/handle_apache_for_security_manager
joviegas cace1f3
Merge branch 'master' into joviegas/handle_apache_for_security_manager
joviegas 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Apache HTTP Client 5", | ||
| "contributor": "", | ||
| "description": "Fail fast at Apache5HttpClient construction when SecurityManager is active and jdk.net.NetworkPermission setOption.TCP_KEEPIDLE, setOption.TCP_KEEPINTERVAL, setOption.TCP_KEEPCOUNT are not granted." | ||
| } |
35 changes: 35 additions & 0 deletions
35
...t/src/test/java/software/amazon/awssdk/http/apache/ApacheSecurityManagerHttpCallTest.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,35 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.http.apache; | ||
|
|
||
| import org.junit.jupiter.api.condition.EnabledForJreRange; | ||
| import org.junit.jupiter.api.condition.JRE; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.http.SdkHttpClientSecurityManagerTestSuite; | ||
|
|
||
| @EnabledForJreRange(max = JRE.JAVA_17) | ||
| class ApacheSecurityManagerHttpCallTest extends SdkHttpClientSecurityManagerTestSuite { | ||
|
|
||
| @Override | ||
| protected SdkHttpClient createHttpClient() { | ||
| return ApacheHttpClient.builder().build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPolicyFileUrl() { | ||
| return getClass().getResource("security-manager-test.policy").toExternalForm(); | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
...client/src/test/resources/software/amazon/awssdk/http/apache/security-manager-test.policy
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,8 @@ | ||
| grant { | ||
| permission java.util.PropertyPermission "*", "read,write"; | ||
| permission java.lang.RuntimePermission "modifyThread"; | ||
| permission java.lang.RuntimePermission "setContextClassLoader"; | ||
| permission java.lang.RuntimePermission "setSecurityManager"; | ||
| permission java.lang.reflect.ReflectPermission "suppressAccessChecks"; | ||
| permission java.net.SocketPermission "*", "connect,accept"; | ||
| }; |
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
150 changes: 150 additions & 0 deletions
150
...st/java/software/amazon/awssdk/http/apache5/Apache5SecurityManagerClientCreationTest.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,150 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.http.apache5; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatNoException; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.security.Permission; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
| import java.util.stream.Stream; | ||
| import org.apache.logging.log4j.Level; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.condition.EnabledForJreRange; | ||
| import org.junit.jupiter.api.condition.JRE; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import software.amazon.awssdk.testutils.LogCaptor; | ||
|
|
||
| /** | ||
| * Tests that Apache5HttpClient fails fast at construction time when a SecurityManager | ||
| * denies jdk.net.NetworkPermission for TCP keepalive extended options. | ||
| */ | ||
| @EnabledForJreRange(max = JRE.JAVA_17) | ||
| class Apache5SecurityManagerClientCreationTest { | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| System.setSecurityManager(null); | ||
| System.clearProperty("java.security.policy"); | ||
| java.security.Policy.getPolicy().refresh(); | ||
| } | ||
|
|
||
| @Test | ||
| void buildWithDefaults_whenStandardPermissionsGrantedButNetworkPermissionMissing_shouldThrowIllegalStateException() { | ||
| System.setProperty("java.security.policy", "=" + getPolicyUrl()); | ||
| java.security.Policy.getPolicy().refresh(); | ||
| System.setSecurityManager(new SecurityManager()); | ||
|
|
||
| assertThatThrownBy(() -> Apache5HttpClient.builder().build()) | ||
| .isInstanceOf(IllegalStateException.class) | ||
| .hasMessageContaining("jdk.net.NetworkPermission"); | ||
| } | ||
|
|
||
| private String getPolicyUrl() { | ||
| return getClass().getResource("security-manager-test.policy").toExternalForm(); | ||
| } | ||
|
|
||
| @Test | ||
| void buildWithDefaults_whenUnrelatedSecurityExceptionThrown_shouldNotThrow() { | ||
| System.setSecurityManager(new SecurityManager() { | ||
| @Override | ||
| public void checkPermission(Permission perm) { | ||
| if ("jdk.net.NetworkPermission".equals(perm.getClass().getName())) { | ||
| throw new SecurityException("access denied: some.unrelated.permission"); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| try (LogCaptor logCaptor = LogCaptor.create(Level.DEBUG)) { | ||
| assertThatNoException().isThrownBy(() -> { | ||
| Apache5HttpClient.builder().build().close(); | ||
| }); | ||
| assertThat(logCaptor.loggedEvents()).anySatisfy(logEvent -> { | ||
| assertThat(logEvent.getLevel()).isEqualTo(Level.DEBUG); | ||
| assertThat(logEvent.getMessage().getFormattedMessage()) | ||
| .contains("SecurityManager denied a non-TCP socket option permission"); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("partiallyGrantedPermissions") | ||
| void buildWithDefaults_whenNotAllPermissionsGranted_shouldThrowIllegalStateException(Set<String> grantedPermissions) { | ||
| System.setSecurityManager(new GrantOnlyNetworkPermissionSecurityManager(grantedPermissions)); | ||
|
|
||
| assertThatThrownBy(() -> Apache5HttpClient.builder().build()) | ||
| .isInstanceOf(IllegalStateException.class) | ||
| .hasMessageContaining("jdk.net.NetworkPermission"); | ||
| } | ||
|
|
||
| @Test | ||
| void buildWithDefaults_whenAllPermissionsGranted_shouldSucceed() { | ||
| Set<String> allGranted = new HashSet<>(Arrays.asList( | ||
| "setOption.TCP_KEEPIDLE", "setOption.TCP_KEEPINTERVAL", "setOption.TCP_KEEPCOUNT")); | ||
| System.setSecurityManager(new GrantOnlyNetworkPermissionSecurityManager(allGranted)); | ||
| assertThatNoException().isThrownBy(() -> { | ||
| Apache5HttpClient.builder().build().close(); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void buildWithDefaults_whenNoSecurityManager_shouldSucceed() { | ||
| assertThatNoException().isThrownBy(() -> { | ||
| Apache5HttpClient.builder().build().close(); | ||
| }); | ||
| } | ||
|
|
||
| static Stream<Arguments> partiallyGrantedPermissions() { | ||
| return Stream.of( | ||
| // 0 out of 3 granted | ||
| Arguments.of(new HashSet<>()), | ||
| // 1 out of 3 granted | ||
| Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPIDLE"))), | ||
| Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPINTERVAL"))), | ||
| Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPCOUNT"))), | ||
| // 2 out of 3 granted | ||
| Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPIDLE", "setOption.TCP_KEEPINTERVAL"))), | ||
| Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPIDLE", "setOption.TCP_KEEPCOUNT"))), | ||
| Arguments.of(new HashSet<>(Arrays.asList("setOption.TCP_KEEPINTERVAL", "setOption.TCP_KEEPCOUNT"))) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * SecurityManager that only grants specific jdk.net.NetworkPermission entries and denies the rest. | ||
| */ | ||
| private static class GrantOnlyNetworkPermissionSecurityManager extends SecurityManager { | ||
| private final Set<String> grantedPermissions; | ||
|
|
||
| GrantOnlyNetworkPermissionSecurityManager(Set<String> grantedPermissions) { | ||
| this.grantedPermissions = grantedPermissions; | ||
| } | ||
|
|
||
| @Override | ||
| public void checkPermission(Permission perm) { | ||
| if ("jdk.net.NetworkPermission".equals(perm.getClass().getName()) | ||
| && !grantedPermissions.contains(perm.getName())) { | ||
| throw new SecurityException("Denied: " + perm.getName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
...src/test/java/software/amazon/awssdk/http/apache5/Apache5SecurityManagerHttpCallTest.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,35 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.http.apache5; | ||
|
|
||
| import org.junit.jupiter.api.condition.EnabledForJreRange; | ||
| import org.junit.jupiter.api.condition.JRE; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.http.SdkHttpClientSecurityManagerTestSuite; | ||
|
|
||
| @EnabledForJreRange(max = JRE.JAVA_17) | ||
| class Apache5SecurityManagerHttpCallTest extends SdkHttpClientSecurityManagerTestSuite { | ||
|
|
||
| @Override | ||
| protected SdkHttpClient createHttpClient() { | ||
| return Apache5HttpClient.builder().build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getPolicyFileUrl() { | ||
| return getClass().getResource("security-manager-test-with-http-call.policy").toExternalForm(); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...resources/software/amazon/awssdk/http/apache5/security-manager-test-with-http-call.policy
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,13 @@ | ||
| grant { | ||
| permission java.util.PropertyPermission "*", "read,write"; | ||
| permission java.lang.RuntimePermission "modifyThread"; | ||
| permission java.lang.RuntimePermission "setContextClassLoader"; | ||
| permission java.lang.RuntimePermission "setSecurityManager"; | ||
| permission java.lang.reflect.ReflectPermission "suppressAccessChecks"; | ||
| permission java.net.SocketPermission "*", "connect,accept"; | ||
|
|
||
| // Required by Apache HC5 for TCP socket options (not needed by Apache HC4) | ||
| permission jdk.net.NetworkPermission "setOption.TCP_KEEPIDLE"; | ||
| permission jdk.net.NetworkPermission "setOption.TCP_KEEPINTERVAL"; | ||
| permission jdk.net.NetworkPermission "setOption.TCP_KEEPCOUNT"; | ||
| }; |
15 changes: 15 additions & 0 deletions
15
...lient/src/test/resources/software/amazon/awssdk/http/apache5/security-manager-test.policy
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,15 @@ | ||
| grant { | ||
| permission java.util.PropertyPermission "*", "read,write"; | ||
| permission java.io.FilePermission "<<ALL FILES>>", "read,write"; | ||
| permission java.lang.RuntimePermission "getenv.*"; | ||
| permission "java.lang.RuntimePermission" "accessDeclaredMembers"; | ||
| permission "javax.net.ssl.SSLPermission" "setDefaultSSLContext"; | ||
| permission "java.net.SocketPermission" "*", "connect,resolve"; | ||
|
|
||
| // Needed for test to remove the security manager | ||
| permission java.lang.RuntimePermission "setSecurityManager"; | ||
|
|
||
| // jdk.net.NetworkPermission for setOption.TCP_KEEPIDLE, setOption.TCP_KEEPINTERVAL, | ||
| // setOption.TCP_KEEPCOUNT is explicitly NOT granted to test that Apache5HttpClient | ||
| // fails fast when these permissions are missing. | ||
| }; |
Oops, something went wrong.
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.
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.
Note that
NetworkPermissionis marked forforRemovalin Java 25 thus loading it from ClassLoaderHelper to prevent ClassNotFoundException for future Java versions.https://docs.oracle.com/en/java/javase/26/docs/api/jdk.net/jdk/net/NetworkPermission.html