Skip to content

Commit

Permalink
feat(#370): Used env variables from new custom configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
vlaskal committed Aug 20, 2022
1 parent 3893d13 commit 5ea3404
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 56 deletions.
25 changes: 13 additions & 12 deletions src/Testcontainers/Builders/MTlsEndpointAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,34 @@ internal sealed class MTlsEndpointAuthenticationProvider : DockerEndpointAuthent
private static readonly Regex PemData = new Regex("-----BEGIN (.*)-----(.*)-----END (.*)-----", RegexOptions.Multiline);

private readonly Uri dockerEngine;
private readonly bool dockerTlsVerifyEnabled;
private readonly string dockerCaCertFile;
private readonly bool? dockerTlsVerifyEnabled;
private readonly string dockerClientCertFile;
private readonly string dockerClientKeyFile;
private readonly Lazy<MSX509.X509Certificate2> caCertificate;
private readonly Lazy<MSX509.X509Certificate2> clientCertificate;

public MTlsEndpointAuthenticationProvider()
{
var dockerHostValue = Environment.GetEnvironmentVariable("DOCKER_HOST");
var dockerTlsVerifyValue = Environment.GetEnvironmentVariable("DOCKER_TLS_VERIFY");
var dockerCertPathValue = Environment.GetEnvironmentVariable("DOCKER_CERT_PATH");
var dockerCertPath = dockerCertPathValue ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), DefaultCertPath);

this.dockerEngine = Uri.TryCreate(dockerHostValue, UriKind.RelativeOrAbsolute, out var dockerHost) ? dockerHost : DefaultTlsDockerEndpoint;
this.dockerTlsVerifyEnabled = int.TryParse(dockerTlsVerifyValue, out var dockerTlsVerify) && dockerTlsVerify == 1;
this.dockerCaCertFile = Path.Combine(dockerCertPath, DefaultCaCertFileName);
ICustomConfiguration propertiesFileConfiguration = new PropertiesFileConfiguration();
ICustomConfiguration environmentConfiguration = new EnvironmentConfiguration();

var dockerCertPath = propertiesFileConfiguration.GetDockerCertPath()
?? environmentConfiguration.GetDockerCertPath()
?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), DefaultCertPath);
var dockerCaCertFile = Path.Combine(dockerCertPath, DefaultCaCertFileName);

this.dockerEngine = propertiesFileConfiguration.GetDockerHost() ?? environmentConfiguration.GetDockerHost() ?? DefaultTlsDockerEndpoint;
this.dockerTlsVerifyEnabled = propertiesFileConfiguration.GetDockerTlsVerify() ?? environmentConfiguration.GetDockerTlsVerify();
this.dockerClientCertFile = Path.Combine(dockerCertPath, DefaultClientCertFileName);
this.dockerClientKeyFile = Path.Combine(dockerCertPath, DefaultClientKeyFileName);
this.caCertificate = new Lazy<MSX509.X509Certificate2>(() => new MSX509.X509Certificate2(this.dockerCaCertFile));
this.caCertificate = new Lazy<MSX509.X509Certificate2>(() => new MSX509.X509Certificate2(dockerCaCertFile));
this.clientCertificate = new Lazy<MSX509.X509Certificate2>(this.GetClientCertificate);
}

/// <inheritdoc />
public override bool IsApplicable()
{
return this.dockerTlsVerifyEnabled && File.Exists(this.dockerClientCertFile) && File.Exists(this.dockerClientKeyFile);
return this.dockerTlsVerifyEnabled.HasValue && this.dockerTlsVerifyEnabled.Value && File.Exists(this.dockerClientCertFile) && File.Exists(this.dockerClientKeyFile);
}

/// <inheritdoc />
Expand Down
23 changes: 12 additions & 11 deletions src/Testcontainers/Builders/TlsEndpointAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@ internal sealed class TlsEndpointAuthenticationProvider : DockerEndpointAuthenti
private static readonly Uri DefaultTlsDockerEndpoint = new Uri("tcp://localhost:2376");

private readonly Uri dockerEngine;
private readonly bool dockerTlsEnabled;
private readonly string dockerCaCertFile;
private readonly bool? dockerTlsEnabled;
private readonly Lazy<X509Certificate2> caCertificate;

public TlsEndpointAuthenticationProvider()
{
var dockerHostValue = Environment.GetEnvironmentVariable("DOCKER_HOST");
var dockerTlsValue = Environment.GetEnvironmentVariable("DOCKER_TLS");
var dockerCertPathValue = Environment.GetEnvironmentVariable("DOCKER_CERT_PATH");
var dockerCertPath = dockerCertPathValue ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), DefaultCertPath);
ICustomConfiguration propertiesFileConfiguration = new PropertiesFileConfiguration();
ICustomConfiguration environmentConfiguration = new EnvironmentConfiguration();

this.dockerEngine = Uri.TryCreate(dockerHostValue, UriKind.RelativeOrAbsolute, out var dockerHost) ? dockerHost : DefaultTlsDockerEndpoint;
this.dockerTlsEnabled = int.TryParse(dockerTlsValue, out var dockerTls) && dockerTls == 1;
this.dockerCaCertFile = Path.Combine(dockerCertPath, DefaultCaCertFileName);
this.caCertificate = new Lazy<X509Certificate2>(() => new X509Certificate2(this.dockerCaCertFile));
var dockerCertPath = propertiesFileConfiguration.GetDockerCertPath()
?? environmentConfiguration.GetDockerCertPath()
?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), DefaultCertPath);
var dockerCaCertFile = Path.Combine(dockerCertPath, DefaultCaCertFileName);

this.dockerEngine = propertiesFileConfiguration.GetDockerHost() ?? environmentConfiguration.GetDockerHost() ?? DefaultTlsDockerEndpoint;
this.dockerTlsEnabled = propertiesFileConfiguration.GetDockerTls() ?? environmentConfiguration.GetDockerTls();
this.caCertificate = new Lazy<X509Certificate2>(() => new X509Certificate2(dockerCaCertFile));
}

/// <inheritdoc />
public override bool IsApplicable()
{
return this.dockerTlsEnabled;
return this.dockerTlsEnabled.HasValue && this.dockerTlsEnabled.Value;
}

/// <inheritdoc />
Expand Down
63 changes: 55 additions & 8 deletions src/Testcontainers/Configurations/CustomConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace DotNet.Testcontainers.Configurations
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;
using DotNet.Testcontainers.Images;

Expand All @@ -14,6 +15,37 @@ protected CustomConfiguration(IReadOnlyDictionary<string, string> properties)
this.properties = properties;
}

protected JsonDocument GetDockerAuthConfig(string propertyName)
{
_ = this.properties.TryGetValue(propertyName, out var propertyValue);

if (string.IsNullOrEmpty(propertyValue))
{
return null;
}

try
{
return JsonDocument.Parse(propertyValue);
}
catch (Exception)
{
return null;
}
}

protected string GetDockerCertPath(string propertyName)
{
_ = this.properties.TryGetValue(propertyName, out var propertyValue);

if (string.IsNullOrWhiteSpace(propertyValue))
{
return null;
}

return propertyValue;
}

protected string GetDockerConfig(string propertyName)
{
_ = this.properties.TryGetValue(propertyName, out var propertyValue);
Expand All @@ -25,23 +57,38 @@ protected Uri GetDockerHost(string propertyName)
return this.properties.TryGetValue(propertyName, out var propertyValue) && Uri.TryCreate(propertyValue, UriKind.RelativeOrAbsolute, out var dockerHost) ? dockerHost : null;
}

protected JsonDocument GetDockerAuthConfig(string propertyName)
protected bool? GetDockerTls(string propertyName)
{
_ = this.properties.TryGetValue(propertyName, out var propertyValue);

if (string.IsNullOrEmpty(propertyValue))
if (!this.properties.TryGetValue(propertyName, out var propertyValue) || string.IsNullOrWhiteSpace(propertyValue))
{
return null;
}

try
propertyValue = propertyValue.Trim().ToLower(CultureInfo.InvariantCulture);

if (bool.TryParse(propertyValue, out var dockerTls))
{
return JsonDocument.Parse(propertyValue);
return dockerTls;
}
catch (Exception)

return propertyValue == "1";
}

protected bool? GetDockerTlsVerify(string propertyName)
{
if (!this.properties.TryGetValue(propertyName, out var propertyValue) || string.IsNullOrWhiteSpace(propertyValue))
{
return null;
}

propertyValue = propertyValue.Trim().ToLower(CultureInfo.InvariantCulture);

if (bool.TryParse(propertyValue, out var dockerTlsVerify))
{
return dockerTlsVerify;
}

return propertyValue == "1";
}

protected bool GetRyukDisabled(string propertyName)
Expand Down
45 changes: 40 additions & 5 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 @@ -10,11 +10,17 @@
/// </summary>
internal sealed class EnvironmentConfiguration : CustomConfiguration, ICustomConfiguration
{
private const string DockerAuthConfig = "DOCKER_AUTH_CONFIG";

private const string DockerCertPath = "DOCKER_CERT_PATH";

private const string DockerConfig = "DOCKER_CONFIG";

private const string DockerHost = "DOCKER_HOST";

private const string DockerAuthConfig = "DOCKER_AUTH_CONFIG";
private const string DockerTls = "DOCKER_TLS";

private const string DockerTlsVerify = "DOCKER_TLS_VERIFY";

private const string RyukDisabled = "TESTCONTAINERS_RYUK_DISABLED";

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 All @@ -41,6 +58,18 @@ public EnvironmentConfiguration()
public static ICustomConfiguration Instance { get; }
= new EnvironmentConfiguration();

/// <inheritdoc />
public JsonDocument GetDockerAuthConfig()
{
return this.GetDockerAuthConfig(DockerAuthConfig);
}

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

/// <inheritdoc />
public string GetDockerConfig()
{
Expand All @@ -54,9 +83,15 @@ public Uri GetDockerHost()
}

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

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

/// <inheritdoc />
Expand Down
34 changes: 28 additions & 6 deletions 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 All @@ -10,6 +10,22 @@
/// </summary>
internal interface ICustomConfiguration
{
/// <summary>
/// Gets the Docker registry authentication custom configuration.
/// </summary>
/// <returns>The Docker authentication custom configuration.</returns>
/// <remarks>https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#access-an-image-from-a-private-container-registry.</remarks>
[CanBeNull]
JsonDocument GetDockerAuthConfig();

/// <summary>
/// Gets the Docker location of your authentication keys and host certificate.
/// </summary>
/// <returns>The Docker location of your authentication keys and host certificate.</returns>
/// <remarks>https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection.</remarks>
[CanBeNull]
string GetDockerCertPath();

/// <summary>
/// Gets the Docker config custom configuration.
/// </summary>
Expand All @@ -27,12 +43,18 @@ internal interface ICustomConfiguration
Uri GetDockerHost();

/// <summary>
/// Gets the Docker registry authentication custom configuration.
/// Gets the Docker uses TLS.
/// </summary>
/// <returns>The Docker authentication custom configuration.</returns>
/// <remarks>https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#access-an-image-from-a-private-container-registry.</remarks>
[CanBeNull]
JsonDocument GetDockerAuthConfig();
/// <returns>The Docker uses TLS.</returns>
/// <remarks>https://www.testcontainers.org/features/configuration/#customizing-docker-host-detection.</remarks>
bool? GetDockerTls();

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

/// <summary>
/// Gets the Ryuk disabled custom configuration.
Expand Down
29 changes: 25 additions & 4 deletions 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 @@ -56,6 +56,20 @@ public PropertiesFileConfiguration(params string[] lines)
public static ICustomConfiguration Instance { get; }
= new PropertiesFileConfiguration();

/// <inheritdoc />
public JsonDocument GetDockerAuthConfig()
{
const string propertyName = "docker.auth.config";
return this.GetDockerAuthConfig(propertyName);
}

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

/// <inheritdoc />
public string GetDockerConfig()
{
Expand All @@ -71,10 +85,17 @@ public Uri GetDockerHost()
}

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

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

/// <inheritdoc />
Expand Down
Loading

0 comments on commit 5ea3404

Please sign in to comment.