Skip to content

Commit

Permalink
feat(#370): Add TLS custom configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaskal authored and HofmeisterAn committed Sep 5, 2022
1 parent fcbd4df commit 2b23a6b
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 7 deletions.
20 changes: 19 additions & 1 deletion src/Testcontainers/Configurations/CustomConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Configurations
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -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.Ordinal) || (bool.TryParse(propertyValue, out var tlsEnabled) && tlsEnabled);
}

protected bool GetDockerTlsVerify(string propertyName)
{
_ = this.properties.TryGetValue(propertyName, out var propertyValue);
return "1".Equals(propertyValue, StringComparison.Ordinal) || (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;
Expand Down
39 changes: 37 additions & 2 deletions src/Testcontainers/Configurations/EnvironmentConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Configurations
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Linq;
Expand All @@ -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";
Expand All @@ -30,7 +36,18 @@ static EnvironmentConfiguration()
/// Initializes a new instance of the <see cref="EnvironmentConfiguration" /> class.
/// </summary>
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))
{
}
Expand Down Expand Up @@ -59,6 +76,24 @@ public JsonDocument GetDockerAuthConfig()
return this.GetDockerAuthConfig(DockerAuthConfig);
}

/// <inheritdoc />
public string GetDockerCertPath()
{
return this.GetDockerCertPath(DockerCertPath);
}

/// <inheritdoc />
public bool GetDockerTls()
{
return this.GetDockerTls(DockerTls);
}

/// <inheritdoc />
public bool GetDockerTlsVerify()
{
return this.GetDockerTlsVerify(DockerTlsVerify);
}

/// <inheritdoc />
public bool GetRyukDisabled()
{
Expand Down
24 changes: 23 additions & 1 deletion src/Testcontainers/Configurations/ICustomConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Configurations
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Text.Json;
Expand Down Expand Up @@ -34,6 +34,28 @@ internal interface ICustomConfiguration
[CanBeNull]
JsonDocument GetDockerAuthConfig();

/// <summary>
/// Gets the Docker certificates path custom configuration.
/// </summary>
/// <returns>The Docker certificates path custom configuration.</returns>
/// <remarks>https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection.</remarks>
[CanBeNull]
string GetDockerCertPath();

/// <summary>
/// Gets the Docker TLS custom configuration.
/// </summary>
/// <returns>The Docker TLS custom configuration.</returns>
/// <remarks>https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection.</remarks>
bool GetDockerTls();

/// <summary>
/// Gets the Docker TLS verify custom configuration.
/// </summary>
/// <returns>The Docker TLS verify custom configuration.</returns>
/// <remarks>https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection.</remarks>
bool GetDockerTlsVerify();

/// <summary>
/// Gets the Ryuk disabled custom configuration.
/// </summary>
Expand Down
23 changes: 22 additions & 1 deletion src/Testcontainers/Configurations/PropertiesFileConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Configurations
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.IO;
Expand Down Expand Up @@ -77,6 +77,27 @@ public JsonDocument GetDockerAuthConfig()
return this.GetDockerAuthConfig(propertyName);
}

/// <inheritdoc />
public string GetDockerCertPath()
{
const string propertyName = "docker.cert.path";
return this.GetDockerCertPath(propertyName);
}

/// <inheritdoc />
public bool GetDockerTls()
{
const string propertyName = "docker.tls";
return this.GetDockerTls(propertyName);
}

/// <inheritdoc />
public bool GetDockerTlsVerify()
{
const string propertyName = "docker.tls.verify";
return this.GetDockerTlsVerify(propertyName);
}

/// <inheritdoc />
public bool GetRyukDisabled()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Collections.Generic;
using System.IO;
using DotNet.Testcontainers.Builders;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Tests.Unit
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Collections.Generic;
Expand All @@ -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");
Expand Down Expand Up @@ -60,6 +63,49 @@ public void GetDockerAuthConfigCustomConfiguration(string propertyName, string p
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 propertyName, string propertyValue, string expected)
{
SetEnvironmentVariable(propertyName, propertyValue);
ICustomConfiguration customConfiguration = new EnvironmentConfiguration();
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 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("", "", false)]
[InlineData("TESTCONTAINERS_RYUK_DISABLED", "", false)]
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 2b23a6b

Please sign in to comment.