How to use createSslContextFromCa method of org.testcontainers.elasticsearch.ElasticsearchContainer class

Best Testcontainers-java code snippet using org.testcontainers.elasticsearch.ElasticsearchContainer.createSslContextFromCa

Source:ElasticsearchContainerTest.java Github

copy

Full Screen

...384 RestClient385 .builder(HttpHost.create("https://" + container.getHttpHostAddress()))386 .setHttpClientConfigCallback(httpClientBuilder -> {387 httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);388 httpClientBuilder.setSSLContext(container.createSslContextFromCa());389 return httpClientBuilder;390 })391 .build();392 return client.performRequest(new Request("GET", "/_cluster/health"));393 }394 private RestClient getClient(ElasticsearchContainer container) {395 if (client == null) {396 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();397 credentialsProvider.setCredentials(398 AuthScope.ANY,399 new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)400 );401 String protocol = container.caCertAsBytes().isPresent() ? "https://" : "http://";402 client =403 RestClient404 .builder(HttpHost.create(protocol + container.getHttpHostAddress()))405 .setHttpClientConfigCallback(httpClientBuilder -> {406 if (container.caCertAsBytes().isPresent()) {407 httpClientBuilder.setSSLContext(container.createSslContextFromCa());408 }409 return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);410 })411 .build();412 }413 return client;414 }415 private RestClient getAnonymousClient(ElasticsearchContainer container) {416 if (anonymousClient == null) {417 anonymousClient = RestClient.builder(HttpHost.create(container.getHttpHostAddress())).build();418 }419 return anonymousClient;420 }421 private void assertElasticsearchContainerHasHeapSize(ElasticsearchContainer container, long heapSizeInBytes)...

Full Screen

Full Screen

Source:ElasticsearchTestServer.java Github

copy

Full Screen

...89 container.start();90 int port = container.getMappedPort(9200);91 boolean useTLS = version.major() >= 8;92 HttpHost host = new HttpHost("localhost", port, useTLS ? "https": "http");93 SSLContext sslContext = useTLS ? container.createSslContextFromCa() : null;94 BasicCredentialsProvider credsProv = new BasicCredentialsProvider();95 credsProv.setCredentials(96 AuthScope.ANY, new UsernamePasswordCredentials("elastic", "changeme")97 );98 restClient = RestClient.builder(host)99 .setHttpClientConfigCallback(hc -> hc100 .setDefaultCredentialsProvider(credsProv)101 .setSSLContext(sslContext)102 )103 .build();104 transport = new RestClientTransport(restClient, mapper);105 client = new ElasticsearchClient(transport);106 return this;107 }...

Full Screen

Full Screen

Source:ElasticsearchAppenderTest.java Github

copy

Full Screen

...67 // Given68 String id = UUID.randomUUID().toString();69 String logger = getRootLogger().getClass().getCanonicalName();70 String address = "https://" + container.getHttpHostAddress();71 SSLContext ssl = new SSLContextBuilder().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build(); //container.createSslContextFromCa()72 ElasticsearchClient client = createClient(address, ssl, ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD);73 ElasticsearchAppender appender = createAppender("myApplication", getLocalHost().getHostName(), address, parallel);74 ThreadContext.put("key", "value");75 // When76 Log4jLogEvent event = Log4jLogEvent.newBuilder()77 .setMessage(new SimpleMessage(id))78 .setLoggerFqcn(logger)79 .setThrown(new RuntimeException())80 .setLevel(INFO)81 .build();82 appender.append(event);83 sleep(5000);84 appender.stop();85 // Then...

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.elasticsearch.client.Client;3import org.elasticsearch.client.transport.TransportClient;4import org.elasticsearch.common.settings.Settings;5import org.elasticsearch.common.transport.InetSocketTransportAddress;6import org.elasticsearch.common.transport.TransportAddress;7import org.elasticsearch.transport.client.PreBuiltTransportClient;8import java.io.IOException;9import java.net.InetAddress;10import java.net.UnknownHostException;11import java.security.KeyManagementException;12import java.security.KeyStoreException;13import java.security.NoSuchAlgorithmException;14import java.security.UnrecoverableKeyException;15import java.security.cert.CertificateException;16import java.util.Arrays;17import java.util.List;18public class TestContainerElasticSearch {19 public static void main(String[] args) throws IOException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {20 ElasticsearchContainer container = new ElasticsearchContainer();21 container.start();22 Settings settings = Settings.builder()23 .put("cluster.name", container.getClusterName())24 .put("xpack.security.transport.ssl.enabled", true)25 .put("xpack.security.user", "elastic:changeme")26 .put("xpack.security.transport.ssl.verification_mode", "certificate")27 .put("xpack.security.transport.ssl.keystore.path", "/usr/share/elasticsearch/config/elastic-certificates.p12")28 .put("xpack.security.transport.ssl.truststore.path", "/usr/share/elasticsearch/config/elastic-certificates.p12")29 .put("xpack.security.transport.ssl.keystore.password", "changeme")30 .put("xpack.security.transport.ssl.truststore.password", "changeme")31 .build();32 List<TransportAddress> transportAddresses = Arrays.asList(33 new InetSocketTransportAddress(InetAddress.getByName(container.getContainerIpAddress()), container.getMappedPort(9300))34 );35 Client client = new PreBuiltTransportClient(settings)36 .addTransportAddresses(transportAddresses.toArray(new TransportAddress[0]));37 client.admin().cluster().prepareHealth().get();38 }39}40package org.testcontainers.elasticsearch;41import org.elasticsearch.client.Client;42import org.elasticsearch.client.transport.TransportClient;43import org.elasticsearch.common.settings.Settings;44import org.elasticsearch.common.transport.InetSocketTransportAddress;45import org.elasticsearch.common.transport.TransportAddress;46import org.elasticsearch.transport.client.PreBuiltTransportClient;47import java.io.IOException;48import java.net.InetAddress

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.elasticsearch.ElasticsearchContainer;2import org.testcontainers.utility.DockerImageName;3import org.testcontainers.containers.Network;4import org.testcontainers.containers.GenericContainer;5import org.testcontainers.containers.output.Slf4jLogConsumer;6import java.io.File;7import java.io.IOException;8import java.nio.charset.StandardCharsets;9import java.nio.file.Files;10import java.nio.file.Paths;11import java.security.KeyManagementException;12import java.security.KeyStoreException;13import java.security.NoSuchAlgorithmException;14import java.security.cert.CertificateException;15import java.util.Base64;16import java.util.Map;17import javax.net.ssl.SSLContext;18import org.apache.http.HttpHost;19import org.apache.http.auth.AuthScope;20import org.apache.http.auth.UsernamePasswordCredentials;21import org.apache.http.client.CredentialsProvider;22import org.apache.http.client.config.RequestConfig;23import org.apache.http.client.methods.HttpGet;24import org.apache.http.conn.ssl.SSLConnectionSocketFactory;25import org.apache.http.conn.ssl.SSLContextBuilder;26import org.apache.http.conn.ssl.TrustStrategy;27import org.apache.http.impl.client.BasicCredentialsProvider;28import org.apache.http.impl.client.CloseableHttpClient;29import org.apache.http.impl.client.HttpClients;30import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;31import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;32import org.apache.http.ssl.SSLContexts;33import org.apache.http.util.EntityUtils;34import org.elasticsearch.client.Request;35import org.elasticsearch.client.RequestOptions;36import org.elasticsearch.client.Response;37import org.elasticsearch.client.RestClient;38import org.elasticsearch.client.RestClientBuilder;39import org.elasticsearch.client.RestHighLevelClient;40import org.junit.Test;41public class TestContainerElasticSearch {42 public void testElasticSearch() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException {43 String elasticSearchVersion = "7.10.2";44 String elasticSearchImage = "docker.elastic.co/elasticsearch/elasticsearch:" + elasticSearchVersion;45 DockerImageName imageName = DockerImageName.parse(elasticSearchImage);46 ElasticsearchContainer container = new ElasticsearchContainer(imageName);47 container.start();48 RestHighLevelClient client = new RestHighLevelClient(49 RestClient.builder(50 new HttpHost(container.getContainerIpAddress(), container.getFirstMappedPort(), "https"))51 .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.GenericContainer;2import org.testcontainers.elasticsearch.ElasticsearchContainer;3public class 1 {4 public static void main(String[] args) {5 ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:6.7.0");6 container.withEnv("xpack.security.enabled", "true");7 container.withEnv("xpack.security.http.ssl.enabled", "true");8 container.withEnv("xpack.security.http.ssl.key", "/usr/share/elasticsearch/config/elastic-certificates.p12");9 container.withEnv("xpack.security.http.ssl.key_passphrase", "changeit");10 container.withEnv("xpack.security.http.ssl.certificate", "/usr/share/elasticsearch/config/elastic-certificates.p12");11 container.withEnv("xpack.security.http.ssl.certificate_authorities", "/usr/share/elasticsearch/config/elastic-certificates.p12");12 container.withEnv("xpack.security.http.ssl.verification_mode", "certificate");13 container.withEnv("xpack.security.transport.ssl.enabled", "true");14 container.withEnv("xpack.security.transport.ssl.key", "/usr/share/elasticsearch/config/elastic-certificates.p12");15 container.withEnv("xpack.security.transport.ssl.key_passphrase", "changeit");16 container.withEnv("xpack.security.transport.ssl.certificate", "/usr/share/elasticsearch/config/elastic-certificates.p12");17 container.withEnv("xpack.security.transport.ssl.certificate_authorities", "/usr/share/elasticsearch/config/elastic-certificates.p12");18 container.withEnv("xpack.security.transport.ssl.verification_mode", "certificate");19 container.withEnv("xpack.security.authc.api_key.enabled", "true");20 container.withEnv("xpack.security.authc.realms.file.type", "file");21 container.withEnv("xpack.security.authc.realms.file.order", "0");22 container.withEnv("xpack.security.authc.realms.file.enabled", "true");23 container.withEnv("xpack.security.authc.realms.file.file.users", "elastic:changeme");24 container.withEnv("xpack.security.authc.realms.file.file.users_hash", "changeme");25 container.withEnv("xpack.security.authc.realms.file.file.roles", "admin:admin");26 container.withEnv("xpack.security.authc.realms.file.file.roles_hash", "admin");27 container.withEnv("xpack.security.authc.realms.file.file

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.elasticsearch;2import org.testcontainers.containers.Network;3import org.testcontainers.utility.DockerImageName;4import org.testcontainers.utility.SSLUtils;5import javax.net.ssl.SSLContext;6import java.io.IOException;7import java.security.GeneralSecurityException;8public class CreateSslContextFromCa {9 public static void main(String[] args) throws IOException, GeneralSecurityException {10 final Network network = Network.newNetwork();11 final ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.12.1"))12 .withNetwork(network)13 .withNetworkAliases("elasticsearch")14 .withEnv("xpack.security.enabled", "true")15 .withEnv("xpack.security.transport.ssl.enabled", "true")16 .withEnv("xpack.security.transport.ssl.verification_mode", "certificate")17 .withEnv("xpack.security.transport.ssl.keystore.path", "elastic-certificates.p12")18 .withEnv("xpack.security.transport.ssl.truststore.path", "elastic-certificates.p12");19 elasticsearchContainer.start();20 final SSLContext sslContext = elasticsearchContainer.createSslContextFromCa();21 System.out.println(sslContext);22 }23}24package org.testcontainers.elasticsearch;25import org.testcontainers.containers.Network;26import org.testcontainers.utility.DockerImageName;27import org.testcontainers.utility.SSLUtils;28import javax.net.ssl.SSLContext;29import java.io.IOException;30import java.security.GeneralSecurityException;31public class CreateSslContextFromCa {32 public static void main(String[] args) throws IOException, GeneralSecurityException {33 final Network network = Network.newNetwork();34 final ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.12.1"))35 .withNetwork(network)36 .withNetworkAliases("elasticsearch")37 .withEnv("xpack.security.enabled", "true")38 .withEnv("xpack.security.transport.ssl.enabled", "true")39 .withEnv("xpack.security.transport.ssl.verification_mode", "certificate")40 .withEnv("xpack.security.transport.ssl.keystore.path", "elastic-cert

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.elasticsearch.ElasticsearchContainer;2public class 1 {3 public static void main(String[] args) {4 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer("elasticsearch:6.2.2");5 elasticsearchContainer.start();6 elasticsearchContainer.createSslContextFromCa();7 }8}9import org.testcontainers.elasticsearch.ElasticsearchContainer;10public class 2 {11 public static void main(String[] args) {12 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer("elasticsearch:6.2.2");13 elasticsearchContainer.start();14 elasticsearchContainer.createSslContextFromCa();15 }16}

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1package com.testcontainers;2import org.testcontainers.containers.GenericContainer;3import org.testcontainers.elasticsearch.ElasticsearchContainer;4import javax.net.ssl.SSLContext;5public class Main {6 public static void main(String[] args) {7 try (GenericContainer container = new ElasticsearchContainer()) {8 container.start();9 String host = container.getContainerIpAddress();10 int port = container.getMappedPort(9200);11 SSLContext sslContext = ElasticsearchContainer.createSslContextFromCa(container);12 }13 }14}15package com.testcontainers;16import org.testcontainers.elasticsearch.ElasticsearchContainer;17import javax.net.ssl.SSLContext;18public class Main {19 public static void main(String[] args) {20 try (ElasticsearchContainer container = new ElasticsearchContainer()) {21 container.start();22 String host = container.getContainerIpAddress();23 int port = container.getMappedPort(9200);24 SSLContext sslContext = ElasticsearchContainer.createSslContextFromCa(container);25 }26 }27}28Error:(16, 42) java: cannot find symbol29 symbol: method createSslContextFromCa(org.testcontainers.elasticsearch.ElasticsearchContainer)

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.elasticsearch.ElasticsearchContainer;2import org.testcontainers.utility.DockerImageName;3import javax.net.ssl.SSLContext;4import javax.net.ssl.SSLHandshakeException;5public class CreateSslContextFromCaTest {6 public static void main(String[] args) {7 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.13.1"));8 elasticsearchContainer.start();9 SSLContext sslContext = elasticsearchContainer.createSslContextFromCa();10 try {11 sslContext.createSSLEngine().getSupportedProtocols();12 } catch (SSLHandshakeException e) {13 System.out.println("SSLHandshakeException");14 }15 }16}17 at org.testcontainers.elasticsearch.ElasticsearchContainer.createSslContextFromCa(ElasticsearchContainer.java:113)18 at CreateSslContextFromCaTest.main(CreateSslContextFromCaTest.java:15)19import org.testcontainers.elasticsearch.ElasticsearchContainer;20public class Test {21 public static void main(String[] args) {22 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer();23 elasticsearchContainer.start();24 elasticsearchContainer.createSslContextFromCa();25 }26}

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.elasticsearch.ElasticsearchContainer;2import org.testcontainers.utility.DockerImageName;3import org.elasticsearch.client.RestHighLevelClient;4public class TestContainersTest {5 public static void main(String[] args) {6 DockerImageName dockerImageName = DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.9.2");7 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(dockerImageName);8 elasticsearchContainer.start();9 RestHighLevelClient restHighLevelClient = elasticsearchContainer.getClient();10 System.out.println(restHighLevelClient);11 }12}13import org.testcontainers.elasticsearch.ElasticsearchContainer;14import org.testcontainers.utility.DockerImageName;15import org.elasticsearch.client.RestHighLevelClient;16public class TestContainersTest {17 public static void main(String[] args) {18 DockerImageName dockerImageName = DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.9.2");19 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(dockerImageName);20 elasticsearchContainer.start();21 RestHighLevelClient restHighLevelClient = elasticsearchContainer.getClient();22 System.out.println(restHighLevelClient);23 }24}25import org.testcontainers.elasticsearch.ElasticsearchContainer;26import org.testcontainers.utility.DockerImageName;27import org.elasticsearch.client.RestHighLevelClient;28public class TestContainersTest {29 public static void main(String[] args) {30 DockerImageName dockerImageName = DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:7.9.2");31 ElasticsearchContainer elasticsearchContainer = new ElasticsearchContainer(dockerImageName);32 elasticsearchContainer.start();33 RestHighLevelClient restHighLevelClient = elasticsearchContainer.getClient();34 System.out.println(restHighLevelClient);35 }36}37import org.testcontainers.elasticsearch.ElasticsearchContainer

Full Screen

Full Screen

createSslContextFromCa

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.elasticsearch.ElasticsearchContainer;2import javax.net.ssl.SSLContext;3import java.security.KeyStore;4import java.security.cert.X509Certificate;5public class 1 {6 public static void main(String[] args) throws Exception {7 ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:7.9.3");8 container.start();9 KeyStore trustStore = KeyStore.getInstance("JKS");10 trustStore.load(null, null);11 X509Certificate cert = container.getHttpHostAddress().getCert();12 trustStore.setCertificateEntry("ca", cert);13 SSLContext sslContext = SSLContext.getInstance("TLS");14 sslContext.init(null, null, null);15 sslContext.init(null, null, trustStore);16 System.out.println("SSLContext created from CA certificate");17 }18}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful