-
Notifications
You must be signed in to change notification settings - Fork 723
Added WithEnvironment overload supporting Keyvault secret refs #8821
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.ApplicationModel; | ||
| using Aspire.Hosting.Utils; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Xunit; | ||
|
|
||
| namespace Aspire.Hosting.Azure.Tests; | ||
|
|
||
| public class AzureKeyVaultTests(ITestOutputHelper output) | ||
| { | ||
| [Fact] | ||
| public async Task AddKeyVaultViaRunMode() | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
|
||
| var mykv = builder.AddAzureKeyVault("mykv"); | ||
|
|
||
| var manifest = await AzureManifestUtils.GetManifestWithBicep(mykv.Resource); | ||
|
|
||
| var expectedManifest = """ | ||
| { | ||
| "type": "azure.bicep.v0", | ||
| "connectionString": "{mykv.outputs.vaultUri}", | ||
| "path": "mykv.module.bicep" | ||
| } | ||
| """; | ||
| Assert.Equal(expectedManifest, manifest.ManifestNode.ToString()); | ||
|
|
||
| var expectedBicep = """ | ||
| @description('The location for the resource(s) to be deployed.') | ||
| param location string = resourceGroup().location | ||
|
|
||
| resource mykv 'Microsoft.KeyVault/vaults@2023-07-01' = { | ||
| name: take('mykv-${uniqueString(resourceGroup().id)}', 24) | ||
| location: location | ||
| properties: { | ||
| tenantId: tenant().tenantId | ||
| sku: { | ||
| family: 'A' | ||
| name: 'standard' | ||
| } | ||
| enableRbacAuthorization: true | ||
| } | ||
| tags: { | ||
| 'aspire-resource-name': 'mykv' | ||
| } | ||
| } | ||
|
|
||
| output vaultUri string = mykv.properties.vaultUri | ||
|
|
||
| output name string = mykv.name | ||
| """; | ||
| output.WriteLine(manifest.BicepText); | ||
| Assert.Equal(expectedBicep, manifest.BicepText); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task AddKeyVaultViaPublishMode() | ||
| { | ||
| using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish); | ||
|
|
||
| var mykv = builder.AddAzureKeyVault("mykv"); | ||
|
|
||
| using var app = builder.Build(); | ||
| var model = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
| var manifest = await AzureManifestUtils.GetManifestWithBicep(model, mykv.Resource); | ||
|
|
||
| var expectedManifest = """ | ||
| { | ||
| "type": "azure.bicep.v0", | ||
| "connectionString": "{mykv.outputs.vaultUri}", | ||
| "path": "mykv.module.bicep" | ||
| } | ||
| """; | ||
| Assert.Equal(expectedManifest, manifest.ManifestNode.ToString()); | ||
|
|
||
| var expectedBicep = """ | ||
| @description('The location for the resource(s) to be deployed.') | ||
| param location string = resourceGroup().location | ||
|
|
||
| resource mykv 'Microsoft.KeyVault/vaults@2023-07-01' = { | ||
| name: take('mykv-${uniqueString(resourceGroup().id)}', 24) | ||
| location: location | ||
| properties: { | ||
| tenantId: tenant().tenantId | ||
| sku: { | ||
| family: 'A' | ||
| name: 'standard' | ||
| } | ||
| enableRbacAuthorization: true | ||
| } | ||
| tags: { | ||
| 'aspire-resource-name': 'mykv' | ||
| } | ||
| } | ||
|
|
||
| output vaultUri string = mykv.properties.vaultUri | ||
|
|
||
| output name string = mykv.name | ||
| """; | ||
| output.WriteLine(manifest.BicepText); | ||
| Assert.Equal(expectedBicep, manifest.BicepText); | ||
|
|
||
| var kvRoles = Assert.Single(model.Resources.OfType<AzureProvisioningResource>().Where(r => r.Name == $"mykv-roles")); | ||
| var kvRolesManifest = await AzureManifestUtils.GetManifestWithBicep(kvRoles, skipPreparer: true); | ||
| expectedBicep = """ | ||
| @description('The location for the resource(s) to be deployed.') | ||
| param location string = resourceGroup().location | ||
|
|
||
| param mykv_outputs_name string | ||
|
|
||
| param principalType string | ||
|
|
||
| param principalId string | ||
|
|
||
| resource mykv 'Microsoft.KeyVault/vaults@2023-07-01' existing = { | ||
| name: mykv_outputs_name | ||
| } | ||
|
|
||
| resource mykv_KeyVaultSecretsUser 'Microsoft.Authorization/roleAssignments@2022-04-01' = { | ||
| name: guid(mykv.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: principalType | ||
| } | ||
| scope: mykv | ||
| } | ||
| """; | ||
| output.WriteLine(kvRolesManifest.BicepText); | ||
| Assert.Equal(expectedBicep, kvRolesManifest.BicepText); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task WithEnvironment_AddsKeyVaultSecretReference() | ||
| { | ||
| // Arrange: Create a test application builder. | ||
| using var builder = TestDistributedApplicationBuilder.Create(); | ||
|
|
||
| // Add a key vault resource. | ||
| var kv = builder.AddAzureKeyVault("myKeyVault"); | ||
|
|
||
| kv.Resource.SecretResolver = (s, ct) => | ||
| { | ||
| return Task.FromResult<string?>("my secret value"); | ||
| }; | ||
|
|
||
| // Get a secret reference from the key vault resource. | ||
| var secretReference = kv.Resource.GetSecret("mySecret"); | ||
|
|
||
| // Add a container resource that supports environment variables. | ||
| var containerBuilder = builder.AddContainer("myContainer", "nginx") | ||
| .WithEnvironment("MY_SECRET", secretReference); | ||
|
|
||
| var env = await containerBuilder.Resource.GetEnvironmentVariableValuesAsync(DistributedApplicationOperation.Run); | ||
|
|
||
| var kvp = Assert.Single(env); | ||
|
|
||
| Assert.Equal("MY_SECRET", kvp.Key); | ||
| Assert.Same("my secret value", kvp.Value); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.