Skip to content

Commit f783196

Browse files
authored
Revert "Revert throwing on AwsSdk2 + ApacheHttpClient (opensearch-project#1333)" (opensearch-project#1338)
This reverts commit 4279d36. Signed-off-by: Thomas Farr <[email protected]>
1 parent 4279d36 commit f783196

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

CHANGELOG.md

-2
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ This section is for maintaining a changelog for all breaking changes for the cli
4343
### Added
4444

4545
### Dependencies
46-
- Upgraded aws-sdk-java dependencies to at least `2.19.22` ([#1333](https://github.com/opensearch-project/opensearch-java/pull/1333))
4746

4847
### Changed
4948

5049
### Deprecated
5150

5251
### Removed
53-
- Removed throwing of exception when using `AwsSdk2Transport` with AWS SDK's `ApacheHttpClient` to make an DELETE/GET request with a body ([#1333](https://github.com/opensearch-project/opensearch-java/pull/1333))
5452

5553
### Fixed
5654
- Fixed an issue where `FieldSort` was not implementing `SortOptionsVariant` ([#1323](https://github.com/opensearch-project/opensearch-java/pull/1323))

guides/auth.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
Requests to [OpenSearch Service and OpenSearch Serverless](https://docs.aws.amazon.com/opensearch-service/index.html) must be signed using the AWS signing protocol. Use `AwsSdk2Transport` to send signed requests.
99

1010
> ⚠️ **Warning** ⚠️
11-
> If you are using `software.amazon.awssdk.http.apache.ApacheHttpClient` please ensure you use at least version `2.29.22` as earlier versions do not support request bodies on GET or DELETE requests, which leads to incorrect handling of requests such as `OpenSearchClient.clearScroll()` and `OpenSearchClient.deletePit()`.
11+
> Using `software.amazon.awssdk.http.apache.ApacheHttpClient` is discouraged as it does not support request bodies on GET or DELETE requests.
12+
> This leads to incorrect handling of requests such as `OpenSearchClient.clearScroll()` and `OpenSearchClient.deletePit()`.
13+
> As such `AwsSdk2Transport` will throw a `TransportException` if an unsupported request is encountered while using `ApacheHttpClient`.
1214
1315
```java
1416
SdkHttpClient httpClient = AwsCrtHttpClient.builder().build();

java-client/build.gradle.kts

+11-11
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,17 @@ dependencies {
220220
testImplementation("com.fasterxml.jackson.datatype", "jackson-datatype-jakarta-jsonp", jacksonVersion)
221221

222222
// For AwsSdk2Transport
223-
"awsSdk2SupportCompileOnly"("software.amazon.awssdk", "sdk-core", "[2.29.22,3.0)")
224-
"awsSdk2SupportCompileOnly"("software.amazon.awssdk", "auth", "[2.29.22,3.0)")
225-
"awsSdk2SupportCompileOnly"("software.amazon.awssdk", "http-auth-aws", "[2.29.22,3.0)")
226-
testImplementation("software.amazon.awssdk", "sdk-core", "[2.29.22,3.0)")
227-
testImplementation("software.amazon.awssdk", "auth", "[2.29.22,3.0)")
228-
testImplementation("software.amazon.awssdk", "http-auth-aws", "[2.29.22,3.0)")
229-
testImplementation("software.amazon.awssdk", "aws-crt-client", "[2.29.22,3.0)")
230-
testImplementation("software.amazon.awssdk", "apache-client", "[2.29.22,3.0)")
231-
testImplementation("software.amazon.awssdk", "netty-nio-client", "[2.29.22,3.0)")
232-
testImplementation("software.amazon.awssdk", "url-connection-client", "[2.29.22,3.0)")
233-
testImplementation("software.amazon.awssdk", "sts", "[2.29.22,3.0)")
223+
"awsSdk2SupportCompileOnly"("software.amazon.awssdk", "sdk-core", "[2.21,3.0)")
224+
"awsSdk2SupportCompileOnly"("software.amazon.awssdk", "auth", "[2.21,3.0)")
225+
"awsSdk2SupportCompileOnly"("software.amazon.awssdk", "http-auth-aws", "[2.21,3.0)")
226+
testImplementation("software.amazon.awssdk", "sdk-core", "[2.21,3.0)")
227+
testImplementation("software.amazon.awssdk", "auth", "[2.21,3.0)")
228+
testImplementation("software.amazon.awssdk", "http-auth-aws", "[2.21,3.0)")
229+
testImplementation("software.amazon.awssdk", "aws-crt-client", "[2.21,3.0)")
230+
testImplementation("software.amazon.awssdk", "apache-client", "[2.21,3.0)")
231+
testImplementation("software.amazon.awssdk", "netty-nio-client", "[2.21,3.0)")
232+
testImplementation("software.amazon.awssdk", "url-connection-client", "[2.21,3.0)")
233+
testImplementation("software.amazon.awssdk", "sts", "[2.21,3.0)")
234234

235235
testImplementation("org.apache.logging.log4j", "log4j-api","[2.17.1,3.0)")
236236
testImplementation("org.apache.logging.log4j", "log4j-core","[2.17.1,3.0)")

java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class AwsSdk2Transport implements OpenSearchTransport {
8585

8686
private static final byte[] NO_BYTES = new byte[0];
8787
private final SdkAutoCloseable httpClient;
88+
private final boolean isApacheHttpClient;
8889
private final String host;
8990
private final String signingServiceName;
9091
private final Region signingRegion;
@@ -194,6 +195,8 @@ private AwsSdk2Transport(
194195
) {
195196
Objects.requireNonNull(host, "Target OpenSearch service host must not be null");
196197
this.httpClient = httpClient;
198+
this.isApacheHttpClient = httpClient instanceof SdkHttpClient
199+
&& httpClient.getClass().getName().equals("software.amazon.awssdk.http.apache.ApacheHttpClient");
197200
this.host = host;
198201
this.signingServiceName = signingServiceName;
199202
this.signingRegion = signingRegion;
@@ -296,9 +299,25 @@ private <RequestT> SignedRequest prepareRequest(
296299
Endpoint<RequestT, ?, ?> endpoint,
297300
@CheckForNull TransportOptions options,
298301
@CheckForNull OpenSearchRequestBodyBuffer body
299-
) throws UnsupportedEncodingException {
302+
) throws UnsupportedEncodingException, TransportException {
300303
SdkHttpMethod method = SdkHttpMethod.fromValue(endpoint.method(request));
301304

305+
// AWS Apache Http Client only supports request bodies on PATCH, POST & PUT requests.
306+
// See:
307+
// https://github.com/aws/aws-sdk-java-v2/blob/master/http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/internal/impl/ApacheHttpRequestFactory.java#L118-L137
308+
if (isApacheHttpClient
309+
&& method != SdkHttpMethod.PATCH
310+
&& method != SdkHttpMethod.POST
311+
&& method != SdkHttpMethod.PUT
312+
&& body != null
313+
&& body.getContentLength() > 0) {
314+
throw new TransportException(
315+
"AWS SDK's ApacheHttpClient does not support request bodies for HTTP method `"
316+
+ method
317+
+ "`. Please use a different SdkHttpClient implementation."
318+
);
319+
}
320+
302321
SdkHttpFullRequest.Builder req = SdkHttpFullRequest.builder().method(method);
303322

304323
StringBuilder url = new StringBuilder();

java-client/src/test/java/org/opensearch/client/transport/aws/AwsSdk2TransportTests.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.apache.hc.core5.http.HttpHeaders.CONTENT_TYPE;
1414
import static org.junit.Assert.assertEquals;
1515
import static org.junit.Assert.assertNotNull;
16+
import static org.junit.Assert.assertThrows;
1617

1718
import java.io.ByteArrayInputStream;
1819
import java.io.ByteArrayOutputStream;
@@ -56,6 +57,7 @@
5657
import org.junit.runners.Parameterized;
5758
import org.opensearch.client.opensearch.OpenSearchClient;
5859
import org.opensearch.client.opensearch.generic.Requests;
60+
import org.opensearch.client.transport.TransportException;
5961
import org.opensearch.client.transport.util.FunnellingHttpsProxy;
6062
import org.opensearch.client.transport.util.SelfSignedCertificateAuthority;
6163
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
@@ -408,7 +410,27 @@ private void assertSigV4Request(
408410
String contentSha256,
409411
String expectedSignature
410412
) throws Exception {
411-
request.invoke(getTestClient());
413+
OpenSearchClient client = getTestClient();
414+
415+
if (sdkHttpClientType != SdkHttpClientType.APACHE
416+
|| contentLength == 0
417+
|| "PATCH".equals(method)
418+
|| "POST".equals(method)
419+
|| "PUT".equals(method)) {
420+
request.invoke(client);
421+
} else {
422+
// AWS Apache Http Client only supports content on PATCH, POST & PUT requests.
423+
// See:
424+
// https://github.com/aws/aws-sdk-java-v2/blob/master/http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/internal/impl/ApacheHttpRequestFactory.java#L118-L137
425+
assertThrows(
426+
"AWS SDK's ApacheHttpClient does not support request bodies for HTTP method `"
427+
+ method
428+
+ "`. Please use a different SdkHttpClient implementation.",
429+
TransportException.class,
430+
() -> request.invoke(client)
431+
);
432+
return;
433+
}
412434

413435
assertEquals(1, receivedRequests.size());
414436
ReceivedRequest req = receivedRequests.poll();

0 commit comments

Comments
 (0)