Skip to content

Commit a37e646

Browse files
[Feature] Add secure configuration store (#16)
- Add a secure configuration store - Added a local testing utility
1 parent 765aca3 commit a37e646

File tree

11 files changed

+370
-0
lines changed

11 files changed

+370
-0
lines changed

src/GitHub/classes/Data/Config.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$script:ConfigTemplate = [pscustomobject]@{
2+
App = [pscustomobject]@{
3+
API = [pscustomobject]@{
4+
BaseURI = 'https://api.github.com' # $script:ConfigTemplate.App.API.BaseURI
5+
Version = '2022-11-28' # $script:ConfigTemplate.App.API.Version
6+
}
7+
Defaults = [pscustomobject]@{} # $script:ConfigTemplate.App.Defaults
8+
}
9+
}
10+
$script:Config = $script:ConfigTemplate
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
$script:SecretVault = [pscustomobject]@{
2+
Name = 'GitHub' # $script:SecretVault.Name
3+
Type = 'Microsoft.PowerShell.SecretStore' # $script:SecretVault.Type
4+
}
5+
$script:Secret = [pscustomobject]@{
6+
Name = 'Config' # $script:Secret.Name
7+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
TOPIC
2+
about_Config
3+
4+
SHORT DESCRIPTION
5+
Provides details about the configuration management functions in the PowerShell Module.
6+
7+
LONG DESCRIPTION
8+
The PowerShell Module provides a set of functions to manage the configuration related to the module.
9+
This configuration is stored in a custom secret vault and can be accessed, modified, saved, and restored
10+
using the provided cmdlets.
11+
12+
Name: SecretVault
13+
Path: \classes\Data\SecretVault.ps1
14+
15+
| Name | Type | Default Value | Description |
16+
| --------------- | -------------- | ---------------------------------- | ----------------------------- |
17+
| SecretVault | pscustomobject | {Name, Type} | |
18+
| SecretVault.Name | string | 'GitHub' | The name of the secret vault. |
19+
| SecretVault.Type | string | 'Microsoft.PowerShell.SecretStore' | The type of the secret vault. |
20+
| Secret | pscustomobject | {Name} | |
21+
| Secret.Name | string | 'Config' | The name of the secret. |
22+
23+
24+
Name: Config
25+
Path: \classes\Data\Config.ps1
26+
27+
| Name | Type | Static Value | Description |
28+
| --------------- | ----------------- | ------------------------ | ------------------------ |
29+
| App | pscustomobject | {API, Defaults} | |
30+
| App.API | pscustomobject | {BaseURI, Version} | |
31+
| App.API.BaseURI | string | 'https://api.github.com' | The GitHub API Base URI. |
32+
| App.API.Version | string | '2022-11-28' | The GitHub API version. |
33+
| App.Defaults | pscustomobject | {} | |
34+
35+
Functions provided in the module:
36+
37+
- Get-GitHubConfig: Fetches the current module configuration.
38+
- Reset-GitHubConfig: Resets all or specific sections to its default values.
39+
- Restore-GitHubConfig: Restores the configuration from the secret vault.
40+
- Save-GitHubConfig: Saves the current configuration to the secret vault.
41+
- Set-GitHubConfig: Allows setting specific elements of the configuration.
42+
43+
The configuration values are securely stored using the SecretManagement and SecretStore modules.
44+
During the module import, the following steps are performed:
45+
- Initialize the configuration store.
46+
- Check for secret vault of type 'Microsoft.PowerShell.SecretStore'.
47+
If not registered for the current user, its configuration will be reset to unattended mode.
48+
- Check for secret vault with the name 'GitHub'.
49+
If it does not exist, it will be created with current configuration.
50+
If the user is already using the secret vault, the existing configuration will be kept.
51+
- Restore saved configuration from the configuration store.
52+
- Look for the 'GitHub' secret vault.
53+
- Look for the secret called 'Config'. If it exists, restore the configuration from it into memory
54+
55+
EXAMPLES
56+
57+
-------------------------- EXAMPLE 1 --------------------------
58+
59+
Get-GitHubConfig
60+
61+
This command retrieves the current GitHub configuration.
62+
63+
-------------------------- EXAMPLE 2 --------------------------
64+
65+
Set-GitHubConfig -APIBaseURI 'https://api.newurl.com' -APIVersion '2023-09-23'
66+
67+
This command sets the API Base URI to 'https://api.newurl.com' and the API version to '2023-09-23'.
68+
69+
-------------------------- EXAMPLE 3 --------------------------
70+
71+
Restore-GitHubConfig
72+
73+
This command restores the GitHub configuration from the secret vault.
74+
75+
-------------------------- EXAMPLE 4 --------------------------
76+
77+
Reset-GitHubConfig -Scope 'App.API'
78+
79+
This command resets the 'App.API' section of the GitHub configuration to its default values.
80+
81+
-------------------------- EXAMPLE 5 --------------------------
82+
83+
Save-GitHubConfig
84+
85+
This command saves the current GitHub configuration to the secret vault.
86+
87+
KEYWORDS
88+
GitHub
89+
PowerShell
90+
SecretManagement
91+
SecretStore
92+
93+
SEE ALSO
94+
- For more information about SecretManagement and SecretStore:
95+
https://learn.microsoft.com/en-us/powershell/utility-modules/secretmanagement/overview?view=ps-modules
96+
- The GitHub repository of this module:
97+
https://github.com/PSModule/GitHub
98+
- PowerShell Gallery page for SecretManagement module:
99+
https://www.powershellgallery.com/packages/Microsoft.PowerShell.SecretManagement/
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#Requires -Version 7.0
2+
#Requires -Modules Microsoft.PowerShell.SecretManagement
3+
#Requires -Modules Microsoft.PowerShell.SecretStore
4+
5+
function Initialize-SecretVault {
6+
<#
7+
.SYNOPSIS
8+
Initialize a secret vault.
9+
10+
.DESCRIPTION
11+
Initialize a secret vault. If the vault does not exist, it will be created.
12+
13+
.EXAMPLE
14+
Initialize-SecretVault -Name 'SecretStore' -Type 'Microsoft.PowerShell.SecretStore'
15+
16+
Initializes a secret vault named 'SecretStore' using the 'Microsoft.PowerShell.SecretStore' module.
17+
18+
.NOTES
19+
For more information aobut secret vaults, see https://learn.microsoft.com/en-us/powershell/utility-modules/secretmanagement/overview?view=ps-modules
20+
#>
21+
[OutputType([void])]
22+
[CmdletBinding()]
23+
param (
24+
# The name of the secret vault.
25+
[Parameter()]
26+
[string] $Name,
27+
28+
# The type of the secret vault.
29+
[Parameter()]
30+
[Alias('ModuleName')]
31+
[string] $Type
32+
)
33+
34+
$secretVault = Get-SecretVault | Where-Object { $_.ModuleName -eq $Type }
35+
$secretVaultExists = $secretVault.count -ne 0
36+
Write-Verbose "A $Name exists: $secretVaultExists"
37+
if (-not $secretVaultExists) {
38+
Write-Verbose "Registering [$Name]"
39+
40+
switch ($Type) {
41+
'Microsoft.PowerShell.SecretStore' {
42+
$vaultParameters = @{
43+
Authentication = 'None'
44+
PasswordTimeout = -1
45+
Interaction = 'None'
46+
Scope = 'CurrentUser'
47+
WarningAction = 'SilentlyContinue'
48+
Confirm = $false
49+
Force = $true
50+
}
51+
Reset-SecretStore @vaultParameters
52+
}
53+
}
54+
}
55+
56+
$secretStore = Get-SecretVault | Where-Object { $_.Name -eq $Name }
57+
$secretStoreExists = $secretStore.count -ne 0
58+
if (-not $secretStoreExists) {
59+
$secretVault = @{
60+
Name = $Name
61+
ModuleName = $Type
62+
DefaultVault = $true
63+
Description = 'SecretStore'
64+
}
65+
Register-SecretVault @secretVault
66+
}
67+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function Get-GitHubConfig {
2+
<#
3+
.SYNOPSIS
4+
Get the current GitHub configuration.
5+
6+
.DESCRIPTION
7+
Get the current GitHub configuration.
8+
If the Refresh switch is used, the configuration will be refreshed from the configuration file.
9+
10+
.EXAMPLE
11+
Get-GitHubConfig
12+
13+
Returns the current GitHub configuration.
14+
15+
.EXAMPLE
16+
Get-GitHubConfig -Refresh
17+
18+
Refreshes the current GitHub configuration from the configuration store beofre returning it.
19+
#>
20+
[Alias('Get-GHConfig')]
21+
[OutputType([PSCustomObject])]
22+
[CmdletBinding()]
23+
param (
24+
# Refresh the configuration from the configuration store before returning it.
25+
[Parameter()]
26+
[switch] $Refresh
27+
)
28+
29+
if ($Refresh) {
30+
Restore-GitHubConfig
31+
}
32+
33+
$script:Config
34+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function Reset-GitHubConfig {
2+
<#
3+
.SYNOPSIS
4+
Reset the GitHub configuration.
5+
6+
.DESCRIPTION
7+
Reset the GitHub configuration. Specific scopes can be reset by using the Scope parameter.
8+
9+
.EXAMPLE
10+
Reset-GitHubConfig
11+
12+
Resets the entire GitHub configuration.
13+
14+
.EXAMPLE
15+
Reset-GitHubConfig -Scope 'App.API'
16+
17+
Resets the App.API scope of the GitHub configuration.
18+
#>
19+
[Alias('Reset-GHConfig')]
20+
[OutputType([void])]
21+
[CmdletBinding()]
22+
param(
23+
[Parameter()]
24+
[ValidateSet('App', 'App.API', 'App.Defaults', 'All')]
25+
[string] $Scope = 'All'
26+
)
27+
28+
switch($Scope) {
29+
'App' {
30+
$script:Config.App = $script:ConfigTemplate.App
31+
}
32+
'App.API' {
33+
$script:Config.App.API = $script:ConfigTemplate.App.API
34+
}
35+
'App.Defaults' {
36+
$script:Config.App.Defaults = $script:ConfigTemplate.App.Defaults
37+
}
38+
'All' {
39+
$script:Config = $script:ConfigTemplateDefaults
40+
}
41+
}
42+
Save-GitHubConfig
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Requires -Version 7.0
2+
#Requires -Modules Microsoft.PowerShell.SecretManagement
3+
4+
function Restore-GitHubConfig {
5+
<#
6+
.SYNOPSIS
7+
Restore the GitHub configuration from the configuration store.
8+
9+
.DESCRIPTION
10+
Restore the GitHub configuration from the configuration store.
11+
12+
.EXAMPLE
13+
Restore-GitHubConfig
14+
15+
Restores the GitHub configuration from the configuration store.
16+
#>
17+
[Alias('Load-GitHubConfig')]
18+
[Alias('Load-GHConfig')]
19+
[Alias('Restore-GHConfig')]
20+
[OutputType([void])]
21+
[CmdletBinding()]
22+
param()
23+
24+
$vault = Get-SecretVault -Name $script:SecretVault.Name
25+
$vaultExists = $vault.count -eq 1
26+
if ($vaultExists) {
27+
$secretExists = Get-SecretInfo -Name $script:Secret.Name -Vault $script:SecretVault.Name
28+
if ($secretExists) {
29+
$script:Config = Get-Secret -Name $script:Secret.Name -AsPlainText -Vault $script:SecretVault.Name | ConvertFrom-Json
30+
} else {
31+
Write-Warning "Unable to restore configuration."
32+
Write-Warning "The secret [$($script:Secret.Name)] does not exist in the vault [$($script:SecretVault.Name)]."
33+
}
34+
} else {
35+
Write-Warning "Unable to restore configuration."
36+
Write-Warning "The vault [$($script:SecretVault.Name)] does not exist."
37+
}
38+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#Requires -Version 7.0
2+
#Requires -Modules Microsoft.PowerShell.SecretManagement
3+
4+
function Save-GitHubConfig {
5+
<#
6+
.SYNOPSIS
7+
Save the GitHub configuration to the configuration store.
8+
9+
.DESCRIPTION
10+
Save the GitHub configuration to the configuration store.
11+
12+
.EXAMPLE
13+
Save-GitHubConfig
14+
15+
Saves the GitHub configuration to the configuration store.
16+
#>
17+
[Alias('Save-GHConfig')]
18+
[OutputType([void])]
19+
[CmdletBinding()]
20+
param()
21+
22+
$config = $script:Config | ConvertTo-Json -Depth 100
23+
Set-Secret -Name $script:Secret.Name -Secret $config -Vault $script:SecretVault.Name
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function Set-GitHubConfig {
2+
<#
3+
.SYNOPSIS
4+
Set the GitHub configuration.
5+
6+
.DESCRIPTION
7+
Set the GitHub configuration. Specific scopes can be set by using the parameters.
8+
9+
.EXAMPLE
10+
Set-GitHubConfig -APIBaseURI 'https://api.github.com' -APIVersion '2022-11-28'
11+
12+
Sets the App.API scope of the GitHub configuration.
13+
#>
14+
[Alias('Set-GHConfig')]
15+
[CmdletBinding()]
16+
param (
17+
# Set the API Base URI.
18+
[Parameter()]
19+
[string] $APIBaseURI,
20+
21+
# Set the GitHub API Version.
22+
[Parameter()]
23+
[string] $APIVersion
24+
)
25+
26+
switch ($PSBoundParameters.Keys) {
27+
'APIBaseURI' {
28+
$script:ConfigTemplate.App.API.BaseURI = $APIBaseURI
29+
}
30+
31+
'APIVersion' {
32+
$script:ConfigTemplate.App.API.Version = $APIVersion
33+
}
34+
}
35+
Save-GitHubConfig
36+
}

src/GitHub/public/loader.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Initialize-SecretVault -Name $script:SecretVault.Name -Type $script:SecretVault.Type
2+
Restore-GitHubConfig

0 commit comments

Comments
 (0)