Files
personaEngine2/tests/Safety/WriteGate.Tests.ps1
T
dave cdc6bb33d3 Implement Stage A: rule engine, validation, audit, and safety gates
Completes 109 of 121 tasks. Every remaining task needs a tenant connection
(T055, T056, T101-T103) or an Azure Automation account (T115-T121).

  354 offline Pester tests      PASS
  Engine purity (Principle IV)  PASS
  Sanitization (SC-013)         PASS  (156 files)
  Graph module loaded in tests  none  (SC-008 holds)

What landed
  - Four-layer configuration validation with stable finding codes, covering
    every VR-002 and VR-003 condition, plus a 23-fixture invalid-config corpus
  - Run loop, audit records (NDJSON through a single sink), summaries,
    reconciliation, and exit codes 0-6
  - Persistence behind a single write-body builder whose result always has
    exactly one key
  - Invoke-PersonaEngine.ps1 and Edit-PersonaEngineConfig.ps1
  - Six docs, two pipelines, traceability matrix, V-5a and sanitization records

Three deviations from tasks.md, each recorded in its status block

  T033 is not in Resolve-UserPersona. evaluationErrorThreshold is run-level
  state and the rule engine is pure; a counter there would break Principle IV.
  It lives in New-PersonaRunCounter and is applied in the run loop.

  A new src/Engine/ layer holds Invoke-PersonaEngineRun. The entry script
  imports the manifest, which requires Microsoft.Graph.Authentication, so a
  loop living only inside it could not run on a machine without the Graph SDK
  and SC-004 could not be proven at all. The entry script is now a thin
  wrapper and what ships is what is tested.

  The invalid-config corpus is generated by a committed script, with the
  generated fixtures committed too, so a reviewer sees the fixture in the diff.

Defects found by running the code, not by reading it

  Group and role ID lists were double-wrapped: @(Get-PersonaGroupIdPage ...)
  around a comma-returned array collapsed every membership list into one
  bogus space-joined entry. That is a silent false non-match, exactly what
  FR-013 exists to prevent.

  A 403 whose status appears only in the exception message parsed as $null,
  which the retry policy treats as a transport error - five requests per
  account against a tenant already refusing. Status extraction now falls back
  to the message text, bounded to 400-599.

  The sanitization scan walked tracked files only, so it covered 34 of 156
  files and none of this phase's code. It now scans untracked non-ignored
  files too, and a negative control confirms it catches a planted leak.

  Test-Json reports one error per violating location, not first-failure-only
  as the V-5a draft claimed. Record and pin corrected.

Enforcement remains blocked on the V-4 security sign-off.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-20 21:48:19 -04:00

193 lines
8.6 KiB
PowerShell

#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0.0' }
<#
FR-016: a write is issued only when all four conditions hold.
1. Evaluation completed successfully (Outcome is not EvaluationError)
2. Calculated differs from stored, ordinal comparison
3. The target attribute is non-blank and approved
4. ShouldProcess returned true for this user
Each condition is tested in isolation by failing exactly that one, so a passing
result cannot be explained by a different condition having blocked the write.
#>
BeforeAll {
$repoRoot = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
. (Join-Path $repoRoot 'tests/TestHelpers.ps1')
foreach ($file in (Get-PersonaSourceFile -RepoRoot $repoRoot)) { . $file }
$script:target = 'extension_<EXTENSION-APP-ID>_<PERSONA>'
function New-Decision {
param(
[string] $Outcome = 'Matched',
[AllowNull()] [string] $Calculated = 'Tier0-Admin',
[AllowNull()] [string] $Stored = 'Employee',
[string] $ErrorReason = $null
)
[pscustomobject]@{
AccountObjectId = '00000000-0000-0000-0000-000000000101'
UserPrincipalName = 'alex@example.invalid'
Outcome = $Outcome
MatchedRuleId = 'RULE-0030-TIER0'
CalculatedPersona = $Calculated
StoredPersona = $Stored
Action = 'Pending'
EvaluationErrorReason = $ErrorReason
RulesEvaluated = 2
DurationMs = 4
ConditionTrace = $null
}
}
}
Describe 'Condition 1 - evaluation must have succeeded' -Tag 'Safety' {
It 'skips an EvaluationError user even when the values differ' {
$result = Compare-PersonaValue -Result (New-Decision -Outcome 'EvaluationError' -Calculated $null -ErrorReason 'lookup failed') `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'Skipped'
}
It 'preserves the stored value on an EvaluationError user (FR-014)' {
$result = Compare-PersonaValue -Result (New-Decision -Outcome 'EvaluationError' -Calculated $null -Stored 'Tier0-Admin' -ErrorReason 'lookup failed') `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.StoredPersona | Should -Be 'Tier0-Admin'
$result.Action | Should -Be 'Skipped'
}
It 'never reaches the write adapter for an EvaluationError user' {
Mock Write-Host { }
Mock Set-UserPersonaAttribute { [pscustomobject]@{ Succeeded = $true } }
Mock Get-PersonaUsers { @(New-TestPopulation -Count 8 -TargetAttribute $script:target) }
Mock Get-PersonaGroupMembership { New-PersonaMembershipRecord -DirectFailureReason 'Graph 503 after 5 attempts' }
$config = New-TestRuntimeConfiguration -TargetAttribute $script:target -Rules @(
(New-TestMembershipRule)
[pscustomobject]@{
id = 'RULE-0900-EMPLOYEE'; name = 'Employees'; enabled = $true; priority = 900; persona = 'Employee'
match = [pscustomobject]@{ operator = 'all'; conditions = @([pscustomobject]@{ type = 'property'; property = 'Department'; operator = 'isNotNull' }) }
})
$outcome = Invoke-PersonaEngineRun -Configuration $config -TargetAttribute $script:target `
-Context (New-TestAuditContext -Mode 'Enforce') -IsEnforcing `
-ShouldProcessGate { param($t, $d) $true }
$outcome.Counters.EvaluationError | Should -Be $outcome.Counters.Processed
Should -Invoke Set-UserPersonaAttribute -Times 0 -Exactly
}
}
Describe 'Condition 2 - the value must actually differ' -Tag 'Safety' {
It 'reports Unchanged when stored and calculated are identical' {
$result = Compare-PersonaValue -Result (New-Decision -Calculated 'Employee' -Stored 'Employee') `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'Unchanged'
}
It 'treats a case-only difference as a real change' {
# Change detection is ordinal (FR-015). Treating these as equal would leave the
# directory permanently inconsistent with the rule set.
$result = Compare-PersonaValue -Result (New-Decision -Calculated 'Employee' -Stored 'employee') `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'Updated'
}
It 'treats a blank stored value against a calculated persona as a change' {
$result = Compare-PersonaValue -Result (New-Decision -Calculated 'Employee' -Stored '') `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'Updated'
}
It 'treats a null stored value as equal to an empty calculated value' {
$result = Compare-PersonaValue -Result (New-Decision -Calculated '' -Stored $null) `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'Unchanged'
}
}
Describe 'Condition 3 - the target must be valid and approved' -Tag 'Safety' {
It 'skips when the target attribute is blank' {
$result = Compare-PersonaValue -Result (New-Decision) `
-IsEnforcing -TargetAttribute '' -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'Skipped'
}
It 'skips when the target is absent from the approved list' {
$result = Compare-PersonaValue -Result (New-Decision) `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @('extension_<EXTENSION-APP-ID>_<OTHER>')
$result.Action | Should -Be 'Skipped'
}
It 'skips when the approved list is empty' {
$result = Compare-PersonaValue -Result (New-Decision) `
-IsEnforcing -TargetAttribute $target -ApprovedWritableAttributes @()
$result.Action | Should -Be 'Skipped'
}
}
Describe 'Condition 4 - the gate must have returned true' -Tag 'Safety' {
It 'reports WouldUpdate rather than Updated when not enforcing' {
$result = Compare-PersonaValue -Result (New-Decision) `
-TargetAttribute $target -ApprovedWritableAttributes @($target)
$result.Action | Should -Be 'WouldUpdate'
}
It 'downgrades Updated to WouldUpdate when the per-user gate refuses' {
Mock Write-Host { }
Mock Set-UserPersonaAttribute { [pscustomobject]@{ Succeeded = $true } }
Mock Get-PersonaUsers { @(New-TestPopulation -Count 12 -TargetAttribute $script:target) }
$outcome = Invoke-PersonaEngineRun `
-Configuration (New-TestRuntimeConfiguration -TargetAttribute $script:target) `
-TargetAttribute $script:target -Context (New-TestAuditContext -Mode 'Enforce') `
-IsEnforcing -ShouldProcessGate { param($t, $d) $false }
$outcome.Counters.Updated | Should -Be 0
$outcome.Counters.WouldUpdate | Should -BeGreaterThan 0
Should -Invoke Set-UserPersonaAttribute -Times 0 -Exactly
}
It 'reports UpdateFailed when the gate allowed the write but the PATCH failed' {
Mock Write-Host { }
Mock Get-PersonaUsers { @(New-TestPopulation -Count 12 -TargetAttribute $script:target) }
Mock Set-UserPersonaAttribute { [pscustomobject]@{ Succeeded = $false; FailureReason = 'Response status code does not indicate success: 403 (Forbidden).' } }
$outcome = Invoke-PersonaEngineRun `
-Configuration (New-TestRuntimeConfiguration -TargetAttribute $script:target) `
-TargetAttribute $script:target -Context (New-TestAuditContext -Mode 'Enforce') `
-IsEnforcing -ShouldProcessGate { param($t, $d) $true }
$outcome.Counters.UpdateFailed | Should -BeGreaterThan 0
$outcome.Counters.Updated | Should -Be 0
}
It 'continues the run after a failed write rather than abandoning the population' {
Mock Write-Host { }
Mock Get-PersonaUsers { @(New-TestPopulation -Count 12 -TargetAttribute $script:target) }
Mock Set-UserPersonaAttribute { [pscustomobject]@{ Succeeded = $false; FailureReason = 'Forbidden' } }
$outcome = Invoke-PersonaEngineRun `
-Configuration (New-TestRuntimeConfiguration -TargetAttribute $script:target) `
-TargetAttribute $script:target -Context (New-TestAuditContext -Mode 'Enforce') `
-IsEnforcing -ShouldProcessGate { param($t, $d) $true }
$outcome.Counters.Processed | Should -Be 12
}
}