| // Copyright (C) 2026 The Android Open Source Project | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| package com.google.gerrit.opensearch; | |
| import com.google.inject.Inject; | |
| import com.google.inject.Singleton; | |
| import java.security.KeyStore; | |
| import javax.net.ssl.SSLContext; | |
| import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; | |
| import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; | |
| import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; | |
| import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder; | |
| import org.apache.hc.core5.http.nio.ssl.TlsStrategy; | |
| import org.apache.hc.core5.reactor.ssl.TlsDetails; | |
| import org.apache.hc.core5.ssl.SSLContextBuilder; | |
| @Singleton | |
| class ContainerRestClientProvider extends RestClientProvider { | |
| private final OpenSearchConfiguration cfg; | |
| private final Container container; | |
| @Inject | |
| ContainerRestClientProvider(OpenSearchConfiguration cfg, Container container) { | |
| super(cfg); | |
| this.cfg = cfg; | |
| this.container = container; | |
| } | |
| @Override | |
| protected void configureHttpClientBuilder(HttpAsyncClientBuilder httpClientBuilder) { | |
| if (container.isSecurityEnabled()) { | |
| try { | |
| KeyStore trustStore = OpenSearchTestUtils.extractTrustStore(container); | |
| SSLContext sslContext = | |
| SSLContextBuilder.create().loadTrustMaterial(trustStore, null).build(); | |
| TlsStrategy tlsStrategy = | |
| ClientTlsStrategyBuilder.create() | |
| .setSslContext(sslContext) | |
| .setTlsDetailsFactory( | |
| sslEngine -> | |
| new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol())) | |
| .build(); | |
| PoolingAsyncClientConnectionManager connectionManager = | |
| PoolingAsyncClientConnectionManagerBuilder.create().setTlsStrategy(tlsStrategy).build(); | |
| httpClientBuilder.setConnectionManager(connectionManager); | |
| } catch (Exception e) { | |
| throw new IllegalStateException("Failed to create SSL context for test container", e); | |
| } | |
| } | |
| } | |
| } |