How to create key vault secret with multiple values in one secret? - azure

I want to create a secret in the azure key vault which will be having multiple keys (like JSON).
e.g-
{
"storageAccountKey":"XXXXX",
"CognitiveServicesKey":"XXXX",
"XXXXXx":"XXXX",
}
is it possible to create?
And then by using PowerShell script, I want to update a particular key in the above-created secret with a new one. Please help me.

Yes, it is possible. You will need to upload the content of a file as a secret. It can be done via the Azure CLI as follow:
az keyvault secret set --name [SECRET-NAME] --vault-name [VAULT_NAME] --file C:\path\to\file.txt --subscription [AZURE-SUBSCRIPTION-ID]

If we will do that....it will reduce large number of calls to the KeyVault services.
I didn't understand how it will be a security threat if we use multiple keys in single secret.

Related

Azure resourcechanges missing KeyVault?

I have to get the history of changes of the key in an Azure KeyVault, like updated expiry date via keyUpdate operation, for example.
Is there any way to get those changes? Are the KeyVault changes even stored by Azure?
Checked the resourcechanges table trough the ResourceGraphManager from the SDK and UI and there are no KeyVault entries at all.
Thanks.
Use the Azure CLI,
List of Key Vault keys will return the details of your keys.
If you want to see the versions of a key using it's ID fetched from above, Show key versions
Then if you want to see it's attributes, show key's details
First make sure you know the name of the Key Vault, have performed an az login and set the subscription az account set -s SUBSCRIPTION_ID

Azure key vault creation error: VaultAlreadyExists - I can't find the existing vault

I'm trying to create a key vault in Azure using this CLI command...
az keyvault create --location $location --name $keyVaultName --resource-group $resourceGroupMainName --output none
But this returns the error...
(VaultAlreadyExists) The vault name '[value of $keyVaultName]' is
already in use. Vault names are globaly unique so it is possible that
the name is already taken. If you are sure that the vault name was not
taken then it is possible that a vault with the same name was recently
deleted but not purged after being placed in a recoverable state. If
the vault is in a recoverable state then the vault will need to be
purged before reusing the name. For more information on soft delete
and purging a vault follow this link
https://go.microsoft.com/fwlink/?linkid=2147740.
So I ran both of these...
az keyvault list
az keyvault list-deleted
And $keyVaultName does not appear in either list. I've asked a colleague to double-check those results but it really doesn't appear. I've also looked in the Manage deleted vaults blade in the portal and that matches the results from the CLI - it's not there.
I also tried to recover the key vault with that name...
(DeletedVaultNotFound) The specified deleted vault '[value of $keyVaultName]' does not exist.
...and to purge a key vault with that name...
No deleted Vault or HSM was found with name [value of $keyVaultName]
So why does Azure think that the name is already in use?
I found an easier way, which is via UI, you can check if the deleted key vault is in the key vaults management page.
I am able to select and purge or recover deleted key vault after clicking the 'manage deleted vaults' hyperlink.
As provided in the comment, Similar to Storage Accounts in Azure, the keyvault is also unique across globally. You can check the similar error code from the docs,
Your attempt to create a new key vault with the specified name has
failed since the name is already in use. If you recently deleted a key
vault with this name, it may still be in the soft deleted state
Vault names and Managed HSM pool names are selected by the user and
are globally unique.
You can verify the existence using Powershell or Rest API
When you create an azure keyvault a soft delete feature is by default enabled which helps the customers to recover their keys and secrets which were accidentally deleted within 90days (default) and for that time period you cannot create another keyvault with the same name as that.
Once soft delete is enabled for Azure Key Vault you cannot disable the soft-delete as it's implemented as a one-way operation and cannot be changed back once enabled. However, You can use the PowerShell cmdlet Remove-AzureRmKeyVault command with the option -InRemovedState and by specifying the location of the deleted key vault with the -Location argument to permanently delete or purge the Azure Key Vault. If you want to permanently delete a key or secret you need to use Remove-AzureKeyVaultKey and Remove-AzureKeyVaultSecret with -InRemovedState parameter. Please refer to How to use Key Vault soft-delete with PowerShell for details.
You can also achieve the same using the Azure CLI. Refer to How to use Key Vault soft-delete with CLI for details.
First, I checked the deleted keyvault and purge which one is creating problem.
az keyvault list-deleted
az keyvault purge --name my-key-vault-dev01

Azure function ZIP Deployment using terraform

I am looking to deploy Azure function by using Terraform, in the same way which is mentioned here in the Ms docs.
https://learn.microsoft.com/en-us/azure/azure-functions/deployment-zip-push
One of my need to store and Storage SAS key to Key Vault. I have seen some exmaple where we can below setting is used, but I can't use sas key without storing to Key Vault.
WEBSITE_RUN_FROM_ZIP = "https://${azurerm_storage_account.this.name}.blob.core.windows.net/${azurerm_storage_container.this.name}/${azurerm_storage_blob.this.name}${data.azurerm_storage_account_sas.this.sas}"
Any suggestion?
Thank you !
For this requirement, you can use the Key Vault reference to get the secrets to the Azure Function like this:
#Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/ec96f02080254f109c51a1f14cdb1931)
Or
#Microsoft.KeyVault(VaultName=myvault;SecretName=mysecret;SecretVersion=ec96f02080254f109c51a1f14cdb1931)
But this means only support system-assigned managed identities. So you need to enable the system-assigned managed identity and assign the right role of the Key Vault to it. Here is more details.

How To Retrieve a Secret From Azure Key Vault Node

I'm looking for a code sample which demonstrates how to connect to an azure key vault and grab a secret from the vault without having to store creds in plain text but instead using certificates and an SPN for authentication to the vault. Has anyone done anything like this before who is willing to shed some light on this?
I'm trying to securely retrieve credentials from azure without ever having to save creds locally, and this has proven to be harder than I originally thought.
yes you can use certificate based authentication while accessing Azure key vault.
Here is a very good article on the same using dot net.
Certificate base authentication
Alternatively Azure Key Vault provides a way to securely store credentials and other keys and secrets, but your code needs to authenticate to Key Vault to retrieve them. Managed Service Identity (MSI) makes solving this problem simpler by giving Azure services an automatically managed identity in Azure Active Directory (Azure AD). You can use this identity to authenticate to any service that supports Azure AD authentication, including Key Vault, without having any credentials in your code.
Run the assign-identity command to create the identity for this application:
az webapp identity assign --name <app_name> --resource-group "<YourResourceGroupName>"
This command is the equivalent of going to the portal and switching Managed service identity to On in the web application properties.
Assign permissions to your application to read secrets from Key Vault
{
"principalId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"type": "SystemAssigned"
}
Then, run this command using the name of your Key Vault and the value of PrincipalId copied from above:
az keyvault set-policy --name '<YourKeyVaultName>' --object-id <PrincipalId> --secret-permissions get
Deploy the Node App to Azure and retrieve the secret value
Deploy your node js app ,After this when you browse https://.azurewebsites.net you can see the secret value. Make sure that you replaced the name with your vault name

Is it possible to create a key vault in Azure via c#

Is it possible to create a Key Vault in Azure via c#? I'm able to manipulate secrets and keys via the KeyVaultClient but I need to
A). See if a key vault already exists and
B). Create a key vault if it doesn't exist.
Yes, you can use the KeyVault management client to list where a key vault exists in an Azure subscription and create the KeyVault if necessary. There is C# SDK for these operations which are against Azure Resource Manager.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.keyvault.vaultsoperationsextensions?view=azure-dotnet

Resources