Skip to content

Commit 10d637a

Browse files
committed
Update tests to force http/protobuf
Made OpenTelemetryApplication more resiliant to misconfiguration now that netstandard/netframework no longer support grpc. Refactor certificate import and update dependencies - Modified `ImportCert` in `CreateDevCert.ps1` to export certificates based on store name and updated friendly name. - Adjusted IIS Express project configuration in `Program.cs`. - Updated assembly bindings in `Web.config` to version `9.0.0.4`. - Added new package references in `C3D.Extensions.SystemWeb.OpenTelemetry.Application.csproj`. - Enhanced application startup in `OpenTelemeteryApplication.cs` with configuration management and logging. - Improved test setup in `SWAIntegrationTests.cs` for OTLP over HTTP with dynamic endpoint configuration.
1 parent 3157dfb commit 10d637a

File tree

6 files changed

+117
-26
lines changed

6 files changed

+117
-26
lines changed

build/CreateDevCert.ps1

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ function ImportCert {
44
[string]$StoreScope,
55
[string]$StoreName
66
)
7-
$export = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, "");
7+
if($StoreName -eq "Root") {
8+
$export = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert, "");
9+
} else {
10+
$export = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, "");
11+
}
12+
#$export = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, "");
813
$flags = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
914
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($export, "", $flags);
10-
$certificate.FriendlyName = "IIS Express Development Certificate"
15+
$certificate.FriendlyName = "ASP.NET Core HTTPS development certificate"
1116

1217
Write-Host "Adding Certificate to $StoreScope/$StoreName"
1318
$Store = New-Object System.Security.Cryptography.X509Certificates.X509Store($StoreName, $StoreScope)
@@ -23,28 +28,36 @@ function ImportCert {
2328
}
2429

2530
$oid = "1.3.6.1.4.1.311.84.1.1"
26-
Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Extensions | Where-Object { $_.Oid.Value -eq $oid } } | Remove-Item
27-
Get-ChildItem -Path Cert:\LocalMachine\Root | Where-Object { $_.Extensions | Where-Object { $_.Oid.Value -eq $oid } } | Remove-Item
31+
Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object { $_.Extensions | Where-Object { $_.Oid.Value -eq $oid } } | Remove-Item
32+
Get-ChildItem -Path Cert:\CurrentUser\Root | Where-Object { $_.Extensions | Where-Object { $_.Oid.Value -eq $oid } } | Remove-Item
2833

2934
Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Extensions | Where-Object { $_.Oid.Value -eq $oid } } | Remove-Item
3035
Get-ChildItem -Path Cert:\LocalMachine\Root | Where-Object { $_.Extensions | Where-Object { $_.Oid.Value -eq $oid } } | Remove-Item
3136

37+
Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object { $_.FriendlyName -eq "IIS Express Development Certificate" } | Remove-Item
38+
Get-ChildItem -Path Cert:\CurrentUser\Root | Where-Object { $_.FriendlyName -eq "IIS Express Development Certificate" } | Remove-Item
39+
3240
Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.FriendlyName -eq "IIS Express Development Certificate" } | Remove-Item
3341
Get-ChildItem -Path Cert:\LocalMachine\Root | Where-Object { $_.FriendlyName -eq "IIS Express Development Certificate" } | Remove-Item
3442

3543
$oid_obj = New-Object System.Security.Cryptography.Oid($oid, "ASP.NET Core HTTPS development certificate")
3644
$ext = New-Object System.Security.Cryptography.X509Certificates.X509Extension($oid_obj, @(2), $false)
3745

38-
$cert = New-SelfSignedCertificate -DnsName "localhost", "localhost" -CertStoreLocation "cert:\LocalMachine\My" -NotAfter (Get-Date).AddYears(5) -FriendlyName "IIS Express Development Certificate" -Extension @( $ext ) -TextExtension @('2.5.29.37={text}1.3.6.1.5.5.7.3.1')
46+
$cert = New-SelfSignedCertificate -DnsName "localhost" `
47+
-CertStoreLocation "cert:\LocalMachine\My" `
48+
-NotAfter (Get-Date).AddYears(5) `
49+
-FriendlyName "IIS Express Development Certificate" `
50+
-Extension @( $ext ) `
51+
-TextExtension @('2.5.29.37={text}1.3.6.1.5.5.7.3.1', '2.5.29.19={text}CA=false') #{critical}
3952
$thumb = $cert.GetCertHashString()
4053
Write-Host "Created certificate $thumb in LocalMachine\My"
4154

4255
For ($i=44300; $i -le 44399; $i++) {
4356
netsh http delete sslcert ipport=0.0.0.0:$i | Out-Null
44-
netsh http add sslcert ipport=0.0.0.0:$i certhash=$thumb appid=`{214124cd-d05b-4309-9af9-9caa44b2b74a`} | Out-Null
57+
netsh http add sslcert ipport=0.0.0.0:$i certhash=$thumb appid="{214124cd-d05b-4309-9af9-9caa44b2b74a}" | Out-Null
4558
}
4659

4760

48-
ImportCert $cert "LocalMachine" "Root"
4961
ImportCert $cert "CurrentUser" "My"
50-
ImportCert $cert "CurrentUser" "Root"
62+
#ImportCert $cert "CurrentUser" "Root" # To avoid the UI Security prompt, we put it in LM Root instead
63+
ImportCert $cert "LocalMachine" "Root"

samples/SWA/SWAAspireHost/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
.WhenDebugMode(r => r.WithHttpHealthCheck("/debug", 204)
6161
.WithEnvironment("WaitForDebugger", "true"))
6262
.WhenUnderTest(r => r.WithTemporaryConfig() // Ensure we use a temp config with random port numbers
63-
.WithDefaultIISExpressEndpoints())
63+
.WithDefaultIISExpressEndpoints()
64+
//,r => r.WithTemporaryConfig()
65+
)
6466
;
6567

6668
var core = builder.AddProject<Projects.SWACore>("core")

samples/SWA/SWAFramework/Web.config

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
</assemblyBinding>
8080
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8181
<dependentAssembly>
82-
<assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
83-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
82+
<assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
83+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
8484
</dependentAssembly>
8585
</assemblyBinding>
8686
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@@ -103,26 +103,26 @@
103103
</assemblyBinding>
104104
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
105105
<dependentAssembly>
106-
<assemblyIdentity name="Microsoft.Extensions.Logging" publicKeyToken="adb9793829ddae60" culture="neutral" />
107-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
106+
<assemblyIdentity name="Microsoft.Extensions.Logging" culture="neutral" publicKeyToken="adb9793829ddae60" />
107+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
108108
</dependentAssembly>
109109
</assemblyBinding>
110110
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
111111
<dependentAssembly>
112-
<assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
113-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
112+
<assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" culture="neutral" publicKeyToken="adb9793829ddae60" />
113+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
114114
</dependentAssembly>
115115
</assemblyBinding>
116116
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
117117
<dependentAssembly>
118-
<assemblyIdentity name="Microsoft.Extensions.Options" publicKeyToken="adb9793829ddae60" culture="neutral" />
119-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
118+
<assemblyIdentity name="Microsoft.Extensions.Options" culture="neutral" publicKeyToken="adb9793829ddae60" />
119+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
120120
</dependentAssembly>
121121
</assemblyBinding>
122122
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
123123
<dependentAssembly>
124-
<assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
125-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
124+
<assemblyIdentity name="Microsoft.Extensions.Primitives" culture="neutral" publicKeyToken="adb9793829ddae60" />
125+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
126126
</dependentAssembly>
127127
</assemblyBinding>
128128
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@@ -133,8 +133,8 @@
133133
</assemblyBinding>
134134
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
135135
<dependentAssembly>
136-
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
137-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
136+
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" culture="neutral" publicKeyToken="cc7b13ffcd2ddd51" />
137+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
138138
</dependentAssembly>
139139
</assemblyBinding>
140140
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
@@ -161,6 +161,18 @@
161161
<bindingRedirect oldVersion="0.0.0.0-1.11.0.400" newVersion="1.11.0.400" />
162162
</dependentAssembly>
163163
</assemblyBinding>
164+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
165+
<dependentAssembly>
166+
<assemblyIdentity name="Microsoft.Extensions.Configuration" publicKeyToken="adb9793829ddae60" culture="neutral" />
167+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.4" newVersion="9.0.0.4" />
168+
</dependentAssembly>
169+
</assemblyBinding>
170+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
171+
<dependentAssembly>
172+
<assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
173+
<bindingRedirect oldVersion="0.0.0.0-8.0.0.5" newVersion="8.0.0.5" />
174+
</dependentAssembly>
175+
</assemblyBinding>
164176
</runtime>
165177
<system.codedom>
166178
<compilers>

src/C3D/Extensions/SystemWeb/OpenTelemetry/Application/C3D.Extensions.SystemWeb.OpenTelemetry.Application.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010

1111
<ItemGroup Label="OpenTelemetry">
12+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.4" />
1213
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
13-
14+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.4" />
15+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.4" />
16+
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
17+
1418
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="$(OpenTelemetryVersion)" />
1519
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="$(OpenTelemetryVersion)" />
1620

src/C3D/Extensions/SystemWeb/OpenTelemetry/Application/OpenTelemeteryApplication.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
23
using Microsoft.Extensions.Logging;
34
using OpenTelemetry;
45
using OpenTelemetry.Logs;
@@ -14,8 +15,14 @@ public class OpenTelemeteryApplication : System.Web.HttpApplication
1415

1516
protected virtual void Application_Start()
1617
{
18+
var config = CreateConfigurationManager();
19+
ConfigureConfiguration(config);
20+
1721
var services = CreateServiceCollection();
1822

23+
services.AddSingleton(config);
24+
services.AddSingleton<IConfiguration>(config);
25+
1926
services.AddLogging(logging =>
2027
{
2128
logging.Configure(options =>
@@ -31,14 +38,32 @@ protected virtual void Application_Start()
3138
ot.IncludeScopes = true;
3239
ot.ParseStateValues = true;
3340
});
41+
ConfigureLogging(logging);
3442
});
3543

36-
services.AddOpenTelemetry()
44+
var otlp = services.AddOpenTelemetry()
3745
.ConfigureResource(ConfigureResource)
3846
.WithTracing(ConfigureTracing)
3947
.WithMetrics(ConfigureMetrics)
40-
.WithLogging(ConfigureLogging)
41-
.UseOtlpExporter();
48+
.WithLogging(ConfigureLogging);
49+
50+
System.Diagnostics.Debug.WriteLine("{0}={1}", "OTEL_EXPORTER_OTLP_PROTOCOL", config.GetValue<string>("OTEL_EXPORTER_OTLP_PROTOCOL"));
51+
System.Diagnostics.Debug.WriteLine("{0}={1}", "OTEL_EXPORTER_OTLP_ENDPOINT", config.GetValue<string>("OTEL_EXPORTER_OTLP_ENDPOINT"));
52+
53+
if (config.GetValue<string>("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf") == "http/protobuf"
54+
&& !string.IsNullOrEmpty(config.GetValue<string>("OTEL_EXPORTER_OTLP_ENDPOINT", "")))
55+
{
56+
otlp.UseOtlpExporter();
57+
System.Diagnostics.Debug.WriteLine("OTLP Exporter enabled");
58+
}
59+
else
60+
{
61+
//System.Diagnostics.Debug.WriteLine("OTLP Exporter not enabled");
62+
System.Console.WriteLine("OTEL_EXPORTER_OTLP_PROTOCOL={0}", config.GetValue<string>("OTEL_EXPORTER_OTLP_PROTOCOL"));
63+
System.Console.WriteLine("OTEL_EXPORTER_OTLP_ENDPOINT={0}", config.GetValue<string>("OTEL_EXPORTER_OTLP_ENDPOINT"));
64+
System.Console.WriteLine("OTLP Exporter not enabled");
65+
otlp.WithLogging(l => l.AddConsoleExporter());
66+
}
4267

4368
ConfigureServiceProvider(services);
4469

@@ -51,6 +76,11 @@ protected virtual void Application_Start()
5176

5277
protected virtual void ConfigureResource(ResourceBuilder builder) { }
5378

79+
protected virtual void ConfigureLogging(ILoggingBuilder logging)
80+
{
81+
82+
}
83+
5484
protected virtual void ConfigureLogging(LoggerProviderBuilder logging) { }
5585

5686
protected virtual void ConfigureMetrics(MeterProviderBuilder metrics)
@@ -68,6 +98,12 @@ protected virtual void ConfigureTracing(TracerProviderBuilder tracing)
6898
}
6999

70100
protected virtual ServiceCollection CreateServiceCollection() => new();
101+
protected virtual ConfigurationManager CreateConfigurationManager() => new();
102+
103+
protected virtual void ConfigureConfiguration(ConfigurationManager configuration)
104+
{
105+
configuration.AddEnvironmentVariables();
106+
}
71107

72108
protected virtual void ConfigureServiceProvider(ServiceCollection services) { }
73109

tests/SWATestProject/Tests/SWAIntegrationTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Runtime.CompilerServices;
33
using Xunit.Abstractions;
44
using Aspire.Hosting;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Hosting;
57

68
namespace SWATestProject.Tests;
79

@@ -20,9 +22,16 @@ private async Task<IDistributedApplicationTestingBuilder> CreateAppHostAsync()
2022
{
2123
//dab.DisableDashboard = true;
2224
dab.EnableResourceLogging = true;
25+
//dab.AllowUnsecuredTransport = true;
26+
27+
//ConfigureOtlpOverHttp(host, outputHelper.WriteLine);
28+
2329
//host.EnvironmentName = "Test";
2430
//dab.AssemblyName = this.GetType().Assembly.GetName().Name;
2531
});
32+
33+
ConfigureOtlpOverHttp(appHost.Configuration, outputHelper.WriteLine);
34+
2635
appHost
2736
.Services
2837
.AddLogging(logging => logging
@@ -59,6 +68,21 @@ private async Task<IDistributedApplicationTestingBuilder> CreateAppHostAsync()
5968
return appHost;
6069
}
6170

71+
private static void ConfigureOtlpOverHttp(HostApplicationBuilderSettings host, Action<string>? logMessage = null) =>
72+
ConfigureOtlpOverHttp(host.Configuration!, logMessage);
73+
74+
private static void ConfigureOtlpOverHttp(ConfigurationManager configuration, Action<string>? logMessage = null)
75+
{
76+
var port = IISExpressEntensions.GetRandomFreePort(20000, 30000);
77+
var otlp = new UriBuilder("https", "localhost", port).ToString();
78+
logMessage?.Invoke($"Setting OTLP endpoint to {otlp}");
79+
var dict = new Dictionary<string, string?>()
80+
{
81+
{ "DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL" , otlp },
82+
{ "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL" , null }
83+
};
84+
configuration.AddInMemoryCollection(dict);
85+
}
6286

6387
private async Task<Aspire.Hosting.DistributedApplication> ArrangeAppHostAsync()
6488
{

0 commit comments

Comments
 (0)