Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure KeyVault for Aspire via CDK #2606

Merged
merged 6 commits into from
Mar 4, 2024
Merged

Azure KeyVault for Aspire via CDK #2606

merged 6 commits into from
Mar 4, 2024

Conversation

mitchdenny
Copy link
Member

@mitchdenny mitchdenny commented Mar 4, 2024

KeyVault migration to CDK. There are some outstanding issues in the CDK outlined here:

Azure/azure-sdk-for-net#42349

Usage:

var keyvault = builder.AddAzureKeyVaultConstruct("mykv", (construct, keyVault) =>
{
    var secret = new KeyVaultSecret(construct, name: "mysecret");
    secret.AssignParameter(x => x.Properties.Value, signaturesecret);
});

Bicep produced:

targetScope = 'resourceGroup'

@description('')
param location string = resourceGroup().location

@description('')
param principalId string

@description('')
param principalType string

@description('')
param signaturesecret string


resource keyVault_IKWI2x0B5 'Microsoft.KeyVault/vaults@2023-02-01' = {
  name: toLower(take(concat('mykv', uniqueString(resourceGroup().id)), 24))
  location: location
  properties: {
    tenantId: tenant().tenantId
    sku: {
      name: 'standard'
      family: 'A'
    }
    enableRbacAuthorization: true
  }
}

resource roleAssignment_Z4xb36awa 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
  scope: keyVault_IKWI2x0B5
  name: guid(keyVault_IKWI2x0B5.id, principalId, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '00482a5a-887f-4fb3-b363-3b7fe8e74483'))
  properties: {
    roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '00482a5a-887f-4fb3-b363-3b7fe8e74483')
    principalId: principalId
    principalType: principalType
  }
}

resource keyVaultSecret_7ClrhkRcM 'Microsoft.KeyVault/vaults/secrets@2023-02-01' = {
  parent: keyVault_IKWI2x0B5
  name: 'mysecret'
  location: location
  properties: {
    value: signaturesecret
  }
}

output vaultUri string = keyVault_IKWI2x0B5.properties.vaultUri

Currently you can use the configureResource callback to add secrets. Although it would be fairly trivial to add a secret via an extension method on the app model itself. The wrinkle is that what makes sense from a consumption point of view. Currently the SecretClient takes a vaultUri, the uri to a KV secret itself isn't something you can wrap a secret around.

It actually makes more sense for KeyVault to behave like Cosmos where AddDatabase(...) doesn't result in a IResourceBuilder<CosmosDatabaseResource> but instead just adds another secret to the pile.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Mar 4, 2024
@mitchdenny mitchdenny self-assigned this Mar 4, 2024
@mitchdenny mitchdenny added this to the preview 5 (Apr) milestone Mar 4, 2024
@davidfowl
Copy link
Member

Can I create a secret that references a parameter.

@mitchdenny
Copy link
Member Author

Yes. Take a look in the CdkSample ... signaturesecret.

@mitchdenny mitchdenny merged commit 5d806ac into main Mar 4, 2024
8 checks passed
@mitchdenny mitchdenny deleted the mitchdenny/cdk-keyvault branch March 4, 2024 23:39
@github-actions github-actions bot locked and limited conversation to collaborators Apr 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants