Защита внутренних соединений и сервисных прав

This commit is contained in:
EgorAnti
2026-11-27 09:22:26 +03:00
parent 3c412926e5
commit a7463797e6
15 changed files with 453 additions and 54 deletions
+56 -13
View File
@@ -7,7 +7,26 @@ param(
)
$ErrorActionPreference = 'Stop'
Set-Location (Join-Path $PSScriptRoot '..')
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
Set-Location $projectRoot
if(-not (Test-Path (Join-Path $projectRoot 'compose.yaml'))) {
throw "Run the repository copy of this script: $projectRoot does not contain compose.yaml."
}
function Get-OpenSslPath {
$command = Get-Command openssl -ErrorAction SilentlyContinue
if($command){ return $command.Source }
$git = Get-Command git -ErrorAction SilentlyContinue
if($git){
$gitRoot = Split-Path (Split-Path $git.Source -Parent) -Parent
foreach($relative in 'usr\bin\openssl.exe','mingw64\bin\openssl.exe'){
$candidate = Join-Path $gitRoot $relative
if(Test-Path -LiteralPath $candidate){ return $candidate }
}
}
return $null
}
if([string]::IsNullOrWhiteSpace($PkiDirectory)){ $PkiDirectory = 'secrets\pki' }
$pki = (Resolve-Path -LiteralPath $PkiDirectory).Path
$now = [DateTimeOffset]::UtcNow
@@ -29,19 +48,43 @@ foreach($certificateFile in Get-ChildItem -LiteralPath $pki -Recurse -Filter tls
throw "$($certificateFile.Directory.Name) certificate has no SAN"
}
$privateKey = [System.Security.Cryptography.RSA]::Create()
$publicKey = $null
try {
$privateKey.ImportFromPem([System.IO.File]::ReadAllText($keyFile))
$privatePublic = [Convert]::ToBase64String($privateKey.ExportSubjectPublicKeyInfo())
$publicKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($certificate)
$certificatePublic = [Convert]::ToBase64String($publicKey.ExportSubjectPublicKeyInfo())
if($privatePublic -ne $certificatePublic){ throw "$($certificateFile.Directory.Name) private key does not match certificate" }
} finally {
if($null -ne $publicKey){ $publicKey.Dispose() }
$privateKey.Dispose()
$certificate.Dispose()
if($PSVersionTable.PSVersion.Major -ge 6) {
$privateKey = [System.Security.Cryptography.RSA]::Create()
$publicKey = $null
try {
$privateKey.ImportFromPem([System.IO.File]::ReadAllText($keyFile))
$privatePublic = [Convert]::ToBase64String($privateKey.ExportSubjectPublicKeyInfo())
$publicKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($certificate)
$certificatePublic = [Convert]::ToBase64String($publicKey.ExportSubjectPublicKeyInfo())
if($privatePublic -ne $certificatePublic){ throw "$($certificateFile.Directory.Name) private key does not match certificate" }
} finally {
if($null -ne $publicKey){ $publicKey.Dispose() }
$privateKey.Dispose()
}
} else {
# Windows PowerShell 5.1 has no ImportFromPem; compare public keys via OpenSSL.
$openssl = Get-OpenSslPath
if($null -eq $openssl){
throw "Cannot verify key pair for $($certificateFile.Directory.Name): install PowerShell 7+ or OpenSSL."
}
$previous = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
try {
$keyPublic = ((& $openssl pkey -in $keyFile -pubout 2>&1 | Out-String).Trim()) -replace "`r",''
$keyExit = $LASTEXITCODE
$certPublic = ((& $openssl x509 -in $certificateFile.FullName -pubkey -noout 2>&1 | Out-String).Trim()) -replace "`r",''
$certExit = $LASTEXITCODE
} finally {
$ErrorActionPreference = $previous
}
if($keyExit -ne 0 -or $certExit -ne 0){
throw "OpenSSL could not read key/certificate for $($certificateFile.Directory.Name)"
}
if($keyPublic -ne $certPublic){
throw "$($certificateFile.Directory.Name) private key does not match certificate"
}
}
$certificate.Dispose()
$leafCount++
}
if($leafCount -eq 0){ throw 'No leaf certificates found' }