-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple script to set value of secrets to be the identical (#8091)
We have some unnecessary duplication of secrets, and the secret manager doesn't have a great way to handle this today.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,60 @@ | ||
<# | ||
.SYNOPSIS | ||
Sets a secret in one vault to be the same as another. Temporary workaround for issues in secret | ||
layout. Assumes that the Keyvaults are in the same subscription and that the current context | ||
is set to that subscription. | ||
.PARAMETER SourceVaultName | ||
Vault where the source secret is located | ||
.PARAMETER SourceSecretName | ||
Name of source secret | ||
.PARAMETER DestinationVaultName | ||
Vault where the destination secret is located | ||
.PARAMETER DestinationSecretName | ||
Name of destination secret. If not specified, the source secret name is used. | ||
#> | ||
|
||
param ( | ||
[Parameter(Mandatory=$true)] | ||
[string]$SourceVaultName, | ||
[Parameter(Mandatory=$true)] | ||
[string]$SourceSecretName, | ||
[Parameter(Mandatory=$true)] | ||
[string]$DestinationVaultName, | ||
[string]$DestinationSecretName | ||
) | ||
|
||
if (!$DestinationSecretName) { | ||
$DestinationSecretName = $SourceSecretName | ||
} | ||
|
||
$sourceSecretInfo = Get-AzKeyVaultSecret -VaultName $SourceVaultName -Name $SourceSecretName | ||
$destinationSecretInfo = Get-AzKeyVaultSecret -VaultName $DestinationVaultName -Name $DestinationSecretName | ||
|
||
if (!$sourceSecretInfo) { | ||
Write-Error "Could not find source secret '$SourceSecretName' in '$SourceVaultName'" | ||
return | ||
} | ||
|
||
if (!$destinationSecretInfo) { | ||
Write-Error "Could not find destination secret '$DestinationSecretName' in '$DestinationVaultName'" | ||
return | ||
} | ||
|
||
if ($($sourceSecretInfo.SecretValue | ConvertFrom-SecureString -AsPlainText) -eq $($destinationSecretInfo.SecretValue | ConvertFrom-SecureString -AsPlainText)) { | ||
Write-Host "Secrets are already the same, not updating." | ||
return | ||
} | ||
|
||
$newDestinationSecret = Set-AzKeyVaultSecret -VaultName $DestinationVaultName -Name $DestinationSecretName -SecretValue $sourceSecretInfo.SecretValue -Expires $sourceSecretInfo.Expires -Tag $sourceSecretInfo.Tags -ContentType $sourceSecretInfo.ContextType | ||
|
||
if ($($sourceSecretInfo.SecretValue | ConvertFrom-SecureString -AsPlainText) -ne $($newDestinationSecret.SecretValue | ConvertFrom-SecureString -AsPlainText)) { | ||
Write-Error "Destination secret was not correctly updated" | ||
return | ||
} else { | ||
Write-Host "Destination secret '$DestinationSecretName' in '$DestinationVaultName' is now the same as '$SourceSecretName' in '$SourceVaultName'" | ||
} |