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

Support Secret Management Extension #11470

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/KeyVault/KeyVault/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
- Additional information about change #1
-->
## Upcoming Release
* Added support to Microsoft.PowerShell.SecretManagement

## Version 1.5.2

* Added breaking change attributes to `New-AzKeyVault`

## Version 1.5.1
Expand Down
4 changes: 4 additions & 0 deletions src/KeyVault/KeyVault/KeyVault.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
<PackageReference Include="Microsoft.Azure.KeyVault.WebKey" Version="3.0.1" />
<PackageReference Include="Microsoft.Azure.Management.KeyVault" Version="2.4.2" />
</ItemGroup>

<ItemGroup>
<None Update="SecretManagementExtension\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@{
ModuleVersion = '1.0'
RootModule = '.\SecretManagementExtension.psm1'
FunctionsToExport = @('Set-Secret','Get-Secret','Remove-Secret','Get-SecretInfo','Test-SecretVault')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

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

Import-Module -Name Az.Accounts

$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}'."
}
}

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

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

Import-Module -Name Az.KeyVault

$secret = Az.KeyVault\Get-AzKeyVaultSecret -Name $Name -VaultName $AdditionalParameters.AZKVaultName
if ($secret -ne $null)
{
return $secret.SecretValue
}
}

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

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

Import-Module -Name Az.KeyVault

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

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

Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName

Import-Module -Name Az.KeyVault

$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

Import-Module -Name Az.KeyVault

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))
{
Write-Output (
[Microsoft.PowerShell.SecretManagement.SecretInformation]::new(
$vaultSecretInfo.Name,
[Microsoft.PowerShell.SecretManagement.SecretType]::SecureString,
$VaultName)
)
}
}
}

function Test-SecretVault
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the vault doesn't exist, should Test-SecretVault return false?

Copy link
Member Author

@dingmeng-xue dingmeng-xue Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I went through code, I think it makes sense because it's possible that user register vault at the first and then create vault on Azure. There is no error from Get-AzKeyVault when user get vault info from an non-existing vault.

{
param (
[string] $VaultName,
[hashtable] $AdditionalParameters
)

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

return $true
}
14 changes: 9 additions & 5 deletions tools/CleanupBuild.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ foreach($RMPath in $resourceManagerPaths)
$acceptedDlls += $assembly.Split("\")[-1]
}

Write-Verbose "Removing redundant dlls in $($RMFolder.Name)"
Write-Host "Removing redundant dlls in $($RMFolder.Name)"
$removedDlls = Get-ChildItem -Path $RMFolder.FullName -Filter "*.dll" -Recurse | where { $acceptedDlls -notcontains $_.Name -and !$_.FullName.Contains("Assemblies") }
$removedDlls | % { Write-Verbose "Removing $($_.Name)"; Remove-Item $_.FullName -Force }
$removedDlls | % { Write-Host "Removing $($_.Name)"; Remove-Item $_.FullName -Force }

Write-Verbose "Removing scripts and psd1 in $($RMFolder.FullName)"
Write-Host "Removing scripts and psd1 in $($RMFolder.FullName)"

$removedPsd1 = Get-ChildItem -Path "$($RMFolder.FullName)" -Include "*.psd1" -Exclude "PsSwaggerUtility*.psd1" -Recurse | where { $_.FullName -ne "$($RMFolder.FullName)$([IO.Path]::DirectorySeparatorChar)$($RMFolder.Name).psd1" }
$removedPsd1 | % { Write-Verbose "Removing $($_.FullName)"; Remove-Item $_.FullName -Force }
$exludedPsd1 = @(
"PsSwaggerUtility*.psd1",
"SecretManagementExtension.psd1"
)
$removedPsd1 = Get-ChildItem -Path "$($RMFolder.FullName)" -Include "*.psd1" -Exclude $exludedPsd1 -Recurse | where { $_.FullName -ne "$($RMFolder.FullName)$([IO.Path]::DirectorySeparatorChar)$($RMFolder.Name).psd1" }
$removedPsd1 | % { Write-Host "Removing $($_.FullName)"; Remove-Item $_.FullName -Force }
}
}
10 changes: 8 additions & 2 deletions tools/NewHelpIndex.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ $HelpFolders = @()

$resourceManagerPath = "$PSScriptRoot/../artifacts/$BuildConfig/"

$RMpsd1s += Get-ChildItem -Path $resourceManagerPath -Depth 2 | Where-Object { $_.Name -like "*.psd1" -and $_.FullName -notlike "*dll-Help*" }
$RMpsd1s += Get-ChildItem -Path $resourceManagerPath -Depth 2 | Where-Object {
$_.Name -like "*.psd1" -and $_.FullName -notlike "*dll-Help*" -and $_.Name -ne "SecretManagementExtension.psd1"
}

$HelpFolders += Get-ChildItem -Path "$PSScriptRoot/../src" -Recurse -Directory | where { $_.Name -eq "help" -and $_.FullName -notlike "*\Stack\*" -and $_.FullName -notlike "*\bin\*"}

Expand All @@ -93,7 +95,11 @@ $RMpsd1s | ForEach-Object {

$outputCmdlets = @{}

$parsedPsd1.CmdletsToExport | ForEach-Object {
$cmdletsToExport = $parsedPsd1.CmdletsToExport | Where-Object { $_ }
$functionsToExport = $parsedPsd1.FunctionsToExport | Where-Object { $_ }
$cmdletsToExport = $cmdletsToExport + $functionsToExport

$cmdletsToExport | ForEach-Object {
$cmdletHelpFile = $HelpFileMapping["$_.md"]
if ($cmdletHelpFile -eq $null -and $Target -eq "Latest")
{
Expand Down