Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Encrypted Standard String #316

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions setup/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ If ($Locale -eq $null) {
}

try {
$RuntimeConfig = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name 'gsaConfigExportLatest' -AsPlainText -ErrorAction Stop | ConvertFrom-Json | Select-Object -Expand runtime
# Get and decrypt the config from Key Vault
$encryptedConfig = Get-AzKeyVaultSecret -VaultName $keyVaultName -Name 'gsaConfigExportLatest' -AsPlainText -ErrorAction Stop
$encryptedBytes = [Convert]::FromBase64String($encryptedConfig)
$decryptedBytes = [System.Security.Cryptography.ProtectedData]::Unprotect(
$encryptedBytes,
$null,
[System.Security.Cryptography.DataProtectionScope]::CurrentUser
)
$configString = [System.Text.Encoding]::UTF8.GetString($decryptedBytes)
$RuntimeConfig = $configString | ConvertFrom-Json
Set-AzContext -SubscriptionId $RuntimeConfig.subscriptionId
}
catch {
Expand Down Expand Up @@ -475,7 +484,3 @@ Add-LogEntry 'Information' "Completed execution of main runbook" -workspaceGuid
# vduHbe/rUCbpQefqNRPCsYhO6dp/k6CH5XGin8lPPIDdRl+LaSY13QYD9rWEeAFo
# A6om4dcNwSng2HswnGtUaDxiDTtAqPv1F5RTFD0ILoHWkDjD4NwHiodDPKn7pbFV
# yOVynr1zu8cGneK2fBidzculEjzOfaASvM/aH/oDSpTrM8ZKKURcEsU+PqxeByn2
# yMExxoMHREyWswmY3LtDgo36H0D1SGJ8OcVHhzGFFV5Q9/u8jodCy2JNH83BuKGh
# 1euy9uKef3TlcDqKCnG2Oaxd6OzqfCTWgWazjQ0M2OZOurZWbXBMVTJuD6GUxNSm
# z8oLhvJYXybSsUZJ6zHql7KukNVheG7WXTrb6Pe0
# SIG # End signature block
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,17 @@
'deployerAzureID' = $config['runtime']['userId']
}

$secretValue = (ConvertTo-SecureString -String (ConvertTo-Json $config -Depth 10) -AsPlainText -Force)
Set-AzKeyVaultSecret -VaultName $config['runtime']['keyVaultName'] -Name $configSecretName -SecretValue $secretValue -Tag $secretTags -ContentType 'application/json' -Verbose:$useVerbose | Out-Null
$jsonConfig = ConvertTo-Json $config -Depth 10
$encryptedBytes = [System.Security.Cryptography.ProtectedData]::Protect(
[System.Text.Encoding]::UTF8.GetBytes($jsonConfig),
$null,
[System.Security.Cryptography.DataProtectionScope]::CurrentUser
)
$encryptedBase64 = [Convert]::ToBase64String($encryptedBytes)
$secureString = ConvertTo-SecureString $encryptedBase64 -AsPlainText -Force
Fixed Show fixed Hide fixed

Set-AzKeyVaultSecret -VaultName $config['runtime']['keyVaultName'] -Name $configSecretName `
-SecretValue $secureString -Tag $secretTags -ContentType 'application/json' -Verbose:$useVerbose | Out-Null

Write-Host "Completed deployment of the Guardrails Solution Accelerator!" -ForegroundColor Green
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Add-Type -AssemblyName System.Security

Function Get-GSAExportedConfig {
<#
.SYNOPSIS
Expand Down Expand Up @@ -45,11 +47,21 @@ Function Get-GSAExportedConfig {
}

try {
[string]$configValue = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name 'gsaConfigExportLatest' -AsPlainText -ErrorAction Stop
$encryptedConfig = Get-AzKeyVaultSecret -VaultName $KeyVaultName -Name 'gsaConfigExportLatest' -AsPlainText -ErrorAction Stop
$encryptedBytes = [Convert]::FromBase64String($encryptedConfig)
$decryptedBytes = [System.Security.Cryptography.ProtectedData]::Unprotect(
$encryptedBytes,
$null,
[System.Security.Cryptography.DataProtectionScope]::CurrentUser
)
$configString = [System.Text.Encoding]::UTF8.GetString($decryptedBytes)

# Return the decrypted config string
[PSCustomObject]@{
configString = $configString
}
}
catch {
Write-Error -Message "Unable to retrieve the latest configuration from the Key Vault. Please ensure that the Key Vault exists and that the latest configuration has been exported. Message: $_" -ErrorAction Stop
Write-Error -Message "Unable to retrieve and decrypt the latest configuration from the Key Vault. Please ensure that the Key Vault exists and that the latest configuration has been exported. Message: $_" -ErrorAction Stop
}

return (New-Object -TypeName PSObject -Property @{configString = $configValue})
}
Loading