Skip to content

[Enhancement]: Allow setting up ElasticsearchContainer with HTTP #1461

@Jsehahn

Description

@Jsehahn

Problem

The Elasticsearch container is setup with HTTPS by default, which forces you to do clunky stuff like this:

/// <summary>
/// Get the Elasticsearch SSL-certificate file from the container, so it can be passed along
/// to dependent container(s) required to connect to Elasticsearch
/// </summary>
private static async Task<byte[]> ExportCertificateAsync(ElasticsearchContainer container)
{
    return await container.ReadFileAsync("/usr/share/elasticsearch/config/certs/http_ca.crt")
        .ConfigureAwait(false);
}

We can work around this behaviour by disabling xpack security, like so:

  private ElasticsearchContainer BuildElasticSearchContainer()
  {
      return new ElasticsearchBuilder()
          .WithImage(ElasticsearchImageVersion)
          .WithNetwork(_network)
          .WithNetworkAliases(ElasticsearchContainerName)
          .WithEnvironment("xpack.security.enabled", "false")
          .WithEnvironment("xpack.security.http.ssl.enabled", "false")
          .Build();
    }

But because ElasticsearchContainer.GetConnectionString() always created an HTTPS endpoint, and this is a sealed class, we're left with needing work-arounds.

  public string GetConnectionString()
  {
      var endpoint = new UriBuilder(Uri.UriSchemeHttps, Hostname, GetMappedPublicPort(ElasticsearchBuilder.ElasticsearchHttpsPort));
      endpoint.UserName = _configuration.Username;
      endpoint.Password = _configuration.Password;
      return endpoint.ToString();
  }

This is one of the workarounds:

public static class ElasticsearchContainerExtensions
{
    public static string GetHttpConnectionString(this ElasticsearchContainer container) =>
        container.GetConnectionString().Replace("https://", "http://");
}

...

var clientSettings = new ElasticsearchClientSettings(new Uri(_elasticsearchContainer.GetHttpConnectionString()));

Solution

My preferred solution would be to Implement an extension method on the ElasticsearchBuilder, called WithHttp or something. This disables the relevant xpacks and sets the correct connection string.

Benefit

Easily set up an HTTP connection without tricky workarounds that I don't fully trust. A secure HTTPS connection is hardly required for a test container, and this saves you from having to pass certificates around (which is also hacky).

Alternatives

As mentioned, there is a workaround.

Would you like to help contributing this enhancement?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestmoduleAn official Testcontainers module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions