Skip to content

Conversation

@davidfowl
Copy link
Member

@davidfowl davidfowl commented Aug 16, 2025

Description

  • Today when using referencing an azure resources marked with AsExisting, that is not taken into account when using AsKeyVaultSecret, nor AddAsExisting. This causes errors to happen at deployment time. This change introduces a new helper to make this easier TryApplyExistingResourceNameAndScope.
  • This change properly takes the existing annotation into account when generating the reference.
  • Added and updated tests

Fixes #10908

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
  • Did you add public API?
    • Yes
  • Does the change make any security assumptions or guarantees?
    • No
  • Does the change require an update in our Aspire docs?
    • No

PS: There will be follow up fixes to apply this to every implementation of AddAsExistingResource

- Today when using referencing an azure resources marked with AsExisting, that is not taken into account when using AsKeyVaultSecret, nor AddAsExisting. This causes errors to happen at deploymetn time.
- This change properly takes the existing annotation into account when generating the reference.
- Added and updated tests
@github-actions github-actions bot added the area-integrations Issues pertaining to Aspire Integrations packages label Aug 16, 2025
@davidfowl davidfowl requested a review from mitchdenny August 16, 2025 05:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where referencing existing Azure Key Vault resources using AsExisting wasn't properly handled when using AsKeyVaultSecret or AddAsExisting. The fix ensures that the existing resource annotation is properly considered when generating Bicep references, preventing deployment-time errors.

  • Fixes improper handling of existing Azure resource annotations in Key Vault secret references
  • Adds a new helper method TryApplyExistingResourceNameAndScope to handle existing resource configuration
  • Updates Key Vault resource provisioning to use the new helper method

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Aspire.Hosting.Azure/AzureProvisioningResourceExtensions.cs Updates Key Vault secret reference logic to properly handle existing resources
src/Aspire.Hosting.Azure/AzureProvisioningResource.cs Adds new helper method for applying existing resource configuration
src/Aspire.Hosting.Azure.KeyVault/AzureKeyVaultResource.cs Updates Key Vault resource provisioning to use the new helper method
tests/Aspire.Hosting.Azure.Tests/AzureKeyVaultTests.cs Adds new test cases for existing Key Vault scenarios
tests/Aspire.Hosting.Azure.Tests/AzureContainerAppsTests.cs Updates tests to include existing Key Vault references
tests/Aspire.Hosting.Azure.Tests/Snapshots/* Updated snapshot files reflecting the corrected Bicep generation

@davidfowl davidfowl added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 16, 2025
@davidfowl
Copy link
Member Author

davidfowl commented Aug 16, 2025

Still testing. When I try to deploy with this I get:

Error BCP139: A resource's scope must match the scope of the Bicep file for it to be deployable. You must use modules to deploy resources to a different scope

@davidfowl
Copy link
Member Author

Turns out this implementation would need to get quite a bit more complex as its not possible to reference across modules like this.

@davidfowl
Copy link
Member Author

OK the issue is the role assignment bicep:

@description('The location for the resource(s) to be deployed.')
param location string = resourceGroup().location

param principalId string

resource kv 'Microsoft.KeyVault/vaults@2024-11-01' existing = {
  name: 'davidfowlkv0'
  scope: resourceGroup('rg-shared-dev')
}

resource kv_KeyVaultSecretsUser 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  name: guid(kv.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '4633458b-17de-408a-b874-0445c86b69e6'))
  properties: {
    principalId: principalId
    roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '4633458b-17de-408a-b874-0445c86b69e6')
    principalType: 'ServicePrincipal'
  }
  scope: kv
}

We need to know if the module has the "right" scope so that we can elide the explicit resourceGroup(...), or maybe we can special case the role assignment logic.

@davidfowl davidfowl removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Aug 19, 2025
@davidfowl
Copy link
Member Author

OK resolved the issue by checking the scope of the target infrastructure. If that is different from the existing resource's scope it skips this part.

var store = KeyVaultService.FromExisting(bicepIdentifier);
store.Name = NameOutputReference.AsProvisioningParameter(infra);

if (!TryApplyExistingResourceNameAndScope(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do this for more Azure resources? Or is KeyVault the only one with this problem?

Copy link
Member Author

@davidfowl davidfowl Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#10992, will throw copilot at it once this is done.

}

// Compare the resource groups only if they are the same type (string or ParameterResource)
if (infraResourceGroup.GetType() == existingResourceGroup.GetType())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this constraint? Couldn't we evaluate the value on a ParameterResource and compare it against a string resource group in a different annotation?

Copy link
Member Author

@davidfowl davidfowl Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't seem like a meaningful comparison.

@davidfowl davidfowl merged commit 1d96ac2 into main Aug 19, 2025
576 of 578 checks passed
@davidfowl davidfowl deleted the davidfowl/fix-azure-refs branch August 19, 2025 17:59
@dotnet-policy-service dotnet-policy-service bot added this to the 9.5 milestone Aug 19, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Sep 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-integrations Issues pertaining to Aspire Integrations packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GetSecret() references from external AsExisting() key vault are invalid when using AddAzureContainerAppEnvironment()

4 participants