Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
WellKnownSecurityPrincipals = @($wellKnown); SiteLinks = @($siteLinks); DhcpServers = @($dhcp); AuthNPolicyContainers = @($authN)
TrustedRootCAs = @($trustedRoots); IntermediateCAs = @($intermediate); EnterpriseCAs = @($enterprise); CertificateTemplates = @($templates)
EnrollmentTemplates = $enrollmentTemplates; CrlDistributionPoints = @($cdp); NtAuthCertificates = @($ntAuth); LdapQueryPolicies = @($policies)
# Whether the Directory Service object itself came back. It is NOT proof that
# tombstoneLifetime was readable: if the account can see the object but has no read
# access to that attribute, Active Directory omits the attribute and returns the
# object, which looks exactly like the attribute not being set. Callers must not
# infer a value from this flag alone - see Test-MtAdTombstoneLifetime.
DirectoryServiceRead = ($null -ne $directoryService)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
TombstoneLifetime = if ($null -ne $directoryService) { $directoryService.tombstoneLifetime } else { $null }
DsHeuristics = if ($null -ne $directoryService) { $directoryService.dSHeuristics } else { $null }
SpnMappings = if ($null -ne $directoryService) { @($directoryService.sPNMappings | Where-Object { $null -ne $_ }) } else { @() }
Expand Down
102 changes: 102 additions & 0 deletions powershell/tests/functions/FalsePassOnEmptySource.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
Describe 'Checks that must not pass when their data source returns nothing' {
BeforeAll {
Import-Module $PSScriptRoot/../../Maester.psd1 -Force
. "$PSScriptRoot/../helpers/Use-MtModuleFunction.ps1"
# A native test, so the function is not exported.
Use-MtModuleFunction -Name Test-MtAdTombstoneLifetime
}

BeforeEach {
$script:TestResult = $null
$script:SkippedBecause = $null

Mock -ModuleName Maester Get-MtLicenseInformation { return $true }
Mock -ModuleName Maester Add-MtTestResultDetail {
param($Result, $SkippedBecause)

$script:TestResult = $Result
$script:SkippedBecause = $SkippedBecause
}
}

Context 'AD-FOR-03 tombstone lifetime' {
It 'fails with the unable-to-retrieve message when the lifetime cannot be read' {
# An unreadable lifetime must not be substituted with a default: the comparison would
# then be impossible to fail and the result would report an invented number.
Mock -ModuleName Maester Get-MtADDomainState {
[pscustomobject]@{
Configuration = [pscustomobject]@{ TombstoneLifetime = $null }
Forest = [pscustomobject]@{ Name = 'contoso.com' }
}
}

Test-MtAdTombstoneLifetime | Should -BeFalse
$script:TestResult | Should -Match 'Unable to retrieve tombstone lifetime'
}

It 'does not invent a lifetime of 180 days when the value is missing' {
# DirectoryServiceRead false: the Directory Service object was not read at all, so
# there is no effective lifetime to report. This is the genuine retrieval failure.
Mock -ModuleName Maester Get-MtADDomainState {
[pscustomobject]@{
Configuration = [pscustomobject]@{ TombstoneLifetime = $null; DirectoryServiceRead = $false }
Forest = [pscustomobject]@{ Name = 'contoso.com' }
}
}

$null = Test-MtAdTombstoneLifetime
$script:TestResult | Should -Not -Match '180 days'
$script:TestResult | Should -Not -Match 'Meets recommendation'
}

It 'does not pass on an inferred 60 days when the attribute is absent' {
# Seeing the Directory Service object is not proof the attribute was readable, so an
# absent attribute must not be read as 'not set' and passed on the 60-day default.
Mock -ModuleName Maester Get-MtADDomainState {
[pscustomobject]@{
Configuration = [pscustomobject]@{ TombstoneLifetime = $null; DirectoryServiceRead = $true }
Forest = [pscustomobject]@{ Name = 'contoso.com' }
}
}

Test-MtAdTombstoneLifetime | Should -BeFalse
$script:TestResult | Should -Match '60 days'
$script:TestResult | Should -Match 'cannot read the attribute'
$script:TestResult | Should -Not -Match 'Meets recommendation'
}

It 'passes and reports the value when the lifetime is readable' {
Mock -ModuleName Maester Get-MtADDomainState {
[pscustomobject]@{
Configuration = [pscustomobject]@{ TombstoneLifetime = 365 }
Forest = [pscustomobject]@{ Name = 'contoso.com' }
}
}

Test-MtAdTombstoneLifetime | Should -BeTrue
$script:TestResult | Should -Match '365 days'
$script:TestResult | Should -Match 'Meets recommendation'
}

It 'reports below the recommendation for a forest on the older default of 60 days' {
# The substituted 180 could also claim the recommendation was met on an older forest,
# whose default is 60.
Mock -ModuleName Maester Get-MtADDomainState {
[pscustomobject]@{
Configuration = [pscustomobject]@{ TombstoneLifetime = 60 }
Forest = [pscustomobject]@{ Name = 'contoso.com' }
}
}

$null = Test-MtAdTombstoneLifetime
$script:TestResult | Should -Match 'Below recommendation'
}

It 'skips when Active Directory is not reachable at all' {
Mock -ModuleName Maester Get-MtADDomainState { return $null }

$null -eq (Test-MtAdTombstoneLifetime) | Should -BeTrue
$script:SkippedBecause | Should -Be 'NotConnectedActiveDirectory'
}
}
}
28 changes: 18 additions & 10 deletions tests/ad/domain/Test.AD-FOR-03.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,27 @@

# Default values: 60 days for older forests, 180 days for newer forests
$defaultValue = 180
if ($null -eq $tombstoneLifetime) {
$tombstoneLifetime = $defaultValue
$isDefault = $true
} else {
$isDefault = $false
}

# Test passes if we successfully retrieved tombstone lifetime
# The lifetime is never substituted with a default. A value this check supplies itself would
# make the comparison below impossible to fail, and 180 is only the default for newer forests.

# A $null lifetime has two causes LDAP does not distinguish: the forest never set the
# attribute, or this account cannot read it. Both return the object with the attribute
# omitted, so the check reports what 'not set' would mean without claiming it retrieved a
# value.
$directoryServiceRead = [bool]$adState.Configuration.DirectoryServiceRead
$notSetLifetime = 60
$isAbsent = $directoryServiceRead -and $null -eq $tombstoneLifetime

# Passes only when the forest actually returned a lifetime.
$testResult = $null -ne $tombstoneLifetime

# Generate markdown results
if ($testResult) {
$result = "| Property | Value |" + "`n"
$result += "| --- | --- |" + "`n"
$result += "| Tombstone Lifetime | $tombstoneLifetime days |" + "`n"
$result += "| Default Value | $defaultValue days |" + "`n"
$result += "| Using Default | $isDefault |" + "`n"
$result += "| Recommended minimum | $defaultValue days |" + "`n"
$result += "| Forest Name | $($adState.Forest.Name) |" + "`n"

$recommendation = if ($tombstoneLifetime -ge 180) { "✅ Meets recommendation (180+ days)" } else { "⚠️ Below recommendation (180 days)" }
Expand All @@ -76,7 +80,11 @@
$testResultMarkdown = "The Active Directory tombstone lifetime has been retrieved. Deleted objects are retained for $tombstoneLifetime days before permanent removal.`n`n%TestResult%"
$testResultMarkdown = $testResultMarkdown -replace "%TestResult%", $result
} else {
$testResultMarkdown = "Unable to retrieve tombstone lifetime. Ensure you have appropriate permissions and the Active Directory module is installed."
$testResultMarkdown = if ($isAbsent) {
"The Directory Service object was read, but it carries no ``tombstoneLifetime``. That is either because the forest never set one - in which case Active Directory uses $notSetLifetime days, below the 180-day recommendation - or because this account cannot read the attribute. Active Directory returns the object with the attribute omitted in both cases, so the two cannot be told apart from here, and this check does not report a lifetime it did not read. Grant read access to ``tombstoneLifetime`` on CN=Directory Service,CN=Windows NT,CN=Services to settle which it is."
} else {
"Unable to retrieve tombstone lifetime. Ensure you have appropriate permissions and the Active Directory module is installed."
}
}

Add-MtTestResultDetail -Result $testResultMarkdown
Expand Down
Loading