This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from PaulHigin/initial_secrets_module
Implementation of supported secret types for CredMan
- Loading branch information
Showing
8 changed files
with
3,300 additions
and
140 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
Modules/Microsoft.PowerShell.SecretsManagement/scripts/Install-SecretsManagement.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,35 @@ | ||
# | ||
# Script to install SecretsManagement prototype module | ||
# | ||
|
||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory=$true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $ModuleInstallPath, | ||
|
||
[switch] $Force | ||
) | ||
|
||
$fullInstallPath = Join-Path (Resolve-Path -Path $ModuleInstallPath) "Microsoft.PowerShell.SecretsManagement" | ||
|
||
if (! (Test-Path -Path $fullInstallPath)) | ||
{ | ||
[System.IO.Directory]::CreateDirectory($fullInstallPath) | ||
} | ||
else | ||
{ | ||
if (! $Force) | ||
{ | ||
throw "Module path already exists. Use -Force to install over existing." | ||
} | ||
|
||
Remove-Item -Path (Join-Path $fullInstallPath '*') -Recurse -Force | ||
} | ||
|
||
$sourcePath = "\\scratch2\scratch\paulhi\Modules\Microsoft.PowerShell.SecretsManagement" | ||
Copy-Item -Path (Join-Path $sourcePath "en-us") -Dest $fullInstallPath -Recurse | ||
Copy-Item -Path (Join-Path $sourcePath "Microsoft.PowerShell.SecretsManagement.dll") -Dest $fullInstallPath | ||
Copy-Item -Path (Join-Path $sourcePath "Microsoft.PowerShell.SecretsManagement.pdb") -Dest $fullInstallPath | ||
Copy-Item -Path (Join-Path $sourcePath "Microsoft.PowerShell.SecretsManagement.psd1") -Dest $fullInstallPath | ||
Copy-Item -Path (Join-Path $sourcePath "Microsoft.PowerShell.SecretsManagement.format.ps1xml") -Dest $fullInstallPath |
186 changes: 186 additions & 0 deletions
186
Modules/Microsoft.PowerShell.SecretsManagement/scripts/Register-AzureVaultExtension.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,186 @@ | ||
# | ||
# Script to install: | ||
# Azure KeyVault module | ||
# Microsoft.PowerShell.SecretsManagement (prototype) module | ||
# and register an Azure vault extention to the PowerShell Secrets Manager | ||
# | ||
|
||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory=$true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $SecretManagerModulePath, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $AzVaultName, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $ExtensionVaultName, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $SubscriptionId, | ||
|
||
[switch] $Force | ||
) | ||
|
||
function CheckModuleInstallation | ||
{ | ||
param ( | ||
[string] $ModuleName, | ||
[switch] $Force | ||
) | ||
|
||
if ((Get-Module -Name $ModuleName -ListAvailable) -eq $null) | ||
{ | ||
$shouldContinue = $Force | ||
if (! $shouldContinue) | ||
{ | ||
$shouldContinue = $PSCmdlet.ShouldContinue("The required $ModuleName module is not installed. Install it now?", "Register-AzureVaultExtension") | ||
} | ||
|
||
if ($shouldContinue) | ||
{ | ||
Install-Module -Name $ModuleName -Force | ||
} | ||
} | ||
|
||
if ((Get-Module -Name $ModuleName -ListAvailable) -eq $null) | ||
{ | ||
throw "Unable to find the $ModuleName module. $ModuleName module is needed to register and use Azure vaults." | ||
} | ||
} | ||
|
||
# Ensure Az.Accounts module is installed | ||
CheckModuleInstallation Az.Accounts $Force | ||
|
||
# Ensure Az.KeyVault extension module is installed | ||
CheckModuleInstallation Az.KeyVault $Force | ||
$extensionVaultModulePath = ((Get-Module -Name Az.KeyVault -ListAvailable )[0] | select Path).Path | ||
|
||
# Import Secrets Management module | ||
Import-Module -Name $SecretManagerModulePath -Force | ||
|
||
# Register extension vault template | ||
$RegisterAzSecretsVaultTemplate = @' | ||
Register-SecretsVault -Name {0} ` | ||
-ModulePath '{1}' ` | ||
-GetSecretScript {{ | ||
param ([string] $Name, | ||
[string] $VaultName, | ||
[string] $SubscriptionId | ||
) | ||
Import-Module -Name Az.KeyVault -Force | ||
Import-Module -Name Az.Accounts -Force | ||
# $VerbosePreference = "Continue" | ||
Write-Verbose "Checking for Azure subscription" | ||
$azContext = Az.Accounts\Get-AzContext | ||
if (! $azContext -or ($azContext.Subscription.Id -ne $SubscriptionId)) | ||
{{ | ||
Write-Warning "Log into Azure account for Subscription: $SubscriptionId" | ||
Az.Accounts\Connect-AzAccount -Subscription $SubscriptionId | ||
}} | ||
if ([string]::IsNullOrEmpty($Name)) | ||
{{ | ||
$Name = "*" | ||
}} | ||
# Return all secrets that match Name pattern | ||
if ([WildcardPattern]::ContainsWildcardCharacters($Name)) | ||
{{ | ||
$pattern = [WildcardPattern]::new($Name) | ||
$vaultSecretInfos = Az.KeyVault\Get-AzKeyVaultSecret -VaultName $VaultName | ||
foreach ($vaultSecretInfo in $vaultSecretInfos) | ||
{{ | ||
if ($pattern.IsMatch($vaultSecretInfo.Name)) | ||
{{ | ||
$secret = Az.KeyVault\Get-AzKeyVaultSecret -VaultName $VaultName -Name $vaultSecretInfo.Name | ||
Write-Output ([pscustomobject] @{{ | ||
Name = $secret.Name | ||
Value = $secret.SecretValue | ||
}}) | ||
}} | ||
}} | ||
return | ||
}} | ||
# Return single Name match value | ||
$secret = Az.KeyVault\Get-AzKeyVaultSecret -VaultName $VaultName -Name $Name | ||
if ($secret -ne $null) | ||
{{ | ||
Write-Output ([pscustomobject] @{{ | ||
Name = $secret.Name | ||
Value = $secret.SecretValue | ||
Vault = $VaultName | ||
}}) | ||
}} | ||
}} -GetSecretParameters @{{ | ||
VaultName = '{2}' | ||
SubscriptionId = '{3}' | ||
}} -SetSecretScript {{ | ||
param ([string] $Name, | ||
[object] $SecretToWrite, | ||
[string] $VaultName, | ||
[string] $SubscriptionId | ||
) | ||
if (! ($SecretToWrite -is [securestring])) | ||
{{ | ||
throw "AzKeyVault only supports SecureString secret data types." | ||
}} | ||
Import-Module -Name Az.Accounts -Force | ||
Import-Module -Name Az.KeyVault -Force | ||
# $VerbosePreference = "Continue" | ||
Write-Verbose "Checking for Azure subscription" | ||
$azContext = Az.Accounts\Get-AzContext | ||
if (! $azContext -or ($azContext.Subscription.Id -ne $SubscriptionId)) | ||
{{ | ||
Write-Warning "Log into Azure account for Subscription: $SubscriptionId" | ||
Az.Accounts\Connect-AzAccount -Subscription $SubscriptionId | ||
}} | ||
Az.KeyVault\Set-AzKeyVaultSecret -VaultName $VaultName -Name $Name -SecretValue $SecretToWrite | ||
}} -SetSecretParameters @{{ | ||
VaultName = '{2}' | ||
SubscriptionId = '{3}' | ||
}} -RemoveSecretScript {{ | ||
param ([string] $Name, | ||
[string] $VaultName, | ||
[string] $SubscriptionId | ||
) | ||
Import-Module -Name Az.Accounts -Force | ||
Import-Module -Name Az.KeyVault -Force | ||
# $VerbosePreference = "Continue" | ||
Write-Verbose "Checking for Azure subscription" | ||
$azContext = Az.Accounts\Get-AzContext | ||
if (! $azContext -or ($azContext.Subscription.Id -ne $SubscriptionId)) | ||
{{ | ||
Write-Warning "Log into Azure account for Subscription: $SubscriptionId" | ||
Az.Accounts\Connect-AzAccount -Subscription $SubscriptionId | ||
}} | ||
Az.KeyVault\Remove-AzKeyVaultSecret -VaultName $VaultName -Name $Name -Force | ||
}} -RemoveSecretParameters @{{ | ||
VaultName = '{2}' | ||
SubscriptionId = '{3}' | ||
}} | ||
'@ | ||
|
||
$RegisterAzSecretsVaultScript = $RegisterAzSecretsVaultTemplate -f $ExtensionVaultName, $extensionVaultModulePath, $AzVaultName, $SubscriptionId | ||
|
||
Write-Output "Registering AZ vault: $ExtensionVaultName" | ||
$sb = [scriptblock]::Create($RegisterAzSecretsVaultScript) | ||
$sb.Invoke() |
51 changes: 51 additions & 0 deletions
51
...oft.PowerShell.SecretsManagement/src/Microsoft.PowerShell.SecretsManagement.format.ps1xml
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,51 @@ | ||
<Configuration> | ||
<ViewDefinitions> | ||
<View> | ||
<Name>VaultInfo</Name> | ||
<ViewSelectedBy> | ||
<TypeName>Microsoft.PowerShell.SecretsManagement.SecretsVaultInfo</TypeName> | ||
</ViewSelectedBy> | ||
<TableControl> | ||
<TableHeaders> | ||
<TableColumnHeader> | ||
<Label>Name</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>ModuleName</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>SupportsGet</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>SupportsSet</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>SupportsRemove</Label> | ||
</TableColumnHeader> | ||
</TableHeaders> | ||
<TableRowEntries> | ||
<TableRowEntry> | ||
<Wrap/> | ||
<TableColumnItems> | ||
<TableColumnItem> | ||
<PropertyName>Name</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>ModuleName</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<ScriptBlock>$_.HaveGetCmdlet -or ($_.GetSecretScript -ne $null)</ScriptBlock> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<ScriptBlock>$_.HaveSetCmdlet -or ($_.SetSecretScript -ne $null)</ScriptBlock> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<ScriptBlock>$_.HaveRemoveCmdlet -or ($_.RemoveSecretScript -ne $null)</ScriptBlock> | ||
</TableColumnItem> | ||
</TableColumnItems> | ||
</TableRowEntry> | ||
</TableRowEntries> | ||
</TableControl> | ||
</View> | ||
</ViewDefinitions> | ||
</Configuration> |
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
Oops, something went wrong.