Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
91 changes: 91 additions & 0 deletions src/Aspire.Dashboard/Model/ResourceOutgoingPeerResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ internal static bool TryResolvePeerNameCore(IDictionary<string, ResourceViewMode

bool TryMatchResourceAddress(string value, [NotNullWhen(true)] out string? name, [NotNullWhen(true)] out ResourceViewModel? resourceMatch)
{
// First, try to match against resource URLs
foreach (var (resourceName, resource) in resources)
{
foreach (var service in resource.Urls)
Expand All @@ -143,6 +144,28 @@ bool TryMatchResourceAddress(string value, [NotNullWhen(true)] out string? name,
return true;
}
}

// Try to match against connection strings (for GitHub models and other resources with connection strings)
if (resource.Properties.TryGetValue(KnownProperties.Resource.ConnectionString, out var connectionStringProperty) &&
connectionStringProperty.Value.TryConvertToString(out var connectionString) &&
TryExtractEndpointFromConnectionString(connectionString, out var endpoint) &&
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) &&
string.Equals(parameterHostAndPort, value, StringComparison.OrdinalIgnoreCase))
{
name = ResourceViewModel.GetResourceName(resource, resources);
resourceMatch = resource;
return true;
}
}

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

private static bool TryExtractEndpointFromConnectionString(string connectionString, [NotNullWhen(true)] out string? endpoint)
{
endpoint = null;

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

// Parse connection string for Endpoint= pattern (used by GitHub Models and other resources)
var parts = connectionString.Split(';', StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
{
var trimmedPart = part.Trim();
if (trimmedPart.StartsWith("Endpoint=", StringComparison.OrdinalIgnoreCase))
{
var endpointValue = trimmedPart[9..]; // Remove "Endpoint="
if (Uri.TryCreate(endpointValue, UriKind.Absolute, out var uri))
{
endpoint = uri.GetComponents(UriComponents.HostAndPort, UriFormat.UriEscaped);
return true;
}
}
}

return false;
}

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
108 changes: 108 additions & 0 deletions tests/Aspire.Dashboard.Tests/ResourceOutgoingPeerResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Aspire.Tests.Shared.DashboardModel;
using Microsoft.AspNetCore.InternalTesting;
using Xunit;
using Value = Google.Protobuf.WellKnownTypes.Value;

namespace Aspire.Dashboard.Tests;

Expand Down Expand Up @@ -219,6 +220,113 @@ private static bool TryResolvePeerName(IDictionary<string, ResourceViewModel> re
return ResourceOutgoingPeerResolver.TryResolvePeerNameCore(resources, attributes, out peerName, out _);
}

[Fact]
public void ConnectionStringWithEndpoint_Match()
{
// Arrange - GitHub Models resource with connection string containing endpoint
var connectionString = "Endpoint=https://models.github.ai/inference;Key=test-key;Model=openai/gpt-4o-mini;DeploymentId=openai/gpt-4o-mini";
var resources = new Dictionary<string, ResourceViewModel>
{
["github-model"] = CreateResourceWithConnectionString("github-model", connectionString)
};

// Act & Assert
Assert.True(TryResolvePeerName(resources, [KeyValuePair.Create("peer.service", "models.github.ai:443")], out var value));
Assert.Equal("github-model", value);
}

[Fact]
public void ConnectionStringWithEndpointOrganization_Match()
{
// Arrange - GitHub Models resource with organization endpoint
var connectionString = "Endpoint=https://models.github.ai/orgs/myorg/inference;Key=test-key;Model=openai/gpt-4o-mini;DeploymentId=openai/gpt-4o-mini";
var resources = new Dictionary<string, ResourceViewModel>
{
["github-model"] = CreateResourceWithConnectionString("github-model", connectionString)
};

// Act & Assert
Assert.True(TryResolvePeerName(resources, [KeyValuePair.Create("peer.service", "models.github.ai:443")], out var value));
Assert.Equal("github-model", value);
}

[Fact]
public void ParameterWithUrlValue_Match()
{
// Arrange - Parameter resource with URL value
var resources = new Dictionary<string, ResourceViewModel>
{
["api-url-param"] = CreateResourceWithParameterValue("api-url-param", "https://api.example.com:8080/endpoint")
};

// Act & Assert
Assert.True(TryResolvePeerName(resources, [KeyValuePair.Create("peer.service", "api.example.com:8080")], out var value));
Assert.Equal("api-url-param", value);
}

[Fact]
public void ConnectionStringWithoutEndpoint_NoMatch()
{
// Arrange - Connection string without Endpoint property
var connectionString = "Server=localhost;Database=test;User=admin;Password=secret";
var resources = new Dictionary<string, ResourceViewModel>
{
["sql-connection"] = CreateResourceWithConnectionString("sql-connection", connectionString)
};

// Act & Assert
Assert.False(TryResolvePeerName(resources, [KeyValuePair.Create("peer.service", "localhost:1433")], out _));
}

[Fact]
public void ParameterWithNonUrlValue_NoMatch()
{
// Arrange - Parameter resource with non-URL value
var resources = new Dictionary<string, ResourceViewModel>
{
["config-param"] = CreateResourceWithParameterValue("config-param", "simple-config-value")
};

// Act & Assert
Assert.False(TryResolvePeerName(resources, [KeyValuePair.Create("peer.service", "localhost:5000")], out _));
}

private static ResourceViewModel CreateResourceWithConnectionString(string name, string connectionString)
{
var properties = new Dictionary<string, ResourcePropertyViewModel>
{
[KnownProperties.Resource.ConnectionString] = new(
name: KnownProperties.Resource.ConnectionString,
value: Value.ForString(connectionString),
isValueSensitive: false,
knownProperty: null,
priority: 0)
};

return ModelTestHelpers.CreateResource(
appName: name,
resourceType: KnownResourceTypes.ConnectionString,
properties: properties);
}

private static ResourceViewModel CreateResourceWithParameterValue(string name, string value)
{
var properties = new Dictionary<string, ResourcePropertyViewModel>
{
[KnownProperties.Parameter.Value] = new(
name: KnownProperties.Parameter.Value,
value: Value.ForString(value),
isValueSensitive: false,
knownProperty: null,
priority: 0)
};

return ModelTestHelpers.CreateResource(
appName: name,
resourceType: KnownResourceTypes.Parameter,
properties: properties);
}

private sealed class MockDashboardClient(Task<ResourceViewModelSubscription> subscribeResult) : IDashboardClient
{
public bool IsEnabled => true;
Expand Down