"Update API configuration and SSL certificates for enhanced security"

This commit is contained in:
2025-05-26 09:54:29 +02:00
parent 45fcc1a948
commit 201f75cfd3
9 changed files with 8308 additions and 84 deletions

View File

@@ -33,10 +33,10 @@ FLASK_DEBUG = True
SESSION_LIFETIME = timedelta(days=7)
# SSL-Konfiguration
SSL_ENABLED = False
SSL_ENABLED = True
SSL_CERT_PATH = "instance/ssl/myp.crt"
SSL_KEY_PATH = "instance/ssl/myp.key"
SSL_HOSTNAME = "raspberrypi"
SSL_HOSTNAME = "localhost"
# Scheduler-Konfiguration
SCHEDULER_INTERVAL = 60 # Sekunden
@@ -92,49 +92,21 @@ def get_ssl_context():
if not os.path.exists(SSL_CERT_PATH) or not os.path.exists(SSL_KEY_PATH):
ensure_ssl_directory()
# Prüfen, ob wir uns im Entwicklungsmodus befinden
# Im Entwicklungsmodus versuchen wir, einfache Zertifikate zu erstellen
if FLASK_DEBUG:
print("SSL-Zertifikate nicht gefunden. Erstelle selbstsignierte Zertifikate...")
# Auf Windows prüfen
import platform
if platform.system() == "Windows":
# Unter Windows verwenden wir OpenSSL direkt über subprocess
import subprocess
try:
# Erstelle ein selbstsigniertes Zertifikat mit OpenSSL
ssl_cmd = [
"openssl", "req", "-new", "-x509", "-newkey", "rsa:2048",
"-nodes", "-keyout", SSL_KEY_PATH, "-out", SSL_CERT_PATH,
"-days", "365", "-subj", f"/CN={SSL_HOSTNAME}"
]
result = subprocess.run(ssl_cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"Fehler beim Erstellen der SSL-Zertifikate: {result.stderr}")
# Fallback: Generiere einfache Schlüssel mit Python
create_simple_ssl_cert()
except Exception as e:
print(f"Fehler beim Ausführen von OpenSSL: {e}")
# Fallback: Generiere einfache Schlüssel mit Python
create_simple_ssl_cert()
else:
# Unter Linux das vorhandene Skript verwenden
script_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),
"install", "create_ssl_cert.sh")
print("SSL-Zertifikate nicht gefunden. Erstelle einfache selbstsignierte Zertifikate...")
try:
# Einfache Zertifikate mit Python erstellen
create_simple_ssl_cert()
# Ausführungsrechte setzen
if os.path.exists(script_path):
os.system(f"chmod +x {script_path}")
# Zertifikate erstellen mit spezifischem Hostnamen
os.system(f"{script_path} -c {SSL_CERT_PATH} -k {SSL_KEY_PATH} -h {SSL_HOSTNAME}")
else:
print(f"WARNUNG: SSL-Zertifikat-Generator nicht gefunden: {script_path}")
# Prüfen, ob die Zertifikate erfolgreich erstellt wurden
if not os.path.exists(SSL_CERT_PATH) or not os.path.exists(SSL_KEY_PATH):
print("Konnte keine SSL-Zertifikate erstellen.")
return None
except Exception as e:
print(f"Fehler beim Erstellen der SSL-Zertifikate: {e}")
return None
else:
print("WARNUNG: SSL-Zertifikate nicht gefunden und Nicht-Debug-Modus. SSL wird deaktiviert.")
return None