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:
@@ -27,6 +27,29 @@ function Fail-Stage {
|
||||
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 {
|
||||
param([int]$TimeoutSeconds = 600)
|
||||
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
. "$PSScriptRoot\..\lib\common.ps1"
|
||||
|
||||
$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'
|
||||
|
||||
try {
|
||||
@@ -119,14 +123,17 @@ try {
|
||||
$domainAcl.CommitChanges()
|
||||
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,
|
||||
# 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
|
||||
# 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.
|
||||
$plainDn = (Get-ADUser 'm.legault').DistinguishedName
|
||||
if (-not (dsacls.exe $plainDn | Select-String 'svc_backup')) {
|
||||
$null = dsacls.exe $plainDn /G '@@NETBIOS@@\svc_backup:GA'
|
||||
Write-LabLog 'dc01: GenericAll granted to svc_backup over m.legault'
|
||||
@@ -240,4 +247,6 @@ try {
|
||||
Complete-Stage $self 'ok'
|
||||
} catch {
|
||||
Fail-Stage $self $_.Exception.Message
|
||||
} finally {
|
||||
Exit-StageLock $self
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
. "$PSScriptRoot\..\lib\common.ps1"
|
||||
|
||||
$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'
|
||||
|
||||
try {
|
||||
@@ -37,4 +41,6 @@ try {
|
||||
Restart-Computer -Force
|
||||
} catch {
|
||||
Fail-Stage $self $_.Exception.Message
|
||||
} finally {
|
||||
Exit-StageLock $self
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
. "$PSScriptRoot\..\lib\common.ps1"
|
||||
|
||||
$self = $PSCommandPath
|
||||
if (-not (Enter-StageLock $self)) {
|
||||
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||
return
|
||||
}
|
||||
Write-LabLog 'fs01: starting'
|
||||
|
||||
try {
|
||||
@@ -29,14 +33,19 @@ try {
|
||||
# IIS ships no MIME mapping for .url; the archived "Your Love" shortcut
|
||||
# needs one or IIS answers 404.3.
|
||||
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' `
|
||||
-Filter 'system.webServer/staticContent' -Name '.' |
|
||||
-Filter 'system.webServer/staticContent' -Name 'collection' |
|
||||
Where-Object { $_.fileExtension -eq '.url' }
|
||||
if (-not $urlMime) {
|
||||
Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
|
||||
-Filter 'system.webServer/staticContent' -Name '.' `
|
||||
-Value @{ fileExtension = '.url'; mimeType = 'application/internet-shortcut' }
|
||||
Write-LabLog 'fs01: .url MIME mapping added'
|
||||
} else {
|
||||
Write-LabLog 'fs01: .url MIME mapping already present'
|
||||
}
|
||||
|
||||
$root = 'C:\inetpub\wwwroot'
|
||||
@@ -81,4 +90,6 @@ Ne pas supprimer.
|
||||
Restart-Computer -Force
|
||||
} catch {
|
||||
Fail-Stage $self $_.Exception.Message
|
||||
} finally {
|
||||
Exit-StageLock $self
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
. "$PSScriptRoot\..\lib\common.ps1"
|
||||
|
||||
$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'
|
||||
|
||||
try {
|
||||
@@ -36,4 +40,6 @@ try {
|
||||
Complete-Stage $self 'ok'
|
||||
} catch {
|
||||
Fail-Stage $self $_.Exception.Message
|
||||
} finally {
|
||||
Exit-StageLock $self
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
. "$PSScriptRoot\..\lib\common.ps1"
|
||||
|
||||
$self = $PSCommandPath
|
||||
if (-not (Enter-StageLock $self)) {
|
||||
Write-LabLog 'stage already running on this guest; duplicate start ignored'
|
||||
return
|
||||
}
|
||||
Write-LabLog 'ws01: starting'
|
||||
|
||||
try {
|
||||
@@ -27,4 +31,6 @@ try {
|
||||
Restart-Computer -Force
|
||||
} catch {
|
||||
Fail-Stage $self $_.Exception.Message
|
||||
} finally {
|
||||
Exit-StageLock $self
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user