-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/KeyVault/KeyVault.Test/ScenarioTests/KeyVaultTestRunner.cs
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,41 @@ | ||
using Microsoft.Azure.Commands.TestFx; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests | ||
{ | ||
public class KeyVaultTestRunner | ||
{ | ||
protected readonly ITestRunner TestRunner; | ||
|
||
protected KeyVaultTestRunner(ITestOutputHelper output) | ||
{ | ||
TestRunner = TestManager.CreateInstance(output) | ||
.WithNewPsScriptFilename($"{GetType().Name}.ps1") | ||
.WithProjectSubfolderForTests("ScenarioTests") | ||
.WithCommonPsScripts(new[] | ||
{ | ||
@"Common.ps1", | ||
@"../AzureRM.Resources.ps1", | ||
}) | ||
.WithNewRmModules(helper => new[] | ||
{ | ||
helper.RMProfileModule, | ||
helper.GetRMModulePath("AzureRM.KeyVault.psd1"), | ||
}) | ||
.WithNewRecordMatcherArguments( | ||
userAgentsToIgnore: new Dictionary<string, string> | ||
{ | ||
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}, | ||
}, | ||
resourceProviders: new Dictionary<string, string> | ||
{ | ||
{"Microsoft.KeyVault", null}, | ||
} | ||
) | ||
.Build(); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/KeyVault/KeyVault.Test/ScenarioTests/SecretManagementExtensionTests.cs
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,43 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using Microsoft.WindowsAzure.Commands.ScenarioTest; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests | ||
{ | ||
public class SecretManagementExtensionTests : KeyVaultTestRunner | ||
{ | ||
public SecretManagementExtensionTests(Xunit.Abstractions.ITestOutputHelper output) | ||
:base(output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.LiveOnly)] | ||
// Test case cannot be recorded | ||
public void TestExtensionRegister() | ||
{ | ||
TestRunner.RunTestScript("Test-ExtensionRegister"); | ||
} | ||
|
||
[Fact] | ||
[Trait(Category.AcceptanceType, Category.LiveOnly)] | ||
// Test case cannot be recorded | ||
public void TestSecretManagementExtension() | ||
{ | ||
TestRunner.RunTestScript("Test-SecretManagementExtension"); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/KeyVault/KeyVault.Test/ScenarioTests/SecretManagementExtensionTests.ps1
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,47 @@ | ||
# ---------------------------------------------------------------------------------- | ||
# | ||
# Copyright Microsoft Corporation | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ---------------------------------------------------------------------------------- | ||
|
||
<# | ||
.SYNOPSIS | ||
Test register secret management extension | ||
#> | ||
function Test-ExtensionRegister | ||
{ | ||
Register-SecretVault -Name AzKeyVault -ModuleName Az.KeyVault -VaultParameters @{ AZKVaultName = 'xdmkv'; SubscriptionId = '<sub>' } | ||
|
||
Get-SecretVault | ||
|
||
Unregister-SecretVault -Name AzKeyVault | ||
} | ||
|
||
<# | ||
.SYNOPSIS | ||
Test secret management extension function | ||
#> | ||
function Test-SecretManagementExtension | ||
{ | ||
Register-SecretVault -Name AzKeyVault -ModuleName Az.KeyVault -VaultParameters @{ AZKVaultName = 'xdmkv'; SubscriptionId = '<sub>' } | ||
|
||
Get-SecretInfo -Vault AzKeyVault | ||
|
||
Get-Secret -Vault AzKeyVault -Name secret1 | ||
|
||
$secure = ConvertTo-SecureString -String "test" -AsPlainText -Force | ||
Set-Secret -Vault AzKeyVault -Name secret3 -SecureStringSecret $secure | ||
|
||
Get-SecretInfo -Vault AzKeyVault | ||
Remove-Secret -Vault AzKeyVault -Name secret3 | ||
|
||
|
||
} |
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
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
5 changes: 5 additions & 0 deletions
5
src/KeyVault/KeyVault/SecretManagementExtension/SecretManagementExtension.psd1
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,5 @@ | ||
@{ | ||
ModuleVersion = '1.0' | ||
RootModule = '.\SecretManagementExtension.psm1' | ||
FunctionsToExport = @('Set-Secret','Get-Secret','Remove-Secret','Get-SecretInfo','Test-SecretVault') | ||
} |
123 changes: 123 additions & 0 deletions
123
src/KeyVault/KeyVault/SecretManagementExtension/SecretManagementExtension.psm1
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,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 | ||
{ | ||
param ( | ||
[string] $VaultName, | ||
[hashtable] $AdditionalParameters | ||
) | ||
|
||
try | ||
{ | ||
Check-SubscriptionLogIn $AdditionalParameters.SubscriptionId $AdditionalParameters.AZKVaultName | ||
} | ||
catch | ||
{ | ||
Write-Error $_ | ||
return $false | ||
} | ||
|
||
return $true | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.