Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/uds/checks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from uds import models
from uds.core import consts, types
from uds.core.checks import AutomaticCheck, CheckOutcome
from uds.core.ui import gui

if typing.TYPE_CHECKING:
from uds.core import auths
Expand Down Expand Up @@ -448,3 +449,66 @@ def run(self) -> CheckOutcome:
True,
_("All authenticators using TLS verify the certificate of the other end."),
)


def _degraded_mode_is_on(transport: models.Transport) -> bool:
transport_type = transport.get_type()
if not isinstance(transport_type.degraded_mode, gui.CheckBoxField):
return bool(transport_type.degraded_mode)
# Not get_instance(): it saves legacy data back to the database, and a check must only read.
instance = transport_type(transport.get_environment(), None)
instance.deserialize(transport.data)
return typing.cast(gui.CheckBoxField, instance.degraded_mode).as_bool()


class TransportConnectivityCheckDisabledCheck(AutomaticCheck):
"""Transports in degraded mode, which skip the reachability check and the pre-connect."""

id: typing.ClassVar[str] = "transport-connectivity-check-disabled"
category: typing.ClassVar[types.checks.CheckCategory] = types.checks.CheckCategory.SECURITY
description: typing.ClassVar[str] = gettext_noop(
"Looks for transports with degraded mode enabled. UDS then hands out connections without checking "
"that the machine is reachable, and does not send the pre-connect, so the connection can fail. "
"Turn it off unless the broker cannot reach the transport endpoints of the machines. Transports "
"whose stored setting raises an error when read are listed too."
)

@typing.override
def run(self) -> CheckOutcome:
disabled: list[models.Transport] = []
unreadable: list[models.Transport] = []
for transport in models.Transport.objects.all().order_by("name"):
try:
if _degraded_mode_is_on(transport):
disabled.append(transport)
except Exception: # Stored data can be corrupt: report that row, keep scanning the others.
unreadable.append(transport)

details = [f"{transport.name} ({transport.uuid})" for transport in disabled] + [
f"{transport.name} ({transport.uuid}): setting could not be read" for transport in unreadable
]
if disabled:
return (
types.checks.CheckSeverity.MEDIUM,
False,
_(
"Degraded mode is enabled on: {names}. UDS does not check that the machine is reachable "
"before handing out the connection, and does not send the pre-connect, so the "
"connection can fail."
).format(names=", ".join(transport.name for transport in disabled)),
details,
)
if unreadable:
return (
types.checks.CheckSeverity.INFO,
False,
_("Could not read the degraded mode setting of: {names}.").format(
names=", ".join(transport.name for transport in unreadable)
),
details,
)
return (
types.checks.CheckSeverity.MEDIUM,
True,
_("No transport has degraded mode enabled."),
)
17 changes: 13 additions & 4 deletions src/uds/core/managers/userservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
Author: Andres Schumann, aschumann at virtualcable dot es
"""

import logging
Expand Down Expand Up @@ -1099,10 +1100,18 @@ def get_user_service_info( # pylint: disable=too-many-locals, too-many-branches
log.log(
userservice, types.log.LogLevel.INFO, "User service ready", types.log.LogSource.WEB
)
self.notify_preconnect(
userservice,
transport_instance.get_connection_info(userservice, user, "", for_notify=True),
)
if transport_instance.is_degraded_mode():
log.log(
userservice,
types.log.LogLevel.INFO,
"Degraded mode: pre-connect skipped",
types.log.LogSource.TRANSPORT,
)
else:
self.notify_preconnect(
userservice,
transport_instance.get_connection_info(userservice, user, "", for_notify=True),
)
trace_logger.info(
'READY on service "%s" for user "%s" with transport "%s" (ip:%s)',
userservice.name,
Expand Down
31 changes: 18 additions & 13 deletions src/uds/core/transports/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
Author: Andres Schumann, aschumann at virtualcable dot es
"""

import collections.abc
Expand Down Expand Up @@ -83,11 +84,11 @@ class Transport(Module):
# For allowing grouping transport on dashboard "new" menu, and maybe other places
group: typing.ClassVar[types.transports.Grouping] = types.transports.Grouping.DIRECT

# Pre-connection reachability check switch. Plain True means "check enabled,
# not configurable". Transports that implement is_ip_reachable() override this
# with fields.allow_ip_check_field() so admins can disable the check (useful
# when the broker has no network visibility of the user service machines).
allow_ip_check: typing.ClassVar[bool | gui.CheckBoxField] = True
# Degraded mode switch. Plain False means "not enabled, not configurable".
# Transports that implement is_ip_reachable() override this with
# fields.degraded_mode_field(): when it is on, the reachability check and the
# pre-connect are skipped, because the broker cannot see the user service machines.
degraded_mode: typing.ClassVar[bool | gui.CheckBoxField] = False

_db_obj: "models.Transport|None" = None

Expand Down Expand Up @@ -140,20 +141,24 @@ def test_connectivity(
) -> bool:
return net.test_connectivity(ip, int(port), timeout)

def is_degraded_mode(self) -> bool:
degraded = self.degraded_mode
if isinstance(degraded, gui.CheckBoxField):
return degraded.as_bool()
return bool(degraded)

@typing.final
def is_ip_allowed(self, userservice: "models.UserService", ip: str) -> bool:
"""
Checks if the transport is available for the requested destination ip.

Common, non-overridable entry point: honors the ``allow_ip_check`` switch
and caches the reachability result for a very short time (both success and
failure), then delegates the real check to :meth:`is_ip_reachable`.
Transports must override :meth:`is_ip_reachable`, never this method.
Common, non-overridable entry point: in degraded mode the destination is not
checked. Otherwise the reachability result is cached for a very short time
(both success and failure), and the real check is delegated to
:meth:`is_ip_reachable`. Transports must override :meth:`is_ip_reachable`,
never this method.
"""
allow = self.allow_ip_check
if isinstance(allow, gui.CheckBoxField):
allow = allow.as_bool()
if not allow:
if self.is_degraded_mode():
return True

cached = self.cache.get(f"reachability:{ip}")
Expand Down
21 changes: 13 additions & 8 deletions src/uds/core/util/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
Author: Andres Schumann, aschumann at virtualcable dot es
"""

import collections.abc
Expand Down Expand Up @@ -54,23 +55,27 @@
# ******************************************************


def allow_ip_check_field(
def degraded_mode_field(
order: int = 99, tab: "types.ui.Tab | None" = types.ui.Tab.ADVANCED
) -> ui.gui.CheckBoxField:
"""Field to let admins disable the transport pre-connection reachability check.
"""Field to let admins enable the degraded mode of a transport.

Transports that check reachability (see ``Transport.is_ip_reachable``) declare
``allow_ip_check = fields.allow_ip_check_field()`` so the check can be turned
off when the broker has no network visibility of the user service machines.
``degraded_mode = fields.degraded_mode_field()``. Enabling it skips the reachability
check and the pre-connect of the transport.
"""
return ui.gui.CheckBoxField(
label=_("Connectivity check"),
label=_("Enable degraded mode"),
order=order,
tooltip=_(
"If enabled, transport will check the connectivity with the user service before "
"allowing the user to connect, reducing possible user connection errors"
"Degraded mode: UDS hands out the connection without checking that the machine is reachable, "
"and does not send the pre-connect to it, so the connection can fail. On RDP, the user is not "
"added to the Remote Desktop Users group, so users who are neither administrators nor already "
"members of that group cannot log in. "
"Use it only to solve incidents, in very specific cases or when support asks for it: "
"enabling it always degrades the system."
),
default=True,
default=False,
tab=tab,
)

Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/HTML5RDP/html5rdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class HTML5RDPTransport(transports.Transport):
tab=types.ui.Tab.ADVANCED,
)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

@typing.override
def initialize(self, values: "types.core.ValuesType") -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/HTML5SSH/html5ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class HTML5SSHTransport(transports.Transport):
tab=types.ui.Tab.PARAMETERS,
)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

@typing.override
def is_ip_reachable(self, userservice: "models.UserService", ip: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/RDP/rdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class RDPTransport(BaseRDPTransport):
mac_use_rdp_file = BaseRDPTransport.mac_use_rdp_file
sign_rdp_file = BaseRDPTransport.sign_rdp_file

allow_ip_check = BaseRDPTransport.allow_ip_check
degraded_mode = BaseRDPTransport.degraded_mode

@typing.override
def initialize(self, values: "types.core.ValuesType") -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/RDP/rdp_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ class BaseRDPTransport(transports.Transport):
default=False,
)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

def check_rdp_can_be_signed(self) -> None:
if not self.sign_rdp_file.as_bool():
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/RDP/rdptunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class TRDPTransport(BaseRDPTransport):
sign_rdp_file = BaseRDPTransport.sign_rdp_file
# optimizeTeams = BaseRDPTransport.optimizeTeams

allow_ip_check = BaseRDPTransport.allow_ip_check
degraded_mode = BaseRDPTransport.degraded_mode

@typing.override
def initialize(self, values: "types.core.ValuesType") -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/RDPEmbedded/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class BaseRDPEmbeddedTransport(transports.Transport):
tab=types.ui.Tab.DISPLAY,
)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

@typing.override
def initialize(self, values: types.core.ValuesType) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/RDPEmbedded/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class RDPEmbeddedTransport(BaseRDPEmbeddedTransport):

screen_size = BaseRDPEmbeddedTransport.screen_size

allow_ip_check = BaseRDPEmbeddedTransport.allow_ip_check
degraded_mode = BaseRDPEmbeddedTransport.degraded_mode

@typing.override
def get_transport_script( # pylint: disable=too-many-locals
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/RDPEmbedded/tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class TRDPEmbeddedTransport(BaseRDPEmbeddedTransport):

screen_size = BaseRDPEmbeddedTransport.screen_size

allow_ip_check = BaseRDPEmbeddedTransport.allow_ip_check
degraded_mode = BaseRDPEmbeddedTransport.degraded_mode

@typing.override
def get_transport_script( # pylint: disable=too-many-locals
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/SPICE/spice.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class SPICETransport(BaseSpiceTransport):
ssl_connection = BaseSpiceTransport.ssl_connection
overrided_proxy = BaseSpiceTransport.overrided_proxy

allow_ip_check = BaseSpiceTransport.allow_ip_check
degraded_mode = BaseSpiceTransport.degraded_mode

@typing.override
def get_transport_script(
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/SPICE/spice_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class BaseSpiceTransport(transports.Transport):
old_field_name="overridedProxy",
)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

@typing.override
def is_ip_reachable(self, userservice: "models.UserService", ip: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/SPICE/spicetunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class TSPICETransport(BaseSpiceTransport):
allow_smartcards = BaseSpiceTransport.allow_smartcards
ssl_connection = BaseSpiceTransport.ssl_connection

allow_ip_check = BaseSpiceTransport.allow_ip_check
degraded_mode = BaseSpiceTransport.degraded_mode

@typing.override
def initialize(self, values: "types.core.ValuesType") -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/UdsRP/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class BaseUdsRPTransport(transports.Transport):

ticket_validity = fields.tunnel_ticket_validity_field(order=99)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

@typing.override
def is_ip_reachable(self, userservice: "models.UserService", ip: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/UdsRP/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class UdsRPTransport(BaseUdsRPTransport):
console_conflict_timeout = BaseUdsRPTransport.console_conflict_timeout
ticket_validity = BaseUdsRPTransport.ticket_validity

allow_ip_check = BaseUdsRPTransport.allow_ip_check
degraded_mode = BaseUdsRPTransport.degraded_mode

@typing.override
def get_transport_script(
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/UdsRP/tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class TUdsRPTransport(BaseUdsRPTransport):
console_conflict_timeout = BaseUdsRPTransport.console_conflict_timeout
ticket_validity = BaseUdsRPTransport.ticket_validity

allow_ip_check = BaseUdsRPTransport.allow_ip_check
degraded_mode = BaseUdsRPTransport.degraded_mode

@typing.override
def get_transport_script(
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/X2GO/x2go.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class X2GOTransport(BaseX2GOTransport):
pack = BaseX2GOTransport.pack
quality = BaseX2GOTransport.quality

allow_ip_check = BaseX2GOTransport.allow_ip_check
degraded_mode = BaseX2GOTransport.degraded_mode

@typing.override
def get_transport_script( # pylint: disable=too-many-locals
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/X2GO/x2go_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class BaseX2GOTransport(transports.Transport):
tab=types.ui.Tab.ADVANCED,
)

allow_ip_check = fields.allow_ip_check_field()
degraded_mode = fields.degraded_mode_field()

@typing.override
def is_ip_reachable(self, userservice: "models.UserService", ip: str) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/uds/transports/X2GO/x2gotunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class TX2GOTransport(BaseX2GOTransport):
pack = BaseX2GOTransport.pack
quality = BaseX2GOTransport.quality

allow_ip_check = BaseX2GOTransport.allow_ip_check
degraded_mode = BaseX2GOTransport.degraded_mode

@typing.override
def initialize(self, values: "types.core.ValuesType") -> None:
Expand Down
1 change: 1 addition & 0 deletions tests/REST/methods/system/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"server-certificates-expiring",
"restrained-service-pools",
"ipauth-proxy-unrestricted",
"transport-connectivity-check-disabled",
# C-family (logs.py)
"failed-logins-24h",
"brute-force-by-ip",
Expand Down
1 change: 1 addition & 0 deletions tests/checks/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"server-certificates-expiring",
"restrained-service-pools",
"ipauth-proxy-unrestricted",
"transport-connectivity-check-disabled",
# C-family (logs.py)
"failed-logins-24h",
"brute-force-by-ip",
Expand Down
Loading
Loading