diff --git a/src/Testcontainers/Configurations/CustomConfiguration.cs b/src/Testcontainers/Configurations/CustomConfiguration.cs index 8c722a918..43c877bf3 100644 --- a/src/Testcontainers/Configurations/CustomConfiguration.cs +++ b/src/Testcontainers/Configurations/CustomConfiguration.cs @@ -1,4 +1,4 @@ -namespace DotNet.Testcontainers.Configurations +namespace DotNet.Testcontainers.Configurations { using System; using System.Collections.Generic; @@ -44,6 +44,24 @@ protected JsonDocument GetDockerAuthConfig(string propertyName) } } + protected string GetDockerCertPath(string propertyName) + { + _ = this.properties.TryGetValue(propertyName, out var propertyValue); + return propertyValue; + } + + protected bool GetDockerTls(string propertyName) + { + _ = this.properties.TryGetValue(propertyName, out var propertyValue); + return "1".Equals(propertyValue, StringComparison.OrdinalIgnoreCase) || (bool.TryParse(propertyValue, out var tlsEnabled) && tlsEnabled); + } + + protected bool GetDockerTlsVerify(string propertyName) + { + _ = this.properties.TryGetValue(propertyName, out var propertyValue); + return "1".Equals(propertyValue, StringComparison.OrdinalIgnoreCase) || (bool.TryParse(propertyValue, out var tlsVerifyEnabled) && tlsVerifyEnabled); + } + protected bool GetRyukDisabled(string propertyName) { return this.properties.TryGetValue(propertyName, out var propertyValue) && bool.TryParse(propertyValue, out var ryukDisabled) && ryukDisabled; diff --git a/src/Testcontainers/Configurations/EnvironmentConfiguration.cs b/src/Testcontainers/Configurations/EnvironmentConfiguration.cs index d99b55926..8056df9cc 100644 --- a/src/Testcontainers/Configurations/EnvironmentConfiguration.cs +++ b/src/Testcontainers/Configurations/EnvironmentConfiguration.cs @@ -1,4 +1,4 @@ -namespace DotNet.Testcontainers.Configurations +namespace DotNet.Testcontainers.Configurations { using System; using System.Linq; @@ -16,6 +16,12 @@ internal sealed class EnvironmentConfiguration : CustomConfiguration, ICustomCon private const string DockerAuthConfig = "DOCKER_AUTH_CONFIG"; + private const string DockerCertPath = "DOCKER_CERT_PATH"; + + private const string DockerTls = "DOCKER_TLS"; + + private const string DockerTlsVerify = "DOCKER_TLS_VERIFY"; + private const string RyukDisabled = "TESTCONTAINERS_RYUK_DISABLED"; private const string RyukContainerImage = "TESTCONTAINERS_RYUK_CONTAINER_IMAGE"; @@ -30,7 +36,18 @@ static EnvironmentConfiguration() /// Initializes a new instance of the class. /// public EnvironmentConfiguration() - : base(new[] { DockerConfig, DockerHost, DockerAuthConfig, RyukDisabled, RyukContainerImage, HubImageNamePrefix } + : base(new[] + { + DockerAuthConfig, + DockerCertPath, + DockerConfig, + DockerHost, + DockerTls, + DockerTlsVerify, + RyukDisabled, + RyukContainerImage, + HubImageNamePrefix, + } .ToDictionary(key => key, Environment.GetEnvironmentVariable)) { } @@ -59,6 +76,24 @@ public JsonDocument GetDockerAuthConfig() return this.GetDockerAuthConfig(DockerAuthConfig); } + /// + public string GetDockerCertPath() + { + return this.GetDockerCertPath(DockerCertPath); + } + + /// + public bool GetDockerTls() + { + return this.GetDockerTls(DockerTls); + } + + /// + public bool GetDockerTlsVerify() + { + return this.GetDockerTlsVerify(DockerTlsVerify); + } + /// public bool GetRyukDisabled() { diff --git a/src/Testcontainers/Configurations/ICustomConfiguration.cs b/src/Testcontainers/Configurations/ICustomConfiguration.cs index 9256cb884..dad298881 100644 --- a/src/Testcontainers/Configurations/ICustomConfiguration.cs +++ b/src/Testcontainers/Configurations/ICustomConfiguration.cs @@ -1,4 +1,4 @@ -namespace DotNet.Testcontainers.Configurations +namespace DotNet.Testcontainers.Configurations { using System; using System.Text.Json; @@ -34,6 +34,28 @@ internal interface ICustomConfiguration [CanBeNull] JsonDocument GetDockerAuthConfig(); + /// + /// Gets the Docker location of your authentication keys and host certificate. + /// + /// The Docker location of your authentication keys and host certificate. + /// https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection. + [CanBeNull] + string GetDockerCertPath(); + + /// + /// Gets the Docker uses TLS. + /// + /// The Docker uses TLS. + /// https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection. + bool GetDockerTls(); + + /// + /// Gets the Docker uses TLS and verifies the remote. + /// + /// The Docker uses TLS and verifies the remote. + /// https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection. + bool GetDockerTlsVerify(); + /// /// Gets the Ryuk disabled custom configuration. /// diff --git a/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs b/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs index 1548425f9..d77124f0f 100644 --- a/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs +++ b/src/Testcontainers/Configurations/PropertiesFileConfiguration.cs @@ -1,4 +1,4 @@ -namespace DotNet.Testcontainers.Configurations +namespace DotNet.Testcontainers.Configurations { using System; using System.IO; @@ -77,6 +77,27 @@ public JsonDocument GetDockerAuthConfig() return this.GetDockerAuthConfig(propertyName); } + /// + public string GetDockerCertPath() + { + const string propertyName = "docker.cert.path"; + return this.GetDockerCertPath(propertyName); + } + + /// + public bool GetDockerTls() + { + const string propertyName = "docker.tls"; + return this.GetDockerTls(propertyName); + } + + /// + public bool GetDockerTlsVerify() + { + const string propertyName = "docker.tls.verify"; + return this.GetDockerTlsVerify(propertyName); + } + /// public bool GetRyukDisabled() { diff --git a/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs b/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs index e4da39622..29f2e5223 100644 --- a/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs +++ b/tests/Testcontainers.Tests/Unit/Configurations/CustomConfigurationTest.cs @@ -1,4 +1,4 @@ -namespace DotNet.Testcontainers.Tests.Unit +namespace DotNet.Testcontainers.Tests.Unit { using System; using System.Collections.Generic; @@ -18,6 +18,9 @@ static EnvironmentConfigurationTest() EnvironmentVariables.Add("DOCKER_CONFIG"); EnvironmentVariables.Add("DOCKER_HOST"); EnvironmentVariables.Add("DOCKER_AUTH_CONFIG"); + EnvironmentVariables.Add("DOCKER_CERT_PATH"); + EnvironmentVariables.Add("DOCKER_TLS"); + EnvironmentVariables.Add("DOCKER_TLS_VERIFY"); EnvironmentVariables.Add("TESTCONTAINERS_RYUK_DISABLED"); EnvironmentVariables.Add("TESTCONTAINERS_RYUK_CONTAINER_IMAGE"); EnvironmentVariables.Add("TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX"); @@ -60,6 +63,49 @@ public void GetDockerAuthConfigCustomConfiguration(string propertyName, string p Assert.Equal(expected, customConfiguration.GetDockerAuthConfig()?.RootElement.ToString()); } + [Theory] + [InlineData("", "", false)] + [InlineData("DOCKER_TLS", "", false)] + [InlineData("DOCKER_TLS", "0", false)] + [InlineData("DOCKER_TLS", "false", false)] + [InlineData("DOCKER_TLS", "FALSE", false)] + [InlineData("DOCKER_TLS", "1", true)] + [InlineData("DOCKER_TLS", "TRUE", true)] + [InlineData("DOCKER_TLS", "true", true)] + public void GetDockerTlsCustomConfiguration(string propertyName, string propertyValue, bool expected) + { + SetEnvironmentVariable(propertyName, propertyValue); + ICustomConfiguration customConfiguration = new EnvironmentConfiguration(); + Assert.Equal(expected, customConfiguration.GetDockerTls()); + } + + [Theory] + [InlineData("", "", false)] + [InlineData("DOCKER_TLS_VERIFY", "", false)] + [InlineData("DOCKER_TLS_VERIFY", "0", false)] + [InlineData("DOCKER_TLS_VERIFY", "false", false)] + [InlineData("DOCKER_TLS_VERIFY", "FALSE", false)] + [InlineData("DOCKER_TLS_VERIFY", "1", true)] + [InlineData("DOCKER_TLS_VERIFY", "TRUE", true)] + [InlineData("DOCKER_TLS_VERIFY", "true", true)] + public void GetDockerTlsVerifyCustomConfiguration(string propertyName, string propertyValue, bool expected) + { + SetEnvironmentVariable(propertyName, propertyValue); + ICustomConfiguration customConfiguration = new EnvironmentConfiguration(); + Assert.Equal(expected, customConfiguration.GetDockerTlsVerify()); + } + + [Theory] + [InlineData("", "", null)] + [InlineData("DOCKER_CERT_PATH", "", null)] + [InlineData("DOCKER_CERT_PATH", "/home/docker/.docker/certs", "/home/docker/.docker/certs")] + public void GetDockerCertPathCustomConfiguration(string propertyName, string propertyValue, string expected) + { + SetEnvironmentVariable(propertyName, propertyValue); + ICustomConfiguration customConfiguration = new EnvironmentConfiguration(); + Assert.Equal(expected, customConfiguration.GetDockerCertPath()); + } + [Theory] [InlineData("", "", false)] [InlineData("TESTCONTAINERS_RYUK_DISABLED", "", false)] @@ -147,6 +193,46 @@ public void GetDockerAuthConfigCustomConfiguration(string configuration, string Assert.Equal(expected, customConfiguration.GetDockerAuthConfig()?.RootElement.ToString()); } + [Theory] + [InlineData("", null)] + [InlineData("docker.cert.path=", null)] + [InlineData("docker.cert.path=/home/docker/.docker/certs", "/home/docker/.docker/certs")] + public void GetDockerCertPathCustomConfiguration(string configuration, string expected) + { + ICustomConfiguration customConfiguration = new PropertiesFileConfiguration(new[] { configuration }); + Assert.Equal(expected, customConfiguration.GetDockerCertPath()); + } + + [Theory] + [InlineData("", false)] + [InlineData("docker.tls=", false)] + [InlineData("docker.tls=0", false)] + [InlineData("docker.tls=false", false)] + [InlineData("docker.tls=FALSE", false)] + [InlineData("docker.tls=1", true)] + [InlineData("docker.tls=TRUE", true)] + [InlineData("docker.tls=true", true)] + public void GetDockerTlsCustomConfiguration(string configuration, bool expected) + { + ICustomConfiguration customConfiguration = new PropertiesFileConfiguration(new[] { configuration }); + Assert.Equal(expected, customConfiguration.GetDockerTls()); + } + + [Theory] + [InlineData("", false)] + [InlineData("docker.tls.verify=", false)] + [InlineData("docker.tls.verify=0", false)] + [InlineData("docker.tls.verify=false", false)] + [InlineData("docker.tls.verify=FALSE", false)] + [InlineData("docker.tls.verify=1", true)] + [InlineData("docker.tls.verify=TRUE", true)] + [InlineData("docker.tls.verify=true", true)] + public void GetDockerTlsVerifyCustomConfiguration(string configuration, bool expected) + { + ICustomConfiguration customConfiguration = new PropertiesFileConfiguration(new[] { configuration }); + Assert.Equal(expected, customConfiguration.GetDockerTlsVerify()); + } + [Theory] [InlineData("", false)] [InlineData("ryuk.disabled=", false)]