Added specifier for the Vault to use with Get-Secret

This commit is contained in:
Nathan Windisch 2024-07-25 02:45:49 +01:00
parent d8f1ac024e
commit 6bcab03ca0
5 changed files with 13 additions and 13 deletions

View File

@ -30,10 +30,10 @@ function Connect-GraylogService {
[PSCredential] [PSCredential]
$Credential $Credential
) )
if ((Test-Session)) { return } # If the session is still valid, don't create a new one if ((Test-GraylogSession -SkipSecretCheck)) { return } # If the session is still valid, don't create a new one
if (-NOT $Credential) { if (-NOT $Credential) {
try { try {
$Credential = Get-Secret Graylog_Credential -ErrorAction Stop $Credential = Get-Secret Graylog_Credential -Vault Graylog -ErrorAction Stop
} catch { } catch {
try { try {
Write-Host "The Graylog Credential secret is missing, prompting for input..." Write-Host "The Graylog Credential secret is missing, prompting for input..."
@ -46,7 +46,7 @@ function Connect-GraylogService {
} }
$Request = @{ $Request = @{
Method = "POST" Method = "POST"
URI = "$(Get-Secret Graylog_BaseURI -AsPlainText)/api/system/sessions" URI = "$(Get-Secret Graylog_BaseURI -Vault Graylog -AsPlainText)/api/system/sessions"
Body = ConvertTo-Json @{ Body = ConvertTo-Json @{
host = $BaseURI.Authority host = $BaseURI.Authority
username = $Credential.Username.Split("@")[0] username = $Credential.Username.Split("@")[0]
@ -59,7 +59,7 @@ function Connect-GraylogService {
} }
# Use a regular Invoke-RestMethod for the initial sign-in request to avoid any issues with the WebSession # Use a regular Invoke-RestMethod for the initial sign-in request to avoid any issues with the WebSession
try { $null = Invoke-RestMethod @Request } try { $null = Invoke-RestMethod @Request }
catch { throw $_ } catch { throw $_.Exception.Message }
$GraylogSession | ConvertFrom-GraylogSession | Set-Secret Graylog_Session $GraylogSession | ConvertFrom-GraylogSession | Set-Secret Graylog_Session
} }

View File

@ -21,7 +21,7 @@ function ConvertFrom-GraylogSession {
$InputObject $InputObject
) )
try { $local:Graylog_BaseURI = Get-Secret Graylog_BaseURI -AsPlainText } try { $local:Graylog_BaseURI = Get-Secret Graylog_BaseURI -Vault Graylog -AsPlainText }
catch { Initialize-ServiceVault } catch { Initialize-ServiceVault }
# $Output = @{ # $Output = @{
# Headers = $InputObject.Headers # Headers = $InputObject.Headers

View File

@ -1,6 +1,6 @@
function Initialize-GraylogServiceVault { function Initialize-GraylogServiceVault {
try { try {
$null = Get-SecretVault "Graylog" -ErrorAction Stop $null = Get-SecretVault Graylog -ErrorAction Stop
} catch { } catch {
try { try {
$null = Register-SecretVault "Graylog" -ModuleName "Microsoft.PowerShell.SecretStore" -ErrorAction Stop $null = Register-SecretVault "Graylog" -ModuleName "Microsoft.PowerShell.SecretStore" -ErrorAction Stop
@ -10,7 +10,7 @@ function Initialize-GraylogServiceVault {
} }
try { try {
$null = Get-Secret Graylog_BaseURI -ErrorAction Stop $null = Get-Secret Graylog_BaseURI -Vault Graylog -ErrorAction Stop
} catch { } catch {
try { try {
do { do {
@ -29,7 +29,7 @@ function Initialize-GraylogServiceVault {
} }
try { try {
$null = Get-Secret Graylog_Credential -ErrorAction Stop $null = Get-Secret Graylog_Credential -Vault Graylog -ErrorAction Stop
} catch { } catch {
try { try {
Write-Host "The Graylog Credential secret is missing, prompting for input..." Write-Host "The Graylog Credential secret is missing, prompting for input..."

View File

@ -49,10 +49,10 @@ function Invoke-GraylogRequest {
[string] [string]
$ContentType = "application/json" $ContentType = "application/json"
) )
$Session = Get-Secret Graylog_Session Graylog -AsPlainText -ErrorAction Stop $Session = Get-Secret Graylog_Session -Vault Graylog -AsPlainText -ErrorAction Stop
$Request = @{ $Request = @{
Method = $Method Method = $Method
URI = "$(Get-Secret Graylog_BaseURI -AsPlainText)/api/$($Path.TrimStart('/api'))" URI = "$(Get-Secret Graylog_BaseURI -Vault Graylog -AsPlainText)/api/$($Path.TrimStart('/api'))"
WebSession = (ConvertTo-GraylogSession $Session) WebSession = (ConvertTo-GraylogSession $Session)
ContentType = $ContentType ContentType = $ContentType
} }

View File

@ -27,11 +27,11 @@ function Test-GraylogSession {
) )
if (-NOT $SkipSecretCheck) { if (-NOT $SkipSecretCheck) {
try { $null = Get-Secret Graylog_BaseURI -ErrorAction Stop } try { $null = Get-Secret Graylog_BaseURI -Vault Graylog -ErrorAction Stop }
catch { Write-Error "The Graylog BaseURI secret is missing, try running Initialize-GraylogServiceVault?"; return $false } catch { Write-Error "The Graylog BaseURI secret is missing, try running Initialize-GraylogServiceVault?"; return $false }
try { $null = Get-Secret Graylog_Credential -ErrorAction Stop } try { $null = Get-Secret Graylog_Credential -Vault Graylog -ErrorAction Stop }
catch { Write-Error "The Graylog Credential secret is missing, try running Initialize-GraylogServiceVault?"; return $false } catch { Write-Error "The Graylog Credential secret is missing, try running Initialize-GraylogServiceVault?"; return $false }
try { $null = Get-Secret Graylog_Session -ErrorAction Stop } try { $null = Get-Secret Graylog_Session -Vault Graylog -ErrorAction Stop }
catch { Write-Error "The Graylog Session secret is missing, try running Connect-GraylogService?"; return $false } catch { Write-Error "The Graylog Session secret is missing, try running Connect-GraylogService?"; return $false }
if ($SkipSessionCheck) { return $true } if ($SkipSessionCheck) { return $true }
} }