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..e252d0b7705 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, [_containerAppEnvironmentContext.Environment], out var crossExpr)) + { + 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, [_containerAppEnvironmentContext.Environment], out var crossExpr)) + { + 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..b5d3c774560 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, [environmentContext.Environment], out var crossExpr)) + { + 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, [environmentContext.Environment], out var crossExpr)) + { + 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.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/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..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. @@ -429,8 +432,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/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/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs b/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs index 8fcabc67d52..ac90f16f99e 100644 --- a/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs +++ b/src/Aspire.Hosting.Foundry/Project/ProjectResource.cs @@ -212,9 +212,48 @@ 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}"); + + // 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/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..6f75bf8acfb 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, [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr)) + { + 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, [kubernetesEnvironmentResource, kubernetesEnvironmentResource.OwningComputeEnvironment], out var crossExpr)) + { + 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..23dfdcf47ce --- /dev/null +++ b/src/Shared/ComputeEnvironmentEndpointResolver.cs @@ -0,0 +1,137 @@ +// 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'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 + /// environment is different from the publisher's current compute environment(s). + /// + /// The endpoint reference expression 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( + EndpointReferenceExpression endpointReferenceExpression, + IReadOnlyList currentComputeEnvironments, + [NotNullWhen(true)] out ReferenceExpression? expression) + { + ArgumentNullException.ThrowIfNull(endpointReferenceExpression); + ArgumentNullException.ThrowIfNull(currentComputeEnvironments); + + expression = null; + + var owningResource = endpointReferenceExpression.Endpoint.Resource; + + // 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; + } + + // 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)) + { + 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..112e54fb25e --- /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 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-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 + { + 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/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.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep new file mode 100644 index 00000000000..f70d350d3a9 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -0,0 +1,148 @@ +@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 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 + +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 + keyVaultReferenceIdentity: web_identity_outputs_id + 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-ha' + } + { + name: 'services__agent__http__0' + value: '${project_outputs_endpoint}/agents/agent-ha' + } + { + 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' + } + { + 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}': { } + '${web_identity_outputs_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..cd9161f896c --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureAppServiceTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json @@ -0,0 +1,18 @@ +{ + "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}", + "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}" + } +} \ 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..c6f7220f3f5 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.bicep @@ -0,0 +1,99 @@ +@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_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 + 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-ha' + } + { + name: 'services__agent__http__0' + value: '${project_outputs_endpoint}/agents/agent-ha' + } + { + 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' + } + ] + } + ] + scale: { + minReplicas: 1 + } + } + } + identity: { + type: 'UserAssigned' + userAssignedIdentities: { + '${web_identity_outputs_id}': { } + '${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..9bba0f4e346 --- /dev/null +++ b/tests/Aspire.Hosting.Azure.Tests/Snapshots/AzureContainerAppsTests.EndpointReferenceToFoundryHostedAgentIsResolvedAcrossComputeEnvironments.verified.json @@ -0,0 +1,15 @@ +{ + "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_identity_outputs_id": "{web-identity.outputs.id}", + "web_containerport": "{web.containerPort}", + "project_outputs_endpoint": "{project.outputs.endpoint}", + "web_identity_outputs_clientid": "{web-identity.outputs.clientId}" + } +} \ No newline at end of file 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"; 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..830a02ff526 --- /dev/null +++ b/tests/Aspire.Hosting.Tests/ComputeEnvironmentEndpointResolverTests.cs @@ -0,0 +1,168 @@ +// 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), [currentEnv.Resource], out var expression); + + 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 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() + { + 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), [currentEnv.Resource], out var expression); + + 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 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); + + var resolved = ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out var expression); + + 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), [currentEnv.Resource], out var expression); + + 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 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 }); + 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), [currentEnv.Resource], out var expression); + + 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. + // 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 }); + var endpoint = AddHttpEndpoint(agent.Resource, port: 8080, targetPort: 5000); + + Assert.Throws(() => + ComputeEnvironmentEndpointResolver.TryGetCrossEnvironmentEndpointExpression( + endpoint.Property(EndpointProperty.Url), [currentEnv.Resource], out _)); + } + + 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 + { + } +}