From c0a568e7d9f8bfa6c6ba7cd4746cb366f3a3fe9d Mon Sep 17 00:00:00 2001 From: Antti Auhtola Date: Mon, 21 Aug 2023 22:14:33 +0300 Subject: [PATCH] test: Add test to verify key created with openssl 3.1 is working. Verify key types in both tests --- .../Fixtures/Containers/Unix/DockerMTls.cs | 11 +++++++ .../Containers/Unix/OpenSsl3_1Fixture.cs | 13 ++++++++ .../Unix/ProtectDockerDaemonSocket.cs | 4 +-- .../Unix/ProtectDockerDaemonSocketTest.cs | 33 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/Testcontainers.Tests/Fixtures/Containers/Unix/OpenSsl3_1Fixture.cs diff --git a/tests/Testcontainers.Tests/Fixtures/Containers/Unix/DockerMTls.cs b/tests/Testcontainers.Tests/Fixtures/Containers/Unix/DockerMTls.cs index 84265df0f..46ec9d527 100644 --- a/tests/Testcontainers.Tests/Fixtures/Containers/Unix/DockerMTls.cs +++ b/tests/Testcontainers.Tests/Fixtures/Containers/Unix/DockerMTls.cs @@ -1,7 +1,9 @@ namespace DotNet.Testcontainers.Tests.Fixtures { using System.Collections.Generic; + using System.IO; using DotNet.Testcontainers.Builders; + using Org.BouncyCastle.OpenSsl; public abstract class DockerMTls : ProtectDockerDaemonSocket { @@ -10,6 +12,15 @@ public DockerMTls(string dockerImageVersion) { } + public object ClientCertificateKey() + { + var path = Path.Combine(_hostCertsDirectoryPath, "client", "key.pem"); + using (var keyFileStream = new StreamReader(path)) + { + return new PemReader(keyFileStream).ReadObject(); + } + } + public override IList CustomProperties { get diff --git a/tests/Testcontainers.Tests/Fixtures/Containers/Unix/OpenSsl3_1Fixture.cs b/tests/Testcontainers.Tests/Fixtures/Containers/Unix/OpenSsl3_1Fixture.cs new file mode 100644 index 000000000..4109d61aa --- /dev/null +++ b/tests/Testcontainers.Tests/Fixtures/Containers/Unix/OpenSsl3_1Fixture.cs @@ -0,0 +1,13 @@ +namespace DotNet.Testcontainers.Tests.Fixtures +{ + using JetBrains.Annotations; + + [UsedImplicitly] + public sealed class OpenSsl3_1Fixture : DockerMTls + { + public const string DockerVersion = "24.0.5"; + public OpenSsl3_1Fixture() : base(DockerVersion) + { + } + } +} diff --git a/tests/Testcontainers.Tests/Fixtures/Containers/Unix/ProtectDockerDaemonSocket.cs b/tests/Testcontainers.Tests/Fixtures/Containers/Unix/ProtectDockerDaemonSocket.cs index 9397e0b07..9803d603a 100644 --- a/tests/Testcontainers.Tests/Fixtures/Containers/Unix/ProtectDockerDaemonSocket.cs +++ b/tests/Testcontainers.Tests/Fixtures/Containers/Unix/ProtectDockerDaemonSocket.cs @@ -16,12 +16,12 @@ public abstract class ProtectDockerDaemonSocket : IAsyncLifetime private const ushort TlsPort = 2376; - private readonly string _hostCertsDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"), CertsDirectoryName); - private readonly string _containerCertsDirectoryPath = Path.Combine("/", CertsDirectoryName); private readonly IContainer _container; + protected readonly string _hostCertsDirectoryPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("D"), CertsDirectoryName); + protected ProtectDockerDaemonSocket(ContainerBuilder containerConfiguration, string dockerImageVersion) { _container = containerConfiguration diff --git a/tests/Testcontainers.Tests/Unit/Containers/Unix/ProtectDockerDaemonSocketTest.cs b/tests/Testcontainers.Tests/Unit/Containers/Unix/ProtectDockerDaemonSocketTest.cs index 5d666827d..003f81a47 100644 --- a/tests/Testcontainers.Tests/Unit/Containers/Unix/ProtectDockerDaemonSocketTest.cs +++ b/tests/Testcontainers.Tests/Unit/Containers/Unix/ProtectDockerDaemonSocketTest.cs @@ -8,6 +8,8 @@ namespace DotNet.Testcontainers.Tests.Unit using DotNet.Testcontainers.Configurations; using DotNet.Testcontainers.Tests.Fixtures; using Microsoft.Extensions.Logging.Abstractions; + using Org.BouncyCastle.Crypto; + using Org.BouncyCastle.Crypto.Parameters; using Xunit; public static class ProtectDockerDaemonSocketTest @@ -20,10 +22,12 @@ private static IDockerEndpointAuthenticationConfiguration GetAuthConfig(ProtectD public sealed class MTlsOpenSsl1_1_1 : IClassFixture { + private readonly OpenSsl1_1_1Fixture _fixture; private readonly IDockerEndpointAuthenticationConfiguration _authConfig; public MTlsOpenSsl1_1_1(OpenSsl1_1_1Fixture dockerMTlsFixture) { + _fixture = dockerMTlsFixture; _authConfig = GetAuthConfig(dockerMTlsFixture); } @@ -35,9 +39,38 @@ public async Task GetVersionReturnsVersion() // When var version = await client.System.GetVersionAsync() .ConfigureAwait(false); + var key = _fixture.ClientCertificateKey(); // Then Assert.Equal(OpenSsl1_1_1Fixture.DockerVersion, version.Version); + Assert.IsType(key); + } + } + + public sealed class MTlsOpenSsl3_1 : IClassFixture + { + private readonly OpenSsl3_1Fixture _fixture; + private readonly IDockerEndpointAuthenticationConfiguration _authConfig; + + public MTlsOpenSsl3_1(OpenSsl3_1Fixture dockerMTlsFixture) + { + _fixture = dockerMTlsFixture; + _authConfig = GetAuthConfig(dockerMTlsFixture); + } + + [Fact] + public async Task GetVersionReturnsVersion() + { + // Given + var client = new TestcontainersClient(Guid.Empty, _authConfig, NullLogger.Instance); + // When + var version = await client.System.GetVersionAsync() + .ConfigureAwait(false); + var key = _fixture.ClientCertificateKey(); + + // Then + Assert.Equal(OpenSsl3_1Fixture.DockerVersion, version.Version); + Assert.IsType(key); } }