Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure an SSL capable ClientHttpRequestFactory for the AclClient RestTemplate #71

Merged
merged 1 commit into from
Aug 12, 2024
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
2 changes: 1 addition & 1 deletion compose/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
COMPOSE_PROJECT_NAME=acldev
TAG=2.3-SNAPSHOT
GATEWAY_TAG=1.7.0
GATEWAY_TAG=1.8.10
8 changes: 3 additions & 5 deletions compose/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ services:
condition: service_healthy
required: true
ports:
- 8080:8080
- 8081:8081
- 8180:8080
- 8181:8081
- 15005:15005
deploy:
resources:
Expand All @@ -57,9 +57,7 @@ services:
user: 1000:1000
environment:
SPRING_PROFILES_ACTIVE: standalone
GEOSERVER_BASE_PATH: /geoserver/cloud
volumes:
- ./gateway-service.yml:/etc/geoserver/gateway-service.yml
TARGETS_ACL: http://acl:8080
ports:
- 9090:8080
deploy:
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@
<artifactId>gs-acl-plugin-accessmanager</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.geoserver.acl.plugin</groupId>
<artifactId>gs-acl-plugin-accessmanager</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.geoserver.acl.plugin</groupId>
<artifactId>gs-acl-plugin-client</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.TrustStrategy;
import org.geoserver.acl.api.client.ApiClient;
import org.geoserver.acl.api.client.AuthorizationApi;
import org.geoserver.acl.api.client.DataRulesApi;
Expand All @@ -20,10 +25,16 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.net.ssl.SSLContext;

public class AclClient {

private ApiClient apiClient;
Expand Down Expand Up @@ -93,11 +104,13 @@ public AuthorizationApi getAuthorizationApi() {

static RestTemplate createRestTemplate() {

// Use Apache HttpComponents HttpClient, otherwise
// SimpleClientHttpRequestFactory fails on
// Use Apache HttpComponents HttpClient, otherwise SimpleClientHttpRequestFactory fails on
// PATCH requests
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
// ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

ClientHttpRequestFactory requestFactory = getClientHttpRequestFactoryForHttps();
RestTemplate restTemplate = new RestTemplate(requestFactory);

// This allows us to read the response more than once - Necessary for debugging
restTemplate.setRequestFactory(
new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
Expand All @@ -109,7 +122,7 @@ static RestTemplate createRestTemplate() {

List<HttpMessageConverter<?>> messageConverters =
restTemplate.getMessageConverters().stream()
.filter(m -> !(MappingJackson2HttpMessageConverter.class.isInstance(m)))
.filter(m -> !(m instanceof MappingJackson2HttpMessageConverter))
.collect(Collectors.toCollection(ArrayList::new));

ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -120,4 +133,25 @@ static RestTemplate createRestTemplate() {

return restTemplate;
}

static ClientHttpRequestFactory getClientHttpRequestFactoryForHttps() {

TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext;
try {
sslContext =
org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new IllegalStateException(e);
}
SSLConnectionSocketFactory csf =
new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
return requestFactory;
}
}
Loading