"feat: Implement SSL Manager for enhanced security in backend"

This commit is contained in:
2025-05-26 12:21:48 +02:00
parent 6284b92076
commit 4282b52a3b
6 changed files with 1540 additions and 21 deletions

View File

@@ -91,6 +91,7 @@ function Test-Dependencies {
"node" = "Node.js"
"npm" = "Node Package Manager"
"git" = "Git"
"curl" = "cURL"
}
$allInstalled = $true
@@ -202,6 +203,409 @@ function Setup-Hosts {
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function Test-BackendConnection {
Show-Header "Backend-Verbindung prüfen"
Write-Host "Welches Backend möchten Sie testen?" -ForegroundColor $ColorInfo
Write-Host "1. Lokales Backend (localhost:443)" -ForegroundColor $ColorCommand
Write-Host "2. Raspberry Pi Backend (192.168.0.105:5000)" -ForegroundColor $ColorCommand
Write-Host "3. Benutzerdefinierte URL" -ForegroundColor $ColorCommand
$choice = Read-Host "Wählen Sie eine Option (1-3, Standard: 1)"
$backendUrl = "https://localhost:443"
$backendHost = "localhost"
switch ($choice) {
"2" {
$backendUrl = "http://192.168.0.105:5000"
$backendHost = "192.168.0.105"
}
"3" {
$backendUrl = Read-Host "Backend-URL eingeben (z.B. https://raspberrypi:443)"
$backendHost = ([System.Uri]$backendUrl).Host
}
default {
$backendUrl = "https://localhost:443"
$backendHost = "localhost"
}
}
Write-Host ""
Write-Host "Teste Backend: $backendUrl" -ForegroundColor $ColorInfo
Write-Host ""
# 1. Netzwerk-Konnektivität prüfen
Write-Host "1. Prüfe Netzwerk-Konnektivität zu $backendHost..." -ForegroundColor $ColorInfo
try {
$ping = Test-Connection -ComputerName $backendHost -Count 1 -Quiet
if ($ping) {
Write-Host "✓ Ping zu $backendHost erfolgreich" -ForegroundColor $ColorSuccess
} else {
Write-Host "✗ Ping zu $backendHost fehlgeschlagen" -ForegroundColor $ColorError
}
}
catch {
Write-Host "✗ Ping-Test fehlgeschlagen: $($_.Exception.Message)" -ForegroundColor $ColorError
}
# 2. Backend-Service prüfen
Write-Host "2. Prüfe Backend-Service..." -ForegroundColor $ColorInfo
try {
$healthUrl = "$backendUrl/health"
$response = Invoke-WebRequest -Uri $healthUrl -TimeoutSec 5 -UseBasicParsing
if ($response.StatusCode -eq 200) {
Write-Host "✓ Backend-Health-Check erfolgreich" -ForegroundColor $ColorSuccess
} else {
Write-Host "⚠ Backend erreichbar, aber Health-Check fehlgeschlagen" -ForegroundColor $ColorWarning
}
}
catch {
try {
$response = Invoke-WebRequest -Uri $backendUrl -TimeoutSec 5 -UseBasicParsing
Write-Host "⚠ Backend erreichbar, aber kein Health-Endpoint" -ForegroundColor $ColorWarning
}
catch {
Write-Host "✗ Backend-Service nicht erreichbar" -ForegroundColor $ColorError
Write-Host " Fehler: $($_.Exception.Message)" -ForegroundColor $ColorError
}
}
# 3. API-Endpunkte prüfen
Write-Host "3. Prüfe Backend-API-Endpunkte..." -ForegroundColor $ColorInfo
$endpoints = @("printers", "jobs", "users")
foreach ($endpoint in $endpoints) {
try {
$apiUrl = "$backendUrl/api/$endpoint"
$response = Invoke-WebRequest -Uri $apiUrl -TimeoutSec 5 -UseBasicParsing
Write-Host "✓ API-Endpunkt /$endpoint erreichbar" -ForegroundColor $ColorSuccess
}
catch {
Write-Host "⚠ API-Endpunkt /$endpoint nicht erreichbar" -ForegroundColor $ColorWarning
}
}
# 4. Frontend-Konfiguration prüfen
Write-Host "4. Prüfe Frontend-Konfigurationsdateien..." -ForegroundColor $ColorInfo
$envLocalPath = "frontend\.env.local"
if (Test-Path $envLocalPath) {
$envContent = Get-Content $envLocalPath -Raw
if ($envContent -match "NEXT_PUBLIC_API_URL") {
Write-Host "✓ .env.local gefunden und konfiguriert" -ForegroundColor $ColorSuccess
} else {
Write-Host "⚠ .env.local existiert, aber Backend-URL fehlt" -ForegroundColor $ColorWarning
}
} else {
Write-Host "⚠ .env.local nicht gefunden" -ForegroundColor $ColorWarning
}
Write-Host ""
Write-Host "Möchten Sie die Frontend-Konfiguration für dieses Backend aktualisieren? (j/n)" -ForegroundColor $ColorInfo
$updateConfig = Read-Host
if ($updateConfig -eq "j") {
Setup-BackendUrl -BackendUrl $backendUrl
}
Write-Host ""
Write-Host "Drücken Sie eine beliebige Taste, um fortzufahren..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function Setup-BackendUrl {
param (
[string]$BackendUrl = ""
)
Show-Header "Backend-URL konfigurieren"
if (-not $BackendUrl) {
Write-Host "Verfügbare Backend-Konfigurationen:" -ForegroundColor $ColorInfo
Write-Host "1. Lokale Entwicklung (https://localhost:443)" -ForegroundColor $ColorCommand
Write-Host "2. Raspberry Pi (http://192.168.0.105:5000)" -ForegroundColor $ColorCommand
Write-Host "3. Benutzerdefinierte URL" -ForegroundColor $ColorCommand
$choice = Read-Host "Wählen Sie eine Option (1-3, Standard: 1)"
switch ($choice) {
"2" {
$BackendUrl = "http://192.168.0.105:5000"
}
"3" {
$BackendUrl = Read-Host "Backend-URL eingeben (z.B. https://raspberrypi:443)"
}
default {
$BackendUrl = "https://localhost:443"
}
}
}
Write-Host "Konfiguriere Frontend für Backend: $BackendUrl" -ForegroundColor $ColorInfo
# .env.local erstellen/aktualisieren
$envLocalPath = "frontend\.env.local"
$envContent = @"
# Backend API Konfiguration
NEXT_PUBLIC_API_URL=$BackendUrl
# Frontend-URL für OAuth Callback
NEXT_PUBLIC_FRONTEND_URL=http://localhost:3000
# OAuth Konfiguration
NEXT_PUBLIC_OAUTH_CALLBACK_URL=http://localhost:3000/auth/login/callback
# GitHub OAuth (hardcodiert)
GITHUB_CLIENT_ID=7c5d8bef1a5519ec1fdc
GITHUB_CLIENT_SECRET=5f1e586204358fbd53cf5fb7d418b3f06ccab8fd
# Entwicklungsumgebung
NODE_ENV=development
DEBUG=true
NEXT_DEBUG=true
# Backend Host
NEXT_PUBLIC_BACKEND_HOST=$((([System.Uri]$BackendUrl).Host))
NEXT_PUBLIC_BACKEND_PROTOCOL=$((([System.Uri]$BackendUrl).Scheme))
"@
try {
$envContent | Out-File -FilePath $envLocalPath -Encoding utf8
Write-Host "✓ .env.local erfolgreich erstellt/aktualisiert" -ForegroundColor $ColorSuccess
}
catch {
Write-Host "✗ Fehler beim Erstellen der .env.local: $($_.Exception.Message)" -ForegroundColor $ColorError
}
Write-Host ""
Write-Host "Frontend-Konfiguration abgeschlossen!" -ForegroundColor $ColorSuccess
Write-Host "Backend: $BackendUrl" -ForegroundColor $ColorCommand
Write-Host "Frontend: http://localhost:3000" -ForegroundColor $ColorCommand
Write-Host ""
Write-Host "Drücken Sie eine beliebige Taste, um fortzufahren..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function Start-DebugServer {
Show-Header "Debug-Server starten"
Write-Host "Welchen Debug-Server möchten Sie starten?" -ForegroundColor $ColorInfo
Write-Host "1. Frontend Debug-Server (Next.js)" -ForegroundColor $ColorCommand
Write-Host "2. Backend Debug-Server (Flask)" -ForegroundColor $ColorCommand
Write-Host "3. Beide Debug-Server" -ForegroundColor $ColorCommand
Write-Host "4. Frontend Debug-Server (einfacher HTTP-Server)" -ForegroundColor $ColorCommand
$choice = Read-Host "Wählen Sie eine Option (1-4, Standard: 1)"
switch ($choice) {
"1" {
Write-Host "Starte Frontend Debug-Server..." -ForegroundColor $ColorInfo
if (Test-Path "frontend") {
if (Test-CommandExists "npm") {
Start-Process -FilePath "cmd" -ArgumentList "/c", "cd frontend && npm run dev" -NoNewWindow
Write-Host "✓ Frontend Debug-Server gestartet" -ForegroundColor $ColorSuccess
} else {
Write-Host "✗ npm nicht gefunden" -ForegroundColor $ColorError
}
} else {
Write-Host "✗ Frontend-Verzeichnis nicht gefunden" -ForegroundColor $ColorError
}
}
"2" {
Write-Host "Starte Backend Debug-Server..." -ForegroundColor $ColorInfo
if (Test-Path "backend\app\app.py") {
if (Test-CommandExists "python") {
Start-Process -FilePath "python" -ArgumentList "backend\app\app.py", "--debug" -NoNewWindow
Write-Host "✓ Backend Debug-Server gestartet" -ForegroundColor $ColorSuccess
} else {
Write-Host "✗ Python nicht gefunden" -ForegroundColor $ColorError
}
} else {
Write-Host "✗ Backend-Anwendung nicht gefunden" -ForegroundColor $ColorError
}
}
"3" {
Write-Host "Starte beide Debug-Server..." -ForegroundColor $ColorInfo
# Backend starten
if (Test-Path "backend\app\app.py" -and (Test-CommandExists "python")) {
Start-Process -FilePath "python" -ArgumentList "backend\app\app.py", "--debug" -NoNewWindow
Write-Host "✓ Backend Debug-Server gestartet" -ForegroundColor $ColorSuccess
}
# Frontend starten
if (Test-Path "frontend" -and (Test-CommandExists "npm")) {
Start-Process -FilePath "cmd" -ArgumentList "/c", "cd frontend && npm run dev" -NoNewWindow
Write-Host "✓ Frontend Debug-Server gestartet" -ForegroundColor $ColorSuccess
}
}
"4" {
Write-Host "Starte einfachen HTTP Debug-Server..." -ForegroundColor $ColorInfo
$debugServerDir = "frontend\debug-server"
if (Test-Path $debugServerDir) {
if (Test-CommandExists "node") {
Start-Process -FilePath "node" -ArgumentList "$debugServerDir\src\app.js" -NoNewWindow
Write-Host "✓ Einfacher Debug-Server gestartet" -ForegroundColor $ColorSuccess
} else {
Write-Host "✗ Node.js nicht gefunden" -ForegroundColor $ColorError
}
} else {
Write-Host "✗ Debug-Server-Verzeichnis nicht gefunden" -ForegroundColor $ColorError
}
}
default {
Write-Host "Ungültige Option" -ForegroundColor $ColorError
}
}
Write-Host ""
Write-Host "Debug-Server-URLs:" -ForegroundColor $ColorInfo
Write-Host "- Frontend: http://localhost:3000" -ForegroundColor $ColorCommand
Write-Host "- Backend: https://localhost:443" -ForegroundColor $ColorCommand
Write-Host "- Debug-Server: http://localhost:8080" -ForegroundColor $ColorCommand
Write-Host ""
Write-Host "Drücken Sie eine beliebige Taste, um fortzufahren..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function Show-SSLStatus {
Show-Header "SSL-Zertifikat-Status"
$certPaths = @(
"backend\instance\ssl\myp.crt",
"backend\instance\ssl\myp.key",
"frontend\ssl\myp.crt",
"frontend\ssl\myp.key"
)
Write-Host "Prüfe SSL-Zertifikate..." -ForegroundColor $ColorInfo
Write-Host ""
foreach ($certPath in $certPaths) {
if (Test-Path $certPath) {
Write-Host "✓ Gefunden: $certPath" -ForegroundColor $ColorSuccess
# Zertifikatsinformationen anzeigen (falls OpenSSL verfügbar)
if (Test-CommandExists "openssl" -and $certPath.EndsWith(".crt")) {
try {
$certInfo = openssl x509 -in $certPath -noout -subject -dates 2>$null
if ($certInfo) {
Write-Host " $certInfo" -ForegroundColor $ColorCommand
}
}
catch {
# OpenSSL-Fehler ignorieren
}
}
} else {
Write-Host "✗ Fehlt: $certPath" -ForegroundColor $ColorError
}
}
Write-Host ""
Write-Host "SSL-Konfiguration in settings.py:" -ForegroundColor $ColorInfo
$settingsPath = "backend\app\config\settings.py"
if (Test-Path $settingsPath) {
$settingsContent = Get-Content $settingsPath -Raw
if ($settingsContent -match "SSL_ENABLED\s*=\s*True") {
Write-Host "✓ SSL ist aktiviert" -ForegroundColor $ColorSuccess
} else {
Write-Host "⚠ SSL ist deaktiviert" -ForegroundColor $ColorWarning
}
} else {
Write-Host "✗ settings.py nicht gefunden" -ForegroundColor $ColorError
}
Write-Host ""
Write-Host "Drücken Sie eine beliebige Taste, um fortzufahren..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function Install-MYPComplete {
Show-Header "Vollständige MYP-Installation"
Write-Host "Diese Funktion führt eine vollständige MYP-Installation durch:" -ForegroundColor $ColorInfo
Write-Host "1. Systemvoraussetzungen prüfen" -ForegroundColor $ColorCommand
Write-Host "2. Python-Abhängigkeiten installieren" -ForegroundColor $ColorCommand
Write-Host "3. Node.js-Abhängigkeiten installieren" -ForegroundColor $ColorCommand
Write-Host "4. SSL-Zertifikate erstellen" -ForegroundColor $ColorCommand
Write-Host "5. Datenbank initialisieren" -ForegroundColor $ColorCommand
Write-Host "6. Konfigurationsdateien erstellen" -ForegroundColor $ColorCommand
Write-Host ""
$confirm = Read-Host "Möchten Sie fortfahren? (j/n, Standard: j)"
if ($confirm -eq "n") {
return
}
# 1. Systemvoraussetzungen prüfen
Write-Host "1. Prüfe Systemvoraussetzungen..." -ForegroundColor $ColorInfo
$pythonInstalled = Test-CommandExists "python"
$pipInstalled = Test-CommandExists "pip"
$nodeInstalled = Test-CommandExists "node"
$npmInstalled = Test-CommandExists "npm"
if (-not $pythonInstalled -or -not $pipInstalled) {
Write-Host "✗ Python oder pip nicht gefunden. Bitte installieren Sie Python 3.6+ mit pip." -ForegroundColor $ColorError
return
}
# 2. Python-Abhängigkeiten installieren
Write-Host "2. Installiere Python-Abhängigkeiten..." -ForegroundColor $ColorInfo
if (Test-Path "backend\requirements.txt") {
Exec-Command "pip install -r backend\requirements.txt" "Installiere Backend-Abhängigkeiten"
} else {
Write-Host "⚠ requirements.txt nicht gefunden" -ForegroundColor $ColorWarning
}
# 3. Node.js-Abhängigkeiten installieren
if ($nodeInstalled -and $npmInstalled) {
Write-Host "3. Installiere Node.js-Abhängigkeiten..." -ForegroundColor $ColorInfo
if (Test-Path "frontend\package.json") {
Exec-Command "cd frontend && npm install" "Installiere Frontend-Abhängigkeiten"
} else {
Write-Host "⚠ package.json nicht gefunden" -ForegroundColor $ColorWarning
}
} else {
Write-Host "3. Überspringe Node.js-Abhängigkeiten (Node.js/npm nicht gefunden)" -ForegroundColor $ColorWarning
}
# 4. SSL-Zertifikate erstellen
Write-Host "4. Erstelle SSL-Zertifikate..." -ForegroundColor $ColorInfo
Create-SSLCertificates
# 5. Datenbank initialisieren
Write-Host "5. Initialisiere Datenbank..." -ForegroundColor $ColorInfo
if (Test-Path "backend\app\models.py") {
try {
Exec-Command "cd backend && python -c `"from app.models import init_db, create_initial_admin; init_db(); create_initial_admin()`"" "Initialisiere Datenbank"
}
catch {
Write-Host "⚠ Datenbankinitialisierung fehlgeschlagen: $($_.Exception.Message)" -ForegroundColor $ColorWarning
}
}
# 6. Konfigurationsdateien erstellen
Write-Host "6. Erstelle Konfigurationsdateien..." -ForegroundColor $ColorInfo
Setup-BackendUrl -BackendUrl "https://localhost:443"
Write-Host ""
Write-Host "✓ Vollständige MYP-Installation abgeschlossen!" -ForegroundColor $ColorSuccess
Write-Host ""
Write-Host "Nächste Schritte:" -ForegroundColor $ColorInfo
Write-Host "1. Backend starten: python backend\app\app.py" -ForegroundColor $ColorCommand
Write-Host "2. Frontend starten: cd frontend && npm run dev" -ForegroundColor $ColorCommand
Write-Host "3. Anwendung öffnen: https://localhost:443" -ForegroundColor $ColorCommand
Write-Host ""
Write-Host "Drücken Sie eine beliebige Taste, um fortzufahren..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
function Create-SSLCertificates {
Show-Header "SSL-Zertifikat-Generator"
@@ -552,9 +956,10 @@ function Start-Application {
Write-Host "3. Beide Server starten (in separaten Fenstern)" -ForegroundColor $ColorCommand
Write-Host "4. Mit Docker Compose starten" -ForegroundColor $ColorCommand
Write-Host "5. Vollständige Installation und Start" -ForegroundColor $ColorCommand
Write-Host "6. Zurück zum Hauptmenü" -ForegroundColor $ColorCommand
Write-Host "6. Debug-Server starten" -ForegroundColor $ColorCommand
Write-Host "7. Zurück zum Hauptmenü" -ForegroundColor $ColorCommand
$choice = Read-Host "Wählen Sie eine Option (1-6)"
$choice = Read-Host "Wählen Sie eine Option (1-7)"
switch ($choice) {
"1" {
@@ -589,15 +994,12 @@ function Start-Application {
}
}
"5" {
Write-Host "Führe vollständige Installation durch..." -ForegroundColor $ColorInfo
Setup-Environment
Create-SSLCertificates
Write-Host "Starte Anwendung..." -ForegroundColor $ColorInfo
Start-Process -FilePath "python" -ArgumentList "backend/app/app.py" -NoNewWindow
Start-Process -FilePath "npm" -ArgumentList "run dev" -WorkingDirectory "frontend" -NoNewWindow
Write-Host "Vollständige Installation und Start abgeschlossen!" -ForegroundColor $ColorSuccess
Install-MYPComplete
}
"6" {
Start-DebugServer
}
"7" {
return
}
default {
@@ -629,6 +1031,7 @@ function Show-ProjectInfo {
Write-Host "Standard-Zugangsdaten:" -ForegroundColor $ColorInfo
Write-Host "- Admin E-Mail: admin@mercedes-benz.com" -ForegroundColor $ColorCommand
Write-Host "- Admin Passwort: 744563017196A" -ForegroundColor $ColorCommand
Write-Host "- Router Passwort: vT6Vsd^p" -ForegroundColor $ColorCommand
Write-Host ""
Write-Host "URLs:" -ForegroundColor $ColorInfo
Write-Host "- Backend: https://localhost:443 oder https://raspberrypi:443" -ForegroundColor $ColorCommand
@@ -654,7 +1057,13 @@ function Clean-OldFiles {
"generate_ssl_certs.ps1",
"generate_ssl_certs_copy.ps1",
"setup_ssl.ps1",
"temp_cert_script.py"
"temp_cert_script.py",
"frontend\check-backend-connection.sh",
"frontend\setup-backend-url.sh",
"frontend\start-debug-server.bat",
"backend\setup_myp.sh",
"backend\install\create_ssl_cert.sh",
"backend\install\ssl_check.sh"
)
foreach ($file in $filesToDelete) {
@@ -683,12 +1092,16 @@ function Show-MainMenu {
Write-Host "3. SSL-Zertifikate erstellen" -ForegroundColor $ColorCommand
Write-Host "4. Umgebung einrichten (Abhängigkeiten installieren)" -ForegroundColor $ColorCommand
Write-Host "5. Anwendung starten" -ForegroundColor $ColorCommand
Write-Host "6. Projekt-Informationen anzeigen" -ForegroundColor $ColorCommand
Write-Host "7. Alte Dateien bereinigen" -ForegroundColor $ColorCommand
Write-Host "8. Beenden" -ForegroundColor $ColorCommand
Write-Host "6. Backend-Verbindung testen" -ForegroundColor $ColorCommand
Write-Host "7. Backend-URL konfigurieren" -ForegroundColor $ColorCommand
Write-Host "8. SSL-Zertifikat-Status anzeigen" -ForegroundColor $ColorCommand
Write-Host "9. Vollständige MYP-Installation" -ForegroundColor $ColorCommand
Write-Host "10. Projekt-Informationen anzeigen" -ForegroundColor $ColorCommand
Write-Host "11. Alte Dateien bereinigen" -ForegroundColor $ColorCommand
Write-Host "12. Beenden" -ForegroundColor $ColorCommand
Write-Host ""
$choice = Read-Host "Wählen Sie eine Option (1-8)"
$choice = Read-Host "Wählen Sie eine Option (1-12)"
switch ($choice) {
"1" {
@@ -712,14 +1125,30 @@ function Show-MainMenu {
Show-MainMenu
}
"6" {
Show-ProjectInfo
Test-BackendConnection
Show-MainMenu
}
"7" {
Clean-OldFiles
Setup-BackendUrl
Show-MainMenu
}
"8" {
Show-SSLStatus
Show-MainMenu
}
"9" {
Install-MYPComplete
Show-MainMenu
}
"10" {
Show-ProjectInfo
Show-MainMenu
}
"11" {
Clean-OldFiles
Show-MainMenu
}
"12" {
Write-Host "Auf Wiedersehen!" -ForegroundColor $ColorSuccess
exit
}