@@ -263,4 +263,107 @@ public void OtlpExporterOptions_ApplyDefaultsTest()
263263 Assert . NotEqual ( defaultOptionsWithData . TimeoutMilliseconds , targetOptionsWithData . TimeoutMilliseconds ) ;
264264 Assert . NotEqual ( defaultOptionsWithData . HttpClientFactory , targetOptionsWithData . HttpClientFactory ) ;
265265 }
266+
267+ #if NET8_0_OR_GREATER
268+ [ Fact ]
269+ public void OtlpExporterOptions_MtlsEnvironmentVariables ( )
270+ {
271+ // Test CA certificate environment variable
272+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CERTIFICATE" , "/path/to/ca.crt" ) ;
273+
274+ try
275+ {
276+ var options = new OtlpExporterOptions ( ) ;
277+
278+ Assert . NotNull ( options . MtlsOptions ) ;
279+ Assert . Equal ( "/path/to/ca.crt" , options . MtlsOptions . CaCertificatePath ) ;
280+ }
281+ finally
282+ {
283+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CERTIFICATE" , null ) ;
284+ }
285+ }
286+
287+ [ Fact ]
288+ public void OtlpExporterOptions_MtlsEnvironmentVariables_ClientCertificate ( )
289+ {
290+ // Test client certificate and key environment variables
291+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" , "/path/to/client.crt" ) ;
292+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_KEY" , "/path/to/client.key" ) ;
293+
294+ try
295+ {
296+ var options = new OtlpExporterOptions ( ) ;
297+
298+ Assert . NotNull ( options . MtlsOptions ) ;
299+ Assert . Equal ( "/path/to/client.crt" , options . MtlsOptions . ClientCertificatePath ) ;
300+ Assert . Equal ( "/path/to/client.key" , options . MtlsOptions . ClientKeyPath ) ;
301+ Assert . True ( options . MtlsOptions . IsEnabled ) ;
302+ }
303+ finally
304+ {
305+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" , null ) ;
306+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_KEY" , null ) ;
307+ }
308+ }
309+
310+ [ Fact ]
311+ public void OtlpExporterOptions_MtlsEnvironmentVariables_AllCertificates ( )
312+ {
313+ // Test all mTLS environment variables together
314+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CERTIFICATE" , "/path/to/ca.crt" ) ;
315+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" , "/path/to/client.crt" ) ;
316+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_KEY" , "/path/to/client.key" ) ;
317+
318+ try
319+ {
320+ var options = new OtlpExporterOptions ( ) ;
321+
322+ Assert . NotNull ( options . MtlsOptions ) ;
323+ Assert . Equal ( "/path/to/ca.crt" , options . MtlsOptions . CaCertificatePath ) ;
324+ Assert . Equal ( "/path/to/client.crt" , options . MtlsOptions . ClientCertificatePath ) ;
325+ Assert . Equal ( "/path/to/client.key" , options . MtlsOptions . ClientKeyPath ) ;
326+ Assert . True ( options . MtlsOptions . IsEnabled ) ;
327+ }
328+ finally
329+ {
330+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CERTIFICATE" , null ) ;
331+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" , null ) ;
332+ Environment . SetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_CLIENT_KEY" , null ) ;
333+ }
334+ }
335+
336+ [ Fact ]
337+ public void OtlpExporterOptions_MtlsEnvironmentVariables_NoEnvironmentVariables ( )
338+ {
339+ // Ensure no mTLS options are set when no environment variables are present
340+ var options = new OtlpExporterOptions ( ) ;
341+
342+ Assert . Null ( options . MtlsOptions ) ;
343+ }
344+
345+ [ Fact ]
346+ public void OtlpExporterOptions_MtlsEnvironmentVariables_UsingIConfiguration ( )
347+ {
348+ // Test using IConfiguration instead of environment variables
349+ var values = new Dictionary < string , string ? >
350+ {
351+ [ "OTEL_EXPORTER_OTLP_CERTIFICATE" ] = "/config/path/to/ca.crt" ,
352+ [ "OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE" ] = "/config/path/to/client.crt" ,
353+ [ "OTEL_EXPORTER_OTLP_CLIENT_KEY" ] = "/config/path/to/client.key" ,
354+ } ;
355+
356+ var configuration = new ConfigurationBuilder ( )
357+ . AddInMemoryCollection ( values )
358+ . Build ( ) ;
359+
360+ var options = new OtlpExporterOptions ( configuration , OtlpExporterOptionsConfigurationType . Default , new ( ) ) ;
361+
362+ Assert . NotNull ( options . MtlsOptions ) ;
363+ Assert . Equal ( "/config/path/to/ca.crt" , options . MtlsOptions . CaCertificatePath ) ;
364+ Assert . Equal ( "/config/path/to/client.crt" , options . MtlsOptions . ClientCertificatePath ) ;
365+ Assert . Equal ( "/config/path/to/client.key" , options . MtlsOptions . ClientKeyPath ) ;
366+ Assert . True ( options . MtlsOptions . IsEnabled ) ;
367+ }
368+ #endif
266369}
0 commit comments