Files
scadaproj/docs/deploy/2026-11-19-qa-nam-ldap/seed-secret.ps1
T

47 lines
3.1 KiB
PowerShell

# Seed nam-ldap-svc-password into every QA app store on THIS box, reading each store's location and KEK from the
# service's own env (never assumed). ScadaBridge Central's store is the shared SQL Server store: seeded once, on vq03,
# with a CLI variant that honours Secrets:Store:Provider=SqlServer. The value arrives in $vf over SMB and is deleted
# at the end; it is never printed. Usage: rendered with __BK__ (the box's protected _ldapcut dir).
$ErrorActionPreference = 'Stop'
$bk = '__BK__'
$vf = "$bk\_v.tmp"
$cli = 'E:\ApiInstall\_tools\secret-0.6.6\ZB.MOM.WW.Secrets.Cli.exe'
$cliSql = 'E:\ApiInstall\_tools\secret-0.6.6-sql\ZB.MOM.WW.Secrets.Cli.exe'
$name = 'nam-ldap-svc-password'
$desc = 'NAM LDAP lookup account (svc_igntadmin, QA interim 2026-11-19)'
& icacls.exe $vf /inheritance:r /grant:r 'BUILTIN\Administrators:F' 'NT AUTHORITY\SYSTEM:F' | Out-Null
function SvcEnv($svc) {
$k = "HKLM:\SYSTEM\CurrentControlSet\Services\$svc"
$l = @((Get-ItemProperty $k -ErrorAction SilentlyContinue).Environment) + @((Get-ItemProperty "$k\Parameters" -ErrorAction SilentlyContinue).AppEnvironmentExtra) | Where-Object { $_ }
$h = @{}; foreach ($e in $l) { $i = $e.IndexOf('='); if ($i -gt 0) { $h[$e.Substring(0,$i)] = $e.Substring($i+1) } }; $h
}
$targets = @('HistorianGateway','MxAccessGw','OtOpcUaHost')
if ($env:COMPUTERNAME -eq 'WONDER-APP-VQ03') { $targets += 'ScadaBridge-Central' }
$names = 'Secrets__SqlitePath','Secrets__MasterKey__Source','Secrets__MasterKey__FilePath','Secrets__MasterKey__EnvVarName','Secrets__Store__Provider','Secrets__SqlServer__ConnectionString','ZB_SECRETS_MASTER_KEY'
$fail = 0
try {
$want = [IO.File]::ReadAllText($vf)
"value file read, length $($want.Length)"
foreach ($svc in $targets) {
$e = SvcEnv $svc
foreach ($n in $names) { Remove-Item "Env:$n" -ErrorAction SilentlyContinue }
foreach ($n in $names) { if ($e.ContainsKey($n)) { Set-Item "Env:$n" $e[$n] } }
$exe = $cli
if ($svc -eq 'ScadaBridge-Central') { $env:Secrets__Store__Provider = 'SqlServer'; $exe = $cliSql
if (-not $env:Secrets__SqlServer__ConnectionString) { throw 'Central env has no Secrets__SqlServer__ConnectionString' } }
if ($env:Secrets__MasterKey__Source -ne 'File') { "$svc master key source '$($env:Secrets__MasterKey__Source)' - not File, skipping"; $fail++; continue }
$where = if ($svc -eq 'ScadaBridge-Central') { 'SqlServer shared store' } else { $env:Secrets__SqlitePath }
$out = & cmd.exe /c "`"$exe`" set $name - --description `"$desc`" < `"$vf`"" 2>&1
$rc = $LASTEXITCODE
$got = (& $exe get $name 2>$null | Out-String).TrimEnd("`r","`n")
$match = ($got -ceq $want)
if (-not $match) { try { $j = $got | ConvertFrom-Json; $match = ($j.value -ceq $want) } catch {} }
"$svc -> $where kek=$($env:Secrets__MasterKey__FilePath) set rc=$rc out=$(($out | Out-String).Trim()) readback-match=$match"
if ($rc -ne 0 -or -not $match) { $fail++ }
}
} finally {
Remove-Item $vf -Force -ErrorAction SilentlyContinue; "value file removed: $(-not (Test-Path $vf))"
foreach ($n in $names) { Remove-Item "Env:$n" -ErrorAction SilentlyContinue }
}
"SEED DONE failures=$fail"; exit $fail