Skip to content

Commit a6ea0eb

Browse files
committed
Handle PR comemnt to add test case with actual request
1 parent bcefb09 commit a6ea0eb

7 files changed

Lines changed: 190 additions & 32 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache;
17+
18+
import org.junit.jupiter.api.condition.EnabledForJreRange;
19+
import org.junit.jupiter.api.condition.JRE;
20+
import software.amazon.awssdk.http.SdkHttpClient;
21+
import software.amazon.awssdk.http.SdkHttpClientSecurityManagerTestSuite;
22+
23+
@EnabledForJreRange(max = JRE.JAVA_17)
24+
class ApacheSecurityManagerHttpCallTest extends SdkHttpClientSecurityManagerTestSuite {
25+
26+
@Override
27+
protected SdkHttpClient createHttpClient() {
28+
return ApacheHttpClient.builder().build();
29+
}
30+
31+
@Override
32+
protected String getPolicyFileUrl() {
33+
return getClass().getResource("security-manager-test.policy").toExternalForm();
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
grant {
2+
permission java.util.PropertyPermission "*", "read,write";
3+
permission java.lang.RuntimePermission "modifyThread";
4+
permission java.lang.RuntimePermission "setContextClassLoader";
5+
permission java.lang.RuntimePermission "setSecurityManager";
6+
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
7+
permission java.net.SocketPermission "*", "connect,accept";
8+
};

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5HttpClientSecurityManagerTest.java renamed to http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5SecurityManagerClientCreationTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* denies jdk.net.NetworkPermission for TCP keepalive extended options.
4040
*/
4141
@EnabledForJreRange(max = JRE.JAVA_17)
42-
class Apache5HttpClientSecurityManagerTest {
42+
class Apache5SecurityManagerClientCreationTest {
4343

4444
@AfterEach
4545
void tearDown() {
@@ -50,7 +50,7 @@ void tearDown() {
5050

5151
@Test
5252
void buildWithDefaults_whenStandardPermissionsGrantedButNetworkPermissionMissing_shouldThrowIllegalStateException() {
53-
System.setProperty("java.security.policy", "=" + getPolicyUrl("security-manager-test.policy"));
53+
System.setProperty("java.security.policy", "=" + getPolicyUrl());
5454
java.security.Policy.getPolicy().refresh();
5555
System.setSecurityManager(new SecurityManager());
5656

@@ -59,19 +59,8 @@ void buildWithDefaults_whenStandardPermissionsGrantedButNetworkPermissionMissing
5959
.hasMessageContaining("jdk.net.NetworkPermission");
6060
}
6161

62-
@Test
63-
void buildWithDefaults_whenPolicyGrantsNetworkPermissions_shouldSucceed() {
64-
System.setProperty("java.security.policy", "=" + getPolicyUrl("security-manager-test-with-network-permissions.policy"));
65-
java.security.Policy.getPolicy().refresh();
66-
System.setSecurityManager(new SecurityManager());
67-
68-
assertThatNoException().isThrownBy(() -> {
69-
Apache5HttpClient.builder().build().close();
70-
});
71-
}
72-
73-
private String getPolicyUrl(String policyFileName) {
74-
return getClass().getResource(policyFileName).toExternalForm();
62+
private String getPolicyUrl() {
63+
return getClass().getResource("security-manager-test.policy").toExternalForm();
7564
}
7665

7766
@Test
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.apache5;
17+
18+
import org.junit.jupiter.api.condition.EnabledForJreRange;
19+
import org.junit.jupiter.api.condition.JRE;
20+
import software.amazon.awssdk.http.SdkHttpClient;
21+
import software.amazon.awssdk.http.SdkHttpClientSecurityManagerTestSuite;
22+
23+
@EnabledForJreRange(max = JRE.JAVA_17)
24+
class Apache5SecurityManagerHttpCallTest extends SdkHttpClientSecurityManagerTestSuite {
25+
26+
@Override
27+
protected SdkHttpClient createHttpClient() {
28+
return Apache5HttpClient.builder().build();
29+
}
30+
31+
@Override
32+
protected String getPolicyFileUrl() {
33+
return getClass().getResource("security-manager-test-with-http-call.policy").toExternalForm();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
grant {
2+
permission java.util.PropertyPermission "*", "read,write";
3+
permission java.lang.RuntimePermission "modifyThread";
4+
permission java.lang.RuntimePermission "setContextClassLoader";
5+
permission java.lang.RuntimePermission "setSecurityManager";
6+
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
7+
permission java.net.SocketPermission "*", "connect,accept";
8+
9+
// Required by Apache HC5 for TCP socket options (not needed by Apache HC4)
10+
permission jdk.net.NetworkPermission "setOption.TCP_KEEPIDLE";
11+
permission jdk.net.NetworkPermission "setOption.TCP_KEEPINTERVAL";
12+
permission jdk.net.NetworkPermission "setOption.TCP_KEEPCOUNT";
13+
};

http-clients/apache5-client/src/test/resources/software/amazon/awssdk/http/apache5/security-manager-test-with-network-permissions.policy

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http;
17+
18+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
21+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
import com.github.tomakehurst.wiremock.WireMockServer;
25+
import java.net.URI;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.condition.EnabledForJreRange;
30+
import org.junit.jupiter.api.condition.JRE;
31+
32+
/**
33+
* Base test suite that verifies an HTTP client can construct and execute requests
34+
* under a SecurityManager with the appropriate permissions granted via a policy file.
35+
*
36+
* <p>Subclasses provide the HTTP client implementation and a policy file path.
37+
* The policy file for Apache 4.x does not need jdk.net.NetworkPermission entries,
38+
* while Apache 5.x requires them for TCP_KEEPIDLE/KEEPINTERVAL/KEEPCOUNT.</p>
39+
*/
40+
@EnabledForJreRange(max = JRE.JAVA_17)
41+
public abstract class SdkHttpClientSecurityManagerTestSuite {
42+
43+
private WireMockServer server;
44+
45+
@BeforeEach
46+
void setUpServer() {
47+
server = new WireMockServer(wireMockConfig().dynamicPort());
48+
server.start();
49+
server.stubFor(get(urlPathEqualTo("/"))
50+
.willReturn(aResponse().withStatus(200).withBody("ok")));
51+
}
52+
53+
@AfterEach
54+
void tearDownServer() {
55+
System.setSecurityManager(null);
56+
System.clearProperty("java.security.policy");
57+
java.security.Policy.getPolicy().refresh();
58+
server.stop();
59+
}
60+
61+
/**
62+
* Creates the HTTP client to test.
63+
*/
64+
protected abstract SdkHttpClient createHttpClient();
65+
66+
/**
67+
* Returns the policy file URL to use. Subclasses load from their own resource path.
68+
*/
69+
protected abstract String getPolicyFileUrl();
70+
71+
@Test
72+
void httpCall_whenSecurityManagerActiveWithCorrectPermissions_shouldSucceed() throws Exception {
73+
System.setProperty("java.security.policy", "=" + getPolicyFileUrl());
74+
java.security.Policy.getPolicy().refresh();
75+
System.setSecurityManager(new SecurityManager());
76+
77+
SdkHttpClient client = createHttpClient();
78+
try {
79+
SdkHttpFullRequest request = SdkHttpFullRequest.builder()
80+
.uri(URI.create("http://localhost:" + server.port() + "/"))
81+
.method(SdkHttpMethod.GET)
82+
.build();
83+
HttpExecuteResponse response = client.prepareRequest(
84+
HttpExecuteRequest.builder().request(request).build()).call();
85+
86+
assertThat(response.httpResponse().statusCode()).isEqualTo(200);
87+
} finally {
88+
client.close();
89+
}
90+
}
91+
92+
protected int serverPort() {
93+
return server.port();
94+
}
95+
}

0 commit comments

Comments
 (0)