Merge pull request #918 from Alfresco/feature/ACS-6625_remove_netty_from_ATS

Test removing reactor netty and instead usage of reactor jetty client…
This commit is contained in:
Marcin Strankowski 2024-01-30 12:18:30 +01:00 committed by GitHub
commit 9bbdb003fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 65 deletions

View File

@ -35,6 +35,16 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-reactor-netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-reactive-httpclient</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.retry</groupId> <groupId>org.springframework.retry</groupId>

View File

@ -26,8 +26,6 @@
*/ */
package org.alfresco.transform.base.config; package org.alfresco.transform.base.config;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import org.alfresco.transform.base.WebClientBuilderAdjuster; import org.alfresco.transform.base.WebClientBuilderAdjuster;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
@ -41,21 +39,19 @@ import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.ssl.TLS; import org.apache.hc.core5.http.ssl.TLS;
import org.apache.hc.core5.ssl.SSLContextBuilder; import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.http.client.reactive.JettyClientHttpConnector;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import reactor.netty.http.client.HttpClient;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.TrustManagerFactory;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.KeyManagementException; import java.security.KeyManagementException;
@ -90,35 +86,33 @@ public class MTLSConfig {
private boolean hostNameVerificationDisabled; private boolean hostNameVerificationDisabled;
@Bean @Bean
public WebClientBuilderAdjuster webClientBuilderAdjuster(SslContextBuilder nettySslContextBuilder) public WebClientBuilderAdjuster webClientBuilderAdjuster(SslContextFactory.Client sslContextFactory)
{ {
return builder -> { return builder -> {
if(isTlsOrMtlsConfigured()) if(isTlsOrMtlsConfigured())
{ {
HttpClient httpClientWithSslContext = null; ClientConnector clientConnector = new ClientConnector();
try { clientConnector.setSslContextFactory(sslContextFactory);
httpClientWithSslContext = createHttpClientWithSslContext(nettySslContextBuilder); HttpClient httpClientWithSslContext = new HttpClient(new HttpClientTransportDynamic(clientConnector));
} catch (SSLException e) {
throw new RuntimeException(e); builder.clientConnector(new JettyClientHttpConnector(httpClientWithSslContext));
}
builder.clientConnector(new ReactorClientHttpConnector(httpClientWithSslContext));
} }
}; };
} }
@Bean @Bean
public RestTemplate restTemplate(SSLContextBuilder apacheSSLContextBuilder) throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, UnrecoverableKeyException public RestTemplate restTemplate(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException
{ {
if(isTlsOrMtlsConfigured()) if(isTlsOrMtlsConfigured())
{ {
return createRestTemplateWithSslContext(apacheSSLContextBuilder); return createRestTemplateWithSslContext(sslContextBuilder);
} else { } else {
return new RestTemplate(); return new RestTemplate();
} }
} }
@Bean @Bean
public SSLContextBuilder apacheSSLContextBuilder() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException { public SSLContextBuilder sslContextBuilder() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
if(isKeystoreConfigured()) if(isKeystoreConfigured())
{ {
@ -136,21 +130,15 @@ public class MTLSConfig {
} }
@Bean @Bean
public SslContextBuilder nettySslContextBuilder() throws UnrecoverableKeyException, CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException { public SslContextFactory.Client sslContextFactory(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient(); {
if(isKeystoreConfigured()) SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
{ sslContextFactory.setSslContext(sslContextBuilder.build());
KeyManagerFactory keyManagerFactory = initKeyManagerFactory(); sslContextFactory.setTrustAll(false);
sslContextBuilder.keyManager(keyManagerFactory); sslContextFactory.setEndpointIdentificationAlgorithm(hostNameVerificationDisabled ? "" : "HTTPS");
} sslContextFactory.setIncludeProtocols(TLS.V_1_2.getId(), TLS.V_1_3.getId());
if(isTruststoreConfigured()) return sslContextFactory;
{
TrustManagerFactory trustManagerFactory = initTrustManagerFactory();
sslContextBuilder.trustManager(trustManagerFactory);
}
return sslContextBuilder;
} }
private boolean isTlsOrMtlsConfigured() private boolean isTlsOrMtlsConfigured()
@ -168,21 +156,6 @@ public class MTLSConfig {
return keyStoreResource != null; return keyStoreResource != null;
} }
private HttpClient createHttpClientWithSslContext(SslContextBuilder sslContextBuilder) throws SSLException {
SslContext sslContext = sslContextBuilder.build();
return HttpClient.create().secure(p -> p.sslContext(sslContext).handlerConfigurator(handler -> {
SSLEngine sslEngine = handler.engine();
SSLParameters sslParameters = sslEngine.getSSLParameters();
if(hostNameVerificationDisabled)
{
sslParameters.setEndpointIdentificationAlgorithm("");
} else {
sslParameters.setEndpointIdentificationAlgorithm("HTTPS");
}
sslEngine.setSSLParameters(sslParameters);
}));
}
private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException { private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException {
final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder = final SSLConnectionSocketFactoryBuilder sslConnectionSocketFactoryBuilder =
SSLConnectionSocketFactoryBuilder.create() SSLConnectionSocketFactoryBuilder.create()
@ -215,20 +188,4 @@ public class MTLSConfig {
} }
return keyStore; return keyStore;
} }
private TrustManagerFactory initTrustManagerFactory() throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException
{
KeyStore trustStore = getKeyStore(trustStoreType, trustStoreResource, trustStorePassword);
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
return trustManagerFactory;
}
private KeyManagerFactory initKeyManagerFactory() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException
{
KeyStore clientKeyStore = getKeyStore(keyStoreType, keyStoreResource, keyStorePassword);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(clientKeyStore, keyStorePassword);
return keyManagerFactory;
}
} }