diff --git a/ScheduledTasks/DailySupportCheck/Invoke-DailySupportCheck.ps1 b/ScheduledTasks/DailySupportCheck/Invoke-DailySupportCheck.ps1 index 5d7323a..5f2f9a2 100644 --- a/ScheduledTasks/DailySupportCheck/Invoke-DailySupportCheck.ps1 +++ b/ScheduledTasks/DailySupportCheck/Invoke-DailySupportCheck.ps1 @@ -718,6 +718,27 @@ function Import-KurrentDbClientAssembly { Add-Type -Path $AssemblyPath } +function Get-KurrentDbWriteActivityCredentials { + [CmdletBinding()] + param([Parameter(Mandatory)] [object] $Settings) + + $username = $null + $password = $null + if ($Settings.PSObject.Properties['Username'] -and -not [string]::IsNullOrWhiteSpace([string] $Settings.Username)) { + $username = [string] $Settings.Username + $passwordEnvironmentVariable = [string] $Settings.PasswordEnvironmentVariable + $password = [Environment]::GetEnvironmentVariable($passwordEnvironmentVariable) + if ([string]::IsNullOrWhiteSpace($password)) { + throw "KurrentDB password environment variable '$passwordEnvironmentVariable' is not set." + } + } + + [pscustomobject]@{ + Username = $username + Password = $password + } +} + function Test-KurrentDbWriteActivity { [CmdletBinding()] param([Parameter(Mandatory)] [object] $Context) @@ -769,14 +790,7 @@ function Test-KurrentDbWriteActivity { "esdb://$($baseUri.Host):${port}?tls=false&tlsVerifyCert=false" } - $password = $null - if ($settings.PSObject.Properties['Username'] -and -not [string]::IsNullOrWhiteSpace([string] $settings.Username)) { - $passwordEnvironmentVariable = [string] $settings.PasswordEnvironmentVariable - $password = [Environment]::GetEnvironmentVariable($passwordEnvironmentVariable) - if ([string]::IsNullOrWhiteSpace($password)) { - throw "KurrentDB password environment variable '$passwordEnvironmentVariable' is not set." - } - } + $credentials = Get-KurrentDbWriteActivityCredentials -Settings $settings $reader = [SupportTools.KurrentDbClient.KurrentDbEventReader]::new() $events = $reader.ReadRecentEventsAsync( @@ -784,8 +798,8 @@ function Test-KurrentDbWriteActivity { $indexName, $requestCount, [timespan]::FromSeconds($timeoutSeconds), - [string] $settings.Username, - $password, + $credentials.Username, + $credentials.Password, [Threading.CancellationToken]::None).GetAwaiter().GetResult() [pscustomobject]@{ events = @($events) } diff --git a/ScheduledTasks/DailySupportCheck/Tests/Invoke-DailySupportCheck.Tests.ps1 b/ScheduledTasks/DailySupportCheck/Tests/Invoke-DailySupportCheck.Tests.ps1 index d33f8f9..177aca6 100644 --- a/ScheduledTasks/DailySupportCheck/Tests/Invoke-DailySupportCheck.Tests.ps1 +++ b/ScheduledTasks/DailySupportCheck/Tests/Invoke-DailySupportCheck.Tests.ps1 @@ -1,4 +1,13 @@ Describe 'Daily support check' { + It 'allows anonymous KurrentDB configuration without a Username property' { + $settings = [pscustomobject]@{ BaseUrl = 'http://kurrentdb' } + + $credentials = Get-KurrentDbWriteActivityCredentials -Settings $settings + + $credentials.Username | Should -BeNullOrEmpty + $credentials.Password | Should -BeNullOrEmpty + } + It 'uses the packaged KurrentDB client rather than a source-tree build output' { $configurationPath = Join-Path $PSScriptRoot '..\daily-support-check.json' $configuration = Get-Content -LiteralPath $configurationPath -Raw | ConvertFrom-Json