Projektarbeit-MYP/generate_ssl_certs_copy.ps1

124 lines
4.7 KiB
PowerShell

# MYP - SSL-Zertifikat-Generator für Windows
# Erstellt selbstsignierte Zertifikate für die HTTPS-Kommunikation
# Überprüfen, ob das Skript als Administrator ausgeführt wird
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "Dieses Skript muss mit Administrator-Rechten ausgeführt werden." -ForegroundColor Red
Write-Host "Bitte starten Sie PowerShell als Administrator und führen Sie das Skript erneut aus." -ForegroundColor Yellow
Exit 1
}
# Header anzeigen
Write-Host "================================================================" -ForegroundColor Blue
Write-Host " MYP - SSL-Zertifikat-Generator " -ForegroundColor Blue
Write-Host "================================================================" -ForegroundColor Blue
Write-Host ""
# Parameter definieren
$certDir = "./backend/instance/ssl"
$backendCertFile = "$certDir/myp.crt"
$backendKeyFile = "$certDir/myp.key"
$frontendCertFile = "$certDir/frontend.crt"
$frontendKeyFile = "$certDir/frontend.key"
$backendHostname = "raspberrypi"
$frontendHostname = "m040tbaraspi001.de040.corpintra.net"
$validityDays = 3650 # 10 Jahre
# Verzeichnis erstellen, falls es nicht existiert
if (!(Test-Path $certDir)) {
Write-Host "Erstelle Verzeichnis $certDir..." -ForegroundColor Yellow
New-Item -ItemType Directory -Path $certDir -Force | Out-Null
}
# Prüfen, ob OpenSSL installiert ist
$openSSLInstalled = $null
try {
$openSSLInstalled = Get-Command openssl -ErrorAction SilentlyContinue
} catch {}
if ($null -eq $openSSLInstalled) {
Write-Host "OpenSSL konnte nicht gefunden werden!" -ForegroundColor Red
Write-Host "Bitte installieren Sie OpenSSL für Windows und stellen Sie sicher, dass es im PATH verfügbar ist." -ForegroundColor Yellow
Write-Host "Sie können OpenSSL von https://slproweb.com/products/Win32OpenSSL.html herunterladen." -ForegroundColor Yellow
Exit 1
}
# Funktion zum Erstellen eines selbstsignierten Zertifikats
function Create-SelfSignedCert {
param(
[string]$CertFile,
[string]$KeyFile,
[string]$Hostname,
[int]$Days
)
Write-Host "Erstelle selbstsigniertes SSL-Zertifikat für $Hostname..." -ForegroundColor Green
# OpenSSL-Konfiguration erstellen
$configFile = "$certDir/openssl_$($Hostname.Split('.')[0]).cnf"
$configContent = @"
[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = req_distinguished_name
x509_extensions = v3_req
[req_distinguished_name]
C = DE
ST = Berlin
L = Berlin
O = Mercedes-Benz AG
OU = Werk 040 Berlin
CN = $Hostname
[v3_req]
keyUsage = critical, digitalSignature, keyAgreement
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = $Hostname
DNS.2 = localhost
IP.1 = 127.0.0.1
"@
$configContent | Out-File -FilePath $configFile -Encoding ASCII
# Zertifikat erstellen
openssl req -x509 -nodes -days $Days -newkey rsa:2048 -keyout $KeyFile -out $CertFile -config $configFile
# Berechtigungen setzen
if (Test-Path $KeyFile) {
# Schlüsseldatei auf 'Nur-Lesen' setzen
Set-ItemProperty -Path $KeyFile -Name IsReadOnly -Value $true
}
# Fingerprint anzeigen
$fingerprint = openssl x509 -noout -fingerprint -sha256 -in $CertFile
Write-Host "Zertifikat erstellt: $CertFile" -ForegroundColor Green
Write-Host "Fingerprint: $fingerprint" -ForegroundColor Yellow
Write-Host ""
}
# Backend-Zertifikat erstellen
Create-SelfSignedCert -CertFile $backendCertFile -KeyFile $backendKeyFile -Hostname $backendHostname -Days $validityDays
# Frontend-Zertifikat erstellen
Create-SelfSignedCert -CertFile $frontendCertFile -KeyFile $frontendKeyFile -Hostname $frontendHostname -Days $validityDays
# Bestätigung und Hinweise
Write-Host "SSL-Zertifikate wurden erfolgreich erstellt!" -ForegroundColor Green
Write-Host ""
Write-Host "Backend-Zertifikat: $backendCertFile" -ForegroundColor Cyan
Write-Host "Frontend-Zertifikat: $frontendCertFile" -ForegroundColor Cyan
Write-Host ""
Write-Host "Hinweise zur Verwendung:" -ForegroundColor Yellow
Write-Host "1. Stellen Sie sicher, dass die Zertifikatpfade in den Konfigurationsdateien korrekt sind." -ForegroundColor White
Write-Host "2. Beim ersten Zugriff auf die Anwendung müssen Sie das Zertifikat im Browser akzeptieren." -ForegroundColor White
Write-Host "3. In einer Produktionsumgebung sollten Sie offiziell signierte Zertifikate verwenden." -ForegroundColor White
Write-Host ""
Write-Host "Starten Sie die Anwendung mit:" -ForegroundColor Blue
Write-Host "docker-compose up -d" -ForegroundColor White