umbau 1.0
This commit is contained in:
@ -11,6 +11,8 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import re
|
||||
import shutil
|
||||
import uuid as uuid_mod
|
||||
|
||||
from fastapi import APIRouter, Query, WebSocket, WebSocketDisconnect
|
||||
@ -18,17 +20,22 @@ from fastapi import APIRouter, Query, WebSocket, WebSocketDisconnect
|
||||
from app.auth.deps import get_current_user_ws
|
||||
from app.config import settings
|
||||
from app.db import get_db
|
||||
from app.rbac import user_has_role_for_host
|
||||
from app.rbac import AmbiguousCredentialError, resolve_credential_for_user_on_host, user_has_role_for_host
|
||||
from app.recordings.recorder import SessionRecorder
|
||||
from app.security import active_sessions
|
||||
from app.security.audit import write_audit_event
|
||||
from app.security.crypto import decrypt_secret
|
||||
from app.rdp_proxy.guacd_client import (
|
||||
GuacamoleProtocolError,
|
||||
GuacdDisconnectedError,
|
||||
GuacdStatusError,
|
||||
GuacdUnreachableError,
|
||||
build_rdp_params,
|
||||
encode_instruction,
|
||||
guac_status_text,
|
||||
open_tunnel,
|
||||
parse_instruction_text,
|
||||
parse_instructions_text,
|
||||
rdp_drive_path,
|
||||
read_instruction,
|
||||
)
|
||||
from app.ssh_proxy.proxy import HostNotConfiguredError, load_host
|
||||
@ -48,6 +55,28 @@ INTERNAL_DATA_OPCODE = ""
|
||||
# zustande und es erreichte kein einziger Frame die Anwendung.
|
||||
GUACAMOLE_SUBPROTOCOL = "guacamole"
|
||||
|
||||
# Grobe Plausibilitaetspruefung fuer eine vom Client mitgeschickte IANA-
|
||||
# Zeitzone (z.B. "Europe/Vienna") -- kein Anspruch auf Vollstaendigkeit gegen
|
||||
# die tz-Datenbank, nur ein Schutz gegen offensichtlich falsche Werte, bevor
|
||||
# sie als RDP-connect-Parameter an guacd/FreeRDP weitergereicht werden.
|
||||
_TIMEZONE_RE = re.compile(r"^[A-Za-z0-9_+\-]+(/[A-Za-z0-9_+\-]+)*$")
|
||||
|
||||
|
||||
async def _cleanup_rdp_drive_path(host_id: int, session_id: int) -> None:
|
||||
"""Entfernt das pro Sitzung umgeleitete RDP-Laufwerk beim Sitzungsende
|
||||
(Befund E6, Umsetzungsauftrag Teil E) -- sonst sammeln sich pro Sitzung
|
||||
Verzeichnisse unbegrenzt an. shutil.rmtree ist blockierendes Datei-I/O
|
||||
(siehe E.0) und laeuft deshalb in einem Thread statt direkt im
|
||||
Event-Loop."""
|
||||
path = rdp_drive_path(host_id, session_id)
|
||||
try:
|
||||
await asyncio.to_thread(shutil.rmtree, path, True)
|
||||
except OSError:
|
||||
logger.warning(
|
||||
"Konnte RDP-Laufwerksverzeichnis fuer Sitzung %s (Host %s) nicht entfernen: %s",
|
||||
session_id, host_id, path, exc_info=True,
|
||||
)
|
||||
|
||||
|
||||
async def _accept(websocket: WebSocket) -> None:
|
||||
"""Nimmt die Verbindung an und bestaetigt dabei das Subprotokoll, sofern
|
||||
@ -59,62 +88,141 @@ async def _accept(websocket: WebSocket) -> None:
|
||||
await websocket.accept()
|
||||
|
||||
|
||||
async def _reject(websocket: WebSocket, code: int, reason: str, *, accepted: bool) -> None:
|
||||
async def _send_error_instruction(websocket: WebSocket, message: str, status_code: int) -> None:
|
||||
"""Sendet eine vollstaendige Guacamole-'error'-Instruktion ueber den noch
|
||||
offenen Tunnel, BEVOR die Verbindung geschlossen wird (Befund B.2,
|
||||
vormals ws_tunnel.py:296-308): die WebSocket-Close-Reason ist nach RFC
|
||||
6455 auf 123 Byte begrenzt und war bisher der EINZIGE Uebertragungsweg
|
||||
fuer den Fehlertext. Eine 'error'-Instruktion im normalen Tunnel-
|
||||
Datenstrom hat kein derartiges Laengenlimit und wird von
|
||||
guacamole-common-js (vendor/guacamole-common.js, Guacamole.Tunnel-
|
||||
Instruktions-Handler) automatisch verarbeitet -- die Close-Reason bleibt
|
||||
als Fallback fuer den Fall, dass dieser Sendeversuch selbst scheitert
|
||||
(z.B. weil der Client bereits getrennt hat).
|
||||
"""
|
||||
try:
|
||||
await websocket.send_text(encode_instruction("error", message, str(status_code)))
|
||||
except Exception:
|
||||
logger.debug("Konnte 'error'-Instruktion nicht mehr senden (Tunnel bereits zu)", exc_info=True)
|
||||
|
||||
|
||||
async def _reject(
|
||||
websocket: WebSocket,
|
||||
code: int,
|
||||
reason: str,
|
||||
*,
|
||||
accepted: bool,
|
||||
correlation_id: str,
|
||||
host_id: int | None = None,
|
||||
username: str | None = None,
|
||||
) -> None:
|
||||
"""Beendet eine Sitzung vor ihrem eigentlichen Beginn -- mit Logeintrag
|
||||
und mit einem fuer den Benutzer lesbaren Grund.
|
||||
|
||||
Bisher endeten alle diese Pfade in einem nackten `websocket.close(code=...)`
|
||||
ohne jede Protokollierung. Im Verbindungslog war deshalb ueberhaupt nichts
|
||||
zu sehen, wenn eine RDP-Sitzung an einer dieser Vorbedingungen scheiterte.
|
||||
Befund B.2 ("gesamter RDP-Pfad"): zusaetzlich fehlten strukturierte
|
||||
Felder (host_id, Benutzer) UND eine Korrelations-ID -- man sah zwar DASS
|
||||
etwas abgelehnt wurde, aber die Ablehnung liess sich nicht mit einer
|
||||
spaeteren Nutzerrueckfrage ("bei mir ging um 14:03 nichts") verknuepfen.
|
||||
|
||||
Der Grundtext wird als WebSocket-Close-Reason mitgegeben:
|
||||
guacamole-common-js reicht ihn (siehe close_tunnel()) als
|
||||
Guacamole.Status.message an client.onerror weiter, wo static/js/rdp.js ihn
|
||||
direkt anzeigt. Voraussetzung dafuer ist ein zustande gekommener
|
||||
Handshake -- vor `accept()` sieht der Browser nur einen HTTP-Fehler.
|
||||
Der Grundtext wird als WebSocket-Close-Reason UND als vollstaendige
|
||||
Guacamole-'error'-Instruktion mitgegeben (siehe _send_error_instruction);
|
||||
letztere unterliegt nicht der 123-Byte-Grenze der Close-Reason.
|
||||
"""
|
||||
logger.warning("RDP-Verbindung abgelehnt (code=%s): %s", code, reason)
|
||||
# Close-Reason ist auf 123 Byte begrenzt (RFC 6455).
|
||||
reason_bytes = reason.encode("utf-8")[:123]
|
||||
logger.warning(
|
||||
"RDP-Verbindung abgelehnt [corr=%s] (code=%s, host_id=%s, user=%s): %s",
|
||||
correlation_id, code, host_id, username, reason,
|
||||
)
|
||||
full_reason = f"{reason} (Ref: {correlation_id})"
|
||||
if not accepted:
|
||||
await _accept(websocket)
|
||||
await _send_error_instruction(websocket, full_reason, code)
|
||||
reason_bytes = full_reason.encode("utf-8")[:123]
|
||||
await websocket.close(code=code, reason=reason_bytes.decode("utf-8", errors="ignore"))
|
||||
|
||||
|
||||
async def _guacd_to_ws(tunnel, websocket: WebSocket, recorder: SessionRecorder) -> None:
|
||||
"""Liest Instruktionen von guacd und reicht sie an den Browser weiter.
|
||||
|
||||
Befund A2: laeuft ab direkt nach open_tunnel() (die NICHT mehr selbst auf
|
||||
'ready' wartet), somit ist ab dem Senden von 'connect' durchgehend jemand
|
||||
am Lesen -- unabhaengig davon, wie lange guacd fuer den eigentlichen
|
||||
RDP-/NLA-Handshake braucht.
|
||||
|
||||
'ready' wird hier (statt in open_tunnel()) erkannt, um die connection_id
|
||||
festzuhalten, und dann wie jede andere Instruktion normal weitergereicht
|
||||
-- guacamole-common-js erwartet 'ready' ohnehin als regulaeren Teil des
|
||||
Instruktionsstroms. Eine 'error'-Instruktion (Befund B.2/C1) wird NICHT
|
||||
weitergereicht, sondern als GuacdStatusError geworfen: der aufrufende
|
||||
Code in rdp_tunnel() bildet daraus die deutsche Klartextmeldung fuer Log
|
||||
und Browser (guac_status_text/_send_error_instruction), statt dass der
|
||||
Browser den rohen Guacamole-Statuscode selbst interpretieren muss.
|
||||
"""
|
||||
while True:
|
||||
instr = await read_instruction(tunnel.reader)
|
||||
if instr and instr[0] == "ready":
|
||||
tunnel.connection_id = instr[1] if len(instr) > 1 else ""
|
||||
elif instr and instr[0] == "error":
|
||||
message = instr[1] if len(instr) > 1 else ""
|
||||
try:
|
||||
code = int(instr[2]) if len(instr) > 2 else 512
|
||||
except ValueError:
|
||||
code = 512
|
||||
raise GuacdStatusError(code, message)
|
||||
text = encode_instruction(*instr)
|
||||
recorder.record("output", text)
|
||||
await websocket.send_text(text)
|
||||
|
||||
|
||||
async def _ws_to_guacd(tunnel, websocket: WebSocket, recorder: SessionRecorder, *, clipboard_enabled: bool) -> None:
|
||||
async def _ws_to_guacd(tunnel, websocket: WebSocket, recorder: SessionRecorder, *, clipboard_enabled: bool, correlation_id: str) -> None:
|
||||
while True:
|
||||
message = await websocket.receive_text()
|
||||
try:
|
||||
instr = parse_instruction_text(message)
|
||||
except (GuacamoleProtocolError, ValueError, IndexError):
|
||||
continue # ungueltige Clientnachricht ignorieren statt die Verbindung zu killen
|
||||
|
||||
if instr and instr[0] == INTERNAL_DATA_OPCODE:
|
||||
# Tunnelinterne Instruktion von guacamole-common-js (leerer
|
||||
# Opcode), z.B. "0.,4.ping,13.<timestamp>;". Diese gehoert dem
|
||||
# Tunnel, NICHT guacd -- bisher wurde sie unbesehen an guacd
|
||||
# weitergereicht, das damit nichts anfangen kann. Ein ping wird
|
||||
# gespiegelt: der Browser bricht den Tunnel nach
|
||||
# tunnel.receiveTimeout (Standard 15 s) ohne empfangene Daten mit
|
||||
# "Server timeout" ab, eine stille Sitzung liefe also in einen
|
||||
# Fehler.
|
||||
if len(instr) > 1 and instr[1] == "ping":
|
||||
await websocket.send_text(encode_instruction(*instr))
|
||||
instructions = parse_instructions_text(message)
|
||||
except GuacamoleProtocolError:
|
||||
# Ungueltige Clientnachricht ignorieren statt die Verbindung zu
|
||||
# killen -- aber (Befund B.2, ws_tunnel.py:96-98) nicht mehr
|
||||
# lautlos: ein gekuerzter Nachrichtenausschnitt landet als DEBUG
|
||||
# im Log, damit ein wiederkehrendes Muster (z.B. ein kaputter
|
||||
# Client) ueberhaupt auffallen kann.
|
||||
logger.debug(
|
||||
"[corr=%s] Ungueltige WS-Nachricht ignoriert: %r",
|
||||
correlation_id, message[:200],
|
||||
)
|
||||
continue
|
||||
|
||||
if not clipboard_enabled and instr and instr[0] == "clipboard":
|
||||
continue # Defense-in-Depth: Clipboard serverseitig blocken
|
||||
# Befund D5: eine einzelne WebSocket-Textnachricht kann MEHRERE
|
||||
# Guacamole-Instruktionen buendeln. Jede wird einzeln gegen den
|
||||
# Zwischenablage-Filter geprueft (statt nur instr[0] der Nachricht)
|
||||
# und nur die ueberlebenden Instruktionen werden neu kodiert
|
||||
# weitergereicht -- eine gefilterte 'clipboard'-Instruktion darf sich
|
||||
# nicht mehr an zweiter/spaeterer Stelle in derselben Nachricht
|
||||
# vorbeischmuggeln koennen.
|
||||
forward: list[str] = []
|
||||
for instr in instructions:
|
||||
if instr and instr[0] == INTERNAL_DATA_OPCODE:
|
||||
# Tunnelinterne Instruktion von guacamole-common-js (leerer
|
||||
# Opcode), z.B. "0.,4.ping,13.<timestamp>;". Gehoert dem
|
||||
# Tunnel, NICHT guacd. Ein ping wird gespiegelt: der Browser
|
||||
# bricht den Tunnel nach tunnel.receiveTimeout (Standard 15s)
|
||||
# ohne empfangene Daten mit "Server timeout" ab.
|
||||
if len(instr) > 1 and instr[1] == "ping":
|
||||
await websocket.send_text(encode_instruction(*instr))
|
||||
continue
|
||||
|
||||
recorder.record("input", message)
|
||||
tunnel.writer.write(message.encode("utf-8"))
|
||||
if not clipboard_enabled and instr and instr[0] == "clipboard":
|
||||
continue # Defense-in-Depth: Clipboard serverseitig blocken
|
||||
|
||||
forward.append(encode_instruction(*instr))
|
||||
|
||||
if not forward:
|
||||
continue
|
||||
|
||||
combined = "".join(forward)
|
||||
recorder.record("input", combined)
|
||||
tunnel.writer.write(combined.encode("utf-8"))
|
||||
await tunnel.writer.drain()
|
||||
|
||||
|
||||
@ -125,14 +233,28 @@ async def rdp_tunnel(
|
||||
width: int = Query(default=1280, ge=320, le=7680),
|
||||
height: int = Query(default=800, ge=240, le=4320),
|
||||
dpi: int = Query(default=96, ge=48, le=384),
|
||||
timezone: str | None = Query(default=None, max_length=64),
|
||||
):
|
||||
# Befund B.2 ("gesamter RDP-Pfad"): Korrelations-ID VOR jeder moeglichen
|
||||
# Ablehnung erzeugen (nicht erst nach dem RBAC-Check) -- damit hat
|
||||
# WIRKLICH JEDE Logzeile zu dieser Verbindung, inklusive der fruehesten
|
||||
# Ablehnungen, ein gemeinsames Merkmal. Wird nach erfolgreichem accept()
|
||||
# zusaetzlich als tunnel_uuid an den Browser gesendet (siehe unten) --
|
||||
# dieselbe ID dient also sowohl der Server-Log-Korrelation als auch der
|
||||
# Client-seitigen Tunnel-Identifikation.
|
||||
correlation_id = str(uuid_mod.uuid4())
|
||||
|
||||
user = await get_current_user_ws(websocket)
|
||||
if user is None:
|
||||
# Einziger Pfad, der bewusst OHNE vorheriges accept() schliesst: eine
|
||||
# nicht authentifizierte Verbindung soll gar nicht erst zustande
|
||||
# kommen. Alle folgenden Ablehnungen laufen ueber _reject(), damit der
|
||||
# Benutzer im Browser den tatsaechlichen Grund zu sehen bekommt.
|
||||
logger.warning("RDP-Verbindung abgelehnt: keine gueltige Sitzung (host_id=%s)", host_id)
|
||||
# kommen (vor accept() sieht der Browser ohnehin nur einen
|
||||
# HTTP-Fehler, eine Guacamole-'error'-Instruktion kann also nicht
|
||||
# ankommen). Alle folgenden Ablehnungen laufen ueber _reject().
|
||||
logger.warning(
|
||||
"RDP-Verbindung abgelehnt [corr=%s]: keine gueltige Sitzung (host_id=%s)",
|
||||
correlation_id, host_id,
|
||||
)
|
||||
await websocket.close(code=4401)
|
||||
return
|
||||
|
||||
@ -145,7 +267,30 @@ async def rdp_tunnel(
|
||||
await _reject(
|
||||
websocket, 4403,
|
||||
f"Keine Berechtigung 'rdp_connect' fuer Host {host_id}",
|
||||
accepted=False,
|
||||
accepted=False, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
|
||||
# E.4 (Umsetzungsauftrag Teil E): Obergrenzen je Benutzer und global,
|
||||
# VOR dem DB-INSERT und jedem Ressourcenverbrauch geprueft -- vorher gab
|
||||
# es keine der beiden Grenzen, mit der Mehrsitzungs-Seitenleiste (Teil F)
|
||||
# wird es zur Normalitaet, dass ein Benutzer mehrere Sitzungen haelt.
|
||||
# Klartext-Ablehnung statt eines wortlosen Verbindungsendes (Teil B gilt
|
||||
# auch hier).
|
||||
if not user.is_admin and active_sessions.count_for_user(user.id) >= settings.max_sessions_per_user:
|
||||
await _reject(
|
||||
websocket, 4429,
|
||||
f"Sie haben bereits {settings.max_sessions_per_user} Sitzungen offen "
|
||||
"(Obergrenze je Benutzer erreicht).",
|
||||
accepted=False, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
if active_sessions.count_total() >= settings.max_sessions_global:
|
||||
await _reject(
|
||||
websocket, 4429,
|
||||
"Der Server hat die maximale Anzahl gleichzeitiger Sitzungen erreicht, "
|
||||
"bitte spaeter erneut versuchen.",
|
||||
accepted=False, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
|
||||
@ -156,21 +301,27 @@ async def rdp_tunnel(
|
||||
# Empfang der ersten Instruktion auf OPEN und startet damit seine
|
||||
# Timeoutueberwachung neu -- ohne diesen Frame blieb der Client bis zum
|
||||
# ersten Bild von guacd in "Warte auf Server ..." haengen und lief bei
|
||||
# einem langsamen RDP-Handshake in den 15-Sekunden-Timeout.
|
||||
tunnel_uuid = str(uuid_mod.uuid4())
|
||||
# einem langsamen RDP-Handshake in den 15-Sekunden-Timeout. Dieselbe ID
|
||||
# wie correlation_id (siehe oben) -- damit ist ein vom Benutzer im UI
|
||||
# angezeigter Fehler direkt mit den Server-Logzeilen dieser Verbindung
|
||||
# verknuepfbar.
|
||||
tunnel_uuid = correlation_id
|
||||
await websocket.send_text(encode_instruction(INTERNAL_DATA_OPCODE, tunnel_uuid))
|
||||
|
||||
try:
|
||||
host = await load_host(conn, host_id)
|
||||
except HostNotConfiguredError as exc:
|
||||
await _reject(websocket, 4404, str(exc), accepted=True)
|
||||
await _reject(
|
||||
websocket, 4404, str(exc), accepted=True,
|
||||
correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
|
||||
if host["protocol"] != "rdp":
|
||||
await _reject(
|
||||
websocket, 4400,
|
||||
f"Host {host['hostname']} ist kein RDP-Ziel (protocol={host['protocol']})",
|
||||
accepted=True,
|
||||
accepted=True, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
|
||||
@ -179,11 +330,40 @@ async def rdp_tunnel(
|
||||
# Beschreibung des Zielsystems. Seit Migration 0012 sind RDP-Zugangsdaten
|
||||
# zudem ein eigenstaendiges, wiederverwendbares Objekt (analog SSH-Keys),
|
||||
# das einem Host ueber host_rdp_credential_map zugewiesen wird, statt
|
||||
# 1:1 am Host zu haengen.
|
||||
# 1:1 am Host zu haengen. Seit Teil D Schritt 4 (Achse B) laeuft die
|
||||
# Auswahl NICHT mehr blind ueber den Host allein:
|
||||
# app.rbac.resolve_credential_for_user_on_host() beruecksichtigt
|
||||
# zusaetzlich, ob DIESER Benutzer ueber eine seiner Gruppen
|
||||
# (group_rdp_credential_grants) Zugriff auf den zugeordneten
|
||||
# Zugangsdatensatz hat. AmbiguousCredentialError kann hier strukturell
|
||||
# nicht auftreten (host_rdp_credential_map hat PK auf host_id, also
|
||||
# hoechstens ein Treffer) -- trotzdem defensiv abgefangen, statt
|
||||
# unbehandelt durchzureichen.
|
||||
try:
|
||||
rdp_credential_id = await resolve_credential_for_user_on_host(
|
||||
conn, user_id=user.id, host_id=host_id, kind="rdp_credential"
|
||||
)
|
||||
except AmbiguousCredentialError as exc:
|
||||
logger.error("Unerwartete Mehrdeutigkeit bei RDP-Zugangsdaten: %s", exc)
|
||||
await _reject(
|
||||
websocket, 4404,
|
||||
f"Fuer Host {host['hostname']} sind mehrdeutige RDP-Zugangsdaten hinterlegt "
|
||||
"(Adminbereich -> Zugangsdaten).",
|
||||
accepted=True, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
if rdp_credential_id is None:
|
||||
await _reject(
|
||||
websocket, 4404,
|
||||
f"Fuer Host {host['hostname']} ist kein RDP-Passwort hinterlegt oder keines Ihrer "
|
||||
"Benutzergruppen freigegeben (Adminbereich -> Zugangsdaten bzw. "
|
||||
"Benutzergruppen -> Zugangsdaten).",
|
||||
accepted=True, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
cred_cursor = await conn.execute(
|
||||
"SELECT rc.password_enc, rc.username, rc.domain FROM host_rdp_credential_map m "
|
||||
"JOIN rdp_credentials rc ON rc.id = m.rdp_credential_id WHERE m.host_id = ?",
|
||||
(host_id,),
|
||||
"SELECT password_enc, username, domain FROM rdp_credentials WHERE id = ?",
|
||||
(rdp_credential_id,),
|
||||
)
|
||||
cred_row = await cred_cursor.fetchone()
|
||||
if cred_row is None:
|
||||
@ -191,7 +371,7 @@ async def rdp_tunnel(
|
||||
websocket, 4404,
|
||||
f"Fuer Host {host['hostname']} ist kein RDP-Passwort hinterlegt "
|
||||
"(Adminbereich -> Zugangsdaten).",
|
||||
accepted=True,
|
||||
accepted=True, correlation_id=correlation_id, host_id=host_id, username=user.username,
|
||||
)
|
||||
return
|
||||
|
||||
@ -206,17 +386,22 @@ async def rdp_tunnel(
|
||||
)
|
||||
await write_audit_event(
|
||||
conn, event_type="rdp_session_start", user_id=user.id, client_ip=client_ip,
|
||||
details={"host_id": host_id, "hostname": host["hostname"], "session_id": session_id},
|
||||
details={"host_id": host_id, "hostname": host["hostname"], "session_id": session_id, "corr": correlation_id},
|
||||
)
|
||||
await conn.commit()
|
||||
logger.debug(
|
||||
"RDP-Sitzung %s gestartet: user=%s host=%s (%s:%s) client_ip=%s",
|
||||
session_id, user.username, host["hostname"], host["address"], host["port"], client_ip,
|
||||
# Befund B.2 (ws_tunnel.py:212-215): Sitzungsstart/-ende gehoeren auf
|
||||
# INFO, nicht DEBUG -- im Produktivbetrieb (basicConfig(level=INFO),
|
||||
# app/main.py) waren sie damit ohne den globalen DEBUG-Override in
|
||||
# app/security/log_stream.py schlicht NICHT sichtbar.
|
||||
logger.info(
|
||||
"RDP-Sitzung %s gestartet [corr=%s]: user=%s host=%s (%s:%s) client_ip=%s",
|
||||
session_id, correlation_id, user.username, host["hostname"], host["address"], host["port"], client_ip,
|
||||
)
|
||||
active_sessions.register(session_id, asyncio.current_task())
|
||||
active_sessions.register(session_id, asyncio.current_task(), user.id)
|
||||
|
||||
end_reason = "logout"
|
||||
error_text: str | None = None
|
||||
error_code = 512
|
||||
tunnel = None
|
||||
tasks: list[asyncio.Task] = []
|
||||
password = None
|
||||
@ -229,19 +414,26 @@ async def rdp_tunnel(
|
||||
# riss ein Fehler hier VOR jeglicher Protokollierung durch und die
|
||||
# Sitzung verschwand spurlos (schwarzer Bildschirm, kein Log-Eintrag).
|
||||
password = decrypt_secret(cred_row[0], associated_data=b"rdp_password")
|
||||
safe_timezone = timezone if (timezone and _TIMEZONE_RE.fullmatch(timezone)) else None
|
||||
params = build_rdp_params(
|
||||
host, password.decode(), username=cred_row[1], domain=cred_row[2]
|
||||
host, password.decode(), session_id=session_id, username=cred_row[1], domain=cred_row[2],
|
||||
client_name=f"jumphost-{user.username}", timezone=safe_timezone,
|
||||
)
|
||||
tunnel = await open_tunnel(
|
||||
guacd_host=settings.guacd_host, guacd_port=settings.guacd_port,
|
||||
protocol="rdp", params=params, screen_width=width, screen_height=height, dpi=dpi,
|
||||
connect_timeout=settings.guacd_connect_timeout_s,
|
||||
handshake_timeout=settings.guacd_handshake_timeout_s,
|
||||
)
|
||||
logger.info(
|
||||
"RDP-Sitzung %s [corr=%s]: guacd-Tunnel zu %s:%s aufgebaut, 'connect' fuer %s gesendet",
|
||||
session_id, correlation_id, settings.guacd_host, settings.guacd_port, host["hostname"],
|
||||
)
|
||||
logger.debug("RDP-Sitzung %s: guacd-Tunnel zu %s aufgebaut", session_id, host["hostname"])
|
||||
clipboard_enabled = bool(host.get("clipboard_enabled", True))
|
||||
tasks = [
|
||||
asyncio.create_task(_guacd_to_ws(tunnel, websocket, recorder)),
|
||||
asyncio.create_task(
|
||||
_ws_to_guacd(tunnel, websocket, recorder, clipboard_enabled=clipboard_enabled)
|
||||
_ws_to_guacd(tunnel, websocket, recorder, clipboard_enabled=clipboard_enabled, correlation_id=correlation_id)
|
||||
),
|
||||
]
|
||||
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_EXCEPTION)
|
||||
@ -253,11 +445,37 @@ async def rdp_tunnel(
|
||||
raise exc
|
||||
except WebSocketDisconnect:
|
||||
end_reason = "logout"
|
||||
except GuacdUnreachableError as exc:
|
||||
# Befund B.2: eigene Fehlerklasse statt eines rohen OSError --
|
||||
# unterscheidbar von "guacd erreichbar, aber Ziel/Anmeldung fehlerhaft".
|
||||
logger.warning("RDP-Sessionfehler (session_id=%s) [corr=%s]: %s", session_id, correlation_id, exc)
|
||||
end_reason = "error"
|
||||
error_text = str(exc)
|
||||
error_code = 514 # UPSTREAM_TIMEOUT als naechstliegender Guacamole-Code fuer "guacd nicht erreichbar"
|
||||
except GuacdStatusError as exc:
|
||||
# guacd hat eine 'error'-Instruktion gesendet (haeufigster Fall:
|
||||
# falsches Passwort/Zielsystem nicht erreichbar/Zertifikatsproblem).
|
||||
logger.warning(
|
||||
"RDP-Sessionfehler (session_id=%s) [corr=%s]: guacd meldet Status %s: %s",
|
||||
session_id, correlation_id, exc.status_code, exc.guac_message,
|
||||
)
|
||||
end_reason = "error"
|
||||
error_text = f"{guac_status_text(exc.status_code)}: {exc.guac_message}" if exc.guac_message else guac_status_text(exc.status_code)
|
||||
error_code = exc.status_code
|
||||
except GuacdDisconnectedError as exc:
|
||||
# EOF mitten im Protokoll -- typischerweise ein Absturz von guacd/
|
||||
# FreeRDP. Vorher landete das unbehandelt im generischen
|
||||
# (GuacamoleProtocolError, ConnectionError, OSError)-Zweig als
|
||||
# IncompleteReadError mit rohem Python-Klassennamen im Browser.
|
||||
logger.warning("RDP-Sessionfehler (session_id=%s) [corr=%s]: %s", session_id, correlation_id, exc)
|
||||
end_reason = "error"
|
||||
error_text = "guacd hat die Verbindung unerwartet beendet"
|
||||
error_code = 515
|
||||
except (GuacamoleProtocolError, ConnectionError, OSError) as exc:
|
||||
logger.warning("RDP-Sessionfehler (session_id=%s): %s", session_id, exc)
|
||||
logger.warning("RDP-Sessionfehler (session_id=%s) [corr=%s]: %s", session_id, correlation_id, exc)
|
||||
end_reason = "error"
|
||||
# Grund an den Browser durchreichen (guacamole-common-js zeigt die
|
||||
# Close-Reason als Guacamole.Status.message an, siehe _reject).
|
||||
# 'error'-Instruktion/Close-Reason an, siehe _send_error_instruction).
|
||||
# Betrifft u.a. den haeufigsten Konfigurationsfehler: kein
|
||||
# RDP-Benutzername am Host, siehe build_rdp_params().
|
||||
error_text = str(exc) or exc.__class__.__name__
|
||||
@ -272,9 +490,10 @@ async def rdp_tunnel(
|
||||
# verschluesselten RDP-Passwort) ab, die vorher unbehandelt bis vor
|
||||
# das erste await in dieser Funktion durchriss und die Sitzung ohne
|
||||
# jede Fehlermeldung/Protokollierung sofort beendete.
|
||||
logger.exception("Unerwarteter Fehler in RDP-Sitzung %s: %s", session_id, exc)
|
||||
logger.exception("Unerwarteter Fehler in RDP-Sitzung %s [corr=%s]: %s", session_id, correlation_id, exc)
|
||||
end_reason = "error"
|
||||
error_text = f"Interner Fehler: {exc.__class__.__name__}"
|
||||
error_code = 512
|
||||
finally:
|
||||
active_sessions.unregister(session_id)
|
||||
del password # Klartext-Passwort so schnell wie moeglich freigeben
|
||||
@ -282,8 +501,13 @@ async def rdp_tunnel(
|
||||
task.cancel()
|
||||
if tunnel:
|
||||
await tunnel.close()
|
||||
recorder.close()
|
||||
logger.debug("RDP-Sitzung %s beendet: reason=%s", session_id, end_reason)
|
||||
await recorder.aclose()
|
||||
# E6 (Umsetzungsauftrag Teil E): das je Sitzung umgeleitete
|
||||
# RDP-Laufwerk (siehe build_rdp_params()/rdp_drive_path()) wird beim
|
||||
# Sitzungsende entfernt -- sonst sammeln sich pro Sitzung
|
||||
# Verzeichnisse unbegrenzt auf der Platte an.
|
||||
await _cleanup_rdp_drive_path(host_id, session_id)
|
||||
logger.info("RDP-Sitzung %s beendet [corr=%s]: reason=%s", session_id, correlation_id, end_reason)
|
||||
await conn.execute(
|
||||
"UPDATE sessions SET ended_at = strftime('%Y-%m-%dT%H:%M:%fZ','now'), end_reason = ? "
|
||||
"WHERE id = ?",
|
||||
@ -291,13 +515,15 @@ async def rdp_tunnel(
|
||||
)
|
||||
await write_audit_event(
|
||||
conn, event_type="rdp_session_end", user_id=user.id, client_ip=client_ip,
|
||||
details={"host_id": host_id, "session_id": session_id, "reason": end_reason},
|
||||
details={"host_id": host_id, "session_id": session_id, "reason": end_reason, "corr": correlation_id},
|
||||
)
|
||||
await conn.commit()
|
||||
try:
|
||||
if error_text:
|
||||
full_reason = f"{error_text} (Ref: {correlation_id})"
|
||||
await _send_error_instruction(websocket, full_reason, error_code)
|
||||
await websocket.close(
|
||||
code=4500, reason=error_text.encode("utf-8")[:123].decode("utf-8", errors="ignore")
|
||||
code=4500, reason=full_reason.encode("utf-8")[:123].decode("utf-8", errors="ignore")
|
||||
)
|
||||
else:
|
||||
await websocket.close()
|
||||
|
||||
Reference in New Issue
Block a user