Skip to content

Commit a253f4b

Browse files
authored
feat(Elasticsearch): Return HTTP connection string if security is disabled (#1494)
1 parent 662eade commit a253f4b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/Testcontainers.Elasticsearch/ElasticsearchContainer.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,29 @@ public ElasticsearchContainer(ElasticsearchConfiguration configuration)
2020
/// Gets the Elasticsearch connection string.
2121
/// </summary>
2222
/// <remarks>
23-
/// The Elasticsearch module does not export the SSL certificate from the container by default.
24-
/// If you are trying to connect to the Elasticsearch service, you need to override the certificate validation callback to establish the connection.
23+
/// The Elasticsearch module does not export the SSL certificate from the container
24+
/// by default. If you are trying to connect to the Elasticsearch service, you need
25+
/// to override the certificate validation callback to establish the connection.
2526
/// We will export the certificate and support trusted SSL connections in the future.
2627
/// </remarks>
2728
/// <returns>The Elasticsearch connection string.</returns>
2829
public string GetConnectionString()
2930
{
30-
var endpoint = new UriBuilder(Uri.UriSchemeHttps, Hostname, GetMappedPublicPort(ElasticsearchBuilder.ElasticsearchHttpsPort));
31+
var hasSecurityEnabled = _configuration.Environments
32+
.TryGetValue("xpack.security.enabled", out var securityEnabled);
33+
34+
var hasHttpSslEnabled = _configuration.Environments
35+
.TryGetValue("xpack.security.http.ssl.enabled", out var httpSslEnabled);
36+
37+
var httpsDisabled =
38+
hasSecurityEnabled &&
39+
hasHttpSslEnabled &&
40+
"false".Equals(securityEnabled, StringComparison.OrdinalIgnoreCase) &&
41+
"false".Equals(httpSslEnabled, StringComparison.OrdinalIgnoreCase);
42+
43+
var scheme = httpsDisabled ? Uri.UriSchemeHttp : Uri.UriSchemeHttps;
44+
45+
var endpoint = new UriBuilder(scheme, Hostname, GetMappedPublicPort(ElasticsearchBuilder.ElasticsearchHttpsPort));
3146
endpoint.UserName = _configuration.Username;
3247
endpoint.Password = _configuration.Password;
3348
return endpoint.ToString();

tests/Testcontainers.WebDriver.Tests/WebDriverContainerTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public async Task ExportVideoWritesFile()
7979
var videoFilePath = Path.Combine(TestSession.TempDirectoryPath, Path.GetRandomFileName());
8080

8181
// When
82+
HeadingElementReturnsHelloWorld();
83+
8284
await _webDriverContainer.StopAsync(TestContext.Current.CancellationToken)
8385
.ConfigureAwait(true);
8486

0 commit comments

Comments
 (0)