From 9f9078dbc873c71344533913ea973756eeca4b27 Mon Sep 17 00:00:00 2001 From: Nathan Windisch Date: Thu, 25 Jul 2024 03:11:15 +0100 Subject: [PATCH] Refactor Get-GraylogStreamId to use Get-GraylogStreams function --- Functions/Public/Get-GraylogStreamId.ps1 | 8 +------- Functions/Public/Get-GraylogStreamIds.ps1 | 24 +++++++++++++++++++++++ Functions/Public/Search-Graylog.ps1 | 2 +- PSGraylog.psd1 | 2 ++ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 Functions/Public/Get-GraylogStreamIds.ps1 diff --git a/Functions/Public/Get-GraylogStreamId.ps1 b/Functions/Public/Get-GraylogStreamId.ps1 index a8a92c6..2fd99e3 100644 --- a/Functions/Public/Get-GraylogStreamId.ps1 +++ b/Functions/Public/Get-GraylogStreamId.ps1 @@ -19,13 +19,7 @@ function Get-GraylogStreamId { $LogName ) - # TODO: Use Secret Management module to get the Graylog API URI - if ($null -eq $global:GraylogStreams) { - $Response = Invoke-GraylogRequest GET "/streams" - $global:GraylogStreams = $Response.Streams - } - - $Stream = $global:GraylogStreams.Where{$_.Title -eq $LogName} + $Stream = (Get-GraylogStreams).Where{$_.Title -eq $LogName} if ($null -eq $Stream) { return $null } return $Stream.Id } \ No newline at end of file diff --git a/Functions/Public/Get-GraylogStreamIds.ps1 b/Functions/Public/Get-GraylogStreamIds.ps1 new file mode 100644 index 0000000..8f9be9d --- /dev/null +++ b/Functions/Public/Get-GraylogStreamIds.ps1 @@ -0,0 +1,24 @@ +function Get-GraylogStreams { + <# + .SYNOPSIS + Gets the streams available in Graylog. + .DESCRIPTION + Gets a list of the streams available in Graylog. + .OUTPUTS + The streams available in Graylog. + .EXAMPLE + Get-GraylogStreams + Gets a list of the streams available in Graylog. + #> + param () + + try { $null = Get-Secret Graylog_BaseURI -Vault Graylog -AsPlainText -ErrorAction Stop } + catch { + try { $Response = Invoke-GraylogRequest GET "/streams" } + catch { throw $_.Exception.Message } + ConvertTo-Json $Response.Streams | Set-Secret Graylog_Streams -Vault Graylog -AsPlainText + } + + $Streams = Get-Secret Graylog_Streams -Vault Graylog -AsPlainText -ErrorAction Stop + return ConvertFrom-Json $Streams +} \ No newline at end of file diff --git a/Functions/Public/Search-Graylog.ps1 b/Functions/Public/Search-Graylog.ps1 index 714b027..0e91642 100644 --- a/Functions/Public/Search-Graylog.ps1 +++ b/Functions/Public/Search-Graylog.ps1 @@ -69,7 +69,7 @@ function Search-Graylog { $Detailed ) - $LogId = Get-GraylogLogStreamId -LogName $LogName + $LogId = Get-GraylogStreamId -LogName $LogName if ($null -eq $LogId) { throw "The log stream '$LogName' does not exist." } $SearchId = [String]::Join('', [GUID]::NewGUID().GUID.Replace("-", "")[0..23]) # Generate a unique identifier for the search $QueryId = [GUID]::NewGUID().GUID.ToString() # Generate a unique identifier for the query diff --git a/PSGraylog.psd1 b/PSGraylog.psd1 index 37e87a7..c9c0e29 100644 --- a/PSGraylog.psd1 +++ b/PSGraylog.psd1 @@ -69,6 +69,7 @@ RequiredModules = @("Microsoft.PowerShell.SecretManagement", "Microsoft.PowerShe NestedModules = @( "Functions\Public\Connect-GraylogService.ps1", "Functions\Public\Disconnect-GraylogService.ps1", +"Functions\Public\Get-GraylogStreams.ps1", "Functions\Public\Get-GraylogStreamId.ps1", "Functions\Public\Initialize-GraylogServiceVault.ps1", "Functions\Public\Invoke-GraylogRequest.ps1", @@ -83,6 +84,7 @@ NestedModules = @( FunctionsToExport = @( "Connect-GraylogService", "Disconnect-GraylogService", +"Get-GraylogStreams", "Get-GraylogStreamId", "Initialize-GraylogServiceVault", "Invoke-GraylogRequest",