Skip to content

Commit 19fb139

Browse files
committed
Refactor, change to RestClient
1 parent f1a6ff0 commit 19fb139

19 files changed

Lines changed: 854 additions & 1731 deletions

File tree

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
package org.opendevstack.apiservice.externalservice.aap.config;
22

3+
import lombok.extern.slf4j.Slf4j;
4+
import org.opendevstack.apiservice.externalservice.api.http.RestClientFactory;
35
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.beans.factory.annotation.Value;
47
import org.springframework.boot.context.properties.EnableConfigurationProperties;
5-
import org.springframework.boot.web.client.RestTemplateBuilder;
68
import org.springframework.context.annotation.Bean;
79
import org.springframework.context.annotation.Configuration;
8-
import org.springframework.http.client.SimpleClientHttpRequestFactory;
910
import org.springframework.scheduling.annotation.EnableAsync;
10-
import org.springframework.util.StringUtils;
11-
import org.springframework.web.client.RestTemplate;
12-
13-
import lombok.extern.slf4j.Slf4j;
14-
15-
import javax.net.ssl.HttpsURLConnection;
16-
import javax.net.ssl.SSLContext;
17-
import javax.net.ssl.TrustManagerFactory;
18-
import java.io.FileInputStream;
19-
import java.io.IOException;
20-
import java.net.HttpURLConnection;
21-
import java.security.GeneralSecurityException;
22-
import java.security.KeyStore;
23-
import java.security.SecureRandom;
11+
import org.springframework.web.client.RestClient;
2412

2513
/**
26-
* Configuration class for external service components.
14+
* Configuration class for the Ansible Automation Platform external service.
15+
*
16+
* <p>SSL wiring is delegated to {@link RestClientFactory} in {@code external-service-api};
17+
* no SSL boilerplate lives here.
2718
*/
2819
@Configuration
2920
@EnableAsync
@@ -33,59 +24,25 @@ public class ExternalServiceConfig {
3324

3425
private final SslProperties sslProperties;
3526

27+
@Value("${automation.platform.ansible.timeout:30000}")
28+
private int timeoutMs;
29+
3630
public ExternalServiceConfig(@Qualifier("aapSslProperties") SslProperties sslProperties) {
3731
this.sslProperties = sslProperties;
3832
}
3933

4034
/**
41-
* Creates a RestTemplate bean for HTTP client operations with configurable SSL settings.
35+
* {@link RestClient} bean for the Ansible Automation Platform.
36+
*
37+
* <p>SSL and timeouts are configured via {@code automation.platform.ansible.ssl.*}
38+
* and {@code automation.platform.ansible.timeout} properties respectively.
4239
*
43-
* @return RestTemplate instance with SSL configuration
40+
* @param builder Spring prototype builder (injected fresh per bean definition)
41+
* @return configured {@link RestClient}
4442
*/
4543
@Bean
46-
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
47-
log.info("SSL certificate verification is ENABLED");
48-
return createSecureRestTemplate();
49-
}
50-
51-
private RestTemplate createSecureRestTemplate() {
52-
if (!StringUtils.hasText(sslProperties.getTrustStorePath())) {
53-
log.info("No custom trust store configured, using JVM default trust store");
54-
return new RestTemplate();
55-
}
56-
57-
try {
58-
log.info("Loading custom trust store from: {}", sslProperties.getTrustStorePath());
59-
KeyStore trustStore = KeyStore.getInstance(sslProperties.getTrustStoreType());
60-
try (FileInputStream fis = new FileInputStream(sslProperties.getTrustStorePath())) {
61-
char[] password = StringUtils.hasText(sslProperties.getTrustStorePassword())
62-
? sslProperties.getTrustStorePassword().toCharArray()
63-
: new char[0];
64-
trustStore.load(fis, password);
65-
}
66-
67-
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
68-
tmf.init(trustStore);
69-
70-
SSLContext sslContext = SSLContext.getInstance("TLS");
71-
sslContext.init(null, tmf.getTrustManagers(), new SecureRandom());
72-
73-
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory() {
74-
@Override
75-
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
76-
if (connection instanceof HttpsURLConnection httpsConnection) {
77-
httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory());
78-
}
79-
super.prepareConnection(connection, httpMethod);
80-
}
81-
};
82-
83-
return new RestTemplate(requestFactory);
84-
85-
} catch (GeneralSecurityException | IOException e) {
86-
log.error("Failed to load custom trust store '{}', falling back to JVM default: {}",
87-
sslProperties.getTrustStorePath(), e.getMessage());
88-
return new RestTemplate();
89-
}
44+
public RestClient aapRestClient(RestClient.Builder builder) {
45+
log.info("Creating AAP RestClient (connect/read timeout={}ms)", timeoutMs);
46+
return RestClientFactory.build(builder, sslProperties, timeoutMs, timeoutMs);
9047
}
9148
}
Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,16 @@
11
package org.opendevstack.apiservice.externalservice.aap.config;
22

3+
import org.opendevstack.apiservice.externalservice.api.http.ExternalServiceSslProperties;
34
import org.springframework.boot.context.properties.ConfigurationProperties;
45
import org.springframework.stereotype.Component;
56

67
/**
7-
* Configuration properties for SSL settings in external service calls.
8+
* SSL configuration properties for the Ansible Automation Platform external service.
9+
* Binds to the {@code automation.platform.ansible.ssl} prefix.
810
*/
911
@Component("aapSslProperties")
1012
@ConfigurationProperties(prefix = "automation.platform.ansible.ssl")
11-
public class SslProperties {
12-
13-
/**
14-
* Whether to verify SSL certificates when making external service calls.
15-
* Default is true for security.
16-
*/
17-
private boolean verifyCertificates = true;
18-
19-
/**
20-
* Path to the trust store file for SSL certificate validation.
21-
* Optional - if not provided, uses system default trust store.
22-
*/
23-
private String trustStorePath;
24-
25-
/**
26-
* Password for the trust store.
27-
*/
28-
private String trustStorePassword;
29-
30-
/**
31-
* Type of the trust store (JKS, PKCS12, etc.).
32-
* Default is JKS.
33-
*/
34-
private String trustStoreType = "JKS";
35-
36-
public boolean isVerifyCertificates() {
37-
return verifyCertificates;
38-
}
39-
40-
public void setVerifyCertificates(boolean verifyCertificates) {
41-
this.verifyCertificates = verifyCertificates;
42-
}
43-
44-
public String getTrustStorePath() {
45-
return trustStorePath;
46-
}
47-
48-
public void setTrustStorePath(String trustStorePath) {
49-
this.trustStorePath = trustStorePath;
50-
}
51-
52-
public String getTrustStorePassword() {
53-
return trustStorePassword;
54-
}
55-
56-
public void setTrustStorePassword(String trustStorePassword) {
57-
this.trustStorePassword = trustStorePassword;
58-
}
59-
60-
public String getTrustStoreType() {
61-
return trustStoreType;
62-
}
63-
64-
public void setTrustStoreType(String trustStoreType) {
65-
this.trustStoreType = trustStoreType;
66-
}
67-
}
13+
public class SslProperties extends ExternalServiceSslProperties {
14+
// All fields inherited from ExternalServiceSslProperties.
15+
// Add AAP-specific SSL overrides here if ever needed.
16+
}

0 commit comments

Comments
 (0)