Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
189fd32
Initial plan
Copilot Jul 11, 2025
df9960b
Implement uninstrumented peer visualization for parameters, connectio…
Copilot Jul 11, 2025
d1fb28f
Support direct URL connection strings in peer resolution
Copilot Jul 11, 2025
138b2a7
Initial implementation of comprehensive connection string parser
Copilot Jul 11, 2025
5f58e3f
Add comprehensive connection string parser with extensive test coverage
Copilot Jul 11, 2025
40c8305
Update src/Aspire.Dashboard/Model/ConnectionStringParser.cs
davidfowl Jul 12, 2025
748f053
Fix failing ConnectionStringParser tests for comprehensive connection…
Copilot Jul 12, 2025
7d232fc
Refactor ConnectionStringParser with source-generated regexes and imp…
Copilot Jul 12, 2025
2c7129f
Use ConnectionStringParser for Parameter resources and remove TryPars…
Copilot Jul 12, 2025
01ae0cb
Implement robust hostname validation using RFC-compliant logic
Copilot Jul 12, 2025
40f8cb8
Simplify hostname validation using URI parsing as suggested
Copilot Jul 12, 2025
fe4c50f
Optimize ConnectionStringParser by using static readonly arrays and s…
Copilot Jul 12, 2025
7334208
Enhance GitHubModel resource initialization with connection string re…
davidfowl Jul 12, 2025
0792ea7
Change ConnectionStringParser class from public to internal
Copilot Jul 12, 2025
67540a7
Refactor to eliminate nested transformer loops and extend change dete…
Copilot Jul 14, 2025
d0c1afb
Cache resource addresses on ResourceOutgoingPeerResolver to avoid rec…
Copilot Jul 14, 2025
b7c2862
Move cache from ResourceOutgoingPeerResolver to ResourceViewModel
Copilot Jul 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 261 additions & 0 deletions src/Aspire.Dashboard/Model/ConnectionStringParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux)

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux)

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux)

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux)

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire (Build Linux)

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)

Check failure on line 6 in src/Aspire.Dashboard/Model/ConnectionStringParser.cs

View check run for this annotation

Azure Pipelines / dotnet.aspire

src/Aspire.Dashboard/Model/ConnectionStringParser.cs#L6

src/Aspire.Dashboard/Model/ConnectionStringParser.cs(6,1): error IDE0005: (NETCORE_ENGINEERING_TELEMETRY=Build) Using directive is unnecessary. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0005)
using System.Text.RegularExpressions;

namespace Aspire.Dashboard.Model;

/// <summary>
/// Provides utilities for parsing connection strings to extract host and port information.
/// </summary>
public static class ConnectionStringParser
{
private static readonly Dictionary<string, int> s_schemeDefaultPorts = new(StringComparer.OrdinalIgnoreCase)
{
["http"] = 80,
["https"] = 443,
["ftp"] = 21,
["ftps"] = 990,
["ssh"] = 22,
["telnet"] = 23,
["smtp"] = 25,
["dns"] = 53,
["dhcp"] = 67,
["tftp"] = 69,
["pop3"] = 110,
["ntp"] = 123,
["imap"] = 143,
["snmp"] = 161,
["ldap"] = 389,
["smtps"] = 465,
["ldaps"] = 636,
["imaps"] = 993,
["pop3s"] = 995,
["mssql"] = 1433,
["mysql"] = 3306,
["postgresql"] = 5432,
["postgres"] = 5432,
["redis"] = 6379,
["mongodb"] = 27017,
["amqp"] = 5672,
["amqps"] = 5671,
["kafka"] = 9092
};

private static readonly string[] s_hostAliases = ["host", "server", "data source", "addr", "address", "endpoint", "contact points"];

private static readonly Regex s_hostPortRegex = new(@"(\[[^\]]+\]|[^,:;\s]+)[:|,](\d{1,5})", RegexOptions.Compiled);

/// <summary>
/// Attempts to extract a host and optional port from an arbitrary connection string.
/// Returns <c>true</c> if a host could be identified; otherwise <c>false</c>.
/// </summary>
/// <param name="connectionString">The connection string to parse.</param>
/// <param name="host">When this method returns <c>true</c>, contains the host part with surrounding brackets removed; otherwise, an empty string.</param>
/// <param name="port">When this method returns <c>true</c>, contains the explicit port, scheme-derived default, or <c>null</c> when unavailable; otherwise, <c>null</c>.</param>
/// <returns><c>true</c> if a host was found; otherwise, <c>false</c>.</returns>
public static bool TryDetectHostAndPort(
string connectionString,
[NotNullWhen(true)] out string? host,
out int? port)
{
host = null;
port = null;

if (string.IsNullOrWhiteSpace(connectionString))
{
return false;
}

// 1. URI parse
if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri) && !string.IsNullOrEmpty(uri.Host))
{
host = TrimBrackets(uri.Host);
port = uri.Port != -1 ? uri.Port : DefaultPortFromScheme(uri.Scheme);
return true;
}

// 2. Key-value scan
var keyValuePairs = SplitIntoDictionary(connectionString);
foreach (var hostAlias in s_hostAliases)
{
if (keyValuePairs.TryGetValue(hostAlias, out var token))
{
// First, check if the token is a complete URL
if (Uri.TryCreate(token, UriKind.Absolute, out var tokenUri) && !string.IsNullOrEmpty(tokenUri.Host))
{
host = TrimBrackets(tokenUri.Host);
port = tokenUri.Port != -1 ? tokenUri.Port : DefaultPortFromScheme(tokenUri.Scheme);
return true;
}

// Remove protocol prefixes like "tcp:", "udp:", etc. (but not from complete URLs)
token = RemoveProtocolPrefix(token);

if (token.Contains(',') || token.Contains(':'))
{
var (hostPart, portPart) = SplitOnLast(token);
if (!string.IsNullOrEmpty(hostPart))
{
host = TrimBrackets(hostPart);
port = ParseIntSafe(portPart) ?? PortFromKV(keyValuePairs);
return true;
}
}
else if (!string.IsNullOrEmpty(token))
{
host = TrimBrackets(token);
port = PortFromKV(keyValuePairs);
return true;
}
}
}

// 3. Regex heuristic for host:port or host,port patterns
var match = s_hostPortRegex.Match(connectionString);
if (match.Success)
{
var hostPart = match.Groups[1].Value;
var portPart = match.Groups[2].Value;
if (!string.IsNullOrEmpty(hostPart))
{
host = TrimBrackets(hostPart);
port = ParseIntSafe(portPart);
return true;
}
}

// 4. Looks like single host token (no '=' etc.)
if (LooksLikeHost(connectionString))
{
host = TrimBrackets(connectionString);
port = null;
return true;
}

return false;
}

private static string TrimBrackets(string s) => s.Trim('[', ']');

private static string RemoveProtocolPrefix(string value)
{
// Remove common protocol prefixes like "tcp:", "udp:", "ssl:", etc.
if (string.IsNullOrEmpty(value))
{
return value;
}

var colonIndex = value.IndexOf(':');
if (colonIndex > 0 && colonIndex < value.Length - 1)
{
var prefix = value[..colonIndex].ToLowerInvariant();
// Only remove known protocol prefixes, not arbitrary single letters
var knownProtocols = new[] { "tcp", "udp", "ssl", "tls", "http", "https", "ftp", "ssh" };
if (knownProtocols.Contains(prefix))
{
return value[(colonIndex + 1)..];
}
}

return value;
}

private static int? DefaultPortFromScheme(string? scheme)
{
if (string.IsNullOrEmpty(scheme))
{
return null;
}

return s_schemeDefaultPorts.TryGetValue(scheme, out var port) ? port : null;
}

private static int? PortFromKV(Dictionary<string, string> keyValuePairs)
{
return keyValuePairs.TryGetValue("port", out var portValue) ? ParseIntSafe(portValue) : null;
}

private static int? ParseIntSafe(string? s)
{
if (string.IsNullOrEmpty(s))
{
return null;
}

if (int.TryParse(s, NumberStyles.None, CultureInfo.InvariantCulture, out var value) &&
value >= 0 && value <= 65535)
{
return value;
}

return null;
}

private static Dictionary<string, string> SplitIntoDictionary(string connectionString)
{
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

// Split by semicolon first, then by whitespace if no semicolons found
var parts = connectionString.Contains(';')
? connectionString.Split(';', StringSplitOptions.RemoveEmptyEntries)
: connectionString.Split([' ', '\t', '\n', '\r'], StringSplitOptions.RemoveEmptyEntries);

foreach (var part in parts)
{
var trimmedPart = part.Trim();
var equalIndex = trimmedPart.IndexOf('=');
if (equalIndex > 0 && equalIndex < trimmedPart.Length - 1)
{
var key = trimmedPart[..equalIndex].Trim();
var value = trimmedPart[(equalIndex + 1)..].Trim();
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
{
result[key] = value;
}
}
}

return result;
}

private static (string host, string port) SplitOnLast(string token)
{
// Split on the last occurrence of ':' or ','
var lastColonIndex = token.LastIndexOf(':');
var lastCommaIndex = token.LastIndexOf(',');
var splitIndex = Math.Max(lastColonIndex, lastCommaIndex);

if (splitIndex > 0 && splitIndex < token.Length - 1)
{
return (token[..splitIndex].Trim(), token[(splitIndex + 1)..].Trim());
}

return (token, string.Empty);
}

private static bool LooksLikeHost(string connectionString)
{
// Simple heuristic: if it doesn't contain '=' and looks like a hostname or IP
if (connectionString.Contains('='))
{
return false;
}

// Remove common file path indicators
if (connectionString.StartsWith('/') || connectionString.StartsWith('\\') ||
(connectionString.Length > 2 && connectionString[1] == ':' && char.IsLetter(connectionString[0])))
{
return false;
}

// Should contain dots (for domains) or be a simple name, and not contain spaces
var trimmed = connectionString.Trim();
return !string.IsNullOrEmpty(trimmed) &&
!trimmed.Contains(' ') &&
(trimmed.Contains('.') || !trimmed.Contains('/'));
}
}
68 changes: 67 additions & 1 deletion src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,43 @@ bool TryMatchResourceAddress(string value, [NotNullWhen(true)] out string? name,
{
foreach (var (resourceName, resource) in resources)
{
// Try to match against URL endpoints
foreach (var service in resource.Urls)
{
var hostAndPort = service.Url.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped);

if (string.Equals(hostAndPort, value, StringComparison.OrdinalIgnoreCase))
if (DoesAddressMatch(hostAndPort, value))
{
name = ResourceViewModel.GetResourceName(resource, resources);
resourceMatch = resource;
return true;
}
}

// Try to match against connection strings using comprehensive parsing
if (resource.Properties.TryGetValue(KnownProperties.Resource.ConnectionString, out var connectionStringProperty) &&
connectionStringProperty.Value.TryConvertToString(out var connectionString) &&
ConnectionStringParser.TryDetectHostAndPort(connectionString, out var host, out var port))
{
var endpoint = port.HasValue ? $"{host}:{port.Value}" : host;
if (DoesAddressMatch(endpoint, value))
{
name = ResourceViewModel.GetResourceName(resource, resources);
resourceMatch = resource;
return true;
}
}

// Try to match against parameter values (for Parameter resources that contain URLs)
if (resource.Properties.TryGetValue(KnownProperties.Parameter.Value, out var parameterValueProperty) &&
parameterValueProperty.Value.TryConvertToString(out var parameterValue) &&
TryParseUrlHostAndPort(parameterValue, out var parameterHostAndPort) &&
DoesAddressMatch(parameterHostAndPort, value))
{
name = ResourceViewModel.GetResourceName(resource, resources);
resourceMatch = resource;
return true;
}
}

name = null;
Expand All @@ -151,6 +177,46 @@ bool TryMatchResourceAddress(string value, [NotNullWhen(true)] out string? name,
}
}

private static bool TryParseUrlHostAndPort(string value, [NotNullWhen(true)] out string? hostAndPort)
{
hostAndPort = null;

if (string.IsNullOrEmpty(value))
{
return false;
}

// Try to parse as a URL
if (Uri.TryCreate(value, UriKind.Absolute, out var uri))
{
hostAndPort = uri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped);
return true;
}

return false;
}

private static bool DoesAddressMatch(string endpoint, string value)
{
if (string.Equals(endpoint, value, StringComparison.OrdinalIgnoreCase))
{
return true;
}

// Apply the same transformations that are applied to the peer service value
var transformedEndpoint = endpoint;
foreach (var transformer in s_addressTransformers)
{
transformedEndpoint = transformer(transformedEndpoint);
if (string.Equals(transformedEndpoint, value, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

private static readonly List<Func<string, string>> s_addressTransformers = [
s =>
{
Expand Down
Loading
Loading