Skip to content

Commit af1af83

Browse files
authored
chore: Add test certificates to common project (#1545)
1 parent d40c646 commit af1af83

File tree

19 files changed

+273
-9
lines changed

19 files changed

+273
-9
lines changed

.gitattributes

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
# Shell scripts
99
*.sh text eol=lf
1010

11-
# Git lfs
12-
*.crt filter=lfs diff=lfs merge=lfs -text
11+
# Treat all SSL-related files as binary
12+
*.crt binary
13+
*.key binary
14+
*.pem binary
15+
*.pfx binary
16+
17+
# Git LFS
1318
*.ico filter=lfs diff=lfs merge=lfs -text
19+
*.pfx filter=lfs diff=lfs merge=lfs -text
1420
*.png filter=lfs diff=lfs merge=lfs -text
1521
*.snk filter=lfs diff=lfs merge=lfs -text
1622

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ ClientBin/
242242
*.dbmdl
243243
*.dbproj.schemaview
244244
*.jfm
245-
*.pfx
246245
*.publishsettings
247246
orleans.codegen.cs
248247

@@ -402,4 +401,4 @@ FodyWeavers.xsd
402401

403402
# JetBrains Rider
404403
.idea/
405-
*.sln.iml
404+
*.sln.iml

docs/api/create_docker_container.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ To configure an ASP.NET Core application, either one or both mechanisms can be u
2727
```csharp
2828
_ = new ContainerBuilder()
2929
.WithEnvironment("ASPNETCORE_URLS", "https://+")
30-
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", "/app/certificate.crt")
30+
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", "/app/certificate.pfx")
3131
.WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", "password")
32-
.WithResourceMapping("certificate.crt", "/app/");
32+
.WithResourceMapping("certificate.pfx", "/app/");
3333
```
3434

3535
`WithBindMount(string, string)` is another option to provide access to directories or files. It mounts a host directory or file into the container. Note, this does not follow our best practices. Host paths differ between environments and may not be available on every system or Docker setup, e.g. CI.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
* text=auto
2-
*.crt filter=lfs diff=lfs merge=lfs -text
2+
*.pfx filter=lfs diff=lfs merge=lfs -text

examples/WeatherForecast/src/WeatherForecast/WeatherForecast.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<InternalsVisibleTo Include="WeatherForecast.InProcess.Test"/>
1818
</ItemGroup>
1919
<ItemGroup>
20-
<None Update="certificate.crt" Visible="false">
20+
<None Update="certificate.pfx" Visible="false">
2121
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2222
</None>
2323
</ItemGroup>

examples/WeatherForecast/src/WeatherForecast/certificate.crt renamed to examples/WeatherForecast/src/WeatherForecast/certificate.pfx

File renamed without changes.

examples/WeatherForecast/tests/WeatherForecast.Tests/WeatherForecastImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public sealed class WeatherForecastImage : IImage, IAsyncLifetime
55
{
66
public const ushort HttpsPort = 443;
77

8-
public const string CertificateFilePath = "certificate.crt";
8+
public const string CertificateFilePath = "certificate.pfx";
99

1010
public const string CertificatePassword = "password";
1111

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace DotNet.Testcontainers.Commons;
2+
3+
[PublicAPI]
4+
public sealed class Certificates
5+
{
6+
public const string Password = "password";
7+
8+
private readonly string _baseDirectory = AppContext.BaseDirectory;
9+
10+
private Certificates()
11+
{
12+
if (!Directory.Exists(CaDirectory))
13+
{
14+
throw new DirectoryNotFoundException($"CA directory not found: '{CaDirectory}'.");
15+
}
16+
17+
if (!Directory.Exists(ClientDirectory))
18+
{
19+
throw new DirectoryNotFoundException($"Client directory not found: '{ClientDirectory}'.");
20+
}
21+
22+
if (!Directory.Exists(ServerDirectory))
23+
{
24+
throw new DirectoryNotFoundException($"Server directory not found: '{ServerDirectory}'.");
25+
}
26+
}
27+
28+
public static Certificates Instance { get; } = new Certificates();
29+
30+
public string CaDirectory => Path.Combine(_baseDirectory, "ssl", "ca");
31+
32+
public string ClientDirectory => Path.Combine(_baseDirectory, "ssl", "client");
33+
34+
public string ServerDirectory => Path.Combine(_baseDirectory, "ssl", "server");
35+
36+
public string GetFilePath(string directoryName, string fileName)
37+
{
38+
string directoryPath;
39+
40+
switch (directoryName.ToLowerInvariant())
41+
{
42+
case "ca":
43+
directoryPath = CaDirectory;
44+
break;
45+
case "client":
46+
directoryPath = ClientDirectory;
47+
break;
48+
case "server":
49+
directoryPath = ServerDirectory;
50+
break;
51+
default:
52+
throw new ArgumentException($"Unknown directory: '{directoryName}'.");
53+
}
54+
55+
var filePath = Path.Combine(directoryPath, fileName);
56+
return File.Exists(filePath) ? filePath : throw new FileNotFoundException($"File not found: '{filePath}'.");
57+
}
58+
}

tests/Testcontainers.Commons/Testcontainers.Commons.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313
<ItemGroup>
1414
<ProjectReference Include="../../src/Testcontainers/Testcontainers.csproj"/>
1515
</ItemGroup>
16+
<ItemGroup>
17+
<None Include="ssl/**/*">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
1621
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDBTCCAe2gAwIBAgIUK6Qz/lUVJw2bY/hrEkOnppUtjsEwDQYJKoZIhvcNAQEL
3+
BQAwEjEQMA4GA1UEAwwHVGVzdCBDQTAeFw0yNTEwMDQxMDU3MzBaFw0zNTEwMDIx
4+
MDU3MzBaMBIxEDAOBgNVBAMMB1Rlc3QgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IB
5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