|
17 | 17 | package org.springframework.ai.vectorstore.opensearch.autoconfigure; |
18 | 18 |
|
19 | 19 | import java.net.URISyntaxException; |
| 20 | +import java.time.Duration; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.Optional; |
| 23 | +import java.util.concurrent.TimeUnit; |
22 | 24 |
|
23 | 25 | import io.micrometer.observation.ObservationRegistry; |
24 | 26 | import org.apache.hc.client5.http.auth.AuthScope; |
25 | 27 | import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; |
| 28 | +import org.apache.hc.client5.http.config.RequestConfig; |
26 | 29 | import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; |
| 30 | +import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; |
| 31 | +import org.apache.hc.client5.http.nio.AsyncClientConnectionManager; |
| 32 | +import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder; |
27 | 33 | import org.apache.hc.core5.http.HttpHost; |
28 | 34 | import org.opensearch.client.opensearch.OpenSearchClient; |
29 | 35 | import org.opensearch.client.transport.OpenSearchTransport; |
|
33 | 39 | import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
34 | 40 | import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
35 | 41 | import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
36 | | -import software.amazon.awssdk.http.SdkHttpClient; |
37 | 42 | import software.amazon.awssdk.http.apache.ApacheHttpClient; |
38 | 43 | import software.amazon.awssdk.regions.Region; |
39 | 44 |
|
|
50 | 55 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; |
51 | 56 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
52 | 57 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 58 | +import org.springframework.boot.ssl.SslBundles; |
53 | 59 | import org.springframework.context.annotation.Bean; |
54 | 60 | import org.springframework.context.annotation.Configuration; |
55 | 61 | import org.springframework.util.StringUtils; |
@@ -99,26 +105,57 @@ static class OpenSearchConfiguration { |
99 | 105 |
|
100 | 106 | @Bean |
101 | 107 | @ConditionalOnMissingBean |
102 | | - OpenSearchClient openSearchClient(OpenSearchConnectionDetails connectionDetails) { |
103 | | - HttpHost[] httpHosts = connectionDetails.getUris() |
104 | | - .stream() |
105 | | - .map(s -> createHttpHost(s)) |
106 | | - .toArray(HttpHost[]::new); |
107 | | - ApacheHttpClient5TransportBuilder transportBuilder = ApacheHttpClient5TransportBuilder.builder(httpHosts); |
108 | | - Optional.ofNullable(connectionDetails.getUsername()) |
109 | | - .map(username -> createBasicCredentialsProvider(httpHosts[0], username, |
110 | | - connectionDetails.getPassword())) |
111 | | - .ifPresent(basicCredentialsProvider -> transportBuilder |
112 | | - .setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder |
113 | | - .setDefaultCredentialsProvider(basicCredentialsProvider))); |
| 108 | + OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, Optional<SslBundles> sslBundles) { |
| 109 | + HttpHost[] httpHosts = properties.getUris().stream().map(this::createHttpHost).toArray(HttpHost[]::new); |
| 110 | + Optional<BasicCredentialsProvider> basicCredentialsProvider = Optional.ofNullable(properties.getUsername()) |
| 111 | + .map(username -> createBasicCredentialsProvider(httpHosts, username, properties.getPassword())); |
| 112 | + |
| 113 | + var transportBuilder = ApacheHttpClient5TransportBuilder.builder(httpHosts); |
| 114 | + transportBuilder.setHttpClientConfigCallback(httpClientBuilder -> { |
| 115 | + basicCredentialsProvider.ifPresent(httpClientBuilder::setDefaultCredentialsProvider); |
| 116 | + httpClientBuilder.setConnectionManager(createConnectionManager(properties, sslBundles)); |
| 117 | + httpClientBuilder.setDefaultRequestConfig(createRequestConfig(properties)); |
| 118 | + return httpClientBuilder; |
| 119 | + }); |
| 120 | + |
114 | 121 | return new OpenSearchClient(transportBuilder.build()); |
115 | 122 | } |
116 | 123 |
|
117 | | - private BasicCredentialsProvider createBasicCredentialsProvider(HttpHost httpHost, String username, |
| 124 | + private AsyncClientConnectionManager createConnectionManager(OpenSearchVectorStoreProperties properties, |
| 125 | + Optional<SslBundles> sslBundles) { |
| 126 | + var connectionManagerBuilder = PoolingAsyncClientConnectionManagerBuilder.create(); |
| 127 | + if (sslBundles.isPresent()) { |
| 128 | + Optional.ofNullable(properties.getSslBundle()) |
| 129 | + .map(bundle -> sslBundles.get().getBundle(bundle)) |
| 130 | + .map(bundle -> ClientTlsStrategyBuilder.create() |
| 131 | + .setSslContext(bundle.createSslContext()) |
| 132 | + .setTlsVersions(bundle.getOptions().getEnabledProtocols()) |
| 133 | + .build()) |
| 134 | + .ifPresent(connectionManagerBuilder::setTlsStrategy); |
| 135 | + } |
| 136 | + return connectionManagerBuilder.build(); |
| 137 | + } |
| 138 | + |
| 139 | + private RequestConfig createRequestConfig(OpenSearchVectorStoreProperties properties) { |
| 140 | + var requestConfigBuilder = RequestConfig.custom(); |
| 141 | + Optional.ofNullable(properties.getConnectionTimeout()) |
| 142 | + .map(Duration::toMillis) |
| 143 | + .ifPresent(timeoutMillis -> requestConfigBuilder.setConnectionRequestTimeout(timeoutMillis, |
| 144 | + TimeUnit.MILLISECONDS)); |
| 145 | + Optional.ofNullable(properties.getReadTimeout()) |
| 146 | + .map(Duration::toMillis) |
| 147 | + .ifPresent( |
| 148 | + timeoutMillis -> requestConfigBuilder.setResponseTimeout(timeoutMillis, TimeUnit.MILLISECONDS)); |
| 149 | + return requestConfigBuilder.build(); |
| 150 | + } |
| 151 | + |
| 152 | + private BasicCredentialsProvider createBasicCredentialsProvider(HttpHost[] httpHosts, String username, |
118 | 153 | String password) { |
119 | 154 | BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); |
120 | | - basicCredentialsProvider.setCredentials(new AuthScope(httpHost), |
121 | | - new UsernamePasswordCredentials(username, password.toCharArray())); |
| 155 | + for (HttpHost httpHost : httpHosts) { |
| 156 | + basicCredentialsProvider.setCredentials(new AuthScope(httpHost), |
| 157 | + new UsernamePasswordCredentials(username, password.toCharArray())); |
| 158 | + } |
122 | 159 | return basicCredentialsProvider; |
123 | 160 | } |
124 | 161 |
|
@@ -159,12 +196,21 @@ PropertiesAwsOpenSearchConnectionDetails awsOpenSearchConnectionDetails( |
159 | 196 |
|
160 | 197 | @Bean |
161 | 198 | @ConditionalOnMissingBean |
162 | | - OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, |
| 199 | + OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, Optional<SslBundles> sslBundles, |
163 | 200 | AwsOpenSearchConnectionDetails connectionDetails, AwsSdk2TransportOptions options) { |
164 | 201 | Region region = Region.of(connectionDetails.getRegion()); |
165 | 202 |
|
166 | | - SdkHttpClient httpClient = ApacheHttpClient.builder().build(); |
167 | | - OpenSearchTransport transport = new AwsSdk2Transport(httpClient, |
| 203 | + var httpClientBuilder = ApacheHttpClient.builder(); |
| 204 | + Optional.ofNullable(properties.getConnectionTimeout()).ifPresent(httpClientBuilder::connectionTimeout); |
| 205 | + Optional.ofNullable(properties.getReadTimeout()).ifPresent(httpClientBuilder::socketTimeout); |
| 206 | + if (sslBundles.isPresent()) { |
| 207 | + Optional.ofNullable(properties.getSslBundle()) |
| 208 | + .map(bundle -> sslBundles.get().getBundle(bundle)) |
| 209 | + .ifPresent(bundle -> httpClientBuilder |
| 210 | + .tlsKeyManagersProvider(() -> bundle.getManagers().getKeyManagers()) |
| 211 | + .tlsTrustManagersProvider(() -> bundle.getManagers().getTrustManagers())); |
| 212 | + } |
| 213 | + OpenSearchTransport transport = new AwsSdk2Transport(httpClientBuilder.build(), |
168 | 214 | connectionDetails.getHost(properties.getAws().getDomainName()), |
169 | 215 | properties.getAws().getServiceName(), region, options); |
170 | 216 | return new OpenSearchClient(transport); |
|
0 commit comments