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

Secret Management Extension for AKV #13506

Merged
merged 4 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@{
ModuleVersion = '1.0'
RootModule = '.\SecretManagementExtension.psm1'
RootModule = '.\Az.KeyVault.Extension.psm1'
FunctionsToExport = @('Set-Secret','Get-Secret','Remove-Secret','Get-SecretInfo','Test-SecretVault')
}
290 changes: 290 additions & 0 deletions src/KeyVault/KeyVault/Az.KeyVault.Extension/Az.KeyVault.Extension.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

function Check-SubscriptionLogIn
{
param (
[string] $SubscriptionId,
[string] $AzKVaultName
)

$azContext = Az.Accounts\Get-AzContext
if (($azContext -eq $null) -or ($azContext.Subscription.Id -ne $SubscriptionId))
{
throw "To use ${AzKVaultName} Azure vault, the current user must be logged into Azure account subscription ${SubscriptionId}. Run 'Connect-AzAccount -SubscriptionId ${SubscriptionId}'."
BethanyZhou marked this conversation as resolved.
Show resolved Hide resolved
}
}

function Get-Secret
{
param (
[string] $Name,
[string] $VaultName,
[hashtable] $AdditionalParameters
)

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

$secret = Az.KeyVault\Get-AzKeyVaultSecret -Name $Name -VaultName $AdditionalParameters.AZKVaultName
if ($secret -ne $null)
{
switch ($secret.ContentType) {
'ByteArray'
{
$SecretValue = Get-ByteArray $Secret
}
'String'
{
$SecretValue = Get-String $Secret
}

'PSCredential'
{
Get-PSCredential $Secret
}
'Hashtable'
{
Get-Hashtable $Secret
}
Default
{
$SecretValue = Get-SecureString $Secret
}
}
return $SecretValue
}
}

function Get-ByteArray
{
param (
[Parameter(Mandatory=$true, Position=0)]
[object] $Secret
)
$secretValueText = Get-String $Secret
return [System.Text.Encoding]::ASCII.GetBytes($secretValueText)
}

function Get-String
{
param (
[Parameter(Mandatory=$true, Position=0)]
[object] $Secret
)

$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Secret.SecretValue)
try {
$secretValueText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
} finally {
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}
return $secretValueText
}

function Get-SecureString
{
param (
[Parameter(Mandatory=$true, Position=0)]
[object] $Secret
)

return $Secret.SecretValue
}

function Get-PSCredential
{
param (
[Parameter(Mandatory=$true, Position=0)]
[object] $Secret
)

$secretHashTable = Get-Hashtable $Secret
return [System.Management.Automation.PSCredential]::new($secretHashTable["UserName"], ($secretHashTable["Password"] | ConvertTo-SecureString -AsPlainText -Force))
}

function Get-Hashtable
{
param (
[Parameter(Mandatory=$true, Position=0)]
[object] $Secret
)

$jsonObject = Get-String $Secret | ConvertFrom-Json
$hashtable = @{}
$jsonObject.psobject.Properties | foreach { $hashtable[$_.Name] = $_.Value }
return $hashtable
}

function Set-Secret
{
param (
[string] $Name,
[object] $Secret,
[string] $VaultName,
[hashtable] $AdditionalParameters
)

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

switch ($Secret.GetType().Name) {
'Byte[]'
{
Set-ByteArray -Name $Name -Secret $Secret -AZKVaultName $AdditionalParameters.AZKVaultName -ContentType 'ByteArray'
}
'String'
{
Set-String -Name $Name -Secret $Secret -AZKVaultName $AdditionalParameters.AZKVaultName -ContentType 'String'
}
'SecureString'
{
Set-SecureString -Name $Name -Secret $Secret -AZKVaultName $AdditionalParameters.AZKVaultName -ContentType 'SecureString'
}
'PSCredential'
{
Set-PSCredential -Name $Name -Secret $Secret -AZKVaultName $AdditionalParameters.AZKVaultName -ContentType 'PSCredential'
}
'Hashtable'
{
Set-Hashtable -Name $Name -Secret $Secret -AZKVaultName $AdditionalParameters.AZKVaultName -ContentType 'Hashtable'
}
Default
{
throw "Invalid type. Types supported: byte[], string, SecureString, PSCredential, Hashtable";
}
}

return $?
}

function Set-ByteArray
{
param (
[string] $Name,
[Byte[]] $Secret,
[string] $AZKVaultName,
[string] $ContentType
)

$SecretString = [System.Text.Encoding]::ASCII.GetString($Secret)
Set-String -Name $Name -Secret $SecretString -AZKVaultName $AZKVaultName -ContentType $ContentType
}

function Set-String
{
param (
[string] $Name,
[string] $Secret,
[string] $AZKVaultName,
[string] $ContentType
)
$SecureSecret = ConvertTo-SecureString -String $Secret -AsPlainText -Force
$null = Az.KeyVault\Set-AzKeyVaultSecret -Name $Name -SecretValue $SecureSecret -VaultName $AZKVaultName -ContentType $ContentType
}

function Set-SecureString
{
param (
[string] $Name,
[SecureString] $Secret,
[string] $AZKVaultName,
[string] $ContentType
)

$null = Az.KeyVault\Set-AzKeyVaultSecret -Name $Name -SecretValue $Secret -VaultName $AZKVaultName -ContentType $ContentType
}

function Set-PSCredential
{
param (
[string] $Name,
[PSCredential] $Secret,
[string] $AZKVaultName,
[string] $ContentType
)
$secretHashTable = @{"UserName" = $Secret.UserName; "Password" = $Secret.GetNetworkCredential().Password}
$SecretString = ConvertTo-Json $secretHashTable
Set-String -Name $Name -Secret $SecretString -AZKVaultName $AZKVaultName -ContentType $ContentType
}

function Set-Hashtable
{
param (
[string] $Name,
[Hashtable] $Secret,
[string] $AZKVaultName,
[string] $ContentType
)
$SecretString = ConvertTo-Json $Secret
Set-String -Name $Name -Secret $SecretString -AZKVaultName $AZKVaultName -ContentType $ContentType
}

function Remove-Secret
{
param (
[string] $Name,
[string] $VaultName,
[hashtable] $AdditionalParameters
)

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

$null = Az.KeyVault\Remove-AzKeyVaultSecret -Name $Name -VaultName $AdditionalParameters.AZKVaultName -Force
return $?
}

function Get-SecretInfo
{
param (
[string] $Filter,
[string] $VaultName,
[hashtable] $AdditionalParameters
)

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

if ([string]::IsNullOrEmpty($Filter))
{
$Filter = "*"
}

$pattern = [WildcardPattern]::new($Filter)

$vaultSecretInfos = Az.KeyVault\Get-AzKeyVaultSecret -VaultName $AdditionalParameters.AZKVaultName

foreach ($vaultSecretInfo in $vaultSecretInfos)
{
if ($pattern.IsMatch($vaultSecretInfo.Name))
{
if($vaultSecretInfo.ContentType -eq $null)
{
$vaultSecretInfo.ContentType = 'Unknown'
}
Write-Output (
[Microsoft.PowerShell.SecretManagement.SecretInformation]::new(
$vaultSecretInfo.Name,
[System.Enum]::Parse([Microsoft.PowerShell.SecretManagement.SecretType], $vaultSecretInfo.ContentType),
$VaultName)
)
}
}
}

function Test-SecretVault
{
param (
[string] $VaultName,
[hashtable] $AdditionalParameters
)

try
{
Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName
}
catch
{
Write-Error $_
return $false
}

return $true
}
2 changes: 1 addition & 1 deletion src/KeyVault/KeyVault/Az.KeyVault.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ RequiredAssemblies = 'Microsoft.Azure.KeyVault.dll',
FormatsToProcess = 'KeyVault.format.ps1xml'

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('Microsoft.Azure.PowerShell.Cmdlets.KeyVault.dll')
NestedModules = @('Microsoft.Azure.PowerShell.Cmdlets.KeyVault.dll', '.\Az.KeyVault.Extension')
dingmeng-xue marked this conversation as resolved.
Show resolved Hide resolved

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @()
Expand Down
2 changes: 2 additions & 0 deletions src/KeyVault/KeyVault/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Additional information about change #1
-->
## Upcoming Release
* Supported new version of SecretManagement module [#13366]
* Supported ByteArray, String, PSCredential and Hashtable for `SecretValue` in SecretManagementModule [#12190]

## Version 3.0.0
* [Breaking Change] Deprecated parameter DisableSoftDelete in `New-AzKeyVault` and EnableSoftDelete in `Update-AzKeyVault`
Expand Down
2 changes: 1 addition & 1 deletion src/KeyVault/KeyVault/KeyVault.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<None Update="SecretManagementExtension\*" CopyToOutputDirectory="PreserveNewest" />
<None Update="Az.KeyVault.Extension\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading