diff --git a/src/main/java/org/opensearch/commons/authuser/User.java b/src/main/java/org/opensearch/commons/authuser/User.java index f698d36b..1197542d 100644 --- a/src/main/java/org/opensearch/commons/authuser/User.java +++ b/src/main/java/org/opensearch/commons/authuser/User.java @@ -14,7 +14,8 @@ import java.util.Map; import java.util.Objects; -import org.apache.http.util.EntityUtils; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.opensearch.client.Response; import org.opensearch.common.Nullable; import org.opensearch.common.Strings; @@ -83,7 +84,7 @@ public User( * @param response * @throws IOException */ - public User(final Response response) throws IOException { + public User(final Response response) throws IOException, ParseException { this(EntityUtils.toString(response.getEntity())); } diff --git a/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java b/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java index 96bd06ba..e80eb87f 100644 --- a/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java +++ b/src/main/java/org/opensearch/commons/destination/message/LegacyBaseMessage.java @@ -10,7 +10,7 @@ import java.net.URISyntaxException; import java.util.Map; -import org.apache.http.client.utils.URIBuilder; +import org.apache.hc.core5.net.URIBuilder; import org.opensearch.common.Strings; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; diff --git a/src/main/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessage.java b/src/main/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessage.java index dbc4b7df..83561be0 100644 --- a/src/main/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessage.java +++ b/src/main/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessage.java @@ -9,9 +9,9 @@ import java.net.URI; import java.util.Map; -import org.apache.http.client.methods.HttpPatch; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpPut; +import org.apache.hc.client5.http.classic.methods.HttpPatch; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpPut; import org.opensearch.common.Strings; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.common.io.stream.StreamOutput; diff --git a/src/main/java/org/opensearch/commons/rest/SecureRestClientBuilder.java b/src/main/java/org/opensearch/commons/rest/SecureRestClientBuilder.java index 96af1540..c6ef0fc5 100644 --- a/src/main/java/org/opensearch/commons/rest/SecureRestClientBuilder.java +++ b/src/main/java/org/opensearch/commons/rest/SecureRestClientBuilder.java @@ -18,15 +18,20 @@ import javax.net.ssl.SSLContext; -import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; -import org.apache.http.ssl.SSLContextBuilder; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.CredentialsProvider; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; +import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; +import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder; +import org.apache.hc.client5.http.ssl.TrustSelfSignedStrategy; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.nio.ssl.TlsStrategy; +import org.apache.hc.core5.ssl.SSLContextBuilder; +import org.apache.hc.core5.util.Timeout; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.OpenSearchException; @@ -96,7 +101,7 @@ public SecureRestClientBuilder( this.passwd = passWord; this.settings = Settings.EMPTY; this.configPath = null; - hosts.add(new HttpHost(host, port, httpSSLEnabled ? ConfigConstants.HTTPS : ConfigConstants.HTTP)); + hosts.add(new HttpHost(httpSSLEnabled ? ConfigConstants.HTTPS : ConfigConstants.HTTP, host, port)); } /** @@ -129,7 +134,7 @@ public SecureRestClientBuilder(Settings settings, Path configPath) { this.passwd = null; String host = ConfigConstants.HOST_DEFAULT; int port = settings.getAsInt(ConfigConstants.HTTP_PORT, ConfigConstants.HTTP_PORT_DEFAULT); - hosts.add(new HttpHost(host, port, httpSSLEnabled ? ConfigConstants.HTTPS : ConfigConstants.HTTP)); + hosts.add(new HttpHost(httpSSLEnabled ? ConfigConstants.HTTPS : ConfigConstants.HTTP, host, port)); } /** @@ -172,9 +177,9 @@ private RestClientBuilder createRestClientBuilder() throws IOException { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder - .setConnectTimeout(defaultConnectTimeOutMSecs) - .setSocketTimeout(defaultSoTimeoutMSecs) - .setConnectionRequestTimeout(defaultConnRequestTimeoutMSecs); + .setConnectTimeout(Timeout.ofMilliseconds(defaultConnectTimeOutMSecs)) + .setResponseTimeout(Timeout.ofMilliseconds(defaultSoTimeoutMSecs)) + .setConnectionRequestTimeout(Timeout.ofMilliseconds(defaultConnRequestTimeoutMSecs)); } }); @@ -189,7 +194,12 @@ public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder reques @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { if (sslContext != null) { - httpClientBuilder.setSSLContext(sslContext); + TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create().setSslContext(sslContext).build(); + PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder + .create() + .setTlsStrategy(tlsStrategy) + .build(); + httpClientBuilder.setConnectionManager(connectionManager); } if (credentialsProvider != null) { httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); @@ -227,8 +237,8 @@ private CredentialsProvider createCredsProvider() { if (Strings.isNullOrEmpty(user) || Strings.isNullOrEmpty(passwd)) return null; - final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, passwd)); + final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(user, passwd.toCharArray())); return credentialsProvider; } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt index fc805917..ec0f98c5 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt @@ -1,7 +1,7 @@ package org.opensearch.commons.alerting.model import org.apache.commons.validator.routines.UrlValidator -import org.apache.http.client.utils.URIBuilder +import org.apache.hc.core5.net.URIBuilder import org.opensearch.common.CheckedFunction import org.opensearch.common.ParseField import org.opensearch.common.io.stream.StreamInput @@ -13,6 +13,7 @@ import org.opensearch.common.xcontent.XContentParser import org.opensearch.common.xcontent.XContentParserUtils import java.io.IOException import java.net.URI +import java.net.URISyntaxException val ILLEGAL_PATH_PARAMETER_CHARACTERS = arrayOf(':', '"', '+', '\\', '|', '?', '#', '>', '<', ' ') @@ -200,12 +201,21 @@ data class ClusterMetricsInput( * @return The constructed [URI]. */ private fun constructUrlFromInputs(): URI { - val uriBuilder = URIBuilder() - .setScheme(SUPPORTED_SCHEME) - .setHost(SUPPORTED_HOST) - .setPort(SUPPORTED_PORT) - .setPath(path + pathParams) - return uriBuilder.build() + /** + * this try-catch block is required due to a httpcomponents 5.1.x library issue + * it auto encodes path params in the url. + */ + return try { + val uriBuilder = URIBuilder("$SUPPORTED_SCHEME://$SUPPORTED_HOST:$SUPPORTED_PORT$path$pathParams") + uriBuilder.build() + } catch (ex: URISyntaxException) { + val uriBuilder = URIBuilder() + .setScheme(SUPPORTED_SCHEME) + .setHost(SUPPORTED_HOST) + .setPort(SUPPORTED_PORT) + .setPath(path + pathParams) + uriBuilder.build() + } } /** diff --git a/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java b/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java index 7443d060..855e2cbc 100644 --- a/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java +++ b/src/test/java/org/opensearch/commons/destination/message/LegacyCustomWebhookMessageTest.java @@ -12,8 +12,8 @@ import java.util.HashMap; import java.util.Map; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpPost; import org.junit.jupiter.api.Test; import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.StreamInput; diff --git a/src/test/java/org/opensearch/commons/rest/IntegrationTests.java b/src/test/java/org/opensearch/commons/rest/IntegrationTests.java index 0c6b7cfe..e2cdfbfc 100644 --- a/src/test/java/org/opensearch/commons/rest/IntegrationTests.java +++ b/src/test/java/org/opensearch/commons/rest/IntegrationTests.java @@ -17,7 +17,7 @@ import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.http.util.EntityUtils; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.opensearch.client.Request; diff --git a/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt b/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt index 6ab056c5..d6101b61 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt @@ -3,8 +3,8 @@ package org.opensearch.commons.alerting import com.carrotsearch.randomizedtesting.generators.RandomNumbers import com.carrotsearch.randomizedtesting.generators.RandomStrings import junit.framework.TestCase.assertNull -import org.apache.http.Header -import org.apache.http.HttpEntity +import org.apache.hc.core5.http.Header +import org.apache.hc.core5.http.HttpEntity import org.opensearch.client.Request import org.opensearch.client.RequestOptions import org.opensearch.client.Response