Skip to content

Commit 70a5ce4

Browse files
committed
chore(): Enforce mTLS requirement in CreateMtlsHttpClient and update tests
1 parent 92a4ee0 commit 70a5ce4

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ internal static class OtlpMtlsHttpClientFactory
1818
/// <param name="mtlsOptions">The mTLS configuration options.</param>
1919
/// <param name="configureClient">Optional action to configure the client.</param>
2020
/// <returns>An HttpClient configured for mTLS.</returns>
21+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="mtlsOptions"/> is null.</exception>
22+
/// <exception cref="InvalidOperationException">Thrown when mTLS is not enabled.</exception>
2123
public static HttpClient CreateMtlsHttpClient(
2224
OtlpMtlsOptions mtlsOptions,
2325
Action<HttpClient>? configureClient = null)
@@ -26,9 +28,9 @@ public static HttpClient CreateMtlsHttpClient(
2628

2729
if (!mtlsOptions.IsEnabled)
2830
{
29-
var client = new HttpClient();
30-
configureClient?.Invoke(client);
31-
return client;
31+
throw new InvalidOperationException(
32+
"mTLS options must include a client or CA certificate path."
33+
);
3234
}
3335

3436
HttpClientHandler? handler = null;

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
1313
public class OtlpMtlsHttpClientFactoryTests
1414
{
1515
[Fact]
16-
public void CreateHttpClient_ReturnsHttpClient_WhenMtlsIsDisabled()
16+
public void CreateHttpClient_ThrowsInvalidOperationException_WhenMtlsIsDisabled()
1717
{
1818
var options = new OtlpMtlsOptions(); // Disabled by default
1919

20-
using var httpClient = OpenTelemetryProtocol.Implementation.OtlpMtlsHttpClientFactory.CreateMtlsHttpClient(options);
21-
22-
Assert.NotNull(httpClient);
23-
Assert.IsType<HttpClient>(httpClient);
20+
Assert.Throws<InvalidOperationException>(() =>
21+
OpenTelemetryProtocol.Implementation.OtlpMtlsHttpClientFactory.CreateMtlsHttpClient(options));
2422
}
2523

2624
[Fact]

0 commit comments

Comments
 (0)