From 4fe4a8a74d3696d0b12fcb56f31f03e0a0198d55 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 08:37:02 -0700 Subject: [PATCH 1/8] Resolve cross-compute-environment endpoint references to Foundry hosted agents Fixes #17749. When a resource deployed to App Service, Azure Container Apps, or Kubernetes used WithReference() to reference a Foundry hosted agent, publishing failed because the publisher resolved the endpoint against its own local endpoint map, which does not contain the agent (deployed to the Foundry project compute environment). Introduce a shared ComputeEnvironmentEndpointResolver that, when an endpoint's owning resource is deployed to a different compute environment than the current publisher, delegates resolution to that owning environment's GetEndpointPropertyExpression. The three compute-environment publishers now call it in both the EndpointReference and EndpointReferenceExpression branches. Azure Front Door and the Foundry hosted-agent resolver are refactored onto the same shared lookup. AzureCognitiveServicesProjectResource gets a GetEndpointPropertyExpression override because the agent address is already a full https URL; the default scheme://host composition would produce a malformed double-scheme value. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Aspire.Hosting.Azure.AppContainers.csproj | 1 + .../BaseContainerAppContext.cs | 15 ++ .../Aspire.Hosting.Azure.AppService.csproj | 1 + .../AzureAppServiceWebsiteContext.cs | 15 ++ .../Aspire.Hosting.Azure.FrontDoor.csproj | 4 + .../AzureFrontDoorExtensions.cs | 7 +- .../Aspire.Hosting.Foundry.csproj | 1 + .../HostedAgent/AzureHostedAgentResource.cs | 3 +- .../Project/ProjectResource.cs | 29 ++++ .../Aspire.Hosting.Kubernetes.csproj | 1 + .../KubernetesResource.cs | 17 +++ .../ComputeEnvironmentEndpointResolver.cs | 118 +++++++++++++++ .../AzureKubernetesFoundryReferenceTests.cs | 70 +++++++++ .../AzureAppServiceTests.cs | 54 +++++++ .../AzureContainerAppsTests.cs | 54 +++++++ ...edAcrossComputeEnvironments.verified.bicep | 134 ++++++++++++++++++ ...vedAcrossComputeEnvironments.verified.json | 16 +++ ...edAcrossComputeEnvironments.verified.bicep | 86 +++++++++++ ...vedAcrossComputeEnvironments.verified.json | 13 ++ 19 files changed, 631 insertions(+), 8 deletions(-) create mode 100644 src/Shared/ComputeEnvironmentEndpointResolver.cs create mode 100644 tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs create mode 100644 tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep create mode 100644 tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json create mode 100644 tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep create mode 100644 tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json diff --git a/src/Aspire.Hosting.Azure.AppContainers/Aspire.Hosting.Azure.AppContainers.csproj b/src/Aspire.Hosting.Azure.AppContainers/Aspire.Hosting.Azure.AppContainers.csproj index 52e78c556c6..92848c8485c 100644 --- a/src/Aspire.Hosting.Azure.AppContainers/Aspire.Hosting.Azure.AppContainers.csproj +++ b/src/Aspire.Hosting.Azure.AppContainers/Aspire.Hosting.Azure.AppContainers.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs b/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs index c0d14ac555f..1b1a13d3f4f 100644 --- a/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs +++ b/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs @@ -222,6 +222,15 @@ BicepValue GetHostValue(string? prefix = null, string? suffix = null) if (value is EndpointReference ep) { + // The referenced endpoint may belong to a resource deployed to a different compute + // environment (for example a Foundry hosted agent). In that case delegate to the owning + // compute environment instead of looking it up in this environment's local endpoint map. + if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + ep.Property(EndpointProperty.Url), out var crossExpr, _containerAppEnvironmentContext.Environment)) + { + return ProcessValue(crossExpr, secretType, parent); + } + var context = ep.Resource == resource ? this : _containerAppEnvironmentContext.GetContainerAppContext(ep.Resource); @@ -274,6 +283,12 @@ BicepValue GetHostValue(string? prefix = null, string? suffix = null) if (value is EndpointReferenceExpression epExpr) { + if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + epExpr, out var crossExpr, _containerAppEnvironmentContext.Environment)) + { + return ProcessValue(crossExpr, secretType, parent); + } + var context = epExpr.Endpoint.Resource == resource ? this : _containerAppEnvironmentContext.GetContainerAppContext(epExpr.Endpoint.Resource); diff --git a/src/Aspire.Hosting.Azure.AppService/Aspire.Hosting.Azure.AppService.csproj b/src/Aspire.Hosting.Azure.AppService/Aspire.Hosting.Azure.AppService.csproj index 84d433e5124..1570e890831 100644 --- a/src/Aspire.Hosting.Azure.AppService/Aspire.Hosting.Azure.AppService.csproj +++ b/src/Aspire.Hosting.Azure.AppService/Aspire.Hosting.Azure.AppService.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs b/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs index ca89a2faac7..e502f24250e 100644 --- a/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs +++ b/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs @@ -167,6 +167,15 @@ private void ProcessEndpoints() if (value is EndpointReference ep) { + // The referenced endpoint may belong to a resource deployed to a different compute + // environment (for example a Foundry hosted agent). In that case delegate to the owning + // compute environment instead of looking it up in this environment's local endpoint map. + if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + ep.Property(EndpointProperty.Url), out var crossExpr, environmentContext.Environment)) + { + return ProcessValue(crossExpr, secretType, parent, isSlot); + } + var context = environmentContext.GetAppServiceContext(ep.Resource); return isSlot ? (GetEndpointValue(context._slotEndpointMapping[ep.EndpointName], EndpointProperty.Url), secretType) : @@ -206,6 +215,12 @@ private void ProcessEndpoints() if (value is EndpointReferenceExpression epExpr) { + if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + epExpr, out var crossExpr, environmentContext.Environment)) + { + return ProcessValue(crossExpr, secretType, parent, isSlot); + } + var context = environmentContext.GetAppServiceContext(epExpr.Endpoint.Resource); var mapping = isSlot ? context._slotEndpointMapping[epExpr.Endpoint.EndpointName] : context._endpointMapping[epExpr.Endpoint.EndpointName]; var val = GetEndpointValue(mapping, epExpr.Property); diff --git a/src/Aspire.Hosting.Azure.FrontDoor/Aspire.Hosting.Azure.FrontDoor.csproj b/src/Aspire.Hosting.Azure.FrontDoor/Aspire.Hosting.Azure.FrontDoor.csproj index 0715429ec9b..f5e42917159 100644 --- a/src/Aspire.Hosting.Azure.FrontDoor/Aspire.Hosting.Azure.FrontDoor.csproj +++ b/src/Aspire.Hosting.Azure.FrontDoor/Aspire.Hosting.Azure.FrontDoor.csproj @@ -15,6 +15,10 @@ + + + + diff --git a/src/Aspire.Hosting.Azure.FrontDoor/AzureFrontDoorExtensions.cs b/src/Aspire.Hosting.Azure.FrontDoor/AzureFrontDoorExtensions.cs index 167c95f44b3..73e557b2926 100644 --- a/src/Aspire.Hosting.Azure.FrontDoor/AzureFrontDoorExtensions.cs +++ b/src/Aspire.Hosting.Azure.FrontDoor/AzureFrontDoorExtensions.cs @@ -201,16 +201,11 @@ public static IResourceBuilder WithOrigin( private static IComputeEnvironmentResource GetEffectiveComputeEnvironment(IResource resource) { - if (resource.GetComputeEnvironment() is { } computeEnvironment) + if (ComputeEnvironmentEndpointResolver.TryGetEffectiveComputeEnvironment(resource, out var computeEnvironment)) { return computeEnvironment; } - if (resource.GetDeploymentTargetAnnotation()?.ComputeEnvironment is { } deploymentComputeEnvironment) - { - return deploymentComputeEnvironment; - } - throw new InvalidOperationException( $"Resource '{resource.Name}' does not have a compute environment. " + "Ensure a compute environment (e.g., Azure Container Apps, Azure App Service) is configured in the application model."); diff --git a/src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj b/src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj index 709af738f3a..d5dd4cae6ad 100644 --- a/src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj +++ b/src/Aspire.Hosting.Foundry/Aspire.Hosting.Foundry.csproj @@ -23,6 +23,7 @@ + diff --git a/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs b/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs index 1c4f139d339..5c28ebb36c5 100644 --- a/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs +++ b/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs @@ -429,8 +429,7 @@ internal static async Task> GetResolvedEnvironmentVar throw CreateEndpointResolutionException(hostedAgent, resource, environmentVariableName, endpointReference, $"Endpoint '{endpoint.Name}' is internal. Foundry hosted agents can only reference externally exposed endpoints during publish."); } - var deploymentTarget = endpointReference.Resource.GetDeploymentTargetAnnotation(); - if (deploymentTarget?.ComputeEnvironment is not { } computeEnvironment) + if (!ComputeEnvironmentEndpointResolver.TryGetEffectiveComputeEnvironment(endpointReference.Resource, out var computeEnvironment)) { var reason = $"Resource '{endpointReference.Resource.Name}' does not have a compute environment deployment target."; throw CreateEndpointResolutionException(hostedAgent, resource, environmentVariableName, endpointReference, reason); diff --git a/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs b/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs index 8fcabc67d52..b9dfd32b655 100644 --- a/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs +++ b/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs @@ -212,6 +212,35 @@ public AzureContainerRegistryResource? ContainerRegistry /// Get the address for the particular agent's endpoint. /// ReferenceExpression IComputeEnvironmentResource.GetHostAddressExpression(EndpointReference endpointReference) + => GetAgentAddressExpression(endpointReference); + + /// + /// Produces the endpoint property expression for a hosted agent endpoint owned by this Foundry project. + /// + /// + /// The agent address is already a fully-qualified https URL + /// (for example https://{account}.services.ai.azure.com/.../agents/{name}), so it is + /// returned directly for URL-shaped properties. The default + /// implementation composes + /// {scheme}://{host}, which would produce a malformed double-scheme value + /// (for example http://https://...). Only the properties that WithReference emits for a + /// hosted agent endpoint are supported. + /// + ReferenceExpression IComputeEnvironmentResource.GetEndpointPropertyExpression(EndpointReferenceExpression endpointReferenceExpression) + { + ArgumentNullException.ThrowIfNull(endpointReferenceExpression); + + var property = endpointReferenceExpression.Property; + return property switch + { + EndpointProperty.Url => GetAgentAddressExpression(endpointReferenceExpression.Endpoint), + EndpointProperty.Scheme => ReferenceExpression.Create($"https"), + _ => throw new InvalidOperationException( + $"The endpoint property '{property}' is not supported for Foundry hosted agent endpoints. Only 'Url' and 'Scheme' are supported.") + }; + } + + private ReferenceExpression GetAgentAddressExpression(EndpointReference endpointReference) { var resource = endpointReference.Resource; return ReferenceExpression.Create($"{Endpoint}/agents/{resource.Name}"); diff --git a/src/Aspire.Hosting.Kubernetes/Aspire.Hosting.Kubernetes.csproj b/src/Aspire.Hosting.Kubernetes/Aspire.Hosting.Kubernetes.csproj index 04e87db475a..a7992c280ff 100644 --- a/src/Aspire.Hosting.Kubernetes/Aspire.Hosting.Kubernetes.csproj +++ b/src/Aspire.Hosting.Kubernetes/Aspire.Hosting.Kubernetes.csproj @@ -14,6 +14,7 @@ + diff --git a/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs b/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs index 66921ab26ad..a14f9c63d1b 100644 --- a/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs +++ b/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs @@ -486,6 +486,16 @@ private async Task ProcessValueAsync(KubernetesEnvironmentContext contex if (value is EndpointReference ep) { + // The referenced endpoint may belong to a resource deployed to a different compute + // environment (for example a Foundry hosted agent). In that case delegate to the owning + // compute environment instead of looking it up in this environment's local endpoint map. + if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + ep.Property(EndpointProperty.Url), out var crossExpr, kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment)) + { + value = crossExpr; + continue; + } + var referencedResource = ep.Resource == this ? this : await context.CreateKubernetesResourceAsync(ep.Resource, executionContext, default).ConfigureAwait(false); @@ -516,6 +526,13 @@ private async Task ProcessValueAsync(KubernetesEnvironmentContext contex if (value is EndpointReferenceExpression epExpr) { + if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + epExpr, out var crossExpr, kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment)) + { + value = crossExpr; + continue; + } + var referencedResource = epExpr.Endpoint.Resource == this ? this : await context.CreateKubernetesResourceAsync(epExpr.Endpoint.Resource, executionContext, default).ConfigureAwait(false); diff --git a/src/Shared/ComputeEnvironmentEndpointResolver.cs b/src/Shared/ComputeEnvironmentEndpointResolver.cs new file mode 100644 index 00000000000..7470bac7db3 --- /dev/null +++ b/src/Shared/ComputeEnvironmentEndpointResolver.cs @@ -0,0 +1,118 @@ +// 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 Aspire.Hosting.ApplicationModel; + +namespace Aspire.Hosting; + +/// +/// Helper for resolving endpoint references that point at a resource deployed to a different +/// compute environment than the one currently generating deployment artifacts. +/// +/// +/// Compute environment publishers (App Service, Azure Container Apps, Kubernetes) resolve +/// endpoint references against their own local endpoint map. When a resource references an +/// endpoint owned by a resource deployed to a different compute environment (for example a +/// Foundry hosted agent deployed to an AzureCognitiveServicesProjectResource), the +/// endpoint is not present in the local map and the lookup fails. In that case the owning +/// compute environment knows how to express the endpoint property, so we delegate to it. +/// This mirrors the inverse-direction logic used by Foundry hosted agents when they reference +/// endpoints owned by App Service/ACA/Kubernetes resources. +/// +internal static class ComputeEnvironmentEndpointResolver +{ + /// + /// Attempts to produce a for an endpoint property by + /// delegating to the compute environment that owns the endpoint's resource, when that + /// environment is different from the publisher's current compute environment(s). + /// + /// The endpoint reference expression to resolve. + /// + /// When this method returns , contains the delegated reference expression. + /// + /// + /// The compute environment(s) the current publisher is generating artifacts for. When the + /// endpoint's owning resource deploys to one of these, resolution is left to the local + /// endpoint map and this method returns . + /// + /// + /// if the endpoint is owned by a different compute environment and a + /// delegated expression was produced; otherwise . + /// + public static bool TryGetCrossEnvironmentEndpointExpression( + EndpointReferenceExpression endpointReferenceExpression, + [NotNullWhen(true)] out ReferenceExpression? expression, + params IComputeEnvironmentResource?[] currentComputeEnvironments) + { + ArgumentNullException.ThrowIfNull(endpointReferenceExpression); + + expression = null; + + var owningResource = endpointReferenceExpression.Endpoint.Resource; + + // If the endpoint's owning resource deploys to one of the current publisher's compute + // environments, the endpoint lives in the local endpoint map. Leave resolution to the + // existing local lookup so generated artifacts (bicep parameters, helm values, etc.) + // are unchanged. GetDeploymentTargetAnnotation(env) accounts for ComputeEnvironmentAnnotation + // binding and returns null when the resource does not deploy to that environment. It is also + // binding-aware and never throws on multi-target resources, so checking it before resolving + // the owning environment preserves existing behavior. + foreach (var current in currentComputeEnvironments) + { + if (current is not null && owningResource.GetDeploymentTargetAnnotation(current) is not null) + { + return false; + } + } + + // The owning resource has no compute environment (for example a plain resource that is not + // deployed anywhere). There is nothing to delegate to, so let the local lookup handle it. + if (!TryGetEffectiveComputeEnvironment(owningResource, out var owningComputeEnvironment)) + { + return false; + } + + // Defensive: if the resolved owning environment is itself one of the current publisher's + // environments, prefer the local map. + foreach (var current in currentComputeEnvironments) + { + if (ReferenceEquals(current, owningComputeEnvironment)) + { + return false; + } + } + +#pragma warning disable ASPIRECOMPUTE002 // Experimental: compute environment endpoint expression + expression = owningComputeEnvironment.GetEndpointPropertyExpression(endpointReferenceExpression); +#pragma warning restore ASPIRECOMPUTE002 + + return true; + } + + /// + /// Resolves the compute environment that a resource is deployed to. A resource may be bound to a + /// compute environment explicitly (via ) or + /// implicitly through its deployment target. + /// + /// The resource whose compute environment should be resolved. + /// + /// When this method returns , contains the owning compute environment. + /// + /// + /// if a compute environment was resolved; otherwise . + /// + public static bool TryGetEffectiveComputeEnvironment( + IResource resource, + [NotNullWhen(true)] out IComputeEnvironmentResource? computeEnvironment) + { + ArgumentNullException.ThrowIfNull(resource); + + // Prefer an explicit compute environment binding, then fall back to the deployment target's + // compute environment. This matches how endpoint references are resolved elsewhere + // (Azure Front Door origins, Foundry hosted agents) so all call sites agree on "where is + // this resource deployed". + computeEnvironment = resource.GetComputeEnvironment() ?? resource.GetDeploymentTargetAnnotation()?.ComputeEnvironment; + return computeEnvironment is not null; + } +} diff --git a/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs b/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs new file mode 100644 index 00000000000..f84daf8f1e8 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs @@ -0,0 +1,70 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#pragma warning disable ASPIREAZURE003 +#pragma warning disable ASPIREPIPELINES003 + +using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Publishing; +using Aspire.Hosting.Tests; +using Aspire.Hosting.Utils; +using Microsoft.Extensions.DependencyInjection; + +namespace Aspire.Hosting.Azure.Tests; + +public class AzureKubernetesFoundryReferenceTests +{ + [Fact] + public async Task EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments() + { + using var tempDir = new TestTempDirectory(); + using var builder = TestDistributedApplicationBuilder.Create( + DistributedApplicationOperation.Publish, + tempDir.Path); + + builder.Services.AddSingleton(); + + var aks = builder.AddAzureKubernetesEnvironment("aks"); + + var project = builder.AddFoundry("foundry") + .AddProject("project"); + + // The agent app is deployed to the Foundry project compute environment via AsHostedAgent. + var agent = builder.AddProject("agent", launchProfileName: null) + .WithHttpEndpoint() + .WithExternalHttpEndpoints(); + agent.AsHostedAgent(project); + + // The web app is deployed to Azure Kubernetes and references the Foundry hosted agent. + // The Kubernetes publisher must delegate endpoint resolution to the Foundry compute + // environment rather than looking the agent up in its own (local) endpoint map, which + // does not contain the cross-environment agent. See issue #17749. + // WithReference(agent) exercises the bare EndpointReference branch; the explicit + // Property(Url) environment variable exercises the EndpointReferenceExpression branch. + builder.AddProject("web", launchProfileName: null) + .WithHttpEndpoint() + .WithExternalHttpEndpoints() + .WithComputeEnvironment(aks) + .WithReference(agent) + .WithEnvironment("AGENT_URL", agent.GetEndpoint("http").Property(EndpointProperty.Url)); + + await using var app = builder.Build(); + await app.RunAsync(); + + // The resolved environment variable values are emitted into the Helm chart's values.yaml. + // The agent endpoint must resolve to the Foundry project endpoint composed with the agent + // path. Prior to the fix, publishing threw because the agent endpoint was not present in the + // Kubernetes environment's local endpoint map. + var valuesPath = Directory.EnumerateFiles(tempDir.Path, "values.yaml", SearchOption.AllDirectories).Single(); + var values = await File.ReadAllTextAsync(valuesPath); + + Assert.Contains("AGENT_HTTP: \"{project.outputs.endpoint}/agents/agent\"", values); + Assert.Contains("AGENT_URL: \"{project.outputs.endpoint}/agents/agent\"", values); + Assert.Contains("services__agent__http__0: \"{project.outputs.endpoint}/agents/agent\"", values); + } + + private sealed class Project : IProjectMetadata + { + public string ProjectPath => "project"; + } +} diff --git a/tests/Aspire.Hosting.Azure.Tests/AzureAppServiceTests.cs b/tests/Aspire.Hosting.Azure.Tests/AzureAppServiceTests.cs index 2c5c185f59b..d8730e90796 100644 --- a/tests/Aspire.Hosting.Azure.Tests/AzureAppServiceTests.cs +++ b/tests/Aspire.Hosting.Azure.Tests/AzureAppServiceTests.cs @@ -6,6 +6,7 @@ using System.Text.Json.Nodes; using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Foundry; using Aspire.Hosting.Pipelines; using Aspire.Hosting.Utils; using Aspire.TestUtilities; @@ -225,6 +226,59 @@ await Verify(manifest.ToString(), "json") .AppendContentAsFile(bicep, "bicep"); } + [Fact] + public async Task EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments() + { + var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + + var appServiceEnv = builder.AddAzureAppServiceEnvironment("env"); + + var project = builder.AddFoundry("foundry") + .AddProject("project"); + + // The agent app is deployed to the Foundry project compute environment via AsHostedAgent. + var agent = builder.AddProject("agent", launchProfileName: null) + .WithHttpEndpoint() + .WithExternalHttpEndpoints(); + agent.AsHostedAgent(project); + + // The web app is deployed to App Service and references the Foundry hosted agent. The App + // Service publisher must delegate endpoint resolution to the Foundry compute environment + // rather than looking the agent up in its own (App Service) endpoint map. See issue #17749. + // WithReference(agent) exercises the bare EndpointReference branch; the explicit + // Property(Url) environment variable exercises the EndpointReferenceExpression branch. + var web = builder.AddProject("web", launchProfileName: null) + .WithHttpEndpoint() + .WithExternalHttpEndpoints() + .WithComputeEnvironment(appServiceEnv) + .WithReference(agent) + .WithEnvironment("AGENT_URL", agent.GetEndpoint("http").Property(EndpointProperty.Url)); + + using var app = builder.Build(); + + await ExecuteBeforeStartHooksAsync(app, default); + + SetFoundryProjectOutputs(project.Resource); + + web.Resource.TryGetLastAnnotation(out var target); + + var resource = target?.DeploymentTarget as AzureProvisioningResource; + + Assert.NotNull(resource); + + var (manifest, bicep) = await GetManifestWithBicep(resource); + + await Verify(manifest.ToString(), "json") + .AppendContentAsFile(bicep, "bicep"); + } + + private static void SetFoundryProjectOutputs(AzureCognitiveServicesProjectResource project) + { + project.Outputs["endpoint"] = "https://account.services.ai.azure.com/api/projects/my-project"; + project.Outputs["APPLICATION_INSIGHTS_CONNECTION_STRING"] = ""; + project.ProvisioningTaskCompletionSource?.TrySetResult(); + } + [Fact] public async Task AzureAppServiceSupportBaitAndSwitchResources() { diff --git a/tests/Aspire.Hosting.Azure.Tests/AzureContainerAppsTests.cs b/tests/Aspire.Hosting.Azure.Tests/AzureContainerAppsTests.cs index 7fbcb2451d4..1fa7da1cf95 100644 --- a/tests/Aspire.Hosting.Azure.Tests/AzureContainerAppsTests.cs +++ b/tests/Aspire.Hosting.Azure.Tests/AzureContainerAppsTests.cs @@ -10,6 +10,7 @@ using System.Text.Json.Nodes; using Aspire.Hosting.ApplicationModel; using Aspire.Hosting.Azure.AppContainers; +using Aspire.Hosting.Foundry; using Aspire.Hosting.Pipelines; using Aspire.Hosting.Utils; using Azure.Provisioning; @@ -120,6 +121,59 @@ await Verify(manifest.ToString(), "json") .AppendContentAsFile(bicep, "bicep"); } + [Fact] + public async Task EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments() + { + var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + + var acaEnv = builder.AddAzureContainerAppEnvironment("env"); + + var project = builder.AddFoundry("foundry") + .AddProject("project"); + + // The agent app is deployed to the Foundry project compute environment via AsHostedAgent. + var agent = builder.AddProject("agent", launchProfileName: null) + .WithHttpEndpoint() + .WithExternalHttpEndpoints(); + agent.AsHostedAgent(project); + + // The web app is deployed to Azure Container Apps and references the Foundry hosted agent. + // The ACA publisher must delegate endpoint resolution to the Foundry compute environment + // rather than looking the agent up in its own endpoint map. See issue #17749. + // WithReference(agent) exercises the bare EndpointReference branch; the explicit + // Property(Url) environment variable exercises the EndpointReferenceExpression branch. + var web = builder.AddProject("web", launchProfileName: null) + .WithHttpEndpoint() + .WithExternalHttpEndpoints() + .WithComputeEnvironment(acaEnv) + .WithReference(agent) + .WithEnvironment("AGENT_URL", agent.GetEndpoint("http").Property(EndpointProperty.Url)); + + using var app = builder.Build(); + + await ExecuteBeforeStartHooksAsync(app, default); + + SetFoundryProjectOutputs(project.Resource); + + var target = web.Resource.GetDeploymentTargetAnnotation(); + + var resource = target?.DeploymentTarget as AzureProvisioningResource; + + Assert.NotNull(resource); + + var (manifest, bicep) = await GetManifestWithBicep(resource); + + await Verify(manifest.ToString(), "json") + .AppendContentAsFile(bicep, "bicep"); + } + + private static void SetFoundryProjectOutputs(AzureCognitiveServicesProjectResource project) + { + project.Outputs["endpoint"] = "https://account.services.ai.azure.com/api/projects/my-project"; + project.Outputs["APPLICATION_INSIGHTS_CONNECTION_STRING"] = ""; + project.ProvisioningTaskCompletionSource?.TrySetResult(); + } + [Fact] public async Task AddExecutableResourceWithPublishAsDockerFileWithAppsInfrastructureAddsDeploymentTargetWithContainerAppToContainerResources() { diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep new file mode 100644 index 00000000000..b86e161b40b --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -0,0 +1,134 @@ +@description('The location for the resource(s) to be deployed.') +param location string = resourceGroup().location + +param env_outputs_azure_container_registry_endpoint string + +param env_outputs_planid string + +param env_outputs_azure_container_registry_managed_identity_id string + +param env_outputs_azure_container_registry_managed_identity_client_id string + +param web_containerimage string + +param web_containerport string + +param project_outputs_endpoint string + +param env_outputs_azure_app_service_dashboard_uri string + +param env_outputs_azure_website_contributor_managed_identity_id string + +param env_outputs_azure_website_contributor_managed_identity_principal_id string + +resource mainContainer 'Microsoft.Web/sites/sitecontainers@2025-03-01' = { + name: 'main' + properties: { + authType: 'UserAssigned' + image: web_containerimage + isMain: true + targetPort: web_containerport + userManagedIdentityClientId: env_outputs_azure_container_registry_managed_identity_client_id + } + parent: webapp +} + +resource webapp 'Microsoft.Web/sites@2025-03-01' = { + name: take('${toLower('web')}-${uniqueString(resourceGroup().id)}', 60) + location: location + properties: { + serverFarmId: env_outputs_planid + siteConfig: { + numberOfWorkers: 30 + linuxFxVersion: 'SITECONTAINERS' + acrUseManagedIdentityCreds: true + acrUserManagedIdentityID: env_outputs_azure_container_registry_managed_identity_client_id + appSettings: [ + { + name: 'WEBSITES_PORT' + value: web_containerport + } + { + name: 'OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY' + value: 'in_memory' + } + { + name: 'ASPNETCORE_FORWARDEDHEADERS_ENABLED' + value: 'true' + } + { + name: 'HTTP_PORTS' + value: web_containerport + } + { + name: 'AGENT_HTTP' + value: '${project_outputs_endpoint}/agents/agent' + } + { + name: 'services__agent__http__0' + value: '${project_outputs_endpoint}/agents/agent' + } + { + name: 'AGENT_URL' + value: '${project_outputs_endpoint}/agents/agent' + } + { + name: 'ASPIRE_ENVIRONMENT_NAME' + value: 'env' + } + { + name: 'OTEL_SERVICE_NAME' + value: 'web' + } + { + name: 'OTEL_EXPORTER_OTLP_PROTOCOL' + value: 'grpc' + } + { + name: 'OTEL_EXPORTER_OTLP_ENDPOINT' + value: 'http://localhost:6001' + } + { + name: 'WEBSITE_ENABLE_ASPIRE_OTEL_SIDECAR' + value: 'true' + } + { + name: 'OTEL_COLLECTOR_URL' + value: env_outputs_azure_app_service_dashboard_uri + } + { + name: 'OTEL_CLIENT_ID' + value: env_outputs_azure_container_registry_managed_identity_client_id + } + ] + } + } + identity: { + type: 'UserAssigned' + userAssignedIdentities: { + '${env_outputs_azure_container_registry_managed_identity_id}': { } + } + } +} + +resource web_website_ra 'Microsoft.Authorization/roleAssignments@2022-04-01' = { + name: guid(webapp.id, env_outputs_azure_website_contributor_managed_identity_id, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'de139f84-1756-47ae-9be6-808fbbe84772')) + properties: { + principalId: env_outputs_azure_website_contributor_managed_identity_principal_id + roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', 'de139f84-1756-47ae-9be6-808fbbe84772') + principalType: 'ServicePrincipal' + } + scope: webapp +} + +resource slotConfigNames 'Microsoft.Web/sites/config@2025-03-01' = { + name: 'slotConfigNames' + properties: { + appSettingNames: [ + 'AGENT_HTTP' + 'services__agent__http__0' + 'OTEL_SERVICE_NAME' + ] + } + parent: webapp +} \ No newline at end of file diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json new file mode 100644 index 00000000000..6273d48fed6 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json @@ -0,0 +1,16 @@ +{ + "type": "azure.bicep.v0", + "path": "web-website.module.bicep", + "params": { + "env_outputs_azure_container_registry_endpoint": "{env.outputs.AZURE_CONTAINER_REGISTRY_ENDPOINT}", + "env_outputs_planid": "{env.outputs.planId}", + "env_outputs_azure_container_registry_managed_identity_id": "{env.outputs.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID}", + "env_outputs_azure_container_registry_managed_identity_client_id": "{env.outputs.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_CLIENT_ID}", + "web_containerimage": "{web.containerImage}", + "web_containerport": "{web.containerPort}", + "project_outputs_endpoint": "{project.outputs.endpoint}", + "env_outputs_azure_app_service_dashboard_uri": "{env.outputs.AZURE_APP_SERVICE_DASHBOARD_URI}", + "env_outputs_azure_website_contributor_managed_identity_id": "{env.outputs.AZURE_WEBSITE_CONTRIBUTOR_MANAGED_IDENTITY_ID}", + "env_outputs_azure_website_contributor_managed_identity_principal_id": "{env.outputs.AZURE_WEBSITE_CONTRIBUTOR_MANAGED_IDENTITY_PRINCIPAL_ID}" + } +} \ No newline at end of file diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep new file mode 100644 index 00000000000..69901e73069 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -0,0 +1,86 @@ +@description('The location for the resource(s) to be deployed.') +param location string = resourceGroup().location + +param env_outputs_azure_container_apps_environment_default_domain string + +param env_outputs_azure_container_apps_environment_id string + +param env_outputs_azure_container_registry_endpoint string + +param env_outputs_azure_container_registry_managed_identity_id string + +param web_containerimage string + +param web_containerport string + +param project_outputs_endpoint string + +resource web 'Microsoft.App/containerApps@2025-10-02-preview' = { + name: 'web' + location: location + properties: { + configuration: { + activeRevisionsMode: 'Single' + ingress: { + external: true + targetPort: int(web_containerport) + transport: 'http' + } + registries: [ + { + server: env_outputs_azure_container_registry_endpoint + identity: env_outputs_azure_container_registry_managed_identity_id + } + ] + runtime: { + dotnet: { + autoConfigureDataProtection: true + } + } + } + environmentId: env_outputs_azure_container_apps_environment_id + template: { + containers: [ + { + image: web_containerimage + name: 'web' + env: [ + { + name: 'OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY' + value: 'in_memory' + } + { + name: 'ASPNETCORE_FORWARDEDHEADERS_ENABLED' + value: 'true' + } + { + name: 'HTTP_PORTS' + value: web_containerport + } + { + name: 'AGENT_HTTP' + value: '${project_outputs_endpoint}/agents/agent' + } + { + name: 'services__agent__http__0' + value: '${project_outputs_endpoint}/agents/agent' + } + { + name: 'AGENT_URL' + value: '${project_outputs_endpoint}/agents/agent' + } + ] + } + ] + scale: { + minReplicas: 1 + } + } + } + identity: { + type: 'UserAssigned' + userAssignedIdentities: { + '${env_outputs_azure_container_registry_managed_identity_id}': { } + } + } +} \ No newline at end of file diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json new file mode 100644 index 00000000000..15972c127e5 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json @@ -0,0 +1,13 @@ +{ + "type": "azure.bicep.v0", + "path": "web-containerapp.module.bicep", + "params": { + "env_outputs_azure_container_apps_environment_default_domain": "{env.outputs.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN}", + "env_outputs_azure_container_apps_environment_id": "{env.outputs.AZURE_CONTAINER_APPS_ENVIRONMENT_ID}", + "env_outputs_azure_container_registry_endpoint": "{env.outputs.AZURE_CONTAINER_REGISTRY_ENDPOINT}", + "env_outputs_azure_container_registry_managed_identity_id": "{env.outputs.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID}", + "web_containerimage": "{web.containerImage}", + "web_containerport": "{web.containerPort}", + "project_outputs_endpoint": "{project.outputs.endpoint}" + } +} \ No newline at end of file From f3e999a34f81ed6ec8f9a9721aa89e0bb88224d5 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 08:51:39 -0700 Subject: [PATCH 2/8] Add branch tests for ComputeEnvironmentEndpointResolver and correct misleading comment Add direct unit tests covering each branch of TryGetCrossEnvironmentEndpointExpression: cross-environment delegation, same-environment deployment target, WithComputeEnvironment binding backstop, no-compute-environment, bound multi-target (no throw), and unbound multi-target (throws). Correct the comment on the fast-path loop which incorrectly claimed it never throws on multi-target resources. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../ComputeEnvironmentEndpointResolver.cs | 20 ++- .../Aspire.Hosting.Tests.csproj | 1 + ...ComputeEnvironmentEndpointResolverTests.cs | 149 ++++++++++++++++++ 3 files changed, 163 insertions(+), 7 deletions(-) create mode 100644 tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs diff --git a/src/Shared/ComputeEnvironmentEndpointResolver.cs b/src/Shared/ComputeEnvironmentEndpointResolver.cs index 7470bac7db3..cbe33917a74 100644 --- a/src/Shared/ComputeEnvironmentEndpointResolver.cs +++ b/src/Shared/ComputeEnvironmentEndpointResolver.cs @@ -51,13 +51,19 @@ public static bool TryGetCrossEnvironmentEndpointExpression( var owningResource = endpointReferenceExpression.Endpoint.Resource; - // If the endpoint's owning resource deploys to one of the current publisher's compute - // environments, the endpoint lives in the local endpoint map. Leave resolution to the - // existing local lookup so generated artifacts (bicep parameters, helm values, etc.) - // are unchanged. GetDeploymentTargetAnnotation(env) accounts for ComputeEnvironmentAnnotation - // binding and returns null when the resource does not deploy to that environment. It is also - // binding-aware and never throws on multi-target resources, so checking it before resolving - // the owning environment preserves existing behavior. + // Fast path: if the endpoint's owning resource deploys to one of the current publisher's + // compute environments, the endpoint lives in the local endpoint map. Leave resolution to + // the existing local lookup so generated artifacts (bicep parameters, helm values, etc.) are + // unchanged. GetDeploymentTargetAnnotation(env) accounts for ComputeEnvironmentAnnotation + // binding and returns null when the resource does not deploy to that environment. + // + // For well-formed inputs this loop is redundant with the TryGetEffectiveComputeEnvironment + + // ReferenceEquals backstop below (a resource resolves to the current environment in both + // places). It is NOT a guard against the multi-target throw: for an unbound resource with + // more than one deployment target, GetDeploymentTargetAnnotation(current) throws exactly like + // the parameterless overload TryGetEffectiveComputeEnvironment uses. That malformed + // configuration is rejected earlier in the pipeline, so it is not reachable here. The loop is + // kept as a harmless explicit fast-path; it can be removed if the backstop is preferred. foreach (var current in currentComputeEnvironments) { if (current is not null && owningResource.GetDeploymentTargetAnnotation(current) is not null) diff --git a/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj b/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj index e8e803885f9..fbf3f7ff342 100644 --- a/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj +++ b/tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj @@ -48,6 +48,7 @@ + diff --git a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs new file mode 100644 index 00000000000..7af5aa9c645 --- /dev/null +++ b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs @@ -0,0 +1,149 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Net.Sockets; +using Aspire.Hosting.Utils; +using Microsoft.AspNetCore.InternalTesting; + +namespace Aspire.Hosting.Tests; + +public class ComputeEnvironmentEndpointResolverTests +{ + [Fact] + public async Task OwningResourceInDifferentEnvironment_DelegatesToOwningEnvironment() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + var owningEnv = builder.AddResource(new TestComputeEnvironmentResource("owning")); + var agent = builder.AddResource(new TestComputeResource("agent")); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(owningEnv.Resource) { ComputeEnvironment = owningEnv.Resource }); + + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + + Assert.True(resolved); + Assert.NotNull(expression); + // The owning environment (TestComputeEnvironmentResource) maps the host to "{name}.example.com". + Assert.Equal("http://agent.example.com:8080", await expression.GetValueAsync(default).DefaultTimeout()); + } + + [Fact] + public void OwningResourceDeploysToCurrentEnvironment_ReturnsFalse() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + var agent = builder.AddResource(new TestComputeResource("agent")); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(currentEnv.Resource) { ComputeEnvironment = currentEnv.Resource }); + + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + + Assert.False(resolved); + Assert.Null(expression); + } + + [Fact] + public void OwningResourceBoundToCurrentEnvironmentWithoutDeploymentTarget_ReturnsFalseViaBackstop() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + // Bind via WithComputeEnvironment only (no DeploymentTargetAnnotation). The fast-path loop + // falls through and the ReferenceEquals backstop catches it after the effective environment + // resolves to the current environment. + var agent = builder.AddResource(new TestComputeResource("agent")) + .WithComputeEnvironment(currentEnv); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + + Assert.False(resolved); + Assert.Null(expression); + } + + [Fact] + public void OwningResourceHasNoComputeEnvironment_ReturnsFalse() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + // No binding and no deployment target: nothing to delegate to. + var agent = builder.AddResource(new TestComputeResource("agent")); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + + Assert.False(resolved); + Assert.Null(expression); + } + + [Fact] + public void OwningResourceBoundToCurrentEnvironmentWithMultipleDeploymentTargets_ReturnsFalseAndDoesNotThrow() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + var otherEnv = builder.AddResource(new TestComputeEnvironmentResource("other")); + // Explicit binding to the current environment plus deployment targets for both environments. + // The binding makes GetDeploymentTargetAnnotation(current) select the current target without + // throwing on the multi-target ambiguity. + var agent = builder.AddResource(new TestComputeResource("agent")) + .WithComputeEnvironment(currentEnv); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(currentEnv.Resource) { ComputeEnvironment = currentEnv.Resource }); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(otherEnv.Resource) { ComputeEnvironment = otherEnv.Resource }); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + + Assert.False(resolved); + Assert.Null(expression); + } + + [Fact] + public void OwningResourceUnboundWithMultipleDeploymentTargets_Throws() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + var otherEnv = builder.AddResource(new TestComputeEnvironmentResource("other")); + // No binding: an unbound resource with more than one deployment target is ambiguous. + // GetDeploymentTargetAnnotation(current) throws, exactly like the parameterless overload used + // by TryGetEffectiveComputeEnvironment. This documents that the fast-path loop is NOT a guard + // against this throw; the pipeline rejects this configuration earlier in practice. + var agent = builder.AddResource(new TestComputeResource("agent")); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(currentEnv.Resource) { ComputeEnvironment = currentEnv.Resource }); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(otherEnv.Resource) { ComputeEnvironment = otherEnv.Resource }); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + + Assert.Throws(() => + ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), out _, currentEnv.Resource)); + } + + private static EndpointReference AddHttpEndpoint(TestComputeResource resource, int? port, int? targetPort) + { + var endpoint = new EndpointAnnotation(ProtocolType.Tcp, uriScheme: "http", name: "http", port: port, targetPort: targetPort); + resource.Annotations.Add(endpoint); + + return new EndpointReference(resource, endpoint); + } + + private sealed class TestComputeEnvironmentResource(string name) : Resource(name), IComputeEnvironmentResource + { +#pragma warning disable ASPIRECOMPUTE002 + public ReferenceExpression GetHostAddressExpression(EndpointReference endpointReference) => + ReferenceExpression.Create($"{endpointReference.Resource.Name}.example.com"); +#pragma warning restore ASPIRECOMPUTE002 + } + + private sealed class TestComputeResource(string name) : Resource(name), IComputeResource, IResourceWithEndpoints + { + } +} From 4fa62ec19af2a8e0289bb7f447d5dc4d8e3e5aa8 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 08:55:27 -0700 Subject: [PATCH 3/8] Remove redundant fast-path loop from ComputeEnvironmentEndpointResolver The first foreach loop using GetDeploymentTargetAnnotation(current) was redundant with TryGetEffectiveComputeEnvironment + the ReferenceEquals backstop for all well-formed inputs, and did not provide multi-target throw-safety. Remove it and keep the simpler resolve-then- compare flow. All six branch tests continue to pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../ComputeEnvironmentEndpointResolver.cs | 31 ++++--------------- ...ComputeEnvironmentEndpointResolverTests.cs | 15 +++++---- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/Shared/ComputeEnvironmentEndpointResolver.cs b/src/Shared/ComputeEnvironmentEndpointResolver.cs index cbe33917a74..c1132e6c784 100644 --- a/src/Shared/ComputeEnvironmentEndpointResolver.cs +++ b/src/Shared/ComputeEnvironmentEndpointResolver.cs @@ -51,36 +51,17 @@ public static bool TryGetCrossEnvironmentEndpointExpression( var owningResource = endpointReferenceExpression.Endpoint.Resource; - // Fast path: if the endpoint's owning resource deploys to one of the current publisher's - // compute environments, the endpoint lives in the local endpoint map. Leave resolution to - // the existing local lookup so generated artifacts (bicep parameters, helm values, etc.) are - // unchanged. GetDeploymentTargetAnnotation(env) accounts for ComputeEnvironmentAnnotation - // binding and returns null when the resource does not deploy to that environment. - // - // For well-formed inputs this loop is redundant with the TryGetEffectiveComputeEnvironment + - // ReferenceEquals backstop below (a resource resolves to the current environment in both - // places). It is NOT a guard against the multi-target throw: for an unbound resource with - // more than one deployment target, GetDeploymentTargetAnnotation(current) throws exactly like - // the parameterless overload TryGetEffectiveComputeEnvironment uses. That malformed - // configuration is rejected earlier in the pipeline, so it is not reachable here. The loop is - // kept as a harmless explicit fast-path; it can be removed if the backstop is preferred. - foreach (var current in currentComputeEnvironments) - { - if (current is not null && owningResource.GetDeploymentTargetAnnotation(current) is not null) - { - return false; - } - } - - // The owning resource has no compute environment (for example a plain resource that is not - // deployed anywhere). There is nothing to delegate to, so let the local lookup handle it. + // Resolve the compute environment the owning resource deploys to. A plain resource that is + // not deployed anywhere has none, so there is nothing to delegate to and the local lookup + // handles it. if (!TryGetEffectiveComputeEnvironment(owningResource, out var owningComputeEnvironment)) { return false; } - // Defensive: if the resolved owning environment is itself one of the current publisher's - // environments, prefer the local map. + // If the owning resource deploys to one of the current publisher's compute environments, the + // endpoint lives in the local endpoint map. Leave resolution to the existing local lookup so + // generated artifacts (bicep parameters, helm values, etc.) are unchanged. foreach (var current in currentComputeEnvironments) { if (ReferenceEquals(current, owningComputeEnvironment)) diff --git a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs index 7af5aa9c645..1ed9e227c08 100644 --- a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs +++ b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs @@ -52,9 +52,8 @@ public void OwningResourceBoundToCurrentEnvironmentWithoutDeploymentTarget_Retur using var builder = TestDistributedApplicationBuilder.Create(); var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); - // Bind via WithComputeEnvironment only (no DeploymentTargetAnnotation). The fast-path loop - // falls through and the ReferenceEquals backstop catches it after the effective environment - // resolves to the current environment. + // Bind via WithComputeEnvironment only (no DeploymentTargetAnnotation). The effective + // environment resolves to the current environment and the ReferenceEquals backstop catches it. var agent = builder.AddResource(new TestComputeResource("agent")) .WithComputeEnvironment(currentEnv); var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); @@ -91,8 +90,8 @@ public void OwningResourceBoundToCurrentEnvironmentWithMultipleDeploymentTargets var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); var otherEnv = builder.AddResource(new TestComputeEnvironmentResource("other")); // Explicit binding to the current environment plus deployment targets for both environments. - // The binding makes GetDeploymentTargetAnnotation(current) select the current target without - // throwing on the multi-target ambiguity. + // The binding lets GetComputeEnvironment() short-circuit to the current environment without + // hitting the parameterless deployment-target resolver that throws on multi-target ambiguity. var agent = builder.AddResource(new TestComputeResource("agent")) .WithComputeEnvironment(currentEnv); agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(currentEnv.Resource) { ComputeEnvironment = currentEnv.Resource }); @@ -114,9 +113,9 @@ public void OwningResourceUnboundWithMultipleDeploymentTargets_Throws() var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); var otherEnv = builder.AddResource(new TestComputeEnvironmentResource("other")); // No binding: an unbound resource with more than one deployment target is ambiguous. - // GetDeploymentTargetAnnotation(current) throws, exactly like the parameterless overload used - // by TryGetEffectiveComputeEnvironment. This documents that the fast-path loop is NOT a guard - // against this throw; the pipeline rejects this configuration earlier in practice. + // TryGetEffectiveComputeEnvironment falls back to the parameterless GetDeploymentTargetAnnotation() + // which throws. This documents that the resolver does not swallow that ambiguity; the pipeline + // rejects this configuration earlier in practice. var agent = builder.AddResource(new TestComputeResource("agent")); agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(currentEnv.Resource) { ComputeEnvironment = currentEnv.Resource }); agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(otherEnv.Resource) { ComputeEnvironment = otherEnv.Resource }); From 9c9e8142a7bcd759ce1384b0c39f4e47cb0b6f41 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 08:59:33 -0700 Subject: [PATCH 4/8] Drop params and move out parameter last in cross-env endpoint resolver Replace the params IComputeEnvironmentResource?[] with an explicit IReadOnlyList parameter placed before the out, so the out parameter comes last per convention. Kubernetes still passes two current environments (its environment plus OwningComputeEnvironment) via a collection expression; ACA and AppService pass a single-element list. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../BaseContainerAppContext.cs | 4 ++-- .../AzureAppServiceWebsiteContext.cs | 4 ++-- src/Aspire.Hosting.Kubernetes/KubernetesResource.cs | 4 ++-- src/Shared/ComputeEnvironmentEndpointResolver.cs | 11 ++++++----- .../ComputeEnvironmentEndpointResolverTests.cs | 12 ++++++------ 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs b/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs index 1b1a13d3f4f..d8e57fbced9 100644 --- a/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs +++ b/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs @@ -226,7 +226,7 @@ BicepValue GetHostValue(string? prefix = null, string? suffix = null) // environment (for example a Foundry hosted agent). In that case delegate to the owning // compute environment instead of looking it up in this environment's local endpoint map. if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - ep.Property(EndpointProperty.Url), out var crossExpr, _containerAppEnvironmentContext.Environment)) + ep.Property(EndpointProperty.Url), [_containerAppEnvironmentContext.Environment], out var crossExpr)) { return ProcessValue(crossExpr, secretType, parent); } @@ -284,7 +284,7 @@ BicepValue GetHostValue(string? prefix = null, string? suffix = null) if (value is EndpointReferenceExpression epExpr) { if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - epExpr, out var crossExpr, _containerAppEnvironmentContext.Environment)) + epExpr, [_containerAppEnvironmentContext.Environment], out var crossExpr)) { return ProcessValue(crossExpr, secretType, parent); } diff --git a/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs b/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs index e502f24250e..daa2c327ce0 100644 --- a/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs +++ b/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs @@ -171,7 +171,7 @@ private void ProcessEndpoints() // environment (for example a Foundry hosted agent). In that case delegate to the owning // compute environment instead of looking it up in this environment's local endpoint map. if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - ep.Property(EndpointProperty.Url), out var crossExpr, environmentContext.Environment)) + ep.Property(EndpointProperty.Url), [environmentContext.Environment], out var crossExpr)) { return ProcessValue(crossExpr, secretType, parent, isSlot); } @@ -216,7 +216,7 @@ private void ProcessEndpoints() if (value is EndpointReferenceExpression epExpr) { if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - epExpr, out var crossExpr, environmentContext.Environment)) + epExpr, [environmentContext.Environment], out var crossExpr)) { return ProcessValue(crossExpr, secretType, parent, isSlot); } diff --git a/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs b/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs index a14f9c63d1b..e5ea907c528 100644 --- a/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs +++ b/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs @@ -490,7 +490,7 @@ private async Task ProcessValueAsync(KubernetesEnvironmentContext contex // environment (for example a Foundry hosted agent). In that case delegate to the owning // compute environment instead of looking it up in this environment's local endpoint map. if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - ep.Property(EndpointProperty.Url), out var crossExpr, kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment)) + ep.Property(EndpointProperty.Url), [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr)) { value = crossExpr; continue; @@ -527,7 +527,7 @@ private async Task ProcessValueAsync(KubernetesEnvironmentContext contex if (value is EndpointReferenceExpression epExpr) { if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - epExpr, out var crossExpr, kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment)) + epExpr, [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr)) { value = crossExpr; continue; diff --git a/src/Shared/ComputeEnvironmentEndpointResolver.cs b/src/Shared/ComputeEnvironmentEndpointResolver.cs index c1132e6c784..77f0e39bb6e 100644 --- a/src/Shared/ComputeEnvironmentEndpointResolver.cs +++ b/src/Shared/ComputeEnvironmentEndpointResolver.cs @@ -28,24 +28,25 @@ internal static class ComputeEnvironmentEndpointResolver /// environment is different from the publisher's current compute environment(s). /// /// The endpoint reference expression to resolve. - /// - /// When this method returns , contains the delegated reference expression. - /// /// /// The compute environment(s) the current publisher is generating artifacts for. When the /// endpoint's owning resource deploys to one of these, resolution is left to the local /// endpoint map and this method returns . /// + /// + /// When this method returns , contains the delegated reference expression. + /// /// /// if the endpoint is owned by a different compute environment and a /// delegated expression was produced; otherwise . /// public static bool TryGetCrossEnvironmentEndpointExpression( EndpointReferenceExpression endpointReferenceExpression, - [NotNullWhen(true)] out ReferenceExpression? expression, - params IComputeEnvironmentResource?[] currentComputeEnvironments) + IReadOnlyList currentComputeEnvironments, + [NotNullWhen(true)] out ReferenceExpression? expression) { ArgumentNullException.ThrowIfNull(endpointReferenceExpression); + ArgumentNullException.ThrowIfNull(currentComputeEnvironments); expression = null; diff --git a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs index 1ed9e227c08..e56f6a25c3c 100644 --- a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs +++ b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs @@ -21,7 +21,7 @@ public async Task OwningResourceInDifferentEnvironment_DelegatesToOwningEnvironm agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(owningEnv.Resource) { ComputeEnvironment = owningEnv.Resource }); var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out var expression); Assert.True(resolved); Assert.NotNull(expression); @@ -40,7 +40,7 @@ public void OwningResourceDeploysToCurrentEnvironment_ReturnsFalse() agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(currentEnv.Resource) { ComputeEnvironment = currentEnv.Resource }); var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out var expression); Assert.False(resolved); Assert.Null(expression); @@ -59,7 +59,7 @@ public void OwningResourceBoundToCurrentEnvironmentWithoutDeploymentTarget_Retur var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out var expression); Assert.False(resolved); Assert.Null(expression); @@ -76,7 +76,7 @@ public void OwningResourceHasNoComputeEnvironment_ReturnsFalse() var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out var expression); Assert.False(resolved); Assert.Null(expression); @@ -99,7 +99,7 @@ public void OwningResourceBoundToCurrentEnvironmentWithMultipleDeploymentTargets var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - endpoint.Property(EndpointProperty.Url), out var expression, currentEnv.Resource); + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out var expression); Assert.False(resolved); Assert.Null(expression); @@ -123,7 +123,7 @@ public void OwningResourceUnboundWithMultipleDeploymentTargets_Throws() Assert.Throws(() => ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - endpoint.Property(EndpointProperty.Url), out _, currentEnv.Resource)); + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out _)); } private static EndpointReference AddHttpEndpoint(TestComputeResource resource, int? port, int? targetPort) From be10e706df3f97ac220eb87c9903b8b74781f0b6 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 09:05:50 -0700 Subject: [PATCH 5/8] Add EndpointReference overload for cross-env endpoint resolver Add an overload taking EndpointReference that uses ep.Property(EndpointProperty.Url) internally, so the three EndpointReference call sites (ACA, AppService, Kubernetes) pass the endpoint directly instead of repeating the .Property(Url) projection. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../BaseContainerAppContext.cs | 2 +- .../AzureAppServiceWebsiteContext.cs | 2 +- .../KubernetesResource.cs | 2 +- .../ComputeEnvironmentEndpointResolver.cs | 31 +++++++++++++++++++ ...ComputeEnvironmentEndpointResolverTests.cs | 20 ++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs b/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs index d8e57fbced9..e252d0b7705 100644 --- a/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs +++ b/src/Aspire.Hosting.Azure.AppContainers/BaseContainerAppContext.cs @@ -226,7 +226,7 @@ BicepValue GetHostValue(string? prefix = null, string? suffix = null) // environment (for example a Foundry hosted agent). In that case delegate to the owning // compute environment instead of looking it up in this environment's local endpoint map. if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - ep.Property(EndpointProperty.Url), [_containerAppEnvironmentContext.Environment], out var crossExpr)) + ep, [_containerAppEnvironmentContext.Environment], out var crossExpr)) { return ProcessValue(crossExpr, secretType, parent); } diff --git a/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs b/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs index daa2c327ce0..b5d3c774560 100644 --- a/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs +++ b/src/Aspire.Hosting.Azure.AppService/AzureAppServiceWebsiteContext.cs @@ -171,7 +171,7 @@ private void ProcessEndpoints() // environment (for example a Foundry hosted agent). In that case delegate to the owning // compute environment instead of looking it up in this environment's local endpoint map. if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - ep.Property(EndpointProperty.Url), [environmentContext.Environment], out var crossExpr)) + ep, [environmentContext.Environment], out var crossExpr)) { return ProcessValue(crossExpr, secretType, parent, isSlot); } diff --git a/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs b/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs index e5ea907c528..6f75bf8acfb 100644 --- a/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs +++ b/src/Aspire.Hosting.Kubernetes/KubernetesResource.cs @@ -490,7 +490,7 @@ private async Task ProcessValueAsync(KubernetesEnvironmentContext contex // environment (for example a Foundry hosted agent). In that case delegate to the owning // compute environment instead of looking it up in this environment's local endpoint map. if (ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( - ep.Property(EndpointProperty.Url), [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr)) + ep, [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr)) { value = crossExpr; continue; diff --git a/src/Shared/ComputeEnvironmentEndpointResolver.cs b/src/Shared/ComputeEnvironmentEndpointResolver.cs index 77f0e39bb6e..23dfdcf47ce 100644 --- a/src/Shared/ComputeEnvironmentEndpointResolver.cs +++ b/src/Shared/ComputeEnvironmentEndpointResolver.cs @@ -22,6 +22,37 @@ namespace Aspire.Hosting; /// internal static class ComputeEnvironmentEndpointResolver { + /// + /// Attempts to produce a for an endpoint's URL by + /// delegating to the compute environment that owns the endpoint's resource, when that + /// environment is different from the publisher's current compute environment(s). + /// + /// The endpoint reference to resolve. + /// + /// The compute environment(s) the current publisher is generating artifacts for. When the + /// endpoint's owning resource deploys to one of these, resolution is left to the local + /// endpoint map and this method returns . + /// + /// + /// When this method returns , contains the delegated reference expression. + /// + /// + /// if the endpoint is owned by a different compute environment and a + /// delegated expression was produced; otherwise . + /// + public static bool TryGetCrossEnvironmentEndpointExpression( + EndpointReference endpointReference, + IReadOnlyList currentComputeEnvironments, + [NotNullWhen(true)] out ReferenceExpression? expression) + { + ArgumentNullException.ThrowIfNull(endpointReference); + + return TryGetCrossEnvironmentEndpointExpression( + endpointReference.Property(EndpointProperty.Url), + currentComputeEnvironments, + out expression); + } + /// /// Attempts to produce a for an endpoint property by /// delegating to the compute environment that owns the endpoint's resource, when that diff --git a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs index e56f6a25c3c..830a02ff526 100644 --- a/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs +++ b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs @@ -29,6 +29,26 @@ public async Task OwningResourceInDifferentEnvironment_DelegatesToOwningEnvironm Assert.Equal("http://agent.example.com:8080", await expression.GetValueAsync(default).DefaultTimeout()); } + [Fact] + public async Task EndpointReferenceOverload_DelegatesToOwningEnvironment() + { + using var builder = TestDistributedApplicationBuilder.Create(); + + var currentEnv = builder.AddResource(new TestComputeEnvironmentResource("current")); + var owningEnv = builder.AddResource(new TestComputeEnvironmentResource("owning")); + var agent = builder.AddResource(new TestComputeResource("agent")); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + agent.Resource.Annotations.Add(new DeploymentTargetAnnotation(owningEnv.Resource) { ComputeEnvironment = owningEnv.Resource }); + + // The EndpointReference overload uses EndpointProperty.Url internally. + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint, [currentEnv.Resource], out var expression); + + Assert.True(resolved); + Assert.NotNull(expression); + Assert.Equal("http://agent.example.com:8080", await expression.GetValueAsync(default).DefaultTimeout()); + } + [Fact] public void OwningResourceDeploysToCurrentEnvironment_ReturnsFalse() { From 11227a80401109a1830fc5b058cbc2e7871b6ae1 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 11:33:06 -0700 Subject: [PATCH 6/8] Use hosted-agent deployment name in cross-env Foundry agent URL Hosted-agent deployment creates the Foundry agent version using the wrapper AzureHostedAgentResource.Name (e.g. "agent-ha" for a target named "agent"). The published cross-environment endpoint path was built from the bare resource name, producing /agents/agent which does not match the deployed /agents/agent-ha. Resolve the hosted-agent deployment target and use its name when present, falling back to the resource name for non-hosted agents. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Project/ProjectResource.cs | 12 +++++++++++- .../AzureKubernetesFoundryReferenceTests.cs | 12 ++++++------ ...sResolvedAcrossComputeEnvironments.verified.bicep | 6 +++--- ...sResolvedAcrossComputeEnvironments.verified.bicep | 6 +++--- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs b/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs index b9dfd32b655..ac90f16f99e 100644 --- a/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs +++ b/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs @@ -243,7 +243,17 @@ ReferenceExpression IComputeEnvironmentResource.GetEndpointPropertyExpression(En private ReferenceExpression GetAgentAddressExpression(EndpointReference endpointReference) { var resource = endpointReference.Resource; - return ReferenceExpression.Create($"{Endpoint}/agents/{resource.Name}"); + + // For hosted agents, deployment creates the Foundry agent version using the wrapper + // AzureHostedAgentResource.Name (e.g. "agent-ha" for a target named "agent"), not the + // target resource name. The published cross-environment URL must point at that deployed + // agent name, so prefer the hosted-agent deployment target's name when one exists and fall + // back to the resource name for plain (non-hosted) agents. + var agentName = resource.GetDeploymentTargetAnnotation(this)?.DeploymentTarget is AzureHostedAgentResource hostedAgent + ? hostedAgent.Name + : resource.Name; + + return ReferenceExpression.Create($"{Endpoint}/agents/{agentName}"); } /// diff --git a/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs b/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs index f84daf8f1e8..112e54fb25e 100644 --- a/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs +++ b/tests/Aspire.Hosting.Azure.Kubernetes.Tests/AzureKubernetesFoundryReferenceTests.cs @@ -52,15 +52,15 @@ public async Task EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEn await app.RunAsync(); // The resolved environment variable values are emitted into the Helm chart's values.yaml. - // The agent endpoint must resolve to the Foundry project endpoint composed with the agent - // path. Prior to the fix, publishing threw because the agent endpoint was not present in the - // Kubernetes environment's local endpoint map. + // The agent endpoint must resolve to the Foundry project endpoint composed with the deployed + // hosted agent path because hosted-agent deployment creates the Foundry agent version with the + // wrapper resource name. var valuesPath = Directory.EnumerateFiles(tempDir.Path, "values.yaml", SearchOption.AllDirectories).Single(); var values = await File.ReadAllTextAsync(valuesPath); - Assert.Contains("AGENT_HTTP: \"{project.outputs.endpoint}/agents/agent\"", values); - Assert.Contains("AGENT_URL: \"{project.outputs.endpoint}/agents/agent\"", values); - Assert.Contains("services__agent__http__0: \"{project.outputs.endpoint}/agents/agent\"", values); + Assert.Contains("AGENT_HTTP: \"{project.outputs.endpoint}/agents/agent-ha\"", values); + Assert.Contains("AGENT_URL: \"{project.outputs.endpoint}/agents/agent-ha\"", values); + Assert.Contains("services__agent__http__0: \"{project.outputs.endpoint}/agents/agent-ha\"", values); } private sealed class Project : IProjectMetadata diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep index b86e161b40b..906f511b10a 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -62,15 +62,15 @@ resource webapp 'Microsoft.Web/sites@2025-03-01' = { } { name: 'AGENT_HTTP' - value: '${project_outputs_endpoint}/agents/agent' + value: '${project_outputs_endpoint}/agents/agent-ha' } { name: 'services__agent__http__0' - value: '${project_outputs_endpoint}/agents/agent' + value: '${project_outputs_endpoint}/agents/agent-ha' } { name: 'AGENT_URL' - value: '${project_outputs_endpoint}/agents/agent' + value: '${project_outputs_endpoint}/agents/agent-ha' } { name: 'ASPIRE_ENVIRONMENT_NAME' diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep index 69901e73069..2b9d0e6dba7 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -59,15 +59,15 @@ resource web 'Microsoft.App/containerApps@2025-10-02-preview' = { } { name: 'AGENT_HTTP' - value: '${project_outputs_endpoint}/agents/agent' + value: '${project_outputs_endpoint}/agents/agent-ha' } { name: 'services__agent__http__0' - value: '${project_outputs_endpoint}/agents/agent' + value: '${project_outputs_endpoint}/agents/agent-ha' } { name: 'AGENT_URL' - value: '${project_outputs_endpoint}/agents/agent' + value: '${project_outputs_endpoint}/agents/agent-ha' } ] } From 1d6c1b5f8100a512cbf6801be7ba485bf519a3b5 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 17:01:00 -0700 Subject: [PATCH 7/8] Auto-wire Azure AI User role for hosted-agent consumers When a compute resource references a Foundry hosted agent's node app via WithReference, automatically grant the consumer the Azure AI User role on the owning Foundry account and provision a managed identity, removing the two manual post-deploy `az role assignment create` steps. Introduces a public, experimental ReferenceRoleAssignmentAnnotation in Aspire.Hosting.Azure. A resource that "fronts" an Azure resource (without being an IAzureResource itself) carries this annotation; AzureResourcePreparer folds its (Target, Roles) into the same role-assignment path used for direct Azure references. Foundry's AsHostedAgent stamps the annotation on the agent's node app granting only the least-privilege Azure AI User role; account defaults and explicit WithRoleAssignments suppression remain owned by the preparer and are not reintroduced. GetAllRoleAssignments now dedupes roles per target to avoid colliding bicep role-assignment identifiers. Adds preparer end-to-end tests (suppression preserved, defaults preserved, dedup) and Foundry stamp tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../AzureResourcePreparer.cs | 59 ++++-- .../ReferenceRoleAssignmentAnnotation.cs | 43 ++++ .../HostedAgent/AzureHostedAgentResource.cs | 5 +- .../HostedAgentBuilderExtension.cs | 31 +++ .../AzureResourcePreparerTests.cs | 198 ++++++++++++++++++ .../HostedAgentExtensionTests.cs | 83 ++++++++ 6 files changed, 403 insertions(+), 16 deletions(-) create mode 100644 src/Aspire.Hosting.Azure/ReferenceRoleAssignmentAnnotation.cs diff --git a/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs b/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs index ac76fc957dd..02448c02bb5 100644 --- a/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs +++ b/src/Aspire.Hosting.Azure/AzureResourcePreparer.cs @@ -133,7 +133,8 @@ private async Task BuildRoleAssignmentAnnotations(DistributedApplicationModel ap foreach (var resource in resourceSnapshot) { var prerequisiteResources = new HashSet(); - var azureReferences = await GetAzureReferences(resource, cancellationToken).ConfigureAwait(false); + var directDependencies = await resource.GetResourceDependenciesAsync(executionContext, ResourceDependencyDiscoveryMode.DirectOnly, cancellationToken).ConfigureAwait(false); + var azureReferences = new HashSet(directDependencies.OfType()); var azureReferencesWithRoleAssignments = (resource.TryGetAnnotationsOfType(out var annotations) @@ -184,6 +185,42 @@ private async Task BuildRoleAssignmentAnnotations(DistributedApplicationModel ap } } + // A direct dependency that is not itself an Azure resource can still "front" one + // (e.g. a Foundry hosted agent's node app fronts its owning Foundry account). Such a + // resource carries ReferenceRoleAssignmentAnnotation(s) declaring that any resource + // referencing it should be granted roles on a transitive Azure target the normal + // IAzureResource-only reference walk above cannot reach. Fold those implied targets + // into the same role-assignment path so the consumer gets an identity + role bicep + // exactly as it would for a direct Azure reference. + foreach (var dependency in directDependencies) + { + if (!dependency.TryGetAnnotationsOfType(out var impliedRoleAssignments)) + { + continue; + } + + foreach (var impliedRoleAssignment in impliedRoleAssignments) + { + var target = impliedRoleAssignment.Target; + if (target.IsContainer() || target.IsEmulator()) + { + continue; + } + + if (executionContext.IsRunMode) + { + AppendGlobalRoleAssignments(globalRoleAssignments, target, impliedRoleAssignment.Roles); + } + else + { + // In PublishMode, materialize as an explicit RoleAssignmentAnnotation so + // GetAllRoleAssignments (which groups by target and unions roles) picks it + // up alongside any roles the consumer already declares for the same target. + resource.Annotations.Add(new RoleAssignmentAnnotation(target, impliedRoleAssignment.Roles)); + } + } + } + // in PublishMode with SupportsTargetedRoleAssignments, we need to create the identity and role assignment resources // if the resource references any Azure resources, or has role assignments to Azure resources if (executionContext.IsPublishMode) @@ -250,7 +287,12 @@ private static Dictionary { foreach (var g in roleAssignments.GroupBy(r => r.Target)) { - result[g.Key] = g.SelectMany(r => r.Roles); + // Deduplicate roles per target. A target can accumulate multiple RoleAssignmentAnnotations + // (e.g. an implied ReferenceRoleAssignmentAnnotation from two hosted agents on the same + // Foundry account, plus a direct reference). Emitting the same RoleDefinition twice would + // produce two RoleAssignment bicep resources with the same identifier ("{prefix}_{roleName}") + // and fail bicep compilation. This mirrors the RunMode path, which unions into a HashSet. + result[g.Key] = g.SelectMany(r => r.Roles).Distinct(); } } return result; @@ -359,19 +401,6 @@ private sealed class AddRoleAssignmentsContext( public DistributedApplicationExecutionContext ExecutionContext => executionContext; } - private async Task> GetAzureReferences(IResource resource, CancellationToken cancellationToken) - { - var dependencies = await resource.GetResourceDependenciesAsync(executionContext, ResourceDependencyDiscoveryMode.DirectOnly, cancellationToken).ConfigureAwait(false); - - HashSet azureReferences = []; - foreach (var azureResource in dependencies.OfType()) - { - azureReferences.Add(azureResource); - } - - return azureReferences; - } - private static void AppendGlobalRoleAssignments(Dictionary> globalRoleAssignments, AzureProvisioningResource azureResource, IEnumerable newRoles) { if (!globalRoleAssignments.TryGetValue(azureResource, out var existingRoles)) diff --git a/src/Aspire.Hosting.Azure/ReferenceRoleAssignmentAnnotation.cs b/src/Aspire.Hosting.Azure/ReferenceRoleAssignmentAnnotation.cs new file mode 100644 index 00000000000..58720eaad6e --- /dev/null +++ b/src/Aspire.Hosting.Azure/ReferenceRoleAssignmentAnnotation.cs @@ -0,0 +1,43 @@ +// 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 Aspire.Hosting.ApplicationModel; + +namespace Aspire.Hosting.Azure; + +/// +/// Declares that any compute resource referencing the annotated resource should be granted +/// on the Azure resource . +/// +/// The Azure resource that referencing resources should be granted roles on. +/// The roles that referencing resources should be assigned on . +/// +/// +/// This annotation is applied to a resource that "fronts" an Azure resource without being an +/// itself. For example, a Foundry hosted agent's node app is a plain +/// compute resource, but invoking the agent requires the caller to hold a role on the owning +/// Foundry account. The account is only a transitive dependency of a consumer, so +/// 's normal reference walk — which only acts on direct +/// dependencies — cannot reach it. +/// +/// +/// When a compute resource takes a direct dependency on a resource carrying this annotation, +/// folds (Target, Roles) into the same role-assignment +/// path used for direct Azure references, so the consumer gets a managed identity and the +/// corresponding role assignment on with no additional wiring. +/// +/// +[Experimental("ASPIREAZURE003", UrlFormat = "https://aka.ms/aspire/diagnostics/{0}")] +public sealed class ReferenceRoleAssignmentAnnotation(AzureProvisioningResource target, IReadOnlySet roles) : IResourceAnnotation +{ + /// + /// Gets the Azure resource that resources referencing the annotated resource should be granted roles on. + /// + public AzureProvisioningResource Target { get; } = target; + + /// + /// Gets the set of roles that resources referencing the annotated resource should be assigned on . + /// + public IReadOnlySet Roles { get; } = roles; +} diff --git a/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs b/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs index 5c28ebb36c5..a73b8de2ddb 100644 --- a/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs +++ b/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs @@ -25,7 +25,10 @@ namespace Aspire.Hosting.Foundry; /// public class AzureHostedAgentResource : Resource, IResourceWithEnvironment { - private const string AzureAIUserRoleDefinitionId = "53ca6127-db72-4b80-b1b0-d745d6d5456d"; + // The "Azure AI User" built-in role (data-plane access to Foundry agents/inference). Granted to + // the agent's own instance identity below, and to consumers that reference the agent (see + // HostedAgentResourceBuilderExtensions.GrantHostedAgentConsumerRoles). + internal const string AzureAIUserRoleDefinitionId = "53ca6127-db72-4b80-b1b0-d745d6d5456d"; /// /// Creates a new instance of the class. diff --git a/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs b/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs index 60104ccc4d3..ce7a545e47f 100644 --- a/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs +++ b/src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExtension.cs @@ -4,6 +4,7 @@ using System.Net.Http.Json; using System.Text.Json; using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Azure; using Aspire.Hosting.Foundry; using Microsoft.Extensions.DependencyInjection; @@ -419,6 +420,35 @@ private static void ConfigurePublishMode( builder.ApplicationBuilder.AddResource(hostedAgent) .WithIconName("Agents") .WithReferenceRelationship(target); + + // Referencing a hosted agent (its node app) only injects the agent's service-discovery URL. + // Unlike referencing a first-class Azure resource, it does not give the consumer a managed + // identity or any RBAC on the Foundry account, so calls to the agent's invocation endpoint + // fail with 401/403 at runtime. Stamp a ReferenceRoleAssignmentAnnotation on the agent's + // target so AzureResourcePreparer grants the "Azure AI User" role on the owning Foundry + // account to every consumer that references this agent, and provisions the identity that + // makes ACA inject AZURE_CLIENT_ID. + StampHostedAgentConsumerRoleAnnotation(target, projectResource.Parent); + } + + private static void StampHostedAgentConsumerRoleAnnotation(IResourceWithEnvironment target, FoundryResource account) + { + // Grant only the "Azure AI User" role required to invoke the hosted agent. We deliberately do + // not union the account's default data-plane roles here: + // - A consumer that also references the account directly still receives those defaults through + // AzureResourcePreparer's normal reference walk (they are preserved when GetAllRoleAssignments + // unions per target). + // - A consumer that declares explicit role assignments on the account intentionally suppresses + // the account defaults; folding them back in here would defeat that suppression. + // So the minimal, least-privilege grant for a pure agent consumer is "Azure AI User" alone. + var roles = new HashSet + { + new(AzureHostedAgentResource.AzureAIUserRoleDefinitionId, "Azure AI User") + }; + +#pragma warning disable ASPIREAZURE003 // Type is for evaluation purposes only and is subject to change or removal in future updates. + target.Annotations.Add(new ReferenceRoleAssignmentAnnotation(account, roles)); +#pragma warning restore ASPIREAZURE003 } private sealed class HostedAgentRunProtocol @@ -454,3 +484,4 @@ private sealed class HostedAgentRunProtocol public required Func CreateRequestContent { get; init; } } } + diff --git a/tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs b/tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs index 889b2c1b964..63dfc210cfd 100644 --- a/tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs +++ b/tests/Aspire.Hosting.Azure.Tests/AzureResourcePreparerTests.cs @@ -538,6 +538,204 @@ public async Task ViteAppDoesNotGetManagedIdentity() Assert.DoesNotContain(model.Resources, r => r.Name == "frontend-identity"); } + [Fact] + public async Task ReferenceRoleAssignmentAnnotation_PublishMode_GrantsRolesOnImpliedTargetToConsumer() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + builder.AddAzureContainerAppEnvironment("env"); + + var storage = builder.AddAzureStorage("storage"); + + // A non-Azure compute resource (the "agent" node app) that fronts the storage account: any + // resource referencing it should be granted a role on storage even though storage is only a + // transitive dependency that the IAzureResource-only reference walk cannot reach. + var agent = builder.AddContainer("agent", "img:latest") + .WithHttpEndpoint(); + agent.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation( + storage.Resource, + new HashSet { new(StorageBuiltInRole.StorageBlobDataReader.ToString(), nameof(StorageBuiltInRole.StorageBlobDataReader)) })); + + var consumer = builder.AddProject("api", launchProfileName: null) + .WithReference(agent.GetEndpoint("http")); + + using var app = builder.Build(); + await ExecuteBeforeStartHooksAsync(app, default); + + Assert.True(consumer.Resource.TryGetLastAnnotation(out var consumerRoleAssignments)); + Assert.Equal(storage.Resource, consumerRoleAssignments.Target); + Assert.Single(consumerRoleAssignments.Roles, role => role.Id == StorageBuiltInRole.StorageBlobDataReader.ToString()); + } + + [Fact] + public async Task ReferenceRoleAssignmentAnnotation_RunMode_AppliesRolesToGlobalRolesResource() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Run); + builder.AddAzureContainerAppEnvironment("env"); + + var storage = builder.AddAzureStorage("storage"); + + var agent = builder.AddContainer("agent", "img:latest") + .WithHttpEndpoint(); + agent.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation( + storage.Resource, + new HashSet { new(StorageBuiltInRole.StorageBlobDataReader.ToString(), nameof(StorageBuiltInRole.StorageBlobDataReader)) })); + + builder.AddProject("api", launchProfileName: null) + .WithReference(agent.GetEndpoint("http")); + + using var app = builder.Build(); + var model = app.Services.GetRequiredService(); + await ExecuteBeforeStartHooksAsync(app, default); + + var storageRoles = Assert.Single(model.Resources.OfType(), r => r.Name == "storage-roles"); + Assert.Same(storage.Resource, storageRoles.TargetAzureResource); + } + + [Fact] + public async Task ReferenceRoleAssignmentAnnotation_ConsumerNotReferencingFrontingResource_GetsNoRole() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + builder.AddAzureContainerAppEnvironment("env"); + + var storage = builder.AddAzureStorage("storage"); + + var agent = builder.AddContainer("agent", "img:latest") + .WithHttpEndpoint(); + agent.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation( + storage.Resource, + new HashSet { new(StorageBuiltInRole.StorageBlobDataReader.ToString(), nameof(StorageBuiltInRole.StorageBlobDataReader)) })); + + // This compute resource does not reference the fronting agent, so it must not be granted any + // role on the implied storage target. + var bystander = builder.AddProject("bystander", launchProfileName: null); + + using var app = builder.Build(); + await ExecuteBeforeStartHooksAsync(app, default); + + Assert.False(bystander.Resource.TryGetLastAnnotation(out _)); + } + + [Fact] + public async Task ReferenceRoleAssignmentAnnotation_SameTargetFromTwoDependencies_DedupesRoles() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + builder.AddAzureContainerAppEnvironment("env"); + + var storage = builder.AddAzureStorage("storage"); + + // Two fronting resources (e.g. two hosted agents on the same Foundry account) each imply the + // same role on the same target. A consumer referencing both must end up with a single role + // assignment for that role, otherwise two RoleAssignment bicep resources collide on the same + // identifier ("{prefix}_{roleName}") and bicep compilation fails. + var role = new HashSet { new(StorageBuiltInRole.StorageBlobDataReader.ToString(), nameof(StorageBuiltInRole.StorageBlobDataReader)) }; + + var agent1 = builder.AddContainer("agent1", "img:latest").WithHttpEndpoint(); + agent1.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation(storage.Resource, role)); + + var agent2 = builder.AddContainer("agent2", "img:latest").WithHttpEndpoint(); + agent2.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation(storage.Resource, role)); + + var consumer = builder.AddProject("api", launchProfileName: null) + .WithReference(agent1.GetEndpoint("http")) + .WithReference(agent2.GetEndpoint("http")); + + using var app = builder.Build(); + var model = app.Services.GetRequiredService(); + await ExecuteBeforeStartHooksAsync(app, default); + + var roleAssignmentResource = Assert.Single(model.Resources.OfType(), r => r.Name == "api-roles-storage"); + Assert.Same(storage.Resource, roleAssignmentResource.TargetAzureResource); + Assert.Same(consumer.Resource, roleAssignmentResource.OwnerResource); + + // The generated bicep must contain exactly one role assignment, not two duplicates of the same role. + var manifest = await GetManifestWithBicep(roleAssignmentResource, skipPreparer: true); + var roleAssignmentCount = System.Text.RegularExpressions.Regex.Matches(manifest.BicepText, "Microsoft.Authorization/roleAssignments@").Count; + Assert.Equal(1, roleAssignmentCount); + } + + [Fact] + public async Task ReferenceRoleAssignmentAnnotation_ConsumerWithExplicitRoleAssignment_DoesNotReintroduceSuppressedDefaults() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + builder.AddAzureContainerAppEnvironment("env"); + + var storage = builder.AddAzureStorage("storage"); + var blobs = storage.AddBlobs("blobs"); + + // A fronting resource (e.g. a Foundry hosted agent's node app) implies the Reader role on storage. + var agent = builder.AddContainer("agent", "img:latest").WithHttpEndpoint(); + agent.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation( + storage.Resource, + new HashSet { new(StorageBuiltInRole.StorageBlobDataReader.ToString(), nameof(StorageBuiltInRole.StorageBlobDataReader)) })); + + // The consumer references storage directly (so its default role assignments would normally be + // applied) but declares an explicit WithRoleAssignments, which intentionally suppresses those + // defaults. The implied-reference hook must add only its Reader role and must NOT re-introduce the + // suppressed defaults - that was the "pit of failure" the union-of-defaults pattern would have caused. + var consumer = builder.AddProject("api", launchProfileName: null) + .WithRoleAssignments(storage, StorageBuiltInRole.StorageBlobDelegator) + .WithReference(blobs) + .WithReference(agent.GetEndpoint("http")); + + using var app = builder.Build(); + await ExecuteBeforeStartHooksAsync(app, default); + + var roleIds = consumer.Resource.Annotations.OfType() + .Where(a => a.Target == storage.Resource) + .SelectMany(a => a.Roles) + .Select(r => r.Id) + .ToHashSet(); + + // Explicit role + implied Reader are present. + Assert.Contains(StorageBuiltInRole.StorageBlobDelegator.ToString(), roleIds); + Assert.Contains(StorageBuiltInRole.StorageBlobDataReader.ToString(), roleIds); + + // The suppressed defaults must not be re-introduced by the implied-reference hook. + Assert.DoesNotContain(StorageBuiltInRole.StorageBlobDataContributor.ToString(), roleIds); + Assert.DoesNotContain(StorageBuiltInRole.StorageTableDataContributor.ToString(), roleIds); + Assert.DoesNotContain(StorageBuiltInRole.StorageQueueDataContributor.ToString(), roleIds); + } + + [Fact] + public async Task ReferenceRoleAssignmentAnnotation_ConsumerWithDirectReference_KeepsDefaultsAndAddsImpliedRole() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + builder.AddAzureContainerAppEnvironment("env"); + + var storage = builder.AddAzureStorage("storage"); + var blobs = storage.AddBlobs("blobs"); + + // A fronting resource (e.g. a Foundry hosted agent's node app) implies the Reader role on storage. + var agent = builder.AddContainer("agent", "img:latest").WithHttpEndpoint(); + agent.Resource.Annotations.Add(new ReferenceRoleAssignmentAnnotation( + storage.Resource, + new HashSet { new(StorageBuiltInRole.StorageBlobDataReader.ToString(), nameof(StorageBuiltInRole.StorageBlobDataReader)) })); + + // The consumer references storage directly without declaring explicit role assignments, so the + // account defaults still apply. The implied-reference hook must add its Reader role alongside the + // defaults (the caller - the preparer - owns defaults; the hook never replaces or removes them). + var consumer = builder.AddProject("api", launchProfileName: null) + .WithReference(blobs) + .WithReference(agent.GetEndpoint("http")); + + using var app = builder.Build(); + await ExecuteBeforeStartHooksAsync(app, default); + + var roleIds = consumer.Resource.Annotations.OfType() + .Where(a => a.Target == storage.Resource) + .SelectMany(a => a.Roles) + .Select(r => r.Id) + .ToHashSet(); + + // Defaults are preserved by the preparer's normal reference walk. + Assert.Contains(StorageBuiltInRole.StorageBlobDataContributor.ToString(), roleIds); + Assert.Contains(StorageBuiltInRole.StorageTableDataContributor.ToString(), roleIds); + Assert.Contains(StorageBuiltInRole.StorageQueueDataContributor.ToString(), roleIds); + + // The implied Reader role is added on top of the defaults. + Assert.Contains(StorageBuiltInRole.StorageBlobDataReader.ToString(), roleIds); + } + private sealed class Project : IProjectMetadata { public string ProjectPath => "project"; diff --git a/tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs b/tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs index 8405cd2e25a..cff2d20891e 100644 --- a/tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs +++ b/tests/Aspire.Hosting.Foundry.Tests/HostedAgentExtensionTests.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Azure; using Aspire.Hosting.Tests.Utils; using Aspire.Hosting.Utils; using Azure.AI.Projects.Agents; @@ -447,6 +448,88 @@ public async Task FoundryProject_DefaultRegistryDoesNotAddGlobalRegistryTargets( [UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ExecuteBeforeStartHooksAsync")] private static extern Task ExecuteBeforeStartHooksAsync(DistributedApplication app, CancellationToken cancellationToken); + [Fact] + public void AsHostedAgent_StampsReferenceRoleAssignmentAnnotationOnTarget_WithAzureAIUserRole() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + var project = builder.AddFoundry("account") + .AddProject("my-project"); + + builder.AddPythonApp("agent", "./app.py", "main:app") + .AsHostedAgent(project); + + var hostedAgent = Assert.Single(builder.Resources.OfType()); + var account = Assert.Single(builder.Resources.OfType()); + +#pragma warning disable ASPIREAZURE003 // Type is for evaluation purposes only and is subject to change or removal in future updates. + var annotation = Assert.Single(hostedAgent.Target.Annotations.OfType()); + Assert.Same(account, annotation.Target); + Assert.Contains(annotation.Roles, role => + string.Equals(role.Id, AzureHostedAgentResource.AzureAIUserRoleDefinitionId, StringComparison.OrdinalIgnoreCase)); +#pragma warning restore ASPIREAZURE003 + } + + [Fact] + public void AsHostedAgent_ReferenceRoleAssignmentAnnotation_GrantsOnlyAzureAIUserRole() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + var project = builder.AddFoundry("account") + .AddProject("my-project"); + + builder.AddPythonApp("agent", "./app.py", "main:app") + .AsHostedAgent(project); + + var hostedAgent = Assert.Single(builder.Resources.OfType()); + var account = Assert.Single(builder.Resources.OfType()); + Assert.True(account.TryGetLastAnnotation(out var defaults)); + +#pragma warning disable ASPIREAZURE003 // Type is for evaluation purposes only and is subject to change or removal in future updates. + var annotation = Assert.Single(hostedAgent.Target.Annotations.OfType()); + + // The implied grant is least-privilege: only "Azure AI User" is required to invoke the agent. + var role = Assert.Single(annotation.Roles); + Assert.Equal(AzureHostedAgentResource.AzureAIUserRoleDefinitionId, role.Id, ignoreCase: true); + + // The account's default data-plane roles must NOT be folded in here. A consumer that references + // the account directly still receives them via the preparer's normal walk, and a consumer that + // explicitly suppresses them must keep them suppressed. + foreach (var defaultRole in defaults.Roles) + { + Assert.DoesNotContain(annotation.Roles, r => string.Equals(r.Id, defaultRole.Id, StringComparison.OrdinalIgnoreCase)); + } +#pragma warning restore ASPIREAZURE003 + } + + [Fact] + public void AsHostedAgent_MultipleHostedAgents_EachTargetCarriesItsOwnReferenceRoleAssignment() + { + using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); + var project = builder.AddFoundry("account") + .AddProject("my-project"); + var otherProject = builder.AddFoundry("account2") + .AddProject("other-project"); + + builder.AddPythonApp("agent", "./app.py", "main:app") + .AsHostedAgent(project); + builder.AddPythonApp("agent2", "./app.py", "main:app") + .AsHostedAgent(otherProject); + + var hostedAgents = builder.Resources.OfType().ToList(); + Assert.Equal(2, hostedAgents.Count); + + var account = Assert.Single(builder.Resources.OfType(), r => r.Name == "account"); + var account2 = Assert.Single(builder.Resources.OfType(), r => r.Name == "account2"); + +#pragma warning disable ASPIREAZURE003 // Type is for evaluation purposes only and is subject to change or removal in future updates. + var targets = hostedAgents + .Select(a => Assert.Single(a.Target.Annotations.OfType()).Target) + .ToList(); + + Assert.Contains(account, targets); + Assert.Contains(account2, targets); +#pragma warning restore ASPIREAZURE003 + } + private sealed class Project : IProjectMetadata { public string ProjectPath => "project"; From 177665cd02d7cde40159df34a6c7b2f900df9471 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Sun, 31 May 2026 17:32:57 -0700 Subject: [PATCH 8/8] Regenerate cross-env Foundry snapshots for RBAC auto-wiring The cross-compute-environment Foundry hosted-agent snapshots were committed before the RBAC auto-wiring change and never regenerated. With the consumer now receiving a managed identity (web-identity) plus AZURE_CLIENT_ID and AZURE_TOKEN_CREDENTIALS env vars, the generated bicep/json changed. CI failed because the verified baselines were stale; local runs masked it because Verify auto-accepts on developer machines. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...esolvedAcrossComputeEnvironments.verified.bicep | 14 ++++++++++++++ ...ResolvedAcrossComputeEnvironments.verified.json | 2 ++ ...esolvedAcrossComputeEnvironments.verified.bicep | 13 +++++++++++++ ...ResolvedAcrossComputeEnvironments.verified.json | 4 +++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep index 906f511b10a..f70d350d3a9 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -15,6 +15,10 @@ param web_containerport string param project_outputs_endpoint string +param web_identity_outputs_id string + +param web_identity_outputs_clientid string + param env_outputs_azure_app_service_dashboard_uri string param env_outputs_azure_website_contributor_managed_identity_id string @@ -38,6 +42,7 @@ resource webapp 'Microsoft.Web/sites@2025-03-01' = { location: location properties: { serverFarmId: env_outputs_planid + keyVaultReferenceIdentity: web_identity_outputs_id siteConfig: { numberOfWorkers: 30 linuxFxVersion: 'SITECONTAINERS' @@ -72,6 +77,14 @@ resource webapp 'Microsoft.Web/sites@2025-03-01' = { name: 'AGENT_URL' value: '${project_outputs_endpoint}/agents/agent-ha' } + { + name: 'AZURE_CLIENT_ID' + value: web_identity_outputs_clientid + } + { + name: 'AZURE_TOKEN_CREDENTIALS' + value: 'ManagedIdentityCredential' + } { name: 'ASPIRE_ENVIRONMENT_NAME' value: 'env' @@ -107,6 +120,7 @@ resource webapp 'Microsoft.Web/sites@2025-03-01' = { type: 'UserAssigned' userAssignedIdentities: { '${env_outputs_azure_container_registry_managed_identity_id}': { } + '${web_identity_outputs_id}': { } } } } diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json index 6273d48fed6..cd9161f896c 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json @@ -9,6 +9,8 @@ "web_containerimage": "{web.containerImage}", "web_containerport": "{web.containerPort}", "project_outputs_endpoint": "{project.outputs.endpoint}", + "web_identity_outputs_id": "{web-identity.outputs.id}", + "web_identity_outputs_clientid": "{web-identity.outputs.clientId}", "env_outputs_azure_app_service_dashboard_uri": "{env.outputs.AZURE_APP_SERVICE_DASHBOARD_URI}", "env_outputs_azure_website_contributor_managed_identity_id": "{env.outputs.AZURE_WEBSITE_CONTRIBUTOR_MANAGED_IDENTITY_ID}", "env_outputs_azure_website_contributor_managed_identity_principal_id": "{env.outputs.AZURE_WEBSITE_CONTRIBUTOR_MANAGED_IDENTITY_PRINCIPAL_ID}" diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep index 2b9d0e6dba7..c6f7220f3f5 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -11,10 +11,14 @@ param env_outputs_azure_container_registry_managed_identity_id string param web_containerimage string +param web_identity_outputs_id string + param web_containerport string param project_outputs_endpoint string +param web_identity_outputs_clientid string + resource web 'Microsoft.App/containerApps@2025-10-02-preview' = { name: 'web' location: location @@ -69,6 +73,14 @@ resource web 'Microsoft.App/containerApps@2025-10-02-preview' = { name: 'AGENT_URL' value: '${project_outputs_endpoint}/agents/agent-ha' } + { + name: 'AZURE_CLIENT_ID' + value: web_identity_outputs_clientid + } + { + name: 'AZURE_TOKEN_CREDENTIALS' + value: 'ManagedIdentityCredential' + } ] } ] @@ -80,6 +92,7 @@ resource web 'Microsoft.App/containerApps@2025-10-02-preview' = { identity: { type: 'UserAssigned' userAssignedIdentities: { + '${web_identity_outputs_id}': { } '${env_outputs_azure_container_registry_managed_identity_id}': { } } } diff --git a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json index 15972c127e5..9bba0f4e346 100644 --- a/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json @@ -7,7 +7,9 @@ "env_outputs_azure_container_registry_endpoint": "{env.outputs.AZURE_CONTAINER_REGISTRY_ENDPOINT}", "env_outputs_azure_container_registry_managed_identity_id": "{env.outputs.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID}", "web_containerimage": "{web.containerImage}", + "web_identity_outputs_id": "{web-identity.outputs.id}", "web_containerport": "{web.containerPort}", - "project_outputs_endpoint": "{project.outputs.endpoint}" + "project_outputs_endpoint": "{project.outputs.endpoint}", + "web_identity_outputs_clientid": "{web-identity.outputs.clientId}" } } \ No newline at end of file