forked from mtusi2026/mtusi2026lab7
89 lines
4.0 KiB
PowerShell
89 lines
4.0 KiB
PowerShell
[CmdletBinding()]
|
|
param([string]$PkiDirectory, [int]$LeafValidityDays = 30, [switch]$Force)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
if([string]::IsNullOrWhiteSpace($PkiDirectory)){
|
|
$PkiDirectory = Join-Path $PSScriptRoot '..\secrets\pki'
|
|
}
|
|
$pki = (Resolve-Path -LiteralPath $PkiDirectory).Path
|
|
$caCertificate = Join-Path $pki 'service-ca\ca.crt'
|
|
$caKey = Join-Path $pki 'service-ca\ca.key'
|
|
if(-not (Test-Path -LiteralPath $caCertificate) -or -not (Test-Path -LiteralPath $caKey)){
|
|
throw 'Existing local service CA certificate/private key is required'
|
|
}
|
|
|
|
$opensslCommand = Get-Command openssl -ErrorAction SilentlyContinue
|
|
$openssl = if($opensslCommand){ $opensslCommand.Source } else { $null }
|
|
if(-not $openssl){
|
|
$git = Get-Command git -ErrorAction SilentlyContinue
|
|
if($git){
|
|
$gitRoot = Split-Path (Split-Path $git.Source -Parent) -Parent
|
|
$candidate = Join-Path $gitRoot 'usr\bin\openssl.exe'
|
|
if(Test-Path -LiteralPath $candidate){ $openssl = $candidate }
|
|
}
|
|
}
|
|
if(-not $openssl){ throw 'OpenSSL is required to extend the existing local PKI' }
|
|
|
|
function Invoke-OpenSsl([string[]]$Arguments) {
|
|
$previous = $ErrorActionPreference
|
|
$ErrorActionPreference = 'Continue'
|
|
try {
|
|
& $openssl @Arguments 2>$null
|
|
$exitCode = $LASTEXITCODE
|
|
} finally {
|
|
$ErrorActionPreference = $previous
|
|
}
|
|
if($exitCode -ne 0){ throw "OpenSSL command failed: $($Arguments[0])" }
|
|
}
|
|
|
|
function New-SerialHex {
|
|
$bytes = [byte[]]::new(16)
|
|
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
|
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
|
|
$bytes[0] = $bytes[0] -band 0x7f
|
|
return '0x' + (($bytes | ForEach-Object { $_.ToString('x2') }) -join '')
|
|
}
|
|
|
|
function New-GatewayLeaf([string]$Name, [bool]$ServerAuth, [string]$DnsName, [string]$Uri) {
|
|
$directory = Join-Path $pki $Name
|
|
if((Test-Path -LiteralPath $directory) -and -not $Force){
|
|
throw "$directory already exists; use -Force only for intentional gateway certificate rotation"
|
|
}
|
|
New-Item -ItemType Directory -Path $directory -Force | Out-Null
|
|
$key = Join-Path $directory 'tls.key'
|
|
$csr = Join-Path $directory 'request.csr'
|
|
$certificate = Join-Path $directory 'tls.crt'
|
|
$caCopy = Join-Path $directory 'ca.crt'
|
|
$extensions = Join-Path $directory 'extensions.cnf'
|
|
$eku = if($ServerAuth){ 'serverAuth' } else { 'clientAuth' }
|
|
$sans = @()
|
|
if($DnsName){ $sans += "DNS:$DnsName" }
|
|
if($Uri){ $sans += "URI:$Uri" }
|
|
$extensionText = @(
|
|
'basicConstraints=critical,CA:FALSE',
|
|
'keyUsage=critical,digitalSignature,keyEncipherment',
|
|
"extendedKeyUsage=$eku",
|
|
"subjectAltName=$($sans -join ',')",
|
|
'authorityKeyIdentifier=keyid,issuer'
|
|
) -join "`n"
|
|
[System.IO.File]::WriteAllText($extensions, $extensionText + "`n", [System.Text.Encoding]::ASCII)
|
|
try {
|
|
Invoke-OpenSsl @('genpkey','-algorithm','RSA','-pkeyopt','rsa_keygen_bits:3072','-out',$key)
|
|
Invoke-OpenSsl @('req','-new','-key',$key,'-subj',"/CN=$Name",'-out',$csr)
|
|
Invoke-OpenSsl @('x509','-req','-in',$csr,'-CA',$caCertificate,'-CAkey',$caKey,
|
|
'-set_serial',(New-SerialHex),'-days',"$LeafValidityDays",'-sha256','-extfile',$extensions,'-out',$certificate)
|
|
Copy-Item -LiteralPath $caCertificate -Destination $caCopy -Force
|
|
} finally {
|
|
Remove-Item -LiteralPath $csr,$extensions -Force -ErrorAction SilentlyContinue
|
|
}
|
|
}
|
|
|
|
New-GatewayLeaf 'rabbit-api-gateway' $true 'rabbit-api-gateway' 'spiffe://hla-lab7/rabbit-api-gateway'
|
|
New-GatewayLeaf 'rabbit-worker-gateway' $true 'rabbit-worker-gateway' 'spiffe://hla-lab7/rabbit-worker-gateway'
|
|
New-GatewayLeaf 'otel-gateway' $true 'otel-gateway' 'spiffe://hla-lab7/otel-gateway'
|
|
New-GatewayLeaf 'rabbit-api-gateway-client' $false '' 'spiffe://hla-lab7/rabbit-api-gateway'
|
|
New-GatewayLeaf 'rabbit-worker-gateway-client' $false '' 'spiffe://hla-lab7/rabbit-worker-gateway'
|
|
New-GatewayLeaf 'otel-gateway-client' $false '' 'spiffe://hla-lab7/otel-gateway'
|
|
|
|
Write-Host "Created six local gateway certificates under $pki; private keys were not printed."
|