Files
Samsara/tests/powershell/Test-Collectors.ps1

145 lines
11 KiB
PowerShell

#requires -Version 5.1
# AST and synthetic collector contract tests only. Does not query any real server.
$ErrorActionPreference = 'Stop'
$root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
$files = @(Get-ChildItem -LiteralPath (Join-Path $root 'powershell') -Filter '*.ps1')
foreach ($file in $files) {
$tokens = $null; $parseErrors = $null
$null = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$tokens, [ref]$parseErrors)
if ($parseErrors.Count) { throw ('PowerShell syntax invalid: ' + $file.Name) }
}
foreach ($file in @($files | Where-Object { $_.Name -ne 'Get-ServerInventory.ps1' })) { . $file.FullName }
$script:SamsaraIsElevated = $true
$script:SyntheticBuild = '20348'
function Get-CimInstance {
[CmdletBinding()] param($ClassName, $Property, $Filter)
switch ($ClassName) {
'Win32_OperatingSystem' { [pscustomobject]@{ Caption='Synthetic Windows Server'; Version=('10.0.' + $script:SyntheticBuild); BuildNumber=$script:SyntheticBuild; ProductType=3; LastBootUpTime=[DateTime]::UtcNow.AddDays(-2) } }
'Win32_ComputerSystem' { [pscustomobject]@{ Domain='example.test'; PartOfDomain=$true } }
'Win32_NetworkAdapterConfiguration' { [pscustomobject]@{ Description='Synthetic NIC'; IPAddress=@('192.0.2.20') } }
'Win32_Service' { [pscustomobject]@{ Name='W3SVC'; DisplayName='Synthetic Web'; State='Running'; StartMode='Auto'; StartName='EXAMPLE\svc_web'; ProcessId=123; PathName='SECRET command arguments' } }
default { throw 'Unexpected synthetic CIM query' }
}
}
function Get-WindowsFeature { [CmdletBinding()] param() [pscustomobject]@{ Name='Web-Server'; DisplayName='Synthetic Web Server'; Installed=$true; InstallState='Installed'; FeatureType='Role' } }
function Get-Process { [CmdletBinding()] param() [pscustomobject]@{ Id=123; ProcessName='SyntheticWorker'; CommandLine='SECRET' } }
function Get-NetTCPConnection { [CmdletBinding()] param() [pscustomobject]@{ State='Established'; LocalAddress='192.0.2.20'; LocalPort=443; RemoteAddress='192.0.2.21'; RemotePort=50000; OwningProcess=123 } }
function Get-ScheduledTask {
[CmdletBinding()] param()
[pscustomobject]@{ TaskName='Synthetic report'; TaskPath='\Example\'; State='Ready'
Settings=[pscustomobject]@{ Enabled=$true }; Principal=[pscustomobject]@{ UserId='EXAMPLE\svc_report' }
Actions=@([pscustomobject]@{ Execute='C:\Tools\report.exe'; Arguments='--password=SECRET' })
Triggers=@([pscustomobject]@{ CimClass=[pscustomobject]@{ CimClassName='MSFT_TaskDailyTrigger' }; StartBoundary='2026-10-01T02:00:00'; Enabled=$true; DaysInterval=1; Repetition=$null }) }
}
function Get-Volume { [CmdletBinding()] param() [pscustomobject]@{ FileSystemLabel='Synthetic OS'; DriveLetter='C'; Path='C:\'; FileSystem='NTFS'; Size=100GB; SizeRemaining=50GB } }
function Get-SmbShare { [CmdletBinding()] param() [pscustomobject]@{ Name='SyntheticReports'; Path='C:\Reports'; Special=$false } }
function Get-SmbOpenFile {
[CmdletBinding()] param()
[pscustomobject]@{ Path='C:\Reports\SECRET-file.txt'; ShareRelativePath='SECRET-file.txt'; ClientComputerName='192.0.2.21'; ClientUserName='SECRET' }
[pscustomobject]@{ Path='C:\Reports\SECRET-admin.txt'; ShareRelativePath='Reports\SECRET-admin.txt'; ClientComputerName='192.0.2.21'; ClientUserName='SECRET' }
}
function Get-ChildItem {
[CmdletBinding()] param($LiteralPath)
if ($LiteralPath -eq 'Cert:\LocalMachine') { [pscustomobject]@{ PSPath='Cert:\LocalMachine\My'; PSChildName='My' } }
else { [pscustomobject]@{ Subject='CN=synthetic.example.test'; Issuer='CN=Synthetic CA'; NotAfter=[DateTime]::UtcNow.AddDays(30); Thumbprint='0000000000000000000000000000000000000000'; PrivateKey='SECRET' } }
}
function Assert-True { param($Value, [string]$Message) if (-not $Value) { throw $Message } }
$collectors = [ordered]@{
system={param($r) Get-SamsaraSystem $r}; roles={param($r) Get-SamsaraRoles $r}
services={param($r) Get-SamsaraServices $r}; network={param($r) Get-SamsaraNetwork $r}
tasks={param($r) Get-SamsaraTasks $r}; storage={param($r) Get-SamsaraStorage $r}
shares={param($r) Get-SamsaraShares $r}; certificates={param($r) Get-SamsaraCertificates $r}
}
foreach ($name in $collectors.Keys) {
$section = Invoke-SamsaraSection $name $collectors[$name]
Assert-True ($section.status -eq 'complete') ('Synthetic section failed: ' + $name)
Assert-True ($section.started_at -and $section.completed_at) 'Missing timestamps'
Assert-True (($section | ConvertTo-Json -Depth 12) -notmatch 'SECRET') 'Secret field serialized'
if ($name -eq 'network') { Assert-True ($section.data[0].process -eq 'SyntheticWorker') 'Process correlation failed' }
if ($name -eq 'shares') { Assert-True ($section.data[0].observed_open_files -eq 1) 'Share activity correlation missing' }
if ($name -eq 'tasks') { Assert-True ($section.data[0].executables -eq 'report.exe') 'Executable basename missing' }
}
foreach ($build in @('14393','17763','20348','26100')) {
$script:SyntheticBuild=$build
$section=Invoke-SamsaraSection 'system' {param($r) Get-SamsaraSystem $r}
Assert-True ($section.status -eq 'complete') 'Supported build mapping failed'
}
# Test registry projection with in-memory objects; never open a real registry hive.
$fakeEntry = New-Object PSObject
$fakeEntry | Add-Member ScriptMethod GetValue { param($key) switch ($key) { 'DisplayName' {'Synthetic application'}; 'DisplayVersion' {'1.0'}; 'Publisher' {'Example'}; 'InstallDate' {'20260101'}; default {throw 'Unexpected registry value requested'} } }
$fakeEntry | Add-Member ScriptMethod Dispose { }
$fakeUninstall = New-Object PSObject
$fakeUninstall | Add-Member ScriptMethod GetSubKeyNames { @('Example') }
$fakeUninstall | Add-Member ScriptMethod OpenSubKey { param($name,$writable) if ($writable) {throw 'Writable registry access'}; return $fakeEntry }
$section=Invoke-SamsaraSection 'software' {param($r) Invoke-SamsaraRead $r 'Synthetic registry view' { Read-SamsaraSoftwareEntries $r $fakeUninstall 'Registry64' } }
Assert-True ($section.status -eq 'complete' -and $section.data[0].name -eq 'Synthetic application') 'Registry metadata projection failed'
function Get-SmbShare { [CmdletBinding()] param() throw [UnauthorizedAccessException]::new('SECRET protected details') }
$section=Invoke-SamsaraSection 'shares' {param($r) Get-SamsaraShares $r}
Assert-True ($section.status -eq 'partial' -and $section.data.Count -eq 0) 'Denied share enumeration was not partial/empty'
Assert-True ($section.errors[0].code -eq 'access_denied') 'Permission error not categorized'
Assert-True (($section | ConvertTo-Json -Depth 12) -notmatch 'SECRET') 'Raw error exposed'
$script:SamsaraIsElevated=$false
$section=Invoke-SamsaraSection 'services' {param($r) Get-SamsaraServices $r}
Assert-True ($section.status -eq 'partial' -and $section.permission_limitations.Count -gt 0) 'Non-elevated scope not marked partial'
Write-Output 'PowerShell AST and synthetic collector tests passed. No real Windows inventory was collected.'
# Reliability cases: mocks never access real tasks, certificates or private keys.
$script:SamsaraIsElevated=$true
$script:SamsaraDiagnostic=$true
function New-TestTask {
param([bool]$Enabled=$true)
[pscustomobject]@{TaskName='Example';TaskPath='\';State='Ready';Settings=[pscustomobject]@{Enabled=$Enabled};Principal=[pscustomobject]@{UserId='SYSTEM'};Actions=@([pscustomobject]@{Execute='"C:\Tools\bad.exe"';Arguments='SECRET'});Triggers=@()}
}
function Get-ScheduledTask { [CmdletBinding()]param() New-TestTask $true; New-TestTask $false }
$r=Invoke-SamsaraSection tasks {param($r) Get-SamsaraTasks $r}
Assert-True ($r.status -eq 'complete' -and $r.data.Count -eq 2 -and -not $r.data[1].enabled) 'Normal/disabled tasks lost'
Assert-True ($r.data[0].executables -eq '[executable name omitted]') 'Malformed executable not sanitized'
function Get-ScheduledTask { [CmdletBinding()]param() }
$r=Invoke-SamsaraSection tasks {param($r) Get-SamsaraTasks $r}
Assert-True ($r.status -eq 'complete' -and $r.data.Count -eq 0) 'Successful empty task query failed'
function Get-ScheduledTask { [CmdletBinding()]param() Write-Error 'SECRET' -Category PermissionDenied }
$r=Invoke-SamsaraSection tasks {param($r) Get-SamsaraTasks $r}
Assert-True ($r.status -eq 'failed' -and $r.data.Count -eq 0) 'Failed task query treated as empty success'
function Get-ScheduledTask {
[CmdletBinding()]param()
New-TestTask
$bad=New-TestTask
$bad | Add-Member ScriptProperty TaskName { throw [UnauthorizedAccessException]::new('SECRET') } -Force
# Throw from ToString to reliably exercise projection failures on both runtimes.
$value=New-Object PSObject
$value | Add-Member ScriptMethod ToString { throw [UnauthorizedAccessException]::new('SECRET') } -Force
$bad.TaskPath=$value
$bad
}
$r=Invoke-SamsaraSection tasks {param($r) Get-SamsaraTasks $r}
Assert-True ($r.status -eq 'partial' -and $r.data.Count -eq 1 -and $r.errors.Count -gt 0) 'Individual task failure discarded healthy records'
Assert-True (($r | ConvertTo-Json -Depth 12) -notmatch 'SECRET') 'Diagnostic leaked secret'
$script:StoreMode='normal'
function Get-ChildItem {
[CmdletBinding()]param($LiteralPath)
if ($LiteralPath -eq 'Cert:\LocalMachine') {
foreach ($n in @('My','Root','Empty')) { [pscustomobject]@{PSChildName=$n;PSPath=('Microsoft.PowerShell.Security\Certificate::'+$n)} }
} elseif ($LiteralPath -eq 'Cert:\LocalMachine\Empty') { return }
elseif ($LiteralPath -in @('Cert:\LocalMachine\My','Cert:\LocalMachine\Root')) {
if ($script:StoreMode -eq 'denied' -or ($script:StoreMode -eq 'partial' -and $LiteralPath -like '*Root')) { throw [UnauthorizedAccessException]::new('SECRET') }
[pscustomobject]@{Subject='CN=Example';Issuer='CN=CA';Thumbprint='00';NotAfter=[datetime]::UtcNow;PrivateKey='SECRET'}
} else { throw 'Incorrect certificate provider path' }
}
$r=Invoke-SamsaraSection certificates {param($r) Get-SamsaraCertificates $r}
Assert-True ($r.status -eq 'complete' -and $r.data.Count -eq 2 -and @($r.data.store | Select-Object -Unique).Count -eq 2) 'Multiple stores/empty store failed'
$script:StoreMode='partial'
$r=Invoke-SamsaraSection certificates {param($r) Get-SamsaraCertificates $r}
Assert-True ($r.status -eq 'partial' -and $r.data.Count -eq 1 -and $r.errors[0].code -eq 'access_denied') 'Partial store failure lost records'
Assert-True (($r | ConvertTo-Json -Depth 12) -notmatch 'SECRET') 'Certificate diagnostics leaked secret'
function Get-ChildItem { [CmdletBinding()]param($LiteralPath) if ($LiteralPath -eq 'Cert:\LocalMachine') { [pscustomobject]@{PSChildName='My'} } else { throw [UnauthorizedAccessException]::new('SECRET') } }
$r=Invoke-SamsaraSection certificates {param($r) Get-SamsaraCertificates $r}
Assert-True ($r.status -eq 'failed' -and $r.data.Count -eq 0) 'Inaccessible stores treated as empty success'
function Get-ChildItem { [CmdletBinding()]param($LiteralPath) }
$r=Invoke-SamsaraSection certificates {param($r) Get-SamsaraCertificates $r}
Assert-True ($r.status -eq 'complete' -and $r.data.Count -eq 0) 'Empty store list failed'
function Get-ScheduledTask { [CmdletBinding()]param() New-TestTask }
function ConvertTo-Json { [CmdletBinding()]param($InputObject,$Depth,[switch]$Compress) throw [InvalidOperationException]::new('SECRET serialization') }
$r=Invoke-SamsaraSection tasks {param($r) Get-SamsaraTasks $r}
Assert-True ($r.status -ne 'complete' -and $r.data.Count -eq 0 -and $r.errors.Count -eq 1) 'Serialization failure accepted as evidence'
Remove-Item Function:\ConvertTo-Json
Write-Output 'Reliability collector tests passed.'