Adopt certificate approach in AKV config provider topic
Key Vault Sample App: https://github.com/guardrex/KeyVaultSample
This is the sample app used with the topic. Leave the app in it's Basic configuration, which uses (provided in the app's appsettings.json file):
KeyVaultNameAzureADApplicationIdAzureADPassword(Client Secret)
The Key Vault is established using the Azure Cloud Shell with secrets used by the sample app:
az group create --name "{RESOURCE GROUP NAME}" --location {LOCATION}
az keyvault create --name "{KEY VAULT NAME}" --resource-group "{RESOURCE GROUP NAME}" --location {LOCATION}
az keyvault secret set --vault-name "{KEY VAULT NAME}" --name "SecretName" --value "secret_value_1_prod"
az keyvault secret set --vault-name "{KEY VAULT NAME}" --name "Section--SecretName" --value "secret_value_2_prod"
The app is registered as an external app (hosted on a local machine) with Azure AD using the App registration experience in the portal. An Application Id and Password (Client Secret) are obtained.
The appsettings.json file receives the key vault name, AppID, and Secret.
What we'd like to do in the topic is stick with Azure CLI commands in cloud shell to permit this app to access the key vault. The following command works for an MSI app, but it doesn't work in the scenario that I've described for a non-Azure-hosted app. Using the app's Azure AD ObjectID, the following command doesn't work in this scenario ...
az keyvault set-policy --name '{KEY VAULT NAME}' --object-id {OBJECT ID} --secret-permissions get list
Here's what does work: I can use the portal process to give this app access to that key vault ...
- Navigate to Key vaults in the Azure portal.
- Select the key vault.
- Select Access policies.
- Select Add new.
- Select Select principal and select the registered app by name. Select the Select button.
- Open Secret permissions and provide the app with Get and List permissions.
- Select OK.
- Select Save.
:+1: ...... that's fine ...... but we would prefer not to maintain portal steps in the topic. We'd prefer to stick with Azure CLI/Clould Shell all the way.
Question: What's preventing the following command from allowing this app to access the key vault via it's ObjectID?
az keyvault set-policy --name '{KEY VAULT NAME}' --object-id {OBJECT ID} --secret-permissions get list
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 2cc0b77a-df21-7007-5cec-4399d98c6dc6
- Version Independent ID: efe5ae5e-ff64-f1c7-6920-4512fc8f36d1
- Content: Azure Key Vault configuration provider in ASP.NET Core
- Content Source: aspnetcore/security/key-vault-configuration.md
- Product: aspnet-core
- GitHub Login: @guardrex
- Microsoft Alias: riande
Moving from #10669
Another most important point is we don't want to demonstrate to customers how to authenticate to Key Vault with Client ID and Secret. We want them to authenticate with MSI (most preferred) and if not we want them to use Client ID and certificate (which gets rotated every so often)
You can look at this intro tutorial - https://docs.microsoft.com/en-us/azure/key-vault/quick-create-net or one of the VM tutorials - https://docs.microsoft.com/en-us/azure/key-vault/tutorial-net-windows-virtual-machine
Thansk @prashanthyv ... I'll convert the Client ID/Password (Client Secret) approach over to using a certificate in the non-Azure-hosted case. ~We'll need to adopt the self-signed/non-automatic-renewal approach~ ([EDIT] that doesn't matter ... that's up to the dev), but I'll link your cert topics into the topic.
I'll add cross-links to the tutorials. I may already have the Azure Apps tutorial and probably only need to add the VM experience.
Cross-ref: https://docs.microsoft.com/en-us/azure/key-vault/about-keys-secrets-and-certificates
@prashanthyv I converted over to using a certificate:
-
Allowed AKV to generate the certificate. Downloaded it to my local machine.
-
Dropped it into the app ... wired it up to
AddAzureKeyVault...config.AddAzureKeyVault( $"https://{builtConfig["KeyVaultName"]}.vault.azure.net/", builtConfig["AzureADApplicationId"], new X509Certificate2("TestCert.pfx")); -
In Azure AD, provided its Key > Public Key ...
... It works except that the original problem remains.
The CLI command ...
az keyvault set-policy --name '{KEY VAULT NAME}' --object-id {OBJECT ID} --secret-permissions get list
... doesn't work to register the app to use the KV. Again, I tried both the app's Application ID and its Object ID with that command.
I had to use the portal again. When using the portal, I get the app registered with Get and List permissions, it shows up in Access Policies, and the app runs normally.
So, I'm still stuck trying to get the access policy registration set using the az keyvault set-policy command.
@blowdart Quick pair of ❓.
This ...
// using System.Linq;
// using System.Security.Cryptography.X509Certificates;
// using Microsoft.Extensions.Configuration;
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var builtConfig = config.Build();
using (var store = new X509Store(StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadOnly);
var cert = store.Certificates
.Find(X509FindType.FindByThumbprint,
builtConfig["CertificateThumbprint"], false);
config.AddAzureKeyVault(
builtConfig["KeyVaultName"],
builtConfig["AzureADApplicationId"],
cert.OfType<X509Certificate2>().Single());
store.Close();
}
})
.UseStartup<Startup>();
... will :boom: xplat, correct? If so, no worries, I'll mark the example Windows OS only.
2nd ❓ ... There's a prominent warning in the X509Store API doc calling for explicit or implicit disposal ...
https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509store?view=netcore-2.2#remarks
The examples shown above that notice don't do it, and I didn't have the original code (shown in the live topic today) do it. I'm planning on using the approach you see above (using) in my updates today. Does that :point_up: look good?
I'm in training most of this week and next. @bartonjs can you comment on the cert cross platform and the persistence?
I'm not sure what the question in the first part is. If it's "do certificates get added to CurrentUser\My on Linux in Azure?" the answer is "I dunno; that's really an Azure deployment question" :smile:
As for disposing X509Store: Putting it in a using statement is good, yes. (Current documentation might not have since most of ASP.Net was targeting net45x, and X509Store became IDisposable in net46).
Other feedback:
var cert=>var certs(it's a collection).new X509Store(StoreLocation.CurrentUser)=>new X509Store(StoreName.My, StoreLocation.CurrentUser)(people sometimes get confused by the former, thinking it's a meta-store of all user stores, or something).
Thanks @bartonjs ... I'll make those code changes.
WRT the first part ... no ... not Azure. Actually, this code (using a certificate) is only for use off of Azure. @prashanthyv has advised us to have apps use a cert for authentication against Azure AD in the non-Azure-hosted scenario (as opposed to using a Password/Client Secret).
I just need to know if this code will fail on Linux and Mac, and I think the answer is yes guessing that there is no exact xplat X509Store scenario for this code to work on with those platforms (but I don't really know). There might be a way to reach into a Linux or Mac cert store and nab the cert for use like this (i.e., to avoid having to drop the cert right into the app's content root), but that's going to have to be for another day even if possible to do. I'm happy with just the Windows approach here today, and we can address Linux/Mac a little later.
Linux and macOS both support reading certificates out of new X509Store(StoreName.My, StoreLocation.CurrentUser); so it's a fine general-purpose guideline.
@Rick-Anderson Do you want me to go ahead an put in what I have for this to make it use a certificate for the auth step? We could at least get that published and move readers away from the ClientID/Secret approach. There's also a tiny bug fix or two for the topic. I have it all sitting on a branch ready to go.
WRT the other bit ... the app accessing the KV without using the portal ... I still don't have an answer on how to do that via the Cloud Shell. The az keyvault set-policy command just doesn't work AFAICT. We can stick with the portal for now ... it works.