|
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; |
@@ -100,26 +106,57 @@ static class OpenSearchConfiguration { |
100 | 106 |
|
101 | 107 | @Bean |
102 | 108 | @ConditionalOnMissingBean |
103 | | - OpenSearchClient openSearchClient(OpenSearchConnectionDetails connectionDetails) { |
104 | | - HttpHost[] httpHosts = connectionDetails.getUris() |
105 | | - .stream() |
106 | | - .map(s -> createHttpHost(s)) |
107 | | - .toArray(HttpHost[]::new); |
108 | | - ApacheHttpClient5TransportBuilder transportBuilder = ApacheHttpClient5TransportBuilder.builder(httpHosts); |
109 | | - Optional.ofNullable(connectionDetails.getUsername()) |
110 | | - .map(username -> createBasicCredentialsProvider(httpHosts[0], username, |
111 | | - connectionDetails.getPassword())) |
112 | | - .ifPresent(basicCredentialsProvider -> transportBuilder |
113 | | - .setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder |
114 | | - .setDefaultCredentialsProvider(basicCredentialsProvider))); |
| 109 | + OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, Optional<SslBundles> sslBundles) { |
| 110 | + HttpHost[] httpHosts = properties.getUris().stream().map(this::createHttpHost).toArray(HttpHost[]::new); |
| 111 | + Optional<BasicCredentialsProvider> basicCredentialsProvider = Optional.ofNullable(properties.getUsername()) |
| 112 | + .map(username -> createBasicCredentialsProvider(httpHosts, username, properties.getPassword())); |
| 113 | + |
| 114 | + var transportBuilder = ApacheHttpClient5TransportBuilder.builder(httpHosts); |
| 115 | + transportBuilder.setHttpClientConfigCallback(httpClientBuilder -> { |
| 116 | + basicCredentialsProvider.ifPresent(httpClientBuilder::setDefaultCredentialsProvider); |
| 117 | + httpClientBuilder.setConnectionManager(createConnectionManager(properties, sslBundles)); |
| 118 | + httpClientBuilder.setDefaultRequestConfig(createRequestConfig(properties)); |
| 119 | + return httpClientBuilder; |
| 120 | + }); |
| 121 | + |
115 | 122 | return new OpenSearchClient(transportBuilder.build()); |
116 | 123 | } |
117 | 124 |
|
118 | | - private BasicCredentialsProvider createBasicCredentialsProvider(HttpHost httpHost, String username, |
| 125 | + private AsyncClientConnectionManager createConnectionManager(OpenSearchVectorStoreProperties properties, |
| 126 | + Optional<SslBundles> sslBundles) { |
| 127 | + var connectionManagerBuilder = PoolingAsyncClientConnectionManagerBuilder.create(); |
| 128 | + if (sslBundles.isPresent()) { |
| 129 | + Optional.ofNullable(properties.getSslBundle()) |
| 130 | + .map(bundle -> sslBundles.get().getBundle(bundle)) |
| 131 | + .map(bundle -> ClientTlsStrategyBuilder.create() |
| 132 | + .setSslContext(bundle.createSslContext()) |
| 133 | + .setTlsVersions(bundle.getOptions().getEnabledProtocols()) |
| 134 | + .build()) |
| 135 | + .ifPresent(connectionManagerBuilder::setTlsStrategy); |
| 136 | + } |
| 137 | + return connectionManagerBuilder.build(); |
| 138 | + } |
| 139 | + |
| 140 | + private RequestConfig createRequestConfig(OpenSearchVectorStoreProperties properties) { |
| 141 | + var requestConfigBuilder = RequestConfig.custom(); |
| 142 | + Optional.ofNullable(properties.getConnectionTimeout()) |
| 143 | + .map(Duration::toMillis) |
| 144 | + .ifPresent(timeoutMillis -> requestConfigBuilder.setConnectionRequestTimeout(timeoutMillis, |
| 145 | + TimeUnit.MILLISECONDS)); |
| 146 | + Optional.ofNullable(properties.getReadTimeout()) |
| 147 | + .map(Duration::toMillis) |
| 148 | + .ifPresent( |
| 149 | + timeoutMillis -> requestConfigBuilder.setResponseTimeout(timeoutMillis, TimeUnit.MILLISECONDS)); |
| 150 | + return requestConfigBuilder.build(); |
| 151 | + } |
| 152 | + |
| 153 | + private BasicCredentialsProvider createBasicCredentialsProvider(HttpHost[] httpHosts, String username, |
119 | 154 | String password) { |
120 | 155 | BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); |
121 | | - basicCredentialsProvider.setCredentials(new AuthScope(httpHost), |
122 | | - new UsernamePasswordCredentials(username, password.toCharArray())); |
| 156 | + for (HttpHost httpHost : httpHosts) { |
| 157 | + basicCredentialsProvider.setCredentials(new AuthScope(httpHost), |
| 158 | + new UsernamePasswordCredentials(username, password.toCharArray())); |
| 159 | + } |
123 | 160 | return basicCredentialsProvider; |
124 | 161 | } |
125 | 162 |
|
@@ -147,12 +184,21 @@ PropertiesAwsOpenSearchConnectionDetails awsOpenSearchConnectionDetails( |
147 | 184 |
|
148 | 185 | @Bean |
149 | 186 | @ConditionalOnMissingBean |
150 | | - OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, |
| 187 | + OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, Optional<SslBundles> sslBundles, |
151 | 188 | AwsOpenSearchConnectionDetails connectionDetails, AwsSdk2TransportOptions options) { |
152 | 189 | Region region = Region.of(connectionDetails.getRegion()); |
153 | 190 |
|
154 | | - SdkHttpClient httpClient = ApacheHttpClient.builder().build(); |
155 | | - OpenSearchTransport transport = new AwsSdk2Transport(httpClient, |
| 191 | + var httpClientBuilder = ApacheHttpClient.builder(); |
| 192 | + Optional.ofNullable(properties.getConnectionTimeout()).ifPresent(httpClientBuilder::connectionTimeout); |
| 193 | + Optional.ofNullable(properties.getReadTimeout()).ifPresent(httpClientBuilder::socketTimeout); |
| 194 | + if (sslBundles.isPresent()) { |
| 195 | + Optional.ofNullable(properties.getSslBundle()) |
| 196 | + .map(bundle -> sslBundles.get().getBundle(bundle)) |
| 197 | + .ifPresent(bundle -> httpClientBuilder |
| 198 | + .tlsKeyManagersProvider(() -> bundle.getManagers().getKeyManagers()) |
| 199 | + .tlsTrustManagersProvider(() -> bundle.getManagers().getTrustManagers())); |
| 200 | + } |
| 201 | + OpenSearchTransport transport = new AwsSdk2Transport(httpClientBuilder.build(), |
156 | 202 | connectionDetails.getHost(properties.getAws().getDomainName()), |
157 | 203 | properties.getAws().getServiceName(), region, options); |
158 | 204 | return new OpenSearchClient(transport); |
|
0 commit comments