Skip to content

Commit

Permalink
Add support of secure OpenSearch clusters in integration tests (#50) (#…
Browse files Browse the repository at this point in the history
…52)

(cherry picked from commit c0aa957)

Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 2d34e35 commit 28cea11
Showing 1 changed file with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@

package org.opensearch.index.codec.rest;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.BasicCredentialsProvider;

import org.opensearch.client.RestClient;
import org.opensearch.client.RestClientBuilder;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.Strings;
import org.opensearch.test.rest.OpenSearchRestTestCase;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.SSLContext;

import static org.opensearch.index.codec.customcodecs.CustomCodecService.ZSTD_CODEC;
import static org.opensearch.index.codec.customcodecs.CustomCodecService.ZSTD_NO_DICT_CODEC;

public class CreateIndexWithCodecIT extends OpenSearchRestTestCase {

public void testCreateIndexWithZstdCodec() throws IOException {
final String index = "test-index";
final String index = "custom-codecs-test-index";

// creating index
createIndex(
Expand All @@ -33,6 +49,65 @@ public void testCreateIndexWithZstdCodec() throws IOException {
.build()
);

ensureGreen(index);
try {
ensureGreen(index);
} finally {
deleteIndex(index);
}
}

@Override
protected RestClient buildClient(Settings settings, HttpHost[] hosts) throws IOException {
RestClientBuilder builder = RestClient.builder(hosts);
configureHttpOrHttpsClient(builder, settings);
builder.setStrictDeprecationMode(true);
return builder.build();
}

protected void configureHttpOrHttpsClient(RestClientBuilder builder, Settings settings) throws IOException {
configureClient(builder, settings);

if (getProtocol().equalsIgnoreCase("https")) {
final String username = System.getProperty("user");
if (Strings.isNullOrEmpty(username)) {
throw new RuntimeException("user name is missing");
}

final String password = System.getProperty("password");
if (Strings.isNullOrEmpty(password)) {
throw new RuntimeException("password is missing");
}

final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

try {
final SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
.build();

builder.setHttpClientConfigCallback(httpClientBuilder -> {
return httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
});
} catch (final NoSuchAlgorithmException | KeyManagementException | KeyStoreException ex) {
throw new IOException(ex);
}
}
}

@Override
protected String getProtocol() {
return Strings.isNullOrEmpty(System.getProperty("https")) ? "http" : "https";
}

/**
* wipeAllIndices won't work since it cannot delete security index
*/
@Override
protected boolean preserveIndicesUponCompletion() {
return true;
}
}

0 comments on commit 28cea11

Please sign in to comment.