Repository navigation
Expand file tree
/
Copy pathSet-ValuesFromKeyVault.ps1
More file actions
47 lines (38 loc) · 1.79 KB
/
Copy pathSet-ValuesFromKeyVault.ps1
File metadata and controls
47 lines (38 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$FileName
)
$keyVaultRegexPattern = '@Microsoft\.KeyVault\(SecretUri=(.+?)\)'
$secretUriPattern = 'SecretUri=([^)]+)'
$jsonContent = Get-Content -Path $FileName -Raw -ErrorAction Stop
$jsonObject = ConvertFrom-Json $jsonContent
# recursive function for retrieving secret values
function Set-SecretPlaceholders($object) {
foreach ($property in $object.PSObject.Properties) {
$propertyValue = $property.Value
# Check if the property value is an object (nested property)
if ($propertyValue -is [System.Management.Automation.PSCustomObject]) {
Set-SecretPlaceholders $propertyValue
} else {
# If the property value is not an object, check for a match with the regex pattern
if ($propertyValue -match $keyVaultRegexPattern) {
# Write-Output "Matched value for property '$($property.Name)': $propertyValue"
$secreUriMatches = $propertyValue | Select-String -Pattern $secretUriPattern
if ($secreUriMatches.Matches.Count -gt 0) {
$secretUri = $secreUriMatches.Matches[0].Groups[1].Value
# Write-Host "Extracted SecretUri: $secretUri"
$secret = Get-AzKeyVaultSecret -VaultName (Get-AzKeyVault -Name (Split-Path $secretUri -Parent)).VaultName -Name (Split-Path $secretUri -Leaf)
$secretValue = ConvertFrom-SecureString $secret.SecretValue -AsPlainText
# Write-Host "Secret Value: $secretValue"
$property.Value = $secretValue
}
}
}
}
}
Set-SecretPlaceholders $jsonObject
# $jsonObject
$json = $jsonObject | ConvertTo-Json
# save result file
Set-Content -Path "out.json" -Value $json | Out-Null