private RestClient getClient(ElasticsearchContainer container) {
        if (client == null) {
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD)
            );

            String protocol = container.caCertAsBytes().isPresent() ? "https://" : "http://";

            client =
                RestClient
                    .builder(HttpHost.create(protocol + container.getHttpHostAddress()))
                    .setHttpClientConfigCallback(httpClientBuilder -> {
                        if (container.caCertAsBytes().isPresent()) {
                            httpClientBuilder.setSSLContext(container.createSslContextFromCa());
                        }
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    })
                    .build();
        }

        return client;
    }
From 6.8, we can optionally activate security with a default password.
modules/elasticsearch/src/test/java/org/testcontainers/elasticsearch/ElasticsearchContainerTest.java