Implement Lab 7 security and infrastructure
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$Root = Split-Path -Parent $PSScriptRoot
|
||||
$Tls = Join-Path $Root "secrets/tls"
|
||||
|
||||
New-Item -ItemType Directory -Force -Path $Tls | Out-Null
|
||||
|
||||
$OpenSSL = Get-Command openssl -ErrorAction SilentlyContinue
|
||||
if (-not $OpenSSL) {
|
||||
throw "OpenSSL not found in PATH"
|
||||
}
|
||||
|
||||
function Run-OpenSSL([string[]]$OpenSslArgs) {
|
||||
& openssl @OpenSslArgs
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "OpenSSL failed: $OpenSslArgs"
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path "$Tls/ca.key")) {
|
||||
Run-OpenSSL @(
|
||||
"req", "-x509", "-newkey", "rsa:3072",
|
||||
"-sha256", "-days", "3650", "-nodes",
|
||||
"-keyout", "$Tls/ca.key",
|
||||
"-out", "$Tls/ca.crt",
|
||||
"-subj", "/CN=Lab7 Server Root CA",
|
||||
"-addext", "basicConstraints=critical,CA:TRUE",
|
||||
"-addext", "keyUsage=critical,keyCertSign,cRLSign"
|
||||
)
|
||||
}
|
||||
|
||||
if (-not (Test-Path "$Tls/lb-ca.key")) {
|
||||
Run-OpenSSL @(
|
||||
"req", "-x509", "-newkey", "rsa:3072",
|
||||
"-sha256", "-days", "3650", "-nodes",
|
||||
"-keyout", "$Tls/lb-ca.key",
|
||||
"-out", "$Tls/lb-ca.crt",
|
||||
"-subj", "/CN=Lab7 Ingress Client CA",
|
||||
"-addext", "basicConstraints=critical,CA:TRUE",
|
||||
"-addext", "keyUsage=critical,keyCertSign,cRLSign"
|
||||
)
|
||||
}
|
||||
|
||||
function New-ServerCert([string]$Name, [string]$San) {
|
||||
$key = "$Tls/$Name.key"
|
||||
$csr = "$Tls/$Name.csr"
|
||||
$crt = "$Tls/$Name.crt"
|
||||
$ext = "$Tls/$Name.ext"
|
||||
|
||||
if (Test-Path $crt) {
|
||||
return
|
||||
}
|
||||
|
||||
Set-Content -Encoding ascii $ext "basicConstraints=critical,CA:FALSE`nsubjectAltName=$San`nextendedKeyUsage=serverAuth`nkeyUsage=digitalSignature,keyEncipherment"
|
||||
|
||||
Run-OpenSSL @(
|
||||
"req", "-new", "-newkey", "rsa:2048", "-nodes",
|
||||
"-keyout", $key,
|
||||
"-out", $csr,
|
||||
"-subj", "/CN=$Name"
|
||||
)
|
||||
|
||||
Run-OpenSSL @(
|
||||
"x509", "-req", "-in", $csr,
|
||||
"-CA", "$Tls/ca.crt",
|
||||
"-CAkey", "$Tls/ca.key",
|
||||
"-CAcreateserial",
|
||||
"-out", $crt,
|
||||
"-days", "825",
|
||||
"-sha256",
|
||||
"-extfile", $ext
|
||||
)
|
||||
|
||||
Remove-Item $csr, $ext -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
New-ServerCert "nginx" "DNS:localhost,IP:127.0.0.1"
|
||||
New-ServerCert "api" "DNS:api,DNS:localhost,IP:127.0.0.1"
|
||||
New-ServerCert "openbao" "DNS:openbao,DNS:localhost,IP:127.0.0.1"
|
||||
New-ServerCert "tls-proxy" "DNS:tls-proxy,DNS:localhost,IP:127.0.0.1"
|
||||
|
||||
if (-not (Test-Path "$Tls/lb-client.crt")) {
|
||||
Set-Content -Encoding ascii "$Tls/lb-client.ext" "basicConstraints=critical,CA:FALSE`nsubjectAltName=URI:spiffe://lab7/ingress/nginx`nextendedKeyUsage=clientAuth`nkeyUsage=digitalSignature,keyEncipherment"
|
||||
|
||||
Run-OpenSSL @(
|
||||
"req", "-new", "-newkey", "rsa:2048", "-nodes",
|
||||
"-keyout", "$Tls/lb-client.key",
|
||||
"-out", "$Tls/lb-client.csr",
|
||||
"-subj", "/CN=lab7-nginx/OU=ingress"
|
||||
)
|
||||
|
||||
Run-OpenSSL @(
|
||||
"x509", "-req", "-in", "$Tls/lb-client.csr",
|
||||
"-CA", "$Tls/lb-ca.crt",
|
||||
"-CAkey", "$Tls/lb-ca.key",
|
||||
"-CAcreateserial",
|
||||
"-out", "$Tls/lb-client.crt",
|
||||
"-days", "365",
|
||||
"-sha256",
|
||||
"-extfile", "$Tls/lb-client.ext"
|
||||
)
|
||||
|
||||
Remove-Item "$Tls/lb-client.csr", "$Tls/lb-client.ext" -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
Write-Host "Certificates created under secrets/tls."
|
||||
Write-Host "Private keys must never be committed to Git."
|
||||
Reference in New Issue
Block a user