microsoft / microsoft/aspire

WithAccessKeyAuthentication/WithPasswordAuthentication doesn't work correctly with AsExisting resources in another resource group

Open
#11,218 2 comments 0 reactions 0 assignees View on GitHub
area-integrations azure
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

Using WithAccessKeyAuthentication (and WithPasswordAuthentication in Azure PostgreSQL) against existing resources in separate resource groups don't work correctly.

**Repro steps**

```C#
var cosmos = builder.AddAzureCosmosDB("cosmos")
.RunAsExisting("mycosmos", "myresourcegroup")
.WithAccessKeyAuthentication();
```

When you try to deploy to a resource group that isn't `myresourcegroup`, the above code fails with:

```
Failed to perform 'write' on resource(s) of type 'vaults/secrets', because the parent resource '/subscriptions/XXXX/resourceGroups/myresourcegroup/providers/Microsoft.KeyVault/vaults/cosmoskv-loehmsay7z6re' could not be found. Click here for details
```

The issue is the way we are generating the bicep for the `cosmos` resource:

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

param cosmos_kv_outputs_name string

resource cosmos 'Microsoft.DocumentDB/databaseAccounts@2024-08-15' = {
name: take('cosmos-${uniqueString(resourceGroup().id)}', 44)
location: location
properties: {
locations: [
{
locationName: location
failoverPriority: 0
}
]
capabilities: [
{
name: 'EnableServerless'
}
]
consistencyPolicy: {
defaultConsistencyLevel: 'Session'
}
databaseAccountOfferType: 'Standard'
disableLocalAuth: false
}
kind: 'GlobalDocumentDB'
tags: {
'aspire-resource-name': 'cosmos'
}
}

resource keyVault 'Microsoft.KeyVault/vaults@2024-11-01' existing = {
name: cosmos_kv_outputs_name
}

resource connectionString 'Microsoft.KeyVault/vaults/secrets@2024-11-01' = {
name: 'connectionstrings--cosmos'
properties: {
value: 'AccountEndpoint=${cosmos.properties.documentEndpoint};AccountKey=${cosmos.listKeys().primaryMasterKey}'
}
parent: keyVault
}

output name string = cosmos.name
```

Note that the way we are reference the `keyVault` is only by `name` - the `cosmos_kv_outputs_name` parameter.

The issue is the whole module that adds the `cosmos` resource has a `scope` set to `myresourcegroup`. So when referencing the `keyVault` only by `name`, it is assumed to be in the same resource group.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.