How to update client new secret in Azure key vault

201 Views Asked by At

My client secret is expired so I need to update the secret, how can I update secret in Azure key vault in Azure data factory using azure portal.

How to update new client secret in Azure key vault, and how to set expired dates

1

There are 1 best solutions below

0
Sridevi On

Initially, I registered one Azure AD application and created client secret in it like this:

enter image description here

In my Azure key vault, I added one secret with above value like this:

enter image description here

To create new client secret in Azure AD app registration and update key vault secret value, you can run below PowerShell script in Azure Cloud Shell:

$resourceGroupName = "YourRGname"
$keyVaultName = "YourKVname"
$secretName = "YourKBsecretname"
$appName = "YourAzureADappName"

$existingSecret = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName -AsPlainText
Write-Output "Existing secret value: $existingSecret"

$customKeyIdentifier = "newSecret"
$customKeyIdentifierBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($customKeyIdentifier))

$clientSecret = New-AzADAppCredential -DisplayName $appName -CustomKeyIdentifier $customKeyIdentifierBase64 -EndDate (Get-Date).AddYears(1) 
$secretValue = ConvertTo-SecureString -String $clientSecret.SecretText -AsPlainText -Force

Set-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName -SecretValue $secretValue
Write-Output "New client secret created and updated in Key Vault successfully."

$newSecret = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name $secretName -AsPlainText
Write-Output "New secret value: $newSecret"

Response:

enter image description here

To confirm that, I checked the same in Portal where new client secret is created in Azure AD app like this:

enter image description here

In Azure Key vault also, secret updated successfully with new client secret value by creating new version as below:

enter image description here