RabbitMQ resource builder should properly encode passing parameters
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
When passing custom username/password to the ```builder.AddRabbitMQ``` method, if those parameters contain special characters like '@','%','/' etc the created connection string would not be formed correctly which leads to various things failing like the built in health check or dependant services connecting to the resource.
Manually connecting from the management web interface does seem to work so this appears to only be a problem with the formed connection string.
### Expected Behavior
I would expect the connection string to be percent-encoded in the relevant parts like what is described in:
[https://www.rabbitmq.com/docs/uri-spec](https://www.rabbitmq.com/docs/uri-spec)
I suggest using ```RabbitMQ.Client.ConnectionFactory``` to create the escaped connection string in ```RabbitMQServerResource.ConnectionStringExpression``` .
### Steps To Reproduce
Create a rabbit resource:
```
var rabbitUser = builder.AddParameter("RABBITUSER");
var rabbitPass = builder.AddParameter("RABBITPASS");
var rabbit = builder.AddRabbitMQ("rabbit", rabbitUser, rabbitPass)
.WithManagementPlugin()
;
```
and have a service be dependent on it:
```
builder.AddProject("start")
.WithReference(rabbit)
.WaitFor(rabbit);
```
in the service use the connectionstring to connect to the resource (im using MassTransit here):
```
builder.Services.AddMassTransit(x =>
{
// A Transport
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host(builder.Configuration.GetConnectionString("rabbit"));
});
});
```
various different characters will cause different exceptions in different locations:
1. RABBITPASS='b@r' will cause ```UriFormatException``` in the health check of the resource
2. RABBITPASS='b%r' the resource will pass the health check, but dependant services will have a ```UriFormatException``` when trying to connect
3. RABBITPASS='#bar' will have the health check never succeed and it will be stuck on ```BrokerUnreachableException```
### Exceptions (if any)
Health check exception:
```
System.UriFormatException: Invalid URI: The hostname could not be parsed.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
at System.Uri..ctor(String uriString)
at Aspire.Hosting.RabbitMQBuilderExtensions.g__CreateConnection|0_3(String connectionString) in /_/src/Aspire.Hosting.RabbitMQ/RabbitMQBuilderExtensions.cs:line 66
at Aspire.Hosting.RabbitMQBuilderExtensions.<>c__DisplayClass0_0.<b__1>d.MoveNext() in /_/src/Aspire.Hosting.RabbitMQ/RabbitMQBuilderExtensions.cs:line 62
--- End of stack trace from previous location ---
at HealthChecks.RabbitMQ.RabbitMQHealthCheck.CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken) in /home/runner/work/AspNetCore.Diagnostics.HealthChecks/AspNetCore.Diagnostics.HealthChecks/src/HealthChecks.Rabbitmq/RabbitMQHealthCheck.cs:line 31
```
### .NET Version info
```
.NET SDK:
Version: 9.0.200
Commit: 90e8b202f2
Workload version: 9.0.200-manifests.69179adf
MSBuild version: 17.13.8+cbc39bea8
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.200\
.NET workloads installed:
[aspire]
Installation Source: VS 17.13.35818.85
Manifest Version: 8.2.2/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
Install Type: Msi
Configured to use loose manifests when installing new manifests.
Host:
Version: 9.0.2
Architecture: x64
Commit: 80aa709f5d
.NET SDKs installed:
6.0.425 [C:\Program Files\dotnet\sdk]
9.0.200 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
```
### Anything else?
Aspire app host versions:
Aspire.AppHost.Sdk=9.0.0
Aspire.Hosting.AppHost=9.1.0
Aspire.Hosting.RabbitMQ=9.1.0
Service:
MassTransit.RabbitMQ=8.3.4
Contributor guide
Assessment
This issue has not been assessed yet.