Skip to content

Conversation

@sandy2008
Copy link
Contributor

@sandy2008 sandy2008 commented Jun 20, 2025

Fixes #2009
Design discussion issue #5918

Changes

This PR adds support for mutual TLS (mTLS) configuration to the OTLP exporter on .NET 8 and later. This allows users to specify certificate paths, enable validation, and inject custom server certificate validation logic.

Key changes include:

  • OtlpMtlsOptions class with properties like ClientCertificatePath, ClientKeyPath, CaCertificatePath, EnableFilePermissionChecks, etc.
  • OtlpMtlsHttpClientFactory for creating HttpClient instances configured for mTLS.
  • OtlpMtlsCertificateManager for certificate loading, validation, and file permission checks.
  • Extended event logging via OpenTelemetryProtocolExporterEventSource for mTLS events.
  • Unit tests for configuration handling and certificate behavior.
  • Initial integration and performance test stubs added.

mTLS support is only enabled for builds targeting NET8_0_OR_GREATER.

Merge requirement checklist

@sandy2008 sandy2008 requested a review from a team as a code owner June 20, 2025 07:13
@github-actions github-actions bot added the pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package label Jun 20, 2025

// Configure chain policy
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to support Offline for if a user is running in a closed environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martincostello Does this commit look ok? 7368c9e

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the same concern as @martincostello, your commit looks fine with me. Why that's not a part of the PR?

@sandy2008 sandy2008 force-pushed the main branch 2 times, most recently from bebeff9 to 4b2dc0b Compare June 20, 2025 08:04
@codecov
Copy link

codecov bot commented Jun 20, 2025

Codecov Report

❌ Patch coverage is 58.54701% with 97 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.12%. Comparing base (d2f8e54) to head (cabc172).
⚠️ Report is 149 commits behind head on main.

Files with missing lines Patch % Lines
...tocol/Implementation/OtlpMtlsCertificateManager.cs 47.28% 68 Missing ⚠️
...otocol/Implementation/OtlpMtlsHttpClientFactory.cs 55.93% 26 Missing ⚠️
...tation/OpenTelemetryProtocolExporterEventSource.cs 62.50% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6343      +/-   ##
==========================================
- Coverage   86.67%   86.12%   -0.56%     
==========================================
  Files         258      261       +3     
  Lines       11873    12099     +226     
==========================================
+ Hits        10291    10420     +129     
- Misses       1582     1679      +97     
Flag Coverage Δ
unittests-Project-Experimental 86.02% <58.54%> (-0.50%) ⬇️
unittests-Project-Stable 85.91% <58.54%> (-0.36%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...orter.OpenTelemetryProtocol/OtlpExporterOptions.cs 99.25% <100.00%> (+0.16%) ⬆️
....Exporter.OpenTelemetryProtocol/OtlpMtlsOptions.cs 100.00% <100.00%> (ø)
...tation/OpenTelemetryProtocolExporterEventSource.cs 86.04% <62.50%> (+0.14%) ⬆️
...otocol/Implementation/OtlpMtlsHttpClientFactory.cs 55.93% <55.93%> (ø)
...tocol/Implementation/OtlpMtlsCertificateManager.cs 47.28% <47.28%> (ø)

... and 2 files with indirect coverage changes

@sandy2008 sandy2008 requested a review from martincostello June 23, 2025 11:51
@alanwest alanwest removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Aug 5, 2025
@sandy2008
Copy link
Contributor Author

@alanwest Could you help change the requested change to approve~? Tyvm!

@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Aug 14, 2025
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Aug 18, 2025
@sandy2008
Copy link
Contributor Author

@rajkumar-rangaraj Hi! Any updates?

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 1, 2025
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 2, 2025
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 10, 2025
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 16, 2025
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 24, 2025
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Sep 24, 2025
@Lemorz56
Copy link

Any updates on this?

@sandy2008
Copy link
Contributor Author

Any updates on this?

Pending @rajkumar-rangaraj 's reviews.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2025

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Oct 8, 2025
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Oct 8, 2025
@github-actions
Copy link
Contributor

This PR was marked stale due to lack of activity and will be closed in 7 days. Commenting or pushing will instruct the bot to automatically remove the label. This bot runs once per day.

@github-actions github-actions bot added the Stale Issues and pull requests which have been flagged for closing due to inactivity label Oct 16, 2025
@Kielek Kielek removed the Stale Issues and pull requests which have been flagged for closing due to inactivity label Oct 16, 2025
@rajkumar-rangaraj rajkumar-rangaraj added the keep-open Prevents issues and pull requests being closed as stale label Oct 23, 2025
/// Gets a value indicating whether mTLS is enabled.
/// mTLS is considered enabled if at least the client certificate path is provided.
/// </summary>
public bool IsEnabled => !string.IsNullOrWhiteSpace(this.ClientCertificatePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user sets only OTEL_EXPORTER_OTLP_CERTIFICATE (CA cert) without a client certificate, the factory returns a plain HttpClient. The custom CA validation is completely ignored.

Users wanting to trust a private CA for server validation (one-way TLS with custom CA) won't get it unless they also provide a client certificate. Changing as below could help.

Suggested change
public bool IsEnabled => !string.IsNullOrWhiteSpace(this.ClientCertificatePath);
public bool IsEnabled =>
!string.IsNullOrWhiteSpace(this.ClientCertificatePath) ||
!string.IsNullOrWhiteSpace(this.CaCertificatePath);

// Try to load as PKCS#12 first, then as PEM
try
{
#if NET9_0_OR_GREATER
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users with password-protected PFX/PKCS#12 keystores cannot use it here. Are you planning to follow up on OTEL_EXPORTER_OTLP_CLIENT_KEY_PASSWORD as follow up?

return cert;
}

private static System.Security.Cryptography.X509Certificates.X509Certificate2 CreateExpiredCertificate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used anywhere?


public class OtlpMtlsCertificateManagerTests
{
private const string TestCertPem =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
private const string TestCertPem =
private const string InvalidCertPem=

@rajkumar-rangaraj
Copy link
Member

Please plan to increase the test coverage for these areas.

  • ValidateServerCertificate
  • Test for CA-only configuration
  • Negative test for expired certificates despite having CreateExpiredCertificate() defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep-open Prevents issues and pull requests being closed as stale pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add OTLP Exporter TLS/mTLS configuration options

6 participants