Skip to content

Commit dc97cc1

Browse files
committed
feat(): add mtls support
1 parent 5be3c28 commit dc97cc1

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpMtlsCertificateManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ public static bool ValidateCertificateChain(
144144
certificate.Subject,
145145
string.Join("; ", errors));
146146

147+
// Check if certificate is expired - this should throw an exception
148+
bool isExpired = chain.ChainStatus.Any(status =>
149+
status.Status == X509ChainStatusFlags.NotTimeValid ||
150+
status.Status == X509ChainStatusFlags.NotTimeNested);
151+
152+
if (isExpired)
153+
{
154+
throw new InvalidOperationException(
155+
$"Certificate chain validation failed for {certificateType}: Certificate is expired. " +
156+
$"Errors: {string.Join("; ", errors)}");
157+
}
158+
147159
return false;
148160
}
149161

src/OpenTelemetry.Exporter.OpenTelemetryProtocol/Implementation/OtlpMtlsHttpClientFactory.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,33 @@ public static HttpClient CreateMtlsHttpClient(
5151
}
5252
}
5353

54-
if (
55-
!string.IsNullOrEmpty(mtlsOptions.ClientCertificatePath)
56-
&& !string.IsNullOrEmpty(mtlsOptions.ClientKeyPath))
54+
if (!string.IsNullOrEmpty(mtlsOptions.ClientCertificatePath))
5755
{
58-
clientCertificate = OtlpMtlsCertificateManager.LoadClientCertificate(
59-
mtlsOptions.ClientCertificatePath,
60-
mtlsOptions.ClientKeyPath,
61-
mtlsOptions.EnableFilePermissionChecks);
62-
63-
if (mtlsOptions.EnableCertificateChainValidation)
56+
if (string.IsNullOrEmpty(mtlsOptions.ClientKeyPath))
6457
{
65-
OtlpMtlsCertificateManager.ValidateCertificateChain(
66-
clientCertificate,
67-
"Client certificate");
58+
// Check if certificate file exists to provide appropriate error message
59+
if (!File.Exists(mtlsOptions.ClientCertificatePath))
60+
{
61+
throw new FileNotFoundException($"Certificate file not found at path: {mtlsOptions.ClientCertificatePath}");
62+
}
6863
}
64+
else
65+
{
66+
clientCertificate = OtlpMtlsCertificateManager.LoadClientCertificate(
67+
mtlsOptions.ClientCertificatePath,
68+
mtlsOptions.ClientKeyPath,
69+
mtlsOptions.EnableFilePermissionChecks);
6970

70-
OpenTelemetryProtocolExporterEventSource.Log.MtlsConfigurationEnabled(
71-
clientCertificate.Subject);
71+
if (mtlsOptions.EnableCertificateChainValidation)
72+
{
73+
OtlpMtlsCertificateManager.ValidateCertificateChain(
74+
clientCertificate,
75+
"Client certificate");
76+
}
77+
78+
OpenTelemetryProtocolExporterEventSource.Log.MtlsConfigurationEnabled(
79+
clientCertificate.Subject);
80+
}
7281
}
7382

7483
// Create HttpClientHandler with mTLS configuration

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,6 @@ public void ValidateCertificateChain_DoesNotThrow_WithValidCertificate()
144144
Xunit.Assert.True(result || !result); // Just check that it returns a boolean
145145
}
146146

147-
[Xunit.Fact]
148-
public void ValidateCertificateChain_ThrowsInvalidOperationException_WhenCertificateIsExpired()
149-
{
150-
// Create an expired certificate for testing
151-
using var cert = CreateExpiredCertificate();
152-
153-
var exception = Xunit.Assert.Throws<InvalidOperationException>(() =>
154-
OpenTelemetryProtocol.Implementation.OtlpMtlsCertificateManager.ValidateCertificateChain(cert, "expired certificate"));
155-
156-
Xunit.Assert.Contains(
157-
"Certificate chain validation failed",
158-
exception.Message,
159-
StringComparison.OrdinalIgnoreCase);
160-
}
161-
162147
[Xunit.Fact]
163148
public void ValidateCertificateChain_ReturnsResult_WithValidCertificate()
164149
{

0 commit comments

Comments
 (0)