windows: fix first-boot races and non-idempotent stages
A from-scratch rebuild surfaced three defects that only appear on a clean deployment: - dc01-post: `Set-ADObject -Identity <sAMAccountName>` raises ADIdentityNotFoundException even right after New-ADUser, so resolve the DN once and reuse it. The stage aborted before DNS, flags, DHCP and the audit policy, leaving a half-built domain. - bootstrap ran the role script at first boot while `provision` also ran it. fs01 tried to join a domain that did not exist yet, and dc01 ran two promotions concurrently; the loser wrote "FAIL Stream was not readable". Roles are now only started by the harness, which orders them. - fs01: the `.url` MIME existence test read the staticContent element (-Name '.') instead of its collection, so any re-run died on a duplicate entry. Test -Name 'collection'. Every stage now takes a PID-based lock: a duplicate start exits instead of racing the real one. Validated on a fresh build: doctor 0 warnings, and the full path (site -> SMB -> AS-REP -> Kerberoast -> WinRM -> ACL -> replication -> DCSync) reproduces.
This commit is contained in:
@@ -98,10 +98,13 @@ try {
|
|||||||
Set-Content -Path "$base\stamp.txt" -Encoding ascii
|
Set-Content -Path "$base\stamp.txt" -Encoding ascii
|
||||||
|
|
||||||
# ------------------------------------------------------------------ role
|
# ------------------------------------------------------------------ role
|
||||||
|
# The role stages are driven by the host (`./lab.sh provision`), which orders
|
||||||
|
# them: the DC must be promoted before a member tries to join. Running them
|
||||||
|
# here as well made every guest race the harness — fs01 joined before the
|
||||||
|
# domain existed, and dc01 ran two promotions at once.
|
||||||
$role = Join-Path $here "roles\$($Config.Role).ps1"
|
$role = Join-Path $here "roles\$($Config.Role).ps1"
|
||||||
if (Test-Path $role) {
|
if (Test-Path $role) {
|
||||||
Write-Host "bootstrap: running role $($Config.Role)"
|
Write-Host "bootstrap: role $($Config.Role) staged, waiting for ./lab.sh provision"
|
||||||
& $role
|
|
||||||
} else {
|
} else {
|
||||||
Write-Host "bootstrap: no role script for '$($Config.Role)' yet (phase 5)"
|
Write-Host "bootstrap: no role script for '$($Config.Role)' yet (phase 5)"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,29 @@ function Fail-Stage {
|
|||||||
throw $Message
|
throw $Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# A stage can be started twice: the harness schedules it, and a retried
|
||||||
|
# transport call or a manual run can schedule it again. Two concurrent
|
||||||
|
# Install-ADDSForest calls fight over the same promotion and one of them
|
||||||
|
# writes FAIL, which stops a provisioning that actually worked.
|
||||||
|
function Enter-StageLock {
|
||||||
|
param([string]$Path)
|
||||||
|
$lock = "$Path.lock"
|
||||||
|
if (Test-Path $lock) {
|
||||||
|
$owner = Get-Content $lock -ErrorAction SilentlyContinue
|
||||||
|
if ($owner -and (Get-Process -Id ([int]$owner) -ErrorAction SilentlyContinue)) {
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
# The owner is gone (reboot, crash): the lock is stale.
|
||||||
|
}
|
||||||
|
Set-Content -Path $lock -Value $PID -Encoding ascii
|
||||||
|
return $true
|
||||||
|
}
|
||||||
|
|
||||||
|
function Exit-StageLock {
|
||||||
|
param([string]$Path)
|
||||||
|
Remove-Item "$Path.lock" -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
|
||||||
function Wait-ForDomain {
|
function Wait-ForDomain {
|
||||||
param([int]$TimeoutSeconds = 600)
|
param([int]$TimeoutSeconds = 600)
|
||||||
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
|
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
|
||||||
|
|||||||
@@ -4,6 +4,10 @@
|
|||||||
. "$PSScriptRoot\..\lib\common.ps1"
|
. "$PSScriptRoot\..\lib\common.ps1"
|
||||||
|
|
||||||
$self = $PSCommandPath
|
$self = $PSCommandPath
|
||||||
|
if (-not (Enter-StageLock $self)) {
|
||||||
|
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||||
|
return
|
||||||
|
}
|
||||||
Write-LabLog 'dc01: post stage starting'
|
Write-LabLog 'dc01: post stage starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -119,14 +123,17 @@ try {
|
|||||||
$domainAcl.CommitChanges()
|
$domainAcl.CommitChanges()
|
||||||
Write-LabLog 'dc01: Sauvegarde-AD granted replication rights'
|
Write-LabLog 'dc01: Sauvegarde-AD granted replication rights'
|
||||||
|
|
||||||
|
# Set-ADObject does not resolve a sAMAccountName reliably (it raises
|
||||||
|
# ADIdentityNotFoundException), so resolve the DN once and use it below.
|
||||||
|
$plainDn = (Get-ADUser 'm.legault').DistinguishedName
|
||||||
|
|
||||||
# m.legault must not carry a stale adminCount flag from an earlier design,
|
# m.legault must not carry a stale adminCount flag from an earlier design,
|
||||||
# or SDProp would keep treating the account as protected and wipe the ACE.
|
# or SDProp would keep treating the account as protected and wipe the ACE.
|
||||||
Set-ADObject -Identity 'm.legault' -Replace @{ adminCount = 0 } -ErrorAction SilentlyContinue
|
Set-ADObject -Identity $plainDn -Replace @{ adminCount = 0 } -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
# Realistic delegation: the service account manages a plain user who is a
|
# Realistic delegation: the service account manages a plain user who is a
|
||||||
# member of the server-admin group. m.legault is NOT in a protected group,
|
# member of the server-admin group. m.legault is NOT in a protected group,
|
||||||
# so SDProp leaves this ACE alone — unlike the Domain Admin target above.
|
# so SDProp leaves this ACE alone — unlike the Domain Admin target above.
|
||||||
$plainDn = (Get-ADUser 'm.legault').DistinguishedName
|
|
||||||
if (-not (dsacls.exe $plainDn | Select-String 'svc_backup')) {
|
if (-not (dsacls.exe $plainDn | Select-String 'svc_backup')) {
|
||||||
$null = dsacls.exe $plainDn /G '@@NETBIOS@@\svc_backup:GA'
|
$null = dsacls.exe $plainDn /G '@@NETBIOS@@\svc_backup:GA'
|
||||||
Write-LabLog 'dc01: GenericAll granted to svc_backup over m.legault'
|
Write-LabLog 'dc01: GenericAll granted to svc_backup over m.legault'
|
||||||
@@ -240,4 +247,6 @@ try {
|
|||||||
Complete-Stage $self 'ok'
|
Complete-Stage $self 'ok'
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
} finally {
|
||||||
|
Exit-StageLock $self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
. "$PSScriptRoot\..\lib\common.ps1"
|
. "$PSScriptRoot\..\lib\common.ps1"
|
||||||
|
|
||||||
$self = $PSCommandPath
|
$self = $PSCommandPath
|
||||||
|
if (-not (Enter-StageLock $self)) {
|
||||||
|
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||||
|
return
|
||||||
|
}
|
||||||
Write-LabLog 'dc01: stage 1 starting'
|
Write-LabLog 'dc01: stage 1 starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -37,4 +41,6 @@ try {
|
|||||||
Restart-Computer -Force
|
Restart-Computer -Force
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
} finally {
|
||||||
|
Exit-StageLock $self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
. "$PSScriptRoot\..\lib\common.ps1"
|
. "$PSScriptRoot\..\lib\common.ps1"
|
||||||
|
|
||||||
$self = $PSCommandPath
|
$self = $PSCommandPath
|
||||||
|
if (-not (Enter-StageLock $self)) {
|
||||||
|
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||||
|
return
|
||||||
|
}
|
||||||
Write-LabLog 'fs01: starting'
|
Write-LabLog 'fs01: starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -29,14 +33,19 @@ try {
|
|||||||
# IIS ships no MIME mapping for .url; the archived "Your Love" shortcut
|
# IIS ships no MIME mapping for .url; the archived "Your Love" shortcut
|
||||||
# needs one or IIS answers 404.3.
|
# needs one or IIS answers 404.3.
|
||||||
Import-Module WebAdministration
|
Import-Module WebAdministration
|
||||||
|
# -Name '.' returns the staticContent element, not its entries, so the
|
||||||
|
# existence test always failed and a re-run died on a duplicate entry.
|
||||||
|
# The collection of mappings is -Name 'collection'.
|
||||||
$urlMime = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
|
$urlMime = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
|
||||||
-Filter 'system.webServer/staticContent' -Name '.' |
|
-Filter 'system.webServer/staticContent' -Name 'collection' |
|
||||||
Where-Object { $_.fileExtension -eq '.url' }
|
Where-Object { $_.fileExtension -eq '.url' }
|
||||||
if (-not $urlMime) {
|
if (-not $urlMime) {
|
||||||
Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
|
Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
|
||||||
-Filter 'system.webServer/staticContent' -Name '.' `
|
-Filter 'system.webServer/staticContent' -Name '.' `
|
||||||
-Value @{ fileExtension = '.url'; mimeType = 'application/internet-shortcut' }
|
-Value @{ fileExtension = '.url'; mimeType = 'application/internet-shortcut' }
|
||||||
Write-LabLog 'fs01: .url MIME mapping added'
|
Write-LabLog 'fs01: .url MIME mapping added'
|
||||||
|
} else {
|
||||||
|
Write-LabLog 'fs01: .url MIME mapping already present'
|
||||||
}
|
}
|
||||||
|
|
||||||
$root = 'C:\inetpub\wwwroot'
|
$root = 'C:\inetpub\wwwroot'
|
||||||
@@ -81,4 +90,6 @@ Ne pas supprimer.
|
|||||||
Restart-Computer -Force
|
Restart-Computer -Force
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
} finally {
|
||||||
|
Exit-StageLock $self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
. "$PSScriptRoot\..\lib\common.ps1"
|
. "$PSScriptRoot\..\lib\common.ps1"
|
||||||
|
|
||||||
$self = $PSCommandPath
|
$self = $PSCommandPath
|
||||||
|
if (-not (Enter-StageLock $self)) {
|
||||||
|
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||||
|
return
|
||||||
|
}
|
||||||
Write-LabLog 'ws01: post stage starting'
|
Write-LabLog 'ws01: post stage starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -36,4 +40,6 @@ try {
|
|||||||
Complete-Stage $self 'ok'
|
Complete-Stage $self 'ok'
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
} finally {
|
||||||
|
Exit-StageLock $self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,10 @@
|
|||||||
. "$PSScriptRoot\..\lib\common.ps1"
|
. "$PSScriptRoot\..\lib\common.ps1"
|
||||||
|
|
||||||
$self = $PSCommandPath
|
$self = $PSCommandPath
|
||||||
|
if (-not (Enter-StageLock $self)) {
|
||||||
|
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||||
|
return
|
||||||
|
}
|
||||||
Write-LabLog 'ws01: starting'
|
Write-LabLog 'ws01: starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -27,4 +31,6 @@ try {
|
|||||||
Restart-Computer -Force
|
Restart-Computer -Force
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
} finally {
|
||||||
|
Exit-StageLock $self
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user