diff --git a/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc b/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc index 1aec17f0a..6195cf615 100644 --- a/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc +++ b/docs/modules/ROOT/pages/spring-cloud-openfeign.adoc @@ -177,6 +177,8 @@ Apache HttpClient 5 uses a connection pool to reuse persistent HTTP connections |=== +To disable SSL validation for the HTTP/2 (Java `HttpClient`) client, set `spring.cloud.openfeign.httpclient.http2.disable-ssl-validation` to `true`. Server certificates and host names are then not verified, and the client logs a warning when it is created. Use it only for testing, never in production. This property is separate from `spring.cloud.openfeign.httpclient.disable-ssl-validation`, which does not affect the HTTP/2 client. + If you can not configure Apache HttpClient 5 by using properties, there is an `HttpClient5FeignConfiguration.HttpClientBuilderCustomizer` interface for programmatic configuration. Similarly, to configure the `HttpClientConnectionManager`, you can use `HttpClient5FeignConfiguration.HttpClientConnectionManagerBuilderCustomizer`. Both usages are shown in the example below. TIP: Apache HTTP Components `5.4` have changed defaults in the HttpClient relating to HTTP/1.1 TLS upgrades. Most proxy servers handle upgrades without issue, however, you may encounter issues with Envoy or Istio. If you need to restore previous behaviour, you can use `HttpClient5FeignConfiguration.HttpClientBuilderCustomizer` to do it, as shown in the example below. diff --git a/docs/modules/ROOT/partials/_configprops.adoc b/docs/modules/ROOT/partials/_configprops.adoc index eb112d630..6ef1600b8 100644 --- a/docs/modules/ROOT/partials/_configprops.adoc +++ b/docs/modules/ROOT/partials/_configprops.adoc @@ -101,6 +101,7 @@ |spring.cloud.openfeign.httpclient.hc5.pool-reuse-policy | `+++fifo+++` | Pool connection re-use policies. |spring.cloud.openfeign.httpclient.hc5.socket-timeout | `+++5+++` | Default value for socket timeout. |spring.cloud.openfeign.httpclient.hc5.socket-timeout-unit | `+++seconds+++` | Default value for socket timeout unit. +|spring.cloud.openfeign.httpclient.http2.disable-ssl-validation | `+++false+++` | Whether to disable SSL validation for the HTTP/2 client: server certificates and host names are not verified. Use only for testing. |spring.cloud.openfeign.httpclient.http2.version | `+++HTTP_2+++` | Configure the protocols used by this client to communicate with remote servers. Uses {@link String} value of {@link HttpClient.Version}. |spring.cloud.openfeign.httpclient.max-connections | `+++200+++` | |spring.cloud.openfeign.httpclient.max-connections-per-route | `+++50+++` | diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/DisabledValidationTrustManager.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/DisabledValidationTrustManager.java new file mode 100644 index 000000000..e3b8ee5dc --- /dev/null +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/DisabledValidationTrustManager.java @@ -0,0 +1,69 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.springframework.cloud.openfeign.clientconfig; + +import java.net.Socket; +import java.security.cert.X509Certificate; + +import javax.net.ssl.SSLEngine; +import javax.net.ssl.X509ExtendedTrustManager; + +/** + * Trusts every certificate. Used by {@link HttpClient5FeignConfiguration} for + * {@code spring.cloud.openfeign.httpclient.disable-ssl-validation} and by + * {@link Http2ClientFeignConfiguration} for + * {@code spring.cloud.openfeign.httpclient.http2.disable-ssl-validation}. + *
+ * It extends {@link X509ExtendedTrustManager} because the JDK
+ * {@link java.net.http.HttpClient} delegates hostname verification to an extended trust
+ * manager, so this also skips hostname verification there, like the Apache HttpClient 5
+ * configuration does with its no-op hostname verifier.
+ *
+ * @author Rajesh Vasa
+ */
+final class DisabledValidationTrustManager extends X509ExtendedTrustManager {
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType) {
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType) {
+ }
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType, Socket socket) {
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) {
+ }
+
+ @Override
+ public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
+ }
+
+ @Override
+ public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) {
+ }
+
+ @Override
+ public X509Certificate[] getAcceptedIssuers() {
+ return new X509Certificate[0];
+ }
+
+}
diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/Http2ClientFeignConfiguration.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/Http2ClientFeignConfiguration.java
index 85d18facb..fa13c64aa 100644
--- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/Http2ClientFeignConfiguration.java
+++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/Http2ClientFeignConfiguration.java
@@ -17,9 +17,18 @@
package org.springframework.cloud.openfeign.clientconfig;
import java.net.http.HttpClient;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
import java.time.Duration;
import java.util.List;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.openfeign.clientconfig.http2client.Http2ClientCustomizer;
import org.springframework.cloud.openfeign.support.FeignHttpClientProperties;
@@ -31,19 +40,36 @@
*
* @author changjin wei(魏昌进)
* @author Luis Duarte
+ * @author Rajesh Vasa
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(HttpClient.class)
public class Http2ClientFeignConfiguration {
+ private static final Log LOG = LogFactory.getLog(Http2ClientFeignConfiguration.class);
+
@Bean
@ConditionalOnMissingBean
public HttpClient.Builder httpClientBuilder(FeignHttpClientProperties httpClientProperties) {
- return HttpClient.newBuilder()
+ HttpClient.Builder builder = HttpClient.newBuilder()
.followRedirects(
httpClientProperties.isFollowRedirects() ? HttpClient.Redirect.ALWAYS : HttpClient.Redirect.NEVER)
.version(HttpClient.Version.valueOf(httpClientProperties.getHttp2().getVersion()))
.connectTimeout(Duration.ofMillis(httpClientProperties.getConnectionTimeout()));
+ if (httpClientProperties.getHttp2().isDisableSslValidation()) {
+ LOG.warn("SSL validation is disabled for the HTTP/2 client "
+ + "(spring.cloud.openfeign.httpclient.http2.disable-ssl-validation=true): server certificates and "
+ + "host names are not verified. Do not use this in production.");
+ try {
+ SSLContext sslContext = SSLContext.getInstance("TLS");
+ sslContext.init(null, new TrustManager[] { new DisabledValidationTrustManager() }, new SecureRandom());
+ builder.sslContext(sslContext);
+ }
+ catch (NoSuchAlgorithmException | KeyManagementException e) {
+ LOG.warn("Error creating SSLContext", e);
+ }
+ }
+ return builder;
}
@Bean
diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java
index 8f056cbf9..9acd8b998 100644
--- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java
+++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/clientconfig/HttpClient5FeignConfiguration.java
@@ -19,13 +19,11 @@
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
-import java.security.cert.X509Certificate;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
import jakarta.annotation.PreDestroy;
import org.apache.commons.logging.Log;
@@ -145,23 +143,6 @@ private LayeredConnectionSocketFactory httpsSSLConnectionSocketFactory(boolean i
return sslConnectionSocketFactoryBuilder.build();
}
- static class DisabledValidationTrustManager implements X509TrustManager {
-
- DisabledValidationTrustManager() {
- }
-
- public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
- }
-
- public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
- }
-
- public X509Certificate[] getAcceptedIssuers() {
- return null;
- }
-
- }
-
/**
* Callback interface that customize {@link HttpClientBuilder} objects before
* HttpClient created.
diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java
index 049a8cae8..af0386b2c 100644
--- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java
+++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/FeignHttpClientProperties.java
@@ -337,6 +337,12 @@ public static class Http2Properties {
*/
private String version = "HTTP_2";
+ /**
+ * Whether to disable SSL validation for the HTTP/2 client: server certificates
+ * and host names are not verified. Use only for testing.
+ */
+ private boolean disableSslValidation = DEFAULT_DISABLE_SSL_VALIDATION;
+
public String getVersion() {
return version;
}
@@ -345,6 +351,14 @@ public void setVersion(String version) {
this.version = version;
}
+ public boolean isDisableSslValidation() {
+ return disableSslValidation;
+ }
+
+ public void setDisableSslValidation(boolean disableSslValidation) {
+ this.disableSslValidation = disableSslValidation;
+ }
+
}
}
diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignHttp2ClientDisableSslValidationTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignHttp2ClientDisableSslValidationTests.java
new file mode 100644
index 000000000..c0b162577
--- /dev/null
+++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignHttp2ClientDisableSslValidationTests.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2013-present the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License 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 org.springframework.cloud.openfeign;
+
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyStore;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLHandshakeException;
+
+import com.sun.net.httpserver.HttpsConfigurator;
+import com.sun.net.httpserver.HttpsServer;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import org.springframework.boot.WebApplicationType;
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.test.system.CapturedOutput;
+import org.springframework.boot.test.system.OutputCaptureExtension;
+import org.springframework.context.ConfigurableApplicationContext;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+
+/**
+ * Tests for {@code spring.cloud.openfeign.httpclient.http2.disable-ssl-validation}. The
+ * server uses a self-signed certificate issued for {@code untrusted.invalid}, so both
+ * certificate validation and hostname verification fail unless SSL validation is
+ * disabled, and a warning is logged when it is. The top-level
+ * {@code spring.cloud.openfeign.httpclient.disable-ssl-validation} property does not
+ * affect the HTTP/2 client.
+ *
+ * @author Rajesh Vasa
+ */
+@ExtendWith(OutputCaptureExtension.class)
+class FeignHttp2ClientDisableSslValidationTests {
+
+ private static final String WARNING = "SSL validation is disabled for the HTTP/2 client";
+
+ private static final char[] KEYSTORE_PASSWORD = "changeit".toCharArray();
+
+ private static HttpsServer server;
+
+ @BeforeAll
+ static void startServer() throws Exception {
+ KeyStore keyStore = KeyStore.getInstance("PKCS12");
+ try (InputStream in = FeignHttp2ClientDisableSslValidationTests.class
+ .getResourceAsStream("/http2-self-signed.p12")) {
+ keyStore.load(in, KEYSTORE_PASSWORD);
+ }
+ KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ keyManagerFactory.init(keyStore, KEYSTORE_PASSWORD);
+ SSLContext sslContext = SSLContext.getInstance("TLS");
+ sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
+
+ server = HttpsServer.create(new InetSocketAddress("localhost", 0), 0);
+ server.setHttpsConfigurator(new HttpsConfigurator(sslContext));
+ server.createContext("/", exchange -> {
+ byte[] body = "ok".getBytes(StandardCharsets.UTF_8);
+ exchange.sendResponseHeaders(200, body.length);
+ exchange.getResponseBody().write(body);
+ exchange.close();
+ });
+ server.start();
+ }
+
+ @AfterAll
+ static void stopServer() {
+ if (server != null) {
+ server.stop(0);
+ }
+ }
+
+ @Test
+ void connectsToSelfSignedServerWhenSslValidationIsDisabled(CapturedOutput output) throws Exception {
+ try (ConfigurableApplicationContext context = createContext(true)) {
+ HttpResponse