Skip to content

Commit f2ba5b9

Browse files
committed
chore(): Enable CA-only mTLS configuration and expand tests
1 parent ad2871d commit f2ba5b9

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpMtlsOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ internal sealed class OtlpMtlsOptions
3030

3131
/// <summary>
3232
/// Gets a value indicating whether mTLS is enabled.
33-
/// mTLS is considered enabled if at least the client certificate path is provided.
33+
/// mTLS is considered enabled if at least the client certificate path or CA certificate path is provided.
3434
/// </summary>
35-
public bool IsEnabled => !string.IsNullOrWhiteSpace(this.ClientCertificatePath);
35+
public bool IsEnabled =>
36+
!string.IsNullOrWhiteSpace(this.ClientCertificatePath)
37+
|| !string.IsNullOrWhiteSpace(this.CaCertificatePath);
3638
}
3739

3840
#endif

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpExporterOptionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ public void OtlpExporterOptions_MtlsEnvironmentVariables()
276276

277277
Assert.NotNull(options.MtlsOptions);
278278
Assert.Equal("/path/to/ca.crt", options.MtlsOptions.CaCertificatePath);
279+
Assert.True(options.MtlsOptions.IsEnabled);
279280
}
280281
finally
281282
{

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMtlsCertificateManagerTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#if NET
55

6-
using Xunit;
7-
86
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
97

108
public class OtlpMtlsCertificateManagerTests

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMtlsHttpClientFactoryTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#if NET
55

6-
using Xunit;
7-
86
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
97

108
public class OtlpMtlsHttpClientFactoryTests
@@ -78,10 +76,7 @@ public void CreateHttpClient_ConfiguresServerCertificateValidation_WhenTrustedRo
7876
{
7977
// Create a self-signed certificate for testing as trusted root
8078
using var trustedCert = CreateSelfSignedCertificate();
81-
var trustedCertPem = Convert.ToBase64String(trustedCert.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Cert));
82-
var pemContent =
83-
$"-----BEGIN CERTIFICATE-----\n{trustedCertPem}\n-----END CERTIFICATE-----";
84-
File.WriteAllText(tempTrustStoreFile, pemContent);
79+
File.WriteAllText(tempTrustStoreFile, trustedCert.ExportCertificatePem());
8580

8681
var options = new OtlpMtlsOptions
8782
{

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpMtlsOptionsTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#if NET
55

6-
using Xunit;
7-
86
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
97

108
public class OtlpMtlsOptionsTests
@@ -46,6 +44,22 @@ public void IsEnabled_ReturnsFalse_WhenNoClientCertificateProvided()
4644
Assert.False(options.IsEnabled);
4745
}
4846

47+
[Fact]
48+
public void IsEnabled_ReturnsTrue_WhenCaCertificateFilePathProvided()
49+
{
50+
var options = new OtlpMtlsOptions { CaCertificatePath = "/path/to/ca.crt" };
51+
Assert.True(options.IsEnabled);
52+
}
53+
54+
[Theory]
55+
[InlineData("")]
56+
[InlineData(" ")]
57+
public void IsEnabled_ReturnsFalse_WhenCaCertificateFilePathIsEmpty(string filePath)
58+
{
59+
var options = new OtlpMtlsOptions { CaCertificatePath = filePath };
60+
Assert.False(options.IsEnabled);
61+
}
62+
4963
[Fact]
5064
public void IsEnabled_ReturnsTrue_WhenClientCertificateFilePathProvided()
5165
{

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpSpecConfigDefinitionsTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#if NET
55

6-
using Xunit;
7-
86
namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
97

108
public class OtlpSpecConfigDefinitionsTests

0 commit comments

Comments
 (0)