Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased 3.x]
### Added
- Detect AWS SDK `Apache5HttpClient` in `AwsSdk2Transport` body-method guardrail ([#1903](https://github.com/opensearch-project/opensearch-java/pull/1970))

### Dependencies

Expand Down
1 change: 1 addition & 0 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ dependencies {
testImplementation("software.amazon.awssdk", "http-auth-aws", "[2.21,3.0)")
testImplementation("software.amazon.awssdk", "aws-crt-client", "[2.21,3.0)")
testImplementation("software.amazon.awssdk", "apache-client", "[2.21,3.0)")
testImplementation("software.amazon.awssdk", "apache5-client", "[2.34,3.0)")
testImplementation("software.amazon.awssdk", "netty-nio-client", "[2.21,3.0)")
testImplementation("software.amazon.awssdk", "url-connection-client", "[2.21,3.0)")
testImplementation("software.amazon.awssdk", "sts", "[2.21,3.0)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
import java.net.URLEncoder;
import java.time.Clock;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
Expand Down Expand Up @@ -85,6 +89,23 @@ public class AwsSdk2Transport implements OpenSearchTransport {
public static final Integer DEFAULT_REQUEST_COMPRESSION_SIZE = 8192;

private static final byte[] NO_BYTES = new byte[0];

/**
* FQCNs of Apache-backed AWS SDK HTTP clients (v4, v5) that share the body-on-GET/DELETE limitation.
*/
private static final Set<String> APACHE_HTTP_CLIENT_CLASS_NAMES = Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList("software.amazon.awssdk.http.apache.ApacheHttpClient", "software.amazon.awssdk.http.apache5.Apache5HttpClient")
)
);

/**
* Package-private to allow assertions in unit tests.
*/
static boolean isAwsSdkApacheHttpClient(SdkAutoCloseable httpClient) {
return httpClient instanceof SdkHttpClient && APACHE_HTTP_CLIENT_CLASS_NAMES.contains(httpClient.getClass().getName());
}

private final SdkAutoCloseable httpClient;
private final boolean isApacheHttpClient;
private final String host;
Expand Down Expand Up @@ -196,8 +217,7 @@ private AwsSdk2Transport(
) {
Objects.requireNonNull(host, "Target OpenSearch service host must not be null");
this.httpClient = httpClient;
this.isApacheHttpClient = httpClient instanceof SdkHttpClient
&& httpClient.getClass().getName().equals("software.amazon.awssdk.http.apache.ApacheHttpClient");
this.isApacheHttpClient = isAwsSdkApacheHttpClient(httpClient);
this.host = host;
this.signingServiceName = signingServiceName;
this.signingRegion = signingRegion;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.client.transport.aws;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.http.apache5.Apache5HttpClient;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.utils.SdkAutoCloseable;

public class AwsSdk2TransportApacheDetectionTests {

@Test
public void nullReturnsFalse() {
assertFalse(AwsSdk2Transport.isAwsSdkApacheHttpClient(null));
}

@Test
public void urlConnectionReturnsFalse() {
try (SdkHttpClient client = UrlConnectionHttpClient.builder().build()) {
assertFalse(AwsSdk2Transport.isAwsSdkApacheHttpClient(client));
}
}

@Test
public void apacheV4ReturnsTrue() {
try (SdkHttpClient client = ApacheHttpClient.builder().build()) {
assertTrue(AwsSdk2Transport.isAwsSdkApacheHttpClient(client));
}
}

@Test
public void apacheV5ReturnsTrue() {
try (SdkHttpClient client = Apache5HttpClient.builder().build()) {
assertTrue(AwsSdk2Transport.isAwsSdkApacheHttpClient(client));
}
}

@Test
public void nonHttpClientWithMatchingNameReturnsFalse() {
SdkAutoCloseable notAnHttpClient = () -> {};
assertFalse(AwsSdk2Transport.isAwsSdkApacheHttpClient(notAnHttpClient));
}
}
Loading